File: gnus-offline.el

package info (click to toggle)
t-gnus 6.13.3.00-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,384 kB
  • ctags: 5,515
  • sloc: lisp: 65,996; makefile: 351; sh: 285
file content (922 lines) | stat: -rw-r--r-- 31,676 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
;;; gnus-offline.el --- To process mail & news at offline environment.
;;; $Id: gnus-offline.el,v 1.1.2.5.2.36.4.8 1999/10/24 10:26:31 czkmt Exp $

;;; Copyright (C) 1998 Tatsuya Ichikawa
;;;                    Yukihiro Ito
;;; Author: Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
;;;         Yukihiro Ito <ito@rs.civil.tohoku.ac.jp>
;;;         Hidekazu Nakamura <u90121@uis-inf.co.jp>
;;;         Tsukamoto Tetsuo <czkmt@remus.dti.ne.jp>

;;; Version: 2.20
;;; Keywords: news , mail , offline , gnus
;;;
;;; SPECIAL THANKS
;;;    Keiichi Suzuki <kei-suzu@mail.wbs.or.jp>
;;;    KORIYAMA Naohiro <kory@ba2.so-net.or.jp>
;;;    Katsumi Yamaoka <yamaoka@jpl.org>

;;; This file is part of Semi-gnus.
;;;
;;; GNU Emacs is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2, or (at your option)
;;; any later version.

;;; GNU Emacs is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.

;;; You should have received a copy of the GNU General Public License
;;; along with GNU Emacs; see the file COPYING.  If not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307, USA.
;;;
;;;; Commentary:
;;; Note.
;;;   This file works only with after version of Emacs 19.30.
;;;   This file needs miee.el and SEMI.
;;;   If you set gnus-offline-drafts-queue-type to 'agent , you don't need 
;;;   miee.el
;;;   You must use T-gnus 6.12.0 or later.
;;;
;;; How to use.
;;;
;;; Add following code at the end in your .emacs
;;;
;;;    (load "gnus-ofsetup")
;;;    (gnus-setup-for-offline)
;;;
;;; If you use gnus-agent as souper , put gnus-agent setup code in you .gnus.el
;;;
;;; If you use nnspool as souper , put following code in your .emacs before
;;; gnus-offline setting.
;;;
;;; Then , put hang.exe in exec-path directory.
;;;
;;; In Gnus group buffer , type g to get all news and mail.
;;; Then send mail and news in spool directory.
;;;
;;; Variables.
;;;  gnus-offline-dialup-program-arguments
;;;                                   ... List of dialup program arguments.
;;;  gnus-offline-hangup-program-arguments
;;;                                   ... List of hangup program arguments.
;;;  gnus-offline-mail-treat-environ  ... toggle sending mail online/offline.
;;;  gnus-offline-articles-to-fetch   ... toggle fetch articles.
;;;                                        both->mail->news->both...
;;;  gnus-offline-load-hook           ... hook before gnus-offline load.
;;;  gnus-offline-before-online-hook  ... hook before all online jobs.
;;;  gnus-offline-after-online-hook   ... hook after all online jobs.
;;;  gnus-offline-interval-time       ... Interval time to do all online jobs.
;;;                                        (minutes)
;;;  gnus-offline-dialup-function     ... Function to diualup.
;;;  gnus-offline-hangup-function     ... Function to hangup.

;;; Code:

(eval '(run-hooks 'gnus-offline-load-hook))

(require 'cl)
(require 'custom)
(require 'easymenu)
(provide 'gnus-offline)

(eval-after-load "eword-decode"
  '(mime-set-field-decoder 'X-Gnus-Offline-Backend nil nil))

(defgroup gnus-offline nil
  "Offline backend utility for Gnus."
  :prefix "gnus-offline-"
  :group 'gnus
  :group 'mail
  :group 'news)

(defconst gnus-offline-version-number "2.20")
(defconst gnus-offline-codename
;;  "Beta5"			; Beta
;;  "This is the time"		; 2.00
;;  "A matter of trust"
;;  "Modern Woman"
;;  "Ahhhhhhh!!"		; 2.10b1
  "Cup of life"                 ; 2.20
;;  "Code of silence"
  )

(defconst gnus-offline-version (format "Gnus offline backend utiliy v%s"
				       gnus-offline-version-number))

(eval-when-compile
  (mapc
   (lambda (symbol)
     (unless (boundp symbol)
       (make-local-variable symbol)
       (eval (list 'setq symbol nil))))
   '(nnagent-version
     nnspool-version
     msspool-news-server
     msspool-news-service
     miee-popup-menu
     gnus-group-toolbar)))

(put 'gnus-offline-set-unplugged-state 'menu-enable 'gnus-offline-connected)
(if (eq system-type 'windows-nt)
    (define-process-argument-editing "/hang\\.exe\\'"
      (lambda (x)
	(general-process-argument-editing-function
	 x nil t t nil t t))))

(defcustom gnus-offline-auto-hangup t
  "*Whether dialup-network automatically hang up when all online jobs has done."
  :group 'gnus-offline
  :type 'boolean)

(defcustom gnus-offline-load-hook nil
  "*Hook to be run after the gnus-offline package has been loaded."
  :group 'gnus-offline
  :type 'hook)

(defcustom gnus-offline-before-online-hook nil
  "*Hook to be run before all online jobs."
  :group 'gnus-offline
  :type 'hook)

(defcustom gnus-offline-after-online-hook nil
  "*Hook to be run after all online jobs."
  :group 'gnus-offline
  :type 'hook)

(defcustom gnus-offline-mail-treat-environ 'offline
  "*If online , gnus-offline send all mail under online environ.
If offline , gnus-offline send all mail temporary to spool dir."
  :group 'gnus-offline
  :type '(choice (const offline)
		 (const online)))

(defcustom gnus-offline-articles-to-fetch 'both
  "*If both , gnus-offline fetch mail and news articles.
If mail , gnus-offline only fetch mail articles.
 If news , gnus-offline only fetch news articles."
  :group 'gnus-offline
  :type '(choice (const both)
		 (const mail)
		 (const news)))

(defcustom gnus-offline-mail-group-level 1
  "*Group level for mail group."
  :group 'gnus-offline
  :type 'integer)

(defcustom gnus-offline-after-empting-spool-hook nil
  "*Hook to be run before empting spool."
  :group 'gnus-offline
  :type 'hook)

(defcustom gnus-offline-before-empting-spool-hook nil
  "*Hook to be run after empting spool."
  :group 'gnus-offline
  :type 'hook)

(defcustom gnus-offline-dialup-function 'gnus-offline-connect-server
  "*Function to dialup."
  :group 'gnus-offline
  :type 'function)

(defcustom gnus-offline-hangup-function 'gnus-offline-hangup-line
  "*Function to hangup."
  :group 'gnus-offline
  :type 'function)

(defcustom gnus-offline-agent-automatic-expire t
  "*Non-nil means expire articles on every session."
  :group 'gnus-offline
  :type 'boolean)

;; These variables should be customized using `gnus-offline-customize',
;; not by `customize'.

(defvar gnus-offline-dialup-program nil
  "*Program name for dialup.")

(defvar gnus-offline-hangup-program nil
  "*Program name for hangup.")

(defvar gnus-offline-dialup-program-arguments nil
  "*Program arguments of gnus-offline-dialup-program.")

(defvar gnus-offline-hangup-program-arguments nil
  "*Program arguments of gnus-offline-hangup-program.")

(defvar gnus-offline-interval-time 0
  "*Interval time(minutes) to do online jobs.
If set to 0 , timer call is disabled.")

(defvar gnus-offline-drafts-queue-type 'agent
  "*Queuing function used for draft messages.")

(defvar gnus-offline-MTA-type 'smtp
  "*Type of MTA, sendmail or smtp.el.")

;;; Internal variables.
(defvar gnus-offline-connected nil
  "*If value is t , dialup line is connected status.
If value is nil , dialup line is disconnected status.")

(defvar gnus-offline-news-fetch-method nil
  "*Method to fetch news articles.")

(defvar gnus-offline-mail-fetch-method nil
  "*Method to fetch mail articles.")

(defvar gnus-offline-header-string
  (format "%s - \"%s\""
	  gnus-offline-version
	  gnus-offline-codename)
  "*Header string for gnus-offline.")

(defvar gnus-offline-stored-group-level nil
  "*Mail Group level before changing.")

(defvar gnus-offline-mail-source nil
  "*mail-sources save variable.")

(defvar gnus-offline-lang)

(defvar gnus-offline-resource-en
  '((error-check-1
     . "WARNING!!: gnus-agent.el or nnagent.el is not loaded.
Please check your .emacs or .gnus.el to work gnus-agent fine.")
    (error-check-2 ."WARNING!!: nnspool.el is not loaded.
Please check your .emacs or .gnus.el to work nnspool fine.")
    (connect-server-1 . "Dialing ...")
    (connect-server-2 . "Dialing ... done.")
    (get-new-news-function-1 . "Set to online status.")
    (hangup-line-1 . "Hang up line ... ")
    (hangup-line-2 . "Hang up line ... done.")
    (after-jobs-done-1 . "All online jobs has done.")
    (toggle-auto-hangup-1 . "Auto hang up logic")
    (toggle-auto-hangup-2 . "disabled.")
    (toggle-auto-hangup-3 . "enabled.")
    (toggle-on/off-send-mail-1 . "Sending mail immidiately.")
    (toggle-on/off-send-mail-2 . "Sending mail temporary to spool directory.")
    (toggle-articles-to-fetch-1 . "Articles fetch from server.")
    (toggle-articles-to-fetch-2 . "Only Mail")
    (toggle-articles-to-fetch-3 . "Only News")
    (toggle-articles-to-fetch-4 . "Mail/News both")
    (empting-spool-1 . "Sending mails in spool ...")
    (empting-spool-2 . "Sending mails in spool ... done.")
    (empting-spool-3 . "Posting news in spool ...")
    (empting-spool-4 . "Posting news in spool ... done.")
    (empting-spool-5 . "Sending messages in spool ...")
    (empting-spool-6 . "Sending messages in spool ... done.")
    (interval-time-1 . "Interval time (now %d minutes) : ")
    (interval-time-2 . "Retrieving message logic by timer is disabled.")
    (interval-time-3 . "Interval time set to %d minutes")
    (menu-miee-1 . "Post news in spool")
    (menu-miee-2 . "Send mails in spool")
    (menu-miee-3 . "Message Offline")
    (menu-miee-4 . "Message Online")
    (menu-1 . "Toggle articles to fetch")
    (menu-2 . "Toggle online/offline send mail")
    (menu-3 . "Toggle auto hangup")
    (menu-4 . "Expire articles")
    (menu-5 . "Set interval time")
    (menu-6 . "Hang up Line.")
    (menu-7 . "Customize options...")))

(defvar gnus-offline-resource-ja
  '((error-check-1
     . "$B7Y9p(B!!: gnus-agent.el $B$^$?$O(B nnagent.el $B$,%m!<%I$5$l$F$$$^$;$s!#(B
.emacs $B$^$?$O(B .gnus.el $B$N(B gnus-agent $B$N@_Dj$r@5$7$/$7$F$/$@$5$$!#(B")
    (error-check-2 ."$B7Y9p(B!!: nnspool.el $B$,%m!<%I$5$l$F$$$^$;$s!#(B
.emacs $B$^$?$O(B .gnus.el $B$N(B nnspool $B$N@_Dj$r@5$7$/$7$F$/$@$5$$!#(B")
    (connect-server-1 . "$B%@%$%d%k$7$F$$$^$9(B...")
    (connect-server-2 . "$B%@%$%d%k$7$F$$$^$9(B...$B40N;!#(B")
    (get-new-news-function-1 . "$B%*%s%i%$%s>uBV$G$9!#(B")
    (hangup-line-1 . "$B@ZCG$7$F$$$^$9(B...")
    (hangup-line-2 . "$B@ZCG$7$F$$$^$9(B...$B40N;!#(B")
    (after-jobs-done-1 . "$BA4$F$N%*%s%i%$%s=hM}$r40N;$7$^$7$?!#(B")
    (toggle-auto-hangup-1 . "$B<+F0@ZCG5!G=$r(B ")
    (toggle-auto-hangup-2 . "$B%*%U(B $B$K$7$^$7$?!#(B")
    (toggle-auto-hangup-3 . "$B%*%s(B $B$K$7$^$7$?!#(B")
    (toggle-on/off-send-mail-1 . "$B%a!<%k$rD>@\Aw?.$7$^$9!#(B")
    (toggle-on/off-send-mail-2 . "$B%a!<%k$O%-%e!<$KAw$i$l$^$9!#(B")
    (toggle-articles-to-fetch-1 . "$B<u?.$9$k%a%C%;!<%8$O(B... ")
    (toggle-articles-to-fetch-2 . "$B%a!<%k$N$_$G$9!#(B")
    (toggle-articles-to-fetch-3 . "$B%K%e!<%9$N$_$G$9!#(B")
    (toggle-articles-to-fetch-4 . "$B%a!<%k$H%K%e!<%9$NN>J}$G$9!#(B")
    (empting-spool-1 . "$B%-%e!<$N%a!<%k$rAw?.Cf(B...")
    (empting-spool-2 . "$B%-%e!<$N%a!<%k$rAw?.Cf(B... $B40N;!#(B")
    (empting-spool-3 . "$B%-%e!<$N%K%e!<%95-;v$rAw?.Cf(B...")
    (empting-spool-4 . "$B%-%e!<$N%K%e!<%95-;v$rAw?.Cf(B... $B40N;!#(B")
    (empting-spool-5 . "$B%-%e!<$N%a%C%;!<%8$rAw?.Cf(B...")
    (empting-spool-6 . "$B%-%e!<$N%a%C%;!<%8$rAw?.Cf(B... $B40N;!#(B")
    (interval-time-1 . "$BAw<u?.%8%g%V$N4V3V(B ($B8=:_$N@_Dj$O(B %d $BJ,$G$9(B) : ")
    (interval-time-2 . "$B<+F0Aw<u?.5!G=$r(B $B%*%U(B $B$K$7$^$7$?!#(B")
    (interval-time-3 . "$B<+F0Aw<u?.$N4V3V$r(B %d $BJ,$K@_Dj$7$^$7$?!#(B")))

(defvar gnus-offline-resource-ja_complete
  (append
   gnus-offline-resource-ja
   '((menu-miee-1 . "Spool $B$K$"$k5-;v$NAw?.(B")
     (menu-miee-2 . "Spool $B$K$"$k(B Mail $B$NAw?.(B")
     (menu-miee-3 . "Offline $B>uBV$X(B")
     (menu-miee-4 . "Online $B>uBV$X(B")
     (menu-1 . "$B<hF@5-;v<oN`$NJQ99(B")
     (menu-2 . "Mail $BAw?.J}K!(B(On/Off)$B$N@ZBX$((B")
     (menu-3 . "$B<+F0@ZCG$N@ZBX$((B")
     (menu-4 . "$B<hF@:Q5-;v$r>C$9(B")
     (menu-5 . "$B5-;v<hF@4V3V;~4V$N@_Dj(B")
     (menu-6 . "$B2s@~$N@ZCG(B")
     (menu-7 . "$B%W%m%Q%F%#(B..."))))

;;; Functions

(defun gnus-offline-get-message (symbol &optional lang)
  (setq lang (or lang gnus-offline-lang))
  (or
   (cdr (assq symbol (symbol-value
		      (intern (format "gnus-offline-resource-%s" lang)))))
   (cdr (assq symbol gnus-offline-resource-en))))

;;
;; Setting up...
;;
(defun gnus-offline-setup ()
  "*Initialize gnus-offline function"

  ;; Menu and keymap
  (gnus-offline-define-menu-and-key)
  
  ;; To transfer Mail/News function.
  (cond ((eq gnus-offline-mail-treat-environ 'offline)
	 ;; send mail under offline environ.
	 (gnus-offline-set-offline-sendmail-function))
	((eq gnus-offline-mail-treat-environ 'online)
	 ;; send mail under offline environ.
	 (gnus-offline-set-online-sendmail-function))))

;;
;; Setting Error check.
(defun gnus-offline-error-check ()
  ;; Check gnus-agent and nnspool setting.
  (let ((buffer " *Offline Error*"))
    (cond ((eq gnus-offline-news-fetch-method 'nnagent)
	   ;; nnagent and gnus-agent loaded ??
	   (unless (and (featurep 'gnus-agent)
			(featurep 'nnagent))
	     (set-buffer (gnus-get-buffer-create buffer))
	     (erase-buffer)
	     (insert (gnus-offline-get-message 'error-check-1))
	     (pop-to-buffer buffer)))
	
	  ((eq gnus-offline-news-fetch-method 'nnspool)
	   (unless (featurep 'nnspool)
	     (set-buffer (gnus-get-buffer-create buffer))
	     (erase-buffer)
	     (insert (gnus-offline-get-message 'error-check-2))
	     (pop-to-buffer buffer)))
	  (t
	   nil))))
;;
;;
(defun gnus-offline-set-offline-sendmail-function ()
  "*Initialize sendmail-function when unplugged status."
  (cond ((eq gnus-offline-drafts-queue-type 'miee)
	 (if (eq gnus-offline-news-fetch-method 'nnagent)
	     (setq gnus-agent-send-mail-function
		   'sendmail-to-spool-in-gnspool-format))
	 (setq message-send-mail-function 'sendmail-to-spool-in-gnspool-format))
	(t
	 (setq gnus-agent-send-mail-function
	       (gnus-offline-set-online-sendmail-function)
	       message-send-mail-function 'gnus-agent-send-mail))))
;;
(defun gnus-offline-set-online-sendmail-function ()
  "*Initialize sendmail-function when plugged status."
  (if (eq gnus-offline-MTA-type 'smtp)
      (setq message-send-mail-function 'message-send-mail-with-smtp)
    (setq message-send-mail-function 'message-send-mail-with-sendmail)))
;;
(defun gnus-offline-set-offline-post-news-function ()
  "*Initialize sendnews-function when unplugged status."
  (if (eq gnus-offline-drafts-queue-type 'miee)
      (setq message-send-news-function 'gnspool-request-post)))
;;
(defun gnus-offline-set-online-post-news-function ()
  "*Initialize sendnews-function when plugged status."
  (setq message-send-news-function 'message-send-news-with-gnus))
;;
;; Get new news jobs. (gnus-agent and nnspool)
;;
(defun gnus-offline-gnus-get-new-news (&optional arg)
  "*Override function \"gnus-group-get-new-news\"."
  (interactive "P")
  (run-hooks 'gnus-offline-before-online-hook)
  (if (functionp gnus-offline-dialup-function)
      (funcall gnus-offline-dialup-function))
  (gnus-offline-get-new-news-function)
  (gnus-group-get-new-news arg))

;;
;; dialup...
;;
(defun gnus-offline-connect-server ()
  "*Dialup function."
  ;; Dialup if gnus-offline-dialup-program is specified
  (if (stringp gnus-offline-dialup-program)
      (progn
	(message (gnus-offline-get-message 'connect-server-1))
	(apply 'call-process gnus-offline-dialup-program nil nil nil
	       gnus-offline-dialup-program-arguments)
	(sleep-for 1)
	(message (gnus-offline-get-message 'connect-server-2)))))

;;
;; Jobs before get new news , send mail and post news.
;;
(defun gnus-offline-get-new-news-function ()
  "*Prepare to get new news/mail."
  ;; Set mail group level
  (if (eq gnus-offline-articles-to-fetch 'mail)
      (gnus-offline-set-mail-group-level gnus-offline-mail-group-level))

  ;; Set to online environ.
  (setq gnus-offline-connected t)

  ;; Set send mail/news functions to online functions.
  (gnus-offline-set-online-sendmail-function)
  (gnus-offline-set-online-post-news-function)
  (message (gnus-offline-get-message 'get-new-news-function-1))

  ;; fetch only news
  (if (eq gnus-offline-articles-to-fetch 'news)
      (gnus-offline-disable-fetch-mail))

  ;; fetch both mail and news. or Only mail.
  (gnus-offline-enable-fetch-news)
  (if (memq gnus-offline-articles-to-fetch '(both mail))
      (gnus-offline-enable-fetch-mail))

  ;; fetch only mail for gnus-agent
  (if (and (eq gnus-offline-news-fetch-method 'nnagent)
	   (eq gnus-offline-articles-to-fetch 'mail))
	  (setq gnus-agent-handle-level gnus-offline-mail-group-level)))

;;
;; Change mail group level to handle only mail.
;;
(defun gnus-offline-set-mail-group-level (level)
  "*Set nnm* group level."
  (switch-to-buffer gnus-group-buffer)
  (goto-char (point-min))
  
  ;; Save current level
  (if (not gnus-offline-stored-group-level)
      (while (re-search-forward " nnm" nil t)
	(setq gnus-offline-stored-group-level
	      (append gnus-offline-stored-group-level
		      (list (gnus-group-group-level)))))
    (forward-line 1)
    (beginning-of-line))
  ;;
  (goto-char (point-min))
  (while (re-search-forward " nnm" nil t)
    (gnus-group-set-current-level 1 level)
    (forward-line 1)
    (beginning-of-line))
  t)
;;
;; Restore mail group level
;;
(defun gnus-offline-restore-mail-group-level ()
  "*Restore nnm* group level."
  (switch-to-buffer gnus-group-buffer)
  (goto-char (point-min))
  (let ((num 0))
    (while (re-search-forward " nnm" nil t)
      (gnus-group-set-current-level 1 (nth num gnus-offline-stored-group-level))
      (forward-line 1)
      (setq num (+ num 1))
      (beginning-of-line))))
;;
;; Jobs after getting new news.
;;
(defun gnus-offline-after-get-new-news ()
  "*After getting news and mail jobs."
  (if (memq gnus-offline-articles-to-fetch '(both mail))
      (progn
	;; Mail/both
	;; send mail/news in spool
	(gnus-offline-empting-spool)
	(if (eq gnus-offline-articles-to-fetch 'mail)
	    (progn
	      ;; Send only mail and hang up...
	      (if (and gnus-offline-connected
		       gnus-offline-auto-hangup)
		  (gnus-offline-set-unplugged-state))
	      ;; Disable fetch mail.
	      (gnus-offline-disable-fetch-mail)
	      (gnus-offline-after-jobs-done)))))
  
  ;; News/Both
  (if (memq gnus-offline-articles-to-fetch '(both news))
      (progn
	(if gnus-offline-connected
	    (cond ((eq gnus-offline-news-fetch-method 'nnagent)
		   ;; Get New News (gnus-agent)
		   (gnus-agent-toggle-plugged t)
		  
		   ;; fetch articles
		   (gnus-agent-fetch-session)
		  
		   ;; Hang Up line. then set to offline status.
		   (if (and gnus-offline-connected
			    gnus-offline-auto-hangup)
		       (gnus-offline-set-unplugged-state))
		   
		   ;; All online jobs has done.
		   (gnus-offline-after-jobs-done))
		  (t
		   (if (eq gnus-offline-news-fetch-method 'nnspool)
		       ;; Get New News (nnspool)
		       (gnspool-get-news))))))))
;;
;; Disable fetch mail
;;
(defun gnus-offline-disable-fetch-mail ()
  "*Set do not fetch mail."
  (setq mail-sources nil
	nnmail-spool-file nil))
;;
;; Enable fetch mail
;;
(defun gnus-offline-enable-fetch-mail ()
  "*Set to fetch mail."
  (setq gnus-offline-mail-fetch-method 'nnmail)
  (setq mail-sources gnus-offline-mail-source))
;;
;; Enable fetch news
;;
(defun gnus-offline-enable-fetch-news ()
  "*Set to fetch news."
  (if (eq gnus-offline-news-fetch-method 'nnagent)
      (progn
	(setq gnus-agent-handle-level gnus-level-subscribed)
	(gnus-agent-toggle-plugged t))))

;;
;; Add your custom header.
;;
(defun gnus-offline-add-custom-header (header string)
  "*Add X-Gnus-Offline-Backend header to Mail/News message."
  (let ((delimline
	 (progn (goto-char (point-min))
		(re-search-forward
		 (concat "^" (regexp-quote mail-header-separator) "\n"))
		(point-marker)))
	hdr str)
    (goto-char (point-min))
    (unless (re-search-forward (concat "^" header) delimline t)
      (goto-char delimline)
      (forward-line -1)
      (beginning-of-line)
      (setq hdr (concat header " "))
      (setq str (concat hdr string))
      (setq hdr (concat str "\n"))
      (insert-string hdr))))
;;
;; Add X-Offline-Backend header.
;;
(defun gnus-offline-message-add-header ()
  "*Add X-Gnus-Offline-Backend header to Mail/News message."
  (when (eq gnus-offline-mail-treat-environ 'offline)
    (let* ((ver (if (eq gnus-offline-news-fetch-method 'nnagent)
		    nnagent-version
		  nnspool-version))
	   (str (format "\n                        with %s" ver)))
    (gnus-offline-add-custom-header
     "X-Gnus-Offline-Backend:" (concat gnus-offline-header-string str)))))


;;
;; Toggle plugged/unplugged
;;
(defun gnus-offline-toggle-plugged (plugged)
  "*Override function \"Jj\" - gnus-agent-toggle-plugged."
  (interactive (list (not gnus-offline-connected)))
  (if plugged
      (progn
	(setq gnus-offline-connected plugged)
	(gnus-agent-toggle-plugged plugged)
	;; Set send mail/news function to offline functions.
	(gnus-offline-set-online-sendmail-function)
	(gnus-offline-set-online-post-news-function))
    ;; Set to offline status
    (gnus-offline-set-unplugged-state)))
;;
;; Function of hang up line.
;;
(defun gnus-offline-set-unplugged-state ()
  "*Set to unplugged state."
  (interactive)
  ;; Hang Up Line.
  (if (functionp gnus-offline-hangup-function)
      (funcall gnus-offline-hangup-function))
  (setq gnus-offline-connected nil)
  (if (eq gnus-offline-news-fetch-method 'nnagent)
      (gnus-agent-toggle-plugged nil))

  ;; Set send mail/news function to offline functions.
  (gnus-offline-set-offline-sendmail-function)
  (gnus-offline-set-offline-post-news-function)
  ;;
  (setenv "MAILHOST" nil))
;;
;; Hangup line function 
;;
(defun gnus-offline-hangup-line ()
  "*Hangup line function."
  (message (gnus-offline-get-message 'hangup-line-1))
  (if (stringp gnus-offline-hangup-program)
      (apply 'start-process "hup" nil gnus-offline-hangup-program
	     gnus-offline-hangup-program-arguments))
  (message (gnus-offline-get-message 'hangup-line-2)))
;;
;; Hang Up line routine whe using nnspool
;;
(defun gnus-offline-nnspool-hangup-line ()
  (if (and gnus-offline-connected
	   gnus-offline-auto-hangup)
      (gnus-offline-set-unplugged-state))
  (gnus-offline-after-jobs-done))
;;
;; Function of all jobs has done.
;;
(defun gnus-offline-after-jobs-done ()
  "*Jobs after all online jobs."
  (run-hooks 'gnus-offline-after-online-hook)
  (if (eq gnus-offline-articles-to-fetch 'mail)
      (gnus-offline-restore-mail-group-level))
  (if (eq gnus-offline-news-fetch-method 'nnagent)
      (gnus-offline-agent-expire))
  (if (and (featurep 'xemacs)
	   (fboundp 'play-sound-file))
      (ding nil 'drum)
    (ding))
  (gnus-group-save-newsrc)
  (message (gnus-offline-get-message 'after-jobs-done-1)))


;;
;; Toggle auto hang up
;;
(defun gnus-offline-toggle-auto-hangup ()
  "*Toggle auto hangup flag."
  (interactive)
  (let ((string (gnus-offline-get-message 'toggle-auto-hangup-1)) str)
    (if gnus-offline-auto-hangup
	(progn
	  (setq gnus-offline-auto-hangup nil
		str (gnus-offline-get-message 'toggle-auto-hangup-2)))
      (setq gnus-offline-auto-hangup t
	    str (gnus-offline-get-message 'toggle-auto-hangup-3)))
    (message (format "%s %s" string str))))
;;
;; Toggle offline/online to send mail.
;;
(defun gnus-offline-toggle-on/off-send-mail ()
  "*Toggel online/offline sendmail."
  (interactive)
  (if (eq gnus-offline-mail-treat-environ 'offline)
      (progn
	;; Sending mail under online environ.
	(gnus-offline-set-online-sendmail-function)
	(setq gnus-offline-mail-treat-environ 'online)
	(message (gnus-offline-get-message 'toggle-on/off-send-mail-1)))
    ;; Sending mail under offline environ.
    (gnus-offline-set-offline-sendmail-function)
    (setq gnus-offline-mail-treat-environ 'offline)
    (message (gnus-offline-get-message 'toggle-on/off-send-mail-2))))
;;
;; Toggle articles to fetch ... both -> mail -> news -> both
;;
(defun gnus-offline-toggle-articles-to-fetch ()
  "*Set articles to fetch... both(Mail/News) -> mail only -> News only -> both"
  (interactive)
  (let ((string (gnus-offline-get-message 'toggle-articles-to-fetch-1))
	str)
    (cond ((eq gnus-offline-articles-to-fetch 'both)
	   (setq gnus-offline-articles-to-fetch 'mail
		 str (gnus-offline-get-message 'toggle-articles-to-fetch-2)))
	  ((eq gnus-offline-articles-to-fetch 'mail)
	   (setq gnus-offline-articles-to-fetch 'news
		 str (gnus-offline-get-message 'toggle-articles-to-fetch-3)))
	  (t
	   (setq gnus-offline-articles-to-fetch 'both
		 str (gnus-offline-get-message 'toggle-articles-to-fetch-4))))
    (message (format "%s %s" string str))))
;;
;; Send mail and Post news using Miee or gnus-agent.
;;
(defun gnus-offline-empting-spool ()
  "*Send all drafts on queue."
  (run-hooks 'gnus-offline-before-empting-spool-hook)
  (if (eq gnus-offline-drafts-queue-type 'miee)
      ;; Send queued message by miee.el.
      (progn
	(if (eq gnus-offline-mail-treat-environ 'offline)
	    (progn
	      (message (gnus-offline-get-message 'empting-spool-1))
	      ;; Using miee to send mail.
	      (mail-spool-send)
	      (message (gnus-offline-get-message 'empting-spool-2))))
	(message (gnus-offline-get-message 'empting-spool-3))
	;; Using miee to post news.
	(if (and (not (stringp msspool-news-server))
		 (not msspool-news-service))
	    (progn
	      (setq msspool-news-server (nth 1 gnus-select-method))
	      (setq msspool-news-service 119)))
	(news-spool-post)
	(message (gnus-offline-get-message 'empting-spool-4)))
    ;; Send queued message by gnus-agent
    (message (gnus-offline-get-message 'empting-spool-5))
    (gnus-group-send-drafts)
    (message (gnus-offline-get-message 'empting-spool-6)))
  ;;
  (run-hooks 'gnus-offline-after-empting-spool-hook))
;;
;; Set interval time
;;
(defun gnus-offline-set-interval-time ()
  "*Set interval time for gnus-daemon."
  (interactive)
  (setq gnus-offline-interval-time
	(string-to-int (read-from-minibuffer
			(format (gnus-offline-get-message 'interval-time-1)
				gnus-offline-interval-time)
			nil)))
  (if (< gnus-offline-interval-time 2)
      (progn
	(message (gnus-offline-get-message 'interval-time-2))
	(setq gnus-offline-interval-time 0))
    (message
     (format (gnus-offline-get-message 'interval-time-3)
	     gnus-offline-interval-time)))
  (gnus-offline-processed-by-timer))
;;
;; Expire articles using gnus-agent.
;;
(defun gnus-offline-agent-expire ()
  "*Expire expirable article on News group."
  (interactive)
  (and gnus-offline-agent-automatic-expire
       (if (eq 0 gnus-agent-expire-days)
	   (let (gnus-agent-expire-all)
	     (gnus-agent-expire))
	 (gnus-agent-expire))))
;;
;; Menu.
;;
(defun gnus-offline-define-menu-and-key ()
  "*Set key and menu."
  (if (eq gnus-offline-drafts-queue-type 'miee)
      (if (featurep 'xemacs)
	  (add-hook 'gnus-group-mode-hook 'gnus-offline-define-menu-on-miee)
	(gnus-offline-define-menu-on-miee))
    (add-hook 'gnus-group-mode-hook 'gnus-offline-define-menu-on-agent))
  (add-hook 'gnus-group-mode-hook
	    '(lambda ()
	       (local-set-key "\C-coh" 'gnus-offline-set-unplugged-state)
	       (local-set-key "\C-cof" 'gnus-offline-toggle-articles-to-fetch)
	       (local-set-key "\C-coo" 'gnus-offline-toggle-on/off-send-mail)
	       (local-set-key "\C-cox" 'gnus-offline-toggle-auto-hangup)
	       (local-set-key "\C-cos" 'gnus-offline-set-interval-time)
	       (substitute-key-definition
		'gnus-group-get-new-news 'gnus-offline-gnus-get-new-news
		gnus-group-mode-map)
	       (if (eq gnus-offline-news-fetch-method 'nnagent)
		   (progn
		     (substitute-key-definition
		      'gnus-agent-toggle-plugged 'gnus-offline-toggle-plugged
		      gnus-agent-group-mode-map)
		     (local-set-key "\C-coe" 'gnus-offline-agent-expire)))
	       (or (featurep 'xemacs)
		   (define-key gnus-group-mode-map 
		     (if (eq system-type 'windows-nt) [S-mouse-2] [mouse-3])
		     'gnus-offline-popup-menu))))
  (if (eq gnus-offline-news-fetch-method 'nnagent)
      (add-hook 'gnus-summary-mode-hook
		'(lambda ()
		   (substitute-key-definition
		    'gnus-agent-toggle-plugged 'gnus-offline-toggle-plugged
		    gnus-agent-summary-mode-map))))
  (if (featurep 'xemacs)
      ;; Overwrite the toolbar spec for gnus-group-mode.
      (add-hook 'gnus-startup-hook
		#'(lambda ()
		    (catch 'tag
		      (mapc (lambda (but)
			      (when (eq 'gnus-group-get-new-news (aref but 1))
				(aset but 1 'gnus-offline-gnus-get-new-news)
				(throw 'tag nil)))
			    gnus-group-toolbar))))))
;;
;;
(defun gnus-offline-get-menu-items (list)
  (mapcar
   #'(lambda (el)
       (if (listp el)
	   (apply 'vector
		  (cons (gnus-offline-get-message (car el)) (cdr el)))
	 el))
   list))

(defvar gnus-offline-menu
  (gnus-offline-get-menu-items
   '((menu-1 gnus-offline-toggle-articles-to-fetch t)
     (menu-2 gnus-offline-toggle-on/off-send-mail t)
     (menu-3 gnus-offline-toggle-auto-hangup t)
     "----"
     (menu-4 gnus-offline-agent-expire
	     (eq gnus-offline-news-fetch-method 'nnagent))
     (menu-5 gnus-offline-set-interval-time t)
     "----"
     (menu-6 gnus-offline-set-unplugged-state gnus-offline-connected)
     "----"
     (menu-7 gnus-ofsetup-customize t))))

(defun gnus-offline-define-menu-on-miee ()
  "*Set and change menu bar on MIEE menu."
  (let ((miee-menu
	 (gnus-offline-get-menu-items
	  '((menu-miee-1 news-spool-post t)
	    (menu-miee-2 mail-spool-send t)
	    "----"
	    (menu-miee-3 message-offline-state (not message-offline-state))
	    (menu-miee-4 message-online-state message-offline-state)
	    "----")))
	menu)
    (setq menu
	  (easy-menu-change
	   nil "Miee"
	   (append miee-menu
		   (list (cons "Gnus Offline" gnus-offline-menu)))))
    (and (featurep 'xemacs)
	 (easy-menu-add menu))))
;;
;; define menu without miee.
;;
(defun gnus-offline-define-menu-on-agent ()
  "*Set menu bar on OFFLINE menu."
  (easy-menu-define
   gnus-offline-menu-on-agent gnus-group-mode-map "Gnus offline Menu"
   (cons "Offline" gnus-offline-menu))
  (and (featurep 'xemacs)
       (easy-menu-add gnus-offline-menu-on-agent)))
;;
;; Popup menu within the group buffer (under Emacs).
;;
(defvar gnus-offline-popup-menu nil)
(defun gnus-offline-popup-menu (event)
  "Popup menu for Gnus Offline."
  (interactive "e")
  (unless gnus-offline-popup-menu
    (setq gnus-offline-popup-menu
	  (let ((menu
		 (if (boundp 'miee-popup-menu)
		     (or (assq 'keymap
				(assq 'Miee (assq 'menu-bar global-map)))
			 miee-popup-menu)
		   (symbol-value 'gnus-offline-menu-on-agent))))
	    (if (string< emacs-version "20")
		(append (list 'keymap
			      (if (boundp 'miee-popup-menu)
				  '(nil "Miee")
				'(nil "Offline"))
			      '(nil "")
			      '(nil ""))
			(cdr menu))
	      menu))))
  (let* ((pop (x-popup-menu t gnus-offline-popup-menu))
	 (func (and pop (lookup-key gnus-offline-popup-menu
				    (apply 'vector pop)))))
    (and pop func (funcall func))))

;;
;; Timer Function
(defun gnus-offline-processed-by-timer ()
  "*Set timer interval."
  (if (and (> gnus-offline-interval-time 0)
	   (not gnus-offline-connected))
      ;; Timer call
      (gnus-demon-add-handler 'gnus-offline-gnus-get-new-news
			      gnus-offline-interval-time
			      gnus-offline-interval-time))
  (if (= gnus-offline-interval-time 0)
      (gnus-demon-remove-handler 'gnus-offline-gnus-get-new-news t)))
;;
;;
;;; gnus-offline.el ends here