Prefer HTTPS to FTP and HTTP in documentation
[emacs.git] / lisp / gnus / nnmail.el
blobdb5415cf9f71b665b7f9a2d43bc3823a96f6e004
1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
3 ;; Copyright (C) 1995-2017 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
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.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl))
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)
37 (autoload 'gnus-add-buffer "gnus")
38 (autoload 'gnus-kill-buffer "gnus")
39 (autoload 'mail-send-and-exit "sendmail" nil t)
41 (defgroup nnmail nil
42 "Reading mail with Gnus."
43 :group 'gnus)
45 (defgroup nnmail-retrieve nil
46 "Retrieving new mail."
47 :group 'nnmail)
49 (defgroup nnmail-prepare nil
50 "Preparing (or mangling) new mail after retrieval."
51 :group 'nnmail)
53 (defgroup nnmail-duplicate nil
54 "Handling of duplicate mail messages."
55 :group 'nnmail)
57 (defgroup nnmail-split nil
58 "Organizing the incoming mail in folders."
59 :group 'nnmail)
61 (defgroup nnmail-files nil
62 "Mail files."
63 :group 'gnus-files
64 :group 'nnmail)
66 (defgroup nnmail-expire nil
67 "Expiring old mail."
68 :group 'nnmail)
70 (defgroup nnmail-procmail nil
71 "Interfacing with procmail and other mail agents."
72 :group 'nnmail)
74 (defgroup nnmail-various nil
75 "Various mail options."
76 :group 'nnmail)
78 (defcustom nnmail-split-methods '(("mail.misc" ""))
79 "Incoming mail will be split according to this variable.
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:
85 (setq nnmail-split-methods
86 \\='((\"mail.4ad\" \"From:.*4ad\")
87 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
88 (\"mail.misc\" \"\")))
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.
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.
101 The last element should always have \"\" as the regexp.
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")))
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)
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)))
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)))
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.
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)
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)
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)
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)))
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'.
177 E.g.:
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-)))
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))
203 (defcustom nnmail-fancy-expiry-targets nil
204 "Determine expiry target based on articles using fancy techniques.
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).
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.
217 Example:
219 \(setq nnmail-fancy-expiry-targets
220 \\='((to-from \"boss\" \"nnfolder:Work\")
221 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
222 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
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"))))
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)
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
248 (defcustom nnmail-resplit-incoming nil
249 "If non-nil, re-split incoming procmail sorted mail."
250 :group 'nnmail-procmail
251 :type 'boolean)
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)
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)
265 (defcustom nnmail-crosspost-link-function
266 (if (eq 'windows-nt 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")))
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.)
289 E.g.:
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))))
299 If you have xwatch running, this will alert it that mail has been
300 read.
302 If you use `display-time', you could use something like this:
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)
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)
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)
326 (defcustom nnmail-prepare-incoming-message-hook nil
327 "Hook called narrowed to each message."
328 :group 'nnmail-prepare
329 :type 'hook)
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)))
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)
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)
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)
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)
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")))
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"))
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)))
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'.
412 The format of this variable is SPLIT, where SPLIT can be one of
413 the following:
415 GROUP: Mail will be stored in GROUP (a string).
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.
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.
428 \(& SPLIT...): Process each SPLIT expression.
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.
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.
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...))
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.
448 FIELD and VALUE can also be Lisp symbols, in that case they are expanded
449 as specified in `nnmail-split-abbrev-alist'.
451 GROUP can contain \\& and \\N which will substitute from matching
452 \\(\\) patterns in the previous VALUE.
454 Example:
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\")
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)
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)))
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")))
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)
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.
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)))
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))
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)
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)
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)
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)
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)
570 ;;; Internal variables.
572 (defvar nnmail-article-buffer " *nnmail incoming*"
573 "The buffer used for splitting incoming mails.")
575 (defvar nnmail-split-history nil
576 "List of group/article elements that say where the previous split put messages.")
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'.")
585 (defvar nnmail-prepare-save-mail-hook nil
586 "Hook called before saving mail.")
588 (defvar nnmail-split-tracing nil)
589 (defvar nnmail-split-trace nil)
590 (defvar nnmail-inhibit-default-split-group nil)
594 (defun nnmail-request-post (&optional server)
595 (mail-send-and-exit nil))
597 (defvar nnmail-file-coding-system 'raw-text
598 "Coding system used in nnmail.")
600 (defvar nnmail-incoming-coding-system
601 mm-text-coding-system
602 "Coding system used in reading inbox")
604 (defcustom nnmail-pathname-coding-system nil
605 "Coding system for file name."
606 :group 'nnmail-various
607 :type 'coding-system)
609 (defun nnmail-find-file (file)
610 "Insert FILE in server buffer safely."
611 (set-buffer nntp-server-buffer)
612 (delete-region (point-min) (point-max))
613 (let ((format-alist nil)
614 (after-insert-file-functions nil))
615 (condition-case ()
616 (let ((coding-system-for-read nnmail-file-coding-system)
617 (auto-mode-alist (mm-auto-mode-alist))
618 (file-name-coding-system nnmail-pathname-coding-system))
619 (insert-file-contents file)
621 (file-error nil))))
623 (defun nnmail-group-pathname (group dir &optional file)
624 "Make file name for GROUP."
625 (concat
626 (let ((dir (file-name-as-directory (expand-file-name dir))))
627 (setq group (nnheader-replace-duplicate-chars-in-string
628 (nnheader-replace-chars-in-string group ?/ ?_)
629 ?. ?_))
630 (setq group (nnheader-translate-file-chars group))
631 ;; If this directory exists, we use it directly.
632 (file-name-as-directory
633 (if (or nnmail-use-long-file-names
634 (file-directory-p (concat dir group)))
635 (expand-file-name group dir)
636 ;; If not, we translate dots into slashes.
637 (expand-file-name
638 (nnheader-replace-chars-in-string group ?. ?/)
639 dir))))
640 (or file "")))
642 (defun nnmail-get-active ()
643 "Returns an assoc of group names and active ranges.
644 nn*-request-list should have been called before calling this function."
645 ;; Go through all groups from the active list.
646 (with-current-buffer nntp-server-buffer
647 (nnmail-parse-active)))
649 (defun nnmail-parse-active ()
650 "Parse the active file in the current buffer and return an alist."
651 (goto-char (point-min))
652 (unless (re-search-forward "[\\\"]" nil t)
653 (goto-char (point-max))
654 (while (re-search-backward "[][';?()#]" nil t)
655 (insert ?\\)))
656 (goto-char (point-min))
657 (let ((buffer (current-buffer))
658 group-assoc group max min)
659 (while (not (eobp))
660 (condition-case err
661 (progn
662 (narrow-to-region (point) (point-at-eol))
663 (setq group (read buffer))
664 (unless (stringp group)
665 (setq group (symbol-name group)))
666 (if (and (numberp (setq max (read buffer)))
667 (numberp (setq min (read buffer))))
668 (push (list (string-as-unibyte group) (cons min max))
669 group-assoc)))
670 (error nil))
671 (widen)
672 (forward-line 1))
673 group-assoc))
675 (defcustom nnmail-active-file-coding-system 'raw-text
676 "Coding system for active file."
677 :group 'nnmail-various
678 :type 'coding-system)
680 (defun nnmail-save-active (group-assoc file-name)
681 "Save GROUP-ASSOC in ACTIVE-FILE."
682 (let ((coding-system-for-write nnmail-active-file-coding-system))
683 (when file-name
684 (with-temp-file file-name
685 (mm-disable-multibyte)
686 (nnmail-generate-active group-assoc)))))
688 (defun nnmail-generate-active (alist)
689 "Generate an active file from group-alist ALIST."
690 (erase-buffer)
691 (let (group)
692 (while (setq group (pop alist))
693 (insert (format "%S %d %d y\n" (intern (car group)) (cdadr group)
694 (caadr group))))
695 (goto-char (point-max))
696 (while (search-backward "\\." nil t)
697 (delete-char 1))))
699 (defun nnmail-get-split-group (file source)
700 "Find out whether this FILE is to be split into GROUP only.
701 If SOURCE is a directory spec, try to return the group name component."
702 (if (eq (car source) 'directory)
703 (let ((file (file-name-nondirectory file)))
704 (mail-source-bind (directory source)
705 (if (string-match (concat (regexp-quote suffix) "$") file)
706 (substring file 0 (match-beginning 0))
707 nil)))
708 nil))
710 (defun nnmail-process-babyl-mail-format (func artnum-func)
711 (let ((case-fold-search t)
712 (count 0)
713 start message-id content-length do-search end)
714 (while (not (eobp))
715 (goto-char (point-min))
716 (re-search-forward
717 "\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
718 (goto-char (match-end 0))
719 (delete-region (match-beginning 0) (match-end 0))
720 (narrow-to-region
721 (setq start (point))
722 (progn
723 ;; Skip all the headers in case there are more "From "s...
724 (or (search-forward "\n\n" nil t)
725 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
726 (search-forward "\x1f\f"))
727 (point)))
728 ;; Unquote the ">From " line, if any.
729 (goto-char (point-min))
730 (when (looking-at ">From ")
731 (replace-match "X-From-Line: ") )
732 (run-hooks 'nnmail-prepare-incoming-header-hook)
733 (goto-char (point-max))
734 ;; Find the Message-ID header.
735 (save-excursion
736 (if (re-search-backward
737 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t)
738 (setq message-id (buffer-substring (match-beginning 1)
739 (match-end 1)))
740 ;; There is no Message-ID here, so we create one.
741 (save-excursion
742 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
743 (beginning-of-line)
744 (insert "Original-")))
745 (forward-line -1)
746 (insert "Message-ID: " (setq message-id (nnmail-message-id))
747 "\n")))
748 ;; Look for a Content-Length header.
749 (if (not (save-excursion
750 (and (re-search-backward
751 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
752 (setq content-length (string-to-number
753 (buffer-substring
754 (match-beginning 1)
755 (match-end 1))))
756 ;; We destroy the header, since none of
757 ;; the backends ever use it, and we do not
758 ;; want to confuse other mailers by having
759 ;; a (possibly) faulty header.
760 (progn (insert "X-") t))))
761 (setq do-search t)
762 (widen)
763 (if (or (= (+ (point) content-length) (point-max))
764 (save-excursion
765 (goto-char (+ (point) content-length))
766 (looking-at "\x1f")))
767 (progn
768 (goto-char (+ (point) content-length))
769 (setq do-search nil))
770 (setq do-search t)))
771 (widen)
772 ;; Go to the beginning of the next article - or to the end
773 ;; of the buffer.
774 (when do-search
775 (if (re-search-forward "^\x1f" nil t)
776 (goto-char (match-beginning 0))
777 (goto-char (1- (point-max)))))
778 (delete-char 1) ; delete ^_
779 (save-excursion
780 (save-restriction
781 (narrow-to-region start (point))
782 (goto-char (point-min))
783 (nnmail-check-duplication message-id func artnum-func)
784 (incf count)
785 (setq end (point-max))))
786 (goto-char end))
787 count))
789 (defsubst nnmail-search-unix-mail-delim ()
790 "Put point at the beginning of the next Unix mbox message."
791 ;; Algorithm used to find the next article in the
792 ;; brain-dead Unix mbox format:
794 ;; 1) Search for "^From ".
795 ;; 2) If we find it, then see whether the previous
796 ;; line is blank and the next line looks like a header.
797 ;; Then it's possible that this is a mail delim, and we use it.
798 (let ((case-fold-search nil)
799 found)
800 (while (not found)
801 (if (not (re-search-forward "^From " nil t))
802 (setq found 'no)
803 (save-excursion
804 (beginning-of-line)
805 (when (and (or (bobp)
806 (save-excursion
807 (forward-line -1)
808 (eq (char-after) ?\n)))
809 (save-excursion
810 (forward-line 1)
811 (while (looking-at ">From \\|From ")
812 (forward-line 1))
813 (looking-at "[^ \n\t:]+[ \n\t]*:")))
814 (setq found 'yes)))))
815 (beginning-of-line)
816 (eq found 'yes)))
818 (defun nnmail-search-unix-mail-delim-backward ()
819 "Put point at the beginning of the current Unix mbox message."
820 ;; Algorithm used to find the next article in the
821 ;; brain-dead Unix mbox format:
823 ;; 1) Search for "^From ".
824 ;; 2) If we find it, then see whether the previous
825 ;; line is blank and the next line looks like a header.
826 ;; Then it's possible that this is a mail delim, and we use it.
827 (let ((case-fold-search nil)
828 found)
829 (while (not found)
830 (if (not (re-search-backward "^From " nil t))
831 (setq found 'no)
832 (save-excursion
833 (beginning-of-line)
834 (when (and (or (bobp)
835 (save-excursion
836 (forward-line -1)
837 (eq (char-after) ?\n)))
838 (save-excursion
839 (forward-line 1)
840 (while (looking-at ">From \\|From ")
841 (forward-line 1))
842 (looking-at "[^ \n\t:]+[ \n\t]*:")))
843 (setq found 'yes)))))
844 (beginning-of-line)
845 (eq found 'yes)))
847 (defun nnmail-process-unix-mail-format (func artnum-func)
848 (let ((case-fold-search t)
849 (count 0)
850 start message-id content-length end skip head-end)
851 (goto-char (point-min))
852 (if (not (and (re-search-forward "^From " nil t)
853 (goto-char (match-beginning 0))))
854 ;; Possibly wrong format?
855 (error "Error, unknown mail format! (Possibly corrupted %s `%s'.)"
856 (if (buffer-file-name) "file" "buffer")
857 (or (buffer-file-name) (buffer-name)))
858 ;; Carry on until the bitter end.
859 (while (not (eobp))
860 (setq start (point)
861 end nil)
862 ;; Find the end of the head.
863 (narrow-to-region
864 start
865 (if (search-forward "\n\n" nil t)
866 (1- (point))
867 ;; This will never happen, but just to be on the safe side --
868 ;; if there is no head-body delimiter, we search a bit manually.
869 (while (and (looking-at "From \\|[^ \t]+:")
870 (not (eobp)))
871 (forward-line 1))
872 (point)))
873 ;; Find the Message-ID header.
874 (goto-char (point-min))
875 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
876 (setq message-id (match-string 1))
877 (save-excursion
878 (when (re-search-forward "^Message-ID[ \t]*:" nil t)
879 (beginning-of-line)
880 (insert "Original-")))
881 ;; There is no Message-ID here, so we create one.
882 (forward-line 1)
883 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
884 ;; Look for a Content-Length header.
885 (goto-char (point-min))
886 (if (not (re-search-forward
887 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
888 (setq content-length nil)
889 (setq content-length (string-to-number (match-string 1)))
890 ;; We destroy the header, since none of the backends ever
891 ;; use it, and we do not want to confuse other mailers by
892 ;; having a (possibly) faulty header.
893 (beginning-of-line)
894 (insert "X-"))
895 (run-hooks 'nnmail-prepare-incoming-header-hook)
896 ;; Find the end of this article.
897 (goto-char (point-max))
898 (widen)
899 (setq head-end (point))
900 ;; We try the Content-Length value. The idea: skip over the header
901 ;; separator, then check what happens content-length bytes into the
902 ;; message body. This should be either the end of the buffer, the
903 ;; message separator or a blank line followed by the separator.
904 ;; The blank line should probably be deleted. If neither of the
905 ;; three is met, the content-length header is probably invalid.
906 (when content-length
907 (forward-line 1)
908 (setq skip (+ (point) content-length))
909 (goto-char skip)
910 (cond ((or (= skip (point-max))
911 (= (1+ skip) (point-max)))
912 (setq end (point-max)))
913 ((looking-at "From ")
914 (setq end skip))
915 ((looking-at "[ \t]*\n\\(From \\)")
916 (setq end (match-beginning 1)))
917 (t (setq end nil))))
918 (if end
919 (goto-char end)
920 ;; No Content-Length, so we find the beginning of the next
921 ;; article or the end of the buffer.
922 (goto-char head-end)
923 (or (nnmail-search-unix-mail-delim)
924 (goto-char (point-max))))
925 ;; Allow the backend to save the article.
926 (save-excursion
927 (save-restriction
928 (narrow-to-region start (point))
929 (goto-char (point-min))
930 (incf count)
931 (nnmail-check-duplication message-id func artnum-func)
932 (setq end (point-max))))
933 (goto-char end)))
934 count))
936 (defun nnmail-process-mmdf-mail-format (func artnum-func &optional junk-func)
937 (let ((delim "^\^A\^A\^A\^A$")
938 (case-fold-search t)
939 (count 0)
940 start message-id end)
941 (goto-char (point-min))
942 (if (not (and (re-search-forward delim nil t)
943 (forward-line 1)))
944 ;; Possibly wrong format?
945 (error "Error, unknown mail format! (Possibly corrupted.)")
946 ;; Carry on until the bitter end.
947 (while (not (eobp))
948 (setq start (point))
949 ;; Find the end of the head.
950 (narrow-to-region
951 start
952 (if (search-forward "\n\n" nil t)
953 (1- (point))
954 ;; This will never happen, but just to be on the safe side --
955 ;; if there is no head-body delimiter, we search a bit manually.
956 (while (and (looking-at "From \\|[^ \t]+:")
957 (not (eobp)))
958 (forward-line 1))
959 (point)))
960 ;; Find the Message-ID header.
961 (goto-char (point-min))
962 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
963 (setq message-id (match-string 1))
964 ;; There is no Message-ID here, so we create one.
965 (save-excursion
966 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
967 (beginning-of-line)
968 (insert "Original-")))
969 (forward-line 1)
970 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
971 (run-hooks 'nnmail-prepare-incoming-header-hook)
972 ;; Find the end of this article.
973 (goto-char (point-max))
974 (widen)
975 (if (re-search-forward delim nil t)
976 (beginning-of-line)
977 (goto-char (point-max)))
978 ;; Allow the backend to save the article.
979 (save-excursion
980 (save-restriction
981 (narrow-to-region start (point))
982 (goto-char (point-min))
983 (incf count)
984 (nnmail-check-duplication message-id func artnum-func junk-func)
985 (setq end (point-max))))
986 (goto-char end)
987 (forward-line 2)))
988 count))
990 (defun nnmail-process-maildir-mail-format (func artnum-func)
991 ;; In a maildir, every file contains exactly one mail.
992 (let ((case-fold-search t)
993 message-id)
994 (goto-char (point-min))
995 ;; Find the end of the head.
996 (narrow-to-region
997 (point-min)
998 (if (search-forward "\n\n" nil t)
999 (1- (point))
1000 ;; This will never happen, but just to be on the safe side --
1001 ;; if there is no head-body delimiter, we search a bit manually.
1002 (while (and (looking-at "From \\|[^ \t]+:")
1003 (not (eobp)))
1004 (forward-line 1))
1005 (point)))
1006 ;; Find the Message-ID header.
1007 (goto-char (point-min))
1008 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
1009 (setq message-id (match-string 1))
1010 ;; There is no Message-ID here, so we create one.
1011 (save-excursion
1012 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
1013 (beginning-of-line)
1014 (insert "Original-")))
1015 (forward-line 1)
1016 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
1017 (run-hooks 'nnmail-prepare-incoming-header-hook)
1018 ;; Allow the backend to save the article.
1019 (widen)
1020 (save-excursion
1021 (goto-char (point-min))
1022 (nnmail-check-duplication message-id func artnum-func))
1025 (defvar nnmail-group-names-not-encoded-p nil
1026 "Non-nil means group names are not encoded.")
1028 (defun nnmail-split-incoming (incoming func &optional exit-func
1029 group artnum-func junk-func)
1030 "Go through the entire INCOMING file and pick out each individual mail.
1031 FUNC will be called with the buffer narrowed to each mail.
1032 INCOMING can also be a buffer object. In that case, the mail
1033 will be copied over from that buffer."
1034 (let ( ;; If this is a group-specific split, we bind the split
1035 ;; methods to just this group.
1036 (nnmail-split-methods (if (and group
1037 (not nnmail-resplit-incoming))
1038 (list (list group ""))
1039 nnmail-split-methods))
1040 (nnmail-group-names-not-encoded-p t))
1041 ;; Insert the incoming file.
1042 (with-current-buffer (get-buffer-create nnmail-article-buffer)
1043 (erase-buffer)
1044 (if (bufferp incoming)
1045 (insert-buffer-substring incoming)
1046 (let ((coding-system-for-read nnmail-incoming-coding-system))
1047 (mm-insert-file-contents incoming)))
1048 (prog1
1049 (if (zerop (buffer-size))
1051 (goto-char (point-min))
1052 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
1053 ;; Handle both babyl, MMDF and unix mail formats, since
1054 ;; movemail will use the former when fetching from a
1055 ;; mailbox, the latter when fetching from a file.
1056 (cond ((or (looking-at "\^L")
1057 (looking-at "BABYL OPTIONS:"))
1058 (nnmail-process-babyl-mail-format func artnum-func))
1059 ((looking-at "\^A\^A\^A\^A")
1060 (nnmail-process-mmdf-mail-format
1061 func artnum-func junk-func))
1062 ((looking-at "Return-Path:")
1063 (nnmail-process-maildir-mail-format func artnum-func))
1065 (nnmail-process-unix-mail-format func artnum-func))))
1066 (when exit-func
1067 (funcall exit-func))
1068 (kill-buffer (current-buffer))))))
1070 (defun nnmail-article-group (func &optional trace junk-func)
1071 "Look at the headers and return an alist of groups that match.
1072 FUNC will be called with the group name to determine the article number."
1073 (let ((methods (or nnmail-split-methods '(("bogus" ""))))
1074 (obuf (current-buffer))
1075 group-art method grp)
1076 (if (and (sequencep methods)
1077 (= (length methods) 1)
1078 (not nnmail-inhibit-default-split-group))
1079 ;; If there is only just one group to put everything in, we
1080 ;; just return a list with just this one method in.
1081 (setq group-art
1082 (list (cons (caar methods) (funcall func (caar methods)))))
1083 ;; We do actual comparison.
1084 ;; Copy the article into the work buffer.
1085 (with-current-buffer nntp-server-buffer
1086 (erase-buffer)
1087 (insert-buffer-substring obuf)
1088 ;; Narrow to headers.
1089 (narrow-to-region
1090 (goto-char (point-min))
1091 (if (search-forward "\n\n" nil t)
1092 (point)
1093 (point-max)))
1094 (goto-char (point-min))
1095 ;; Decode MIME headers and charsets.
1096 (when nnmail-mail-splitting-decodes
1097 (let ((mail-parse-charset nnmail-mail-splitting-charset))
1098 (mail-decode-encoded-word-region (point-min) (point-max))))
1099 ;; Fold continuation lines.
1100 (goto-char (point-min))
1101 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1102 (replace-match " " t t))
1103 ;; Nuke pathologically long headers. Since Gnus applies
1104 ;; pathologically complex regexps to the buffer, lines
1105 ;; that are looong will take longer than the Universe's
1106 ;; existence to process.
1107 (goto-char (point-min))
1108 (while (not (eobp))
1109 (unless (< (move-to-column nnmail-split-header-length-limit)
1110 nnmail-split-header-length-limit)
1111 (delete-region (point) (point-at-eol)))
1112 (forward-line 1))
1113 ;; Allow washing.
1114 (goto-char (point-min))
1115 (run-hooks 'nnmail-split-hook)
1116 (when (setq nnmail-split-tracing trace)
1117 (setq nnmail-split-trace nil))
1118 (if (or (and (symbolp nnmail-split-methods)
1119 (fboundp nnmail-split-methods))
1120 (not (consp (car-safe nnmail-split-methods)))
1121 (and (listp nnmail-split-methods)
1122 ;; Not a regular split method, so it has to be a
1123 ;; fancy one.
1124 (not (let ((top-element (car-safe nnmail-split-methods)))
1125 (and (= 2 (length top-element))
1126 (stringp (nth 0 top-element))
1127 (stringp (nth 1 top-element)))))))
1128 (let* ((method-function
1129 (if (and (symbolp nnmail-split-methods)
1130 (fboundp nnmail-split-methods))
1131 nnmail-split-methods
1132 'nnmail-split-fancy))
1133 (split
1134 (condition-case error-info
1135 ;; `nnmail-split-methods' is a function, so we
1136 ;; just call this function here and use the
1137 ;; result.
1138 (or (funcall method-function)
1139 (and (not nnmail-inhibit-default-split-group)
1140 '("bogus")))
1141 (error
1142 (nnheader-message
1143 5 "Error in `nnmail-split-methods'; using `bogus' mail group: %S" error-info)
1144 (sit-for 1)
1145 '("bogus")))))
1146 (setq split (delete-dups split))
1147 ;; The article may be "cross-posted" to `junk'. What
1148 ;; to do? Just remove the `junk' spec. Don't really
1149 ;; see anything else to do...
1150 (when (and (memq 'junk split)
1151 junk-func)
1152 (funcall junk-func 'junk))
1153 (setq split (delq 'junk split))
1154 (when split
1155 (setq group-art
1156 (mapcar
1157 (lambda (group) (cons group (funcall func group)))
1158 split))))
1159 ;; Go through the split methods to find a match.
1160 (while (and methods
1161 (or nnmail-crosspost
1162 (not group-art)))
1163 (goto-char (point-max))
1164 (setq method (pop methods)
1165 grp (car method))
1166 (if (or methods
1167 (not (equal "" (nth 1 method))))
1168 (when (and
1169 (ignore-errors
1170 (if (stringp (nth 1 method))
1171 (let ((expand (string-match "\\\\[0-9&]" grp))
1172 (pos (re-search-backward (cadr method)
1173 nil t)))
1174 (and expand
1175 (setq grp (nnmail-expand-newtext grp)))
1176 pos)
1177 ;; Function to say whether this is a match.
1178 (funcall (nth 1 method) grp)))
1179 ;; Don't enter the article into the same
1180 ;; group twice.
1181 (not (assoc grp group-art)))
1182 (push (cons grp (funcall func grp))
1183 group-art))
1184 ;; This is the final group, which is used as a
1185 ;; catch-all.
1186 (when (and (not group-art)
1187 (or (equal "" (nth 1 method))
1188 (not nnmail-inhibit-default-split-group)))
1189 (setq group-art
1190 (list (cons (car method)
1191 (funcall func (car method))))))))
1192 ;; Fall back on "bogus" if all else fails.
1193 (when (and (not group-art)
1194 (not nnmail-inhibit-default-split-group))
1195 (setq group-art (list (cons "bogus" (funcall func "bogus"))))))
1196 ;; Produce a trace if non-empty.
1197 (when (and trace nnmail-split-trace)
1198 (let ((restore (current-buffer)))
1199 (nnheader-set-temp-buffer "*Split Trace*")
1200 (gnus-add-buffer)
1201 (dolist (trace (nreverse nnmail-split-trace))
1202 (prin1 trace (current-buffer))
1203 (insert "\n"))
1204 (goto-char (point-min))
1205 (gnus-configure-windows 'split-trace)
1206 (set-buffer restore)))
1207 (widen)
1208 ;; See whether the split methods returned `junk'.
1209 (if (equal group-art '(junk))
1211 ;; The article may be "cross-posted" to `junk'. What
1212 ;; to do? Just remove the `junk' spec. Don't really
1213 ;; see anything else to do...
1214 (let (elem)
1215 (while (setq elem (car (memq 'junk group-art)))
1216 (setq group-art (delq elem group-art)))
1217 (nreverse group-art)))))))
1219 (defun nnmail-insert-lines ()
1220 "Insert how many lines there are in the body of the mail.
1221 Return the number of characters in the body."
1222 (let (lines chars)
1223 (save-excursion
1224 (goto-char (point-min))
1225 (unless (search-forward "\n\n" nil t)
1226 (goto-char (point-max))
1227 (insert "\n"))
1228 (setq chars (- (point-max) (point)))
1229 (setq lines (count-lines (point) (point-max)))
1230 (forward-char -1)
1231 (save-excursion
1232 (when (re-search-backward "^Lines: " nil t)
1233 (delete-region (point) (progn (forward-line 1) (point)))))
1234 (beginning-of-line)
1235 (insert (format "Lines: %d\n" (max lines 0)))
1236 chars)))
1238 (defun nnmail-insert-xref (group-alist)
1239 "Insert an Xref line based on the (group . article) alist."
1240 (save-excursion
1241 (goto-char (point-min))
1242 (unless (search-forward "\n\n" nil t)
1243 (goto-char (point-max))
1244 (insert "\n"))
1245 (forward-char -1)
1246 (when (re-search-backward "^Xref: " nil t)
1247 (delete-region (match-beginning 0)
1248 (progn (forward-line 1) (point))))
1249 (insert (format "Xref: %s" (system-name)))
1250 (while group-alist
1251 (insert (if (mm-multibyte-p)
1252 (string-as-multibyte
1253 (format " %s:%d" (caar group-alist) (cdar group-alist)))
1254 (string-as-unibyte
1255 (format " %s:%d" (caar group-alist) (cdar group-alist)))))
1256 (setq group-alist (cdr group-alist)))
1257 (insert "\n")))
1259 ;;; Message washing functions
1261 (defun nnmail-remove-leading-whitespace ()
1262 "Remove excessive whitespace from all headers."
1263 (goto-char (point-min))
1264 (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1265 (replace-match "\\1" t)))
1267 (defun nnmail-remove-list-identifiers ()
1268 "Remove list identifiers from Subject headers."
1269 (let ((regexp
1270 (if (consp nnmail-list-identifiers)
1271 (mapconcat 'identity nnmail-list-identifiers " *\\|")
1272 nnmail-list-identifiers)))
1273 (when regexp
1274 (goto-char (point-min))
1275 (while (re-search-forward
1276 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1277 nil t)
1278 (delete-region (match-beginning 2) (match-end 0))
1279 (beginning-of-line))
1280 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1281 nil t)
1282 (delete-region (match-beginning 1) (match-end 1))
1283 (beginning-of-line)))))
1285 (defun nnmail-remove-tabs ()
1286 "Translate TAB characters into SPACE characters."
1287 (subst-char-in-region (point-min) (point-max) ?\t ? t))
1289 (defcustom nnmail-broken-references-mailers
1290 "^X-Mailer:.*\\(Eudora\\|Pegasus\\)"
1291 "Header line matching mailer producing bogus References lines.
1292 See `nnmail-ignore-broken-references'."
1293 :group 'nnmail-prepare
1294 :version "23.1" ;; No Gnus
1295 :type 'regexp)
1297 (defun nnmail-ignore-broken-references ()
1298 "Ignore the References line and use In-Reply-To
1300 Eudora has a broken References line, but an OK In-Reply-To."
1301 (goto-char (point-min))
1302 (when (re-search-forward nnmail-broken-references-mailers nil t)
1303 (goto-char (point-min))
1304 (when (re-search-forward "^References:" nil t)
1305 (beginning-of-line)
1306 (insert "X-Gnus-Broken-Eudora-"))
1307 (goto-char (point-min))
1308 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1309 (replace-match "\\1" t))))
1311 (defalias 'nnmail-fix-eudora-headers 'nnmail-ignore-broken-references)
1312 (make-obsolete 'nnmail-fix-eudora-headers 'nnmail-ignore-broken-references "Emacs 23.1")
1314 (custom-add-option 'nnmail-prepare-incoming-header-hook
1315 'nnmail-ignore-broken-references)
1317 ;;; Utility functions
1319 (declare-function gnus-activate-group "gnus-start"
1320 (group &optional scan dont-check method dont-sub-check))
1322 (defun nnmail-do-request-post (accept-func &optional server)
1323 "Utility function to directly post a message to an nnmail-derived group.
1324 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1325 to actually put the message in the right group."
1326 (let ((success t))
1327 (dolist (mbx (message-unquote-tokens
1328 (message-tokenize-header
1329 (message-fetch-field "Newsgroups") ", ")) success)
1330 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1331 (or (gnus-active to-newsgroup)
1332 (gnus-activate-group to-newsgroup)
1333 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1334 to-newsgroup))
1335 (or (and (gnus-request-create-group
1336 to-newsgroup gnus-command-method)
1337 (gnus-activate-group to-newsgroup nil nil
1338 gnus-command-method))
1339 (error "Couldn't create group %s" to-newsgroup)))
1340 (error "No such group: %s" to-newsgroup))
1341 (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1342 (setq success nil))))))
1344 (defun nnmail-split-fancy ()
1345 "Fancy splitting method.
1346 See the documentation for the variable `nnmail-split-fancy' for details."
1347 (with-syntax-table nnmail-split-fancy-syntax-table
1348 (nnmail-split-it nnmail-split-fancy)))
1350 (defvar nnmail-split-cache nil)
1351 ;; Alist of split expressions their equivalent regexps.
1353 (defun nnmail-split-it (split)
1354 ;; Return a list of groups matching SPLIT.
1355 (let (cached-pair)
1356 (cond
1357 ;; nil split
1358 ((null split)
1359 nil)
1361 ;; A group name. Do the \& and \N subs into the string.
1362 ((stringp split)
1363 (when nnmail-split-tracing
1364 (push split nnmail-split-trace))
1365 (list (nnmail-expand-newtext split t)))
1367 ;; Junk the message.
1368 ((eq split 'junk)
1369 (when nnmail-split-tracing
1370 (push "junk" nnmail-split-trace))
1371 (list 'junk))
1373 ;; Builtin & operation.
1374 ((eq (car split) '&)
1375 (mapcan 'nnmail-split-it (cdr split)))
1377 ;; Builtin | operation.
1378 ((eq (car split) '|)
1379 (let (done)
1380 (while (and (not done) (cdr split))
1381 (setq split (cdr split)
1382 done (nnmail-split-it (car split))))
1383 done))
1385 ;; Builtin : operation.
1386 ((eq (car split) ':)
1387 (when nnmail-split-tracing
1388 (push split nnmail-split-trace))
1389 (nnmail-split-it (save-excursion (eval (cdr split)))))
1391 ;; Builtin ! operation.
1392 ((eq (car split) '!)
1393 (funcall (cadr split) (nnmail-split-it (caddr split))))
1395 ;; Check the cache for the regexp for this split.
1396 ((setq cached-pair (assq split nnmail-split-cache))
1397 (let (split-result
1398 match-data
1399 (end-point (point-max))
1400 (value (nth 1 split)))
1401 (if (symbolp value)
1402 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1403 (while (and (goto-char end-point)
1404 (re-search-backward (cdr cached-pair) nil t))
1405 (setq match-data (match-data))
1406 (when nnmail-split-tracing
1407 (push split nnmail-split-trace))
1408 (let ((split-rest (cddr split))
1409 (end (match-end 0))
1410 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1411 ;; So, start-of-value is the point just before the
1412 ;; beginning of the value, whereas after-header-name
1413 ;; is the point just after the field name.
1414 (start-of-value (match-end 1))
1415 (after-header-name (match-end 2)))
1416 ;; Start the next search just before the beginning of the
1417 ;; VALUE match.
1418 (setq end-point (1- start-of-value))
1419 ;; Handle - RESTRICTs
1420 (while (eq (car split-rest) '-)
1421 ;; RESTRICT must start after-header-name and
1422 ;; end after start-of-value, so that, for
1423 ;; (any "foo" - "x-foo" "foo.list")
1424 ;; we do not exclude foo.list just because
1425 ;; the header is: ``To: x-foo, foo''
1426 (goto-char end)
1427 (if (and (re-search-backward (cadr split-rest)
1428 after-header-name t)
1429 (> (match-end 0) start-of-value))
1430 (setq split-rest nil)
1431 (setq split-rest (cddr split-rest))))
1432 (when split-rest
1433 (goto-char end)
1434 ;; Someone might want to do a \N sub on this match, so
1435 ;; restore the match data.
1436 (set-match-data match-data)
1437 (dolist (sp (nnmail-split-it (car split-rest)))
1438 (unless (member sp split-result)
1439 (push sp split-result))))))
1440 split-result))
1442 ;; Not in cache, compute a regexp for the field/value pair.
1444 (let ((field (nth 0 split))
1445 (value (nth 1 split))
1446 (split-rest (cddr split))
1447 partial-front
1448 partial-rear
1449 regexp)
1450 (if (symbolp value)
1451 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1452 (if (and (>= (length value) 2)
1453 (string= ".*" (substring value 0 2)))
1454 (setq value (substring value 2)
1455 partial-front ""))
1456 ;; Same trick for the rear of the regexp
1457 (if (and (>= (length value) 2)
1458 (string= ".*" (substring value -2)))
1459 (setq value (substring value 0 -2)
1460 partial-rear ""))
1461 ;; Invert the match-partial-words behavior if the optional
1462 ;; last element is specified.
1463 (while (eq (car split-rest) '-)
1464 (setq split-rest (cddr split-rest)))
1465 (when (if (cadr split-rest)
1466 (not nnmail-split-fancy-match-partial-words)
1467 nnmail-split-fancy-match-partial-words)
1468 (setq partial-front ""
1469 partial-rear ""))
1470 (setq regexp (concat "^\\(\\("
1471 (if (symbolp field)
1472 (cdr (assq field nnmail-split-abbrev-alist))
1473 field)
1474 "\\):.*\\)"
1475 (or partial-front "\\<")
1476 "\\("
1477 value
1478 "\\)"
1479 (or partial-rear "\\>")))
1480 (push (cons split regexp) nnmail-split-cache)
1481 ;; Now that it's in the cache, just call nnmail-split-it again
1482 ;; on the same split, which will find it immediately in the cache.
1483 (nnmail-split-it split))))))
1485 (defun nnmail-expand-newtext (newtext &optional fancyp)
1486 (let ((len (length newtext))
1487 (pos 0)
1488 c expanded beg N did-expand)
1489 (while (< pos len)
1490 (setq beg pos)
1491 (while (and (< pos len)
1492 (not (= (aref newtext pos) ?\\)))
1493 (setq pos (1+ pos)))
1494 (unless (= beg pos)
1495 (push (substring newtext beg pos) expanded))
1496 (when (< pos len)
1497 ;; We hit a \; expand it.
1498 (setq did-expand t
1499 pos (1+ pos)
1500 c (aref newtext pos))
1501 (if (not (or (= c ?\&)
1502 (and (>= c ?1)
1503 (<= c ?9))))
1504 ;; \ followed by some character we don't expand.
1505 (push (char-to-string c) expanded)
1506 ;; \& or \N
1507 (if (= c ?\&)
1508 (setq N 0)
1509 (setq N (- c ?0)))
1510 ;; We wrapped the searches in parentheses, so we have to
1511 ;; add some parentheses here...
1512 (when fancyp
1513 (setq N (+ N 3)))
1514 (when (match-beginning N)
1515 (push (if nnmail-split-lowercase-expanded
1516 (downcase (buffer-substring (match-beginning N)
1517 (match-end N)))
1518 (buffer-substring (match-beginning N) (match-end N)))
1519 expanded))))
1520 (setq pos (1+ pos)))
1521 (if did-expand
1522 (apply 'concat (nreverse expanded))
1523 newtext)))
1525 ;; Activate a backend only if it isn't already activated.
1526 ;; If FORCE, re-read the active file even if the backend is
1527 ;; already activated.
1528 (defun nnmail-activate (backend &optional force)
1529 (nnheader-init-server-buffer)
1530 (let (file timestamp file-time)
1531 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1532 force
1533 (and (setq file (ignore-errors
1534 (symbol-value (intern (format "%s-active-file"
1535 backend)))))
1536 (setq file-time (nth 5 (file-attributes file)))
1537 (or (not
1538 (setq timestamp
1539 (condition-case ()
1540 (symbol-value (intern
1541 (format "%s-active-timestamp"
1542 backend)))
1543 (error 'none))))
1544 (not (consp timestamp))
1545 (equal timestamp '(0 0))
1546 (> (nth 0 file-time) (nth 0 timestamp))
1547 (and (= (nth 0 file-time) (nth 0 timestamp))
1548 (> (nth 1 file-time) (nth 1 timestamp))))))
1549 (save-excursion
1550 (or (eq timestamp 'none)
1551 (set (intern (format "%s-active-timestamp" backend))
1552 file-time))
1553 (funcall (intern (format "%s-request-list" backend)))))
1556 (defun nnmail-message-id ()
1557 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1560 ;;; nnmail duplicate handling
1563 (defvar nnmail-cache-buffer nil)
1565 (defun nnmail-cache-open ()
1566 (if (or (not nnmail-treat-duplicates)
1567 (and nnmail-cache-buffer
1568 (buffer-name nnmail-cache-buffer)))
1569 () ; The buffer is open.
1570 (with-current-buffer
1571 (setq nnmail-cache-buffer
1572 (get-buffer-create " *nnmail message-id cache*"))
1573 (gnus-add-buffer)
1574 (when (file-exists-p nnmail-message-id-cache-file)
1575 (nnheader-insert-file-contents nnmail-message-id-cache-file))
1576 (set-buffer-modified-p nil)
1577 (current-buffer))))
1579 (defun nnmail-cache-close ()
1580 (when (and nnmail-cache-buffer
1581 nnmail-treat-duplicates
1582 (buffer-name nnmail-cache-buffer)
1583 (buffer-modified-p nnmail-cache-buffer))
1584 (with-current-buffer nnmail-cache-buffer
1585 ;; Weed out the excess number of Message-IDs.
1586 (goto-char (point-max))
1587 (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1588 (progn
1589 (beginning-of-line)
1590 (delete-region (point-min) (point))))
1591 ;; Save the buffer.
1592 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1593 (make-directory (file-name-directory nnmail-message-id-cache-file)
1595 (nnmail-write-region (point-min) (point-max)
1596 nnmail-message-id-cache-file nil 'silent)
1597 (set-buffer-modified-p nil)
1598 (setq nnmail-cache-buffer nil)
1599 (gnus-kill-buffer (current-buffer)))))
1601 (defun nnmail-cache-insert (id grp &optional subject sender)
1602 (when (stringp id)
1603 ;; this will handle cases like `B r' where the group is nil
1604 (let ((grp (or grp gnus-newsgroup-name "UNKNOWN")))
1605 (run-hook-with-args 'nnmail-spool-hook
1606 id grp subject sender))
1607 (when nnmail-treat-duplicates
1608 ;; Store some information about the group this message is written
1609 ;; to. This is passed in as the grp argument -- all locations this
1610 ;; has been called from have been checked and the group is available.
1611 ;; The only ambiguous case is nnmail-check-duplication which will only
1612 ;; pass the first (of possibly >1) group which matches. -Josh
1613 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1614 (nnmail-cache-open))
1615 (with-current-buffer nnmail-cache-buffer
1616 (goto-char (point-max))
1617 (if (and grp (not (string= "" grp))
1618 (gnus-methods-equal-p gnus-command-method
1619 (nnmail-cache-primary-mail-backend)))
1620 (let ((regexp (if (consp nnmail-cache-ignore-groups)
1621 (mapconcat 'identity nnmail-cache-ignore-groups
1622 "\\|")
1623 nnmail-cache-ignore-groups)))
1624 (unless (and regexp (string-match regexp grp))
1625 (insert id "\t" grp "\n")))
1626 (insert id "\n"))))))
1628 (defun nnmail-cache-primary-mail-backend ()
1629 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
1630 (be nil)
1631 (res nil)
1632 (get-new-mail nil))
1633 (while (and (null res) be-list)
1634 (setq be (car be-list))
1635 (setq be-list (cdr be-list))
1636 (when (and (gnus-method-option-p be 'respool)
1637 (setq get-new-mail
1638 (intern (format "%s-get-new-mail" (car be))))
1639 (boundp get-new-mail)
1640 (symbol-value get-new-mail))
1641 (setq res be)))
1642 res))
1644 ;; Fetch the group name corresponding to the message id stored in the
1645 ;; cache.
1646 (defun nnmail-cache-fetch-group (id)
1647 (when (and nnmail-treat-duplicates nnmail-cache-buffer)
1648 (with-current-buffer nnmail-cache-buffer
1649 (goto-char (point-max))
1650 (when (search-backward id nil t)
1651 (beginning-of-line)
1652 (skip-chars-forward "^\n\r\t")
1653 (unless (looking-at "[\r\n]")
1654 (forward-char 1)
1655 (buffer-substring (point) (point-at-eol)))))))
1657 ;; Function for nnmail-split-fancy: look up all references in the
1658 ;; cache and if a match is found, return that group.
1659 (defun nnmail-split-fancy-with-parent ()
1660 "Split this message into the same group as its parent.
1661 This function can be used as an entry in `nnmail-split-fancy', for
1662 example like this: (: nnmail-split-fancy-with-parent)
1663 For a message to be split, it looks for the parent message in the
1664 References or In-Reply-To header and then looks in the message id
1665 cache file (given by the variable `nnmail-message-id-cache-file') to
1666 see which group that message was put in. This group is returned.
1668 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1669 (let* ((refstr (or (message-fetch-field "references")
1670 (message-fetch-field "in-reply-to")))
1671 (references nil)
1672 (res nil)
1673 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1674 (mapconcat
1675 (lambda (x) (format "\\(%s\\)" x))
1676 nnmail-split-fancy-with-parent-ignore-groups
1677 "\\|")
1678 nnmail-split-fancy-with-parent-ignore-groups)))
1679 (when refstr
1680 (setq references (nreverse (gnus-split-references refstr)))
1681 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1682 (nnmail-cache-open))
1683 (dolist (x references)
1684 (setq res (or (nnmail-cache-fetch-group x) res))
1685 (when (or (member res '("delayed" "drafts" "queue"))
1686 (and regexp res (string-match regexp res)))
1687 (setq res nil)))
1688 res)))
1690 (defun nnmail-cache-id-exists-p (id)
1691 (when nnmail-treat-duplicates
1692 (with-current-buffer nnmail-cache-buffer
1693 (goto-char (point-max))
1694 (search-backward id nil t))))
1696 (defun nnmail-fetch-field (header)
1697 (save-excursion
1698 (save-restriction
1699 (message-narrow-to-head)
1700 (message-fetch-field header))))
1702 (defun nnmail-check-duplication (message-id func artnum-func
1703 &optional junk-func)
1704 (run-hooks 'nnmail-prepare-incoming-message-hook)
1705 ;; If this is a duplicate message, then we do not save it.
1706 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1707 (case-fold-search t)
1708 (action (when duplication
1709 (cond
1710 ((memq nnmail-treat-duplicates '(warn delete))
1711 nnmail-treat-duplicates)
1712 ((functionp nnmail-treat-duplicates)
1713 (funcall nnmail-treat-duplicates message-id))
1715 nnmail-treat-duplicates))))
1716 group-art)
1717 ;; We insert a line that says what the mail source is.
1718 (let ((case-fold-search t))
1719 (goto-char (point-min))
1720 (re-search-forward "^message-id[ \t]*:" nil t)
1721 (beginning-of-line)
1722 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1724 ;; Let the backend save the article (or not).
1725 (cond
1726 ((not duplication)
1727 (funcall func (setq group-art
1728 (nreverse (nnmail-article-group
1729 artnum-func nil junk-func))))
1730 (nnmail-cache-insert message-id (caar group-art)))
1731 ((eq action 'delete)
1732 (setq group-art nil))
1733 ((eq action 'warn)
1734 ;; We insert a warning.
1735 (let ((case-fold-search t))
1736 (goto-char (point-min))
1737 (re-search-forward "^message-id[ \t]*:" nil t)
1738 (beginning-of-line)
1739 (insert
1740 "Gnus-Warning: This is a duplicate of message " message-id "\n")
1741 (funcall func (setq group-art
1742 (nreverse (nnmail-article-group artnum-func))))))
1744 (funcall func (setq group-art
1745 (nreverse (nnmail-article-group artnum-func))))))
1746 ;; Add the group-art list to the history list.
1747 (if group-art
1748 (push group-art nnmail-split-history)
1749 (delete-region (point-min) (point-max)))))
1751 ;;; Get new mail.
1753 (defvar nnmail-fetched-sources nil)
1755 (defun nnmail-get-value (&rest args)
1756 (let ((sym (intern (apply 'format args))))
1757 (when (boundp sym)
1758 (symbol-value sym))))
1760 (defun nnmail-get-new-mail (method exit-func temp
1761 &optional group spool-func)
1762 "Read new incoming mail."
1763 (nnmail-get-new-mail-1 method exit-func temp group nil spool-func))
1765 (defun nnmail-get-new-mail-per-group ()
1766 "Tell us whether the mail-sources specify that `nnmail-get-new-mail' should
1767 be called once per group or once for all groups."
1768 (or (assq 'group mail-sources)
1769 (assq 'directory mail-sources)))
1771 (defun nnmail-get-new-mail-1 (method exit-func temp
1772 group in-group spool-func)
1773 (let* ((sources mail-sources)
1774 fetching-sources
1775 (i 0)
1776 (new 0)
1777 (total 0)
1778 source)
1779 (when (and (nnmail-get-value "%s-get-new-mail" method)
1780 sources)
1781 (while (setq source (pop sources))
1782 ;; Use group's parameter
1783 (when (and (eq (car source) 'group)
1784 group)
1785 (let ((mail-sources
1786 (list
1787 (gnus-group-find-parameter
1788 (concat (symbol-name method) ":" group)
1789 'mail-source t))))
1790 (nnmail-get-new-mail-1 method exit-func temp
1791 group group spool-func))
1792 (setq source nil))
1793 ;; Hack to only fetch the contents of a single group's spool file.
1794 (when (and (eq (car source) 'directory)
1795 (null nnmail-scan-directory-mail-source-once)
1796 group)
1797 (mail-source-bind (directory source)
1798 (setq source (append source
1799 (list
1800 :predicate
1801 (gnus-byte-compile
1802 `(lambda (file)
1803 (string-equal
1804 ,(concat group suffix)
1805 (file-name-nondirectory file)))))))))
1806 (when nnmail-fetched-sources
1807 (if (member source nnmail-fetched-sources)
1808 (setq source nil)
1809 (push source nnmail-fetched-sources)
1810 (push source fetching-sources)))))
1811 (when fetching-sources
1812 ;; We first activate all the groups.
1813 (nnmail-activate method)
1814 ;; Allow the user to hook.
1815 (run-hooks 'nnmail-pre-get-new-mail-hook)
1816 ;; Open the message-id cache.
1817 (nnmail-cache-open)
1818 ;; The we go through all the existing mail source specification
1819 ;; and fetch the mail from each.
1820 (while (setq source (pop fetching-sources))
1821 (when (setq new
1822 (condition-case cond
1823 (mail-source-fetch
1824 source
1825 (gnus-byte-compile
1826 `(lambda (file orig-file)
1827 (nnmail-split-incoming
1828 file ',(intern (format "%s-save-mail" method))
1829 ',spool-func
1830 (or in-group
1831 (if (equal file orig-file)
1833 (nnmail-get-split-group orig-file
1834 ',source)))
1835 ',(intern (format "%s-active-number" method))))))
1836 ((error quit)
1837 (message "Mail source %s failed: %s" source cond)
1838 0)))
1839 (incf total new)
1840 (incf i)))
1841 ;; If we did indeed read any incoming spools, we save all info.
1842 (if (zerop total)
1843 (when mail-source-plugged
1844 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1845 method (car source)))
1846 (nnmail-save-active
1847 (nnmail-get-value "%s-group-alist" method)
1848 (nnmail-get-value "%s-active-file" method))
1849 (when exit-func
1850 (funcall exit-func))
1851 (run-hooks 'nnmail-read-incoming-hook)
1852 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1853 total))
1854 ;; Close the message-id cache.
1855 (nnmail-cache-close)
1856 ;; Allow the user to hook.
1857 (run-hooks 'nnmail-post-get-new-mail-hook))))
1859 (defun nnmail-expired-article-p (group time force &optional inhibit)
1860 "Say whether an article that is TIME old in GROUP should be expired.
1861 If TIME is nil, then return the cutoff time for oldness instead."
1862 (if force
1863 (if (null time)
1864 (current-time)
1866 (let ((days (or (and nnmail-expiry-wait-function
1867 (funcall nnmail-expiry-wait-function group))
1868 nnmail-expiry-wait)))
1869 (cond ((or (eq days 'never)
1870 (and (not force)
1871 inhibit))
1872 ;; This isn't an expirable group.
1873 nil)
1874 ((eq days 'immediate)
1875 ;; We expire all articles on sight.
1876 (if (null time)
1877 (current-time)
1879 ((equal time '(0 0))
1880 ;; This is an ange-ftp group, and we don't have any dates.
1881 nil)
1882 ((numberp days)
1883 (setq days (days-to-time days))
1884 ;; Compare the time with the current time.
1885 (if (null time)
1886 (time-subtract (current-time) days)
1887 (ignore-errors (time-less-p days (time-since time)))))))))
1889 (declare-function gnus-group-mark-article-read "gnus-group" (group article))
1891 (defun nnmail-expiry-target-group (target group)
1892 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1893 ;; that buffer if the nnfolder group isn't selected.
1894 (let (nnmail-cache-accepted-message-ids)
1895 ;; Don't enter Message-IDs into cache.
1896 ;; Let users hack it in TARGET function.
1897 (when (functionp target)
1898 (setq target (funcall target group)))
1899 (unless (eq target 'delete)
1900 (when (or (gnus-request-group target nil nil (gnus-get-info target))
1901 (gnus-request-create-group target))
1902 (let ((group-art (gnus-request-accept-article target nil nil t)))
1903 (when (and (consp group-art)
1904 (cdr group-art))
1905 (gnus-group-mark-article-read target (cdr group-art))))))))
1907 (defun nnmail-fancy-expiry-target (group)
1908 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1909 (let* (header
1910 (case-fold-search nil)
1911 (from (or (message-fetch-field "from") ""))
1912 (to (or (message-fetch-field "to") ""))
1913 (date (message-fetch-field "date"))
1914 (target 'delete))
1915 (setq date (if date
1916 (condition-case err
1917 (date-to-time date)
1918 (error
1919 (message "%s" (error-message-string err))
1920 (current-time)))
1921 (current-time)))
1922 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1923 (setq header (car regexp-target-pair))
1924 (cond
1925 ;; If the header is to-from then match against the
1926 ;; To or From header
1927 ((and (equal header 'to-from)
1928 (or (string-match (cadr regexp-target-pair) from)
1929 (and (string-match (cadr regexp-target-pair) to)
1930 (let ((mail-dont-reply-to-names
1931 (message-dont-reply-to-names)))
1932 (equal (if (fboundp 'rmail-dont-reply-to)
1933 (rmail-dont-reply-to from)
1934 (mail-dont-reply-to from)) "")))))
1935 (setq target (format-time-string (caddr regexp-target-pair) date)))
1936 ((and (not (equal header 'to-from))
1937 (string-match (cadr regexp-target-pair)
1939 (message-fetch-field header)
1940 "")))
1941 (setq target
1942 (format-time-string (caddr regexp-target-pair) date)))))))
1944 (defun nnmail-check-syntax ()
1945 "Check (and modify) the syntax of the message in the current buffer."
1946 (save-restriction
1947 (message-narrow-to-head)
1948 (let ((case-fold-search t))
1949 (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1950 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1952 (defun nnmail-write-region (start end filename &optional append visit lockname)
1953 "Do a `write-region', and then set the file modes."
1954 (let ((coding-system-for-write nnmail-file-coding-system)
1955 (file-name-coding-system nnmail-pathname-coding-system))
1956 (write-region start end filename append visit lockname)
1957 (set-file-modes filename nnmail-default-file-modes)))
1960 ;;; Status functions
1963 (defun nnmail-replace-status (name value)
1964 "Make status NAME and VALUE part of the current status line."
1965 (save-restriction
1966 (message-narrow-to-head)
1967 (let ((status (nnmail-decode-status)))
1968 (setq status (delq (member name status) status))
1969 (when value
1970 (push (cons name value) status))
1971 (message-remove-header "status")
1972 (goto-char (point-max))
1973 (insert "Status: " (nnmail-encode-status status) "\n"))))
1975 (defun nnmail-decode-status ()
1976 "Return a status-value alist from STATUS."
1977 (goto-char (point-min))
1978 (when (re-search-forward "^Status: " nil t)
1979 (let (name value status)
1980 (save-restriction
1981 ;; Narrow to the status.
1982 (narrow-to-region
1983 (point)
1984 (if (re-search-forward "^[^ \t]" nil t)
1985 (1- (point))
1986 (point-max)))
1987 ;; Go through all elements and add them to the list.
1988 (goto-char (point-min))
1989 (while (re-search-forward "[^ \t=]+" nil t)
1990 (setq name (match-string 0))
1991 (if (not (eq (char-after) ?=))
1992 ;; Implied "yes".
1993 (setq value "yes")
1994 (forward-char 1)
1995 (if (not (eq (char-after) ?\"))
1996 (if (not (looking-at "[^ \t]"))
1997 ;; Implied "no".
1998 (setq value "no")
1999 ;; Unquoted value.
2000 (setq value (match-string 0))
2001 (goto-char (match-end 0)))
2002 ;; Quoted value.
2003 (setq value (read (current-buffer)))))
2004 (push (cons name value) status)))
2005 status)))
2007 (defun nnmail-encode-status (status)
2008 "Return a status string from STATUS."
2009 (mapconcat
2010 (lambda (elem)
2011 (concat
2012 (car elem) "="
2013 (if (string-match "[ \t]" (cdr elem))
2014 (prin1-to-string (cdr elem))
2015 (cdr elem))))
2016 status " "))
2018 (defun nnmail-split-history ()
2019 "Generate an overview of where the last mail split put articles."
2020 (interactive)
2021 (unless nnmail-split-history
2022 (error "No current split history"))
2023 (with-output-to-temp-buffer "*nnmail split history*"
2024 (with-current-buffer standard-output
2025 (fundamental-mode))
2026 (dolist (elem nnmail-split-history)
2027 (princ (mapconcat (lambda (ga)
2028 (concat (car ga) ":" (int-to-string (cdr ga))))
2029 elem
2030 ", "))
2031 (princ "\n"))))
2033 (defun nnmail-purge-split-history (group)
2034 "Remove all instances of GROUP from `nnmail-split-history'."
2035 (let ((history nnmail-split-history))
2036 (while history
2037 (setcar history (gnus-remove-if (lambda (e) (string= (car e) group))
2038 (car history)))
2039 (pop history))
2040 (setq nnmail-split-history (delq nil nnmail-split-history))))
2042 (defun nnmail-new-mail-p (group)
2043 "Say whether GROUP has new mail."
2044 (let ((his nnmail-split-history)
2045 found)
2046 (while his
2047 (when (assoc group (pop his))
2048 (setq found t
2049 his nil)))
2050 found))
2052 (defun nnmail-within-headers-p ()
2053 "Check to see if point is within the headers of a unix mail message.
2054 Doesn't change point."
2055 (let ((pos (point)))
2056 (save-excursion
2057 (and (nnmail-search-unix-mail-delim-backward)
2058 (not (search-forward "\n\n" pos t))))))
2060 (run-hooks 'nnmail-load-hook)
2062 (provide 'nnmail)
2064 ;;; nnmail.el ends here