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