* pop3.el (pop3-open-server): Set process buffer unibyte.
[elisp/gnus.git-] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Semi-gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;      Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
9 ;;      Katsumi Yamaoka  <yamaoka@jpl.org>
10 ;;      Kiyokazu SUTO    <suto@merry.xmath.ous.ac.jp>
11 ;; Keywords: mail, news, MIME
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 ;;; Commentary:
31
32 ;;; Code:
33
34 (eval-when-compile (require 'cl))
35 (eval-when-compile (require 'static))
36
37 (require 'gnus)
38 (require 'gnus-ems)
39 (require 'message)
40 (require 'gnus-art)
41
42 (defcustom gnus-post-method 'current
43   "*Preferred method for posting USENET news.
44
45 If this variable is `current' (which is the default), Gnus will use
46 the \"current\" select method when posting.  If it is `native', Gnus
47 will use the native select method when posting.
48
49 This method will not be used in mail groups and the like, only in
50 \"real\" newsgroups.
51
52 If not `native' nor `current', the value must be a valid method as discussed
53 in the documentation of `gnus-select-method'.  It can also be a list of
54 methods.  If that is the case, the user will be queried for what select
55 method to use when posting."
56   :group 'gnus-group-foreign
57   :link '(custom-manual "(gnus)Posting Server")
58   :type `(choice (const native)
59                  (const current)
60                  (sexp :tag "Methods" ,gnus-select-method)))
61
62 (defcustom gnus-outgoing-message-group nil
63   "*All outgoing messages will be put in this group.
64 If you want to store all your outgoing mail and articles in the group
65 \"nnml:archive\", you set this variable to that value.  This variable
66 can also be a list of group names.
67
68 If you want to have greater control over what group to put each
69 message in, you can set this variable to a function that checks the
70 current newsgroup name and then returns a suitable group name (or list
71 of names)."
72   :group 'gnus-message
73   :type '(choice (string :tag "Group")
74                  (function)))
75
76 (defcustom gnus-mailing-list-groups nil
77   "*Regexp matching groups that are really mailing lists.
78 This is useful when you're reading a mailing list that has been
79 gatewayed to a newsgroup, and you want to followup to an article in
80 the group."
81   :group 'gnus-message
82   :type 'regexp)
83
84 (defcustom gnus-add-to-list nil
85   "*If non-nil, add a `to-list' parameter automatically."
86   :group 'gnus-message
87   :type 'boolean)
88
89 (defcustom gnus-crosspost-complaint
90   "Hi,
91
92 You posted the article below with the following Newsgroups header:
93
94 Newsgroups: %s
95
96 The %s group, at least, was an inappropriate recipient
97 of this message.  Please trim your Newsgroups header to exclude this
98 group before posting in the future.
99
100 Thank you.
101
102 "
103   "Format string to be inserted when complaining about crossposts.
104 The first %s will be replaced by the Newsgroups header;
105 the second with the current group name."
106   :group 'gnus-message
107   :type 'string)
108
109 (defcustom gnus-message-setup-hook '(gnus-maybe-setup-default-charset)
110   "Hook run after setting up a message buffer."
111   :group 'gnus-message
112   :type 'hook)
113
114 (defcustom gnus-bug-create-help-buffer t
115   "*Should we create the *Gnus Help Bug* buffer?"
116   :group 'gnus-message
117   :type 'boolean)
118
119 (defcustom gnus-posting-styles nil
120   "*Alist of styles to use when posting.
121 See Info node `(gnus)Posting Styles'."
122   :group 'gnus-message
123   :type '(repeat (cons (choice (regexp)
124                                (function)
125                                (variable)
126                                (sexp))
127                        (repeat (list
128                                 (choice (const signature)
129                                         (const signature-file)
130                                         (const organization)
131                                         (const address)
132                                         (const name)
133                                         (const body)
134                                         (string :tag "Header"))
135                                 (choice (string)
136                                         (function)
137                                         (variable)
138                                         (sexp)))))))
139
140 (defcustom gnus-gcc-mark-as-read nil
141   "If non-nil, automatically mark Gcc articles as read."
142   :group 'gnus-message
143   :type 'boolean)
144
145 (defvar gnus-inews-mark-gcc-as-read nil
146   "Obsolete variable. Use `gnus-gcc-mark-as-read' instead.")
147
148 (make-obsolete-variable 'gnus-inews-mark-gcc-as-read
149                         'gnus-gcc-mark-as-read)
150
151 (defcustom gnus-gcc-externalize-attachments nil
152   "Should local-file attachments be included as external parts in Gcc copies?
153 If it is `all', attach files as external parts;
154 if a regexp and matches the Gcc group name, attach files as external parts;
155 If nil, attach files as normal parts."
156   :group 'gnus-message
157   :type '(choice (const nil :tag "None")
158                  (const all :tag "Any")
159                  (string :tag "Regexp")))
160
161 (defcustom gnus-group-posting-charset-alist
162   '(("^\\(no\\|fr\\)\\.[^,]*\\(,[ \t\n]*\\(no\\|fr\\)\\.[^,]*\\)*$" iso-8859-1 (iso-8859-1))
163     ("^\\(fido7\\|relcom\\)\\.[^,]*\\(,[ \t\n]*\\(fido7\\|relcom\\)\\.[^,]*\\)*$" koi8-r (koi8-r))
164     (message-this-is-mail nil nil)
165     (message-this-is-news nil t))
166   "Alist of regexps and permitted unencoded charsets for posting.
167 Each element of the alist has the form (TEST HEADER BODY-LIST), where
168 TEST is either a regular expression matching the newsgroup header or a
169 variable to query,
170 HEADER is the charset which may be left unencoded in the header (nil
171 means encode all charsets),
172 BODY-LIST is a list of charsets which may be encoded using 8bit
173 content-transfer encoding in the body, or one of the special values
174 nil (always encode using quoted-printable) or t (always use 8bit).
175
176 Note that any value other than nil for HEADER infringes some RFCs, so
177 use this option with care."
178   :type '(repeat (list :tag "Permitted unencoded charsets"
179                        (choice :tag "Where"
180                                (regexp :tag "Group")
181                                (const :tag "Mail message"
182                                       :value message-this-is-mail)
183                                (const :tag "News article"
184                                       :value message-this-is-news))
185                        (choice :tag "Header"
186                                (const :tag "None" nil)
187                                (symbol :tag "Charset"))
188                        (choice :tag "Body"
189                                (const :tag "Any" :value t)
190                                (const :tag "None" :value nil)
191                                (repeat :tag "Charsets"
192                                        (symbol :tag "Charset")))))
193   :group 'gnus-charset)
194
195 ;;; Internal variables.
196
197 (defvar gnus-inhibit-posting-styles nil
198   "Inhibit the use of posting styles.")
199
200 (defvar gnus-message-buffer "*Mail Gnus*")
201 (defvar gnus-article-copy nil)
202 (defvar gnus-check-before-posting nil)
203 (defvar gnus-last-posting-server nil)
204 (defvar gnus-message-group-art nil)
205
206 (defvar gnus-msg-force-broken-reply-to nil)
207
208 (defconst gnus-bug-message
209   (format "Sending a bug report to the Gnus Towers.
210 ========================================
211
212 This gnus is the %s%s.
213 If you think the bug is a Semi-gnus bug, send a bug report to Semi-gnus
214 Developers. (the addresses below are mailing list addresses)
215
216 ========================================
217
218 The buffer below is a mail buffer.  When you press `C-c C-c', it will
219 be sent to the Gnus Bug Exterminators.
220
221 The thing near the bottom of the buffer is how the environment
222 settings will be included in the mail.  Please do not delete that.
223 They will tell the Bug People what your environment is, so that it
224 will be easier to locate the bugs.
225
226 If you have found a bug that makes Emacs go \"beep\", set
227 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
228 and include the backtrace in your bug report.
229
230 Please describe the bug in annoying, painstaking detail.
231
232 Thank you for your help in stamping out bugs.
233 "
234           gnus-product-name
235           (if (string= gnus-product-name "Semi-gnus")
236               ""
237             ", a modified version of Semi-gnus")))
238
239 (eval-and-compile
240   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
241   (autoload 'news-setup "rnewspost")
242   (autoload 'news-reply-mode "rnewspost")
243   (autoload 'rmail-dont-reply-to "mail-utils")
244   (autoload 'rmail-output "rmailout"))
245
246 \f
247 ;;;
248 ;;; Gnus Posting Functions
249 ;;;
250
251 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
252   "p" gnus-summary-post-news
253   "i" gnus-summary-news-other-window
254   "f" gnus-summary-followup
255   "F" gnus-summary-followup-with-original
256   "c" gnus-summary-cancel-article
257   "s" gnus-summary-supersede-article
258   "r" gnus-summary-reply
259   "y" gnus-summary-yank-message
260   "R" gnus-summary-reply-with-original
261   "w" gnus-summary-wide-reply
262   "W" gnus-summary-wide-reply-with-original
263   "v" gnus-summary-very-wide-reply
264   "V" gnus-summary-very-wide-reply-with-original
265   "n" gnus-summary-followup-to-mail
266   "N" gnus-summary-followup-to-mail-with-original
267   "m" gnus-summary-mail-other-window
268   "u" gnus-uu-post-news
269   "\M-c" gnus-summary-mail-crosspost-complaint
270   "Br" gnus-summary-reply-broken-reply-to
271   "BR" gnus-summary-reply-broken-reply-to-with-original
272   "om" gnus-summary-mail-forward
273   "op" gnus-summary-post-forward
274   "Om" gnus-summary-digest-mail-forward
275   "Op" gnus-summary-digest-post-forward)
276
277 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
278   "b" gnus-summary-resend-bounced-mail
279   ;; "c" gnus-summary-send-draft
280   "r" gnus-summary-resend-message)
281
282 ;;; Internal functions.
283
284 (defvar gnus-article-reply nil)
285 (defmacro gnus-setup-message (config &rest forms)
286   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
287         (buffer (make-symbol "gnus-setup-message-buffer"))
288         (article (make-symbol "gnus-setup-message-article"))
289         (group (make-symbol "gnus-setup-message-group")))
290     `(let ((,winconf (current-window-configuration))
291            (,buffer (buffer-name (current-buffer)))
292            (,article gnus-article-reply)
293            (,group gnus-newsgroup-name)
294            (message-header-setup-hook
295             (copy-sequence message-header-setup-hook))
296            (message-mode-hook (copy-sequence message-mode-hook))
297            (message-startup-parameter-alist
298             '((reply-buffer . gnus-copy-article-buffer)
299               (original-buffer . gnus-original-article-buffer)
300               (user-agent . Gnus))))
301        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
302        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
303        ;; #### FIXME: for a reason that I did not manage to identify yet,
304        ;; the variable `gnus-newsgroup-name' does not honor a dynamically
305        ;; scoped or setq'ed value from a caller like `C-u gnus-summary-mail'.
306        ;; After evaluation of @forms below, it gets the value we actually want
307        ;; to override, and the posting styles are used. For that reason, I've
308        ;; added an optional argument to `gnus-configure-posting-styles' to
309        ;; make sure that the correct value for the group name is used. -- drv
310        (add-hook 'message-mode-hook
311                  (lambda ()
312                    (gnus-configure-posting-styles ,group)))
313        (unwind-protect
314            (progn
315              ,@forms)
316          (gnus-inews-add-send-actions ,winconf ,buffer ,article ,config)
317          (gnus-inews-insert-draft-meta-information ,group ,article)
318          (setq gnus-message-buffer (current-buffer))
319          (set (make-local-variable 'gnus-message-group-art)
320               (cons ,group ,article))
321          (set (make-local-variable 'gnus-newsgroup-name) ,group)
322          (gnus-run-hooks 'gnus-message-setup-hook))
323        (gnus-add-buffer)
324        (gnus-configure-windows ,config t)
325        (run-hooks 'post-command-hook)
326        (set-buffer-modified-p nil))))
327
328 (defun gnus-inews-insert-draft-meta-information (group article)
329   (save-excursion
330     (when (and group
331                (not (string= group ""))
332                (not (message-fetch-field gnus-draft-meta-information-header)))
333       (goto-char (point-min))
334       (insert gnus-draft-meta-information-header ": (\"" group "\" "
335               (if article (number-to-string
336                            (if (listp article)
337                                (car article)
338                              article)) "\"\"")
339               ")\n"))))
340
341 ;;;###autoload
342 (defun gnus-msg-mail (&optional to subject other-headers continue
343                                 switch-action yank-action send-actions)
344   "Start editing a mail message to be sent.
345 Like `message-mail', but with Gnus paraphernalia, particularly the
346 Gcc: header for archiving purposes."
347   (interactive)
348   (let ((buf (current-buffer))
349         mail-buf)
350     (gnus-setup-message 'message
351       (message-mail to subject other-headers continue
352                     nil yank-action send-actions))
353     (when switch-action
354       (setq mail-buf (current-buffer))
355       (switch-to-buffer buf)
356       (apply switch-action mail-buf nil)))
357   ;; COMPOSEFUNC should return t if succeed.  Undocumented ???
358   t)
359
360 (defvar save-selected-window-window)
361
362 ;;;###autoload
363 (defun gnus-button-mailto (address)
364   "Mail to ADDRESS."
365   (set-buffer (gnus-copy-article-buffer))
366   (gnus-setup-message 'message
367     (message-reply address))
368   (and (boundp 'save-selected-window-window)
369        (not (window-live-p save-selected-window-window))
370        (setq save-selected-window-window (selected-window))))
371
372 ;;;###autoload
373 (defun gnus-button-reply (&optional to-address wide)
374   "Like `message-reply'."
375   (interactive)
376   (gnus-setup-message 'message
377     (message-reply to-address wide))
378   (and (boundp 'save-selected-window-window)
379        (not (window-live-p save-selected-window-window))
380        (setq save-selected-window-window (selected-window))))
381
382 ;;;###autoload
383 (define-mail-user-agent 'gnus-user-agent
384   'gnus-msg-mail 'message-send-and-exit
385   'message-kill-buffer 'message-send-hook)
386
387 (defun gnus-setup-posting-charset (group)
388   (let ((alist gnus-group-posting-charset-alist)
389         (group (or group ""))
390         elem)
391     (when group
392       (catch 'found
393         (while (setq elem (pop alist))
394           (when (or (and (stringp (car elem))
395                          (string-match (car elem) group))
396                     (and (gnus-functionp (car elem))
397                          (funcall (car elem) group))
398                     (and (symbolp (car elem))
399                          (symbol-value (car elem))))
400             (throw 'found (cons (cadr elem) (caddr elem)))))))))
401
402 (defun gnus-inews-add-send-actions (winconf buffer article &optional config)
403   (make-local-hook 'message-sent-hook)
404   (add-hook 'message-sent-hook (if gnus-agent 'gnus-agent-possibly-do-gcc
405                                  'gnus-inews-do-gcc) nil t)
406   (when gnus-agent
407     (make-local-hook 'message-header-hook)
408     (add-hook 'message-header-hook 'gnus-agent-possibly-save-gcc nil t))
409   (setq message-post-method
410         `(lambda (arg)
411            (gnus-post-method arg ,gnus-newsgroup-name)))
412   (setq message-user-agent (gnus-extended-version))
413   (when (not message-use-multi-frames)
414     (message-add-action
415      `(set-window-configuration ,winconf) 'exit 'postpone 'kill))
416   (message-add-action
417    `(when (gnus-buffer-exists-p ,buffer)
418       (save-excursion
419         (set-buffer ,buffer)
420         ,(when article
421            (if (eq config 'forward)
422                `(gnus-summary-mark-article-as-forwarded ',article)
423              `(gnus-summary-mark-article-as-replied ',article)))))
424    'send))
425
426 (put 'gnus-setup-message 'lisp-indent-function 1)
427 (put 'gnus-setup-message 'edebug-form-spec '(form body))
428
429 ;;; Post news commands of Gnus group mode and summary mode
430
431 (defun gnus-group-mail (&optional arg)
432   "Start composing a mail.
433 If ARG, use the group under the point to find a posting style.
434 If ARG is 1, prompt for a group name to find the posting style."
435   (interactive "P")
436   ;; We can't `let' gnus-newsgroup-name here, since that leads
437   ;; to local variables leaking.
438   (let ((group gnus-newsgroup-name)
439         (buffer (current-buffer)))
440     (unwind-protect
441         (progn
442           (setq gnus-newsgroup-name
443                 (if arg
444                     (if (= 1 (prefix-numeric-value arg))
445                         (completing-read "Use posting style of group: "
446                                          gnus-active-hashtb nil
447                                          (gnus-read-active-file-p))
448                       (gnus-group-group-name))
449                   ""))
450           ;; #### see comment in gnus-setup-message -- drv
451           (gnus-setup-message 'message (message-mail)))
452       (save-excursion
453         (set-buffer buffer)
454         (setq gnus-newsgroup-name group)))))
455
456 (defun gnus-group-news (&optional arg)
457   "Start composing a news.
458 If ARG, post to group under point.
459 If ARG is 1, prompt for group name to post to.
460
461 This function prepares a news even when using mail groups.  This is useful
462 for posting messages to mail groups without actually sending them over the
463 network.  The corresponding backend must have a 'request-post method."
464   (interactive "P")
465   ;; We can't `let' gnus-newsgroup-name here, since that leads
466   ;; to local variables leaking.
467   (let ((group gnus-newsgroup-name)
468         (buffer (current-buffer)))
469     (unwind-protect
470         (progn
471           (setq gnus-newsgroup-name
472                 (if arg
473                     (if (= 1 (prefix-numeric-value arg))
474                         (completing-read "Use group: "
475                                          gnus-active-hashtb nil
476                                          (gnus-read-active-file-p))
477                       (gnus-group-group-name))
478                   ""))
479           ;; #### see comment in gnus-setup-message -- drv
480           (gnus-setup-message 'message
481             (message-news (gnus-group-real-name gnus-newsgroup-name))))
482       (save-excursion
483         (set-buffer buffer)
484         (setq gnus-newsgroup-name group)))))
485
486 (defun gnus-group-post-news (&optional arg)
487   "Start composing a message (a news by default).
488 If ARG, post to group under point.  If ARG is 1, prompt for group name.
489 Depending on the selected group, the message might be either a mail or
490 a news."
491   (interactive "P")
492   ;; Bind this variable here to make message mode hooks work ok.
493   (let ((gnus-newsgroup-name
494          (if arg
495              (if (= 1 (prefix-numeric-value arg))
496                  (completing-read "Newsgroup: " gnus-active-hashtb nil
497                                   (gnus-read-active-file-p))
498                (gnus-group-group-name))
499            "")))
500     (gnus-post-news 'post gnus-newsgroup-name)))
501
502 (defun gnus-summary-mail-other-window (&optional arg)
503   "Start composing a mail in another window.
504 Use the posting of the current group by default.
505 If ARG, don't do that.  If ARG is 1, prompt for group name to find the
506 posting style."
507   (interactive "P")
508   ;; We can't `let' gnus-newsgroup-name here, since that leads
509   ;; to local variables leaking.
510   (let ((group gnus-newsgroup-name)
511         (buffer (current-buffer)))
512     (unwind-protect
513         (progn
514           (setq gnus-newsgroup-name
515                 (if arg
516                     (if (= 1 (prefix-numeric-value arg))
517                         (completing-read "Use group: "
518                                          gnus-active-hashtb nil
519                                          (gnus-read-active-file-p))
520                       "")
521                   gnus-newsgroup-name))
522           ;; #### see comment in gnus-setup-message -- drv
523           (gnus-setup-message 'message (message-mail)))
524       (save-excursion
525         (set-buffer buffer)
526         (setq gnus-newsgroup-name group)))))
527
528 (defun gnus-summary-news-other-window (&optional arg)
529   "Start composing a news in another window.
530 Post to the current group by default.
531 If ARG, don't do that.  If ARG is 1, prompt for group name to post to.
532
533 This function prepares a news even when using mail groups.  This is useful
534 for posting messages to mail groups without actually sending them over the
535 network.  The corresponding backend must have a 'request-post method."
536   (interactive "P")
537   ;; We can't `let' gnus-newsgroup-name here, since that leads
538   ;; to local variables leaking.
539   (let ((group gnus-newsgroup-name)
540         (buffer (current-buffer)))
541     (unwind-protect
542         (progn
543           (setq gnus-newsgroup-name
544                 (if arg
545                     (if (= 1 (prefix-numeric-value arg))
546                         (completing-read "Use group: "
547                                          gnus-active-hashtb nil
548                                          (gnus-read-active-file-p))
549                       "")
550                   gnus-newsgroup-name))
551           ;; #### see comment in gnus-setup-message -- drv
552           (gnus-setup-message 'message
553             (message-news (gnus-group-real-name gnus-newsgroup-name))))
554       (save-excursion
555         (set-buffer buffer)
556         (setq gnus-newsgroup-name group)))))
557
558 (defun gnus-summary-post-news (&optional arg)
559   "Start composing a message.  Post to the current group by default.
560 If ARG, don't do that.  If ARG is 1, prompt for a group name to post to.
561 Depending on the selected group, the message might be either a mail or
562 a news."
563   (interactive "P")
564   ;; Bind this variable here to make message mode hooks work ok.
565   (let ((gnus-newsgroup-name
566          (if arg
567              (if (= 1 (prefix-numeric-value arg))
568                  (completing-read "Newsgroup: " gnus-active-hashtb nil
569                                   (gnus-read-active-file-p))
570                "")
571            gnus-newsgroup-name)))
572     (gnus-post-news 'post gnus-newsgroup-name)))
573
574
575 (defun gnus-summary-followup (yank &optional force-news)
576   "Compose a followup to an article.
577 If prefix argument YANK is non-nil, the original article is yanked
578 automatically.
579 YANK is a list of elements, where the car of each element is the
580 article number, and the two following numbers is the region to be
581 yanked."
582   (interactive
583    (list (and current-prefix-arg
584               (gnus-summary-work-articles 1))))
585   (when yank
586     (gnus-summary-goto-subject
587      (if (listp (car yank))
588          (caar yank)
589        (car yank))))
590   (save-window-excursion
591     (gnus-summary-select-article))
592   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
593         (gnus-newsgroup-name gnus-newsgroup-name))
594     ;; Send a followup.
595     (gnus-post-news nil gnus-newsgroup-name
596                     headers gnus-article-buffer
597                     yank nil force-news)))
598
599 (defun gnus-summary-followup-with-original (n &optional force-news)
600   "Compose a followup to an article and include the original article."
601   (interactive "P")
602   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
603
604 (defun gnus-summary-followup-to-mail (&optional arg)
605   "Followup to the current mail message via news."
606   (interactive
607    (list (and current-prefix-arg
608               (gnus-summary-work-articles 1))))
609   (gnus-summary-followup arg t))
610
611 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
612   "Followup to the current mail message via news."
613   (interactive "P")
614   (gnus-summary-followup (gnus-summary-work-articles arg) t))
615
616 (defun gnus-inews-yank-articles (articles)
617   (let (beg article yank-string
618             (more-than-one (cdr articles))
619             (cur (current-buffer))
620             refs window)
621     (message-goto-body)
622     (while (setq article (pop articles))
623       (when (listp article)
624         (setq yank-string (nth 1 article)
625               article (nth 0 article)))
626       (save-window-excursion
627         (set-buffer gnus-summary-buffer)
628         (gnus-summary-select-article nil nil nil article)
629         (gnus-summary-remove-process-mark article))
630
631       ;; Gathering references.
632       (when more-than-one
633         (setq refs (message-list-references
634                     refs
635                     (mail-header-references gnus-current-headers)
636                     (mail-header-message-id gnus-current-headers)))
637         (when message-use-multi-frames
638           (when (setq window (get-buffer-window cur t))
639             (select-frame (window-frame window)))))
640
641       (gnus-copy-article-buffer nil yank-string)
642       (let ((message-reply-buffer gnus-article-copy)
643             (message-reply-headers
644              ;; The headers are decoded.
645              (with-current-buffer gnus-article-copy
646                (nnheader-parse-head t))))
647         (message-yank-original)
648         (setq beg (or beg (mark t))))
649       (when articles
650         (insert "\n")))
651     (push-mark)
652
653     ;; Replace with the gathered references.
654     (when refs
655       (push-mark beg)
656       (save-restriction
657         (message-narrow-to-headers)
658         (let ((case-fold-search t))
659           (if (re-search-forward "^References:\\([\t ]+.+\n\\)+" nil t)
660               (replace-match "")
661             (goto-char (point-max))))
662         (mail-header-format
663          (list (or (assq 'References message-header-format-alist)
664                    '(References . message-shorten-references)))
665          (list (cons 'References
666                      (mapconcat 'identity (nreverse refs) " "))))
667         (backward-delete-char 1))
668       (setq beg (mark t))
669       (pop-mark))
670
671     (goto-char beg)))
672
673 (defun gnus-summary-cancel-article (&optional n symp)
674   "Cancel an article you posted.
675 Uses the process-prefix convention.  If given the symbolic
676 prefix `a', cancel using the standard posting method; if not
677 post using the current select method."
678   (interactive (gnus-interactive "P\ny"))
679   (let ((articles (gnus-summary-work-articles n))
680         (message-post-method
681          `(lambda (arg)
682             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
683         article)
684     (while (setq article (pop articles))
685       (when (gnus-summary-select-article t nil nil article)
686         (when (gnus-eval-in-buffer-window gnus-article-buffer
687                 (save-excursion
688                   (set-buffer gnus-original-article-buffer)
689                   (message-cancel-news)))
690           (gnus-summary-mark-as-read article gnus-canceled-mark)
691           (gnus-cache-remove-article 1))
692         (gnus-article-hide-headers-if-wanted))
693       (gnus-summary-remove-process-mark article))))
694
695 (defun gnus-summary-supersede-article ()
696   "Compose an article that will supersede a previous article.
697 This is done simply by taking the old article and adding a Supersedes
698 header line with the old Message-ID."
699   (interactive)
700   (let ((article (gnus-summary-article-number))
701         (gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
702     (gnus-setup-message 'reply-yank
703       (gnus-summary-select-article t)
704       (set-buffer gnus-original-article-buffer)
705       (message-supersede)
706       (push
707        `((lambda ()
708            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
709              (save-excursion
710                (set-buffer ,gnus-summary-buffer)
711                (gnus-cache-possibly-remove-article ,article nil nil nil t)
712                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
713        message-send-actions))))
714
715 \f
716
717 (defun gnus-copy-article-buffer (&optional article-buffer yank-string)
718   ;; make a copy of the article buffer with all text properties removed
719   ;; this copy is in the buffer gnus-article-copy.
720   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
721   ;; this buffer should be passed to all mail/news reply/post routines.
722   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
723   (let ((article-buffer (or article-buffer gnus-article-buffer))
724         end beg)
725     (if (not (and (get-buffer article-buffer)
726                   (gnus-buffer-exists-p article-buffer)))
727         (error "Can't find any article buffer")
728       (save-excursion
729         (set-buffer article-buffer)
730         (let ((gnus-newsgroup-charset (or gnus-article-charset
731                                           gnus-newsgroup-charset))
732               (gnus-newsgroup-ignored-charsets
733                (or gnus-article-ignored-charsets
734                    gnus-newsgroup-ignored-charsets)))
735           (save-restriction
736             ;; Copy over the (displayed) article buffer, delete
737             ;; hidden text and remove text properties.
738             (widen)
739             (let ((inhibit-read-only t))
740               (copy-to-buffer gnus-article-copy (point-min) (point-max))
741               (set-buffer gnus-article-copy)
742               (when yank-string
743                 (message-goto-body)
744                 (delete-region (point) (point-max))
745                 (insert yank-string))
746               ;; Encode bitmap smileys to ordinary text.
747               ;; Possibly, the original text might be restored.
748               (static-unless (featurep 'xemacs)
749                 (when (featurep 'smiley-mule)
750                   (smiley-encode-buffer)))
751               (gnus-article-delete-text-of-type 'annotation)
752               (gnus-remove-text-with-property 'gnus-prev)
753               (gnus-remove-text-with-property 'gnus-next)
754               (gnus-remove-text-with-property 'x-face-mule-bitmap-image)
755               (insert
756                (prog1
757                    (static-if (featurep 'xemacs)
758                        ;; Revome smiley extents for (possibly) XEmacs 21.1.
759                        (format "%s"
760                                (buffer-substring-no-properties (point-min)
761                                                                (point-max)))
762                      (buffer-substring-no-properties (point-min) (point-max)))
763                  (erase-buffer))))
764             ;; Find the original headers.
765             (set-buffer gnus-original-article-buffer)
766             (goto-char (point-min))
767             (while (looking-at message-unix-mail-delimiter)
768               (forward-line 1))
769             (setq beg (point)
770                   end (or (message-goto-body) beg))
771             ;; Delete the headers from the displayed articles.
772             (set-buffer gnus-article-copy)
773             (delete-region (goto-char (point-min))
774                            (or (message-goto-body) (point-max)))
775             ;; Insert the original article headers.
776             (insert-buffer-substring gnus-original-article-buffer beg end)
777             ;; Decode charsets.
778             (let ((gnus-article-decode-hook
779                    (delq 'article-decode-charset
780                          (copy-sequence gnus-article-decode-hook))))
781               ;; Needed for T-gnus.
782               (add-hook 'gnus-article-decode-hook
783                         'article-decode-encoded-words)
784               (run-hooks 'gnus-article-decode-hook)))))
785       gnus-article-copy)))
786
787 (defun gnus-post-news (post &optional group header article-buffer yank subject
788                             force-news)
789   (when article-buffer
790     (gnus-copy-article-buffer))
791   (let ((gnus-article-reply (and article-buffer (gnus-summary-article-number)))
792         (add-to-list gnus-add-to-list))
793     (gnus-setup-message (cond (yank 'reply-yank)
794                               (article-buffer 'reply)
795                               (t 'message))
796       (let* ((group (or group gnus-newsgroup-name))
797              (charset (gnus-group-name-charset nil group))
798              (pgroup group)
799              to-address to-group mailing-list to-list
800              newsgroup-p)
801         (when group
802           (setq to-address (gnus-parameter-to-address group)
803                 to-group (gnus-group-find-parameter group 'to-group)
804                 to-list (gnus-parameter-to-list group)
805                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
806                 mailing-list (when gnus-mailing-list-groups
807                                (string-match gnus-mailing-list-groups group))
808                 group (gnus-group-name-decode (gnus-group-real-name group)
809                                               charset)))
810         (if (or (and to-group
811                      (gnus-news-group-p to-group))
812                 newsgroup-p
813                 force-news
814                 (and (gnus-news-group-p
815                       (or pgroup gnus-newsgroup-name)
816                       (or header gnus-current-article))
817                      (not mailing-list)
818                      (not to-list)
819                      (not to-address)))
820             ;; This is news.
821             (if post
822                 (message-news (or to-group group))
823               (set-buffer gnus-article-copy)
824               (gnus-msg-treat-broken-reply-to)
825               (message-followup (if (or newsgroup-p force-news)
826                                     (if (save-restriction
827                                           (article-narrow-to-head)
828                                           (message-fetch-field "newsgroups"))
829                                         nil
830                                       "")
831                                   to-group)))
832           ;; The is mail.
833           (if post
834               (progn
835                 (message-mail (or to-address to-list))
836                 ;; Arrange for mail groups that have no `to-address' to
837                 ;; get that when the user sends off the mail.
838                 (when (and (not to-list)
839                            (not to-address)
840                            add-to-list)
841                   (push (list 'gnus-inews-add-to-address pgroup)
842                         message-send-actions)))
843             (set-buffer gnus-article-copy)
844             (gnus-msg-treat-broken-reply-to)
845             (message-wide-reply to-address)))
846         (when yank
847           (gnus-inews-yank-articles yank))))))
848
849 (defun gnus-msg-treat-broken-reply-to (&optional force)
850   "Remove the Reply-to header iff broken-reply-to."
851   (when (or force
852             (gnus-group-find-parameter
853              gnus-newsgroup-name 'broken-reply-to))
854     (save-restriction
855       (message-narrow-to-head)
856       (message-remove-header "reply-to"))))
857
858 (defun gnus-post-method (arg group &optional silent)
859   "Return the posting method based on GROUP and ARG.
860 If SILENT, don't prompt the user."
861   (let ((gnus-post-method (or (gnus-parameter-post-method group)
862                               gnus-post-method))
863         (group-method (gnus-find-method-for-group group)))
864     (cond
865      ;; If the group-method is nil (which shouldn't happen) we use
866      ;; the default method.
867      ((null group-method)
868       (or (and (listp gnus-post-method) ;If not current/native/nil
869                (not (listp (car gnus-post-method))) ; and not a list of methods
870                gnus-post-method)        ;then use it.
871           gnus-select-method
872           message-post-method))
873      ;; We want the inverse of the default
874      ((and arg (not (eq arg 0)))
875       (if (eq gnus-post-method 'current)
876           gnus-select-method
877         group-method))
878      ;; We query the user for a post method.
879      ((or arg
880           (and (listp gnus-post-method)
881                (listp (car gnus-post-method))))
882       (let* ((methods
883               ;; Collect all methods we know about.
884               (append
885                (when (listp gnus-post-method)
886                  (if (listp (car gnus-post-method))
887                      gnus-post-method
888                    (list gnus-post-method)))
889                gnus-secondary-select-methods
890                (mapcar 'cdr gnus-server-alist)
891                (mapcar 'car gnus-opened-servers)
892                (list gnus-select-method)
893                (list group-method)))
894              method-alist post-methods method)
895         ;; Weed out all mail methods.
896         (while methods
897           (setq method (gnus-server-get-method "" (pop methods)))
898           (when (and (or (gnus-method-option-p method 'post)
899                          (gnus-method-option-p method 'post-mail))
900                      (not (member method post-methods)))
901             (push method post-methods)))
902         ;; Create a name-method alist.
903         (setq method-alist
904               (mapcar
905                (lambda (m)
906                  (if (equal (cadr m) "")
907                      (list (symbol-name (car m)) m)
908                    (list (concat (cadr m) " (" (symbol-name (car m)) ")") m)))
909                post-methods))
910         ;; Query the user.
911         (cadr
912          (assoc
913           (setq gnus-last-posting-server
914                 (if (and silent
915                          gnus-last-posting-server)
916                     ;; Just use the last value.
917                     gnus-last-posting-server
918                   (completing-read
919                    "Posting method: " method-alist nil t
920                    (cons (or gnus-last-posting-server "") 0))))
921           method-alist))))
922      ;; Override normal method.
923      ((and (eq gnus-post-method 'current)
924            (not (eq (car group-method) 'nndraft))
925            (gnus-get-function group-method 'request-post t))
926       (assert (not arg))
927       group-method)
928      ;; Use gnus-post-method.
929      ((listp gnus-post-method)          ;A method...
930       (assert (not (listp (car gnus-post-method)))) ;... not a list of methods.
931       gnus-post-method)
932      ;; Use the normal select method (nil or native).
933      (t gnus-select-method))))
934
935 \f
936 (defun gnus-message-make-user-agent (&optional include-mime-info max-column
937                                                  newline-product)
938   "Return a user-agent info.  If INCLUDE-MIME-INFO is non-nil and the
939 variable `mime-edit-user-agent-value' is bound, the value will be
940 included in the return value.  If MAX-COLUMN is specified, the return
941 value will be folded up as it were filled.  NEWLINE-PRODUCT specifies
942 whether a newline should be inserted in front of each product-token.
943 If the value is t or `hard', it works strictly.  Otherwise, if it is
944 non-nil (e.g. `soft'), it works semi-strictly.
945
946 Here is an example of how to use this function:
947
948 \(add-hook 'gnus-message-setup-hook
949           (lambda nil
950             (setq message-user-agent nil)
951             (save-excursion
952               (save-restriction
953                 (message-narrow-to-headers)
954                 (goto-char (point-max))
955                 (insert \"User-Agent: \"
956                         (gnus-message-make-user-agent t 76 'soft)
957                         \"\\n\")))))
958 "
959   (let ((user-agent (if (and include-mime-info
960                              (boundp 'mime-edit-user-agent-value))
961                         (concat (gnus-extended-version)
962                                 " "
963                                 mime-edit-user-agent-value)
964                       (gnus-extended-version))))
965     (when max-column
966       (unless (natnump max-column)
967         (setq max-column 76))
968       (with-temp-buffer
969         (set-buffer-multibyte t)
970         (insert (mapconcat 'identity (split-string user-agent) " "))
971         (goto-char (point-min))
972         (let ((bol t)
973               start agent agents width element swidth)
974           (while (re-search-forward "\\([^ ]+\\) ?" nil t)
975             (setq start (match-beginning 0))
976             (if (eq (char-after start) ?\()
977                 (progn
978                   (goto-char start)
979                   (forward-list)
980                   (push (buffer-substring start (point)) agent))
981               (when agent
982                 (push (nreverse agent) agents))
983               (setq agent (list (match-string 1)))))
984           (when agent
985             (push (nreverse agent) agents))
986           (setq agents (nreverse agents))
987           (if (> (+ 12 (string-width (caar agents))) max-column)
988               (setq user-agent "\n"
989                     width 0)
990             (setq user-agent ""
991                   width 11))
992           (while agents
993             (setq agent (car agents)
994                   agents (cdr agents))
995             (when (and (not bol)
996                        (or (memq newline-product '(t hard))
997                            (and newline-product
998                                 (> (+ width 1
999                                       (string-width (mapconcat 'identity
1000                                                                agent " ")))
1001                                    max-column))))
1002               (setq user-agent (concat user-agent "\n")
1003                     width 0
1004                     bol t))
1005             (while agent
1006               (setq element (car agent)
1007                     swidth (string-width element)
1008                     agent (cdr agent))
1009               (if bol
1010                   (setq user-agent (if (member user-agent '("" "\n"))
1011                                        (concat user-agent element)
1012                                      (concat user-agent " " element))
1013                         width (+ width 1 swidth)
1014                         bol nil)
1015                 (if (> (+ width 1 swidth) max-column)
1016                     (setq user-agent (concat user-agent "\n " element)
1017                           width (1+ swidth))
1018                   (setq user-agent (concat user-agent " " element)
1019                         width (+ width 1 swidth)))))))))
1020     user-agent))
1021
1022 \f
1023 ;;;
1024 ;;; Gnus Mail Functions
1025 ;;;
1026
1027 ;;; Mail reply commands of Gnus summary mode
1028
1029 (defun gnus-summary-reply (&optional yank wide very-wide)
1030   "Start composing a mail reply to the current message.
1031 If prefix argument YANK is non-nil, the original article is yanked
1032 automatically.
1033 If WIDE, make a wide reply.
1034 If VERY-WIDE, make a very wide reply."
1035   (interactive
1036    (list (and current-prefix-arg
1037               (gnus-summary-work-articles 1))))
1038   (let* ((article
1039           (if (listp (car yank))
1040               (caar yank)
1041             (car yank)))
1042          (gnus-article-reply (or article (gnus-summary-article-number)))
1043          (headers ""))
1044     ;; Stripping headers should be specified with mail-yank-ignored-headers.
1045     (when yank
1046       (gnus-summary-goto-subject article))
1047     (gnus-setup-message (if yank 'reply-yank 'reply)
1048       (if (not very-wide)
1049           (gnus-summary-select-article)
1050         (dolist (article very-wide)
1051           (gnus-summary-select-article nil nil nil article)
1052           (save-excursion
1053             (set-buffer (gnus-copy-article-buffer))
1054             (gnus-msg-treat-broken-reply-to)
1055             (save-restriction
1056               (message-narrow-to-head)
1057               (setq headers (concat headers (buffer-string)))))))
1058       (set-buffer (gnus-copy-article-buffer))
1059       (gnus-msg-treat-broken-reply-to gnus-msg-force-broken-reply-to)
1060       (save-restriction
1061         (message-narrow-to-head)
1062         (when very-wide
1063           (erase-buffer)
1064           (insert headers))
1065         (goto-char (point-max)))
1066       (message-reply nil wide)
1067       (when yank
1068         (gnus-inews-yank-articles yank)))))
1069
1070 (defun gnus-summary-reply-with-original (n &optional wide)
1071   "Start composing a reply mail to the current message.
1072 The original article will be yanked."
1073   (interactive "P")
1074   (gnus-summary-reply (gnus-summary-work-articles n) wide))
1075
1076 (defun gnus-summary-reply-broken-reply-to (&optional yank wide very-wide)
1077   "Like `gnus-summary-reply' except removing reply-to field.
1078 If prefix argument YANK is non-nil, the original article is yanked
1079 automatically.
1080 If WIDE, make a wide reply.
1081 If VERY-WIDE, make a very wide reply."
1082   (interactive
1083    (list (and current-prefix-arg
1084               (gnus-summary-work-articles 1))))
1085   (let ((gnus-msg-force-broken-reply-to t))
1086     (gnus-summary-reply yank wide very-wide)))
1087
1088 (defun gnus-summary-reply-broken-reply-to-with-original (n &optional wide)
1089   "Like `gnus-summary-reply-with-original' except removing reply-to field.
1090 The original article will be yanked."
1091   (interactive "P")
1092   (gnus-summary-reply-broken-reply-to (gnus-summary-work-articles n) wide))
1093
1094 (defun gnus-summary-wide-reply (&optional yank)
1095   "Start composing a wide reply mail to the current message.
1096 If prefix argument YANK is non-nil, the original article is yanked
1097 automatically."
1098   (interactive
1099    (list (and current-prefix-arg
1100               (gnus-summary-work-articles 1))))
1101   (gnus-summary-reply yank t))
1102
1103 (defun gnus-summary-wide-reply-with-original (n)
1104   "Start composing a wide reply mail to the current message.
1105 The original article will be yanked."
1106   (interactive "P")
1107   (gnus-summary-reply-with-original n t))
1108
1109 (defun gnus-summary-very-wide-reply (&optional yank)
1110   "Start composing a very wide reply mail to the current message.
1111 If prefix argument YANK is non-nil, the original article is yanked
1112 automatically."
1113   (interactive
1114    (list (and current-prefix-arg
1115               (gnus-summary-work-articles 1))))
1116   (gnus-summary-reply yank t (gnus-summary-work-articles yank)))
1117
1118 (defun gnus-summary-very-wide-reply-with-original (n)
1119   "Start composing a very wide reply mail to the current message.
1120 The original article will be yanked."
1121   (interactive "P")
1122   (gnus-summary-reply
1123    (gnus-summary-work-articles n) t (gnus-summary-work-articles n)))
1124
1125 (defun gnus-summary-mail-forward (&optional full-headers post)
1126   "Forward the current message(s) to another user.
1127 If process marks exist, forward all marked messages;
1128 If FULL-HEADERS (the prefix), include full headers when forwarding."
1129   (interactive "P")
1130   (if (null (cdr (gnus-summary-work-articles nil)))
1131       (gnus-setup-message 'forward
1132         (gnus-summary-select-article)
1133         (let ((charset default-mime-charset))
1134           (set-buffer gnus-original-article-buffer)
1135           (make-local-variable 'default-mime-charset)
1136           (setq default-mime-charset charset))
1137         (let ((message-included-forward-headers
1138                (if full-headers "" message-included-forward-headers)))
1139           (message-forward post)))
1140     (gnus-summary-digest-mail-forward nil post)))
1141
1142 (defun gnus-summary-digest-mail-forward (&optional n post)
1143   "Digests and forwards all articles in this series.
1144 If N is a positive number, forward the N next articles.
1145 If N is a negative number, forward the N previous articles.
1146 If N is nil and any articles have been marked with the process mark,
1147 forward those articles instead.
1148 Optional POST will use news to forward instead of mail."
1149   (interactive "P")
1150   (let ((articles (gnus-summary-work-articles n))
1151         (topics "Topics:\n")
1152         subject article frame)
1153     (when (car articles)
1154       (gnus-setup-message 'forward
1155         (gnus-summary-select-article)
1156         (if (cdr articles)
1157             (setq articles (sort articles '<)
1158                   subject "Digested Articles")
1159           (with-current-buffer gnus-original-article-buffer
1160             (setq subject (message-make-forward-subject))))
1161         (if post
1162             (message-news nil subject)
1163           (message-mail nil subject))
1164         (when (and message-use-multi-frames (cdr articles))
1165           (setq frame (window-frame (get-buffer-window (current-buffer)))))
1166         (message-goto-body)
1167         (while (setq article (pop articles))
1168           (save-window-excursion
1169             (set-buffer gnus-summary-buffer)
1170             (gnus-summary-select-article nil nil nil article)
1171             (setq topics (concat topics "    "
1172                                  (mail-header-subject gnus-current-headers)
1173                                  "\n"))
1174             (gnus-summary-remove-process-mark article))
1175           (when frame
1176             (select-frame frame))
1177           (insert (mime-make-tag "message" "rfc822") "\n")
1178           (narrow-to-region (point) (point))
1179           (insert-buffer-substring gnus-original-article-buffer)
1180           (save-restriction
1181             (article-narrow-to-head)
1182             (message-remove-header message-included-forward-headers t nil t))
1183           (goto-char (point-max))
1184           (widen))
1185         (push-mark)
1186         (message-goto-body)
1187         (insert topics)
1188         (message-goto-body)
1189         (mime-edit-enclose-digest-region (point)(mark t))))))
1190
1191 (defun gnus-summary-digest-post-forward (&optional n)
1192   "Digest and forwards all articles in this series to a newsgroup.
1193 If N is a positive number, forward the N next articles.
1194 If N is a negative number, forward the N previous articles.
1195 If N is nil and any articles have been marked with the process mark,
1196 forward those articles instead."
1197   (interactive "P")
1198   (gnus-summary-digest-mail-forward n t))
1199
1200 (defun gnus-summary-resend-message (address n)
1201   "Resend the current article to ADDRESS."
1202   (interactive
1203    (list (message-read-from-minibuffer 
1204           "Resend message(s) to: "
1205           (when (gnus-buffer-live-p gnus-original-article-buffer)
1206             ;; If some other article is currently selected, the
1207             ;; initial-contents is wrong. Whatever, it is just the
1208             ;; initial-contents.
1209             (with-current-buffer gnus-original-article-buffer
1210               (nnmail-fetch-field "to"))))
1211          current-prefix-arg))
1212   (let ((articles (gnus-summary-work-articles n))
1213         article)
1214     (while (setq article (pop articles))
1215       (gnus-summary-select-article nil nil nil article)
1216       (save-excursion
1217         (set-buffer gnus-original-article-buffer)
1218         (message-resend address))
1219       (gnus-summary-mark-article-as-forwarded article))))
1220
1221 (defun gnus-summary-post-forward (&optional full-headers)
1222   "Forward the current article to a newsgroup.
1223 If FULL-HEADERS (the prefix), include full headers when forwarding."
1224   (interactive "P")
1225   (gnus-summary-mail-forward full-headers t))
1226
1227 (defvar gnus-nastygram-message
1228   "The following article was inappropriately posted to %s.\n\n"
1229   "Format string to insert in nastygrams.
1230 The current group name will be inserted at \"%s\".")
1231
1232 (defun gnus-summary-mail-nastygram (n)
1233   "Send a nastygram to the author of the current article."
1234   (interactive "P")
1235   (when (or gnus-expert-user
1236             (gnus-y-or-n-p
1237              "Really send a nastygram to the author of the current article? "))
1238     (let ((group gnus-newsgroup-name))
1239       (gnus-summary-reply-with-original n)
1240       (set-buffer gnus-message-buffer)
1241       (message-goto-body)
1242       (insert (format gnus-nastygram-message group))
1243       (message-send-and-exit))))
1244
1245 (defun gnus-summary-mail-crosspost-complaint (n)
1246   "Send a complaint about crossposting to the current article(s)."
1247   (interactive "P")
1248   (let ((articles (gnus-summary-work-articles n))
1249         article)
1250     (while (setq article (pop articles))
1251       (set-buffer gnus-summary-buffer)
1252       (gnus-summary-goto-subject article)
1253       (let ((group (gnus-group-real-name gnus-newsgroup-name))
1254             newsgroups followup-to)
1255         (gnus-summary-select-article)
1256         (set-buffer gnus-original-article-buffer)
1257         (if (and (<= (length (message-tokenize-header
1258                               (setq newsgroups
1259                                     (mail-fetch-field "newsgroups"))
1260                               ", "))
1261                      1)
1262                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
1263                      (not (member group (message-tokenize-header
1264                                          followup-to ", ")))))
1265             (if followup-to
1266                 (gnus-message 1 "Followup-to restricted")
1267               (gnus-message 1 "Not a crossposted article"))
1268           (set-buffer gnus-summary-buffer)
1269           (gnus-summary-reply-with-original 1)
1270           (set-buffer gnus-message-buffer)
1271           (message-goto-body)
1272           (insert (format gnus-crosspost-complaint newsgroups group))
1273           (message-goto-subject)
1274           (re-search-forward " *$")
1275           (replace-match " (crosspost notification)" t t)
1276           (gnus-deactivate-mark)
1277           (when (gnus-y-or-n-p "Send this complaint? ")
1278             (message-send-and-exit)))))))
1279
1280 (defun gnus-mail-parse-comma-list ()
1281   (let (accumulated
1282         beg)
1283     (skip-chars-forward " ")
1284     (while (not (eobp))
1285       (setq beg (point))
1286       (skip-chars-forward "^,")
1287       (while (zerop
1288               (save-excursion
1289                 (save-restriction
1290                   (let ((i 0))
1291                     (narrow-to-region beg (point))
1292                     (goto-char beg)
1293                     (logand (progn
1294                               (while (search-forward "\"" nil t)
1295                                 (incf i))
1296                               (if (zerop i) 2 i))
1297                             2)))))
1298         (skip-chars-forward ",")
1299         (skip-chars-forward "^,"))
1300       (skip-chars-backward " ")
1301       (push (buffer-substring beg (point))
1302             accumulated)
1303       (skip-chars-forward "^,")
1304       (skip-chars-forward ", "))
1305     accumulated))
1306
1307 (defun gnus-inews-add-to-address (group)
1308   (let ((to-address (mail-fetch-field "to")))
1309     (when (and to-address
1310                (gnus-alive-p))
1311       ;; This mail group doesn't have a `to-list', so we add one
1312       ;; here.  Magic!
1313       (when (gnus-y-or-n-p
1314              (format "Do you want to add this as `to-list': %s " to-address))
1315         (gnus-group-add-parameter group (cons 'to-list to-address))))))
1316
1317 (defun gnus-put-message ()
1318   "Put the current message in some group and return to Gnus."
1319   (interactive)
1320   (let ((reply gnus-article-reply)
1321         (winconf gnus-prev-winconf)
1322         (group gnus-newsgroup-name))
1323     (unless (and group
1324                  (not (gnus-group-read-only-p group)))
1325       (setq group (read-string "Put in group: " nil (gnus-writable-groups))))
1326
1327     (when (gnus-gethash group gnus-newsrc-hashtb)
1328       (error "No such group: %s" group))
1329     (save-excursion
1330       (save-restriction
1331         (widen)
1332         (message-narrow-to-headers)
1333         (let ((gnus-deletable-headers nil))
1334           (message-generate-headers
1335            (if (message-news-p)
1336                message-required-news-headers
1337              message-required-mail-headers)))
1338         (goto-char (point-max))
1339         (insert "Gcc: " group "\n")
1340         (widen)))
1341     (gnus-inews-do-gcc)
1342     (when (and (get-buffer gnus-group-buffer)
1343                (gnus-buffer-exists-p (car-safe reply))
1344                (cdr reply))
1345       (set-buffer (car reply))
1346       (gnus-summary-mark-article-as-replied (cdr reply)))
1347     (when winconf
1348       (set-window-configuration winconf))))
1349
1350 (defun gnus-article-mail (yank)
1351   "Send a reply to the address near point.
1352 If YANK is non-nil, include the original article."
1353   (interactive "P")
1354   (let ((address
1355          (buffer-substring
1356           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
1357           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
1358     (when address
1359       (message-reply address)
1360       (when yank
1361         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
1362
1363 (defvar nntp-server-type)
1364 (defun gnus-bug ()
1365   "Send a bug report to the Gnus maintainers."
1366   (interactive)
1367   (unless (gnus-alive-p)
1368     (error "Gnus has been shut down"))
1369   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
1370     (unless (message-mail-user-agent)
1371       (message-pop-to-buffer "*Gnus Bug*")
1372       (delete-other-windows)
1373       (when gnus-bug-create-help-buffer
1374         (switch-to-buffer "*Gnus Help Bug*")
1375         (erase-buffer)
1376         (insert gnus-bug-message)
1377         (goto-char (point-min))
1378         (sit-for 0)
1379         (set-buffer "*Gnus Bug*")))
1380     (let ((message-this-is-mail t))
1381       (message-setup `((To . ,gnus-maintainer) (Subject . ""))))
1382     (when gnus-bug-create-help-buffer
1383       (push `(gnus-bug-kill-buffer) message-send-actions))
1384     (goto-char (point-min))
1385     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1386     (forward-line 1)
1387     (insert gnus-product-name " " gnus-version-number
1388             " (r" gnus-revision-number ") "
1389             "based on " gnus-original-product-name " v"
1390             gnus-original-version-number "\n"
1391             (emacs-version) "\n")
1392     (when (and (boundp 'nntp-server-type)
1393                (stringp nntp-server-type))
1394       (insert nntp-server-type))
1395     (insert "\n\n\n\n\n")
1396     (let (mime-content-types)
1397       (mime-edit-insert-tag
1398        "application" "emacs-lisp"
1399        "\nContent-Disposition: inline\nContent-Description: User settings"))
1400     (insert (with-temp-buffer
1401               (gnus-debug)
1402               (buffer-string)))
1403     (let (mime-content-types)
1404       (mime-edit-insert-tag "text" "plain"))
1405     (goto-char (point-min))
1406     (search-forward "Subject: " nil t)
1407     (message "")))
1408
1409 (defun gnus-bug-kill-buffer ()
1410   (when (get-buffer "*Gnus Help Bug*")
1411     (kill-buffer "*Gnus Help Bug*")))
1412
1413 (defun gnus-summary-yank-message (buffer n)
1414   "Yank the current article into a composed message."
1415   (interactive
1416    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
1417          current-prefix-arg))
1418   (when (gnus-buffer-live-p buffer)
1419     (let ((summary-frame (selected-frame))
1420           (message-frame (when (static-if (featurep 'xemacs)
1421                                    (device-on-window-system-p)
1422                                  window-system)
1423                            (let ((window (get-buffer-window buffer t)))
1424                              (when window
1425                                (window-frame window)))))
1426           (separator (concat "^" (regexp-quote mail-header-separator)
1427                              "\n")))
1428       (gnus-summary-iterate n
1429         (gnus-summary-select-article)
1430         (gnus-copy-article-buffer)
1431         (when (frame-live-p message-frame)
1432           (raise-frame message-frame)
1433           (select-frame message-frame))
1434         (with-current-buffer buffer
1435           (when (save-excursion
1436                   (beginning-of-line)
1437                   (let (case-fold-search)
1438                     (and (not (re-search-backward separator nil t))
1439                          (re-search-forward separator nil t))))
1440             (goto-char (match-end 0)))
1441           (message-yank-buffer gnus-article-copy))
1442         (select-frame summary-frame))
1443       (when (frame-live-p message-frame)
1444         (select-frame message-frame)))))
1445
1446 (defun gnus-debug ()
1447   "Attempts to go through the Gnus source file and report what variables have been changed.
1448 The source file has to be in the Emacs load path."
1449   (interactive)
1450   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
1451                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
1452                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
1453                  "nnmail.el" "nntp.el" "message.el"))
1454         (point (point))
1455         file expr olist sym)
1456     (gnus-message 4 "Please wait while we snoop your variables...")
1457     ;; Go through all the files looking for non-default values for variables.
1458     (save-excursion
1459       (sit-for 0)
1460       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1461       (while files
1462         (erase-buffer)
1463         (when (and (setq file (locate-library (pop files)))
1464                    (file-exists-p file))
1465           (insert-file-contents file)
1466           (goto-char (point-min))
1467           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1468               (gnus-message 4 "Malformed sources in file %s" file)
1469             (narrow-to-region (point-min) (point))
1470             (goto-char (point-min))
1471             (while (setq expr (ignore-errors (read (current-buffer))))
1472               (ignore-errors
1473                 (and (memq (car expr) '(defvar defcustom defvoo))
1474                      (stringp (nth 3 expr))
1475                      (or (not (boundp (nth 1 expr)))
1476                          (not (equal (eval (nth 2 expr))
1477                                      (symbol-value (nth 1 expr)))))
1478                      (push (nth 1 expr) olist)))))))
1479       (kill-buffer (current-buffer)))
1480     (when (setq olist (nreverse olist))
1481       (insert ";----------------- Environment follows ------------------\n\n"))
1482     (while olist
1483       (if (boundp (car olist))
1484           (condition-case ()
1485               (pp `(setq ,(car olist)
1486                          ,(if (or (consp (setq sym (symbol-value (car olist))))
1487                                   (and (symbolp sym)
1488                                        (not (or (eq sym nil)
1489                                                 (eq sym t)))))
1490                               (list 'quote (symbol-value (car olist)))
1491                             (symbol-value (car olist))))
1492                   (current-buffer))
1493             (error
1494              (format "(setq %s 'whatever)\n" (car olist))))
1495         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1496       (setq olist (cdr olist)))
1497     ;; Remove any control chars - they seem to cause trouble for some
1498     ;; mailers.  (Byte-compiled output from the stuff above.)
1499     (goto-char point)
1500     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1501       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1502                      t t))
1503     ;; Break MIME tags purposely.
1504     (goto-char point)
1505     (while (re-search-forward mime-edit-tag-regexp nil t)
1506       (goto-char (1+ (match-beginning 0)))
1507       (insert "X"))))
1508
1509 ;;; Treatment of rejected articles.
1510 ;;; Bounced mail.
1511
1512 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1513   "Re-mail the current message.
1514 This only makes sense if the current message is a bounce message than
1515 contains some mail you have written which has been bounced back to
1516 you.
1517 If FETCH, try to fetch the article that this is a reply to, if indeed
1518 this is a reply."
1519   (interactive "P")
1520   (gnus-summary-select-article t)
1521   (set-buffer gnus-original-article-buffer)
1522   (let ((gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
1523     (gnus-setup-message 'compose-bounce
1524       (let* ((references (mail-fetch-field "references"))
1525              (parent (and references (gnus-parent-id references))))
1526         (message-bounce)
1527         ;; If there are references, we fetch the article we answered to.
1528         (and fetch parent
1529              (gnus-summary-refer-article parent)
1530              (gnus-summary-show-all-headers))))))
1531
1532 ;;; Gcc handling.
1533
1534 (defun gnus-inews-group-method (group)
1535   (cond
1536    ;; If the group doesn't exist, we assume
1537    ;; it's an archive group...
1538    ((and (null (gnus-get-info group))
1539          (eq (car (gnus-server-to-method gnus-message-archive-method))
1540              (car (gnus-server-to-method (gnus-group-method group)))))
1541     gnus-message-archive-method)
1542    ;; Use the method.
1543    ((gnus-info-method (gnus-get-info group))
1544     (gnus-info-method (gnus-get-info group)))
1545    ;; Find the method.
1546    (t (gnus-server-to-method (gnus-group-method group)))))
1547
1548 ;; Do Gcc handling, which copied the message over to some group.
1549 (defun gnus-inews-do-gcc (&optional gcc)
1550   (interactive)
1551   (save-excursion
1552     (save-restriction
1553       (message-narrow-to-headers)
1554       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1555             (coding-system-for-write 'raw-text)
1556             (output-coding-system 'raw-text)
1557             groups group method group-art
1558             mml-externalize-attachments)
1559         (when gcc
1560           (message-remove-header "gcc")
1561           (widen)
1562           (setq groups (message-unquote-tokens
1563                         (message-tokenize-header gcc " ,")))
1564           ;; Copy the article over to some group(s).
1565           (while (setq group (pop groups))
1566             (unless (gnus-check-server
1567                      (setq method (gnus-inews-group-method group)))
1568               (error "Can't open server %s" (if (stringp method) method
1569                                               (car method))))
1570             (unless (gnus-request-group group nil method)
1571               (gnus-request-create-group group method))
1572             (setq mml-externalize-attachments
1573                   (if (stringp gnus-gcc-externalize-attachments)
1574                       (string-match gnus-gcc-externalize-attachments group)
1575                     gnus-gcc-externalize-attachments))
1576             (save-excursion
1577               (nnheader-set-temp-buffer " *acc*")
1578               (insert-buffer-substring message-encoding-buffer)
1579               (gnus-run-hooks 'gnus-before-do-gcc-hook)
1580               (goto-char (point-min))
1581               (when (re-search-forward
1582                      (concat "^" (regexp-quote mail-header-separator) "$")
1583                      nil t)
1584                 (replace-match "" t t ))
1585               (unless (setq group-art
1586                             (gnus-request-accept-article group method t t))
1587                 (gnus-message 1 "Couldn't store article in group %s: %s"
1588                               group (gnus-status-message method))
1589                 (sit-for 2))
1590               (when (and group-art
1591                          (or gnus-gcc-mark-as-read
1592                              gnus-inews-mark-gcc-as-read))
1593                 (gnus-group-mark-article-read group (cdr group-art)))
1594               (kill-buffer (current-buffer)))))))))
1595
1596 (defun gnus-inews-insert-gcc ()
1597   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1598   (save-excursion
1599     (save-restriction
1600       (message-narrow-to-headers)
1601       (let* ((group gnus-outgoing-message-group)
1602              (gcc (cond
1603                    ((gnus-functionp group)
1604                     (funcall group))
1605                    ((or (stringp group) (list group))
1606                     group))))
1607         (when gcc
1608           (insert "Gcc: "
1609                   (if (stringp gcc) gcc
1610                     (mapconcat 'identity gcc " "))
1611                   "\n"))))))
1612
1613 (defun gnus-inews-insert-archive-gcc (&optional group)
1614   "Insert the Gcc to say where the article is to be archived."
1615   (let* ((var gnus-message-archive-group)
1616          (group (or group gnus-newsgroup-name ""))
1617          (gcc-self-val
1618           (and gnus-newsgroup-name
1619                (not (equal gnus-newsgroup-name ""))
1620                (gnus-group-find-parameter
1621                 gnus-newsgroup-name 'gcc-self)))
1622          result
1623          (groups
1624           (cond
1625            ((null gnus-message-archive-method)
1626             ;; Ignore.
1627             nil)
1628            ((stringp var)
1629             ;; Just a single group.
1630             (list var))
1631            ((null var)
1632             ;; We don't want this.
1633             nil)
1634            ((and (listp var) (stringp (car var)))
1635             ;; A list of groups.
1636             var)
1637            ((gnus-functionp var)
1638             ;; A function.
1639             (funcall var group))
1640            (t
1641             ;; An alist of regexps/functions/forms.
1642             (while (and var
1643                         (not
1644                          (setq result
1645                                (cond
1646                                 ((stringp (caar var))
1647                                  ;; Regexp.
1648                                  (when (string-match (caar var) group)
1649                                    (cdar var)))
1650                                 ((gnus-functionp (car var))
1651                                  ;; Function.
1652                                  (funcall (car var) group))
1653                                 (t
1654                                  (eval (car var)))))))
1655               (setq var (cdr var)))
1656             result)))
1657          name)
1658     (when (or groups gcc-self-val)
1659       (when (stringp groups)
1660         (setq groups (list groups)))
1661       (save-excursion
1662         (save-restriction
1663           (message-narrow-to-headers)
1664           (goto-char (point-max))
1665           (insert "Gcc: ")
1666           (if gcc-self-val
1667               ;; Use the `gcc-self' param value instead.
1668               (progn
1669                 (insert
1670                  (if (stringp gcc-self-val)
1671                      gcc-self-val
1672                    group))
1673                 (if (not (eq gcc-self-val 'none))
1674                     (insert "\n")
1675                   (progn
1676                     (beginning-of-line)
1677                     (kill-line))))
1678             ;; Use the list of groups.
1679             (while (setq name (pop groups))
1680               (insert (if (string-match ":" name)
1681                           name
1682                         (gnus-group-prefixed-name
1683                          name gnus-message-archive-method)))
1684               (when groups
1685                 (insert " ")))
1686             (insert "\n")))))))
1687
1688 ;;; Posting styles.
1689
1690 (defun gnus-configure-posting-styles (&optional group-name)
1691   "Configure posting styles according to `gnus-posting-styles'."
1692   (unless gnus-inhibit-posting-styles
1693     (let ((group (or group-name gnus-newsgroup-name ""))
1694           (styles gnus-posting-styles)
1695           style match variable attribute value v results
1696           filep name address element)
1697       ;; If the group has a posting-style parameter, add it at the end with a
1698       ;; regexp matching everything, to be sure it takes precedence over all
1699       ;; the others.
1700       (when gnus-newsgroup-name
1701         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1702           (when tmp-style
1703             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1704       ;; Go through all styles and look for matches.
1705       (dolist (style styles)
1706         (setq match (pop style))
1707         (goto-char (point-min))
1708         (when (cond
1709                ((stringp match)
1710                 ;; Regexp string match on the group name.
1711                 (string-match match group))
1712                ((eq match 'header)
1713                 (and (gnus-buffer-live-p gnus-article-copy)
1714                      (with-current-buffer gnus-article-copy
1715                        (let ((header (message-fetch-field (pop style))))
1716                          (and header
1717                               (string-match (pop style) header))))))
1718                ((or (symbolp match)
1719                     (gnus-functionp match))
1720                 (cond
1721                  ((gnus-functionp match)
1722                   ;; Function to be called.
1723                   (funcall match))
1724                  ((boundp match)
1725                   ;; Variable to be checked.
1726                   (symbol-value match))))
1727                ((listp match)
1728                 ;; This is a form to be evaled.
1729                 (eval match)))
1730           ;; We have a match, so we set the variables.
1731           (dolist (attribute style)
1732             (setq element (pop attribute)
1733                   variable nil
1734                   filep nil)
1735             (setq value
1736                   (cond
1737                    ((eq (car attribute) ':file)
1738                     (setq filep t)
1739                     (cadr attribute))
1740                    ((eq (car attribute) :value)
1741                     (cadr attribute))
1742                    (t
1743                     (car attribute))))
1744             ;; We get the value.
1745             (setq v
1746                   (cond
1747                    ((stringp value)
1748                     value)
1749                    ((or (symbolp value)
1750                         (gnus-functionp value))
1751                     (cond ((gnus-functionp value)
1752                            (funcall value))
1753                           ((boundp value)
1754                            (symbol-value value))))
1755                    ((listp value)
1756                     (eval value))))
1757             ;; Translate obsolescent value.
1758             (when (eq element 'signature-file)
1759               (setq element 'signature
1760                     filep t))
1761             ;; Get the contents of file elems.
1762             (when (and filep v)
1763               (setq v (with-temp-buffer
1764                         (insert-file-contents v)
1765                         (buffer-string))))
1766             (setq results (delq (assoc element results) results))
1767             (push (cons element v) results))))
1768       ;; Now we have all the styles, so we insert them.
1769       (setq name (assq 'name results)
1770             address (assq 'address results))
1771       (setq results (delq name (delq address results)))
1772       ;; make-local-hook is not obsolete in Emacs 20 or XEmacs.
1773       (make-local-hook 'message-setup-hook)
1774       (dolist (result results)
1775         (add-hook 'message-setup-hook
1776                   (cond
1777                    ((eq 'eval (car result))
1778                     'ignore)
1779                    ((eq 'body (car result))
1780                     `(lambda ()
1781                        (save-excursion
1782                          (message-goto-body)
1783                          (insert ,(cdr result)))))
1784                    ((eq 'signature (car result))
1785                     (set (make-local-variable 'message-signature) nil)
1786                     (set (make-local-variable 'message-signature-file) nil)
1787                     (if (not (cdr result))
1788                         'ignore
1789                       `(lambda ()
1790                          (save-excursion
1791                            (let ((message-signature ,(cdr result)))
1792                              (when message-signature
1793                                (message-insert-signature)))))))
1794                    (t
1795                     (let ((header
1796                            (if (symbolp (car result))
1797                                (capitalize (symbol-name (car result)))
1798                              (car result))))
1799                       `(lambda ()
1800                          (save-excursion
1801                            (message-remove-header ,header)
1802                            (let ((value ,(cdr result)))
1803                              (when value
1804                                (message-goto-eoh)
1805                                (insert ,header ": " value "\n"))))))))
1806                   nil 'local))
1807       (when (or name address)
1808         (add-hook 'message-setup-hook
1809                   `(lambda ()
1810                      (set (make-local-variable 'user-mail-address)
1811                           ,(or (cdr address) user-mail-address))
1812                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1813                            (user-mail-address
1814                             ,(or (cdr address) user-mail-address)))
1815                        (save-excursion
1816                          (message-remove-header "From")
1817                          (message-goto-eoh)
1818                          (insert "From: " (message-make-from) "\n"))))
1819                   nil 'local)))))
1820
1821
1822 ;;; @ for MIME Edit mode
1823 ;;;
1824
1825 (defun gnus-maybe-setup-default-charset ()
1826   (let ((charset
1827          (and (boundp 'gnus-summary-buffer)
1828               (buffer-live-p gnus-summary-buffer)
1829               (save-excursion
1830                 (set-buffer gnus-summary-buffer)
1831                 default-mime-charset))))
1832     (if charset
1833         (progn
1834           (make-local-variable 'default-mime-charset)
1835           (setq default-mime-charset charset)
1836           ))))
1837
1838
1839 ;;; @ for MIME view mode
1840 ;;;
1841
1842 (defun gnus-following-method (buf)
1843   (gnus-setup-message 'reply-yank
1844     (set-buffer buf)
1845     (if (message-news-p)
1846         (message-followup)
1847       (message-reply nil 'wide))
1848     (let ((message-reply-buffer buf))
1849       (message-yank-original))
1850     (message-goto-body))
1851   (kill-buffer buf))
1852
1853
1854 ;;; Allow redefinition of functions.
1855
1856 (gnus-ems-redefine)
1857
1858 (provide 'gnus-msg)
1859
1860 ;;; gnus-msg.el ends here