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