emacs / lisp / nnmail.el

  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
;;; nnmail.el --- mail support functions for the Gnus mail backends

;; Copyright (C) 1995 Free Software Foundation, Inc.

;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
;; Keywords: news, mail

;; This file is part of GNU Emacs.

;; 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:

;;; Code:

(require 'nnheader)
(require 'rmail)
(require 'timezone)
(require 'sendmail)

(defvar nnmail-split-methods
  '(("mail.misc" ""))
  "*Incoming mail will be split according to this variable.

If you'd like, for instance, one mail group for mail from the
\"4ad-l\" mailing list, one group for junk mail and one for everything
else, you could do something like this:

 (setq nnmail-split-methods
       '((\"mail.4ad\" \"From:.*4ad\")
         (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
         (\"mail.misc\" \"\")))

As you can see, this variable is a list of lists, where the first
element in each \"rule\" is the name of the group (which, by the way,
does not have to be called anything beginning with \"mail\",
\"yonka.zow\" is a fine, fine name), and the second is a regexp that
nnmail will try to match on the header to find a fit.

The second element can also be a function.  In that case, it will be
called narrowed to the headers with the first element of the rule as
the argument.  It should return a non-nil value if it thinks that the
mail belongs in that group.

The last element should always have \"\" as the regexp.

This variable can also have a function as its value.")

;; Suggested by Erik Selberg <speed@cs.washington.edu>.
(defvar nnmail-crosspost t
  "*If non-nil, do crossposting if several split methods match the mail.
If nil, the first match found will be used.")

;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
(defvar nnmail-keep-last-article nil
  "*If non-nil, nnmail will never delete the last expired article in a
directory.  You may need to set this variable if other programs are putting
new mail into folder numbers that Gnus has marked as expired.")

(defvar nnmail-expiry-wait 7
  "*Articles that are older than `nnmail-expiry-wait' days will be expired.")

(defvar nnmail-expiry-wait-function nil
  "*Variable that holds function to specify how old articles should be before they are expired.
  The function will be called with the name of the group that the
expiry is to be performed in, and it should return an integer that
says how many days an article can be stored before it is considered
'old'. 

Eg.:

(setq nnmail-expiry-wait-function
      (lambda (newsgroup)
        (cond ((string-match \"private\" newsgroup) 31)
              ((string-match \"junk\" newsgroup) 1)
	      (t 7))))")

(defvar nnmail-spool-file 
  (or (getenv "MAIL")
      (concat "/usr/spool/mail/" (user-login-name)))
  "Where the mail backends will look for incoming mail.
This variable is \"/usr/spool/mail/$user\" by default.
If this variable is nil, no mail backends will read incoming mail.
If this variable is a list, all files mentioned in this list will be
used as incoming mailboxes.")

(defvar nnmail-use-procmail nil
  "*If non-nil, the mail backends will look in `nnmail-procmail-directory' for spool files.
The file(s) in `nnmail-spool-file' will also be read.")

(defvar nnmail-procmail-directory "~/incoming/"
  "*When using procmail (and the like), incoming mail is put in this directory.
The Gnus mail backends will read the mail from this directory.")

(defvar nnmail-procmail-suffix ".spool"
  "*Suffix of files created by procmail (and the like).
This variable might be a suffix-regexp to match the suffixes of
several files - eg. \".spool[0-9]*\".")

(defvar nnmail-resplit-incoming nil
  "*If non-nil, re-split incoming procmail sorted mail.")

(defvar nnmail-movemail-program "movemail"
  "*A command to be executed to move mail from the inbox.
The default is \"movemail\".")

(defvar nnmail-read-incoming-hook nil
  "*Hook that will be run after the incoming mail has been transferred.
The incoming mail is moved from `nnmail-spool-file' (which normally is
something like \"/usr/spool/mail/$user\") to the user's home
directory. This hook is called after the incoming mail box has been
emptied, and can be used to call any mail box programs you have
running (\"xwatch\", etc.)

Eg.

(add-hook 'nnmail-read-incoming-hook 
	   (lambda () 
	     (start-process \"mailsend\" nil 
			    \"/local/bin/mailsend\" \"read\" \"mbox\")))")

;; Suggested by Erik Selberg <speed@cs.washington.edu>.
(defvar nnmail-prepare-incoming-hook nil
  "*Hook called before treating incoming mail.
The hook is run in a buffer with all the new, incoming mail.")

;; Suggested by Mejia Pablo J <pjm9806@usl.edu>.
(defvar nnmail-tmp-directory nil
  "*If non-nil, use this directory for temporary storage when reading incoming mail.")

(defvar nnmail-large-newsgroup 50
  "*The number of the articles which indicates a large newsgroup.
If the number of the articles is greater than the value, verbose
messages will be shown to indicate the current status.")

(defvar nnmail-split-fancy "mail.misc"
  "*Incoming mail can be split according to this fancy variable.
To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.

The format is this variable is SPLIT, where SPLIT can be one of
the following:

GROUP: Mail will be stored in GROUP (a string).

\(FIELD VALUE SPLIT): If the message field FIELD (a regexp) contains
  VALUE (a regexp), store the messages as specified by SPLIT.

\(| SPLIT...): Process each SPLIT expression until one of them matches.
  A SPLIT expression is said to match if it will cause the mail
  message to be stored in one or more groups.  

\(& SPLIT...): Process each SPLIT expression.

FIELD must match a complete field name.  VALUE must match a complete
word according to the fundamental mode syntax table.  You can use .*
in the regexps to match partial field names or words.

FIELD and VALUE can also be lisp symbols, in that case they are expanded
as specified in `nnmail-split-abbrev-alist'.

Example:

\(setq nnmail-split-methods 'nnmail-split-fancy
      nnmail-split-fancy
      ;; Messages from the mailer deamon are not crossposted to any of
      ;; the ordinary groups.  Warnings are put in a separate group
      ;; from real errors.
      '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
			  \"mail.misc\"))
	  ;; Non-error messages are crossposted to all relevant
	  ;; groups, but we don't crosspost between the group for the
	  ;; (ding) list and the group for other (ding) related mail.
	  (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
		(\"subject\" \"ding\" \"ding.misc\"))
	     ;; Other mailing lists...
	     (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
	     (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
	     ;; People...
	     (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
	  ;; Unmatched mail goes to the catch all group.
	  \"misc.misc\"))")

(defvar nnmail-split-abbrev-alist
  '((any . "from\\|to\\|cc\\|sender\\|apparently-to")
    (mail . "mailer-daemon\\|postmaster"))
  "*Alist of abbreviations allowed in `nnmail-split-fancy'.")

(defvar nnmail-delete-incoming t
  "*If non-nil, the mail backends will delete incoming files after splitting.")

(defvar nnmail-message-id-cache-length 1000
  "*The approximate number of Message-IDs nnmail will keep in its cache.
If this variable is nil, no checking on duplicate messages will be
performed.")

(defvar nnmail-message-id-cache-file "~/.nnmail-cache"
  "*The file name of the nnmail Message-ID cache.")

(defvar nnmail-delete-duplicates nil
  "*If non-nil, nnmail will delete any duplicate mails it sees.")



(defconst nnmail-version "nnmail 1.0"
  "nnmail version.")



(defun nnmail-request-post (&optional server)
  (mail-send-and-exit nil))

(defun nnmail-request-post-buffer (post group subject header article-buffer
					info follow-to respect-poster)
  (let ((method-address (cdr (assq 'to-address (nth 5 info))))
	from date to reply-to message-of
	references message-id cc new-cc sendto elt)
    (setq method-address
	  (if (and (stringp method-address) 
		   (string= method-address ""))
	      nil method-address))
    (save-excursion
      (set-buffer (get-buffer-create "*mail*"))
      (mail-mode)
      (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
      (if (and (buffer-modified-p)
	       (> (buffer-size) 0)
	       (not (y-or-n-p "Unsent mail being composed; erase it? ")))
	  ()
	(erase-buffer)
	(if post
	    (progn
	      (mail-setup method-address subject nil nil nil nil)
	      (auto-save-mode auto-save-default))
	  (save-excursion
	    (set-buffer article-buffer)
	    (goto-char (point-min))
	    (narrow-to-region (point-min)
			      (progn (search-forward "\n\n") (point)))
	    (let ((buffer-read-only nil))
	      (set-text-properties (point-min) (point-max) nil))
	    (setq from (mail-header-from header))
	    (setq date (mail-header-date header))
	    (and from
		 (let ((stop-pos 
			(string-match "  *at \\|  *@ \\| *(\\| *<" from)))
		   (setq message-of
			 (concat (if stop-pos (substring from 0 stop-pos) from)
				 "'s message of " date))))
	    (setq cc (mail-strip-quoted-names (or (mail-fetch-field "cc") "")))
	    (setq to (mail-strip-quoted-names (or (mail-fetch-field "to") "")))
	    (setq new-cc (rmail-dont-reply-to 
			  (concat (or to "")
				  (if cc (concat (if to ", " "") cc) ""))))
	    (let ((rmail-dont-reply-to-names 
		   (regexp-quote (mail-strip-quoted-names
				  (or method-address reply-to from "")))))
	      (setq new-cc (rmail-dont-reply-to new-cc)))
	    (setq subject (mail-header-subject header))
	    (or (string-match "^[Rr][Ee]:" subject)
		(setq subject (concat "Re: " subject)))
	    (setq reply-to (mail-fetch-field "reply-to"))
	    (setq references (mail-header-references header))
	    (setq message-id (mail-header-id header))
	    (widen))
	  (setq news-reply-yank-from from)
	  (setq news-reply-yank-message-id message-id)
	  
	  ;; Gather the "to" addresses out of the follow-to list and remove
	  ;; them as we go.
	  (if (and follow-to (listp follow-to))
	      (while (setq elt (assoc "To" follow-to))
		(setq sendto (concat sendto (and sendto ", ") (cdr elt)))
		(setq follow-to (delq elt follow-to))))
	  (mail-setup (if (and follow-to (listp follow-to)) 
			  sendto
			(or method-address reply-to from ""))
		      subject message-of 
		      (if (zerop (length new-cc)) nil new-cc)
		      article-buffer nil)
	  (auto-save-mode auto-save-default)
	  ;; Note that "To" elements should already be in the message.
	  (if (and follow-to (listp follow-to))
	      (progn
		(goto-char (point-min))
		(re-search-forward "^To:" nil t)
		(beginning-of-line)
		(forward-line 1)
		(while follow-to
		  (insert 
		   (car (car follow-to)) ": " (cdr (car follow-to)) "\n")
		  (setq follow-to (cdr follow-to)))))
	  (nnheader-insert-references references message-id)))
      (current-buffer))))

(defun nnmail-find-file (file)
  "Insert FILE in server buffer safely."
  (set-buffer nntp-server-buffer)
  (erase-buffer)
  (condition-case ()
      (progn (insert-file-contents file) t)
    (file-error nil)))

(defun nnmail-article-pathname (group mail-dir)
  "Make pathname for GROUP."
  (concat (file-name-as-directory (expand-file-name mail-dir))
	  (nnmail-replace-chars-in-string group ?. ?/) "/"))

(defun nnmail-replace-chars-in-string (string from to)
  "Replace characters in STRING from FROM to TO."
  (let ((string (substring string 0))	;Copy string.
	(len (length string))
	(idx 0))
    ;; Replace all occurrences of FROM with TO.
    (while (< idx len)
      (if (= (aref string idx) from)
	  (aset string idx to))
      (setq idx (1+ idx)))
    string))

(defun nnmail-days-between (date1 date2)
  ;; Return the number of days between date1 and date2.
  (let ((d1 (mapcar (lambda (s) (and s (string-to-int s)) )
		    (timezone-parse-date date1)))
	(d2 (mapcar (lambda (s) (and s (string-to-int s)) )
		    (timezone-parse-date date2))))
    (- (timezone-absolute-from-gregorian 
	(nth 1 d1) (nth 2 d1) (car d1))
       (timezone-absolute-from-gregorian 
	(nth 1 d2) (nth 2 d2) (car d2)))))

;; Function taken from rmail.el.
(defun nnmail-move-inbox (inbox tofile)
  (let ((inbox (file-truename
		(expand-file-name (substitute-in-file-name inbox))))
	movemail popmail errors)
    ;; Check whether the inbox is to be moved to the special tmp dir. 
    (if nnmail-tmp-directory
	(setq tofile (concat (file-name-as-directory nnmail-tmp-directory)
			     (file-name-nondirectory tofile))))
    ;; Make the filename unique.
    (setq tofile (nnmail-make-complex-temp-name (expand-file-name tofile)))
    ;; We create the directory the tofile is to reside in if it
    ;; doesn't exist.
    (or (file-exists-p (file-name-directory tofile))
	(make-directory tofile 'parents))
    ;; If getting from mail spool directory,
    ;; use movemail to move rather than just renaming,
    ;; so as to interlock with the mailer.
    (or (setq popmail (string-match "^po:" (file-name-nondirectory inbox)))
	(setq movemail t))
    (if popmail (setq inbox (file-name-nondirectory inbox)))
    (if movemail
	;; On some systems, /usr/spool/mail/foo is a directory
	;; and the actual inbox is /usr/spool/mail/foo/foo.
	(if (file-directory-p inbox)
	    (setq inbox (expand-file-name (user-login-name) inbox))))
    (if popmail
	(message "Getting mail from post office ...")
      (if (or (and (file-exists-p tofile)
		   (/= 0 (nth 7 (file-attributes tofile))))
	      (and (file-exists-p inbox)
		   (/= 0 (nth 7 (file-attributes inbox)))))
	  (message "Getting mail from %s..." inbox)))
    ;; Set TOFILE if have not already done so, and
    ;; rename or copy the file INBOX to TOFILE if and as appropriate.
    (cond ((or (file-exists-p tofile) (and (not popmail)
					   (not (file-exists-p inbox))))
	   nil)
	  ((and (not movemail) (not popmail))
	   ;; Try copying.  If that fails (perhaps no space),
	   ;; rename instead.
	   (condition-case nil
	       (copy-file inbox tofile nil)
	     (error
	      ;; Third arg is t so we can replace existing file TOFILE.
	      (rename-file inbox tofile t)))
	   ;; Make the real inbox file empty.
	   ;; Leaving it deleted could cause lossage
	   ;; because mailers often won't create the file.
	   (condition-case ()
	       (write-region (point) (point) inbox)
	     (file-error nil)))
	  (t
	   (unwind-protect
	       (save-excursion
		 (setq errors (generate-new-buffer " *nnmail loss*"))
		 (buffer-disable-undo errors)
		 (call-process
		  (expand-file-name nnmail-movemail-program exec-directory)
		  nil errors nil inbox tofile)
		 (if (not (buffer-modified-p errors))
		     ;; No output => movemail won
		     nil
		   (set-buffer errors)
		   (subst-char-in-region (point-min) (point-max) ?\n ?\  )
		   (goto-char (point-max))
		   (skip-chars-backward " \t")
		   (delete-region (point) (point-max))
		   (goto-char (point-min))
		   (if (looking-at "movemail: ")
		       (delete-region (point-min) (match-end 0)))
		   (beep t)
		   (message "movemail: %s"
			    (buffer-substring (point-min)
					      (point-max)))
		   (sit-for 3)
		   nil)))))
    (and errors
	 (buffer-name errors)
	 (kill-buffer errors))
    tofile))


(defun nnmail-get-active ()
  "Returns an assoc of group names and active ranges.
nn*-request-list should have been called before calling this function."
  (let (group-assoc)
    ;; Go through all groups from the active list.
    (save-excursion
      (set-buffer nntp-server-buffer)
      (goto-char (point-min))
      (while (re-search-forward 
	      "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)" nil t)
	(setq group-assoc
	      (cons (list (buffer-substring (match-beginning 1) 
					    (match-end 1))
			  (cons (string-to-int 
				 (buffer-substring (match-beginning 3)
						   (match-end 3)))
				(string-to-int 
				 (buffer-substring (match-beginning 2)
						   (match-end 2)))))
		    group-assoc))))

    ;;    ;; In addition, add all groups mentioned in `nnmail-split-methods'.
    ;;    (let ((methods (and (not (symbolp nnmail-split-methods))
    ;;			nnmail-split-methods)))
    ;;      (while methods
    ;;	(if (not (assoc (car (car methods)) group-assoc))
    ;;	    (setq group-assoc
    ;;		  (cons (list (car (car methods)) (cons 1 0)) 
    ;;			group-assoc)))
    ;;	(setq methods (cdr methods)))
    
    group-assoc))

(defun nnmail-save-active (group-assoc file-name)
  (let (group)
    (save-excursion
      (set-buffer (get-buffer-create " *nnmail active*"))
      (buffer-disable-undo (current-buffer))
      (erase-buffer)
      (while group-assoc
	(setq group (car group-assoc))
	(insert (format "%s %d %d y\n" (car group) (cdr (car (cdr group)) )
			(car (car (cdr group)))))
	(setq group-assoc (cdr group-assoc)))
      (write-region 1 (point-max) (expand-file-name file-name) nil 'nomesg)
      (kill-buffer (current-buffer)))))

(defun nnmail-get-split-group (file group)
  (if (or (eq nnmail-spool-file 'procmail)
	  nnmail-use-procmail)
      (cond (group group)
	    ((string-match (concat "^" (expand-file-name
					(file-name-as-directory
					 nnmail-procmail-directory))
				   "\\(.*\\)" nnmail-procmail-suffix "$")
			   (expand-file-name file))
	     (substring (expand-file-name file)
			(match-beginning 1) (match-end 1)))
	    (t
	     group))
    group))

(defun nnmail-split-incoming (incoming func &optional dont-kill group)
  "Go through the entire INCOMING file and pick out each individual mail.
FUNC will be called with the buffer narrowed to each mail."
  (let ((delim (concat "^" rmail-unix-mail-delimiter))
	;; If this is a group-specific split, we bind the split
	;; methods to just this group.
	(nnmail-split-methods (if (and group
				       (or (eq nnmail-spool-file 'procmail)
					   nnmail-use-procmail)
				       (not nnmail-resplit-incoming))
				  (list (list group ""))
				nnmail-split-methods))
	start end content-length do-search message-id)
    (save-excursion
      ;; Open the message-id cache.
      (nnmail-cache-open)
      ;; Insert the incoming file.
      (set-buffer (get-buffer-create " *nnmail incoming*"))
      (buffer-disable-undo (current-buffer))
      (erase-buffer)
      (insert-file-contents incoming)
      (goto-char (point-min))
      (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
      ;; Go to the beginning of the first mail...
      (if (and (re-search-forward delim nil t)
	       (goto-char (match-beginning 0)))
	  ;; and then carry on until the bitter end.
	  (while (not (eobp))
	    (setq start (point))
	    ;; Skip all the headers in case there are more "From "s...
	    (if (not (search-forward "\n\n" nil t))
		(forward-line 1))
	    ;; Find the Message-ID header.
	    (save-excursion
	      (if (re-search-backward "^Message-ID:[ \t]*\\(<[^>]*>\\)" nil t)
		  (setq message-id (buffer-substring (match-beginning 1)
						     (match-end 1)))
		;; There is no Message-ID here, so we create one.
		(forward-line -1)
		(insert "Message-ID: " (setq message-id (nnmail-message-id))
			"\n")))
	    ;; Look for a Content-Length header.
	    (if (not (save-excursion
		       (and (re-search-backward 
			     "^Content-Length: \\([0-9]+\\)" start t)
			    (setq content-length (string-to-int
						  (buffer-substring 
						   (match-beginning 1)
						   (match-end 1))))
			    ;; We destroy the header, since none of
			    ;; the backends ever use it, and we do not
			    ;; want to confuse other mailers by having
			    ;; a (possibly) faulty header.
			    (progn (insert "X-") t))))
		(setq do-search t)
	      (if (or (= (+ (point) content-length) (point-max))
		      (save-excursion
			(goto-char (+ (point) content-length))
			(looking-at delim)))
		  (progn
		    (goto-char (+ (point) content-length))
		    (setq do-search nil))
		(setq do-search t)))
	    ;; Go to the beginning of the next article - or to the end
	    ;; of the buffer.  
	    (if do-search
		(if (re-search-forward delim nil t)
		    (goto-char (match-beginning 0))
		  (goto-char (point-max))))
	    (save-excursion
	      (save-restriction
		(narrow-to-region start (point))
		(goto-char (point-min))
		;; If this is a duplicate message, then we do not save it.
		(if (nnmail-cache-id-exists-p message-id)
		    (delete-region (point-min) (point-max))
		  (nnmail-cache-insert message-id)
		  (funcall func))
		(setq end (point-max))))
	    (goto-char end)))
      ;; Close the message-id cache.
      (nnmail-cache-close)
      (if dont-kill
	  (current-buffer)
	(kill-buffer (current-buffer))))))

;; Mail crossposts suggested by Brian Edmonds <edmonds@cs.ubc.ca>. 
(defun nnmail-article-group (func)
  "Look at the headers and return an alist of groups that match.
FUNC will be called with the group name to determine the article number."
  (let ((methods nnmail-split-methods)
	(obuf (current-buffer))
	(beg (point-min))
	end group-art)
    (if (and (sequencep methods) (= (length methods) 1))
	;; If there is only just one group to put everything in, we
	;; just return a list with just this one method in.
	(setq group-art
	      (list (cons (car (car methods))
			  (funcall func (car (car methods))))))
      ;; We do actual comparison.
      (save-excursion
	;; Find headers.
	(goto-char beg)
	(setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
	(set-buffer (get-buffer-create " *nnmail work*"))
	(buffer-disable-undo (current-buffer))
	(erase-buffer)
	;; Copy the headers into the work buffer.
	(insert-buffer-substring obuf beg end)
	;; Fold continuation lines.
	(goto-char (point-min))
	(while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
	  (replace-match " " t t))
	(if (and (symbolp nnmail-split-methods)
		 (fboundp nnmail-split-methods))
	    (setq group-art
		  (mapcar
		   (lambda (group) (cons group (funcall func group)))
		   (condition-case nil
		       (funcall nnmail-split-methods)
		     (error
		      (message "\
Problems with `nnmail-split-methods', using `bogus' mail group")
		      (sit-for 1)
		      '("bogus")))))
	  ;; Go through the split methods to find a match.
	  (while (and methods (or nnmail-crosspost (not group-art)))
	    (goto-char (point-max))
	    (if (or (cdr methods)
		    (not (equal "" (nth 1 (car methods)))))
		(if (and (condition-case () 
			     (if (stringp (nth 1 (car methods)))
				 (re-search-backward
				  (car (cdr (car methods))) nil t)
			       ;; Suggested by Brian Edmonds 
			       ;; <edmonds@cs.ubc.ca>.
			       (funcall (nth 1 (car methods)) 
					(car (car methods))))
			   (error nil))
			 ;; Don't enter the article into the same group twice.
			 (not (assoc (car (car methods)) group-art)))
		    (setq group-art
			  (cons (cons (car (car methods))
				      (funcall func (car (car methods)))) 
				group-art)))
	      (or group-art
		  (setq group-art 
			(list (cons (car (car methods)) 
				    (funcall func (car (car methods))))))))
	    (setq methods (cdr methods))))
	(kill-buffer (current-buffer))
	group-art))))

(defun nnmail-insert-lines ()
  "Insert how many lines and chars there are in the body of the mail."
  (let (lines chars)
    (save-excursion
      (goto-char (point-min))
      (if (search-forward "\n\n" nil t) 
	  (progn
	    (setq chars (- (point-max) (point)))
	    (setq lines (- (count-lines (point) (point-max)) 1))
	    (forward-char -1)
	    (save-excursion
	      (if (re-search-backward "^Lines: " nil t)
		  (delete-region (point) (progn (forward-line 1) (point)))))
	    (insert (format "Lines: %d\n" lines))
	    chars)))))

(defun nnmail-insert-xref (group-alist)
  "Insert an Xref line based on the (group . article) alist."
  (save-excursion
    (goto-char (point-min))
    (if (search-forward "\n\n" nil t) 
	(progn
	  (forward-char -1)
	  (if (re-search-backward "^Xref: " nil t)
	      (delete-region (match-beginning 0) 
			     (progn (forward-line 1) (point))))
	  (insert (format "Xref: %s" (system-name)))
	  (while group-alist
	    (insert (format " %s:%d" (car (car group-alist)) 
			    (cdr (car group-alist))))
	    (setq group-alist (cdr group-alist)))
	  (insert "\n")))))

;; Written by byer@mv.us.adobe.com (Scott Byer).
(defun nnmail-make-complex-temp-name (prefix)
  (let ((newname (make-temp-name prefix))
	(newprefix prefix))
    (while (file-exists-p newname)
      (setq newprefix (concat newprefix "x"))
      (setq newname (make-temp-name newprefix)))
    newname))

;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.

(defun nnmail-split-fancy ()
  "Fancy splitting method.
See the documentation for the variable `nnmail-split-fancy' for documentation."
  (nnmail-split-it nnmail-split-fancy))

(defvar nnmail-split-cache nil)
;; Alist of split expressions their equivalent regexps.

(defun nnmail-split-it (split)
  ;; Return a list of groups matching SPLIT.
  (cond ((stringp split)
	 ;; A group.
	 (list split))
	((eq (car split) '&)
	 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
	((eq (car split) '|)
	 (let (done)
	   (while (and (not done) (cdr split))
	     (setq split (cdr split)
		   done (nnmail-split-it (car split))))
	   done))	((assq split nnmail-split-cache)
			 ;; A compiled match expression.
	 (goto-char (point-max))
	 (if (re-search-backward (cdr (assq split nnmail-split-cache)) nil t)
	     (nnmail-split-it (nth 2 split))))
	(t
	 ;; An uncompiled match.
	 (let* ((field (nth 0 split))
		(value (nth 1 split))
		(regexp (concat "^\\(" 
				(if (symbolp field)
				    (cdr (assq field 
					       nnmail-split-abbrev-alist))
				  field)
				"\\):.*\\<\\("
				(if (symbolp value)
				    (cdr (assq value
					       nnmail-split-abbrev-alist))
				  value)
				"\\>\\)")))
	   (setq nnmail-split-cache 
		 (cons (cons split regexp) nnmail-split-cache))
	   (goto-char (point-max))
	   (if (re-search-backward regexp nil t)
	       (nnmail-split-it (nth 2 split)))))))

;; Get a list of spool files to read.
(defun nnmail-get-spool-files (&optional group)
  (if (null nnmail-spool-file)
      ;; No spool file whatsoever.
      nil)
  (let* ((procmails 
	  ;; If procmail is used to get incoming mail, the files
	  ;; are stored in this directory.
	  (and (file-exists-p nnmail-procmail-directory)
	       (directory-files 
		nnmail-procmail-directory 
		t (concat (if group group "")
			  nnmail-procmail-suffix "$") t)))
	 (p procmails))
    ;; Remove any directories that inadvertently match the procmail
    ;; suffix, which might happen if the suffix is "".
    (while p
      (and (or (file-directory-p (car p))
	       (file-symlink-p (car p)))
	   (setq procmails (delete (car p) procmails)))
      (setq p (cdr p)))
    (cond ((listp nnmail-spool-file)
	   (append nnmail-spool-file procmails))
	  ((stringp nnmail-spool-file)
	   (cons nnmail-spool-file procmails))
	  (t
	   procmails))))

;; Activate a backend only if it isn't already activated. 
;; If FORCE, re-read the active file even if the backend is 
;; already activated.
(defun nnmail-activate (backend &optional force)
  (let (file timestamp file-time)
    (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
	    force
	    (and (setq file (condition-case ()
				(symbol-value (intern (format "%s-active-file" 
							      backend)))
			      (error nil)))
		 (setq file-time (nth 5 (file-attributes file)))
		 (or (not
		      (setq timestamp
			    (condition-case ()
				(symbol-value (intern
					       (format "%s-active-timestamp" 
						       backend)))
			      (error 'none))))
		     (not (consp timestamp))
		     (equal timestamp '(0 0))
		     (> (nth 0 file-time) (nth 0 timestamp))
		     (and (= (nth 0 file-time) (nth 0 timestamp))
			  (> (nth 1 file-time) (nth 1 timestamp))))))
	(save-excursion
	  (or (eq timestamp 'none)
	      (set (intern (format "%s-active-timestamp" backend)) 
		   (current-time)))
	  (funcall (intern (format "%s-request-list" backend)))
	  (set (intern (format "%s-group-alist" backend)) 
	       (nnmail-get-active))))
    t))

(defun nnmail-message-id ()
  (concat "<" (nnmail-unique-id) "@totally-fudged-out-message-id>"))

(defvar nnmail-unique-id-char nil)

(defun nnmail-number-base36 (num len)
  (if (if (< len 0) (<= num 0) (= len 0))
      ""
    (concat (nnmail-number-base36 (/ num 36) (1- len))
	    (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
				  (% num 36))))))

(defun nnmail-unique-id ()
  (setq nnmail-unique-id-char
	(% (1+ (or nnmail-unique-id-char (logand (random t) (1- (lsh 1 20)))))
	   ;; (current-time) returns 16-bit ints,
	   ;; and 2^16*25 just fits into 4 digits i base 36.
	   (* 25 25)))
  (let ((tm (if (fboundp 'current-time)
		(current-time) '(12191 46742 287898))))
    (concat
     (nnmail-number-base36 (+ (car   tm) 
			      (lsh (% nnmail-unique-id-char 25) 16)) 4)
     (nnmail-number-base36 (+ (nth 1 tm) 
			      (lsh (/ nnmail-unique-id-char 25) 16)) 4))))

;;;
;;; nnmail duplicate handling
;;;

(defvar nnmail-cache-buffer nil)

(defun nnmail-cache-open ()
  (if (or (not nnmail-delete-duplicates)
	  (and nnmail-cache-buffer
	       (buffer-name nnmail-cache-buffer)))
      ()				; The buffer is open.
    (save-excursion
      (set-buffer 
       (setq nnmail-cache-buffer 
	     (get-buffer-create " *nnmail message-id cache*")))
      (buffer-disable-undo (current-buffer))
      (and (file-exists-p nnmail-message-id-cache-file)
	   (insert-file-contents nnmail-message-id-cache-file))
      (current-buffer))))

(defun nnmail-cache-close ()
  (if (or (not nnmail-cache-buffer)
	  (not nnmail-delete-duplicates)
	  (not (buffer-name nnmail-cache-buffer))
	  (not (buffer-modified-p nnmail-cache-buffer)))
      ()				; The buffer is closed.
    (save-excursion
      (set-buffer nnmail-cache-buffer)
      ;; Weed out the excess number of Message-IDs.
      (goto-char (point-max))
      (and (search-backward "\n" nil t nnmail-message-id-cache-length)
	   (progn
	     (beginning-of-line)
	     (delete-region (point-min) (point))))
      ;; Save the buffer.
      (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
	  (make-directory (file-name-directory nnmail-message-id-cache-file)
			  t))
      (write-region (point-min) (point-max)
		    nnmail-message-id-cache-file nil 'silent)
      (set-buffer-modified-p nil))))

(defun nnmail-cache-insert (id)
  (and nnmail-delete-duplicates
       (save-excursion
	 (set-buffer nnmail-cache-buffer)
	 (goto-char (point-max))
	 (insert id "\n"))))

(defun nnmail-cache-id-exists-p (id)
  (and nnmail-delete-duplicates
       (save-excursion
	 (set-buffer nnmail-cache-buffer)
	 (goto-char (point-max))
	 (search-backward id nil t))))


(provide 'nnmail)

;;; nnmail.el ends here
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.