Added gud-speedbar-buttons, and support for GDB buttons.
[emacs.git] / lisp / nnmail.el
blobd108d590dad5f4780ce2a9042426e97955a8f0ed
1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news, mail
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Commentary:
26 ;;; Code:
28 (require 'nnheader)
29 (require 'timezone)
30 (require 'message)
31 (eval-when-compile (require 'cl))
33 (defvar nnmail-split-methods
34 '(("mail.misc" ""))
35 "*Incoming mail will be split according to this variable.
37 If you'd like, for instance, one mail group for mail from the
38 \"4ad-l\" mailing list, one group for junk mail and one for everything
39 else, you could do something like this:
41 (setq nnmail-split-methods
42 '((\"mail.4ad\" \"From:.*4ad\")
43 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
44 (\"mail.misc\" \"\")))
46 As you can see, this variable is a list of lists, where the first
47 element in each \"rule\" is the name of the group (which, by the way,
48 does not have to be called anything beginning with \"mail\",
49 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
50 nnmail will try to match on the header to find a fit.
52 The second element can also be a function. In that case, it will be
53 called narrowed to the headers with the first element of the rule as
54 the argument. It should return a non-nil value if it thinks that the
55 mail belongs in that group.
57 The last element should always have \"\" as the regexp.
59 This variable can also have a function as its value.")
61 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
62 (defvar nnmail-crosspost t
63 "*If non-nil, do crossposting if several split methods match the mail.
64 If nil, the first match found will be used.")
66 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
67 (defvar nnmail-keep-last-article nil
68 "*If non-nil, nnmail will never delete the last expired article in a directory.
69 You may need to set this variable if other programs are putting
70 new mail into folder numbers that Gnus has marked as expired.")
72 (defvar nnmail-use-long-file-names nil
73 "*If non-nil the mail backends will use long file and directory names.
74 If nil, groups like \"mail.misc\" will end up in directories like
75 \"mail/misc/\".")
77 (defvar nnmail-expiry-wait 7
78 "*Expirable articles that are older than this will be expired.
79 This variable can either be a number (which will be interpreted as a
80 number of days) -- this doesn't have to be an integer. This variable
81 can also be `immediate' and `never'.")
83 (defvar nnmail-expiry-wait-function nil
84 "*Variable that holds function to specify how old articles should be before they are expired.
85 The function will be called with the name of the group that the
86 expiry is to be performed in, and it should return an integer that
87 says how many days an article can be stored before it is considered
88 \"old\". It can also return the values `never' and `immediate'.
90 Eg.:
92 (setq nnmail-expiry-wait-function
93 (lambda (newsgroup)
94 (cond ((string-match \"private\" newsgroup) 31)
95 ((string-match \"junk\" newsgroup) 1)
96 ((string-match \"important\" newsgroup) 'never)
97 (t 7))))")
99 (defvar nnmail-spool-file
100 (or (getenv "MAIL")
101 (concat "/usr/spool/mail/" (user-login-name)))
102 "Where the mail backends will look for incoming mail.
103 This variable is \"/usr/spool/mail/$user\" by default.
104 If this variable is nil, no mail backends will read incoming mail.
105 If this variable is a list, all files mentioned in this list will be
106 used as incoming mailboxes.")
108 (defvar nnmail-crash-box "~/.gnus-crash-box"
109 "*File where Gnus will store mail while processing it.")
111 (defvar nnmail-use-procmail nil
112 "*If non-nil, the mail backends will look in `nnmail-procmail-directory' for spool files.
113 The file(s) in `nnmail-spool-file' will also be read.")
115 (defvar nnmail-procmail-directory "~/incoming/"
116 "*When using procmail (and the like), incoming mail is put in this directory.
117 The Gnus mail backends will read the mail from this directory.")
119 (defvar nnmail-procmail-suffix "\\.spool"
120 "*Suffix of files created by procmail (and the like).
121 This variable might be a suffix-regexp to match the suffixes of
122 several files - eg. \".spool[0-9]*\".")
124 (defvar nnmail-resplit-incoming nil
125 "*If non-nil, re-split incoming procmail sorted mail.")
127 (defvar nnmail-delete-file-function 'delete-file
128 "Function called to delete files in some mail backends.")
130 (defvar nnmail-crosspost-link-function 'add-name-to-file
131 "Function called to create a copy of a file.
132 This is `add-name-to-file' by default, which means that crossposts
133 will use hard links. If your file system doesn't allow hard
134 links, you could set this variable to `copy-file' instead.")
136 (defvar nnmail-movemail-program "movemail"
137 "*A command to be executed to move mail from the inbox.
138 The default is \"movemail\".")
140 (defvar nnmail-pop-password-required nil
141 "*Non-nil if a password is required when reading mail using POP.")
143 (defvar nnmail-read-incoming-hook nil
144 "*Hook that will be run after the incoming mail has been transferred.
145 The incoming mail is moved from `nnmail-spool-file' (which normally is
146 something like \"/usr/spool/mail/$user\") to the user's home
147 directory. This hook is called after the incoming mail box has been
148 emptied, and can be used to call any mail box programs you have
149 running (\"xwatch\", etc.)
153 \(add-hook 'nnmail-read-incoming-hook
154 (lambda ()
155 (start-process \"mailsend\" nil
156 \"/local/bin/mailsend\" \"read\" \"mbox\")))
158 If you have xwatch running, this will alert it that mail has been
159 read.
161 If you use `display-time', you could use something like this:
163 \(add-hook 'nnmail-read-incoming-hook
164 (lambda ()
165 ;; Update the displayed time, since that will clear out
166 ;; the flag that says you have mail.
167 (if (eq (process-status \"display-time\") 'run)
168 (display-time-filter display-time-process \"\"))))")
170 (when (eq system-type 'windows-nt)
171 (add-hook 'nnmail-prepare-incoming-hook 'nnheader-ms-strip-cr))
173 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
174 (defvar nnmail-prepare-incoming-hook nil
175 "*Hook called before treating incoming mail.
176 The hook is run in a buffer with all the new, incoming mail.")
178 (defvar nnmail-pre-get-new-mail-hook nil
179 "Hook called just before starting to handle new incoming mail.")
181 (defvar nnmail-post-get-new-mail-hook nil
182 "Hook called just after finishing handling new incoming mail.")
184 ;; Suggested by Mejia Pablo J <pjm9806@usl.edu>.
185 (defvar nnmail-tmp-directory nil
186 "*If non-nil, use this directory for temporary storage when reading incoming mail.")
188 (defvar nnmail-large-newsgroup 50
189 "*The number of the articles which indicates a large newsgroup.
190 If the number of the articles is greater than the value, verbose
191 messages will be shown to indicate the current status.")
193 (defvar nnmail-split-fancy "mail.misc"
194 "*Incoming mail can be split according to this fancy variable.
195 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
197 The format is this variable is SPLIT, where SPLIT can be one of
198 the following:
200 GROUP: Mail will be stored in GROUP (a string).
202 \(FIELD VALUE SPLIT): If the message field FIELD (a regexp) contains
203 VALUE (a regexp), store the messages as specified by SPLIT.
205 \(| SPLIT...): Process each SPLIT expression until one of them matches.
206 A SPLIT expression is said to match if it will cause the mail
207 message to be stored in one or more groups.
209 \(& SPLIT...): Process each SPLIT expression.
211 FIELD must match a complete field name. VALUE must match a complete
212 word according to the `nnmail-split-fancy-syntax-table' syntax table.
213 You can use .* in the regexps to match partial field names or words.
215 FIELD and VALUE can also be lisp symbols, in that case they are expanded
216 as specified in `nnmail-split-abbrev-alist'.
218 Example:
220 \(setq nnmail-split-methods 'nnmail-split-fancy
221 nnmail-split-fancy
222 ;; Messages from the mailer deamon are not crossposted to any of
223 ;; the ordinary groups. Warnings are put in a separate group
224 ;; from real errors.
225 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
226 \"mail.misc\"))
227 ;; Non-error messages are crossposted to all relevant
228 ;; groups, but we don't crosspost between the group for the
229 ;; (ding) list and the group for other (ding) related mail.
230 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
231 (\"subject\" \"ding\" \"ding.misc\"))
232 ;; Other mailing lists...
233 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
234 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
235 ;; People...
236 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
237 ;; Unmatched mail goes to the catch all group.
238 \"misc.misc\"))")
240 (defvar nnmail-split-abbrev-alist
241 '((any . "from\\|to\\|cc\\|sender\\|apparently-to")
242 (mail . "mailer-daemon\\|postmaster"))
243 "*Alist of abbreviations allowed in `nnmail-split-fancy'.")
245 (defvar nnmail-delete-incoming t
246 "*If non-nil, the mail backends will delete incoming files after splitting.")
248 (defvar nnmail-message-id-cache-length 1000
249 "*The approximate number of Message-IDs nnmail will keep in its cache.
250 If this variable is nil, no checking on duplicate messages will be
251 performed.")
253 (defvar nnmail-message-id-cache-file "~/.nnmail-cache"
254 "*The file name of the nnmail Message-ID cache.")
256 (defvar nnmail-treat-duplicates 'warn
257 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
258 Three values are legal: nil, which means that nnmail is not to keep a
259 Message-ID cache; `warn', which means that nnmail should insert extra
260 headers to warn the user about the duplication (this is the default);
261 and `delete', which means that nnmail will delete duplicated mails.
263 This variable can also be a function. It will be called from a buffer
264 narrowed to the article in question with the Message-ID as a
265 parameter. It should return nil, `warn' or `delete'.")
267 ;;; Internal variables.
269 (defvar nnmail-pop-password nil
270 "*Password to use when reading mail from a POP server, if required.")
272 (defvar nnmail-split-fancy-syntax-table
273 (copy-syntax-table (standard-syntax-table))
274 "Syntax table used by `nnmail-split-fancy'.")
276 (defvar nnmail-prepare-save-mail-hook nil
277 "Hook called before saving mail.")
279 (defvar nnmail-moved-inboxes nil
280 "List of inboxes that have been moved.")
282 (defvar nnmail-internal-password nil)
286 (defconst nnmail-version "nnmail 1.0"
287 "nnmail version.")
291 (defun nnmail-request-post (&optional server)
292 (mail-send-and-exit nil))
294 (defun nnmail-find-file (file)
295 "Insert FILE in server buffer safely."
296 (set-buffer nntp-server-buffer)
297 (erase-buffer)
298 (let ((format-alist nil)
299 (after-insert-file-functions nil))
300 (condition-case ()
301 (progn (insert-file-contents file) t)
302 (file-error nil))))
304 (defun nnmail-group-pathname (group dir &optional file)
305 "Make pathname for GROUP."
306 (concat
307 (let ((dir (file-name-as-directory (expand-file-name dir))))
308 ;; If this directory exists, we use it directly.
309 (if (or nnmail-use-long-file-names
310 (file-directory-p (concat dir group)))
311 (concat dir group "/")
312 ;; If not, we translate dots into slashes.
313 (concat dir (nnheader-replace-chars-in-string group ?. ?/) "/")))
314 (or file "")))
316 (defun nnmail-date-to-time (date)
317 "Convert DATE into time."
318 (let* ((d1 (timezone-parse-date date))
319 (t1 (timezone-parse-time (aref d1 3))))
320 (apply 'encode-time
321 (mapcar (lambda (el)
322 (and el (string-to-number el)))
323 (list
324 (aref t1 2) (aref t1 1) (aref t1 0)
325 (aref d1 2) (aref d1 1) (aref d1 0)
326 (aref d1 4))))))
328 (defun nnmail-time-less (t1 t2)
329 "Say whether time T1 is less than time T2."
330 (or (< (car t1) (car t2))
331 (and (= (car t1) (car t2))
332 (< (nth 1 t1) (nth 1 t2)))))
334 (defun nnmail-days-to-time (days)
335 "Convert DAYS into time."
336 (let* ((seconds (* 1.0 days 60 60 24))
337 (rest (expt 2 16))
338 (ms (condition-case nil (round (/ seconds rest))
339 (range-error (expt 2 16)))))
340 (list ms (condition-case nil (round (- seconds (* ms rest)))
341 (range-error (expt 2 16))))))
343 (defun nnmail-time-since (time)
344 "Return the time since TIME, which is either an internal time or a date."
345 (when (stringp time)
346 ;; Convert date strings to internal time.
347 (setq time (nnmail-date-to-time time)))
348 (let* ((current (current-time))
349 (rest (if (< (nth 1 current) (nth 1 time)) (expt 2 16))))
350 (list (- (+ (car current) (if rest -1 0)) (car time))
351 (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
353 ;; Function rewritten from rmail.el.
354 (defun nnmail-move-inbox (inbox)
355 "Move INBOX to `nnmail-crash-box'."
356 (let ((inbox (file-truename
357 (expand-file-name (substitute-in-file-name inbox))))
358 (tofile (file-truename (expand-file-name
359 (substitute-in-file-name nnmail-crash-box))))
360 movemail popmail errors password)
361 ;; If getting from mail spool directory,
362 ;; use movemail to move rather than just renaming,
363 ;; so as to interlock with the mailer.
364 (unless (setq popmail (string-match "^po:" (file-name-nondirectory inbox)))
365 (setq movemail t))
366 (when popmail
367 (setq inbox (file-name-nondirectory inbox)))
368 (when (and movemail
369 ;; On some systems, /usr/spool/mail/foo is a directory
370 ;; and the actual inbox is /usr/spool/mail/foo/foo.
371 (file-directory-p inbox))
372 (setq inbox (expand-file-name (user-login-name) inbox)))
373 (if (member inbox nnmail-moved-inboxes)
375 (if popmail
376 (progn
377 (setq nnmail-internal-password nnmail-pop-password)
378 (when (and nnmail-pop-password-required (not nnmail-pop-password))
379 (setq nnmail-internal-password
380 (nnmail-read-passwd
381 (format "Password for %s: "
382 (substring inbox (+ popmail 3))))))
383 (message "Getting mail from post office ..."))
384 (when (or (and (file-exists-p tofile)
385 (/= 0 (nnheader-file-size tofile)))
386 (and (file-exists-p inbox)
387 (/= 0 (nnheader-file-size inbox))))
388 (message "Getting mail from %s..." inbox)))
389 ;; Set TOFILE if have not already done so, and
390 ;; rename or copy the file INBOX to TOFILE if and as appropriate.
391 (cond
392 ((file-exists-p tofile)
393 ;; The crash box exists already.
395 ((and (not popmail)
396 (not (file-exists-p inbox)))
397 ;; There is no inbox.
398 (setq tofile nil))
399 ((and (not movemail) (not popmail))
400 ;; Try copying. If that fails (perhaps no space),
401 ;; rename instead.
402 (condition-case nil
403 (copy-file inbox tofile nil)
404 (error
405 ;; Third arg is t so we can replace existing file TOFILE.
406 (rename-file inbox tofile t)))
407 (push inbox nnmail-moved-inboxes)
408 ;; Make the real inbox file empty.
409 ;; Leaving it deleted could cause lossage
410 ;; because mailers often won't create the file.
411 (condition-case ()
412 (write-region (point) (point) inbox)
413 (file-error nil)))
415 ;; Use movemail.
416 (unwind-protect
417 (save-excursion
418 (setq errors (generate-new-buffer " *nnmail loss*"))
419 (buffer-disable-undo errors)
420 (let ((default-directory "/"))
421 (apply
422 'call-process
423 (append
424 (list
425 (expand-file-name nnmail-movemail-program exec-directory)
426 nil errors nil inbox tofile)
427 (when nnmail-internal-password
428 (list nnmail-internal-password)))))
429 (if (not (buffer-modified-p errors))
430 ;; No output => movemail won
431 (push inbox nnmail-moved-inboxes)
432 (set-buffer errors)
433 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
434 (goto-char (point-max))
435 (skip-chars-backward " \t")
436 (delete-region (point) (point-max))
437 (goto-char (point-min))
438 (if (looking-at "movemail: ")
439 (delete-region (point-min) (match-end 0)))
440 (beep t)
441 (message (concat "movemail: "
442 (buffer-substring (point-min)
443 (point-max))))
444 (sit-for 3)
445 (setq tofile nil))))))
446 (and errors
447 (buffer-name errors)
448 (kill-buffer errors))
449 tofile)))
451 (defun nnmail-get-active ()
452 "Returns an assoc of group names and active ranges.
453 nn*-request-list should have been called before calling this function."
454 (let (group-assoc)
455 ;; Go through all groups from the active list.
456 (save-excursion
457 (set-buffer nntp-server-buffer)
458 (goto-char (point-min))
459 (while (re-search-forward
460 "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)" nil t)
461 ;; We create an alist with `(GROUP (LOW . HIGH))' elements.
462 (push (list (match-string 1)
463 (cons (string-to-int (match-string 3))
464 (string-to-int (match-string 2))))
465 group-assoc)))
466 group-assoc))
468 (defun nnmail-save-active (group-assoc file-name)
469 "Save GROUP-ASSOC in ACTIVE-FILE."
470 (when file-name
471 (let (group)
472 (save-excursion
473 (set-buffer (get-buffer-create " *nnmail active*"))
474 (buffer-disable-undo (current-buffer))
475 (erase-buffer)
476 (while group-assoc
477 (setq group (pop group-assoc))
478 (insert (format "%s %d %d y\n" (car group) (cdadr group)
479 (caadr group))))
480 (unless (file-exists-p (file-name-directory file-name))
481 (make-directory (file-name-directory file-name) t))
482 (write-region 1 (point-max) (expand-file-name file-name) nil 'nomesg)
483 (kill-buffer (current-buffer))))))
485 (defun nnmail-get-split-group (file group)
486 (if (or (eq nnmail-spool-file 'procmail)
487 nnmail-use-procmail)
488 (cond (group group)
489 ((string-match (concat "^" (expand-file-name
490 (file-name-as-directory
491 nnmail-procmail-directory))
492 "\\([^/]*\\)" nnmail-procmail-suffix "$")
493 (expand-file-name file))
494 (substring (expand-file-name file)
495 (match-beginning 1) (match-end 1)))
497 group))
498 group))
500 (defun nnmail-process-babyl-mail-format (func)
501 (let ((case-fold-search t)
502 start message-id content-length do-search end)
503 (while (not (eobp))
504 (goto-char (point-min))
505 (re-search-forward
506 "\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
507 (goto-char (match-end 0))
508 (delete-region (match-beginning 0) (match-end 0))
509 (setq start (point))
510 ;; Skip all the headers in case there are more "From "s...
511 (or (search-forward "\n\n" nil t)
512 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
513 (search-forward "\x1f\f"))
514 ;; Find the Message-ID header.
515 (save-excursion
516 (if (re-search-backward "^Message-ID:[ \t]*\\(<[^>]*>\\)" nil t)
517 (setq message-id (buffer-substring (match-beginning 1)
518 (match-end 1)))
519 ;; There is no Message-ID here, so we create one.
520 (save-excursion
521 (when (re-search-backward "^Message-ID:" nil t)
522 (beginning-of-line)
523 (insert "Original-")))
524 (forward-line -1)
525 (insert "Message-ID: " (setq message-id (nnmail-message-id))
526 "\n")))
527 ;; Look for a Content-Length header.
528 (if (not (save-excursion
529 (and (re-search-backward
530 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
531 (setq content-length (string-to-int
532 (buffer-substring
533 (match-beginning 1)
534 (match-end 1))))
535 ;; We destroy the header, since none of
536 ;; the backends ever use it, and we do not
537 ;; want to confuse other mailers by having
538 ;; a (possibly) faulty header.
539 (progn (insert "X-") t))))
540 (setq do-search t)
541 (if (or (= (+ (point) content-length) (point-max))
542 (save-excursion
543 (goto-char (+ (point) content-length))
544 (looking-at "\x1f")))
545 (progn
546 (goto-char (+ (point) content-length))
547 (setq do-search nil))
548 (setq do-search t)))
549 ;; Go to the beginning of the next article - or to the end
550 ;; of the buffer.
551 (if do-search
552 (if (re-search-forward "^\x1f" nil t)
553 (goto-char (match-beginning 0))
554 (goto-char (1- (point-max)))))
555 (delete-char 1) ; delete ^_
556 (save-excursion
557 (save-restriction
558 (narrow-to-region start (point))
559 (goto-char (point-min))
560 (nnmail-check-duplication message-id func)
561 (setq end (point-max))))
562 (goto-char end))))
564 (defun nnmail-search-unix-mail-delim ()
565 "Put point at the beginning of the next message."
566 (let ((case-fold-search t)
567 (delim (concat "^" message-unix-mail-delimiter))
568 found)
569 (while (not found)
570 (if (re-search-forward delim nil t)
571 (when (or (looking-at "[^\n :]+ *:")
572 (looking-at delim)
573 (looking-at (concat ">" message-unix-mail-delimiter)))
574 (forward-line -1)
575 (setq found 'yes))
576 (setq found 'no)))
577 (eq found 'yes)))
579 (defun nnmail-process-unix-mail-format (func)
580 (let ((case-fold-search t)
581 (delim (concat "^" message-unix-mail-delimiter))
582 start message-id content-length end skip head-end)
583 (goto-char (point-min))
584 (if (not (and (re-search-forward delim nil t)
585 (goto-char (match-beginning 0))))
586 ;; Possibly wrong format?
587 (error "Error, unknown mail format! (Possibly corrupted.)")
588 ;; Carry on until the bitter end.
589 (while (not (eobp))
590 (setq start (point)
591 end nil)
592 ;; Find the end of the head.
593 (narrow-to-region
594 start
595 (if (search-forward "\n\n" nil t)
596 (1- (point))
597 ;; This will never happen, but just to be on the safe side --
598 ;; if there is no head-body delimiter, we search a bit manually.
599 (while (and (looking-at "From \\|[^ \t]+:")
600 (not (eobp)))
601 (forward-line 1)
602 (point))))
603 ;; Find the Message-ID header.
604 (goto-char (point-min))
605 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
606 (setq message-id (match-string 1))
607 (save-excursion
608 (when (re-search-forward "^Message-ID:" nil t)
609 (beginning-of-line)
610 (insert "Original-")))
611 ;; There is no Message-ID here, so we create one.
612 (forward-line 1)
613 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
614 ;; Look for a Content-Length header.
615 (goto-char (point-min))
616 (if (not (re-search-forward
617 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
618 (setq content-length nil)
619 (setq content-length (string-to-int (match-string 1)))
620 ;; We destroy the header, since none of the backends ever
621 ;; use it, and we do not want to confuse other mailers by
622 ;; having a (possibly) faulty header.
623 (beginning-of-line)
624 (insert "X-"))
625 ;; Find the end of this article.
626 (goto-char (point-max))
627 (widen)
628 (setq head-end (point))
629 ;; We try the Content-Length value. The idea: skip over the header
630 ;; separator, then check what happens content-length bytes into the
631 ;; message body. This should be either the end ot the buffer, the
632 ;; message separator or a blank line followed by the separator.
633 ;; The blank line should probably be deleted. If neither of the
634 ;; three is met, the content-length header is probably invalid.
635 (when content-length
636 (forward-line 1)
637 (setq skip (+ (point) content-length))
638 (goto-char skip)
639 (cond ((or (= skip (point-max))
640 (= (1+ skip) (point-max)))
641 (setq end (point-max)))
642 ((looking-at delim)
643 (setq end skip))
644 ((looking-at
645 (concat "[ \t]*\n\\(" delim "\\)"))
646 (setq end (match-beginning 1)))
647 (t (setq end nil))))
648 (if end
649 (goto-char end)
650 ;; No Content-Length, so we find the beginning of the next
651 ;; article or the end of the buffer.
652 (goto-char head-end)
653 (or (nnmail-search-unix-mail-delim)
654 (goto-char (point-max))))
655 ;; Allow the backend to save the article.
656 (save-excursion
657 (save-restriction
658 (narrow-to-region start (point))
659 (goto-char (point-min))
660 (nnmail-check-duplication message-id func)
661 (setq end (point-max))))
662 (goto-char end)))))
664 (defun nnmail-process-mmdf-mail-format (func)
665 (let ((delim "^\^A\^A\^A\^A$")
666 (case-fold-search t)
667 start message-id end)
668 (goto-char (point-min))
669 (if (not (and (re-search-forward delim nil t)
670 (forward-line 1)))
671 ;; Possibly wrong format?
672 (error "Error, unknown mail format! (Possibly corrupted.)")
673 ;; Carry on until the bitter end.
674 (while (not (eobp))
675 (setq start (point))
676 ;; Find the end of the head.
677 (narrow-to-region
678 start
679 (if (search-forward "\n\n" nil t)
680 (1- (point))
681 ;; This will never happen, but just to be on the safe side --
682 ;; if there is no head-body delimiter, we search a bit manually.
683 (while (and (looking-at "From \\|[^ \t]+:")
684 (not (eobp)))
685 (forward-line 1)
686 (point))))
687 ;; Find the Message-ID header.
688 (goto-char (point-min))
689 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
690 (setq message-id (match-string 1))
691 ;; There is no Message-ID here, so we create one.
692 (save-excursion
693 (when (re-search-backward "^Message-ID:" nil t)
694 (beginning-of-line)
695 (insert "Original-")))
696 (forward-line 1)
697 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
698 ;; Find the end of this article.
699 (goto-char (point-max))
700 (widen)
701 (if (re-search-forward delim nil t)
702 (beginning-of-line)
703 (goto-char (point-max)))
704 ;; Allow the backend to save the article.
705 (save-excursion
706 (save-restriction
707 (narrow-to-region start (point))
708 (goto-char (point-min))
709 (nnmail-check-duplication message-id func)
710 (setq end (point-max))))
711 (goto-char end)
712 (forward-line 2)))))
714 (defun nnmail-split-incoming (incoming func &optional exit-func group)
715 "Go through the entire INCOMING file and pick out each individual mail.
716 FUNC will be called with the buffer narrowed to each mail."
717 (let (;; If this is a group-specific split, we bind the split
718 ;; methods to just this group.
719 (nnmail-split-methods (if (and group
720 (or (eq nnmail-spool-file 'procmail)
721 nnmail-use-procmail)
722 (not nnmail-resplit-incoming))
723 (list (list group ""))
724 nnmail-split-methods)))
725 (save-excursion
726 ;; Insert the incoming file.
727 (set-buffer (get-buffer-create " *nnmail incoming*"))
728 (buffer-disable-undo (current-buffer))
729 (erase-buffer)
730 (nnheader-insert-file-contents-literally incoming)
731 (unless (zerop (buffer-size))
732 (goto-char (point-min))
733 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
734 ;; Handle both babyl, MMDF and unix mail formats, since movemail will
735 ;; use the former when fetching from a mailbox, the latter when
736 ;; fetches from a file.
737 (cond ((or (looking-at "\^L")
738 (looking-at "BABYL OPTIONS:"))
739 (nnmail-process-babyl-mail-format func))
740 ((looking-at "\^A\^A\^A\^A")
741 (nnmail-process-mmdf-mail-format func))
743 (nnmail-process-unix-mail-format func))))
744 (if exit-func (funcall exit-func))
745 (kill-buffer (current-buffer)))))
747 ;; Mail crossposts suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
748 (defun nnmail-article-group (func)
749 "Look at the headers and return an alist of groups that match.
750 FUNC will be called with the group name to determine the article number."
751 (let ((methods nnmail-split-methods)
752 (obuf (current-buffer))
753 (beg (point-min))
754 end group-art method)
755 (if (and (sequencep methods) (= (length methods) 1))
756 ;; If there is only just one group to put everything in, we
757 ;; just return a list with just this one method in.
758 (setq group-art
759 (list (cons (caar methods) (funcall func (caar methods)))))
760 ;; We do actual comparison.
761 (save-excursion
762 ;; Find headers.
763 (goto-char beg)
764 (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
765 (set-buffer nntp-server-buffer)
766 (erase-buffer)
767 ;; Copy the headers into the work buffer.
768 (insert-buffer-substring obuf beg end)
769 ;; Fold continuation lines.
770 (goto-char (point-min))
771 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
772 (replace-match " " t t))
773 (if (and (symbolp nnmail-split-methods)
774 (fboundp nnmail-split-methods))
775 ;; `nnmail-split-methods' is a function, so we just call
776 ;; this function here and use the result.
777 (setq group-art
778 (mapcar
779 (lambda (group) (cons group (funcall func group)))
780 (condition-case nil
781 (or (funcall nnmail-split-methods)
782 '("bogus"))
783 (error
784 (message
785 "Error in `nnmail-split-methods'; using `bogus' mail group")
786 (sit-for 1)
787 '("bogus")))))
788 ;; Go through the split methods to find a match.
789 (while (and methods (or nnmail-crosspost (not group-art)))
790 (goto-char (point-max))
791 (setq method (pop methods))
792 (if (or methods
793 (not (equal "" (nth 1 method))))
794 (when (and
795 (condition-case ()
796 (if (stringp (nth 1 method))
797 (re-search-backward (cadr method) nil t)
798 ;; Function to say whether this is a match.
799 (funcall (nth 1 method) (car method)))
800 (error nil))
801 ;; Don't enter the article into the same
802 ;; group twice.
803 (not (assoc (car method) group-art)))
804 (push (cons (car method) (funcall func (car method)))
805 group-art))
806 ;; This is the final group, which is used as a
807 ;; catch-all.
808 (unless group-art
809 (setq group-art
810 (list (cons (car method)
811 (funcall func (car method)))))))))
812 group-art))))
814 (defun nnmail-insert-lines ()
815 "Insert how many lines there are in the body of the mail.
816 Return the number of characters in the body."
817 (let (lines chars)
818 (save-excursion
819 (goto-char (point-min))
820 (when (search-forward "\n\n" nil t)
821 (setq chars (- (point-max) (point)))
822 (setq lines (count-lines (point) (point-max)))
823 (forward-char -1)
824 (save-excursion
825 (when (re-search-backward "^Lines: " nil t)
826 (delete-region (point) (progn (forward-line 1) (point)))))
827 (beginning-of-line)
828 (insert (format "Lines: %d\n" (max lines 0)))
829 chars))))
831 (defun nnmail-insert-xref (group-alist)
832 "Insert an Xref line based on the (group . article) alist."
833 (save-excursion
834 (goto-char (point-min))
835 (when (search-forward "\n\n" nil t)
836 (forward-char -1)
837 (when (re-search-backward "^Xref: " nil t)
838 (delete-region (match-beginning 0)
839 (progn (forward-line 1) (point))))
840 (insert (format "Xref: %s" (system-name)))
841 (while group-alist
842 (insert (format " %s:%d" (caar group-alist) (cdar group-alist)))
843 (setq group-alist (cdr group-alist)))
844 (insert "\n"))))
846 ;; Written by byer@mv.us.adobe.com (Scott Byer).
847 (defun nnmail-make-complex-temp-name (prefix)
848 (let ((newname (make-temp-name prefix))
849 (newprefix prefix))
850 (while (file-exists-p newname)
851 (setq newprefix (concat newprefix "x"))
852 (setq newname (make-temp-name newprefix)))
853 newname))
855 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
857 (defun nnmail-split-fancy ()
858 "Fancy splitting method.
859 See the documentation for the variable `nnmail-split-fancy' for documentation."
860 (let ((syntab (syntax-table)))
861 (unwind-protect
862 (progn
863 (set-syntax-table nnmail-split-fancy-syntax-table)
864 (nnmail-split-it nnmail-split-fancy))
865 (set-syntax-table syntab))))
867 (defvar nnmail-split-cache nil)
868 ;; Alist of split expressions their equivalent regexps.
870 (defun nnmail-split-it (split)
871 ;; Return a list of groups matching SPLIT.
872 (cond ((stringp split)
873 ;; A group.
874 (list split))
875 ((eq (car split) '&)
876 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
877 ((eq (car split) '|)
878 (let (done)
879 (while (and (not done) (cdr split))
880 (setq split (cdr split)
881 done (nnmail-split-it (car split))))
882 done))
883 ((assq split nnmail-split-cache)
884 ;; A compiled match expression.
885 (goto-char (point-max))
886 (if (re-search-backward (cdr (assq split nnmail-split-cache)) nil t)
887 (nnmail-split-it (nth 2 split))))
889 ;; An uncompiled match.
890 (let* ((field (nth 0 split))
891 (value (nth 1 split))
892 (regexp (concat "^\\("
893 (if (symbolp field)
894 (cdr (assq field
895 nnmail-split-abbrev-alist))
896 field)
897 "\\):.*\\<\\("
898 (if (symbolp value)
899 (cdr (assq value
900 nnmail-split-abbrev-alist))
901 value)
902 "\\)\\>")))
903 (setq nnmail-split-cache
904 (cons (cons split regexp) nnmail-split-cache))
905 (goto-char (point-max))
906 (if (re-search-backward regexp nil t)
907 (nnmail-split-it (nth 2 split)))))))
909 ;; Get a list of spool files to read.
910 (defun nnmail-get-spool-files (&optional group)
911 (if (null nnmail-spool-file)
912 ;; No spool file whatsoever.
914 (let* ((procmails
915 ;; If procmail is used to get incoming mail, the files
916 ;; are stored in this directory.
917 (and (file-exists-p nnmail-procmail-directory)
918 (or (eq nnmail-spool-file 'procmail)
919 nnmail-use-procmail)
920 (directory-files
921 nnmail-procmail-directory
922 t (concat (if group (concat "^" group) "")
923 nnmail-procmail-suffix "$") t)))
924 (p procmails)
925 (crash (when (and (file-exists-p nnmail-crash-box)
926 (> (nnheader-file-size
927 (file-truename nnmail-crash-box)) 0))
928 (list nnmail-crash-box))))
929 ;; Remove any directories that inadvertantly match the procmail
930 ;; suffix, which might happen if the suffix is "".
931 (while p
932 (when (file-directory-p (car p))
933 (setq procmails (delete (car p) procmails)))
934 (setq p (cdr p)))
935 ;; Return the list of spools.
936 (append
937 crash
938 (cond ((and group
939 (or (eq nnmail-spool-file 'procmail)
940 nnmail-use-procmail)
941 procmails)
942 procmails)
943 ((and group
944 (eq nnmail-spool-file 'procmail))
945 nil)
946 ((listp nnmail-spool-file)
947 (append nnmail-spool-file procmails))
948 ((stringp nnmail-spool-file)
949 (cons nnmail-spool-file procmails))
950 ((eq nnmail-spool-file 'pop)
951 (cons (format "po:%s" (user-login-name)) procmails))
953 procmails))))))
955 ;; Activate a backend only if it isn't already activated.
956 ;; If FORCE, re-read the active file even if the backend is
957 ;; already activated.
958 (defun nnmail-activate (backend &optional force)
959 (let (file timestamp file-time)
960 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
961 force
962 (and (setq file (condition-case ()
963 (symbol-value (intern (format "%s-active-file"
964 backend)))
965 (error nil)))
966 (setq file-time (nth 5 (file-attributes file)))
967 (or (not
968 (setq timestamp
969 (condition-case ()
970 (symbol-value (intern
971 (format "%s-active-timestamp"
972 backend)))
973 (error 'none))))
974 (not (consp timestamp))
975 (equal timestamp '(0 0))
976 (> (nth 0 file-time) (nth 0 timestamp))
977 (and (= (nth 0 file-time) (nth 0 timestamp))
978 (> (nth 1 file-time) (nth 1 timestamp))))))
979 (save-excursion
980 (or (eq timestamp 'none)
981 (set (intern (format "%s-active-timestamp" backend))
982 (current-time)))
983 (funcall (intern (format "%s-request-list" backend)))
984 (set (intern (format "%s-group-alist" backend))
985 (nnmail-get-active))))
988 (defun nnmail-message-id ()
989 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
992 ;;; nnmail duplicate handling
995 (defvar nnmail-cache-buffer nil)
997 (defun nnmail-cache-open ()
998 (if (or (not nnmail-treat-duplicates)
999 (and nnmail-cache-buffer
1000 (buffer-name nnmail-cache-buffer)))
1001 () ; The buffer is open.
1002 (save-excursion
1003 (set-buffer
1004 (setq nnmail-cache-buffer
1005 (get-buffer-create " *nnmail message-id cache*")))
1006 (buffer-disable-undo (current-buffer))
1007 (and (file-exists-p nnmail-message-id-cache-file)
1008 (insert-file-contents nnmail-message-id-cache-file))
1009 (set-buffer-modified-p nil)
1010 (current-buffer))))
1012 (defun nnmail-cache-close ()
1013 (when (and nnmail-cache-buffer
1014 nnmail-treat-duplicates
1015 (buffer-name nnmail-cache-buffer)
1016 (buffer-modified-p nnmail-cache-buffer))
1017 (save-excursion
1018 (set-buffer nnmail-cache-buffer)
1019 ;; Weed out the excess number of Message-IDs.
1020 (goto-char (point-max))
1021 (and (search-backward "\n" nil t nnmail-message-id-cache-length)
1022 (progn
1023 (beginning-of-line)
1024 (delete-region (point-min) (point))))
1025 ;; Save the buffer.
1026 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1027 (make-directory (file-name-directory nnmail-message-id-cache-file)
1029 (write-region (point-min) (point-max)
1030 nnmail-message-id-cache-file nil 'silent)
1031 (set-buffer-modified-p nil)
1032 (setq nnmail-cache-buffer nil)
1033 ;;(kill-buffer (current-buffer))
1036 (defun nnmail-cache-insert (id)
1037 (when nnmail-treat-duplicates
1038 (save-excursion
1039 (set-buffer nnmail-cache-buffer)
1040 (goto-char (point-max))
1041 (insert id "\n"))))
1043 (defun nnmail-cache-id-exists-p (id)
1044 (when nnmail-treat-duplicates
1045 (save-excursion
1046 (set-buffer nnmail-cache-buffer)
1047 (goto-char (point-max))
1048 (search-backward id nil t))))
1050 (defun nnmail-check-duplication (message-id func)
1051 ;; If this is a duplicate message, then we do not save it.
1052 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1053 (action (when duplication
1054 (cond
1055 ((memq nnmail-treat-duplicates '(warn delete))
1056 nnmail-treat-duplicates)
1057 ((nnheader-functionp nnmail-treat-duplicates)
1058 (funcall nnmail-treat-duplicates message-id))
1060 nnmail-treat-duplicates)))))
1061 (cond
1062 ((not duplication)
1063 (nnmail-cache-insert message-id)
1064 (funcall func))
1065 ((eq action 'delete)
1066 (delete-region (point-min) (point-max)))
1067 ((eq action 'warn)
1068 ;; We insert a warning.
1069 (let ((case-fold-search t)
1070 (newid (nnmail-message-id)))
1071 (goto-char (point-min))
1072 (when (re-search-forward "^message-id:" nil t)
1073 (beginning-of-line)
1074 (insert "Original-"))
1075 (beginning-of-line)
1076 (insert
1077 "Message-ID: " newid "\n"
1078 "Gnus-Warning: This is a duplicate of message " message-id "\n")
1079 (nnmail-cache-insert newid)
1080 (funcall func)))
1082 (funcall func)))))
1084 ;;; Get new mail.
1086 (defun nnmail-get-value (&rest args)
1087 (let ((sym (intern (apply 'format args))))
1088 (when (boundp sym)
1089 (symbol-value sym))))
1091 (defun nnmail-get-new-mail (method exit-func temp
1092 &optional group spool-func)
1093 "Read new incoming mail."
1094 (let* ((spools (nnmail-get-spool-files group))
1095 (group-in group)
1096 incoming incomings spool)
1097 (when (and (nnmail-get-value "%s-get-new-mail" method)
1098 nnmail-spool-file)
1099 ;; We first activate all the groups.
1100 (nnmail-activate method)
1101 ;; Allow the user to hook.
1102 (run-hooks 'nnmail-pre-get-new-mail-hook)
1103 ;; Open the message-id cache.
1104 (nnmail-cache-open)
1105 ;; The we go through all the existing spool files and split the
1106 ;; mail from each.
1107 (while spools
1108 (setq spool (pop spools))
1109 ;; We read each spool file if either the spool is a POP-mail
1110 ;; spool, or the file exists. We can't check for the
1111 ;; existance of POPped mail.
1112 (when (or (string-match "^po:" spool)
1113 (and (file-exists-p spool)
1114 (> (nnheader-file-size (file-truename spool)) 0)))
1115 (nnheader-message 3 "%s: Reading incoming mail..." method)
1116 (when (and (nnmail-move-inbox spool)
1117 (file-exists-p nnmail-crash-box))
1118 ;; There is new mail. We first find out if all this mail
1119 ;; is supposed to go to some specific group.
1120 (setq group (nnmail-get-split-group spool group-in))
1121 ;; We split the mail
1122 (nnmail-split-incoming
1123 nnmail-crash-box (intern (format "%s-save-mail" method))
1124 spool-func group)
1125 ;; Check whether the inbox is to be moved to the special tmp dir.
1126 (setq incoming
1127 (nnmail-make-complex-temp-name
1128 (expand-file-name
1129 (if nnmail-tmp-directory
1130 (concat
1131 (file-name-as-directory nnmail-tmp-directory)
1132 (file-name-nondirectory (concat temp "Incoming")))
1133 (concat temp "Incoming")))))
1134 (rename-file nnmail-crash-box incoming t)
1135 (push incoming incomings))))
1136 ;; If we did indeed read any incoming spools, we save all info.
1137 (when incomings
1138 (nnmail-save-active
1139 (nnmail-get-value "%s-group-alist" method)
1140 (nnmail-get-value "%s-active-file" method))
1141 (when exit-func
1142 (funcall exit-func))
1143 (run-hooks 'nnmail-read-incoming-hook)
1144 (nnheader-message 3 "%s: Reading incoming mail...done" method))
1145 ;; Close the message-id cache.
1146 (nnmail-cache-close)
1147 ;; Allow the user to hook.
1148 (run-hooks 'nnmail-post-get-new-mail-hook)
1149 ;; Delete all the temporary files.
1150 (while incomings
1151 (setq incoming (pop incomings))
1152 (and nnmail-delete-incoming
1153 (file-exists-p incoming)
1154 (file-writable-p incoming)
1155 (delete-file incoming))))))
1157 (defun nnmail-expired-article-p (group time force &optional inhibit)
1158 "Say whether an article that is TIME old in GROUP should be expired."
1159 (if force
1161 (let ((days (or (and nnmail-expiry-wait-function
1162 (funcall nnmail-expiry-wait-function group))
1163 nnmail-expiry-wait)))
1164 (cond ((or (eq days 'never)
1165 (and (not force)
1166 inhibit))
1167 ;; This isn't an expirable group.
1168 nil)
1169 ((eq days 'immediate)
1170 ;; We expire all articles on sight.
1172 ((equal time '(0 0))
1173 ;; This is an ange-ftp group, and we don't have any dates.
1174 nil)
1175 ((numberp days)
1176 (setq days (nnmail-days-to-time days))
1177 ;; Compare the time with the current time.
1178 (nnmail-time-less days (nnmail-time-since time)))))))
1180 (defvar nnmail-read-passwd nil)
1181 (defun nnmail-read-passwd (prompt)
1182 (unless nnmail-read-passwd
1183 (if (load "passwd" t)
1184 (setq nnmail-read-passwd 'read-passwd)
1185 (autoload 'ange-ftp-read-passwd "ange-ftp")
1186 (setq nnmail-read-passwd 'ange-ftp-read-passwd)))
1187 (funcall nnmail-read-passwd prompt))
1189 (defun nnmail-check-syntax ()
1190 "Check (and modify) the syntax of the message in the current buffer."
1191 (save-restriction
1192 (message-narrow-to-head)
1193 (let ((case-fold-search t))
1194 (unless (re-search-forward "^Message-Id:" nil t)
1195 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1197 (run-hooks 'nnmail-load-hook)
1199 (provide 'nnmail)
1201 ;;; nnmail.el ends here