* gnus-offline.el: Rewrite to work with pGnus v0.74 based T-gnus.
[elisp/gnus.git-] / lisp / gnus-offline.el
1 ;;; gnus-offline.el --- To process mail & news at offline environment.
2 ;;; $Id: gnus-offline.el,v 1.1.2.5.2.28 1999-01-31 23:06:20 yamaoka Exp $
3
4 ;;; Copyright (C) 1998 Tatsuya Ichikawa
5 ;;;                    Yukihiro Ito
6 ;;; Author: Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
7 ;;;         Yukihiro Ito <ito@rs.civil.tohoku.ac.jp>
8 ;;;         Hidekazu Nakamura <u90121@uis-inf.co.jp>
9 ;;;         Tsukamoto Tetsuo <czkmt@remus.dti.ne.jp>
10
11 ;;; Version: 2.10
12 ;;; Keywords: news , mail , offline , gnus
13 ;;;
14 ;;; SPECIAL THANKS
15 ;;;    Keiichi Suzuki <kei-suzu@mail.wbs.or.jp>
16 ;;;    KORIYAMA Naohiro <kory@ba2.so-net.or.jp>
17 ;;;    Katsumi Yamaoka <yamaoka@jpl.org>
18
19 ;;; This file is part of Semi-gnus.
20 ;;;
21 ;;; GNU Emacs is free software; you can redistribute it and/or modify
22 ;;; it under the terms of the GNU General Public License as published by
23 ;;; the Free Software Foundation; either version 2, or (at your option)
24 ;;; any later version.
25
26 ;;; GNU Emacs is distributed in the hope that it will be useful,
27 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
28 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 ;;; GNU General Public License for more details.
30
31 ;;; You should have received a copy of the GNU General Public License
32 ;;; along with GNU Emacs; see the file COPYING.  If not, write to the
33 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
34 ;;; Boston, MA 02111-1307, USA.
35 ;;;
36 ;;;; Commentary:
37 ;;; Note.
38 ;;;   This file works only with after version of Emacs 19.30.
39 ;;;   This file needs miee.el and SEMI.
40 ;;;   If you set gnus-offline-drafts-queue-type to 'agent , you don't need 
41 ;;;   miee.el
42 ;;;   You must use Semi-gnus 6.X.X.
43 ;;;
44 ;;; How to use.
45 ;;;
46 ;;; Add following code at the end in your .emacs
47 ;;;
48 ;;;    (load "gnus-ofsetup")
49 ;;;    (gnus-setup-for-offline)
50 ;;;    (load gnus-offline-setting-file)
51 ;;;
52 ;;; If you use gnus-agent as souper , put gnus-agent setup code in you .gnus.el
53 ;;;
54 ;;; If you use nnspool as souper , put following code in your .emacs before
55 ;;; gnus-offline setting.
56 ;;;
57 ;;; Then , put hang.exe in exec-path directory.
58 ;;;
59 ;;; In Gnus group buffer , type g to get all news and mail.
60 ;;; Then send mail and news in spool directory.
61 ;;;
62 ;;; Security Notice. (This is available before version 2.02)
63 ;;;
64 ;;; You can set the variable gnus-offline-pop-password-file to save your POP
65 ;;; passwords. But TAKE CARE. Use it at your own risk.
66 ;;; If you decide to use it, then write in .emacs or .gnus-offline.el 
67 ;;; something like:
68 ;;;
69 ;;;  (setq gnus-offline-pop-password-file "~/.pop.passwd")
70 ;;;
71 ;;; and write in this file something like:
72 ;;;
73 ;;;  (setq pop3-fma-password
74 ;;;      '(("SERVER1" "ACCOUNT1" "PASSWORD1")
75 ;;;        ("SERVER2" "ACCOUNT2" "PASSWORD2")
76 ;;;        ............................
77 ;;;        ))
78 ;;;
79 ;;; If you want to encode the file with base64, try:
80 ;;;
81 ;;;    M-: (base64-encode-region (point-min) (point-max))
82 ;;;
83 ;;; Variables.
84 ;;;  gnus-offline-dialup-program-arguments
85 ;;;                                   ... List of dialup program arguments.
86 ;;;  gnus-offline-hangup-program-arguments
87 ;;;                                   ... List of hangup program arguments.
88 ;;;  gnus-offline-mail-treat-environ  ... toggle sending mail online/offline.
89 ;;;  gnus-offline-articles-to-fetch   ... toggle fetch articles.
90 ;;;                                        both->mail->news->both...
91 ;;;  gnus-offline-load-hook           ... hook before gnus-offline load.
92 ;;;  gnus-offline-before-online-hook  ... hook before all online jobs.
93 ;;;  gnus-offline-after-online-hook   ... hook after all online jobs.
94 ;;;  gnus-offline-interval-time       ... Interval time to do all online jobs.
95 ;;;                                        (minutes)
96 ;;;  gnus-offline-dialup-function     ... Function to diualup.
97 ;;;  gnus-offline-hangup-function     ... Function to hangup.
98 ;;;  gnus-offline-pop-password-file   ... File to keep the POP password info.
99 ;;;  gnus-offline-pop-password-decoding-function
100 ;;;                                   ... Function to decode the password info.
101
102 ;;; Code:
103
104 (eval '(run-hooks 'gnus-offline-load-hook))
105
106 (require 'cl)
107 (require 'custom)
108 (require 'easymenu)
109
110 (unless (and (condition-case ()
111                  (require 'custom)
112                (file-error nil))
113              (fboundp 'defgroup)
114              (fboundp 'defcustom))
115   (require 'backquote)
116   (defmacro defgroup (&rest args))
117   (defmacro defcustom (symbol value &optional doc &rest args)
118     (` (defvar (, symbol) (, value) (, doc))))
119   )
120 (defgroup gnus-offline nil
121   "Offline backend utility for Gnus."
122   :prefix "gnus-offline-"
123   :group 'mail
124   :group 'news)
125
126 (defconst gnus-offline-version-number "2.10b1")
127 (defconst gnus-offline-codename
128 ;;  "Beta5"                     ; Beta
129 ;;  "This is the time"          ; 2.00
130 ;;  "A matter of trust"
131 ;;  "Modern Woman"
132   "Ahhhhhhh!!"                  ; 2.10b1
133 ;;  "Code of silence"
134   )
135
136 (defconst gnus-offline-version (format "Gnus offline backend utiliy v%s"
137                                        gnus-offline-version-number))
138
139 (defcustom gnus-offline-dialup-program-arguments nil
140   "*Program arguments of gnus-offline-dialup-program."
141   :group 'gnus-offline
142   :type '(repeat (string :tag "Argument")))
143
144 (defcustom gnus-offline-hangup-program-arguments nil
145   "*Program arguments of gnus-offline-hangup-program."
146   :group 'gnus-offline
147   :type '(repeat (string :tag "Argument")))
148
149 (defcustom gnus-offline-auto-hangup t
150   "*Whether dialup-network automatically hang up when all online jobs has done."
151   :group 'gnus-offline
152   :type 'boolean)
153
154 (defcustom gnus-offline-load-hook nil
155   "*Hook to be run after the gnus-offline package has been loaded."
156   :group 'gnus-offline
157   :type 'hook)
158
159 (defcustom gnus-offline-before-online-hook nil
160   "*Hook to be run before all online jobs."
161   :group 'gnus-offline
162   :type 'hook)
163
164 (defcustom gnus-offline-after-online-hook nil
165   "*Hook to be run after all online jobs."
166   :group 'gnus-offline
167   :type 'hook)
168
169 (defcustom gnus-offline-mail-treat-environ 'offline
170   "*If online , gnus-offline send all mail under online environ.
171 If offline , gnus-offline send all mail temporary to spool dir."
172   :group 'gnus-offline
173   :type '(choice (const offline)
174                  (const online)))
175
176 (defcustom gnus-offline-articles-to-fetch 'both
177   "*If both , gnus-offline fetch mail and news articles.
178 If mail , gnus-offline only fetch mail articles.
179  If news , gnus-offline only fetch news articles."
180   :group 'gnus-offline
181   :type '(choice (const both)
182                  (const mail)
183                  (const news)))
184
185 (defcustom gnus-offline-interval-time 0
186   "*Interval time(minutes) to do online jobs.
187 If set to 0 , timer call is disabled."
188   :group 'gnus-offline
189   :type 'integer)
190
191 (defcustom gnus-offline-mail-group-level 1
192   "*Group level for mail group."
193   :group 'gnus-offline
194   :type 'integer)
195
196 (defcustom gnus-offline-after-empting-spool-hook nil
197   "*Hook to be run before empting spool."
198   :group 'gnus-offline
199   :type 'hook)
200
201 (defcustom gnus-offline-before-empting-spool-hook nil
202   "*Hook to be run after empting spool."
203   :group 'gnus-offline
204   :type 'hook)
205
206 (defcustom gnus-offline-dialup-function 'gnus-offline-connect-server
207   "*Function to dialup."
208   :group 'gnus-offline
209   :type 'function)
210
211 (defcustom gnus-offline-hangup-function 'gnus-offline-hangup-line
212   "*Function to hangup."
213   :group 'gnus-offline
214   :type 'function)
215
216 (defcustom gnus-offline-pop-password-file nil
217   "*File name for saving one's POP password information.
218 This variable should be nil if there's some possibility that
219 your passwords be stolen."
220   :group 'gnus-offline
221   :type '(choice (file :tag "File")
222                  (const nil)))
223
224 (defcustom gnus-offline-pop-password-decoding-function 
225   (function (lambda () (base64-decode-region (point-min) (point-max))))
226   "*Function for decoding one's password information.
227 The value has no effect when `gnus-offline-pop-password-file'
228 is nil.
229 This variable might be nil if you don't need to encode your passwords."
230   :group 'gnus-offline
231   :type 'function)
232
233 ;;; Internal variables.
234 (defvar gnus-offline-connected nil
235   "*If value is t , dialup line is connected status.
236 If value is nil , dialup line is disconnected status.")
237
238 (defvar gnus-offline-news-fetch-method nil
239   "*Method to fetch news articles.")
240
241 (defvar gnus-offline-mail-fetch-method nil
242   "*Method to fetch mail articles.")
243
244 (defvar gnus-offline-header-string
245   (format "%s - \"%s\""
246           gnus-offline-version
247           gnus-offline-codename)
248   "*Header string for gnus-offline.")
249
250 (defvar gnus-offline-stored-group-level nil
251   "*Mail Group level before changing.")
252
253 (defvar gnus-offline-movemail-arguments nil
254   "*All command line arguments of exec-directory/movemail.")
255
256 (defvar gnus-offline-mail-source nil
257   "*nnmail-spool-file save variable.")
258
259 ;;; Temporary variable:
260 (defvar string)
261 (defvar hdr)
262 (defvar str)
263 (defvar ver)
264 (defvar passwd)
265 (defvar num)
266 (defvar gnus-offline-error-buffer " *Error*")
267 (defvar gnus-offline-map (make-sparse-keymap))
268
269 ;;; To silence byte compiler
270 (and
271  (fboundp 'eval-when-compile)
272  (eval-when-compile
273    (save-excursion
274      (beginning-of-defun)
275      (eval-region (point-min) (point)))
276    (let (case-fold-search)
277      (mapcar
278       (function
279        (lambda (symbol)
280          (unless (boundp symbol)
281            (make-local-variable symbol)
282            (eval (list 'setq symbol nil)))))
283       '(:group
284         :prefix :type
285         sendmail-to-spool-directory
286         news-spool-request-post-directory
287         nnspool-version
288         nnagent-version
289         msspool-news-server
290         msspool-news-service
291         gnspool-get-news
292         mail-spool-send
293         news-spool-post
294         gnus-agent-handle-level
295         ))
296      (make-local-variable 'byte-compile-warnings)
297      (setq byte-compile-warnings nil))))
298        
299 (put 'gnus-offline-set-unplugged-state 'menu-enable 'gnus-offline-connected)
300 (if (eq system-type 'windows-nt)
301     (define-process-argument-editing "/hang\\.exe\\'"
302       (lambda (x) (general-process-argument-editing-function
303                    x nil t t nil t t))))
304 ;;; Functions
305 ;;
306 ;; Setting up...
307 ;;
308 (defun gnus-offline-setup ()
309   "*Initialize gnus-offline function"
310
311   ;; Load setting file - required.
312   (load gnus-offline-setting-file)
313
314   ;; Menu and keymap
315   (gnus-offline-define-menu-and-key)
316   
317   ;; To transfer Mail/News function.
318   (cond ((eq gnus-offline-mail-treat-environ 'offline)
319          ;; send mail under offline environ.
320          (gnus-offline-set-offline-sendmail-function))
321         ((eq gnus-offline-mail-treat-environ 'online)
322          ;; send mail under offline environ.
323          (gnus-offline-set-online-sendmail-function))))
324 ;;  (add-hook 'gnus-group-mode-hook 'gnus-offline-setup))
325
326 ;;
327 ;; Setting Error check.
328 (defun gnus-offline-error-check ()
329   ;; Check gnus-agent and nnspool setting.
330   (cond ((eq gnus-offline-news-fetch-method 'nnagent)
331          ;; nnagent and gnus-agent loaded ??
332          (if (not (and (featurep 'gnus-agent)
333                        (featurep 'nnagent)))
334              (progn
335                (get-buffer-create gnus-offline-error-buffer)
336                (set-buffer gnus-offline-error-buffer)
337                (erase-buffer)
338                (insert "WARNING!!: gnus-agent.el or nnagent.el is not loaded.\n")
339                (insert "Please check your .emacs or .gnus.el to work gnus-agent fine.")
340                (pop-to-buffer gnus-offline-error-buffer))))
341         
342         ((eq gnus-offline-news-fetch-method 'nnspool)
343          (if (not (featurep 'nnspool))
344              (progn
345                (get-buffer-create gnus-offline-error-buffer)
346                (set-buffer gnus-offline-error-buffer)
347                (erase-buffer)
348                (insert "WARNING!!: nnspool.el is not loaded.\n")
349                (insert "Please check your .emacs or .gnus.el to work nnspool fine.")
350                (pop-to-buffer gnus-offline-error-buffer))))))
351 ;;
352 ;;
353 (defun gnus-offline-set-offline-sendmail-function ()
354   "*Initialize sendmail-function when unplugged status."
355   (if (eq gnus-offline-drafts-queue-type 'miee)
356       (progn
357         (if (eq gnus-offline-news-fetch-method 'nnagent)
358             (setq gnus-agent-send-mail-function 'sendmail-to-spool-in-gnspool-format))
359         (setq message-send-mail-function 'sendmail-to-spool-in-gnspool-format))
360     (setq gnus-agent-send-mail-function (gnus-offline-set-online-sendmail-function)
361           message-send-mail-function 'gnus-agent-send-mail)))
362 ;;
363 (defun gnus-offline-set-online-sendmail-function ()
364   "*Initialize sendmail-function when plugged status."
365   (if (eq gnus-offline-MTA-type 'smtp)
366       (setq message-send-mail-function 'message-send-mail-with-smtp)
367     (setq message-send-mail-function 'message-send-mail-with-sendmail)))
368 ;;
369 (defun gnus-offline-set-offline-post-news-function ()
370   "*Initialize sendnews-function when unplugged status."
371   (if (eq gnus-offline-drafts-queue-type 'miee)
372       (setq message-send-news-function 'gnspool-request-post)))
373 ;;
374 (defun gnus-offline-set-online-post-news-function ()
375   "*Initialize sendnews-function when plugged status."
376   (setq message-send-news-function 'message-send-news-with-gnus))
377 ;;
378 ;; Get new news jobs. (gnus-agent and nnspool)
379 ;;
380 (defun gnus-offline-gnus-get-new-news (&optional arg)
381   "*Override function \"gnus-group-get-new-news\"."
382   (interactive "P")
383   (run-hooks 'gnus-offline-before-online-hook)
384   (if (functionp gnus-offline-dialup-function)
385       (funcall gnus-offline-dialup-function))
386   (gnus-offline-get-new-news-function)
387   (if (not (locate-library "mail-source"))
388       (progn
389         (let (buffer)
390           (unwind-protect
391               (progn
392                 (save-excursion
393                   (or pop3-fma-password
394                       (when gnus-offline-pop-password-file
395                         (setq pop3-fma-save-password-information t)
396                         (setq buffer (get-buffer-create "*offline-temp*"))
397                         (set-buffer buffer)
398                         (erase-buffer)
399                         (insert-file-contents-as-binary gnus-offline-pop-password-file)
400                         (and gnus-offline-pop-password-decoding-function
401                              (funcall gnus-offline-pop-password-decoding-function))
402                         (eval-buffer))))
403                 (gnus-group-get-new-news arg))
404             (when gnus-offline-pop-password-file
405               (setq pop3-fma-password nil)
406               (setq pop3-fma-save-password-information nil)
407               (kill-buffer buffer)))))
408     ;;
409     ;; Use mail-source.el
410     (gnus-group-get-new-news arg)))
411
412 ;;
413 ;; dialup...
414 ;;
415 (defun gnus-offline-connect-server ()
416   "*Dialup function."
417   ;; Dialup if gnus-offline-dialup-program is specified
418   (if (stringp gnus-offline-dialup-program)
419       (progn
420         (message "Dialing ...")
421         (apply 'call-process gnus-offline-dialup-program nil nil nil
422                gnus-offline-dialup-program-arguments)
423         (sleep-for 1)
424         (message "Dialing ... done."))))
425
426 ;;
427 ;; Jobs before get new news , send mail and post news.
428 ;;
429 (defun gnus-offline-get-new-news-function ()
430   "*Prepare to get new news/mail."
431   ;; Set mail group level
432   (if (eq gnus-offline-articles-to-fetch 'mail)
433       (gnus-offline-set-mail-group-level gnus-offline-mail-group-level))
434
435   ;; Set to online environ.
436   (setq gnus-offline-connected t)
437
438   ;; Set send mail/news functions to online functions.
439   (gnus-offline-set-online-sendmail-function)
440   (gnus-offline-set-online-post-news-function)
441   (message "Set to online status.")
442
443   ;; fetch only news
444   (if (eq gnus-offline-articles-to-fetch 'news)
445       (gnus-offline-disable-fetch-mail))
446
447   ;; fetch both mail and news. or Only mail.
448   (gnus-offline-enable-fetch-news)
449   (if (memq gnus-offline-articles-to-fetch '(both mail))
450       (gnus-offline-enable-fetch-mail))
451
452   ;; fetch only mail for gnus-agent
453   (if (and (eq gnus-offline-news-fetch-method 'nnagent)
454            (eq gnus-offline-articles-to-fetch 'mail))
455           (setq gnus-agent-handle-level gnus-offline-mail-group-level)))
456
457 ;;
458 ;; Change mail group level to handle only mail.
459 ;;
460 (defun gnus-offline-set-mail-group-level (level)
461   "*Set nnm* group level."
462   (switch-to-buffer gnus-group-buffer)
463   (goto-char (point-min))
464   
465   ;; Save current level
466   (if (not gnus-offline-stored-group-level)
467       (while (re-search-forward " nnm" nil t)
468         (setq gnus-offline-stored-group-level
469               (append gnus-offline-stored-group-level
470                       (list (gnus-group-group-level)))))
471     (forward-line 1)
472     (beginning-of-line))
473   ;;
474   (goto-char (point-min))
475   (while (re-search-forward " nnm" nil t)
476     (gnus-group-set-current-level 1 level)
477     (forward-line 1)
478     (beginning-of-line))
479   t)
480 ;;
481 ;; Restore mail group level
482 ;;
483 (defun gnus-offline-restore-mail-group-level ()
484   "*Restore nnm* group level."
485   (switch-to-buffer gnus-group-buffer)
486   (goto-char (point-min))
487   (setq num 0)
488   (while (re-search-forward " nnm" nil t)
489     (gnus-group-set-current-level 1 (nth num gnus-offline-stored-group-level))
490     (forward-line 1)
491     (setq num (+ num 1))
492     (beginning-of-line)))
493 ;;
494 ;; Jobs after getting new news.
495 ;;
496 (defun gnus-offline-after-get-new-news ()
497   "*After getting news and mail jobs."
498   (if (memq gnus-offline-articles-to-fetch '(both mail))
499       (progn
500         ;; Mail/both
501         ;; send mail/news in spool
502         (gnus-offline-empting-spool)
503         (if (eq gnus-offline-articles-to-fetch 'mail)
504             (progn
505               ;; Send only mail and hang up...
506               (if (and gnus-offline-connected
507                        gnus-offline-auto-hangup)
508                   (gnus-offline-set-unplugged-state))
509               ;; Disable fetch mail.
510               (gnus-offline-disable-fetch-mail)
511               (gnus-offline-after-jobs-done)))))
512   
513   ;; News/Both
514   (if (memq gnus-offline-articles-to-fetch '(both news))
515       (progn
516         (if gnus-offline-connected
517             (cond ((eq gnus-offline-news-fetch-method 'nnagent)
518                    ;; Get New News (gnus-agent)
519                    (gnus-agent-toggle-plugged t)
520                   
521                    ;; fetch articles
522                    (gnus-agent-fetch-session)
523                   
524                    ;; Hang Up line. then set to offline status.
525                    (if (and gnus-offline-connected
526                             gnus-offline-auto-hangup)
527                        (gnus-offline-set-unplugged-state))
528                    
529                    ;; All online jobs has done.
530                    (gnus-offline-after-jobs-done))
531                   (t
532                    (if (eq gnus-offline-news-fetch-method 'nnspool)
533                        ;; Get New News (nnspool)
534                        (gnspool-get-news))))))))
535 ;;
536 ;; Disable fetch mail
537 ;;
538 (defun gnus-offline-disable-fetch-mail ()
539   "*Set do not fetch mail."
540   (setq nnmail-spool-file nil))
541 ;;
542 ;; Enable fetch mail
543 ;;
544 (defun gnus-offline-enable-fetch-mail ()
545   "*Set to fetch mail."
546   (setq gnus-offline-mail-fetch-method 'nnmail)
547   (if (not (locate-library "mail-source"))
548       (progn
549         (setq nnmail-movemail-program 'pop3-fma-movemail)
550         (setq nnmail-spool-file (append
551                                  pop3-fma-local-spool-file-alist
552                                  (mapcar
553                                   (lambda (spool)
554                                     (car spool))
555                                   pop3-fma-spool-file-alist))))
556     (setq nnmail-spool-file gnus-offline-mail-source)))
557 ;;
558 ;; Enable fetch news
559 ;;
560 (defun gnus-offline-enable-fetch-news ()
561   "*Set to fetch news."
562   (if (eq gnus-offline-news-fetch-method 'nnagent)
563       (progn
564         (setq gnus-agent-handle-level gnus-level-subscribed)
565         (gnus-agent-toggle-plugged t))))
566 \f
567 ;;
568 ;; Add your custom header.
569 ;;
570 (defun gnus-offline-add-custom-header (header string)
571   "*Add X-Gnus-Offline-Backend header to Mail/News message."
572   (let ((delimline
573          (progn (goto-char (point-min))
574                 (re-search-forward
575                  (concat "^" (regexp-quote mail-header-separator) "\n"))
576                 (point-marker))))
577     (goto-char (point-min))
578     (or (re-search-forward (concat "^" header) delimline t)
579         (progn
580           (goto-char delimline)
581           (forward-line -1)
582           (beginning-of-line)
583           (setq hdr (concat header " "))
584           (setq str (concat hdr string))
585           (setq hdr (concat str "\n"))
586           (insert-string hdr)))))
587 ;;
588 ;; Add X-Offline-Backend header.
589 ;;
590 (defun gnus-offline-message-add-header ()
591   "*Add X-Gnus-Offline-Backend header to Mail/News message."
592   (if (eq gnus-offline-mail-treat-environ 'offline)
593       (progn
594         (if (eq gnus-offline-news-fetch-method 'nnagent)
595             (setq ver nnagent-version)
596           (setq ver nnspool-version))
597         (setq str (format "\n                        with %s" ver)
598               string (concat gnus-offline-header-string str))
599         (gnus-offline-add-custom-header "X-Gnus-Offline-Backend:" string))))
600   
601 \f
602 ;;
603 ;; Toggle plugged/unplugged
604 ;;
605 (defun gnus-offline-toggle-plugged (plugged)
606   "*Override function \"Jj\" - gnus-agent-toggle-plugged."
607   (interactive (list (not gnus-offline-connected)))
608   (if plugged
609       (progn
610         (setq gnus-offline-connected plugged)
611         (gnus-agent-toggle-plugged plugged)
612         ;; Set send mail/news function to offline functions.
613         (gnus-offline-set-online-sendmail-function)
614         (gnus-offline-set-online-post-news-function))
615     ;; Set to offline status
616     (gnus-offline-set-unplugged-state)))
617 ;;
618 ;; Function of hang up line.
619 ;;
620 (defun gnus-offline-set-unplugged-state ()
621   "*Set to unplugged state."
622   (interactive)
623   ;; Hang Up Line.
624   (if (functionp gnus-offline-hangup-function)
625       (funcall gnus-offline-hangup-function))
626   (setq gnus-offline-connected nil)
627   (if (eq gnus-offline-news-fetch-method 'nnagent)
628       (gnus-agent-toggle-plugged nil))
629
630   ;; Set send mail/news function to offline functions.
631   (gnus-offline-set-offline-sendmail-function)
632   (gnus-offline-set-offline-post-news-function)
633   ;;
634   (setenv "MAILHOST" nil))
635 ;;
636 ;; Hangup line function 
637 ;;
638 (defun gnus-offline-hangup-line ()
639   "*Hangup line function."
640   (message "Hang up line ... ")
641   (if (stringp gnus-offline-hangup-program)
642       (apply 'start-process "hup" nil gnus-offline-hangup-program
643              gnus-offline-hangup-program-arguments))
644   (message "Hang up line ... done."))
645 ;;
646 ;; Hang Up line routine whe using nnspool
647 ;;
648 (defun gnus-offline-nnspool-hangup-line ()
649   (if (and gnus-offline-connected
650            gnus-offline-auto-hangup)
651       (gnus-offline-set-unplugged-state))
652   (gnus-offline-after-jobs-done))
653 ;;
654 ;; Function of all jobs has done.
655 ;;
656 (defun gnus-offline-after-jobs-done ()
657   "*Jobs after all online jobs."
658   (run-hooks 'gnus-offline-after-online-hook)
659   (if (eq gnus-offline-articles-to-fetch 'mail)
660       (gnus-offline-restore-mail-group-level))
661   (if (eq gnus-offline-news-fetch-method 'nnagent)
662       (or gnus-agent-expire-all
663           (gnus-offline-agent-expire)))
664   (if (and (featurep 'xemacs)
665            (fboundp 'play-sound-file))
666       (ding nil 'drum)
667     (ding))
668   (gnus-group-save-newsrc)
669   (message "All online jobs has done."))
670
671 \f
672 ;;
673 ;; Toggle auto hang up
674 ;;
675 (defun gnus-offline-toggle-auto-hangup ()
676   "*Toggle auto hangup flag."
677   (interactive)
678   (setq string "Auto hang up logic")
679   (if gnus-offline-auto-hangup
680       (progn
681         (setq gnus-offline-auto-hangup nil
682               str "disabled."))
683     (setq gnus-offline-auto-hangup t
684           str "enabled."))
685   (message (format "%s %s" string str)))
686 ;;
687 ;; Toggle offline/online to send mail.
688 ;;
689 (defun gnus-offline-toggle-on/off-send-mail ()
690   "*Toggel online/offline sendmail."
691   (interactive)
692   (if (eq gnus-offline-mail-treat-environ 'offline)
693       (progn
694         ;; Sending mail under online environ.
695         (gnus-offline-set-online-sendmail-function)
696         (setq gnus-offline-mail-treat-environ 'online)
697         (message "Sending mail immidiately."))
698     ;; Sending mail under offline environ.
699     (gnus-offline-set-offline-sendmail-function)
700     (setq gnus-offline-mail-treat-environ 'offline)
701     (message "Sending mail temporary to spool directory.")))
702 ;;
703 ;; Toggle articles to fetch ... both -> mail -> news -> both
704 ;;
705 (defun gnus-offline-toggle-articles-to-fetch ()
706   "*Set articles to fetch... both(Mail/News) -> mail only -> News only -> both"
707   (interactive)
708   (setq string "Articles fetch from server.")
709   (cond ((eq gnus-offline-articles-to-fetch 'both)
710          (setq gnus-offline-articles-to-fetch 'mail
711                str "Only Mail"))
712         ((eq gnus-offline-articles-to-fetch 'mail)
713            (setq gnus-offline-articles-to-fetch 'news
714                  str "Only News"))
715         (t
716          (setq gnus-offline-articles-to-fetch 'both
717                str "Mail/News both")))
718   (message (format "%s %s" string str)))
719 ;;
720 ;; Toggle movemail program pop3.el -> movemail -> pop3.el
721 ;;
722 (defun gnus-offline-toggle-movemail-program ()
723   "*Toggle movemail program movemail -> pop3.el -> movemail ->..."
724   (interactive)
725   (setq string "Set nnmail-movemail-program")
726   (cond ((eq pop3-fma-movemail-type 'lisp)
727          (setq pop3-fma-movemail-type 'exe
728                str "to movemail"))
729         (t
730          (setq pop3-fma-movemail-type 'lisp
731                str "to pop3.el")))
732   (message (format "%s %s" string str)))
733 ;;
734 ;; Send mail and Post news using Miee or gnus-agent.
735 ;;
736 (defun gnus-offline-empting-spool ()
737   "*Send all drafts on queue."
738   (run-hooks 'gnus-offline-before-empting-spool-hook)
739   (if (eq gnus-offline-drafts-queue-type 'miee)
740       ;; Send queued message by miee.el.
741       (progn
742         (if (eq gnus-offline-mail-treat-environ 'offline)
743             (progn
744               (message "Sending mails in spool ...")
745               ;; Using miee to send mail.
746               (mail-spool-send)
747               (message "Sending mails in spool ... done.")))
748         (message "Posting news in spool ...")
749         ;; Using miee to post news.
750         (if (and (not (stringp msspool-news-server))
751                  (not msspool-news-service))
752             (progn
753               (setq msspool-news-server (nth 1 gnus-select-method))
754               (setq msspool-news-service 119)))
755         (news-spool-post)
756         (message "Posting news in spool ... done."))
757     ;; Send queued message by gnus-agent
758     (message "Sending messages in spool ...")
759     (gnus-group-send-drafts)
760     (message "Sending messages in spool ... done."))
761   ;;
762   (run-hooks 'gnus-offline-after-empting-spool-hook))
763 ;;
764 ;; Set interval time
765 ;;
766 (defun gnus-offline-set-interval-time ()
767   "*Set interval time for gnus-daemon."
768   (interactive)
769   (setq gnus-offline-interval-time
770         (string-to-int (read-from-minibuffer
771                         (format "Interval time (now %s minutes) : "
772                                 gnus-offline-interval-time)
773                         nil)))
774   (if (< gnus-offline-interval-time 2)
775       (progn
776         (message "Retrieving message logic by timer is disabled.")
777         (setq gnus-offline-interval-time 0))
778     (message (format "Interval time set to %d minutes" gnus-offline-interval-time)))
779   (gnus-offline-processed-by-timer))
780 ;;
781 ;; Expire articles using gnus-agent.
782 ;;
783 (defun gnus-offline-agent-expire ()
784   "*Expire expirable article on News group."
785   (interactive)
786   (gnus-agent-expire))
787 ;;
788 ;; Menu.
789 ;;
790 (defun gnus-offline-define-menu-and-key ()
791   "*Set key and menu."
792   (if (eq gnus-offline-drafts-queue-type 'miee)
793       (if (featurep 'xemacs)
794           (add-hook 'gnus-group-mode-hook 'gnus-offline-define-menu-on-miee)
795         (gnus-offline-define-menu-on-miee))
796     (add-hook 'gnus-group-mode-hook 'gnus-offline-define-menu-on-agent))
797   (add-hook 'gnus-group-mode-hook
798             '(lambda ()
799                (local-set-key "\C-coh" 'gnus-offline-set-unplugged-state)
800                (if (not (locate-library "mail-source"))
801                    (local-set-key "\C-com" 'gnus-offline-toggle-movemail-program))
802                (local-set-key "\C-cof" 'gnus-offline-toggle-articles-to-fetch)
803                (local-set-key "\C-coo" 'gnus-offline-toggle-on/off-send-mail)
804                (local-set-key "\C-cox" 'gnus-offline-toggle-auto-hangup)
805                (local-set-key "\C-cos" 'gnus-offline-set-interval-time)
806                (substitute-key-definition
807                 'gnus-group-get-new-news 'gnus-offline-gnus-get-new-news
808                 gnus-group-mode-map)
809                (if (eq gnus-offline-news-fetch-method 'nnagent)
810                    (progn
811                      (substitute-key-definition
812                       'gnus-agent-toggle-plugged 'gnus-offline-toggle-plugged
813                       gnus-agent-group-mode-map)
814                      (local-set-key "\C-coe" 'gnus-offline-agent-expire)))
815                (or (featurep 'xemacs)
816                    (define-key gnus-group-mode-map 
817                      (if (eq system-type 'windows-nt) [S-mouse-2] [mouse-3])
818                      'gnus-offline-popup-menu))))
819   (if (eq gnus-offline-news-fetch-method 'nnagent)
820       (add-hook 'gnus-summary-mode-hook
821                 '(lambda ()
822                    (substitute-key-definition
823                     'gnus-agent-toggle-plugged 'gnus-offline-toggle-plugged
824                     gnus-agent-summary-mode-map))))
825   (if (featurep 'xemacs)
826       ;; Overwrite the toolbar spec for gnus-group-mode.
827       (add-hook 'gnus-startup-hook
828                 (lambda ()
829                   (let ((i 0) (stat t) but)
830                     (while (and stat (setq but (nth i gnus-group-toolbar)))
831                       (and (equal 'gnus-group-get-new-news (aref but 1))
832                            (aset but 1 'gnus-offline-gnus-get-new-news)
833                            (setq stat nil))
834                       (setq i (1+ i))))))))
835 ;;
836 ;;
837 (defun gnus-offline-define-menu-on-miee ()
838   "*Set and change menu bar on MIEE menu."
839   (let ((menu
840   (if (featurep 'meadow)
841       (easy-menu-change
842        nil
843        "Miee"
844        '(
845          ["Spool \e$B$K$"$k5-;v$NAw?.\e(B" news-spool-post t]
846          ["Spool \e$B$K$"$k\e(B Mail \e$B$NAw?.\e(B" mail-spool-send t]
847          "----"
848          ["Offline \e$B>uBV$X\e(B" message-offline-state (not message-offline-state)]
849          ["Online \e$B>uBV$X\e(B" message-online-state message-offline-state]
850          "----"
851          ("Gnus Offline"
852           ["movemail \e$B$N@ZBX$(\e(B" gnus-offline-toggle-movemail-program
853            (not (locate-library "mail-source"))]
854           ["\e$B<hF@5-;v<oN`$NJQ99\e(B" gnus-offline-toggle-articles-to-fetch t]
855           ["Mail \e$BAw?.J}K!\e(B(On/Off)\e$B$N@ZBX$(\e(B" gnus-offline-toggle-on/off-send-mail t]
856           ["\e$B<+F0@ZCG$N@ZBX$(\e(B" gnus-offline-toggle-auto-hangup t]
857           "----"
858           ["\e$B<hF@:Q5-;v$r>C$9\e(B" gnus-offline-agent-expire (eq gnus-offline-news-fetch-method 'nnagent)]
859           ["\e$B5-;v<hF@4V3V;~4V$N@_Dj\e(B" gnus-offline-set-interval-time t]
860           "----"
861           ["\e$B2s@~$N@ZCG\e(B" gnus-offline-set-unplugged-state gnus-offline-connected])
862          ))
863     (easy-menu-change
864      nil
865      "Miee"
866      '(
867        ["Post news in spool" news-spool-post t]
868        ["Send mails in spool" mail-spool-send t]
869        "----"
870        ["Message Offline" message-offline-state (not message-offline-state)]
871        ["Message Online" message-online-state message-offline-state]
872        "----"
873        ("Gnus Offline"
874         ["Toggle movemail program" gnus-offline-toggle-movemail-program
875          (not (locate-library "mail-source"))]
876         ["Toggle articles to fetch" gnus-offline-toggle-articles-to-fetch t]
877         ["Toggle online/offline send mail" gnus-offline-toggle-on/off-send-mail t]
878         ["Toggle auto hangup" gnus-offline-toggle-auto-hangup t]
879         "----"
880         ["Expire articles" gnus-offline-agent-expire (eq gnus-offline-news-fetch-method 'nnagent)]
881         ["Set interval time" gnus-offline-set-interval-time t]
882         "----"
883         ["Hang up Line." gnus-offline-set-unplugged-state gnus-offline-connected]
884         ))))))
885   (and (featurep 'xemacs)
886        (easy-menu-add menu))))
887 ;;
888 ;; define menu without miee.
889 ;;
890 (defun gnus-offline-define-menu-on-agent ()
891   "*Set menu bar on OFFLINE menu."
892   (easy-menu-define 
893    gnus-offline-menu-on-agent
894    gnus-group-mode-map
895    "Gnus offline Menu"
896    (if (featurep 'meadow)
897        '("Offline"
898          ["movemail \e$B$N@ZBX$(\e(B" gnus-offline-toggle-movemail-program
899           (not (locate-library "mail-source"))]
900          ["\e$B<hF@5-;v<oN`$NJQ99\e(B" gnus-offline-toggle-articles-to-fetch t]
901          ["Mail \e$BAw?.J}K!\e(B(On/Off)\e$B$N@ZBX$(\e(B" gnus-offline-toggle-on/off-send-mail t]
902          ["\e$B<+F0@ZCG$N@ZBX$(\e(B" gnus-offline-toggle-auto-hangup t]
903          "----"
904          ["\e$B<hF@:Q5-;v$r>C$9\e(B" gnus-offline-agent-expire (eq gnus-offline-news-fetch-method 'nnagent)]
905          ["\e$B5-;v<hF@4V3V;~4V$N@_Dj\e(B" gnus-offline-set-interval-time t]
906          "----"
907          ["\e$B2s@~$N@ZCG\e(B" gnus-offline-set-unplugged-state gnus-offline-connected])
908      '("Offline"
909        ["Toggle movemail program" gnus-offline-toggle-movemail-program
910         (not (locate-library "mail-source"))]
911        ["Toggle articles to fetch" gnus-offline-toggle-articles-to-fetch t]
912        ["Toggle online/offline send mail" gnus-offline-toggle-on/off-send-mail t]
913        ["Toggle auto hangup" gnus-offline-toggle-auto-hangup t]
914        "----"
915        ["Expire articles" gnus-offline-agent-expire (eq gnus-offline-news-fetch-method 'nnagent)]
916        ["Set interval time" gnus-offline-set-interval-time t]
917        "----"
918        ["Hang up Line." gnus-offline-set-unplugged-state gnus-offline-connected])))
919   (and (featurep 'xemacs)
920        (easy-menu-add gnus-offline-menu-on-agent)))
921 ;;
922 ;; Popup menu within the group buffer (under Emacs).
923 ;;
924 (defun gnus-offline-popup-menu (event)
925   "Popup menu for Gnus offline."
926   (interactive "e")
927   (let* ((menu (if (boundp 'miee-popup-menu)
928                    (or (assoc 'keymap
929                               (assoc 'Miee (assoc 'menu-bar global-map)))
930                        miee-popup-menu)
931                  gnus-offline-menu-on-agent))
932          (pop (x-popup-menu t menu))
933          (func (and pop (lookup-key menu (apply 'vector pop)))))
934     (and pop func (funcall func))))
935 \f
936 ;;
937 ;; Timer Function
938 (defun gnus-offline-processed-by-timer ()
939   "*Set timer interval."
940   (if (and (> gnus-offline-interval-time 0)
941            (not gnus-offline-connected))
942       ;; Timer call
943       (gnus-demon-add-handler 'gnus-offline-gnus-get-new-news
944                               gnus-offline-interval-time
945                               gnus-offline-interval-time))
946   (if (= gnus-offline-interval-time 0)
947       (gnus-demon-remove-handler 'gnus-offline-gnus-get-new-news t)))
948 ;;
949 ;;
950 (provide 'gnus-offline)
951 ;;; gnus-offline.el ends here