]> code.delx.au - gnu-emacs/blob - lisp/gnus/nnmail.el
Merge from origin/emacs-25
[gnu-emacs] / lisp / gnus / nnmail.el
1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2
3 ;; Copyright (C) 1995-2016 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28
29 (require 'gnus) ; for macro gnus-kill-buffer, at least
30 (require 'nnheader)
31 (require 'message)
32 (require 'gnus-util)
33 (require 'mail-source)
34 (require 'mm-util)
35 (require 'gnus-int)
36
37 (autoload 'gnus-add-buffer "gnus")
38 (autoload 'gnus-kill-buffer "gnus")
39 (autoload 'mail-send-and-exit "sendmail" nil t)
40
41 (defgroup nnmail nil
42 "Reading mail with Gnus."
43 :group 'gnus)
44
45 (defgroup nnmail-retrieve nil
46 "Retrieving new mail."
47 :group 'nnmail)
48
49 (defgroup nnmail-prepare nil
50 "Preparing (or mangling) new mail after retrieval."
51 :group 'nnmail)
52
53 (defgroup nnmail-duplicate nil
54 "Handling of duplicate mail messages."
55 :group 'nnmail)
56
57 (defgroup nnmail-split nil
58 "Organizing the incoming mail in folders."
59 :group 'nnmail)
60
61 (defgroup nnmail-files nil
62 "Mail files."
63 :group 'gnus-files
64 :group 'nnmail)
65
66 (defgroup nnmail-expire nil
67 "Expiring old mail."
68 :group 'nnmail)
69
70 (defgroup nnmail-procmail nil
71 "Interfacing with procmail and other mail agents."
72 :group 'nnmail)
73
74 (defgroup nnmail-various nil
75 "Various mail options."
76 :group 'nnmail)
77
78 (defcustom nnmail-split-methods '(("mail.misc" ""))
79 "*Incoming mail will be split according to this variable.
80
81 If you'd like, for instance, one mail group for mail from the
82 \"4ad-l\" mailing list, one group for junk mail and one for everything
83 else, you could do something like this:
84
85 (setq nnmail-split-methods
86 \\='((\"mail.4ad\" \"From:.*4ad\")
87 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
88 (\"mail.misc\" \"\")))
89
90 As you can see, this variable is a list of lists, where the first
91 element in each \"rule\" is the name of the group (which, by the way,
92 does not have to be called anything beginning with \"mail\",
93 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
94 nnmail will try to match on the header to find a fit.
95
96 The second element can also be a function. In that case, it will be
97 called narrowed to the headers with the first element of the rule as
98 the argument. It should return a non-nil value if it thinks that the
99 mail belongs in that group.
100
101 The last element should always have \"\" as the regexp.
102
103 This variable can also have a function as its value, and it can
104 also have a fancy split method as its value. See
105 `nnmail-split-fancy' for an explanation of that syntax."
106 :group 'nnmail-split
107 :type '(choice (repeat :tag "Alist" (group (string :tag "Name")
108 (choice regexp function)))
109 (function-item nnmail-split-fancy)
110 (function :tag "Other")))
111
112 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
113 (defcustom nnmail-crosspost t
114 "If non-nil, do crossposting if several split methods match the mail.
115 If nil, the first match found will be used."
116 :group 'nnmail-split
117 :type 'boolean)
118
119 (defcustom nnmail-split-fancy-with-parent-ignore-groups nil
120 "Regexp that matches group names to be ignored when applying `nnmail-split-fancy-with-parent'.
121 This can also be a list of regexps."
122 :version "22.1"
123 :group 'nnmail-split
124 :type '(choice (const :tag "none" nil)
125 (regexp :value ".*")
126 (repeat :value (".*") regexp)))
127
128 (defcustom nnmail-cache-ignore-groups nil
129 "Regexp that matches group names to be ignored when inserting message ids into the cache (`nnmail-cache-insert').
130 This can also be a list of regexps."
131 :version "22.1"
132 :group 'nnmail-split
133 :type '(choice (const :tag "none" nil)
134 (regexp :value ".*")
135 (repeat :value (".*") regexp)))
136
137 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
138 (defcustom nnmail-keep-last-article nil
139 "If non-nil, nnmail will never delete/move a group's last article.
140 It can be marked expirable, so it will be deleted when it is no longer last.
141
142 You may need to set this variable if other programs are putting
143 new mail into folder numbers that Gnus has marked as expired."
144 :group 'nnmail-procmail
145 :group 'nnmail-various
146 :type 'boolean)
147
148 (defcustom nnmail-use-long-file-names nil
149 "If non-nil the mail backends will use long file and directory names.
150 If nil, groups like \"mail.misc\" will end up in directories like
151 \"mail/misc/\"."
152 :group 'nnmail-files
153 :type 'boolean)
154
155 (defcustom nnmail-default-file-modes 384
156 "Set the mode bits of all new mail files to this integer."
157 :group 'nnmail-files
158 :type 'integer)
159
160 (defcustom nnmail-expiry-wait 7
161 "*Expirable articles that are older than this will be expired.
162 This variable can either be a number (which will be interpreted as a
163 number of days) -- this doesn't have to be an integer. This variable
164 can also be `immediate' and `never'."
165 :group 'nnmail-expire
166 :type '(choice (const immediate)
167 (number :tag "days")
168 (const never)))
169
170 (defcustom nnmail-expiry-wait-function nil
171 "Variable that holds function to specify how old articles should be before they are expired.
172 The function will be called with the name of the group that the expiry
173 is to be performed in, and it should return an integer that says how
174 many days an article can be stored before it is considered \"old\".
175 It can also return the values `never' and `immediate'.
176
177 E.g.:
178
179 \(setq nnmail-expiry-wait-function
180 (lambda (newsgroup)
181 (cond ((string-match \"private\" newsgroup) 31)
182 ((string-match \"junk\" newsgroup) 1)
183 ((string-match \"important\" newsgroup) \\='never)
184 (t 7))))"
185 :group 'nnmail-expire
186 :type '(choice (const :tag "nnmail-expiry-wait" nil)
187 (function :format "%v" nnmail-)))
188
189 (defcustom nnmail-expiry-target 'delete
190 "*Variable that says where expired messages should end up.
191 The default value is `delete' (which says to delete the messages),
192 but it can also be a string or a function. If it is a string, expired
193 messages end up in that group. If it is a function, the function is
194 called in a buffer narrowed to the message in question. The function
195 receives one argument, the name of the group the message comes from.
196 The return value should be `delete' or a group name (a string)."
197 :version "21.1"
198 :group 'nnmail-expire
199 :type '(choice (const delete)
200 function
201 string))
202
203 (defcustom nnmail-fancy-expiry-targets nil
204 "Determine expiry target based on articles using fancy techniques.
205
206 This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries. If
207 `nnmail-expiry-target' is set to the function
208 `nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
209 the message will be expired to a group determined by invoking
210 `format-time-string' with TARGET used as the format string and the
211 time extracted from the articles' Date header (if missing the current
212 time is used).
213
214 In the special cases that HEADER is the symbol `to-from', the regexp
215 will try to match against both the From and the To header.
216
217 Example:
218
219 \(setq nnmail-fancy-expiry-targets
220 \\='((to-from \"boss\" \"nnfolder:Work\")
221 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
222 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
223
224 In this case, articles containing the string \"boss\" in the To or the
225 From header will be expired to the group \"nnfolder:Work\";
226 articles containing the string \"IMPORTANT\" in the Subject header will
227 be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
228 everything else will be expired to \"nnfolder:Archive-YYYY\"."
229 :version "22.1"
230 :group 'nnmail-expire
231 :type '(repeat (list (choice :tag "Match against"
232 (string :tag "Header")
233 (const to-from))
234 regexp
235 (string :tag "Target group format string"))))
236
237 (defcustom nnmail-cache-accepted-message-ids nil
238 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
239 If non-nil, also update the cache when copy or move articles."
240 :group 'nnmail
241 :type 'boolean)
242
243 (make-obsolete-variable 'nnmail-spool-file 'mail-sources
244 "Gnus 5.9 (Emacs 22.1)")
245 ;; revision 5.29 / p0-85 / Gnus 5.9
246 ;; Variable removed in No Gnus v0.7
247
248 (defcustom nnmail-resplit-incoming nil
249 "*If non-nil, re-split incoming procmail sorted mail."
250 :group 'nnmail-procmail
251 :type 'boolean)
252
253 (defcustom nnmail-scan-directory-mail-source-once nil
254 "*If non-nil, scan all incoming procmail sorted mails once.
255 It scans low-level sorted spools even when not required."
256 :version "21.1"
257 :group 'nnmail-procmail
258 :type 'boolean)
259
260 (defcustom nnmail-delete-file-function 'delete-file
261 "Function called to delete files in some mail backends."
262 :group 'nnmail-files
263 :type 'function)
264
265 (defcustom nnmail-crosspost-link-function
266 (if (string-match "windows-nt" (symbol-name system-type))
267 'copy-file
268 'add-name-to-file)
269 "*Function called to create a copy of a file.
270 This is `add-name-to-file' by default, which means that crossposts
271 will use hard links. If your file system doesn't allow hard
272 links, you could set this variable to `copy-file' instead."
273 :group 'nnmail-files
274 :type '(radio (function-item add-name-to-file)
275 (function-item copy-file)
276 (function :tag "Other")))
277
278 (defcustom nnmail-read-incoming-hook
279 (if (eq system-type 'windows-nt)
280 '(nnheader-ms-strip-cr)
281 nil)
282 "*Hook that will be run after the incoming mail has been transferred.
283 The incoming mail is moved from the specified spool file (which normally is
284 something like \"/usr/spool/mail/$user\") to the user's home
285 directory. This hook is called after the incoming mail box has been
286 emptied, and can be used to call any mail box programs you have
287 running (\"xwatch\", etc.)
288
289 E.g.:
290
291 \(add-hook \\='nnmail-read-incoming-hook
292 (lambda ()
293 (call-process \"/local/bin/mailsend\" nil nil nil
294 \"read\"
295 ;; The incoming mail box file.
296 (expand-file-name (user-login-name)
297 rmail-spool-directory))))
298
299 If you have xwatch running, this will alert it that mail has been
300 read.
301
302 If you use `display-time', you could use something like this:
303
304 \(add-hook \\='nnmail-read-incoming-hook
305 (lambda ()
306 ;; Update the displayed time, since that will clear out
307 ;; the flag that says you have mail.
308 (when (eq (process-status \"display-time\") \\='run)
309 (display-time-filter display-time-process \"\"))))"
310 :group 'nnmail-prepare
311 :type 'hook)
312
313 (defcustom nnmail-prepare-incoming-hook nil
314 "Hook called before treating incoming mail.
315 The hook is run in a buffer with all the new, incoming mail."
316 :group 'nnmail-prepare
317 :type 'hook)
318
319 (defcustom nnmail-prepare-incoming-header-hook nil
320 "Hook called narrowed to the headers of each message.
321 This can be used to remove excessive spaces (and stuff like
322 that) from the headers before splitting and saving the messages."
323 :group 'nnmail-prepare
324 :type 'hook)
325
326 (defcustom nnmail-prepare-incoming-message-hook nil
327 "Hook called narrowed to each message."
328 :group 'nnmail-prepare
329 :type 'hook)
330
331 (defcustom nnmail-list-identifiers nil
332 "Regexp that matches list identifiers to be removed.
333 This can also be a list of regexps."
334 :group 'nnmail-prepare
335 :type '(choice (const :tag "none" nil)
336 (regexp :value ".*")
337 (repeat :value (".*") regexp)))
338
339 (defcustom nnmail-pre-get-new-mail-hook nil
340 "Hook called just before starting to handle new incoming mail."
341 :group 'nnmail-retrieve
342 :type 'hook)
343
344 (defcustom nnmail-post-get-new-mail-hook nil
345 "Hook called just after finishing handling new incoming mail."
346 :group 'nnmail-retrieve
347 :type 'hook)
348
349 (defcustom nnmail-split-hook nil
350 "Hook called before deciding where to split an article.
351 The functions in this hook are free to modify the buffer
352 contents in any way they choose -- the buffer contents are
353 discarded after running the split process."
354 :group 'nnmail-split
355 :type 'hook)
356
357 (defcustom nnmail-spool-hook nil
358 "*A hook called when a new article is spooled."
359 :version "22.1"
360 :group 'nnmail
361 :type 'hook)
362
363 (defcustom nnmail-large-newsgroup 50
364 "*The number of articles which indicates a large newsgroup or nil.
365 If the number of articles is greater than the value, verbose
366 messages will be shown to indicate the current status."
367 :group 'nnmail-various
368 :type '(choice (const :tag "infinite" nil)
369 (number :tag "count")))
370
371 (define-widget 'nnmail-split-fancy 'lazy
372 "Widget for customizing splits in the variable of the same name."
373 :tag "Split"
374 :type '(menu-choice :value (any ".*value.*" "misc")
375 :tag "Type"
376 (string :tag "Destination")
377 (list :tag "Use first match (|)" :value (|)
378 (const :format "" |)
379 (editable-list :inline t nnmail-split-fancy))
380 (list :tag "Use all matches (&)" :value (&)
381 (const :format "" &)
382 (editable-list :inline t nnmail-split-fancy))
383 (list :tag "Function with fixed arguments (:)"
384 :value (:)
385 (const :format "" :value :)
386 function
387 (editable-list :inline t (sexp :tag "Arg"))
388 )
389 (list :tag "Function with split arguments (!)"
390 :value (!)
391 (const :format "" !)
392 function
393 (editable-list :inline t nnmail-split-fancy))
394 (list :tag "Field match"
395 (choice :tag "Field"
396 regexp symbol)
397 (choice :tag "Match"
398 regexp
399 (symbol :value mail))
400 (repeat :inline t
401 :tag "Restrictions"
402 (group :inline t
403 (const :format "" -)
404 regexp))
405 nnmail-split-fancy)
406 (const :tag "Junk (delete mail)" junk)))
407
408 (defcustom nnmail-split-fancy "mail.misc"
409 "Incoming mail can be split according to this fancy variable.
410 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
411
412 The format of this variable is SPLIT, where SPLIT can be one of
413 the following:
414
415 GROUP: Mail will be stored in GROUP (a string).
416
417 \(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
418 field FIELD (a regexp) contains VALUE (a regexp), store the messages
419 as specified by SPLIT. If RESTRICT (a regexp) matches some string
420 after FIELD and before the end of the matched VALUE, return nil,
421 otherwise process SPLIT. Multiple RESTRICTs add up, further
422 restricting the possibility of processing SPLIT.
423
424 \(| SPLIT...): Process each SPLIT expression until one of them matches.
425 A SPLIT expression is said to match if it will cause the mail
426 message to be stored in one or more groups.
427
428 \(& SPLIT...): Process each SPLIT expression.
429
430 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
431 the buffer containing the message headers. The return value FUNCTION
432 should be a split, which is then recursively processed.
433
434 \(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT. The
435 return value FUNCTION should be a split, which is then recursively
436 processed.
437
438 junk: Mail will be deleted. Use with care! Do not submerge in water!
439 Example:
440 (setq nnmail-split-fancy
441 \\='(| (\"Subject\" \"MAKE MONEY FAST\" junk)
442 ...other.rules.omitted...))
443
444 FIELD must match a complete field name. VALUE must match a complete
445 word according to the `nnmail-split-fancy-syntax-table' syntax table.
446 You can use \".*\" in the regexps to match partial field names or words.
447
448 FIELD and VALUE can also be Lisp symbols, in that case they are expanded
449 as specified in `nnmail-split-abbrev-alist'.
450
451 GROUP can contain \\& and \\N which will substitute from matching
452 \\(\\) patterns in the previous VALUE.
453
454 Example:
455
456 \(setq nnmail-split-methods \\='nnmail-split-fancy
457 nnmail-split-fancy
458 ;; Messages from the mailer daemon are not crossposted to any of
459 ;; the ordinary groups. Warnings are put in a separate group
460 ;; from real errors.
461 \\='(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
462 \"mail.misc\"))
463 ;; Non-error messages are crossposted to all relevant
464 ;; groups, but we don't crosspost between the group for the
465 ;; (ding) list and the group for other (ding) related mail.
466 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
467 (\"subject\" \"ding\" \"ding.misc\"))
468 ;; Other mailing lists...
469 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
470 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
471 ;; Both lists below have the same suffix, so prevent
472 ;; cross-posting to mkpkg.list of messages posted only to
473 ;; the bugs- list, but allow cross-posting when the
474 ;; message was really cross-posted.
475 (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
476 (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
477 ;;
478 ;; People...
479 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
480 ;; Unmatched mail goes to the catch all group.
481 \"misc.misc\"))"
482 :group 'nnmail-split
483 :type 'nnmail-split-fancy)
484
485 (defcustom nnmail-split-abbrev-alist
486 '((any . "from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
487 (mail . "mailer-daemon\\|postmaster\\|uucp")
488 (to . "to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
489 (from . "from\\|sender\\|resent-from")
490 (nato . "to\\|cc\\|resent-to\\|resent-cc")
491 (naany . "from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
492 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
493 :group 'nnmail-split
494 :type '(repeat (cons :format "%v" symbol regexp)))
495
496 (defcustom nnmail-message-id-cache-length 1000
497 "*The approximate number of Message-IDs nnmail will keep in its cache.
498 If this variable is nil, no checking on duplicate messages will be
499 performed."
500 :group 'nnmail-duplicate
501 :type '(choice (const :tag "disable" nil)
502 (integer :format "%v")))
503
504 (defcustom nnmail-message-id-cache-file
505 (nnheader-concat gnus-home-directory ".nnmail-cache")
506 "The file name of the nnmail Message-ID cache."
507 :group 'nnmail-duplicate
508 :group 'nnmail-files
509 :type 'file)
510
511 (defcustom nnmail-treat-duplicates 'warn
512 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
513 Three values are valid: nil, which means that nnmail is not to keep a
514 Message-ID cache; `warn', which means that nnmail should insert extra
515 headers to warn the user about the duplication (this is the default);
516 and `delete', which means that nnmail will delete duplicated mails.
517
518 This variable can also be a function. It will be called from a buffer
519 narrowed to the article in question with the Message-ID as a
520 parameter. It should return nil, `warn' or `delete'."
521 :group 'nnmail-duplicate
522 :type '(choice (const :tag "off" nil)
523 (const warn)
524 (const delete)))
525
526 (defcustom nnmail-extra-headers '(To Newsgroups Cc)
527 "Extra headers to parse.
528 In addition to the standard headers, these extra headers will be
529 included in NOV headers (and the like) when backends parse headers."
530 :version "24.3"
531 :group 'nnmail
532 :type '(repeat symbol))
533
534 (defcustom nnmail-split-header-length-limit 2048
535 "Header lines longer than this limit are excluded from the split function."
536 :version "21.1"
537 :group 'nnmail
538 :type 'integer)
539
540 (defcustom nnmail-mail-splitting-charset nil
541 "Default charset to be used when splitting incoming mail."
542 :version "22.1"
543 :group 'nnmail
544 :type 'symbol)
545
546 (defcustom nnmail-mail-splitting-decodes nil
547 "Whether the nnmail splitting functionality should MIME decode headers."
548 :version "22.1"
549 :group 'nnmail
550 :type 'boolean)
551
552 (defcustom nnmail-split-fancy-match-partial-words nil
553 "Whether to match partial words when fancy splitting.
554 Normally, regexes given in `nnmail-split-fancy' are implicitly surrounded
555 by \"\\=\\<...\\>\". If this variable is true, they are not implicitly\
556 surrounded
557 by anything."
558 :version "22.1"
559 :group 'nnmail
560 :type 'boolean)
561
562 (defcustom nnmail-split-lowercase-expanded t
563 "Whether to lowercase expanded entries (i.e. \\N) when splitting mails.
564 This avoids the creation of multiple groups when users send to an address
565 using different case (i.e. mailing-list@domain vs Mailing-List@Domain)."
566 :version "22.1"
567 :group 'nnmail
568 :type 'boolean)
569
570 ;;; Internal variables.
571
572 (defvar nnmail-article-buffer " *nnmail incoming*"
573 "The buffer used for splitting incoming mails.")
574
575 (defvar nnmail-split-history nil
576 "List of group/article elements that say where the previous split put messages.")
577
578 (defvar nnmail-split-fancy-syntax-table
579 (let ((table (make-syntax-table)))
580 ;; support the %-hack
581 (modify-syntax-entry ?\% "." table)
582 table)
583 "Syntax table used by `nnmail-split-fancy'.")
584
585 (defvar nnmail-prepare-save-mail-hook nil
586 "Hook called before saving mail.")
587
588 (defvar nnmail-split-tracing nil)
589 (defvar nnmail-split-trace nil)
590 (defvar nnmail-inhibit-default-split-group nil)
591
592 \f
593
594 (defun nnmail-request-post (&optional server)
595 (mail-send-and-exit nil))
596
597 (defvar nnmail-file-coding-system 'raw-text
598 "Coding system used in nnmail.")
599
600 (defvar nnmail-incoming-coding-system
601 mm-text-coding-system
602 "Coding system used in reading inbox")
603
604 (defvar nnmail-pathname-coding-system nil
605 "*Coding system for file name.")
606
607 (defun nnmail-find-file (file)
608 "Insert FILE in server buffer safely."
609 (set-buffer nntp-server-buffer)
610 (delete-region (point-min) (point-max))
611 (let ((format-alist nil)
612 (after-insert-file-functions nil))
613 (condition-case ()
614 (let ((coding-system-for-read nnmail-file-coding-system)
615 (auto-mode-alist (mm-auto-mode-alist))
616 (file-name-coding-system nnmail-pathname-coding-system))
617 (insert-file-contents file)
618 t)
619 (file-error nil))))
620
621 (defun nnmail-group-pathname (group dir &optional file)
622 "Make file name for GROUP."
623 (concat
624 (let ((dir (file-name-as-directory (expand-file-name dir))))
625 (setq group (nnheader-replace-duplicate-chars-in-string
626 (nnheader-replace-chars-in-string group ?/ ?_)
627 ?. ?_))
628 (setq group (nnheader-translate-file-chars group))
629 ;; If this directory exists, we use it directly.
630 (file-name-as-directory
631 (if (or nnmail-use-long-file-names
632 (file-directory-p (concat dir group)))
633 (expand-file-name group dir)
634 ;; If not, we translate dots into slashes.
635 (expand-file-name
636 (nnheader-replace-chars-in-string group ?. ?/)
637 dir))))
638 (or file "")))
639
640 (defun nnmail-get-active ()
641 "Returns an assoc of group names and active ranges.
642 nn*-request-list should have been called before calling this function."
643 ;; Go through all groups from the active list.
644 (with-current-buffer nntp-server-buffer
645 (nnmail-parse-active)))
646
647 (defun nnmail-parse-active ()
648 "Parse the active file in the current buffer and return an alist."
649 (goto-char (point-min))
650 (unless (re-search-forward "[\\\"]" nil t)
651 (goto-char (point-max))
652 (while (re-search-backward "[][';?()#]" nil t)
653 (insert ?\\)))
654 (goto-char (point-min))
655 (let ((buffer (current-buffer))
656 group-assoc group max min)
657 (while (not (eobp))
658 (condition-case err
659 (progn
660 (narrow-to-region (point) (point-at-eol))
661 (setq group (read buffer))
662 (unless (stringp group)
663 (setq group (symbol-name group)))
664 (if (and (numberp (setq max (read buffer)))
665 (numberp (setq min (read buffer))))
666 (push (list (string-as-unibyte group) (cons min max))
667 group-assoc)))
668 (error nil))
669 (widen)
670 (forward-line 1))
671 group-assoc))
672
673 (defvar nnmail-active-file-coding-system 'raw-text
674 "*Coding system for active file.")
675
676 (defun nnmail-save-active (group-assoc file-name)
677 "Save GROUP-ASSOC in ACTIVE-FILE."
678 (let ((coding-system-for-write nnmail-active-file-coding-system))
679 (when file-name
680 (with-temp-file file-name
681 (mm-disable-multibyte)
682 (nnmail-generate-active group-assoc)))))
683
684 (defun nnmail-generate-active (alist)
685 "Generate an active file from group-alist ALIST."
686 (erase-buffer)
687 (let (group)
688 (while (setq group (pop alist))
689 (insert (format "%S %d %d y\n" (intern (car group)) (cdadr group)
690 (caadr group))))
691 (goto-char (point-max))
692 (while (search-backward "\\." nil t)
693 (delete-char 1))))
694
695 (defun nnmail-get-split-group (file source)
696 "Find out whether this FILE is to be split into GROUP only.
697 If SOURCE is a directory spec, try to return the group name component."
698 (if (eq (car source) 'directory)
699 (let ((file (file-name-nondirectory file)))
700 (mail-source-bind (directory source)
701 (if (string-match (concat (regexp-quote suffix) "$") file)
702 (substring file 0 (match-beginning 0))
703 nil)))
704 nil))
705
706 (defun nnmail-process-babyl-mail-format (func artnum-func)
707 (let ((case-fold-search t)
708 (count 0)
709 start message-id content-length do-search end)
710 (while (not (eobp))
711 (goto-char (point-min))
712 (re-search-forward
713 "\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
714 (goto-char (match-end 0))
715 (delete-region (match-beginning 0) (match-end 0))
716 (narrow-to-region
717 (setq start (point))
718 (progn
719 ;; Skip all the headers in case there are more "From "s...
720 (or (search-forward "\n\n" nil t)
721 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
722 (search-forward "\1f\f"))
723 (point)))
724 ;; Unquote the ">From " line, if any.
725 (goto-char (point-min))
726 (when (looking-at ">From ")
727 (replace-match "X-From-Line: ") )
728 (run-hooks 'nnmail-prepare-incoming-header-hook)
729 (goto-char (point-max))
730 ;; Find the Message-ID header.
731 (save-excursion
732 (if (re-search-backward
733 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t)
734 (setq message-id (buffer-substring (match-beginning 1)
735 (match-end 1)))
736 ;; There is no Message-ID here, so we create one.
737 (save-excursion
738 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
739 (beginning-of-line)
740 (insert "Original-")))
741 (forward-line -1)
742 (insert "Message-ID: " (setq message-id (nnmail-message-id))
743 "\n")))
744 ;; Look for a Content-Length header.
745 (if (not (save-excursion
746 (and (re-search-backward
747 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
748 (setq content-length (string-to-number
749 (buffer-substring
750 (match-beginning 1)
751 (match-end 1))))
752 ;; We destroy the header, since none of
753 ;; the backends ever use it, and we do not
754 ;; want to confuse other mailers by having
755 ;; a (possibly) faulty header.
756 (progn (insert "X-") t))))
757 (setq do-search t)
758 (widen)
759 (if (or (= (+ (point) content-length) (point-max))
760 (save-excursion
761 (goto-char (+ (point) content-length))
762 (looking-at "\1f")))
763 (progn
764 (goto-char (+ (point) content-length))
765 (setq do-search nil))
766 (setq do-search t)))
767 (widen)
768 ;; Go to the beginning of the next article - or to the end
769 ;; of the buffer.
770 (when do-search
771 (if (re-search-forward "^\1f" nil t)
772 (goto-char (match-beginning 0))
773 (goto-char (1- (point-max)))))
774 (delete-char 1) ; delete ^_
775 (save-excursion
776 (save-restriction
777 (narrow-to-region start (point))
778 (goto-char (point-min))
779 (nnmail-check-duplication message-id func artnum-func)
780 (incf count)
781 (setq end (point-max))))
782 (goto-char end))
783 count))
784
785 (defsubst nnmail-search-unix-mail-delim ()
786 "Put point at the beginning of the next Unix mbox message."
787 ;; Algorithm used to find the next article in the
788 ;; brain-dead Unix mbox format:
789 ;;
790 ;; 1) Search for "^From ".
791 ;; 2) If we find it, then see whether the previous
792 ;; line is blank and the next line looks like a header.
793 ;; Then it's possible that this is a mail delim, and we use it.
794 (let ((case-fold-search nil)
795 found)
796 (while (not found)
797 (if (not (re-search-forward "^From " nil t))
798 (setq found 'no)
799 (save-excursion
800 (beginning-of-line)
801 (when (and (or (bobp)
802 (save-excursion
803 (forward-line -1)
804 (eq (char-after) ?\n)))
805 (save-excursion
806 (forward-line 1)
807 (while (looking-at ">From \\|From ")
808 (forward-line 1))
809 (looking-at "[^ \n\t:]+[ \n\t]*:")))
810 (setq found 'yes)))))
811 (beginning-of-line)
812 (eq found 'yes)))
813
814 (defun nnmail-search-unix-mail-delim-backward ()
815 "Put point at the beginning of the current Unix mbox message."
816 ;; Algorithm used to find the next article in the
817 ;; brain-dead Unix mbox format:
818 ;;
819 ;; 1) Search for "^From ".
820 ;; 2) If we find it, then see whether the previous
821 ;; line is blank and the next line looks like a header.
822 ;; Then it's possible that this is a mail delim, and we use it.
823 (let ((case-fold-search nil)
824 found)
825 (while (not found)
826 (if (not (re-search-backward "^From " nil t))
827 (setq found 'no)
828 (save-excursion
829 (beginning-of-line)
830 (when (and (or (bobp)
831 (save-excursion
832 (forward-line -1)
833 (eq (char-after) ?\n)))
834 (save-excursion
835 (forward-line 1)
836 (while (looking-at ">From \\|From ")
837 (forward-line 1))
838 (looking-at "[^ \n\t:]+[ \n\t]*:")))
839 (setq found 'yes)))))
840 (beginning-of-line)
841 (eq found 'yes)))
842
843 (defun nnmail-process-unix-mail-format (func artnum-func)
844 (let ((case-fold-search t)
845 (count 0)
846 start message-id content-length end skip head-end)
847 (goto-char (point-min))
848 (if (not (and (re-search-forward "^From " nil t)
849 (goto-char (match-beginning 0))))
850 ;; Possibly wrong format?
851 (error "Error, unknown mail format! (Possibly corrupted %s `%s'.)"
852 (if (buffer-file-name) "file" "buffer")
853 (or (buffer-file-name) (buffer-name)))
854 ;; Carry on until the bitter end.
855 (while (not (eobp))
856 (setq start (point)
857 end nil)
858 ;; Find the end of the head.
859 (narrow-to-region
860 start
861 (if (search-forward "\n\n" nil t)
862 (1- (point))
863 ;; This will never happen, but just to be on the safe side --
864 ;; if there is no head-body delimiter, we search a bit manually.
865 (while (and (looking-at "From \\|[^ \t]+:")
866 (not (eobp)))
867 (forward-line 1))
868 (point)))
869 ;; Find the Message-ID header.
870 (goto-char (point-min))
871 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
872 (setq message-id (match-string 1))
873 (save-excursion
874 (when (re-search-forward "^Message-ID[ \t]*:" nil t)
875 (beginning-of-line)
876 (insert "Original-")))
877 ;; There is no Message-ID here, so we create one.
878 (forward-line 1)
879 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
880 ;; Look for a Content-Length header.
881 (goto-char (point-min))
882 (if (not (re-search-forward
883 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
884 (setq content-length nil)
885 (setq content-length (string-to-number (match-string 1)))
886 ;; We destroy the header, since none of the backends ever
887 ;; use it, and we do not want to confuse other mailers by
888 ;; having a (possibly) faulty header.
889 (beginning-of-line)
890 (insert "X-"))
891 (run-hooks 'nnmail-prepare-incoming-header-hook)
892 ;; Find the end of this article.
893 (goto-char (point-max))
894 (widen)
895 (setq head-end (point))
896 ;; We try the Content-Length value. The idea: skip over the header
897 ;; separator, then check what happens content-length bytes into the
898 ;; message body. This should be either the end of the buffer, the
899 ;; message separator or a blank line followed by the separator.
900 ;; The blank line should probably be deleted. If neither of the
901 ;; three is met, the content-length header is probably invalid.
902 (when content-length
903 (forward-line 1)
904 (setq skip (+ (point) content-length))
905 (goto-char skip)
906 (cond ((or (= skip (point-max))
907 (= (1+ skip) (point-max)))
908 (setq end (point-max)))
909 ((looking-at "From ")
910 (setq end skip))
911 ((looking-at "[ \t]*\n\\(From \\)")
912 (setq end (match-beginning 1)))
913 (t (setq end nil))))
914 (if end
915 (goto-char end)
916 ;; No Content-Length, so we find the beginning of the next
917 ;; article or the end of the buffer.
918 (goto-char head-end)
919 (or (nnmail-search-unix-mail-delim)
920 (goto-char (point-max))))
921 ;; Allow the backend to save the article.
922 (save-excursion
923 (save-restriction
924 (narrow-to-region start (point))
925 (goto-char (point-min))
926 (incf count)
927 (nnmail-check-duplication message-id func artnum-func)
928 (setq end (point-max))))
929 (goto-char end)))
930 count))
931
932 (defun nnmail-process-mmdf-mail-format (func artnum-func &optional junk-func)
933 (let ((delim "^\^A\^A\^A\^A$")
934 (case-fold-search t)
935 (count 0)
936 start message-id end)
937 (goto-char (point-min))
938 (if (not (and (re-search-forward delim nil t)
939 (forward-line 1)))
940 ;; Possibly wrong format?
941 (error "Error, unknown mail format! (Possibly corrupted.)")
942 ;; Carry on until the bitter end.
943 (while (not (eobp))
944 (setq start (point))
945 ;; Find the end of the head.
946 (narrow-to-region
947 start
948 (if (search-forward "\n\n" nil t)
949 (1- (point))
950 ;; This will never happen, but just to be on the safe side --
951 ;; if there is no head-body delimiter, we search a bit manually.
952 (while (and (looking-at "From \\|[^ \t]+:")
953 (not (eobp)))
954 (forward-line 1))
955 (point)))
956 ;; Find the Message-ID header.
957 (goto-char (point-min))
958 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
959 (setq message-id (match-string 1))
960 ;; There is no Message-ID here, so we create one.
961 (save-excursion
962 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
963 (beginning-of-line)
964 (insert "Original-")))
965 (forward-line 1)
966 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
967 (run-hooks 'nnmail-prepare-incoming-header-hook)
968 ;; Find the end of this article.
969 (goto-char (point-max))
970 (widen)
971 (if (re-search-forward delim nil t)
972 (beginning-of-line)
973 (goto-char (point-max)))
974 ;; Allow the backend to save the article.
975 (save-excursion
976 (save-restriction
977 (narrow-to-region start (point))
978 (goto-char (point-min))
979 (incf count)
980 (nnmail-check-duplication message-id func artnum-func junk-func)
981 (setq end (point-max))))
982 (goto-char end)
983 (forward-line 2)))
984 count))
985
986 (defun nnmail-process-maildir-mail-format (func artnum-func)
987 ;; In a maildir, every file contains exactly one mail.
988 (let ((case-fold-search t)
989 message-id)
990 (goto-char (point-min))
991 ;; Find the end of the head.
992 (narrow-to-region
993 (point-min)
994 (if (search-forward "\n\n" nil t)
995 (1- (point))
996 ;; This will never happen, but just to be on the safe side --
997 ;; if there is no head-body delimiter, we search a bit manually.
998 (while (and (looking-at "From \\|[^ \t]+:")
999 (not (eobp)))
1000 (forward-line 1))
1001 (point)))
1002 ;; Find the Message-ID header.
1003 (goto-char (point-min))
1004 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
1005 (setq message-id (match-string 1))
1006 ;; There is no Message-ID here, so we create one.
1007 (save-excursion
1008 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
1009 (beginning-of-line)
1010 (insert "Original-")))
1011 (forward-line 1)
1012 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
1013 (run-hooks 'nnmail-prepare-incoming-header-hook)
1014 ;; Allow the backend to save the article.
1015 (widen)
1016 (save-excursion
1017 (goto-char (point-min))
1018 (nnmail-check-duplication message-id func artnum-func))
1019 1))
1020
1021 (defvar nnmail-group-names-not-encoded-p nil
1022 "Non-nil means group names are not encoded.")
1023
1024 (defun nnmail-split-incoming (incoming func &optional exit-func
1025 group artnum-func junk-func)
1026 "Go through the entire INCOMING file and pick out each individual mail.
1027 FUNC will be called with the buffer narrowed to each mail.
1028 INCOMING can also be a buffer object. In that case, the mail
1029 will be copied over from that buffer."
1030 (let ( ;; If this is a group-specific split, we bind the split
1031 ;; methods to just this group.
1032 (nnmail-split-methods (if (and group
1033 (not nnmail-resplit-incoming))
1034 (list (list group ""))
1035 nnmail-split-methods))
1036 (nnmail-group-names-not-encoded-p t))
1037 ;; Insert the incoming file.
1038 (with-current-buffer (get-buffer-create nnmail-article-buffer)
1039 (erase-buffer)
1040 (if (bufferp incoming)
1041 (insert-buffer-substring incoming)
1042 (let ((coding-system-for-read nnmail-incoming-coding-system))
1043 (mm-insert-file-contents incoming)))
1044 (prog1
1045 (if (zerop (buffer-size))
1046 0
1047 (goto-char (point-min))
1048 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
1049 ;; Handle both babyl, MMDF and unix mail formats, since
1050 ;; movemail will use the former when fetching from a
1051 ;; mailbox, the latter when fetching from a file.
1052 (cond ((or (looking-at "\^L")
1053 (looking-at "BABYL OPTIONS:"))
1054 (nnmail-process-babyl-mail-format func artnum-func))
1055 ((looking-at "\^A\^A\^A\^A")
1056 (nnmail-process-mmdf-mail-format
1057 func artnum-func junk-func))
1058 ((looking-at "Return-Path:")
1059 (nnmail-process-maildir-mail-format func artnum-func))
1060 (t
1061 (nnmail-process-unix-mail-format func artnum-func))))
1062 (when exit-func
1063 (funcall exit-func))
1064 (kill-buffer (current-buffer))))))
1065
1066 (defun nnmail-article-group (func &optional trace junk-func)
1067 "Look at the headers and return an alist of groups that match.
1068 FUNC will be called with the group name to determine the article number."
1069 (let ((methods (or nnmail-split-methods '(("bogus" ""))))
1070 (obuf (current-buffer))
1071 group-art method grp)
1072 (if (and (sequencep methods)
1073 (= (length methods) 1)
1074 (not nnmail-inhibit-default-split-group))
1075 ;; If there is only just one group to put everything in, we
1076 ;; just return a list with just this one method in.
1077 (setq group-art
1078 (list (cons (caar methods) (funcall func (caar methods)))))
1079 ;; We do actual comparison.
1080 ;; Copy the article into the work buffer.
1081 (with-current-buffer nntp-server-buffer
1082 (erase-buffer)
1083 (insert-buffer-substring obuf)
1084 ;; Narrow to headers.
1085 (narrow-to-region
1086 (goto-char (point-min))
1087 (if (search-forward "\n\n" nil t)
1088 (point)
1089 (point-max)))
1090 (goto-char (point-min))
1091 ;; Decode MIME headers and charsets.
1092 (when nnmail-mail-splitting-decodes
1093 (let ((mail-parse-charset nnmail-mail-splitting-charset))
1094 (mail-decode-encoded-word-region (point-min) (point-max))))
1095 ;; Fold continuation lines.
1096 (goto-char (point-min))
1097 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1098 (replace-match " " t t))
1099 ;; Nuke pathologically long headers. Since Gnus applies
1100 ;; pathologically complex regexps to the buffer, lines
1101 ;; that are looong will take longer than the Universe's
1102 ;; existence to process.
1103 (goto-char (point-min))
1104 (while (not (eobp))
1105 (unless (< (move-to-column nnmail-split-header-length-limit)
1106 nnmail-split-header-length-limit)
1107 (delete-region (point) (point-at-eol)))
1108 (forward-line 1))
1109 ;; Allow washing.
1110 (goto-char (point-min))
1111 (run-hooks 'nnmail-split-hook)
1112 (when (setq nnmail-split-tracing trace)
1113 (setq nnmail-split-trace nil))
1114 (if (or (and (symbolp nnmail-split-methods)
1115 (fboundp nnmail-split-methods))
1116 (not (consp (car-safe nnmail-split-methods)))
1117 (and (listp nnmail-split-methods)
1118 ;; Not a regular split method, so it has to be a
1119 ;; fancy one.
1120 (not (let ((top-element (car-safe nnmail-split-methods)))
1121 (and (= 2 (length top-element))
1122 (stringp (nth 0 top-element))
1123 (stringp (nth 1 top-element)))))))
1124 (let* ((method-function
1125 (if (and (symbolp nnmail-split-methods)
1126 (fboundp nnmail-split-methods))
1127 nnmail-split-methods
1128 'nnmail-split-fancy))
1129 (split
1130 (condition-case error-info
1131 ;; `nnmail-split-methods' is a function, so we
1132 ;; just call this function here and use the
1133 ;; result.
1134 (or (funcall method-function)
1135 (and (not nnmail-inhibit-default-split-group)
1136 '("bogus")))
1137 (error
1138 (nnheader-message
1139 5 "Error in `nnmail-split-methods'; using `bogus' mail group: %S" error-info)
1140 (sit-for 1)
1141 '("bogus")))))
1142 (setq split (delete-dups split))
1143 ;; The article may be "cross-posted" to `junk'. What
1144 ;; to do? Just remove the `junk' spec. Don't really
1145 ;; see anything else to do...
1146 (when (and (memq 'junk split)
1147 junk-func)
1148 (funcall junk-func 'junk))
1149 (setq split (delq 'junk split))
1150 (when split
1151 (setq group-art
1152 (mapcar
1153 (lambda (group) (cons group (funcall func group)))
1154 split))))
1155 ;; Go through the split methods to find a match.
1156 (while (and methods
1157 (or nnmail-crosspost
1158 (not group-art)))
1159 (goto-char (point-max))
1160 (setq method (pop methods)
1161 grp (car method))
1162 (if (or methods
1163 (not (equal "" (nth 1 method))))
1164 (when (and
1165 (ignore-errors
1166 (if (stringp (nth 1 method))
1167 (let ((expand (string-match "\\\\[0-9&]" grp))
1168 (pos (re-search-backward (cadr method)
1169 nil t)))
1170 (and expand
1171 (setq grp (nnmail-expand-newtext grp)))
1172 pos)
1173 ;; Function to say whether this is a match.
1174 (funcall (nth 1 method) grp)))
1175 ;; Don't enter the article into the same
1176 ;; group twice.
1177 (not (assoc grp group-art)))
1178 (push (cons grp (funcall func grp))
1179 group-art))
1180 ;; This is the final group, which is used as a
1181 ;; catch-all.
1182 (when (and (not group-art)
1183 (or (equal "" (nth 1 method))
1184 (not nnmail-inhibit-default-split-group)))
1185 (setq group-art
1186 (list (cons (car method)
1187 (funcall func (car method))))))))
1188 ;; Fall back on "bogus" if all else fails.
1189 (when (and (not group-art)
1190 (not nnmail-inhibit-default-split-group))
1191 (setq group-art (list (cons "bogus" (funcall func "bogus"))))))
1192 ;; Produce a trace if non-empty.
1193 (when (and trace nnmail-split-trace)
1194 (let ((restore (current-buffer)))
1195 (nnheader-set-temp-buffer "*Split Trace*")
1196 (gnus-add-buffer)
1197 (dolist (trace (nreverse nnmail-split-trace))
1198 (prin1 trace (current-buffer))
1199 (insert "\n"))
1200 (goto-char (point-min))
1201 (gnus-configure-windows 'split-trace)
1202 (set-buffer restore)))
1203 (widen)
1204 ;; See whether the split methods returned `junk'.
1205 (if (equal group-art '(junk))
1206 nil
1207 ;; The article may be "cross-posted" to `junk'. What
1208 ;; to do? Just remove the `junk' spec. Don't really
1209 ;; see anything else to do...
1210 (let (elem)
1211 (while (setq elem (car (memq 'junk group-art)))
1212 (setq group-art (delq elem group-art)))
1213 (nreverse group-art)))))))
1214
1215 (defun nnmail-insert-lines ()
1216 "Insert how many lines there are in the body of the mail.
1217 Return the number of characters in the body."
1218 (let (lines chars)
1219 (save-excursion
1220 (goto-char (point-min))
1221 (unless (search-forward "\n\n" nil t)
1222 (goto-char (point-max))
1223 (insert "\n"))
1224 (setq chars (- (point-max) (point)))
1225 (setq lines (count-lines (point) (point-max)))
1226 (forward-char -1)
1227 (save-excursion
1228 (when (re-search-backward "^Lines: " nil t)
1229 (delete-region (point) (progn (forward-line 1) (point)))))
1230 (beginning-of-line)
1231 (insert (format "Lines: %d\n" (max lines 0)))
1232 chars)))
1233
1234 (defun nnmail-insert-xref (group-alist)
1235 "Insert an Xref line based on the (group . article) alist."
1236 (save-excursion
1237 (goto-char (point-min))
1238 (unless (search-forward "\n\n" nil t)
1239 (goto-char (point-max))
1240 (insert "\n"))
1241 (forward-char -1)
1242 (when (re-search-backward "^Xref: " nil t)
1243 (delete-region (match-beginning 0)
1244 (progn (forward-line 1) (point))))
1245 (insert (format "Xref: %s" (system-name)))
1246 (while group-alist
1247 (insert (if (mm-multibyte-p)
1248 (string-as-multibyte
1249 (format " %s:%d" (caar group-alist) (cdar group-alist)))
1250 (string-as-unibyte
1251 (format " %s:%d" (caar group-alist) (cdar group-alist)))))
1252 (setq group-alist (cdr group-alist)))
1253 (insert "\n")))
1254
1255 ;;; Message washing functions
1256
1257 (defun nnmail-remove-leading-whitespace ()
1258 "Remove excessive whitespace from all headers."
1259 (goto-char (point-min))
1260 (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1261 (replace-match "\\1" t)))
1262
1263 (defun nnmail-remove-list-identifiers ()
1264 "Remove list identifiers from Subject headers."
1265 (let ((regexp
1266 (if (consp nnmail-list-identifiers)
1267 (mapconcat 'identity nnmail-list-identifiers " *\\|")
1268 nnmail-list-identifiers)))
1269 (when regexp
1270 (goto-char (point-min))
1271 (while (re-search-forward
1272 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1273 nil t)
1274 (delete-region (match-beginning 2) (match-end 0))
1275 (beginning-of-line))
1276 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1277 nil t)
1278 (delete-region (match-beginning 1) (match-end 1))
1279 (beginning-of-line)))))
1280
1281 (defun nnmail-remove-tabs ()
1282 "Translate TAB characters into SPACE characters."
1283 (subst-char-in-region (point-min) (point-max) ?\t ? t))
1284
1285 (defcustom nnmail-broken-references-mailers
1286 "^X-Mailer:.*\\(Eudora\\|Pegasus\\)"
1287 "Header line matching mailer producing bogus References lines.
1288 See `nnmail-ignore-broken-references'."
1289 :group 'nnmail-prepare
1290 :version "23.1" ;; No Gnus
1291 :type 'regexp)
1292
1293 (defun nnmail-ignore-broken-references ()
1294 "Ignore the References line and use In-Reply-To
1295
1296 Eudora has a broken References line, but an OK In-Reply-To."
1297 (goto-char (point-min))
1298 (when (re-search-forward nnmail-broken-references-mailers nil t)
1299 (goto-char (point-min))
1300 (when (re-search-forward "^References:" nil t)
1301 (beginning-of-line)
1302 (insert "X-Gnus-Broken-Eudora-"))
1303 (goto-char (point-min))
1304 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1305 (replace-match "\\1" t))))
1306
1307 (defalias 'nnmail-fix-eudora-headers 'nnmail-ignore-broken-references)
1308 (make-obsolete 'nnmail-fix-eudora-headers 'nnmail-ignore-broken-references "Emacs 23.1")
1309
1310 (custom-add-option 'nnmail-prepare-incoming-header-hook
1311 'nnmail-ignore-broken-references)
1312
1313 ;;; Utility functions
1314
1315 (declare-function gnus-activate-group "gnus-start"
1316 (group &optional scan dont-check method dont-sub-check))
1317
1318 (defun nnmail-do-request-post (accept-func &optional server)
1319 "Utility function to directly post a message to an nnmail-derived group.
1320 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1321 to actually put the message in the right group."
1322 (let ((success t))
1323 (dolist (mbx (message-unquote-tokens
1324 (message-tokenize-header
1325 (message-fetch-field "Newsgroups") ", ")) success)
1326 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1327 (or (gnus-active to-newsgroup)
1328 (gnus-activate-group to-newsgroup)
1329 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1330 to-newsgroup))
1331 (or (and (gnus-request-create-group
1332 to-newsgroup gnus-command-method)
1333 (gnus-activate-group to-newsgroup nil nil
1334 gnus-command-method))
1335 (error "Couldn't create group %s" to-newsgroup)))
1336 (error "No such group: %s" to-newsgroup))
1337 (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1338 (setq success nil))))))
1339
1340 (defun nnmail-split-fancy ()
1341 "Fancy splitting method.
1342 See the documentation for the variable `nnmail-split-fancy' for details."
1343 (with-syntax-table nnmail-split-fancy-syntax-table
1344 (nnmail-split-it nnmail-split-fancy)))
1345
1346 (defvar nnmail-split-cache nil)
1347 ;; Alist of split expressions their equivalent regexps.
1348
1349 (defun nnmail-split-it (split)
1350 ;; Return a list of groups matching SPLIT.
1351 (let (cached-pair)
1352 (cond
1353 ;; nil split
1354 ((null split)
1355 nil)
1356
1357 ;; A group name. Do the \& and \N subs into the string.
1358 ((stringp split)
1359 (when nnmail-split-tracing
1360 (push split nnmail-split-trace))
1361 (list (nnmail-expand-newtext split t)))
1362
1363 ;; Junk the message.
1364 ((eq split 'junk)
1365 (when nnmail-split-tracing
1366 (push "junk" nnmail-split-trace))
1367 (list 'junk))
1368
1369 ;; Builtin & operation.
1370 ((eq (car split) '&)
1371 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1372
1373 ;; Builtin | operation.
1374 ((eq (car split) '|)
1375 (let (done)
1376 (while (and (not done) (cdr split))
1377 (setq split (cdr split)
1378 done (nnmail-split-it (car split))))
1379 done))
1380
1381 ;; Builtin : operation.
1382 ((eq (car split) ':)
1383 (when nnmail-split-tracing
1384 (push split nnmail-split-trace))
1385 (nnmail-split-it (save-excursion (eval (cdr split)))))
1386
1387 ;; Builtin ! operation.
1388 ((eq (car split) '!)
1389 (funcall (cadr split) (nnmail-split-it (caddr split))))
1390
1391 ;; Check the cache for the regexp for this split.
1392 ((setq cached-pair (assq split nnmail-split-cache))
1393 (let (split-result
1394 match-data
1395 (end-point (point-max))
1396 (value (nth 1 split)))
1397 (if (symbolp value)
1398 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1399 (while (and (goto-char end-point)
1400 (re-search-backward (cdr cached-pair) nil t))
1401 (setq match-data (match-data))
1402 (when nnmail-split-tracing
1403 (push split nnmail-split-trace))
1404 (let ((split-rest (cddr split))
1405 (end (match-end 0))
1406 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1407 ;; So, start-of-value is the point just before the
1408 ;; beginning of the value, whereas after-header-name
1409 ;; is the point just after the field name.
1410 (start-of-value (match-end 1))
1411 (after-header-name (match-end 2)))
1412 ;; Start the next search just before the beginning of the
1413 ;; VALUE match.
1414 (setq end-point (1- start-of-value))
1415 ;; Handle - RESTRICTs
1416 (while (eq (car split-rest) '-)
1417 ;; RESTRICT must start after-header-name and
1418 ;; end after start-of-value, so that, for
1419 ;; (any "foo" - "x-foo" "foo.list")
1420 ;; we do not exclude foo.list just because
1421 ;; the header is: ``To: x-foo, foo''
1422 (goto-char end)
1423 (if (and (re-search-backward (cadr split-rest)
1424 after-header-name t)
1425 (> (match-end 0) start-of-value))
1426 (setq split-rest nil)
1427 (setq split-rest (cddr split-rest))))
1428 (when split-rest
1429 (goto-char end)
1430 ;; Someone might want to do a \N sub on this match, so
1431 ;; restore the match data.
1432 (set-match-data match-data)
1433 (dolist (sp (nnmail-split-it (car split-rest)))
1434 (unless (member sp split-result)
1435 (push sp split-result))))))
1436 split-result))
1437
1438 ;; Not in cache, compute a regexp for the field/value pair.
1439 (t
1440 (let ((field (nth 0 split))
1441 (value (nth 1 split))
1442 (split-rest (cddr split))
1443 partial-front
1444 partial-rear
1445 regexp)
1446 (if (symbolp value)
1447 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1448 (if (and (>= (length value) 2)
1449 (string= ".*" (substring value 0 2)))
1450 (setq value (substring value 2)
1451 partial-front ""))
1452 ;; Same trick for the rear of the regexp
1453 (if (and (>= (length value) 2)
1454 (string= ".*" (substring value -2)))
1455 (setq value (substring value 0 -2)
1456 partial-rear ""))
1457 ;; Invert the match-partial-words behavior if the optional
1458 ;; last element is specified.
1459 (while (eq (car split-rest) '-)
1460 (setq split-rest (cddr split-rest)))
1461 (when (if (cadr split-rest)
1462 (not nnmail-split-fancy-match-partial-words)
1463 nnmail-split-fancy-match-partial-words)
1464 (setq partial-front ""
1465 partial-rear ""))
1466 (setq regexp (concat "^\\(\\("
1467 (if (symbolp field)
1468 (cdr (assq field nnmail-split-abbrev-alist))
1469 field)
1470 "\\):.*\\)"
1471 (or partial-front "\\<")
1472 "\\("
1473 value
1474 "\\)"
1475 (or partial-rear "\\>")))
1476 (push (cons split regexp) nnmail-split-cache)
1477 ;; Now that it's in the cache, just call nnmail-split-it again
1478 ;; on the same split, which will find it immediately in the cache.
1479 (nnmail-split-it split))))))
1480
1481 (defun nnmail-expand-newtext (newtext &optional fancyp)
1482 (let ((len (length newtext))
1483 (pos 0)
1484 c expanded beg N did-expand)
1485 (while (< pos len)
1486 (setq beg pos)
1487 (while (and (< pos len)
1488 (not (= (aref newtext pos) ?\\)))
1489 (setq pos (1+ pos)))
1490 (unless (= beg pos)
1491 (push (substring newtext beg pos) expanded))
1492 (when (< pos len)
1493 ;; We hit a \; expand it.
1494 (setq did-expand t
1495 pos (1+ pos)
1496 c (aref newtext pos))
1497 (if (not (or (= c ?\&)
1498 (and (>= c ?1)
1499 (<= c ?9))))
1500 ;; \ followed by some character we don't expand.
1501 (push (char-to-string c) expanded)
1502 ;; \& or \N
1503 (if (= c ?\&)
1504 (setq N 0)
1505 (setq N (- c ?0)))
1506 ;; We wrapped the searches in parentheses, so we have to
1507 ;; add some parentheses here...
1508 (when fancyp
1509 (setq N (+ N 3)))
1510 (when (match-beginning N)
1511 (push (if nnmail-split-lowercase-expanded
1512 (downcase (buffer-substring (match-beginning N)
1513 (match-end N)))
1514 (buffer-substring (match-beginning N) (match-end N)))
1515 expanded))))
1516 (setq pos (1+ pos)))
1517 (if did-expand
1518 (apply 'concat (nreverse expanded))
1519 newtext)))
1520
1521 ;; Activate a backend only if it isn't already activated.
1522 ;; If FORCE, re-read the active file even if the backend is
1523 ;; already activated.
1524 (defun nnmail-activate (backend &optional force)
1525 (nnheader-init-server-buffer)
1526 (let (file timestamp file-time)
1527 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1528 force
1529 (and (setq file (ignore-errors
1530 (symbol-value (intern (format "%s-active-file"
1531 backend)))))
1532 (setq file-time (nth 5 (file-attributes file)))
1533 (or (not
1534 (setq timestamp
1535 (condition-case ()
1536 (symbol-value (intern
1537 (format "%s-active-timestamp"
1538 backend)))
1539 (error 'none))))
1540 (not (consp timestamp))
1541 (equal timestamp '(0 0))
1542 (> (nth 0 file-time) (nth 0 timestamp))
1543 (and (= (nth 0 file-time) (nth 0 timestamp))
1544 (> (nth 1 file-time) (nth 1 timestamp))))))
1545 (save-excursion
1546 (or (eq timestamp 'none)
1547 (set (intern (format "%s-active-timestamp" backend))
1548 file-time))
1549 (funcall (intern (format "%s-request-list" backend)))))
1550 t))
1551
1552 (defun nnmail-message-id ()
1553 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1554
1555 ;;;
1556 ;;; nnmail duplicate handling
1557 ;;;
1558
1559 (defvar nnmail-cache-buffer nil)
1560
1561 (defun nnmail-cache-open ()
1562 (if (or (not nnmail-treat-duplicates)
1563 (and nnmail-cache-buffer
1564 (buffer-name nnmail-cache-buffer)))
1565 () ; The buffer is open.
1566 (with-current-buffer
1567 (setq nnmail-cache-buffer
1568 (get-buffer-create " *nnmail message-id cache*"))
1569 (gnus-add-buffer)
1570 (when (file-exists-p nnmail-message-id-cache-file)
1571 (nnheader-insert-file-contents nnmail-message-id-cache-file))
1572 (set-buffer-modified-p nil)
1573 (current-buffer))))
1574
1575 (defun nnmail-cache-close ()
1576 (when (and nnmail-cache-buffer
1577 nnmail-treat-duplicates
1578 (buffer-name nnmail-cache-buffer)
1579 (buffer-modified-p nnmail-cache-buffer))
1580 (with-current-buffer nnmail-cache-buffer
1581 ;; Weed out the excess number of Message-IDs.
1582 (goto-char (point-max))
1583 (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1584 (progn
1585 (beginning-of-line)
1586 (delete-region (point-min) (point))))
1587 ;; Save the buffer.
1588 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1589 (make-directory (file-name-directory nnmail-message-id-cache-file)
1590 t))
1591 (nnmail-write-region (point-min) (point-max)
1592 nnmail-message-id-cache-file nil 'silent)
1593 (set-buffer-modified-p nil)
1594 (setq nnmail-cache-buffer nil)
1595 (gnus-kill-buffer (current-buffer)))))
1596
1597 (defun nnmail-cache-insert (id grp &optional subject sender)
1598 (when (stringp id)
1599 ;; this will handle cases like `B r' where the group is nil
1600 (let ((grp (or grp gnus-newsgroup-name "UNKNOWN")))
1601 (run-hook-with-args 'nnmail-spool-hook
1602 id grp subject sender))
1603 (when nnmail-treat-duplicates
1604 ;; Store some information about the group this message is written
1605 ;; to. This is passed in as the grp argument -- all locations this
1606 ;; has been called from have been checked and the group is available.
1607 ;; The only ambiguous case is nnmail-check-duplication which will only
1608 ;; pass the first (of possibly >1) group which matches. -Josh
1609 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1610 (nnmail-cache-open))
1611 (with-current-buffer nnmail-cache-buffer
1612 (goto-char (point-max))
1613 (if (and grp (not (string= "" grp))
1614 (gnus-methods-equal-p gnus-command-method
1615 (nnmail-cache-primary-mail-backend)))
1616 (let ((regexp (if (consp nnmail-cache-ignore-groups)
1617 (mapconcat 'identity nnmail-cache-ignore-groups
1618 "\\|")
1619 nnmail-cache-ignore-groups)))
1620 (unless (and regexp (string-match regexp grp))
1621 (insert id "\t" grp "\n")))
1622 (insert id "\n"))))))
1623
1624 (defun nnmail-cache-primary-mail-backend ()
1625 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
1626 (be nil)
1627 (res nil)
1628 (get-new-mail nil))
1629 (while (and (null res) be-list)
1630 (setq be (car be-list))
1631 (setq be-list (cdr be-list))
1632 (when (and (gnus-method-option-p be 'respool)
1633 (setq get-new-mail
1634 (intern (format "%s-get-new-mail" (car be))))
1635 (boundp get-new-mail)
1636 (symbol-value get-new-mail))
1637 (setq res be)))
1638 res))
1639
1640 ;; Fetch the group name corresponding to the message id stored in the
1641 ;; cache.
1642 (defun nnmail-cache-fetch-group (id)
1643 (when (and nnmail-treat-duplicates nnmail-cache-buffer)
1644 (with-current-buffer nnmail-cache-buffer
1645 (goto-char (point-max))
1646 (when (search-backward id nil t)
1647 (beginning-of-line)
1648 (skip-chars-forward "^\n\r\t")
1649 (unless (looking-at "[\r\n]")
1650 (forward-char 1)
1651 (buffer-substring (point) (point-at-eol)))))))
1652
1653 ;; Function for nnmail-split-fancy: look up all references in the
1654 ;; cache and if a match is found, return that group.
1655 (defun nnmail-split-fancy-with-parent ()
1656 "Split this message into the same group as its parent.
1657 This function can be used as an entry in `nnmail-split-fancy', for
1658 example like this: (: nnmail-split-fancy-with-parent)
1659 For a message to be split, it looks for the parent message in the
1660 References or In-Reply-To header and then looks in the message id
1661 cache file (given by the variable `nnmail-message-id-cache-file') to
1662 see which group that message was put in. This group is returned.
1663
1664 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1665 (let* ((refstr (or (message-fetch-field "references")
1666 (message-fetch-field "in-reply-to")))
1667 (references nil)
1668 (res nil)
1669 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1670 (mapconcat
1671 (lambda (x) (format "\\(%s\\)" x))
1672 nnmail-split-fancy-with-parent-ignore-groups
1673 "\\|")
1674 nnmail-split-fancy-with-parent-ignore-groups)))
1675 (when refstr
1676 (setq references (nreverse (gnus-split-references refstr)))
1677 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1678 (nnmail-cache-open))
1679 (dolist (x references)
1680 (setq res (or (nnmail-cache-fetch-group x) res))
1681 (when (or (member res '("delayed" "drafts" "queue"))
1682 (and regexp res (string-match regexp res)))
1683 (setq res nil)))
1684 res)))
1685
1686 (defun nnmail-cache-id-exists-p (id)
1687 (when nnmail-treat-duplicates
1688 (with-current-buffer nnmail-cache-buffer
1689 (goto-char (point-max))
1690 (search-backward id nil t))))
1691
1692 (defun nnmail-fetch-field (header)
1693 (save-excursion
1694 (save-restriction
1695 (message-narrow-to-head)
1696 (message-fetch-field header))))
1697
1698 (defun nnmail-check-duplication (message-id func artnum-func
1699 &optional junk-func)
1700 (run-hooks 'nnmail-prepare-incoming-message-hook)
1701 ;; If this is a duplicate message, then we do not save it.
1702 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1703 (case-fold-search t)
1704 (action (when duplication
1705 (cond
1706 ((memq nnmail-treat-duplicates '(warn delete))
1707 nnmail-treat-duplicates)
1708 ((functionp nnmail-treat-duplicates)
1709 (funcall nnmail-treat-duplicates message-id))
1710 (t
1711 nnmail-treat-duplicates))))
1712 group-art)
1713 ;; We insert a line that says what the mail source is.
1714 (let ((case-fold-search t))
1715 (goto-char (point-min))
1716 (re-search-forward "^message-id[ \t]*:" nil t)
1717 (beginning-of-line)
1718 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1719
1720 ;; Let the backend save the article (or not).
1721 (cond
1722 ((not duplication)
1723 (funcall func (setq group-art
1724 (nreverse (nnmail-article-group
1725 artnum-func nil junk-func))))
1726 (nnmail-cache-insert message-id (caar group-art)))
1727 ((eq action 'delete)
1728 (setq group-art nil))
1729 ((eq action 'warn)
1730 ;; We insert a warning.
1731 (let ((case-fold-search t))
1732 (goto-char (point-min))
1733 (re-search-forward "^message-id[ \t]*:" nil t)
1734 (beginning-of-line)
1735 (insert
1736 "Gnus-Warning: This is a duplicate of message " message-id "\n")
1737 (funcall func (setq group-art
1738 (nreverse (nnmail-article-group artnum-func))))))
1739 (t
1740 (funcall func (setq group-art
1741 (nreverse (nnmail-article-group artnum-func))))))
1742 ;; Add the group-art list to the history list.
1743 (if group-art
1744 (push group-art nnmail-split-history)
1745 (delete-region (point-min) (point-max)))))
1746
1747 ;;; Get new mail.
1748
1749 (defvar nnmail-fetched-sources nil)
1750
1751 (defun nnmail-get-value (&rest args)
1752 (let ((sym (intern (apply 'format args))))
1753 (when (boundp sym)
1754 (symbol-value sym))))
1755
1756 (defun nnmail-get-new-mail (method exit-func temp
1757 &optional group spool-func)
1758 "Read new incoming mail."
1759 (nnmail-get-new-mail-1 method exit-func temp group nil spool-func))
1760
1761 (defun nnmail-get-new-mail-per-group ()
1762 "Tell us whether the mail-sources specify that `nnmail-get-new-mail' should
1763 be called once per group or once for all groups."
1764 (or (assq 'group mail-sources)
1765 (assq 'directory mail-sources)))
1766
1767 (defun nnmail-get-new-mail-1 (method exit-func temp
1768 group in-group spool-func)
1769 (let* ((sources mail-sources)
1770 fetching-sources
1771 (i 0)
1772 (new 0)
1773 (total 0)
1774 source)
1775 (when (and (nnmail-get-value "%s-get-new-mail" method)
1776 sources)
1777 (while (setq source (pop sources))
1778 ;; Use group's parameter
1779 (when (and (eq (car source) 'group)
1780 group)
1781 (let ((mail-sources
1782 (list
1783 (gnus-group-find-parameter
1784 (concat (symbol-name method) ":" group)
1785 'mail-source t))))
1786 (nnmail-get-new-mail-1 method exit-func temp
1787 group group spool-func))
1788 (setq source nil))
1789 ;; Hack to only fetch the contents of a single group's spool file.
1790 (when (and (eq (car source) 'directory)
1791 (null nnmail-scan-directory-mail-source-once)
1792 group)
1793 (mail-source-bind (directory source)
1794 (setq source (append source
1795 (list
1796 :predicate
1797 (gnus-byte-compile
1798 `(lambda (file)
1799 (string-equal
1800 ,(concat group suffix)
1801 (file-name-nondirectory file)))))))))
1802 (when nnmail-fetched-sources
1803 (if (member source nnmail-fetched-sources)
1804 (setq source nil)
1805 (push source nnmail-fetched-sources)
1806 (push source fetching-sources)))))
1807 (when fetching-sources
1808 ;; We first activate all the groups.
1809 (nnmail-activate method)
1810 ;; Allow the user to hook.
1811 (run-hooks 'nnmail-pre-get-new-mail-hook)
1812 ;; Open the message-id cache.
1813 (nnmail-cache-open)
1814 ;; The we go through all the existing mail source specification
1815 ;; and fetch the mail from each.
1816 (while (setq source (pop fetching-sources))
1817 (when (setq new
1818 (condition-case cond
1819 (mail-source-fetch
1820 source
1821 (gnus-byte-compile
1822 `(lambda (file orig-file)
1823 (nnmail-split-incoming
1824 file ',(intern (format "%s-save-mail" method))
1825 ',spool-func
1826 (or in-group
1827 (if (equal file orig-file)
1828 nil
1829 (nnmail-get-split-group orig-file
1830 ',source)))
1831 ',(intern (format "%s-active-number" method))))))
1832 ((error quit)
1833 (message "Mail source %s failed: %s" source cond)
1834 0)))
1835 (incf total new)
1836 (incf i)))
1837 ;; If we did indeed read any incoming spools, we save all info.
1838 (if (zerop total)
1839 (when mail-source-plugged
1840 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1841 method (car source)))
1842 (nnmail-save-active
1843 (nnmail-get-value "%s-group-alist" method)
1844 (nnmail-get-value "%s-active-file" method))
1845 (when exit-func
1846 (funcall exit-func))
1847 (run-hooks 'nnmail-read-incoming-hook)
1848 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1849 total))
1850 ;; Close the message-id cache.
1851 (nnmail-cache-close)
1852 ;; Allow the user to hook.
1853 (run-hooks 'nnmail-post-get-new-mail-hook))))
1854
1855 (defun nnmail-expired-article-p (group time force &optional inhibit)
1856 "Say whether an article that is TIME old in GROUP should be expired.
1857 If TIME is nil, then return the cutoff time for oldness instead."
1858 (if force
1859 (if (null time)
1860 (current-time)
1861 t)
1862 (let ((days (or (and nnmail-expiry-wait-function
1863 (funcall nnmail-expiry-wait-function group))
1864 nnmail-expiry-wait)))
1865 (cond ((or (eq days 'never)
1866 (and (not force)
1867 inhibit))
1868 ;; This isn't an expirable group.
1869 nil)
1870 ((eq days 'immediate)
1871 ;; We expire all articles on sight.
1872 (if (null time)
1873 (current-time)
1874 t))
1875 ((equal time '(0 0))
1876 ;; This is an ange-ftp group, and we don't have any dates.
1877 nil)
1878 ((numberp days)
1879 (setq days (days-to-time days))
1880 ;; Compare the time with the current time.
1881 (if (null time)
1882 (time-subtract (current-time) days)
1883 (ignore-errors (time-less-p days (time-since time)))))))))
1884
1885 (declare-function gnus-group-mark-article-read "gnus-group" (group article))
1886
1887 (defun nnmail-expiry-target-group (target group)
1888 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1889 ;; that buffer if the nnfolder group isn't selected.
1890 (let (nnmail-cache-accepted-message-ids)
1891 ;; Don't enter Message-IDs into cache.
1892 ;; Let users hack it in TARGET function.
1893 (when (functionp target)
1894 (setq target (funcall target group)))
1895 (unless (eq target 'delete)
1896 (when (or (gnus-request-group target nil nil (gnus-get-info target))
1897 (gnus-request-create-group target))
1898 (let ((group-art (gnus-request-accept-article target nil nil t)))
1899 (when (and (consp group-art)
1900 (cdr group-art))
1901 (gnus-group-mark-article-read target (cdr group-art))))))))
1902
1903 (defun nnmail-fancy-expiry-target (group)
1904 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1905 (let* (header
1906 (case-fold-search nil)
1907 (from (or (message-fetch-field "from") ""))
1908 (to (or (message-fetch-field "to") ""))
1909 (date (message-fetch-field "date"))
1910 (target 'delete))
1911 (setq date (if date
1912 (condition-case err
1913 (date-to-time date)
1914 (error
1915 (message "%s" (error-message-string err))
1916 (current-time)))
1917 (current-time)))
1918 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1919 (setq header (car regexp-target-pair))
1920 (cond
1921 ;; If the header is to-from then match against the
1922 ;; To or From header
1923 ((and (equal header 'to-from)
1924 (or (string-match (cadr regexp-target-pair) from)
1925 (and (string-match (cadr regexp-target-pair) to)
1926 (let ((mail-dont-reply-to-names
1927 (message-dont-reply-to-names)))
1928 (equal (if (fboundp 'rmail-dont-reply-to)
1929 (rmail-dont-reply-to from)
1930 (mail-dont-reply-to from)) "")))))
1931 (setq target (format-time-string (caddr regexp-target-pair) date)))
1932 ((and (not (equal header 'to-from))
1933 (string-match (cadr regexp-target-pair)
1934 (or
1935 (message-fetch-field header)
1936 "")))
1937 (setq target
1938 (format-time-string (caddr regexp-target-pair) date)))))))
1939
1940 (defun nnmail-check-syntax ()
1941 "Check (and modify) the syntax of the message in the current buffer."
1942 (save-restriction
1943 (message-narrow-to-head)
1944 (let ((case-fold-search t))
1945 (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1946 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1947
1948 (defun nnmail-write-region (start end filename &optional append visit lockname)
1949 "Do a `write-region', and then set the file modes."
1950 (let ((coding-system-for-write nnmail-file-coding-system)
1951 (file-name-coding-system nnmail-pathname-coding-system))
1952 (write-region start end filename append visit lockname)
1953 (set-file-modes filename nnmail-default-file-modes)))
1954
1955 ;;;
1956 ;;; Status functions
1957 ;;;
1958
1959 (defun nnmail-replace-status (name value)
1960 "Make status NAME and VALUE part of the current status line."
1961 (save-restriction
1962 (message-narrow-to-head)
1963 (let ((status (nnmail-decode-status)))
1964 (setq status (delq (member name status) status))
1965 (when value
1966 (push (cons name value) status))
1967 (message-remove-header "status")
1968 (goto-char (point-max))
1969 (insert "Status: " (nnmail-encode-status status) "\n"))))
1970
1971 (defun nnmail-decode-status ()
1972 "Return a status-value alist from STATUS."
1973 (goto-char (point-min))
1974 (when (re-search-forward "^Status: " nil t)
1975 (let (name value status)
1976 (save-restriction
1977 ;; Narrow to the status.
1978 (narrow-to-region
1979 (point)
1980 (if (re-search-forward "^[^ \t]" nil t)
1981 (1- (point))
1982 (point-max)))
1983 ;; Go through all elements and add them to the list.
1984 (goto-char (point-min))
1985 (while (re-search-forward "[^ \t=]+" nil t)
1986 (setq name (match-string 0))
1987 (if (not (eq (char-after) ?=))
1988 ;; Implied "yes".
1989 (setq value "yes")
1990 (forward-char 1)
1991 (if (not (eq (char-after) ?\"))
1992 (if (not (looking-at "[^ \t]"))
1993 ;; Implied "no".
1994 (setq value "no")
1995 ;; Unquoted value.
1996 (setq value (match-string 0))
1997 (goto-char (match-end 0)))
1998 ;; Quoted value.
1999 (setq value (read (current-buffer)))))
2000 (push (cons name value) status)))
2001 status)))
2002
2003 (defun nnmail-encode-status (status)
2004 "Return a status string from STATUS."
2005 (mapconcat
2006 (lambda (elem)
2007 (concat
2008 (car elem) "="
2009 (if (string-match "[ \t]" (cdr elem))
2010 (prin1-to-string (cdr elem))
2011 (cdr elem))))
2012 status " "))
2013
2014 (defun nnmail-split-history ()
2015 "Generate an overview of where the last mail split put articles."
2016 (interactive)
2017 (unless nnmail-split-history
2018 (error "No current split history"))
2019 (with-output-to-temp-buffer "*nnmail split history*"
2020 (with-current-buffer standard-output
2021 (fundamental-mode))
2022 (dolist (elem nnmail-split-history)
2023 (princ (mapconcat (lambda (ga)
2024 (concat (car ga) ":" (int-to-string (cdr ga))))
2025 elem
2026 ", "))
2027 (princ "\n"))))
2028
2029 (defun nnmail-purge-split-history (group)
2030 "Remove all instances of GROUP from `nnmail-split-history'."
2031 (let ((history nnmail-split-history))
2032 (while history
2033 (setcar history (gnus-remove-if (lambda (e) (string= (car e) group))
2034 (car history)))
2035 (pop history))
2036 (setq nnmail-split-history (delq nil nnmail-split-history))))
2037
2038 (defun nnmail-new-mail-p (group)
2039 "Say whether GROUP has new mail."
2040 (let ((his nnmail-split-history)
2041 found)
2042 (while his
2043 (when (assoc group (pop his))
2044 (setq found t
2045 his nil)))
2046 found))
2047
2048 (defun nnmail-within-headers-p ()
2049 "Check to see if point is within the headers of a unix mail message.
2050 Doesn't change point."
2051 (let ((pos (point)))
2052 (save-excursion
2053 (and (nnmail-search-unix-mail-delim-backward)
2054 (not (search-forward "\n\n" pos t))))))
2055
2056 (run-hooks 'nnmail-load-hook)
2057
2058 (provide 'nnmail)
2059
2060 ;;; nnmail.el ends here