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