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