94c524e2d7ee32425ac1e8f078b321c0fc5a36e9
[elisp/gnus.git-] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-ems)
33 (require 'message)
34 (require 'gnus-art)
35
36 (defcustom gnus-post-method 'current
37   "*Preferred method for posting USENET news.
38
39 If this variable is `current', Gnus will use the \"current\" select
40 method when posting.  If it is nil (which is the default), Gnus will
41 use the native select method when posting.
42
43 This method will not be used in mail groups and the like, only in
44 \"real\" newsgroups.
45
46 If not nil nor `native', the value must be a valid method as discussed
47 in the documentation of `gnus-select-method'.  It can also be a list of
48 methods.  If that is the case, the user will be queried for what select
49 method to use when posting."
50   :group 'gnus-group-foreign
51   :type `(choice (const nil)
52                  (const current)
53                  (const native)
54                  (sexp :tag "Methods" ,gnus-select-method)))
55
56 (defvar gnus-outgoing-message-group nil
57   "*All outgoing messages will be put in this group.
58 If you want to store all your outgoing mail and articles in the group
59 \"nnml:archive\", you set this variable to that value.  This variable
60 can also be a list of group names.
61
62 If you want to have greater control over what group to put each
63 message in, you can set this variable to a function that checks the
64 current newsgroup name and then returns a suitable group name (or list
65 of names).")
66
67 (defvar gnus-mailing-list-groups nil
68   "*Regexp matching groups that are really mailing lists.
69 This is useful when you're reading a mailing list that has been
70 gatewayed to a newsgroup, and you want to followup to an article in
71 the group.")
72
73 (defvar gnus-add-to-list nil
74   "*If non-nil, add a `to-list' parameter automatically.")
75
76 (defvar gnus-crosspost-complaint
77   "Hi,
78
79 You posted the article below with the following Newsgroups header:
80
81 Newsgroups: %s
82
83 The %s group, at least, was an inappropriate recipient
84 of this message.  Please trim your Newsgroups header to exclude this
85 group before posting in the future.
86
87 Thank you.
88
89 "
90   "Format string to be inserted when complaining about crossposts.
91 The first %s will be replaced by the Newsgroups header;
92 the second with the current group name.")
93
94 (defvar gnus-message-setup-hook nil
95   "Hook run after setting up a message buffer.")
96
97 (defvar gnus-bug-create-help-buffer t
98   "*Should we create the *Gnus Help Bug* buffer?")
99
100 (defvar gnus-posting-styles nil
101   "*Alist of styles to use when posting.")
102
103 (defcustom gnus-group-posting-charset-alist
104   '(("^\\(no\\|fr\\|dk\\)\\.[^,]*\\(,[ \t\n]*\\(no\\|fr\\|dk\\)\\.[^,]*\\)*$" iso-8859-1 (iso-8859-1))
105     (message-this-is-mail nil nil)
106     (message-this-is-news nil t))
107   "Alist of regexps and permitted unencoded charsets for posting.
108 Each element of the alist has the form (TEST HEADER BODY-LIST), where
109 TEST is either a regular expression matching the newsgroup header or a
110 variable to query,
111 HEADER is the charset which may be left unencoded in the header (nil
112 means encode all charsets),
113 BODY-LIST is a list of charsets which may be encoded using 8bit
114 content-transfer encoding in the body, or one of the special values
115 nil (always encode using quoted-printable) or t (always use 8bit).
116
117 Note that any value other tha nil for HEADER infringes some RFCs, so
118 use this option with care."
119   :type '(repeat (list :tag "Permitted unencoded charsets"
120                   (choice :tag "Where"
121                    (regexp :tag "Group")
122                    (const :tag "Mail message" :value message-this-is-mail)
123                    (const :tag "News article" :value message-this-is-news))
124                   (choice :tag "Header"
125                    (const :tag "None" nil)
126                    (symbol :tag "Charset"))
127                   (choice :tag "Body"
128                           (const :tag "Any" :value t)
129                           (const :tag "None" :value nil)
130                           (repeat :tag "Charsets"
131                                   (symbol :tag "Charset")))))
132   :group 'gnus-charset)
133
134 ;;; Internal variables.
135
136 (defvar gnus-inhibit-posting-styles nil
137   "Inhibit the use of posting styles.")
138
139 (defvar gnus-message-buffer "*Mail Gnus*")
140 (defvar gnus-article-copy nil)
141 (defvar gnus-last-posting-server nil)
142 (defvar gnus-message-group-art nil)
143
144 (defconst gnus-bug-message
145   "Sending a bug report to the Gnus Towers.
146 ========================================
147
148 The buffer below is a mail buffer.  When you press `C-c C-c', it will
149 be sent to the Gnus Bug Exterminators.
150
151 The thing near the bottom of the buffer is how the environment
152 settings will be included in the mail.  Please do not delete that.
153 They will tell the Bug People what your environment is, so that it
154 will be easier to locate the bugs.
155
156 If you have found a bug that makes Emacs go \"beep\", set
157 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
158 and include the backtrace in your bug report.
159
160 Please describe the bug in annoying, painstaking detail.
161
162 Thank you for your help in stamping out bugs.
163 ")
164
165 (eval-and-compile
166   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
167   (autoload 'news-setup "rnewspost")
168   (autoload 'news-reply-mode "rnewspost")
169   (autoload 'rmail-dont-reply-to "mail-utils")
170   (autoload 'rmail-output "rmailout"))
171
172 \f
173 ;;;
174 ;;; Gnus Posting Functions
175 ;;;
176
177 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
178   "p" gnus-summary-post-news
179   "f" gnus-summary-followup
180   "F" gnus-summary-followup-with-original
181   "c" gnus-summary-cancel-article
182   "s" gnus-summary-supersede-article
183   "r" gnus-summary-reply
184   "y" gnus-summary-yank-message
185   "R" gnus-summary-reply-with-original
186   "w" gnus-summary-wide-reply
187   "W" gnus-summary-wide-reply-with-original
188   "n" gnus-summary-followup-to-mail
189   "N" gnus-summary-followup-to-mail-with-original
190   "m" gnus-summary-mail-other-window
191   "u" gnus-uu-post-news
192   "\M-c" gnus-summary-mail-crosspost-complaint
193   "om" gnus-summary-mail-forward
194   "op" gnus-summary-post-forward
195   "Om" gnus-uu-digest-mail-forward
196   "Op" gnus-uu-digest-post-forward)
197
198 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
199   "b" gnus-summary-resend-bounced-mail
200   ;; "c" gnus-summary-send-draft
201   "r" gnus-summary-resend-message)
202
203 ;;; Internal functions.
204
205 (defvar gnus-article-reply nil)
206 (defmacro gnus-setup-message (config &rest forms)
207   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
208         (buffer (make-symbol "gnus-setup-message-buffer"))
209         (article (make-symbol "gnus-setup-message-article"))
210         (group (make-symbol "gnus-setup-message-group")))
211     `(let ((,winconf (current-window-configuration))
212            (,buffer (buffer-name (current-buffer)))
213            (,article (and gnus-article-reply (gnus-summary-article-number)))
214            (,group gnus-newsgroup-name)
215            (message-header-setup-hook
216             (copy-sequence message-header-setup-hook))
217            (message-mode-hook (copy-sequence message-mode-hook)))
218        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
219        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
220        (add-hook 'message-mode-hook 'gnus-configure-posting-styles)
221        (unwind-protect
222            (progn
223              ,@forms)
224          (gnus-inews-add-send-actions ,winconf ,buffer ,article)
225          (setq gnus-message-buffer (current-buffer))
226          (set (make-local-variable 'gnus-message-group-art)
227               (cons ,group ,article))
228          (set (make-local-variable 'gnus-newsgroup-name) ,group)
229          (gnus-run-hooks 'gnus-message-setup-hook))
230        (gnus-add-buffer)
231        (gnus-configure-windows ,config t)
232        (set-buffer-modified-p nil))))
233
234 (defun gnus-setup-posting-charset (group)
235   (let ((alist gnus-group-posting-charset-alist)
236         (group (or group ""))
237         elem)
238     (when group
239       (catch 'found
240         (while (setq elem (pop alist))
241           (when (or (and (stringp (car elem))
242                          (string-match (car elem) group))
243                     (and (gnus-functionp (car elem))
244                          (funcall (car elem) group))
245                     (and (symbolp (car elem))
246                          (symbol-value (car elem))))
247             (throw 'found (cons (cadr elem) (caddr elem)))))))))
248
249 (defun gnus-inews-add-send-actions (winconf buffer article)
250   (make-local-hook 'message-sent-hook)
251   (add-hook 'message-sent-hook 'gnus-inews-do-gcc nil t)
252   (setq message-post-method
253         `(lambda (arg)
254            (gnus-post-method arg ,gnus-newsgroup-name)))
255   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
256   (message-add-action
257    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
258   (message-add-action
259    `(when (gnus-buffer-exists-p ,buffer)
260       (save-excursion
261         (set-buffer ,buffer)
262         ,(when article
263            `(gnus-summary-mark-article-as-replied ,article))))
264    'send))
265
266 (put 'gnus-setup-message 'lisp-indent-function 1)
267 (put 'gnus-setup-message 'edebug-form-spec '(form body))
268
269 ;;; Post news commands of Gnus group mode and summary mode
270
271 (defun gnus-group-mail (&optional arg)
272   "Start composing a mail.
273 If ARG, use the group under the point to find a posting style.
274 If ARG is 1, prompt for a group name to find the posting style."
275   (interactive "P")
276   ;; We can't `let' gnus-newsgroup-name here, since that leads
277   ;; to local variables leaking.
278   (let ((group gnus-newsgroup-name)
279         (buffer (current-buffer)))
280     (unwind-protect
281         (progn
282           (setq gnus-newsgroup-name
283                 (if arg
284                     (if (= 1 (prefix-numeric-value arg))
285                         (completing-read "Use posting style of group: "
286                                          gnus-active-hashtb nil
287                                          (gnus-read-active-file-p))
288                       (gnus-group-group-name))
289                   ""))
290           (gnus-setup-message 'message (message-mail)))
291       (save-excursion
292         (set-buffer buffer)
293         (setq gnus-newsgroup-name group)))))
294
295 (defun gnus-group-post-news (&optional arg)
296   "Start composing a news message.
297 If ARG, post to the group under point.
298 If ARG is 1, prompt for a group name."
299   (interactive "P")
300   ;; Bind this variable here to make message mode hooks work ok.
301   (let ((gnus-newsgroup-name
302          (if arg
303              (if (= 1 (prefix-numeric-value arg))
304                  (completing-read "Newsgroup: " gnus-active-hashtb nil
305                                   (gnus-read-active-file-p))
306                (gnus-group-group-name))
307            "")))
308     (gnus-post-news 'post gnus-newsgroup-name)))
309
310 (defun gnus-summary-post-news ()
311   "Start composing a news message."
312   (interactive)
313   (gnus-post-news 'post gnus-newsgroup-name))
314
315 (defun gnus-summary-followup (yank &optional force-news)
316   "Compose a followup to an article.
317 If prefix argument YANK is non-nil, original article is yanked automatically."
318   (interactive
319    (list (and current-prefix-arg
320               (gnus-summary-work-articles 1))))
321   (when yank
322     (gnus-summary-goto-subject (car yank)))
323   (save-window-excursion
324     (gnus-summary-select-article))
325   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
326         (gnus-newsgroup-name gnus-newsgroup-name))
327     ;; Send a followup.
328     (gnus-post-news nil gnus-newsgroup-name
329                     headers gnus-article-buffer
330                     yank nil force-news)))
331
332 (defun gnus-summary-followup-with-original (n &optional force-news)
333   "Compose a followup to an article and include the original article."
334   (interactive "P")
335   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
336
337 (defun gnus-summary-followup-to-mail (&optional arg)
338   "Followup to the current mail message via news."
339   (interactive
340    (list (and current-prefix-arg
341               (gnus-summary-work-articles 1))))
342   (gnus-summary-followup arg t))
343
344 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
345   "Followup to the current mail message via news."
346   (interactive "P")
347   (gnus-summary-followup (gnus-summary-work-articles arg) t))
348
349 (defun gnus-inews-yank-articles (articles)
350   (let (beg article)
351     (message-goto-body)
352     (while (setq article (pop articles))
353       (save-window-excursion
354         (set-buffer gnus-summary-buffer)
355         (gnus-summary-select-article nil nil nil article)
356         (gnus-summary-remove-process-mark article))
357       (gnus-copy-article-buffer)
358       (let ((message-reply-buffer gnus-article-copy)
359             (message-reply-headers gnus-current-headers))
360         (message-yank-original)
361         (setq beg (or beg (mark t))))
362       (when articles
363         (insert "\n")))
364     (push-mark)
365     (goto-char beg)))
366
367 (defun gnus-summary-cancel-article (&optional n symp)
368   "Cancel an article you posted.
369 Uses the process-prefix convention.  If given the symbolic
370 prefix `a', cancel using the standard posting method; if not
371 post using the current select method."
372   (interactive (gnus-interactive "P\ny"))
373   (let ((articles (gnus-summary-work-articles n))
374         (message-post-method
375          `(lambda (arg)
376             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
377         article)
378     (while (setq article (pop articles))
379       (when (gnus-summary-select-article t nil nil article)
380         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
381                 (message-cancel-news))
382           (gnus-summary-mark-as-read article gnus-canceled-mark)
383           (gnus-cache-remove-article 1))
384         (gnus-article-hide-headers-if-wanted))
385       (gnus-summary-remove-process-mark article))))
386
387 (defun gnus-summary-supersede-article ()
388   "Compose an article that will supersede a previous article.
389 This is done simply by taking the old article and adding a Supersedes
390 header line with the old Message-ID."
391   (interactive)
392   (let ((article (gnus-summary-article-number)))
393     (gnus-setup-message 'reply-yank
394       (gnus-summary-select-article t)
395       (set-buffer gnus-original-article-buffer)
396       (message-supersede)
397       (push
398        `((lambda ()
399            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
400              (save-excursion
401                (set-buffer ,gnus-summary-buffer)
402                (gnus-cache-possibly-remove-article ,article nil nil nil t)
403                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
404        message-send-actions))))
405
406 \f
407
408 (defun gnus-copy-article-buffer (&optional article-buffer)
409   ;; make a copy of the article buffer with all text properties removed
410   ;; this copy is in the buffer gnus-article-copy.
411   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
412   ;; this buffer should be passed to all mail/news reply/post routines.
413   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
414   (save-excursion
415     (set-buffer gnus-article-copy)
416     (mm-enable-multibyte))
417   (let ((article-buffer (or article-buffer gnus-article-buffer))
418         end beg)
419     (if (not (and (get-buffer article-buffer)
420                   (gnus-buffer-exists-p article-buffer)))
421         (error "Can't find any article buffer")
422       (save-excursion
423         (set-buffer article-buffer)
424         (save-restriction
425           ;; Copy over the (displayed) article buffer, delete
426           ;; hidden text and remove text properties.
427           (widen)
428           (copy-to-buffer gnus-article-copy (point-min) (point-max))
429           (set-buffer gnus-article-copy)
430           (gnus-article-delete-text-of-type 'annotation)
431           (gnus-remove-text-with-property 'gnus-prev)
432           (gnus-remove-text-with-property 'gnus-next)
433           (insert
434            (prog1
435                (format "%s" (buffer-string))
436              (erase-buffer)))
437           ;; Find the original headers.
438           (set-buffer gnus-original-article-buffer)
439           (goto-char (point-min))
440           (while (looking-at message-unix-mail-delimiter)
441             (forward-line 1))
442           (setq beg (point))
443           (setq end (or (search-forward "\n\n" nil t) (point)))
444           ;; Delete the headers from the displayed articles.
445           (set-buffer gnus-article-copy)
446           (delete-region (goto-char (point-min))
447                          (or (search-forward "\n\n" nil t) (point-max)))
448           ;; Insert the original article headers.
449           (insert-buffer-substring gnus-original-article-buffer beg end)
450           (article-decode-encoded-words)))
451       gnus-article-copy)))
452
453 (defun gnus-post-news (post &optional group header article-buffer yank subject
454                             force-news)
455   (when article-buffer
456     (gnus-copy-article-buffer))
457   (let ((gnus-article-reply article-buffer)
458         (add-to-list gnus-add-to-list))
459     (gnus-setup-message (cond (yank 'reply-yank)
460                               (article-buffer 'reply)
461                               (t 'message))
462       (let* ((group (or group gnus-newsgroup-name))
463              (pgroup group)
464              to-address to-group mailing-list to-list
465              newsgroup-p)
466         (when group
467           (setq to-address (gnus-group-find-parameter group 'to-address)
468                 to-group (gnus-group-find-parameter group 'to-group)
469                 to-list (gnus-group-find-parameter group 'to-list)
470                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
471                 mailing-list (when gnus-mailing-list-groups
472                                (string-match gnus-mailing-list-groups group))
473                 group (gnus-group-real-name group)))
474         (if (or (and to-group
475                      (gnus-news-group-p to-group))
476                 newsgroup-p
477                 force-news
478                 (and (gnus-news-group-p
479                       (or pgroup gnus-newsgroup-name)
480                       (if header (mail-header-number header)
481                         gnus-current-article))
482                      (not mailing-list)
483                      (not to-list)
484                      (not to-address)))
485             ;; This is news.
486             (if post
487                 (message-news (or to-group group))
488               (set-buffer gnus-article-copy)
489               (gnus-msg-treat-broken-reply-to)
490               (message-followup (if (or newsgroup-p force-news) nil to-group)))
491           ;; The is mail.
492           (if post
493               (progn
494                 (message-mail (or to-address to-list))
495                 ;; Arrange for mail groups that have no `to-address' to
496                 ;; get that when the user sends off the mail.
497                 (when (and (not to-list)
498                            (not to-address)
499                            add-to-list)
500                   (push (list 'gnus-inews-add-to-address pgroup)
501                         message-send-actions)))
502             (set-buffer gnus-article-copy)
503             (gnus-msg-treat-broken-reply-to)
504             (message-wide-reply to-address)))
505         (when yank
506           (gnus-inews-yank-articles yank))))))
507
508 (defun gnus-msg-treat-broken-reply-to ()
509   "Remove the Reply-to header iff broken-reply-to."
510   (when (gnus-group-find-parameter
511          gnus-newsgroup-name 'broken-reply-to)
512     (save-restriction
513       (message-narrow-to-head)
514       (message-remove-header "reply-to"))))
515
516 (defun gnus-post-method (arg group &optional silent)
517   "Return the posting method based on GROUP and ARG.
518 If SILENT, don't prompt the user."
519   (let ((group-method (gnus-find-method-for-group group)))
520     (cond
521      ;; If the group-method is nil (which shouldn't happen) we use
522      ;; the default method.
523      ((null group-method)
524       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
525           gnus-select-method message-post-method))
526      ;; We want the inverse of the default
527      ((and arg (not (eq arg 0)))
528       (if (eq gnus-post-method 'active)
529           gnus-select-method
530         group-method))
531      ;; We query the user for a post method.
532      ((or arg
533           (and gnus-post-method
534                (not (eq gnus-post-method 'current))
535                (listp (car gnus-post-method))))
536       (let* ((methods
537               ;; Collect all methods we know about.
538               (append
539                (when (and gnus-post-method
540                           (not (eq gnus-post-method 'current)))
541                  (if (listp (car gnus-post-method))
542                      gnus-post-method
543                    (list gnus-post-method)))
544                gnus-secondary-select-methods
545                (mapcar 'cdr gnus-server-alist)
546                (mapcar 'car gnus-opened-servers)
547                (list gnus-select-method)
548                (list group-method)))
549              method-alist post-methods method)
550         ;; Weed out all mail methods.
551         (while methods
552           (setq method (gnus-server-get-method "" (pop methods)))
553           (when (and (or (gnus-method-option-p method 'post)
554                          (gnus-method-option-p method 'post-mail))
555                      (not (member method post-methods)))
556             (push method post-methods)))
557         ;; Create a name-method alist.
558         (setq method-alist
559               (mapcar
560                (lambda (m)
561                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
562                post-methods))
563         ;; Query the user.
564         (cadr
565          (assoc
566           (setq gnus-last-posting-server
567                 (if (and silent
568                          gnus-last-posting-server)
569                     ;; Just use the last value.
570                     gnus-last-posting-server
571                   (completing-read
572                    "Posting method: " method-alist nil t
573                    (cons (or gnus-last-posting-server "") 0))))
574           method-alist))))
575      ;; Override normal method.
576      ((and (eq gnus-post-method 'current)
577            (not (eq (car group-method) 'nndraft))
578            (gnus-get-function group-method 'request-post t)
579            (not arg))
580       group-method)
581      ((and gnus-post-method
582            (not (eq gnus-post-method 'current)))
583       gnus-post-method)
584      ;; Use the normal select method.
585      (t gnus-select-method))))
586
587 \f
588
589 ;; Dummies to avoid byte-compile warning.
590 (defvar nnspool-rejected-article-hook)
591 (defvar xemacs-codename)
592
593 (defun gnus-extended-version ()
594   "Stringified Gnus version and Emacs version."
595   (interactive)
596   (concat
597    "Gnus/" (prin1-to-string (gnus-continuum-version gnus-version) t)
598    " (" gnus-version ")"
599    " "
600    (cond
601     ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version)
602      (concat "Emacs/" (match-string 1 emacs-version)))
603     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)[^(]*\\(\\((beta.*)\\|'\\)\\)?"
604                    emacs-version)
605      (concat (match-string 1 emacs-version)
606              (format "/%d.%d" emacs-major-version emacs-minor-version)
607              (if (match-beginning 3)
608                  (match-string 3 emacs-version)
609                "")
610              (if (boundp 'xemacs-codename)
611                  (concat " (" xemacs-codename ")")
612                "")))
613     (t emacs-version))))
614
615 \f
616 ;;;
617 ;;; Gnus Mail Functions
618 ;;;
619
620 ;;; Mail reply commands of Gnus summary mode
621
622 (defun gnus-summary-reply (&optional yank wide)
623   "Start composing a reply mail to the current message.
624 If prefix argument YANK is non-nil, the original article is yanked
625 automatically."
626   (interactive
627    (list (and current-prefix-arg
628               (gnus-summary-work-articles 1))))
629   ;; Stripping headers should be specified with mail-yank-ignored-headers.
630   (when yank
631     (gnus-summary-goto-subject (car yank)))
632   (let ((gnus-article-reply t))
633     (gnus-setup-message (if yank 'reply-yank 'reply)
634       (gnus-summary-select-article)
635       (set-buffer (gnus-copy-article-buffer))
636       (gnus-msg-treat-broken-reply-to)
637       (message-reply nil wide)
638       (when yank
639         (gnus-inews-yank-articles yank)))))
640
641 (defun gnus-summary-reply-with-original (n &optional wide)
642   "Start composing a reply mail to the current message.
643 The original article will be yanked."
644   (interactive "P")
645   (gnus-summary-reply (gnus-summary-work-articles n) wide))
646
647 (defun gnus-summary-wide-reply (&optional yank)
648   "Start composing a wide reply mail to the current message.
649 If prefix argument YANK is non-nil, the original article is yanked
650 automatically."
651   (interactive
652    (list (and current-prefix-arg
653               (gnus-summary-work-articles 1))))
654   (gnus-summary-reply yank t))
655
656 (defun gnus-summary-wide-reply-with-original (n)
657   "Start composing a wide reply mail to the current message.
658 The original article will be yanked."
659   (interactive "P")
660   (gnus-summary-reply-with-original n t))
661
662 (defun gnus-summary-mail-forward (&optional not-used post)
663   "Forward the current message to another user.
664 If POST, post instead of mail."
665   (interactive "P")
666   (gnus-setup-message 'forward
667     (gnus-summary-select-article)
668     (let (text)
669       (save-excursion
670         (set-buffer gnus-original-article-buffer)
671         (setq text (buffer-string)))
672       (set-buffer (gnus-get-buffer-create
673                    (generate-new-buffer-name " *Gnus forward*")))
674       (erase-buffer)
675       (insert text)
676       (goto-char (point-min))
677       (when (looking-at "From ")
678         (replace-match "X-From-Line: ") )
679       (run-hooks 'gnus-article-decode-hook)
680       (message-forward post))))
681
682 (defun gnus-summary-resend-message (address n)
683   "Resend the current article to ADDRESS."
684   (interactive "sResend message(s) to: \nP")
685   (let ((articles (gnus-summary-work-articles n))
686         article)
687     (while (setq article (pop articles))
688       (gnus-summary-select-article nil nil nil article)
689       (save-excursion
690         (set-buffer gnus-original-article-buffer)
691         (message-resend address)))))
692
693 (defun gnus-summary-post-forward (&optional full-headers)
694   "Forward the current article to a newsgroup.
695 If FULL-HEADERS (the prefix), include full headers when forwarding."
696   (interactive "P")
697   (gnus-summary-mail-forward full-headers t))
698
699 (defvar gnus-nastygram-message
700   "The following article was inappropriately posted to %s.\n\n"
701   "Format string to insert in nastygrams.
702 The current group name will be inserted at \"%s\".")
703
704 (defun gnus-summary-mail-nastygram (n)
705   "Send a nastygram to the author of the current article."
706   (interactive "P")
707   (when (or gnus-expert-user
708             (gnus-y-or-n-p
709              "Really send a nastygram to the author of the current article? "))
710     (let ((group gnus-newsgroup-name))
711       (gnus-summary-reply-with-original n)
712       (set-buffer gnus-message-buffer)
713       (message-goto-body)
714       (insert (format gnus-nastygram-message group))
715       (message-send-and-exit))))
716
717 (defun gnus-summary-mail-crosspost-complaint (n)
718   "Send a complaint about crossposting to the current article(s)."
719   (interactive "P")
720   (let ((articles (gnus-summary-work-articles n))
721         article)
722     (while (setq article (pop articles))
723       (set-buffer gnus-summary-buffer)
724       (gnus-summary-goto-subject article)
725       (let ((group (gnus-group-real-name gnus-newsgroup-name))
726             newsgroups followup-to)
727         (gnus-summary-select-article)
728         (set-buffer gnus-original-article-buffer)
729         (if (and (<= (length (message-tokenize-header
730                               (setq newsgroups
731                                     (mail-fetch-field "newsgroups"))
732                               ", "))
733                      1)
734                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
735                      (not (member group (message-tokenize-header
736                                          followup-to ", ")))))
737             (if followup-to
738                 (gnus-message 1 "Followup-to restricted")
739               (gnus-message 1 "Not a crossposted article"))
740           (set-buffer gnus-summary-buffer)
741           (gnus-summary-reply-with-original 1)
742           (set-buffer gnus-message-buffer)
743           (message-goto-body)
744           (insert (format gnus-crosspost-complaint newsgroups group))
745           (message-goto-subject)
746           (re-search-forward " *$")
747           (replace-match " (crosspost notification)" t t)
748           (gnus-deactivate-mark)
749           (when (gnus-y-or-n-p "Send this complaint? ")
750             (message-send-and-exit)))))))
751
752 (defun gnus-summary-mail-other-window ()
753   "Compose mail in other window."
754   (interactive)
755   (gnus-setup-message 'message
756     (message-mail)))
757
758 (defun gnus-mail-parse-comma-list ()
759   (let (accumulated
760         beg)
761     (skip-chars-forward " ")
762     (while (not (eobp))
763       (setq beg (point))
764       (skip-chars-forward "^,")
765       (while (zerop
766               (save-excursion
767                 (save-restriction
768                   (let ((i 0))
769                     (narrow-to-region beg (point))
770                     (goto-char beg)
771                     (logand (progn
772                               (while (search-forward "\"" nil t)
773                                 (incf i))
774                               (if (zerop i) 2 i))
775                             2)))))
776         (skip-chars-forward ",")
777         (skip-chars-forward "^,"))
778       (skip-chars-backward " ")
779       (push (buffer-substring beg (point))
780             accumulated)
781       (skip-chars-forward "^,")
782       (skip-chars-forward ", "))
783     accumulated))
784
785 (defun gnus-inews-add-to-address (group)
786   (let ((to-address (mail-fetch-field "to")))
787     (when (and to-address
788                (gnus-alive-p))
789       ;; This mail group doesn't have a `to-list', so we add one
790       ;; here.  Magic!
791       (when (gnus-y-or-n-p
792              (format "Do you want to add this as `to-list': %s " to-address))
793         (gnus-group-add-parameter group (cons 'to-list to-address))))))
794
795 (defun gnus-put-message ()
796   "Put the current message in some group and return to Gnus."
797   (interactive)
798   (let ((reply gnus-article-reply)
799         (winconf gnus-prev-winconf)
800         (group gnus-newsgroup-name))
801
802     (or (and group (not (gnus-group-read-only-p group)))
803         (setq group (read-string "Put in group: " nil
804                                  (gnus-writable-groups))))
805     (when (gnus-gethash group gnus-newsrc-hashtb)
806       (error "No such group: %s" group))
807
808     (save-excursion
809       (save-restriction
810         (widen)
811         (message-narrow-to-headers)
812         (let (gnus-deletable-headers)
813           (if (message-news-p)
814               (message-generate-headers message-required-news-headers)
815             (message-generate-headers message-required-mail-headers)))
816         (goto-char (point-max))
817         (insert "Gcc: " group "\n")
818         (widen)))
819
820     (gnus-inews-do-gcc)
821
822     (when (get-buffer gnus-group-buffer)
823       (when (gnus-buffer-exists-p (car-safe reply))
824         (set-buffer (car reply))
825         (and (cdr reply)
826              (gnus-summary-mark-article-as-replied
827               (cdr reply))))
828       (when winconf
829         (set-window-configuration winconf)))))
830
831 (defun gnus-article-mail (yank)
832   "Send a reply to the address near point.
833 If YANK is non-nil, include the original article."
834   (interactive "P")
835   (let ((address
836          (buffer-substring
837           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
838           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
839     (when address
840       (message-reply address)
841       (when yank
842         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
843
844 (defvar nntp-server-type)
845 (defun gnus-bug ()
846   "Send a bug report to the Gnus maintainers."
847   (interactive)
848   (unless (gnus-alive-p)
849     (error "Gnus has been shut down"))
850   (gnus-setup-message 'bug
851     (delete-other-windows)
852     (when gnus-bug-create-help-buffer
853       (switch-to-buffer "*Gnus Help Bug*")
854       (erase-buffer)
855       (insert gnus-bug-message)
856       (goto-char (point-min)))
857     (message-pop-to-buffer "*Gnus Bug*")
858     (message-setup `((To . ,gnus-maintainer) (Subject . "")))
859     (when gnus-bug-create-help-buffer
860       (push `(gnus-bug-kill-buffer) message-send-actions))
861     (goto-char (point-min))
862     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
863     (forward-line 1)
864     (insert (gnus-version) "\n"
865             (emacs-version) "\n")
866     (when (and (boundp 'nntp-server-type)
867                (stringp nntp-server-type))
868       (insert nntp-server-type))
869     (insert "\n\n\n\n\n")
870     (save-excursion
871       (set-buffer (gnus-get-buffer-create " *gnus environment info*"))
872       (gnus-debug))
873     (insert "<#part type=application/x-emacs-lisp buffer=\" *gnus environment info*\" disposition=inline description=\"User settings\"><#/part>")
874     (goto-char (point-min))
875     (search-forward "Subject: " nil t)
876     (message "")))
877
878 (defun gnus-bug-kill-buffer ()
879   (when (get-buffer "*Gnus Help Bug*")
880     (kill-buffer "*Gnus Help Bug*")))
881
882 (defun gnus-summary-yank-message (buffer n)
883   "Yank the current article into a composed message."
884   (interactive
885    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
886          current-prefix-arg))
887   (gnus-summary-iterate n
888     (let ((gnus-display-mime-function nil)
889           (gnus-inhibit-treatment t))
890       (gnus-summary-select-article))
891     (save-excursion
892       (set-buffer buffer)
893       (message-yank-buffer gnus-article-buffer))))
894
895 (defun gnus-debug ()
896   "Attempts to go through the Gnus source file and report what variables have been changed.
897 The source file has to be in the Emacs load path."
898   (interactive)
899   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
900                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
901                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
902                  "nnmail.el" "message.el"))
903         (point (point))
904         file expr olist sym)
905     (gnus-message 4 "Please wait while we snoop your variables...")
906     (sit-for 0)
907     ;; Go through all the files looking for non-default values for variables.
908     (save-excursion
909       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
910       (while files
911         (erase-buffer)
912         (when (and (setq file (locate-library (pop files)))
913                    (file-exists-p file))
914           (insert-file-contents file)
915           (goto-char (point-min))
916           (if (not (re-search-forward "^;;* *Internal variables" nil t))
917               (gnus-message 4 "Malformed sources in file %s" file)
918             (narrow-to-region (point-min) (point))
919             (goto-char (point-min))
920             (while (setq expr (ignore-errors (read (current-buffer))))
921               (ignore-errors
922                 (and (or (eq (car expr) 'defvar)
923                          (eq (car expr) 'defcustom))
924                      (stringp (nth 3 expr))
925                      (or (not (boundp (nth 1 expr)))
926                          (not (equal (eval (nth 2 expr))
927                                      (symbol-value (nth 1 expr)))))
928                      (push (nth 1 expr) olist)))))))
929       (kill-buffer (current-buffer)))
930     (when (setq olist (nreverse olist))
931       (insert "------------------ Environment follows ------------------\n\n"))
932     (while olist
933       (if (boundp (car olist))
934           (condition-case ()
935               (pp `(setq ,(car olist)
936                          ,(if (or (consp (setq sym (symbol-value (car olist))))
937                                   (and (symbolp sym)
938                                        (not (or (eq sym nil)
939                                                 (eq sym t)))))
940                               (list 'quote (symbol-value (car olist)))
941                             (symbol-value (car olist))))
942                   (current-buffer))
943             (error
944              (format "(setq %s 'whatever)\n" (car olist))))
945         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
946       (setq olist (cdr olist)))
947     (insert "\n\n")
948     ;; Remove any control chars - they seem to cause trouble for some
949     ;; mailers.  (Byte-compiled output from the stuff above.)
950     (goto-char point)
951     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
952       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
953                      t t))))
954
955 ;;; Treatment of rejected articles.
956 ;;; Bounced mail.
957
958 (defun gnus-summary-resend-bounced-mail (&optional fetch)
959   "Re-mail the current message.
960 This only makes sense if the current message is a bounce message than
961 contains some mail you have written which has been bounced back to
962 you.
963 If FETCH, try to fetch the article that this is a reply to, if indeed
964 this is a reply."
965   (interactive "P")
966   (gnus-summary-select-article t)
967   (set-buffer gnus-original-article-buffer)
968   (gnus-setup-message 'compose-bounce
969     (let* ((references (mail-fetch-field "references"))
970            (parent (and references (gnus-parent-id references))))
971       (message-bounce)
972       ;; If there are references, we fetch the article we answered to.
973       (and fetch parent
974            (gnus-summary-refer-article parent)
975            (gnus-summary-show-all-headers)))))
976
977 ;;; Gcc handling.
978
979 ;; Do Gcc handling, which copied the message over to some group.
980 (defun gnus-inews-do-gcc (&optional gcc)
981   (interactive)
982   (when (gnus-alive-p)
983     (save-excursion
984       (save-restriction
985         (message-narrow-to-headers)
986         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
987               (cur (current-buffer))
988               groups group method)
989           (when gcc
990             (message-remove-header "gcc")
991             (widen)
992             (setq groups (message-tokenize-header gcc " ,"))
993             ;; Copy the article over to some group(s).
994             (while (setq group (pop groups))
995               (gnus-check-server
996                (setq method
997                      (cond ((and (null (gnus-get-info group))
998                                  (eq (car gnus-message-archive-method)
999                                      (car
1000                                       (gnus-server-to-method
1001                                        (gnus-group-method group)))))
1002                             ;; If the group doesn't exist, we assume
1003                             ;; it's an archive group...
1004                             gnus-message-archive-method)
1005                            ;; Use the method.
1006                            ((gnus-info-method (gnus-get-info group))
1007                             (gnus-info-method (gnus-get-info group)))
1008                            ;; Find the method.
1009                            (t (gnus-group-method group)))))
1010               (gnus-check-server method)
1011               (unless (gnus-request-group group t method)
1012                 (gnus-request-create-group group method))
1013               (save-excursion
1014                 (nnheader-set-temp-buffer " *acc*")
1015                 (insert-buffer-substring cur)
1016                 (message-encode-message-body)
1017                 (save-restriction
1018                   (message-narrow-to-headers)
1019                   (mail-encode-encoded-word-buffer))
1020                 (goto-char (point-min))
1021                 (when (re-search-forward
1022                        (concat "^" (regexp-quote mail-header-separator) "$")
1023                        nil t)
1024                   (replace-match "" t t ))
1025                 (unless (gnus-request-accept-article group method t t)
1026                   (gnus-message 1 "Couldn't store article in group %s: %s"
1027                                 group (gnus-status-message method))
1028                   (sit-for 2))
1029                 (kill-buffer (current-buffer))))))))))
1030
1031 (defun gnus-inews-insert-gcc ()
1032   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1033   (save-excursion
1034     (save-restriction
1035       (message-narrow-to-headers)
1036       (let* ((group gnus-outgoing-message-group)
1037              (gcc (cond
1038                    ((gnus-functionp group)
1039                     (funcall group))
1040                    ((or (stringp group) (list group))
1041                     group))))
1042         (when gcc
1043           (insert "Gcc: "
1044                   (if (stringp gcc) gcc
1045                     (mapconcat 'identity gcc " "))
1046                   "\n"))))))
1047
1048 (defun gnus-inews-insert-archive-gcc (&optional group)
1049   "Insert the Gcc to say where the article is to be archived."
1050   (let* ((var gnus-message-archive-group)
1051          (group (or group gnus-newsgroup-name ""))
1052          (gcc-self-val
1053           (and gnus-newsgroup-name
1054                (gnus-group-find-parameter
1055                 gnus-newsgroup-name 'gcc-self)))
1056          result
1057          (groups
1058           (cond
1059            ((null gnus-message-archive-method)
1060             ;; Ignore.
1061             nil)
1062            ((stringp var)
1063             ;; Just a single group.
1064             (list var))
1065            ((null var)
1066             ;; We don't want this.
1067             nil)
1068            ((and (listp var) (stringp (car var)))
1069             ;; A list of groups.
1070             var)
1071            ((gnus-functionp var)
1072             ;; A function.
1073             (funcall var group))
1074            (t
1075             ;; An alist of regexps/functions/forms.
1076             (while (and var
1077                         (not
1078                          (setq result
1079                                (cond
1080                                 ((stringp (caar var))
1081                                  ;; Regexp.
1082                                  (when (string-match (caar var) group)
1083                                    (cdar var)))
1084                                 ((gnus-functionp (car var))
1085                                  ;; Function.
1086                                  (funcall (car var) group))
1087                                 (t
1088                                  (eval (car var)))))))
1089               (setq var (cdr var)))
1090             result)))
1091          name)
1092     (when (or groups gcc-self-val)
1093       (when (stringp groups)
1094         (setq groups (list groups)))
1095       (save-excursion
1096         (save-restriction
1097           (message-narrow-to-headers)
1098           (goto-char (point-max))
1099           (insert "Gcc: ")
1100           (if gcc-self-val
1101               ;; Use the `gcc-self' param value instead.
1102               (progn
1103                 (insert
1104                  (if (stringp gcc-self-val)
1105                      gcc-self-val
1106                    group))
1107                 (if (not (eq gcc-self-val 'none))
1108                     (insert "\n")
1109                   (progn
1110                     (beginning-of-line)
1111                     (kill-line))))
1112             ;; Use the list of groups.
1113             (while (setq name (pop groups))
1114               (insert (if (string-match ":" name)
1115                           name
1116                         (gnus-group-prefixed-name
1117                          name gnus-message-archive-method)))
1118               (when groups
1119                 (insert " ")))
1120             (insert "\n")))))))
1121
1122 ;;; Posting styles.
1123
1124 (defun gnus-configure-posting-styles ()
1125   "Configure posting styles according to `gnus-posting-styles'."
1126   (unless gnus-inhibit-posting-styles
1127     (let ((group (or gnus-newsgroup-name ""))
1128           (styles gnus-posting-styles)
1129           style match variable attribute value v results
1130           filep name address element)
1131       ;; If the group has a posting-style parameter, add it at the end with a
1132       ;; regexp matching everything, to be sure it takes precedence over all
1133       ;; the others.
1134       (when gnus-newsgroup-name
1135         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1136           (when tmp-style
1137             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1138       ;; Go through all styles and look for matches.
1139       (dolist (style styles)
1140         (setq match (pop style))
1141         (goto-char (point-min))
1142         (when (cond
1143                ((stringp match)
1144                 ;; Regexp string match on the group name.
1145                 (string-match match group))
1146                ((eq match 'header)
1147                 (let ((header (message-fetch-field (pop style))))
1148                   (and header
1149                        (string-match (pop style) header))))
1150                ((or (symbolp match)
1151                     (gnus-functionp match))
1152                 (cond
1153                  ((gnus-functionp match)
1154                   ;; Function to be called.
1155                   (funcall match))
1156                  ((boundp match)
1157                   ;; Variable to be checked.
1158                   (symbol-value match))))
1159                ((listp match)
1160                 ;; This is a form to be evaled.
1161                 (eval match)))
1162           ;; We have a match, so we set the variables.
1163           (dolist (attribute style)
1164             (setq element (pop attribute)
1165                   variable nil
1166                   filep nil)
1167             (setq value
1168                   (cond
1169                    ((eq (car attribute) :file)
1170                     (setq filep t)
1171                     (cadr attribute))
1172                    ((eq (car attribute) :value)
1173                     (cadr attribute))
1174                    (t
1175                     (car attribute))))
1176             ;; We get the value.
1177             (setq v
1178                   (cond
1179                    ((stringp value)
1180                     value)
1181                    ((or (symbolp value)
1182                         (gnus-functionp value))
1183                     (cond ((gnus-functionp value)
1184                            (funcall value))
1185                           ((boundp value)
1186                            (symbol-value value))))
1187                    ((listp value)
1188                     (eval value))))
1189             ;; Translate obsolescent value.
1190             (when (eq element 'signature-file)
1191               (setq element 'signature
1192                     filep t))
1193             ;; Get the contents of file elems.
1194             (when (and filep v)
1195               (setq v (with-temp-buffer
1196                         (insert-file-contents v)
1197                         (buffer-string))))
1198             (setq results (delq (assoc element results) results))
1199             (push (cons element v) results))))
1200       ;; Now we have all the styles, so we insert them.
1201       (setq name (assq 'name results)
1202             address (assq 'address results))
1203       (setq results (delq name (delq address results)))
1204       (make-local-variable 'message-setup-hook)
1205       (dolist (result results)
1206         (when (cdr result)
1207           (add-hook 'message-setup-hook
1208                     (cond
1209                      ((eq 'eval (car result))
1210                       'ignore)
1211                      ((eq 'body (car result))
1212                       `(lambda ()
1213                          (save-excursion
1214                            (message-goto-body)
1215                            (insert ,(cdr result)))))
1216                      ((eq 'signature (car result))
1217                       (set (make-local-variable 'message-signature) nil)
1218                       (set (make-local-variable 'message-signature-file) nil)
1219                       (if (not (cdr result))
1220                           'ignore
1221                         `(lambda ()
1222                            (save-excursion
1223                              (let ((message-signature ,(cdr result)))
1224                                (when message-signature
1225                                  (message-insert-signature)))))))
1226                      (t
1227                       (let ((header
1228                              (if (symbolp (car result))
1229                                  (capitalize (symbol-name (car result)))
1230                                (car result))))
1231                         `(lambda ()
1232                            (save-excursion
1233                              (message-remove-header ,header)
1234                              (message-goto-eoh)
1235                              (insert ,header ": " ,(cdr result) "\n")))))))))
1236       (when (or name address)
1237         (add-hook 'message-setup-hook
1238                   `(lambda ()
1239                      (set (make-local-variable 'user-mail-address)
1240                           ,(or (cdr address) user-mail-address))
1241                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1242                            (user-mail-address
1243                             ,(or (cdr address) user-mail-address)))
1244                        (save-excursion
1245                          (message-remove-header "From")
1246                          (message-goto-eoh)
1247                          (insert "From: " (message-make-from) "\n")))))))))
1248
1249 ;;; Allow redefinition of functions.
1250
1251 (gnus-ems-redefine)
1252
1253 (provide 'gnus-msg)
1254
1255 ;;; gnus-msg.el ends here