lisp/gnus-offline.el: Add security notice document by Tetsuya Tsukamoto
[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.27 1999-01-26 02:35:37 ichikawa 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.02
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.
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 'pop3-fma)
109 (require 'easymenu)
110
111 (unless (and (condition-case ()
112                  (require 'custom)
113                (file-error nil))
114              (fboundp 'defgroup)
115              (fboundp 'defcustom))
116   (require 'backquote)
117   (defmacro defgroup (&rest args))
118   (defmacro defcustom (symbol value &optional doc &rest args)
119     (` (defvar (, symbol) (, value) (, doc))))
120   )
121 (defgroup gnus-offline nil
122   "Offline backend utility for Gnus."
123   :prefix "gnus-offline-"
124   :group 'mail
125   :group 'news)
126
127 (defconst gnus-offline-version-number "2.02")
128 (defconst gnus-offline-codename
129 ;;  "Beta5"                     ; Beta
130 ;;  "This is the time"          ; 2.00
131 ;;  "A matter of trust"
132   "Modern Woman"
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 ;;; Temporary variable:
257 (defvar string)
258 (defvar hdr)
259 (defvar str)
260 (defvar ver)
261 (defvar passwd)
262 (defvar num)
263 (defvar gnus-offline-error-buffer " *Error*")
264 (defvar gnus-offline-map (make-sparse-keymap))
265
266 ;;; To silence byte compiler
267 (and
268  (fboundp 'eval-when-compile)
269  (eval-when-compile
270    (save-excursion
271      (beginning-of-defun)
272      (eval-region (point-min) (point)))
273    (let (case-fold-search)
274      (mapcar
275       (function
276        (lambda (symbol)
277          (unless (boundp symbol)
278            (make-local-variable symbol)
279            (eval (list 'setq symbol nil)))))
280       '(:group
281         :prefix :type
282         sendmail-to-spool-directory
283         news-spool-request-post-directory
284         nnspool-version
285         nnagent-version
286         msspool-news-server
287         msspool-news-service
288         gnspool-get-news
289         mail-spool-send
290         news-spool-post
291         gnus-agent-handle-level
292         ))
293      (make-local-variable 'byte-compile-warnings)
294      (setq byte-compile-warnings nil))))
295        
296 (put 'gnus-offline-set-unplugged-state 'menu-enable 'gnus-offline-connected)
297 (if (eq system-type 'windows-nt)
298     (define-process-argument-editing "/hang\\.exe\\'"
299       (lambda (x) (general-process-argument-editing-function
300                    x nil t t nil t t))))
301 ;;; Functions
302 ;;
303 ;; Setting up...
304 ;;
305 (defun gnus-offline-setup ()
306   "*Initialize gnus-offline function"
307
308   ;; Load setting file - required.
309   (load gnus-offline-setting-file)
310
311   ;; Menu and keymap
312   (gnus-offline-define-menu-and-key)
313   
314   ;; To transfer Mail/News function.
315   (cond ((eq gnus-offline-mail-treat-environ 'offline)
316          ;; send mail under offline environ.
317          (gnus-offline-set-offline-sendmail-function))
318         ((eq gnus-offline-mail-treat-environ 'online)
319          ;; send mail under offline environ.
320          (gnus-offline-set-online-sendmail-function))))
321 ;;  (add-hook 'gnus-group-mode-hook 'gnus-offline-setup))
322
323 ;;
324 ;; Setting Error check.
325 (defun gnus-offline-error-check ()
326   ;; Check gnus-agent and nnspool setting.
327   (cond ((eq gnus-offline-news-fetch-method 'nnagent)
328          ;; nnagent and gnus-agent loaded ??
329          (if (not (and (featurep 'gnus-agent)
330                        (featurep 'nnagent)))
331              (progn
332                (get-buffer-create gnus-offline-error-buffer)
333                (set-buffer gnus-offline-error-buffer)
334                (erase-buffer)
335                (insert "WARNING!!: gnus-agent.el or nnagent.el is not loaded.\n")
336                (insert "Please check your .emacs or .gnus.el to work gnus-agent fine.")
337                (pop-to-buffer gnus-offline-error-buffer))))
338         
339         ((eq gnus-offline-news-fetch-method 'nnspool)
340          (if (not (featurep 'nnspool))
341              (progn
342                (get-buffer-create gnus-offline-error-buffer)
343                (set-buffer gnus-offline-error-buffer)
344                (erase-buffer)
345                (insert "WARNING!!: nnspool.el is not loaded.\n")
346                (insert "Please check your .emacs or .gnus.el to work nnspool fine.")
347                (pop-to-buffer gnus-offline-error-buffer))))))
348 ;;
349 ;;
350 (defun gnus-offline-set-offline-sendmail-function ()
351   "*Initialize sendmail-function when unplugged status."
352   (if (eq gnus-offline-drafts-queue-type 'miee)
353       (progn
354         (if (eq gnus-offline-news-fetch-method 'nnagent)
355             (setq gnus-agent-send-mail-function 'sendmail-to-spool-in-gnspool-format))
356         (setq message-send-mail-function 'sendmail-to-spool-in-gnspool-format))
357     (setq gnus-agent-send-mail-function (gnus-offline-set-online-sendmail-function)
358           message-send-mail-function 'gnus-agent-send-mail)))
359 ;;
360 (defun gnus-offline-set-online-sendmail-function ()
361   "*Initialize sendmail-function when plugged status."
362   (if (eq gnus-offline-MTA-type 'smtp)
363       (setq message-send-mail-function 'message-send-mail-with-smtp)
364     (setq message-send-mail-function 'message-send-mail-with-sendmail)))
365 ;;
366 (defun gnus-offline-set-offline-post-news-function ()
367   "*Initialize sendnews-function when unplugged status."
368   (if (eq gnus-offline-drafts-queue-type 'miee)
369       (setq message-send-news-function 'gnspool-request-post)))
370 ;;
371 (defun gnus-offline-set-online-post-news-function ()
372   "*Initialize sendnews-function when plugged status."
373   (setq message-send-news-function 'message-send-news-with-gnus))
374 ;;
375 ;; Get new news jobs. (gnus-agent and nnspool)
376 ;;
377 (defun gnus-offline-gnus-get-new-news (&optional arg)
378   "*Override function \"gnus-group-get-new-news\"."
379   (interactive "P")
380   (run-hooks 'gnus-offline-before-online-hook)
381   (if (functionp gnus-offline-dialup-function)
382       (funcall gnus-offline-dialup-function))
383   (gnus-offline-get-new-news-function)
384   (let (buffer)
385     (unwind-protect
386         (progn
387           (save-excursion
388             (or pop3-fma-password
389                 (when gnus-offline-pop-password-file
390                   (setq pop3-fma-save-password-information t)
391                   (setq buffer (get-buffer-create "*offline-temp*"))
392                   (set-buffer buffer)
393                   (erase-buffer)
394                   (insert-file-contents-as-binary gnus-offline-pop-password-file)
395                   (and gnus-offline-pop-password-decoding-function
396                        (funcall gnus-offline-pop-password-decoding-function))
397                   (eval-buffer))))
398           (gnus-group-get-new-news arg))
399       (when gnus-offline-pop-password-file
400         (setq pop3-fma-password nil)
401         (setq pop3-fma-save-password-information nil)
402         (kill-buffer buffer)))))
403
404 ;;
405 ;; dialup...
406 ;;
407 (defun gnus-offline-connect-server ()
408   "*Dialup function."
409   ;; Dialup if gnus-offline-dialup-program is specified
410   (if (stringp gnus-offline-dialup-program)
411       (progn
412         (message "Dialing ...")
413         (apply 'call-process gnus-offline-dialup-program nil nil nil
414                gnus-offline-dialup-program-arguments)
415         (sleep-for 1)
416         (message "Dialing ... done."))))
417
418 ;;
419 ;; Jobs before get new news , send mail and post news.
420 ;;
421 (defun gnus-offline-get-new-news-function ()
422   "*Prepare to get new news/mail."
423   ;; Set mail group level
424   (if (eq gnus-offline-articles-to-fetch 'mail)
425       (gnus-offline-set-mail-group-level gnus-offline-mail-group-level))
426
427   ;; Set to online environ.
428   (setq gnus-offline-connected t)
429
430   ;; Set send mail/news functions to online functions.
431   (gnus-offline-set-online-sendmail-function)
432   (gnus-offline-set-online-post-news-function)
433   (message "Set to online status.")
434
435   ;; fetch only news
436   (if (eq gnus-offline-articles-to-fetch 'news)
437       (gnus-offline-disable-fetch-mail))
438
439   ;; fetch both mail and news. or Only mail.
440   (gnus-offline-enable-fetch-news)
441   (if (memq gnus-offline-articles-to-fetch '(both mail))
442       (gnus-offline-enable-fetch-mail))
443
444   ;; fetch only mail for gnus-agent
445   (if (and (eq gnus-offline-news-fetch-method 'nnagent)
446            (eq gnus-offline-articles-to-fetch 'mail))
447           (setq gnus-agent-handle-level gnus-offline-mail-group-level)))
448
449 ;;
450 ;; Change mail group level to handle only mail.
451 ;;
452 (defun gnus-offline-set-mail-group-level (level)
453   "*Set nnm* group level."
454   (switch-to-buffer gnus-group-buffer)
455   (goto-char (point-min))
456   
457   ;; Save current level
458   (if (not gnus-offline-stored-group-level)
459       (while (re-search-forward " nnm" nil t)
460         (setq gnus-offline-stored-group-level
461               (append gnus-offline-stored-group-level
462                       (list (gnus-group-group-level)))))
463     (forward-line 1)
464     (beginning-of-line))
465   ;;
466   (goto-char (point-min))
467   (while (re-search-forward " nnm" nil t)
468     (gnus-group-set-current-level 1 level)
469     (forward-line 1)
470     (beginning-of-line))
471   t)
472 ;;
473 ;; Restore mail group level
474 ;;
475 (defun gnus-offline-restore-mail-group-level ()
476   "*Restore nnm* group level."
477   (switch-to-buffer gnus-group-buffer)
478   (goto-char (point-min))
479   (setq num 0)
480   (while (re-search-forward " nnm" nil t)
481     (gnus-group-set-current-level 1 (nth num gnus-offline-stored-group-level))
482     (forward-line 1)
483     (setq num (+ num 1))
484     (beginning-of-line)))
485 ;;
486 ;; Jobs after getting new news.
487 ;;
488 (defun gnus-offline-after-get-new-news ()
489   "*After getting news and mail jobs."
490   (if (memq gnus-offline-articles-to-fetch '(both mail))
491       (progn
492         ;; Mail/both
493         ;; send mail/news in spool
494         (gnus-offline-empting-spool)
495         (if (eq gnus-offline-articles-to-fetch 'mail)
496             (progn
497               ;; Send only mail and hang up...
498               (if (and gnus-offline-connected
499                        gnus-offline-auto-hangup)
500                   (gnus-offline-set-unplugged-state))
501               ;; Disable fetch mail.
502               (gnus-offline-disable-fetch-mail)
503               (gnus-offline-after-jobs-done)))))
504   
505   ;; News/Both
506   (if (memq gnus-offline-articles-to-fetch '(both news))
507       (progn
508         (if gnus-offline-connected
509             (cond ((eq gnus-offline-news-fetch-method 'nnagent)
510                    ;; Get New News (gnus-agent)
511                    (gnus-agent-toggle-plugged t)
512                   
513                    ;; fetch articles
514                    (gnus-agent-fetch-session)
515                   
516                    ;; Hang Up line. then set to offline status.
517                    (if (and gnus-offline-connected
518                             gnus-offline-auto-hangup)
519                        (gnus-offline-set-unplugged-state))
520                    
521                    ;; All online jobs has done.
522                    (gnus-offline-after-jobs-done))
523                   (t
524                    (if (eq gnus-offline-news-fetch-method 'nnspool)
525                        ;; Get New News (nnspool)
526                        (gnspool-get-news))))))))
527 ;;
528 ;; Disable fetch mail
529 ;;
530 (defun gnus-offline-disable-fetch-mail ()
531   "*Set do not fetch mail."
532   (setq nnmail-spool-file nil))
533 ;;
534 ;; Enable fetch mail
535 ;;
536 (defun gnus-offline-enable-fetch-mail ()
537   "*Set to fetch mail."
538   (setq gnus-offline-mail-fetch-method 'nnmail)
539   (setq nnmail-movemail-program 'pop3-fma-movemail)
540   (setq nnmail-spool-file (append
541                            pop3-fma-local-spool-file-alist
542                            (mapcar
543                             (lambda (spool)
544                               (car spool))
545                             pop3-fma-spool-file-alist))))
546 ;;
547 ;; Enable fetch news
548 ;;
549 (defun gnus-offline-enable-fetch-news ()
550   "*Set to fetch news."
551   (if (eq gnus-offline-news-fetch-method 'nnagent)
552       (progn
553         (setq gnus-agent-handle-level gnus-level-subscribed)
554         (gnus-agent-toggle-plugged t))))
555 \f
556 ;;
557 ;; Add your custom header.
558 ;;
559 (defun gnus-offline-add-custom-header (header string)
560   "*Add X-Gnus-Offline-Backend header to Mail/News message."
561   (let ((delimline
562          (progn (goto-char (point-min))
563                 (re-search-forward
564                  (concat "^" (regexp-quote mail-header-separator) "\n"))
565                 (point-marker))))
566     (goto-char (point-min))
567     (or (re-search-forward (concat "^" header) delimline t)
568         (progn
569           (goto-char delimline)
570           (forward-line -1)
571           (beginning-of-line)
572           (setq hdr (concat header " "))
573           (setq str (concat hdr string))
574           (setq hdr (concat str "\n"))
575           (insert-string hdr)))))
576 ;;
577 ;; Add X-Offline-Backend header.
578 ;;
579 (defun gnus-offline-message-add-header ()
580   "*Add X-Gnus-Offline-Backend header to Mail/News message."
581   (if (eq gnus-offline-mail-treat-environ 'offline)
582       (progn
583         (if (eq gnus-offline-news-fetch-method 'nnagent)
584             (setq ver nnagent-version)
585           (setq ver nnspool-version))
586         (setq str (format "\n                        with %s" ver)
587               string (concat gnus-offline-header-string str))
588         (gnus-offline-add-custom-header "X-Gnus-Offline-Backend:" string))))
589   
590 \f
591 ;;
592 ;; Toggle plugged/unplugged
593 ;;
594 (defun gnus-offline-toggle-plugged (plugged)
595   "*Override function \"Jj\" - gnus-agent-toggle-plugged."
596   (interactive (list (not gnus-offline-connected)))
597   (if plugged
598       (progn
599         (setq gnus-offline-connected plugged)
600         (gnus-agent-toggle-plugged plugged)
601         ;; Set send mail/news function to offline functions.
602         (gnus-offline-set-online-sendmail-function)
603         (gnus-offline-set-online-post-news-function))
604     ;; Set to offline status
605     (gnus-offline-set-unplugged-state)))
606 ;;
607 ;; Function of hang up line.
608 ;;
609 (defun gnus-offline-set-unplugged-state ()
610   "*Set to unplugged state."
611   (interactive)
612   ;; Hang Up Line.
613   (if (functionp gnus-offline-hangup-function)
614       (funcall gnus-offline-hangup-function))
615   (setq gnus-offline-connected nil)
616   (if (eq gnus-offline-news-fetch-method 'nnagent)
617       (gnus-agent-toggle-plugged nil))
618
619   ;; Set send mail/news function to offline functions.
620   (gnus-offline-set-offline-sendmail-function)
621   (gnus-offline-set-offline-post-news-function)
622   ;;
623   (setenv "MAILHOST" nil))
624 ;;
625 ;; Hangup line function 
626 ;;
627 (defun gnus-offline-hangup-line ()
628   "*Hangup line function."
629   (message "Hang up line ... ")
630   (if (stringp gnus-offline-hangup-program)
631       (apply 'start-process "hup" nil gnus-offline-hangup-program
632              gnus-offline-hangup-program-arguments))
633   (message "Hang up line ... done."))
634 ;;
635 ;; Hang Up line routine whe using nnspool
636 ;;
637 (defun gnus-offline-nnspool-hangup-line ()
638   (if (and gnus-offline-connected
639            gnus-offline-auto-hangup)
640       (gnus-offline-set-unplugged-state))
641   (gnus-offline-after-jobs-done))
642 ;;
643 ;; Function of all jobs has done.
644 ;;
645 (defun gnus-offline-after-jobs-done ()
646   "*Jobs after all online jobs."
647   (run-hooks 'gnus-offline-after-online-hook)
648   (if (eq gnus-offline-articles-to-fetch 'mail)
649       (gnus-offline-restore-mail-group-level))
650   (if (eq gnus-offline-news-fetch-method 'nnagent)
651       (or gnus-agent-expire-all
652           (gnus-offline-agent-expire)))
653   (if (and (featurep 'xemacs)
654            (fboundp 'play-sound-file))
655       (ding nil 'drum)
656     (ding))
657   (gnus-group-save-newsrc)
658   (message "All online jobs has done."))
659
660 \f
661 ;;
662 ;; Toggle auto hang up
663 ;;
664 (defun gnus-offline-toggle-auto-hangup ()
665   "*Toggle auto hangup flag."
666   (interactive)
667   (setq string "Auto hang up logic")
668   (if gnus-offline-auto-hangup
669       (progn
670         (setq gnus-offline-auto-hangup nil
671               str "disabled."))
672     (setq gnus-offline-auto-hangup t
673           str "enabled."))
674   (message (format "%s %s" string str)))
675 ;;
676 ;; Toggle offline/online to send mail.
677 ;;
678 (defun gnus-offline-toggle-on/off-send-mail ()
679   "*Toggel online/offline sendmail."
680   (interactive)
681   (if (eq gnus-offline-mail-treat-environ 'offline)
682       (progn
683         ;; Sending mail under online environ.
684         (gnus-offline-set-online-sendmail-function)
685         (setq gnus-offline-mail-treat-environ 'online)
686         (message "Sending mail immidiately."))
687     ;; Sending mail under offline environ.
688     (gnus-offline-set-offline-sendmail-function)
689     (setq gnus-offline-mail-treat-environ 'offline)
690     (message "Sending mail temporary to spool directory.")))
691 ;;
692 ;; Toggle articles to fetch ... both -> mail -> news -> both
693 ;;
694 (defun gnus-offline-toggle-articles-to-fetch ()
695   "*Set articles to fetch... both(Mail/News) -> mail only -> News only -> both"
696   (interactive)
697   (setq string "Articles fetch from server.")
698   (cond ((eq gnus-offline-articles-to-fetch 'both)
699          (setq gnus-offline-articles-to-fetch 'mail
700                str "Only Mail"))
701         ((eq gnus-offline-articles-to-fetch 'mail)
702            (setq gnus-offline-articles-to-fetch 'news
703                  str "Only News"))
704         (t
705          (setq gnus-offline-articles-to-fetch 'both
706                str "Mail/News both")))
707   (message (format "%s %s" string str)))
708 ;;
709 ;; Toggle movemail program pop3.el -> movemail -> pop3.el
710 ;;
711 (defun gnus-offline-toggle-movemail-program ()
712   "*Toggle movemail program movemail -> pop3.el -> movemail ->..."
713   (interactive)
714   (setq string "Set nnmail-movemail-program")
715   (cond ((eq pop3-fma-movemail-type 'lisp)
716          (setq pop3-fma-movemail-type 'exe
717                str "to movemail"))
718         (t
719          (setq pop3-fma-movemail-type 'lisp
720                str "to pop3.el")))
721   (message (format "%s %s" string str)))
722 ;;
723 ;; Send mail and Post news using Miee or gnus-agent.
724 ;;
725 (defun gnus-offline-empting-spool ()
726   "*Send all drafts on queue."
727   (run-hooks 'gnus-offline-before-empting-spool-hook)
728   (if (eq gnus-offline-drafts-queue-type 'miee)
729       ;; Send queued message by miee.el.
730       (progn
731         (if (eq gnus-offline-mail-treat-environ 'offline)
732             (progn
733               (message "Sending mails in spool ...")
734               ;; Using miee to send mail.
735               (mail-spool-send)
736               (message "Sending mails in spool ... done.")))
737         (message "Posting news in spool ...")
738         ;; Using miee to post news.
739         (if (and (not (stringp msspool-news-server))
740                  (not msspool-news-service))
741             (progn
742               (setq msspool-news-server (nth 1 gnus-select-method))
743               (setq msspool-news-service 119)))
744         (news-spool-post)
745         (message "Posting news in spool ... done."))
746     ;; Send queued message by gnus-agent
747     (message "Sending messages in spool ...")
748     (gnus-group-send-drafts)
749     (message "Sending messages in spool ... done."))
750   ;;
751   (run-hooks 'gnus-offline-after-empting-spool-hook))
752 ;;
753 ;; Set interval time
754 ;;
755 (defun gnus-offline-set-interval-time ()
756   "*Set interval time for gnus-daemon."
757   (interactive)
758   (setq gnus-offline-interval-time
759         (string-to-int (read-from-minibuffer
760                         (format "Interval time (now %s minutes) : "
761                                 gnus-offline-interval-time)
762                         nil)))
763   (if (< gnus-offline-interval-time 2)
764       (progn
765         (message "Retrieving message logic by timer is disabled.")
766         (setq gnus-offline-interval-time 0))
767     (message (format "Interval time set to %d minutes" gnus-offline-interval-time)))
768   (gnus-offline-processed-by-timer))
769 ;;
770 ;; Expire articles using gnus-agent.
771 ;;
772 (defun gnus-offline-agent-expire ()
773   "*Expire expirable article on News group."
774   (interactive)
775   (gnus-agent-expire))
776 ;;
777 ;; Menu.
778 ;;
779 (defun gnus-offline-define-menu-and-key ()
780   "*Set key and menu."
781   (if (eq gnus-offline-drafts-queue-type 'miee)
782       (if (featurep 'xemacs)
783           (add-hook 'gnus-group-mode-hook 'gnus-offline-define-menu-on-miee)
784         (gnus-offline-define-menu-on-miee))
785     (add-hook 'gnus-group-mode-hook 'gnus-offline-define-menu-on-agent))
786   (add-hook 'gnus-group-mode-hook
787             '(lambda ()
788                (local-set-key "\C-coh" 'gnus-offline-set-unplugged-state)
789                (local-set-key "\C-com" 'gnus-offline-toggle-movemail-program)
790                (local-set-key "\C-cof" 'gnus-offline-toggle-articles-to-fetch)
791                (local-set-key "\C-coo" 'gnus-offline-toggle-on/off-send-mail)
792                (local-set-key "\C-cox" 'gnus-offline-toggle-auto-hangup)
793                (local-set-key "\C-cos" 'gnus-offline-set-interval-time)
794                (substitute-key-definition
795                 'gnus-group-get-new-news 'gnus-offline-gnus-get-new-news
796                 gnus-group-mode-map)
797                (if (eq gnus-offline-news-fetch-method 'nnagent)
798                    (progn
799                      (substitute-key-definition
800                       'gnus-agent-toggle-plugged 'gnus-offline-toggle-plugged
801                       gnus-agent-group-mode-map)
802                      (local-set-key "\C-coe" 'gnus-offline-agent-expire)))
803                (or (featurep 'xemacs)
804                    (define-key gnus-group-mode-map 
805                      (if (eq system-type 'windows-nt) [S-mouse-2] [mouse-3])
806                      'gnus-offline-popup-menu))))
807   (if (eq gnus-offline-news-fetch-method 'nnagent)
808       (add-hook 'gnus-summary-mode-hook
809                 '(lambda ()
810                    (substitute-key-definition
811                     'gnus-agent-toggle-plugged 'gnus-offline-toggle-plugged
812                     gnus-agent-summary-mode-map))))
813   (if (featurep 'xemacs)
814       ;; Overwrite the toolbar spec for gnus-group-mode.
815       (add-hook 'gnus-startup-hook
816                 (lambda ()
817                   (let ((i 0) (stat t) but)
818                     (while (and stat (setq but (nth i gnus-group-toolbar)))
819                       (and (equal 'gnus-group-get-new-news (aref but 1))
820                            (aset but 1 'gnus-offline-gnus-get-new-news)
821                            (setq stat nil))
822                       (setq i (1+ i))))))))
823 ;;
824 ;;
825 (defun gnus-offline-define-menu-on-miee ()
826   "*Set and change menu bar on MIEE menu."
827   (let ((menu
828   (if (featurep 'meadow)
829       (easy-menu-change
830        nil
831        "Miee"
832        '(
833          ["Spool \e$B$K$"$k5-;v$NAw?.\e(B" news-spool-post t]
834          ["Spool \e$B$K$"$k\e(B Mail \e$B$NAw?.\e(B" mail-spool-send t]
835          "----"
836          ["Offline \e$B>uBV$X\e(B" message-offline-state (not message-offline-state)]
837          ["Online \e$B>uBV$X\e(B" message-online-state message-offline-state]
838          "----"
839          ("Gnus Offline"
840           ["movemail \e$B$N@ZBX$(\e(B" gnus-offline-toggle-movemail-program t]
841           ["\e$B<hF@5-;v<oN`$NJQ99\e(B" gnus-offline-toggle-articles-to-fetch t]
842           ["Mail \e$BAw?.J}K!\e(B(On/Off)\e$B$N@ZBX$(\e(B" gnus-offline-toggle-on/off-send-mail t]
843           ["\e$B<+F0@ZCG$N@ZBX$(\e(B" gnus-offline-toggle-auto-hangup t]
844           "----"
845           ["\e$B<hF@:Q5-;v$r>C$9\e(B" gnus-offline-agent-expire (eq gnus-offline-news-fetch-method 'nnagent)]
846           ["\e$B5-;v<hF@4V3V;~4V$N@_Dj\e(B" gnus-offline-set-interval-time t]
847           "----"
848           ["\e$B2s@~$N@ZCG\e(B" gnus-offline-set-unplugged-state gnus-offline-connected])
849          ))
850     (easy-menu-change
851      nil
852      "Miee"
853      '(
854        ["Post news in spool" news-spool-post t]
855        ["Send mails in spool" mail-spool-send t]
856        "----"
857        ["Message Offline" message-offline-state (not message-offline-state)]
858        ["Message Online" message-online-state message-offline-state]
859        "----"
860        ("Gnus Offline"
861         ["Toggle movemail program" gnus-offline-toggle-movemail-program t]
862         ["Toggle articles to fetch" gnus-offline-toggle-articles-to-fetch t]
863         ["Toggle online/offline send mail" gnus-offline-toggle-on/off-send-mail t]
864         ["Toggle auto hangup" gnus-offline-toggle-auto-hangup t]
865         "----"
866         ["Expire articles" gnus-offline-agent-expire (eq gnus-offline-news-fetch-method 'nnagent)]
867         ["Set interval time" gnus-offline-set-interval-time t]
868         "----"
869         ["Hang up Line." gnus-offline-set-unplugged-state gnus-offline-connected]
870         ))))))
871   (and (featurep 'xemacs)
872        (easy-menu-add menu))))
873 ;;
874 ;; define menu without miee.
875 ;;
876 (defun gnus-offline-define-menu-on-agent ()
877   "*Set menu bar on OFFLINE menu."
878   (easy-menu-define 
879    gnus-offline-menu-on-agent
880    gnus-group-mode-map
881    "Gnus offline Menu"
882    (if (featurep 'meadow)
883        '("Offline"
884          ["movemail \e$B$N@ZBX$(\e(B" gnus-offline-toggle-movemail-program t]
885          ["\e$B<hF@5-;v<oN`$NJQ99\e(B" gnus-offline-toggle-articles-to-fetch t]
886          ["Mail \e$BAw?.J}K!\e(B(On/Off)\e$B$N@ZBX$(\e(B" gnus-offline-toggle-on/off-send-mail t]
887          ["\e$B<+F0@ZCG$N@ZBX$(\e(B" gnus-offline-toggle-auto-hangup t]
888          "----"
889          ["\e$B<hF@:Q5-;v$r>C$9\e(B" gnus-offline-agent-expire (eq gnus-offline-news-fetch-method 'nnagent)]
890          ["\e$B5-;v<hF@4V3V;~4V$N@_Dj\e(B" gnus-offline-set-interval-time t]
891          "----"
892          ["\e$B2s@~$N@ZCG\e(B" gnus-offline-set-unplugged-state gnus-offline-connected])
893      '("Offline"
894        ["Toggle movemail program" gnus-offline-toggle-movemail-program t]
895        ["Toggle articles to fetch" gnus-offline-toggle-articles-to-fetch t]
896        ["Toggle online/offline send mail" gnus-offline-toggle-on/off-send-mail t]
897        ["Toggle auto hangup" gnus-offline-toggle-auto-hangup t]
898        "----"
899        ["Expire articles" gnus-offline-agent-expire (eq gnus-offline-news-fetch-method 'nnagent)]
900        ["Set interval time" gnus-offline-set-interval-time t]
901        "----"
902        ["Hang up Line." gnus-offline-set-unplugged-state gnus-offline-connected])))
903   (and (featurep 'xemacs)
904        (easy-menu-add gnus-offline-menu-on-agent)))
905 ;;
906 ;; Popup menu within the group buffer (under Emacs).
907 ;;
908 (defun gnus-offline-popup-menu (event)
909   "Popup menu for Gnus offline."
910   (interactive "e")
911   (let* ((menu (if (boundp 'miee-popup-menu)
912                    (or (assoc 'keymap
913                               (assoc 'Miee (assoc 'menu-bar global-map)))
914                        miee-popup-menu)
915                  gnus-offline-menu-on-agent))
916          (pop (x-popup-menu t menu))
917          (func (and pop (lookup-key menu (apply 'vector pop)))))
918     (and pop func (funcall func))))
919 \f
920 ;;
921 ;; Timer Function
922 (defun gnus-offline-processed-by-timer ()
923   "*Set timer interval."
924   (if (and (> gnus-offline-interval-time 0)
925            (not gnus-offline-connected))
926       ;; Timer call
927       (gnus-demon-add-handler 'gnus-offline-gnus-get-new-news
928                               gnus-offline-interval-time
929                               gnus-offline-interval-time))
930   (if (= gnus-offline-interval-time 0)
931       (gnus-demon-remove-handler 'gnus-offline-gnus-get-new-news t)))
932 ;;
933 ;;
934 (provide 'gnus-offline)
935 ;;; gnus-offline.el ends here