*** empty log message ***
[gnus] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29 (require 'sendmail)
30 (require 'gnus-ems)
31 (eval-when-compile (require 'cl))
32
33 (defvar gnus-organization-file "/usr/lib/news/organization"
34   "*Local news organization file.")
35
36 (defvar gnus-prepare-article-hook nil
37   "*A hook called after preparing body, but before preparing header headers.")
38
39 (defvar gnus-post-prepare-function nil
40   "*Function that is run after a post buffer has been prepared.
41 It is called with the name of the newsgroup that is posted to. It
42 might be used, for instance, for inserting signatures based on the
43 newsgroup name. (In that case, `gnus-signature-file' and
44 `mail-signature' should both be set to nil).")
45
46 (defvar gnus-post-prepare-hook nil
47   "*Hook that is run after a post buffer has been prepared.
48 If you want to insert the signature, you might put
49 `gnus-inews-insert-signature' in this hook.")
50
51 (defvar gnus-use-followup-to t
52   "*Specifies what to do with Followup-To header.
53 If nil, ignore the header. If it is t, use its value, but ignore 
54 `poster'.  If it is the symbol `ask', query the user before posting.
55 If it is the symbol `use', always use the value.") 
56
57 (defvar gnus-followup-to-function nil
58   "*A variable that contains a function that returns a followup address.
59 The function will be called in the buffer of the article that is being
60 followed up. The buffer will be narrowed to the headers of the
61 article. To pick header headers, one might use `mail-fetch-field'.  The
62 function will be called with the name of the current newsgroup as the
63 argument.
64
65 Here's an example `gnus-followup-to-function':
66
67 (setq gnus-followup-to-function
68       (lambda (group)
69         (cond ((string= group \"mail.list\")
70                (or (mail-fetch-field \"sender\") 
71                    (mail-fetch-field \"from\")))
72               (t
73                (or (mail-fetch-field \"reply-to\") 
74                    (mail-fetch-field \"from\"))))))")
75
76 (defvar gnus-reply-to-function nil
77   "*A variable that contains a function that returns a reply address.
78 See the `gnus-followup-to-function' variable for an explanation of how
79 this variable is used.
80
81 This function should return a string that will be used to fill in the
82 header.  This function may also return a list.  In that case, every
83 list element should be a cons where the first car should be a string
84 with the header name, and the cdr should be a string with the header
85 value.")
86
87 (defvar gnus-author-copy (getenv "AUTHORCOPY")
88   "*Save outgoing articles in this file.
89 Initialized from the AUTHORCOPY environment variable.
90
91 If this variable begins with the character \"|\", outgoing articles
92 will be piped to the named program. It is possible to save an article
93 in an MH folder as follows:
94
95 \(setq gnus-author-copy \"|/usr/local/lib/mh/rcvstore +Article\")
96
97 If the first character is not a pipe, articles are saved using the
98 function specified by the `gnus-author-copy-saver' variable.")
99
100 (defvar gnus-mail-self-blind nil
101   "*Non-nil means insert a BCC header in all outgoing articles.
102 This will result in having a copy of the article mailed to yourself.
103 The BCC header is inserted when the post buffer is initialized, so you
104 can remove or alter the BCC header to override the default.")
105
106 (defvar gnus-author-copy-saver (function rmail-output)
107   "*A function called to save outgoing articles.
108 This function will be called with the same of the file to store the
109 article in. The default function is `rmail-output' which saves in Unix
110 mailbox format.")
111
112 (defvar gnus-outgoing-message-group nil
113   "*All outgoing messages will be put in this group.
114 If you want to store all your outgoing mail and articles in the group
115 \"nnml:archive\", you set this variable to that value. This variable
116 can also be a list of group names. 
117
118 If you want to have greater control over what group to put each
119 message in, you can set this variable to a function that checks the
120 current newsgroup name and then returns a suitable group name (or list
121 of names).")
122
123 (defvar gnus-mailing-list-groups nil
124   "*Regexp matching groups that are really mailing lists.
125 This is useful when you're reading a mailing list that has been
126 gatewayed to a newsgroup, and you want to followup to an article in
127 the group.")
128
129 (defvar gnus-draft-group-directory 
130   (expand-file-name
131    (concat (file-name-as-directory gnus-article-save-directory)
132            "drafts"))
133   "*The directory where draft messages will be stored.")
134
135 (defvar gnus-posting-styles nil
136   "*Alist of styles to use when posting.")
137
138 (defvar gnus-posting-style-alist
139   '((organization . gnus-organization-file)
140     (signature . gnus-signature-file)
141     (from . gnus-user-from-line)))
142
143 (defvar gnus-user-login-name nil
144   "*The login name of the user.
145 Got from the function `user-login-name' if undefined.")
146
147 (defvar gnus-user-full-name nil
148   "*The full name of the user.
149 Got from the NAME environment variable if undefined.")
150
151 (defvar gnus-user-from-line nil
152   "*Your full, complete e-mail address.  
153 Overrides the other Gnus variables if it is non-nil.
154
155 Here are two example values of this variable:
156
157  \"Lars Magne Ingebrigtsen <larsi@ifi.uio.no>\"
158
159 and
160
161  \"larsi@ifi.uio.no (Lars Magne Ingebrigtsen)\"
162
163 The first version is recommended, but the name has to be quoted if it
164 contains non-alphanumerical characters.")
165
166 (defvar gnus-signature-file "~/.signature"
167   "*Your signature file.
168 If the variable is a string that doesn't correspond to a file, the
169 string itself is inserted.")
170
171 (defvar gnus-signature-function nil
172   "*A function that should return a signature file name.
173 The function will be called with the name of the newsgroup being
174 posted to.
175 If the function returns a string that doesn't correspond to a file, the
176 string itself is inserted.
177 If the function returns nil, the `gnus-signature-file' variable will
178 be used instead.")
179
180 (defvar gnus-forward-start-separator 
181   "------- Start of forwarded message -------\n"
182   "*Delimiter inserted before forwarded messages.")
183
184 (defvar gnus-forward-end-separator
185   "------- End of forwarded message -------\n"
186   "*Delimiter inserted after forwarded messages.")
187
188 (defvar gnus-signature-before-forwarded-message t
189   "*If non-nil, put the signature before any included forwarded message.")
190
191 (defvar gnus-required-headers
192   '(From Date Newsgroups Subject Message-ID Organization Lines X-Newsreader)
193   "*Headers to be generated or prompted for when posting an article.
194 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
195 Message-ID.  Organization, Lines, In-Reply-To and X-Newsreader are
196 optional.  If you want Gnus not to insert some header, remove it from
197 this list.")
198
199 (defvar gnus-required-mail-headers 
200   '(From Date To Subject (optional . In-Reply-To) Message-ID Organization Lines)
201   "*Headers to be generated or prompted for when mailing a message.
202 RFC822 required that From, Date, To, Subject and Message-ID be
203 included.  Organization, Lines and X-Mailer are optional.")
204
205 (defvar gnus-deletable-headers '(Message-ID Date)
206   "*Headers to be deleted if they already exists and were generated by Gnus previously.")
207
208 (defvar gnus-removable-headers '(NNTP-Posting-Host Bcc Xref)
209   "*Headers to be removed unconditionally before posting.")
210
211 (defvar gnus-check-before-posting 
212   '(subject-cmsg multiple-headers sendsys message-id from
213                  long-lines control-chars size new-text
214                  signature approved sender)
215   "In non-nil, Gnus will attempt to run some checks on outgoing posts.
216 If this variable is t, Gnus will check everything it can.  If it is a
217 list, then those elements in that list will be checked.")
218
219 (defvar gnus-delete-supersedes-headers
220   "^Path:\\|^Date\\|^NNTP-Posting-Host:\\|^Supersedes:\\|^Xref:\\|^Lines:"
221   "*Header lines matching this regexp will be deleted before posting.
222 It's best to delete old Path and Date headers before posting to avoid
223 any confusion.")
224
225 (defvar gnus-auto-mail-to-author nil
226   "*If non-nil, mail the authors of articles a copy of your follow-ups.
227 If this variable is `ask', the user will be prompted for whether to
228 mail a copy.  The string given by `gnus-mail-courtesy-message' will be
229 inserted at the beginning of the mail copy.")
230
231 ;; Added by Ethan Bradford <ethanb@ptolemy.astro.washington.edu>.
232 (defvar gnus-mail-courtesy-message
233   "The following message is a courtesy copy of an article\nthat has been posted as well.\n\n"
234   "*This is inserted at the start of a mailed copy of a posted message.
235 If this variable is nil, no such courtesy message will be added.")
236
237 (defvar gnus-mail-reply-method (function gnus-mail-reply-using-mail)
238   "*Function to compose a reply.
239 Three pre-made functions are `gnus-mail-reply-using-mail' (sendmail);
240 `gnus-mail-reply-using-mhe' (MH-E); and `gnus-mail-reply-using-vm'.")
241
242 (defvar gnus-mail-forward-method (function gnus-mail-forward-using-mail)
243   "*Function to forward the current message to another user.
244 Three pre-made functions are `gnus-mail-forward-using-mail' (sendmail);
245 `gnus-mail-forward-using-mhe' (MH-E); and `gnus-mail-forward-using-vm'.") 
246
247 (defvar gnus-mail-other-window-method 'gnus-mail-other-window-using-mail
248   "*Function to compose mail in the other window.
249 Three pre-made functions are `gnus-mail-other-window-using-mail'
250 (sendmail); `gnus-mail-other-window-using-mhe' (MH-E); and
251 `gnus-mail-other-window-using-vm'.")
252
253 (defvar gnus-inews-article-function 'gnus-inews-article
254   "*Function to post an article.")
255
256 (defvar gnus-bounced-headers-junk "^\\(Received\\):"
257   "*Regexp that matches headers to be removed in resent bounced mail.")
258
259 (defvar gnus-inews-article-hook (list 'gnus-inews-do-fcc)
260   "*A hook called before finally posting an article.
261 The default hook (`gnus-inews-do-fcc') does FCC processing (ie. saves
262 the article to a file).")
263
264 (defvar gnus-inews-article-header-hook nil
265   "*A hook called after inserting the headers in an article to be posted.
266 The hook is called from the *post-news* buffer, narrowed to the
267 headers.")
268
269 (defvar gnus-mail-hook nil
270   "*A hook called as the last thing after setting up a mail buffer.")
271
272 (defvar gnus-message-sent-hook nil
273   "*A hook run after an article has been sent (or attempted sent).")
274
275 ;;; Internal variables.
276
277 (defvar gnus-post-news-buffer "*post-news*")
278 (defvar gnus-mail-buffer "*mail*")
279 (defvar gnus-summary-send-map nil)
280 (defvar gnus-send-bounce-map nil)
281 (defvar gnus-article-copy nil)
282 (defvar gnus-reply-subject nil)
283 (defvar gnus-add-to-address nil)
284 (defvar gnus-in-reply-to nil)
285
286 (eval-and-compile
287   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
288   (autoload 'news-setup "rnewspost")
289   (autoload 'news-reply-mode "rnewspost")
290   (autoload 'rmail-output "rmailout"))
291
292 \f
293 ;;;
294 ;;; Gnus Posting Functions
295 ;;;
296
297 (define-prefix-command 'gnus-summary-send-map)
298 (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
299 (define-key gnus-summary-send-map "p" 'gnus-summary-post-news)
300 (define-key gnus-summary-send-map "f" 'gnus-summary-followup)
301 (define-key gnus-summary-send-map "F" 'gnus-summary-followup-with-original)
302 (define-key gnus-summary-send-map "b" 'gnus-summary-followup-and-reply)
303 (define-key gnus-summary-send-map "B" 'gnus-summary-followup-and-reply-with-original)
304 (define-key gnus-summary-send-map "c" 'gnus-summary-cancel-article)
305 (define-key gnus-summary-send-map "s" 'gnus-summary-supersede-article)
306 (define-key gnus-summary-send-map "r" 'gnus-summary-reply)
307 (define-key gnus-summary-send-map "R" 'gnus-summary-reply-with-original)
308 (define-key gnus-summary-send-map "m" 'gnus-summary-mail-other-window)
309 (define-key gnus-summary-send-map "u" 'gnus-uu-post-news)
310 (define-key gnus-summary-send-map "om" 'gnus-summary-mail-forward)
311 (define-key gnus-summary-send-map "op" 'gnus-summary-post-forward)
312 (define-key gnus-summary-send-map "Om" 'gnus-uu-digest-mail-forward)
313 (define-key gnus-summary-send-map "Op" 'gnus-uu-digest-post-forward)
314
315 (define-prefix-command 'gnus-send-bounce-map)
316 (define-key gnus-summary-send-map "D" 'gnus-send-bounce-map)
317 (define-key gnus-send-bounce-map "b" 'gnus-summary-resend-bounced-mail)
318 (define-key gnus-send-bounce-map "c" 'gnus-summary-send-draft)
319 (define-key gnus-send-bounce-map "r" 'gnus-summary-resend-message)
320
321 ;;; Internal functions.
322
323 (defun gnus-number-base36 (num len)
324   (if (if (< len 0) (<= num 0) (= len 0))
325       ""
326     (concat (gnus-number-base36 (/ num 36) (1- len))
327             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
328                                   (% num 36))))))
329
330 ;;; Post news commands of Gnus group mode and summary mode
331
332 (defun gnus-group-mail ()
333   "Start composing a mail."
334   (interactive)
335   (gnus-new-mail
336    ;; We might want to prompt here.
337    (when (and gnus-interactive-post
338               (not gnus-expert-user))
339      (read-string "To: "))))
340
341 (defun gnus-group-post-news (&optional arg)
342   "Post an article.
343 The newsgroup under the cursor is used as the group to post to.
344
345 If you wish to get an empty post buffer, use a prefix ARG.  You can
346 also do this by calling this function from the bottom of the Group
347 buffer."
348   (interactive "P")
349   (let ((gnus-newsgroup-name nil)
350         (group (unless arg (gnus-group-group-name)))
351         subject)
352     ;; We might want to prompt here.
353     (when (and gnus-interactive-post
354                (not gnus-expert-user))
355       (setq gnus-newsgroup-name
356             (setq group 
357                   (completing-read "Group: " gnus-active-hashtb nil nil
358                                    (cons group 0)))))
359     (gnus-post-news 'post group nil gnus-article-buffer)))
360
361 (defun gnus-summary-post-news ()
362   "Post an article."
363   (interactive)
364   (gnus-set-global-variables)
365   (gnus-post-news 'post gnus-newsgroup-name))
366
367 (defun gnus-summary-followup (yank &optional yank-articles)
368   "Compose a followup to an article.
369 If prefix argument YANK is non-nil, original article is yanked automatically."
370   (interactive "P")
371   (gnus-set-global-variables)
372   (if yank-articles (gnus-summary-goto-subject (car yank-articles)))
373   (save-window-excursion
374     (gnus-summary-select-article))
375   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
376         (gnus-newsgroup-name gnus-newsgroup-name))
377     ;; Check Followup-To: poster.
378     (set-buffer gnus-article-buffer)
379     (if (and gnus-use-followup-to
380              (string-equal "poster" (gnus-fetch-field "followup-to"))
381              (or (not (memq gnus-use-followup-to '(t ask)))
382                  (not (gnus-y-or-n-p 
383                        "Do you want to ignore `Followup-To: poster'? "))))
384         ;; Mail to the poster. 
385         (gnus-summary-reply yank)
386       ;; Send a followup.
387       (gnus-post-news nil gnus-newsgroup-name
388                       headers gnus-article-buffer 
389                       (or yank-articles (not (not yank)))))))
390
391 (defun gnus-summary-followup-with-original (n)
392   "Compose a followup to an article and include the original article."
393   (interactive "P")
394   (gnus-summary-followup t (gnus-summary-work-articles n)))
395
396 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
397 (defun gnus-summary-followup-and-reply (yank &optional yank-articles)
398   "Compose a followup and do an auto mail to author."
399   (interactive "P")
400   (gnus-set-global-variables)
401   (let ((gnus-auto-mail-to-author 'force))
402     (gnus-summary-followup yank yank-articles)))
403
404 (defun gnus-summary-followup-and-reply-with-original (n)
405   "Compose a followup, include the original, and do an auto mail to author."
406   (interactive "P")
407   (gnus-summary-followup-and-reply t (gnus-summary-work-articles n)))
408
409 (defun gnus-summary-cancel-article (n)
410   "Cancel an article you posted."
411   (interactive "P")
412   (gnus-set-global-variables)
413   (let ((articles (gnus-summary-work-articles n)))
414     (while articles
415       (gnus-summary-select-article t nil nil (car articles))
416       (and (gnus-eval-in-buffer-window gnus-article-buffer (gnus-cancel-news))
417            (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
418       (gnus-summary-remove-process-mark (car articles))
419       (gnus-article-hide-headers-if-wanted)
420       (setq articles (cdr articles)))))
421
422 (defun gnus-summary-supersede-article ()
423   "Compose an article that will supersede a previous article.
424 This is done simply by taking the old article and adding a Supersedes
425 header line with the old Message-ID."
426   (interactive)
427   (gnus-set-global-variables)
428   (gnus-summary-select-article t)
429   ;; Check whether the user owns the article that is to be superseded. 
430   (unless (string-equal
431            (downcase (mail-strip-quoted-names 
432                       (mail-header-from gnus-current-headers)))
433            (downcase (mail-strip-quoted-names (gnus-inews-user-name))))
434     (error "This article is not yours."))
435   ;; Get a normal *post-news* buffer.
436   (gnus-new-news gnus-newsgroup-name t)
437   (erase-buffer)
438   (insert-buffer-substring gnus-original-article-buffer)
439   (gnus-narrow-to-headers)
440   ;; Remove unwanted headers.
441   (when gnus-delete-supersedes-headers
442     (nnheader-remove-header gnus-delete-supersedes-headers t))
443   (goto-char (point-min))
444   (if (not (re-search-forward "^Message-ID: " nil t))
445       (error "No Message-ID in this article")
446     (replace-match "Supersedes: " t t))
447   (goto-char (point-max))
448   (insert mail-header-separator)
449   (widen)
450   (forward-line 1))
451
452 \f
453 ;;;###autoload
454 (defalias 'sendnews 'gnus-post-news)
455
456 ;;;###autoload
457 (defalias 'postnews 'gnus-post-news)
458
459 (defun gnus-copy-article-buffer (&optional article-buffer)
460   ;; make a copy of the article buffer with all text properties removed
461   ;; this copy is in the buffer gnus-article-copy.
462   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
463   ;; this buffer should be passed to all mail/news reply/post routines.
464   (setq gnus-article-copy (get-buffer-create " *gnus article copy*"))
465   (buffer-disable-undo gnus-article-copy)
466   (or (memq gnus-article-copy gnus-buffer-list)
467       (setq gnus-buffer-list (cons gnus-article-copy gnus-buffer-list)))
468   (let ((article-buffer (or article-buffer gnus-article-buffer)))
469     (if (and (get-buffer article-buffer)
470              (buffer-name (get-buffer article-buffer)))
471         (save-excursion
472           (set-buffer article-buffer)
473           (widen)
474           (copy-to-buffer gnus-article-copy (point-min) (point-max))
475           (set-text-properties (point-min) (point-max) 
476                                nil gnus-article-copy)))))
477
478 (defun gnus-post-news (post &optional group header article-buffer yank subject)
479   "Begin editing a new USENET news article to be posted.
480 Type \\[describe-mode] in the buffer to get a list of commands."
481   (interactive (list t))
482   (let* ((group (or group gnus-newsgroup-name))
483          (pgroup group)
484          to-address to-group mailing-list to-list)
485     (when group
486       (setq to-address (gnus-group-get-parameter group 'to-address)
487             to-group (gnus-group-get-parameter group 'to-group)
488             to-list (gnus-group-get-parameter group 'to-list)
489             mailing-list (when gnus-mailing-list-groups
490                            (string-match gnus-mailing-list-groups group))
491             group (gnus-group-real-name group)))
492     (if (or to-group
493             (and (gnus-member-of-valid 'post (or pgroup gnus-newsgroup-name))
494                  (not mailing-list)
495                  (not to-list)
496                  (not to-address)))
497         ;; This is news.
498         (if post
499             (gnus-new-news (or to-group group))
500           (gnus-news-followup yank (or to-group group)))
501       ;; The is mail.
502       (if post
503           (progn
504             (gnus-new-mail (or to-address to-list))
505             ;; Arrange for mail groups that have no `to-address' to
506             ;; get that when the user sends off the mail.
507             (unless to-address
508               (make-local-variable 'gnus-add-to-address)
509               (setq gnus-add-to-address group)))
510         (gnus-mail-reply yank to-address 'followup)))))
511
512 (defun gnus-inews-news (&optional use-group-method)
513   "Send a news message.
514 If given a prefix, and the group is a foreign group, this function
515 will attempt to use the foreign server to post the article."
516   (interactive "P")
517   (or gnus-current-select-method
518       (setq gnus-current-select-method gnus-select-method))
519   (let* ((case-fold-search nil)
520          (reply gnus-article-reply)
521          error post-result)
522     (save-excursion
523       (gnus-start-news-server)          ;Use default server.
524       (widen)
525       (goto-char (point-min))
526       (run-hooks 'news-inews-hook)
527
528       ;; Send to server. 
529       (gnus-message 5 "Posting to USENET...")
530       (setq post-result (funcall gnus-inews-article-function use-group-method))
531       (cond ((eq post-result 'illegal)
532              (setq error t)
533              (ding))
534             (post-result
535              (gnus-message 5 "Posting to USENET...done")
536              (if (gnus-buffer-exists-p (car-safe reply))
537                  (progn
538                    (save-excursion
539                      (set-buffer gnus-summary-buffer)
540                      (gnus-summary-mark-article-as-replied 
541                       (cdr reply)))))
542              (set-buffer-modified-p nil))
543             (t
544              ;; We cannot signal an error.
545              (setq error t)
546              (ding)
547              (gnus-message 
548               1 "Article rejected: %s" 
549               (gnus-status-message
550                (gnus-post-method gnus-newsgroup-name use-group-method))))))
551
552     (let ((conf gnus-prev-winconf))
553       (unless error
554         (bury-buffer)
555         ;; Restore last window configuration.
556         (and conf (set-window-configuration conf))))))
557
558 (defun gnus-inews-narrow-to-headers ()
559   (widen)
560   (narrow-to-region
561    (goto-char (point-min))
562    (or (and (re-search-forward 
563              (concat "^" (regexp-quote mail-header-separator) "$") nil t)
564             (match-beginning 0))
565        (point-max))))
566
567 (defun gnus-inews-send-mail-copy ()
568   ;; Mail the message if To, Bcc or Cc exists.
569   (let* ((types '("to" "bcc" "cc"))
570          (ty types)
571          (buffer (current-buffer))
572          fcc)
573     (save-restriction
574       (widen)
575       (gnus-inews-narrow-to-headers)
576
577       (while ty
578         (or (mail-fetch-field (car ty) nil t)
579             (setq types (delete (car ty) types)))
580         (setq ty (cdr ty)))
581
582       (if (not types)
583           ;; We do not want to send mail.
584           ()
585         (gnus-message 5 "Sending via mail...")
586         (widen)
587         (save-excursion
588           ;; We copy the article over to a temp buffer since we are
589           ;; going to modify it a little.  
590           (nnheader-set-temp-buffer " *Gnus-mailing*")
591           (insert-buffer-substring buffer)
592           ;; We remove Fcc, because we don't want the mailer to see
593           ;; that header.  
594           (gnus-inews-narrow-to-headers)
595           (nnheader-remove-header "fcc")
596
597           ;; Insert the X-Courtesy-Message header.
598           (and (or (member "to" types)
599                    (member "cc" types))
600                (progn
601                 (goto-char (point-max))
602                 (insert "Posted-To: " 
603                         (mail-fetch-field "newsgroups") "\n")))
604           
605           (widen)
606           
607           (if (and gnus-mail-courtesy-message
608                    (or (member "to" types)
609                        (member "cc" types)))
610               ;; We only want to insert the courtesy mail message if
611               ;; we use To or Cc; Bcc should not have one. Well, if
612               ;; both Bcc and To are present, it will get one
613               ;; anyway.
614               (progn
615                 ;; Insert "courtesy" mail message.
616                 (goto-char (point-min))
617                 (re-search-forward
618                  (concat "^" (regexp-quote mail-header-separator) "$"))
619                 (forward-line 1)
620                 (insert gnus-mail-courtesy-message)))
621
622           (gnus-mail-send)
623           (kill-buffer (current-buffer))
624           (gnus-message 5 "Sending via mail...done"))))))
625
626 (defun gnus-inews-remove-headers-after-mail ()
627   (save-excursion
628     (save-restriction
629       (let ((case-fold-search t))
630         (gnus-inews-narrow-to-headers)
631         ;; Remove Bcc completely.
632         (nnheader-remove-header "bcc")
633         ;; We transform To and Cc headers to avoid re-mailing if the user
634         ;; accidentally (or purposefully) leans on the `C-c C-c' keys
635         ;; and the news server rejects the posting.
636         (while (re-search-forward "^\\(to\\|[bcf]cc\\|cc\\):" nil t)
637           (beginning-of-line)
638           (insert "X-"))
639         (widen)))))
640
641 (defun gnus-inews-dex-headers ()
642   "Remove \"X-\" prefixes from To and Cc headers."
643   (save-excursion
644     (save-restriction
645       (let ((case-fold-search t))
646         (gnus-narrow-to-headers)
647         (while (re-search-forward "^X-\\(to\\|[bcf]cc\\|cc\\):" nil t)
648           (beginning-of-line)
649           (delete-char 2))
650         (widen)))))
651
652 (defun gnus-inews-remove-empty-headers ()
653   "Remove empty headers from news and mail.
654 The buffer should be narrowed to the headers before this function is
655 called."
656   (save-excursion
657     (goto-char (point-min))
658     (while (re-search-forward "^[^ \t:]+:\\([ \t]*\n\\)+[^ \t]" nil t)
659       (delete-region (match-beginning 0) (1- (match-end 0)))
660       (beginning-of-line))))
661
662 (defun gnus-inews-check-post ()
663   "Check whether the post looks ok."
664   (or
665    (not gnus-check-before-posting)
666    (and 
667     ;; We narrow to the headers and check them first.
668     (save-excursion
669       (save-restriction
670         (goto-char (point-min))
671         (narrow-to-region 
672          (point) 
673          (progn
674            (re-search-forward 
675             (concat "^" (regexp-quote mail-header-separator) "$"))
676            (match-beginning 0)))
677         (goto-char (point-min))
678         (and 
679          ;; Check for commands in Subject.
680          (or 
681           (gnus-check-before-posting 'subject-cmsg)
682           (save-excursion
683             (if (string-match "^cmsg " (mail-fetch-field "subject"))
684                 (gnus-y-or-n-p
685                  "The control code \"cmsg \" is in the subject. Really post? ")
686               t)))
687          ;; Check for multiple identical headers.
688          (or (gnus-check-before-posting 'multiple-headers)
689              (save-excursion
690                (let (found)
691                  (while (and (not found) (re-search-forward "^[^ \t:]+: "
692                                                             nil t))
693                    (save-excursion
694                      (or (re-search-forward 
695                           (concat "^" (setq found
696                                             (buffer-substring 
697                                              (match-beginning 0) 
698                                              (- (match-end 0) 2))))
699                           nil t)
700                          (setq found nil))))
701                  (if found
702                      (gnus-y-or-n-p 
703                       (format "Multiple %s headers. Really post? " found))
704                    t))))
705          ;; Check for Version and Sendsys.
706          (or (gnus-check-before-posting 'sendsys)
707              (save-excursion
708                (if (re-search-forward "^Sendsys:\\|^Version:" nil t)
709                    (gnus-y-or-n-p
710                     (format "The article contains a %s command. Really post? "
711                             (buffer-substring (match-beginning 0) 
712                                               (1- (match-end 0)))))
713                  t)))
714          ;; Check for Approved.
715          (or (gnus-check-before-posting 'approved)
716              (save-excursion
717                (if (re-search-forward "^Approved:" nil t)
718                    (gnus-y-or-n-p
719                     "The article contains an Approved header. Really post? ")
720                  t)))
721          ;; Check the Message-ID header.
722          (or (gnus-check-before-posting 'message-id)
723              (save-excursion
724                (let* ((case-fold-search t)
725                       (message-id (mail-fetch-field "message-id")))
726                  (or (not message-id)
727                      (and (string-match "@" message-id)
728                           (string-match "@[^\\.]*\\." message-id))
729                      (gnus-y-or-n-p
730                       (format 
731                        "The Message-ID looks strange: \"%s\". Really post? "
732                        message-id))))))
733          ;; Check the From header.
734          (or (gnus-check-before-posting 'from)
735              (save-excursion
736                (let* ((case-fold-search t)
737                       (from (mail-fetch-field "from")))
738                  (cond
739                   ((not from)
740                    (gnus-y-or-n-p "There is no From line. Really post? "))
741                   ((not (string-match "@[^\\.]*\\." from))
742                    (gnus-y-or-n-p
743                     (format 
744                      "The address looks strange: \"%s\". Really post? " from)))
745                   ((string-match "(.*).*(.*)" from)
746                    (gnus-y-or-n-p
747                     (format
748                      "The From header looks strange: \"%s\". Really post? " 
749                      from)))
750                   ((string-match "<[^>]+> *$" from)
751                    (let ((name (substring from 0 (match-beginning 0))))
752                      (or 
753                       (string-match "^ *\"[^\"]*\" *$" name)
754                       (not (string-match "[][.!()<>@,;:\\]" name))
755                       (gnus-y-or-n-p
756                        (format
757                         "The From header name has bogus characters.  Really post? " 
758                         from)))))
759                   (t t)))))
760          )))
761     ;; Check for long lines.
762     (or (gnus-check-before-posting 'long-lines)
763         (save-excursion
764           (goto-char (point-min))
765           (re-search-forward
766            (concat "^" (regexp-quote mail-header-separator) "$"))
767           (while (and
768                   (progn
769                     (end-of-line)
770                     (< (current-column) 80))
771                   (zerop (forward-line 1))))
772           (or (bolp)
773               (eobp)
774               (gnus-y-or-n-p
775                (format
776                 "You have lines longer than 79 characters.  Really post? ")))))
777     ;; Check for control characters.
778     (or (gnus-check-before-posting 'control-chars)
779         (save-excursion
780           (if (re-search-forward "[\000-\007\013\015-\037\200-\237]" nil t)
781               (gnus-y-or-n-p 
782                "The article contains control characters. Really post? ")
783             t)))
784     ;; Check excessive size.
785     (or (gnus-check-before-posting 'size)
786         (if (> (buffer-size) 60000)
787             (gnus-y-or-n-p
788              (format "The article is %d octets long. Really post? "
789                      (buffer-size)))
790           t))
791     ;; Use the (size . checksum) variable to see whether the
792     ;; article is empty or has only quoted text.
793     (or
794      (gnus-check-before-posting 'new-text)
795      (if (and (= (buffer-size) (car gnus-article-check-size))
796               (= (gnus-article-checksum) (cdr gnus-article-check-size)))
797          (gnus-y-or-n-p
798           "It looks like there's no new text in your article. Really post? ")
799        t))
800     ;; Check the length of the signature.
801     (or (gnus-check-before-posting 'signature)
802         (progn
803           (goto-char (point-max))
804           (if (not (re-search-backward gnus-signature-separator nil t))
805               t
806             (if (> (count-lines (point) (point-max)) 5)
807                 (gnus-y-or-n-p
808                  (format
809                   "Your .sig is %d lines; it should be max 4.  Really post? "
810                   (count-lines (point) (point-max))))
811               t)))))))
812
813 (defun gnus-article-checksum ()
814   (let ((sum 0))
815     (save-excursion
816       (while (not (eobp))
817         (setq sum (logxor sum (following-char)))
818         (forward-char 1)))
819     sum))
820
821 ;; Returns non-nil if this type is not to be checked.
822 (defun gnus-check-before-posting (type)
823   (not 
824    (or (not gnus-check-before-posting)
825        (if (listp gnus-check-before-posting)
826            (memq type gnus-check-before-posting)
827          t))))
828
829 (defun gnus-cancel-news ()
830   "Cancel an article you posted."
831   (interactive)
832   (if (or gnus-expert-user
833           (gnus-yes-or-no-p "Do you really want to cancel this article? "))
834       (let ((from nil)
835             (newsgroups nil)
836             (message-id nil)
837             (distribution nil))
838         (or (gnus-member-of-valid 'post gnus-newsgroup-name)
839             (error "This backend does not support canceling"))
840         (save-excursion
841           ;; Get header info. from original article.
842           (save-restriction
843             (gnus-article-show-all-headers)
844             (goto-char (point-min))
845             (search-forward "\n\n" nil 'move)
846             (narrow-to-region (point-min) (point))
847             (setq from (mail-fetch-field "from"))
848             (setq newsgroups (mail-fetch-field "newsgroups"))
849             (setq message-id (mail-fetch-field "message-id"))
850             (setq distribution (mail-fetch-field "distribution")))
851           ;; Verify if the article is absolutely user's by comparing
852           ;; user id with value of its From: field.
853           (if (not
854                (string-equal
855                 (downcase (mail-strip-quoted-names from))
856                 (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
857               (progn
858                 (ding) (gnus-message 3 "This article is not yours.")
859                 nil)
860             ;; Make control article.
861             (set-buffer (get-buffer-create " *Gnus-canceling*"))
862             (buffer-disable-undo (current-buffer))
863             (erase-buffer)
864             (insert "Newsgroups: " newsgroups "\n"
865                     "From: " (gnus-inews-real-user-address) "\n"
866                     "Subject: cancel " message-id "\n"
867                     "Control: cancel " message-id "\n"
868                     (if distribution
869                         (concat "Distribution: " distribution "\n")
870                       "")
871                     mail-header-separator "\n"
872                     "This is a cancel message from " from ".\n")
873             ;; Send the control article to NNTP server.
874             (gnus-message 5 "Canceling your article...")
875             (prog1
876                 (if (funcall gnus-inews-article-function)
877                     (gnus-message 5 "Canceling your article...done")
878                   (progn
879                     (ding) 
880                     (gnus-message 1 "Cancel failed; %s" 
881                                   (gnus-status-message gnus-newsgroup-name))
882                     nil)
883                   t)
884               ;; Kill the article buffer.
885               (kill-buffer (current-buffer))))))))
886
887 \f
888 ;;; Lowlevel inews interface.
889
890 ;; Dummy to avoid byte-compile warning.
891 (defvar nnspool-rejected-article-hook)
892
893 (defun gnus-inews-article (&optional use-group-method)
894   "Post an article in current buffer using NNTP protocol."
895   (let ((artbuf (current-buffer))
896         gcc result)
897     (widen)
898     (goto-char (point-max))
899     ;; Require a newline at the end of the buffer since inews may
900     ;; append a .signature.
901     (or (= (preceding-char) ?\n)
902         (insert ?\n))
903     ;; Prepare article headers.  All message body such as signature
904     ;; must be inserted before Lines: field is prepared.
905     (save-restriction
906       (gnus-inews-narrow-to-headers)
907       ;; Fix some headers.
908       (gnus-inews-cleanup-headers)
909       ;; Remove some headers.
910       (gnus-inews-remove-headers)
911       ;; Insert some headers.
912       (gnus-inews-insert-headers)
913       ;; Let the user do all of the above.
914       (run-hooks 'gnus-inews-article-header-hook)
915       ;; Copy the Gcc header, if any.
916       (setq gcc (mail-fetch-field "gcc"))
917       (widen))
918     ;; Check whether the article is a good Net Citizen.
919     (if (and gnus-article-check-size
920              (not (gnus-inews-check-post)))
921         ;; Aber nein!
922         'illegal
923       ;; We fudge a hook for nnspool.
924       (setq nnspool-rejected-article-hook
925             (`
926              (list
927               (lambda ()
928                 (condition-case ()
929                     (save-excursion
930                       (set-buffer (, (buffer-name)))
931                       (gnus-put-in-draft-group nil 'silent))
932                   (error 
933                    (ding)
934                    (gnus-message 
935                     1 "Couldn't enter rejected article into draft group")))))))
936                                    
937       ;; Looks ok, so we do the nasty.
938       (save-excursion
939         ;; This hook may insert a signature.
940         (save-excursion
941           (goto-char (point-min))
942           (let ((gnus-newsgroup-name (or (mail-fetch-field "newsgroups")
943                                          gnus-newsgroup-name)))
944             (run-hooks 'gnus-prepare-article-hook)))
945         ;; Send off copies using mail, if that is wanted.
946         (gnus-inews-send-mail-copy)
947         ;; Remove more headers.
948         (gnus-inews-remove-headers-after-mail)
949         ;; Copy the article over to a temp buffer.
950         (nnheader-set-temp-buffer " *Gnus-posting*")
951         (insert-buffer-substring artbuf)
952         ;; Remove the header separator.
953         (goto-char (point-min))
954         (re-search-forward
955          (concat "^" (regexp-quote mail-header-separator) "$"))
956         (replace-match "" t t)
957         ;; Remove X- prefixes to headers.
958         (gnus-inews-dex-headers)
959         ;; Run final inews hooks.  This hook may do FCC.
960         ;; The article must be saved before being posted because
961         ;; `gnus-request-post' modifies the buffer.
962         (run-hooks 'gnus-inews-article-hook)
963         ;; Copy the article over to some group, possibly.
964         (and gcc (gnus-inews-do-gcc gcc))
965         ;; Post the article.
966         (let ((method (gnus-post-method gnus-newsgroup-name use-group-method)))
967           (setq result (gnus-request-post method)))
968         (kill-buffer (current-buffer)))
969       (run-hooks 'gnus-message-sent-hook)
970       ;; If the posting was unsuccessful (that it, it was rejected) we
971       ;; put it into the draft group.
972       (or result (gnus-put-in-draft-group))
973       result)))
974
975 (defun gnus-inews-cleanup-headers ()
976   ;; Correct newsgroups field: change sequence of spaces to comma and 
977   ;; eliminate spaces around commas.  Eliminate imbedded line breaks.
978   (goto-char (point-min))
979   (if (re-search-forward "^Newsgroups: +" nil t)
980       (save-restriction
981         (narrow-to-region
982          (point)
983          (if (re-search-forward "^[^ \t]" nil t)
984              (match-beginning 0)
985            (forward-line 1)
986            (point)))
987         (goto-char (point-min))
988         (while (re-search-forward "\n[ \t]+" nil t)
989           (replace-match " " t t))      ;No line breaks (too confusing)
990         (goto-char (point-min))
991         (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
992           (replace-match "," t t))
993         (goto-char (point-min))
994         ;; Remove a trailing comma.
995         (if (re-search-forward ",$" nil t)
996             (replace-match "" t t))))
997
998   ;; Added by Per Abrahamsen <abraham@iesd.auc.dk>.
999   ;; Help save the the world!
1000   (or 
1001    gnus-expert-user
1002    (let ((newsgroups (mail-fetch-field "newsgroups"))
1003          (followup-to (mail-fetch-field "followup-to"))
1004          groups to)
1005      (if (and newsgroups
1006               (string-match "," newsgroups) (not followup-to))
1007          (progn
1008            (while (string-match "," newsgroups)
1009              (setq groups
1010                    (cons (list (substring newsgroups 0 (match-beginning 0)))
1011                          groups))
1012              (setq newsgroups (substring newsgroups (match-end 0))))
1013            (setq groups (nreverse (cons (list newsgroups) groups)))
1014
1015            (setq to (completing-read 
1016                      "Followups to: (default all groups) " groups))
1017            (if (> (length to) 0)
1018                (progn
1019                  (goto-char (point-min))
1020                  (insert "Followup-To: " to "\n")))))))
1021
1022   ;; Cleanup Followup-To.
1023   (goto-char (point-min))
1024   (if (search-forward-regexp "^Followup-To: +" nil t)
1025       (save-restriction
1026         (narrow-to-region
1027          (point)
1028          (if (re-search-forward "^[^ \t]" nil 'end)
1029              (match-beginning 0)
1030            (point-max)))
1031         (goto-char (point-min))
1032         (replace-regexp "\n[ \t]+" " ") ;No line breaks (too confusing)
1033         (goto-char (point-min))
1034         (replace-regexp "[ \t\n]*,[ \t\n]*\\|[ \t]+" ","))))
1035
1036
1037 (defun gnus-inews-remove-headers ()
1038   (let ((case-fold-search t)
1039         (headers gnus-removable-headers))
1040     ;; Remove toxic headers.
1041     (while headers
1042       (goto-char (point-min))
1043       (and (re-search-forward 
1044             (concat "^" (downcase (format "%s" (car headers))))
1045             nil t)
1046            (delete-region (progn (beginning-of-line) (point))
1047                           (progn (forward-line 1) (point))))
1048       (setq headers (cdr headers)))))
1049
1050 ;;; Since the X-Newsreader/X-Mailer are ``vanity'' headers, they might
1051 ;;; as well include the Emacs version as well.
1052 ;;; The following function works with later GNU Emacs, and XEmacs.
1053 (defun gnus-extended-version ()
1054   "Stringified Gnus version and Emacs version"
1055   (interactive)
1056   (concat
1057    gnus-version
1058    "/"
1059    (cond
1060     ((string-match "^\\([0-9]+\\.[0-9]+\\)\\.[.0-9]+$" emacs-version)
1061      (concat "Emacs " (substring emacs-version
1062                                  (match-beginning 1)
1063                                  (match-end 1))))
1064     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)" emacs-version)
1065      (concat (substring emacs-version
1066                         (match-beginning 1)
1067                         (match-end 1))
1068              (format " %d.%d" emacs-major-version emacs-minor-version)))
1069     (t emacs-version))))
1070
1071 (defun gnus-inews-insert-headers (&optional headers)
1072   "Prepare article headers.
1073 Headers already prepared in the buffer are not modified.
1074 Headers in `gnus-required-headers' will be generated."
1075   (let ((Date (gnus-inews-date))
1076         (Message-ID (gnus-inews-message-id))
1077         (Organization (gnus-inews-organization))
1078         (From (gnus-inews-user-name))
1079         (Path (gnus-inews-path))
1080         (Subject nil)
1081         (Newsgroups nil)
1082         (In-Reply-To (gnus-inews-in-reply-to))
1083         (To nil)
1084         (Distribution nil)
1085         (Lines (gnus-inews-lines))
1086         (X-Newsreader (gnus-extended-version))
1087         (X-Mailer (gnus-extended-version))
1088         (headers (or headers gnus-required-headers))
1089         (case-fold-search t)
1090         header value elem)
1091     ;; First we remove any old generated headers.
1092     (let ((headers gnus-deletable-headers))
1093       (while headers
1094         (goto-char (point-min))
1095         (and (re-search-forward 
1096               (concat "^" (symbol-name (car headers)) ": *") nil t)
1097              (get-text-property (1+ (match-beginning 0)) 'gnus-deletable)
1098              (gnus-delete-line))
1099         (setq headers (cdr headers))))
1100     ;; If there are References, and no "Re: ", then the thread has
1101     ;; changed name. See Son-of-1036.
1102     (if (and (mail-fetch-field "references")
1103              (get-buffer gnus-article-buffer))
1104         (let ((psubject (gnus-simplify-subject-re
1105                          (mail-fetch-field "subject"))))
1106           (or (and psubject gnus-reply-subject 
1107                    (string= (gnus-simplify-subject-re gnus-reply-subject)
1108                             psubject))
1109               (progn
1110                 (string-match "@" Message-ID)
1111                 (setq Message-ID
1112                       (concat (substring Message-ID 0 (match-beginning 0))
1113                               "_-_" 
1114                               (substring Message-ID (match-beginning 0))))))))
1115     ;; Go through all the required headers and see if they are in the
1116     ;; articles already. If they are not, or are empty, they are
1117     ;; inserted automatically - except for Subject, Newsgroups and
1118     ;; Distribution. 
1119     (while headers
1120       (goto-char (point-min))
1121       (setq elem (pop headers))
1122       (if (consp elem)
1123           (setq header (car elem))
1124         (setq header elem))
1125       (when (or (not (re-search-forward 
1126                       (concat "^" (downcase (symbol-name header)) ":") nil t))
1127                 (progn
1128                   ;; The header was found. We insert a space after the
1129                   ;; colon, if there is none.
1130                   (if (/= (following-char) ? ) (insert " "))
1131                   ;; Find out whether the header is empty...
1132                   (looking-at "[ \t]*$")))
1133         ;; So we find out what value we should insert.
1134         (setq value
1135               (cond 
1136                ((and (consp elem) (eq (car elem) 'optional))
1137                 ;; This is an optional header.  If the cdr of this
1138                 ;; is something that is nil, then we do not insert
1139                 ;; this header.
1140                 (setq header (cdr elem))
1141                 (or (and (fboundp (cdr elem)) (funcall (cdr elem)))
1142                     (and (boundp (cdr elem)) (symbol-value (cdr elem)))))
1143                ((consp elem)
1144                 ;; The element is a cons.  Either the cdr is a
1145                 ;; string to be inserted verbatim, or it is a
1146                 ;; function, and we insert the value returned from
1147                 ;; this function.
1148                 (or (and (stringp (cdr elem)) (cdr elem))
1149                     (and (fboundp (cdr elem)) (funcall (cdr elem)))))
1150                ((and (boundp header) (symbol-value header))
1151                 ;; The element is a symbol.  We insert the value
1152                 ;; of this symbol, if any.
1153                 (symbol-value header))
1154                (t
1155                 ;; We couldn't generate a value for this header,
1156                 ;; so we just ask the user.
1157                 (read-from-minibuffer
1158                  (format "Empty header for %s; enter value: " header)))))
1159         ;; Finally insert the header.
1160         (when (and value 
1161                    (not (equal value "")))
1162           (save-excursion
1163             (if (bolp)
1164                 (progn
1165                   ;; This header didn't exist, so we insert it.
1166                   (goto-char (point-max))
1167                   (insert (symbol-name header) ": " value "\n")
1168                   (forward-line -1))
1169               ;; The value of this header was empty, so we clear
1170               ;; totally and insert the new value.
1171               (delete-region (point) (gnus-point-at-eol))
1172               (insert value))
1173             ;; Add the deletable property to the headers that require it.
1174             (and (memq header gnus-deletable-headers)
1175                  (progn (beginning-of-line) (looking-at "[^:]+: "))
1176                  (add-text-properties 
1177                   (point) (match-end 0)
1178                   '(gnus-deletable t face italic) (current-buffer)))))))
1179     ;; Insert new Sender if the From is strange. 
1180     (let ((from (mail-fetch-field "from"))
1181           (sender (mail-fetch-field "sender")))
1182       (if (and from 
1183                (not (gnus-check-before-posting 'sender))
1184                (not (string=
1185                      (downcase (car (gnus-extract-address-components from)))
1186                      (downcase (gnus-inews-real-user-address))))
1187                (or (null sender)
1188                    (not 
1189                     (string=
1190                      (downcase (car (gnus-extract-address-components sender)))
1191                      (downcase (gnus-inews-real-user-address))))))
1192           (progn
1193             (goto-char (point-min))    
1194             (and (re-search-forward "^Sender:" nil t)
1195                  (progn
1196                    (beginning-of-line)
1197                    (insert "Original-")
1198                    (beginning-of-line)))
1199             (insert "Sender: " (gnus-inews-real-user-address) "\n"))))))
1200
1201
1202 (defun gnus-inews-insert-signature ()
1203   "Insert a signature file.
1204 If `gnus-signature-function' is bound and returns a string, this
1205 string is used instead of the variable `gnus-signature-file'.
1206 In either case, if the string is a file name, this file is
1207 inserted. If the string is not a file name, the string itself is
1208 inserted. 
1209
1210 If you never want any signature inserted, set both of these variables to
1211 nil."
1212   (save-excursion
1213     (let ((signature 
1214            (or (and gnus-signature-function
1215                     (funcall gnus-signature-function gnus-newsgroup-name))
1216                gnus-signature-file)))
1217       (if (and signature
1218                (or (file-exists-p signature)
1219                    (string-match " " signature)
1220                    (not (string-match 
1221                          "^/[^/]+/" (expand-file-name signature)))))
1222           (progn
1223             (goto-char (point-max))
1224             (if (and mail-signature (search-backward "\n-- \n" nil t))
1225                 ()
1226               ;; Delete any previous signatures.
1227               (if (search-backward "\n-- \n" nil t)
1228                   (delete-region (point) (point-max)))
1229               (or (eolp) (insert "\n"))
1230               (insert "-- \n")
1231               (if (file-exists-p signature)
1232                   (insert-file-contents signature)
1233                 (insert signature))
1234               (goto-char (point-max))
1235               (or (bolp) (insert "\n"))))))))
1236
1237 ;; Written by "Mr. Per Persson" <pp@solace.mh.se>.
1238 (defun gnus-inews-insert-mime-headers ()
1239   (goto-char (point-min))
1240   (let ((mail-header-separator 
1241          (progn 
1242            (goto-char (point-min))
1243            (if (and (search-forward (concat "\n" mail-header-separator "\n")
1244                                     nil t)
1245                     (not (search-backward "\n\n" nil t)))
1246                mail-header-separator
1247              ""))))
1248     (or (mail-position-on-field "Mime-Version")
1249         (insert "1.0")
1250         (cond ((progn
1251                  (goto-char (point-min))
1252                  (re-search-forward "[\200-\377]" nil t))
1253                (or (mail-position-on-field "Content-Type")
1254                    (insert "text/plain; charset=ISO-8859-1"))
1255                (or (mail-position-on-field "Content-Transfer-Encoding")
1256                    (insert "8bit")))
1257               (t (or (mail-position-on-field "Content-Type")
1258                      (insert "text/plain; charset=US-ASCII"))
1259                  (or (mail-position-on-field "Content-Transfer-Encoding")
1260                      (insert "7bit")))))))
1261
1262 (defun gnus-inews-do-fcc ()
1263   "Process Fcc headers in the current buffer.
1264 Unless the first character of the field is `|', the article is saved
1265 to the specified file using the function specified by the variable
1266 gnus-author-copy-saver.  The default function rmail-output saves in
1267 Unix mailbox format.
1268
1269 If the first character is `|', the contents of the article is sent to
1270 a program specified by the rest of the value."
1271   (let ((case-fold-search t)            ;Should ignore case.
1272         list file)
1273     (save-excursion
1274       (save-restriction
1275         (gnus-narrow-to-headers)
1276         (while (setq file (mail-fetch-field "fcc"))
1277           (push file list)
1278           (nnheader-remove-header "fcc" nil t))
1279         ;; Process FCC operations.
1280         (widen)
1281         (while list
1282           (setq file (pop list))
1283           (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
1284               ;; Pipe the article to the program in question.
1285               (call-process-region (point-min) (point-max) shell-file-name
1286                                    nil nil nil "-c" (match-string 1 file))
1287             ;; Save the article.
1288             (setq file (expand-file-name file))
1289             (gnus-make-directory (file-name-directory file))
1290             (if (and gnus-author-copy-saver
1291                      (not (eq gnus-author-copy-saver 'rmail-output)))
1292                 (funcall gnus-author-copy-saver file)
1293               (if (and (file-readable-p file) (mail-file-babyl-p file))
1294                   (gnus-output-to-rmail file)
1295                 (rmail-output file 1 t t)))))))))
1296
1297 (defun gnus-inews-path ()
1298   "Return uucp path."
1299   (let ((login-name (gnus-inews-login-name)))
1300     (cond ((null gnus-use-generic-path)
1301            (concat (nth 1 gnus-select-method) "!" login-name))
1302           ((stringp gnus-use-generic-path)
1303            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
1304            (concat gnus-use-generic-path "!" login-name))
1305           (t login-name))))
1306
1307 (defun gnus-inews-user-name ()
1308   "Return user's network address as \"NAME@DOMAIN (FULL-NAME)\"."
1309   (let ((full-name (gnus-inews-full-name))
1310         (address (if (or gnus-user-login-name gnus-use-generic-from
1311                          gnus-local-domain (getenv "DOMAINNAME"))
1312                      (concat (gnus-inews-login-name) "@"
1313                              (gnus-inews-domain-name gnus-use-generic-from))
1314                    user-mail-address))) 
1315     (or gnus-user-from-line
1316         (concat address
1317                 ;; User's full name.
1318                 (cond ((string-equal full-name "&") ;Unix hack.
1319                        (concat " (" (user-login-name) ")"))
1320                       ((string-match "[^ ]+@[^ ]+ +(.*)" address)
1321                        "")
1322                       (t
1323                        (concat " (" full-name ")")))))))
1324
1325 (defun gnus-inews-real-user-address ()
1326   "Return the \"real\" user address.
1327 This function tries to ignore all user modifications, and 
1328 give as trustworthy answer as possible."
1329   (concat (user-login-name) "@" (gnus-inews-full-address)))
1330
1331 (defun gnus-inews-login-name ()
1332   "Return login name."
1333   (or gnus-user-login-name (getenv "LOGNAME") (user-login-name)))
1334
1335 (defun gnus-inews-full-name ()
1336   "Return full user name."
1337   (or gnus-user-full-name (getenv "NAME") (user-full-name)))
1338
1339 (defun gnus-inews-domain-name (&optional genericfrom)
1340   "Return user's domain name.
1341 If optional argument GENERICFROM is a string, use it as the domain
1342 name; if it is non-nil, strip off local host name from the domain name.
1343 If the function `system-name' returns full internet name and the
1344 domain is undefined, the domain name is got from it."
1345   (if (or genericfrom gnus-local-domain (getenv "DOMAINNAME"))
1346       (let* ((system-name (system-name))
1347              (domain 
1348               (or (if (stringp genericfrom) genericfrom)
1349                   (getenv "DOMAINNAME")
1350                   gnus-local-domain
1351                   ;; Function `system-name' may return full internet name.
1352                   ;; Suggested by Mike DeCorte <mrd@sun.soe.clarkson.edu>.
1353                   (if (string-match "\\.." system-name)
1354                       ;; Some machines return "name.", and that's not
1355                       ;; very nice. 
1356                       (substring system-name (1- (match-end 0))))
1357                   (read-string "Domain name (no host): ")))
1358              (host (or (if (string-match "\\." system-name)
1359                            (substring system-name 0 (match-beginning 0)))
1360                        system-name)))
1361         (if (string-equal "." (substring domain 0 1))
1362             (setq domain (substring domain 1)))
1363         ;; Support GENERICFROM as same as standard Bnews system.
1364         ;; Suggested by ohm@kaba.junet and vixie@decwrl.dec.com.
1365         (cond ((null genericfrom)
1366                (concat host "." domain))
1367               ;;((stringp genericfrom) genericfrom)
1368               (t domain)))
1369     (if (string-match "\\." (system-name))
1370         (system-name)
1371       (substring user-mail-address 
1372                  (1+ (string-match "@" user-mail-address))))))
1373
1374 (defun gnus-inews-full-address ()
1375   (let ((domain (gnus-inews-domain-name))
1376         (system (system-name))
1377         (case-fold-search t))
1378     (if (string-match "\\." system) system
1379       (if (string-match (concat "^" (regexp-quote system)) domain) domain
1380         (concat system "." domain)))))
1381
1382 (defun gnus-inews-message-id ()
1383   "Generate unique Message-ID for user."
1384   ;; Message-ID should not contain a slash and should be terminated by
1385   ;; a number.  I don't know the reason why it is so.
1386   (concat "<" (gnus-inews-unique-id) "@" (gnus-inews-full-address) ">"))
1387
1388 (defvar gnus-unique-id-char nil)
1389
1390 ;; If you ever change this function, make sure the new version
1391 ;; cannot generate IDs that the old version could.
1392 ;; You might for example insert a "." somewhere (not next to another dot
1393 ;; or string boundary), or modify the newsreader name to "Ding".
1394 (defun gnus-inews-unique-id ()
1395   ;; Dont use microseconds from (current-time), they may be unsupported.
1396   ;; Instead we use this randomly inited counter.
1397   (setq gnus-unique-id-char
1398         (% (1+ (or gnus-unique-id-char (logand (random t) (1- (lsh 1 20)))))
1399            ;; (current-time) returns 16-bit ints,
1400            ;; and 2^16*25 just fits into 4 digits i base 36.
1401            (* 25 25)))
1402   (let ((tm (if (fboundp 'current-time)
1403                 (current-time) '(12191 46742 287898))))
1404     (concat
1405      (if (memq system-type '(ms-dos emx vax-vms))
1406          (let ((user (downcase (gnus-inews-login-name))))
1407            (while (string-match "[^a-z0-9_]" user)
1408              (aset user (match-beginning 0) ?_))
1409            user)
1410        (gnus-number-base36 (user-uid) -1))
1411      (gnus-number-base36 (+ (car   tm) (lsh (% gnus-unique-id-char 25) 16)) 4)
1412      (gnus-number-base36 (+ (nth 1 tm) (lsh (/ gnus-unique-id-char 25) 16)) 4)
1413      ;; Append the newsreader name, because while the generated
1414      ;; ID is unique to this newsreader, other newsreaders might
1415      ;; otherwise generate the same ID via another algorithm.
1416      ".fsf")))
1417
1418
1419 (defun gnus-inews-date ()
1420   "Current time string."
1421   (timezone-make-date-arpa-standard 
1422    (current-time-string) (current-time-zone)))
1423
1424 (defun gnus-inews-organization ()
1425   "Return user's organization.
1426 The ORGANIZATION environment variable is used if defined.
1427 If not, the variable `gnus-local-organization' is used instead.
1428 If it is a function, the function will be called with the current
1429 newsgroup name as the argument.
1430 If this is a file name, the contents of this file will be used as the
1431 organization."
1432   (let* ((organization 
1433           (or (getenv "ORGANIZATION")
1434               (if gnus-local-organization
1435                   (if (gnus-functionp gnus-local-organization)
1436                       (funcall gnus-local-organization gnus-newsgroup-name)
1437                     gnus-local-organization))
1438               gnus-organization-file
1439               "~/.organization")))
1440     (and (stringp organization)
1441          (> (length organization) 0)
1442          (or (file-exists-p organization)
1443              (string-match " " organization)
1444              (not (string-match "^/usr/lib/\\|^~/" organization)))
1445          (save-excursion
1446            (gnus-set-work-buffer)
1447            (if (file-exists-p organization)
1448                (insert-file-contents organization)
1449              (insert organization))
1450            (goto-char (point-min))
1451            (while (re-search-forward " *\n *" nil t)
1452              (replace-match " " t t))
1453            (buffer-substring (point-min) (point-max))))))
1454
1455 (defun gnus-inews-lines ()
1456   "Count the number of lines and return numeric string."
1457   (save-excursion
1458     (save-restriction
1459       (widen)
1460       (goto-char (point-min))
1461       (re-search-forward 
1462        (concat "^" (regexp-quote mail-header-separator) "$"))
1463       (forward-line 1)
1464       (int-to-string (count-lines (point) (point-max))))))
1465
1466 (defun gnus-inews-in-reply-to ()
1467   "Return the In-Reply-To header for this message."
1468   gnus-in-reply-to)
1469
1470 \f
1471 ;;;
1472 ;;; Gnus Mail Functions 
1473 ;;;
1474
1475 ;;; Mail reply commands of Gnus summary mode
1476
1477 (defun gnus-summary-reply (yank &optional yank-articles)
1478   "Reply mail to news author.
1479 If prefix argument YANK is non-nil, original article is yanked automatically.
1480 Customize the variable gnus-mail-reply-method to use another mailer."
1481   (interactive "P")
1482   ;; Bug fix by jbw@bigbird.bu.edu (Joe Wells)
1483   ;; Stripping headers should be specified with mail-yank-ignored-headers.
1484   (gnus-set-global-variables)
1485   (if yank-articles (gnus-summary-goto-subject (car yank-articles)))
1486   (gnus-summary-select-article)
1487   (bury-buffer gnus-article-buffer)
1488   (gnus-mail-reply (or yank-articles (not (not yank)))))
1489
1490 (defun gnus-summary-reply-with-original (n)
1491   "Reply mail to news author with original article.
1492 Customize the variable gnus-mail-reply-method to use another mailer."
1493   (interactive "P")
1494   (gnus-summary-reply t (gnus-summary-work-articles n)))
1495
1496 (defun gnus-summary-mail-forward (post)
1497   "Forward the current message to another user.
1498 Customize the variable gnus-mail-forward-method to use another mailer."
1499   (interactive "P")
1500   (gnus-set-global-variables)
1501   (gnus-summary-select-article)
1502   (gnus-copy-article-buffer)
1503   (if post
1504       (gnus-forward-using-post gnus-article-copy)
1505     (gnus-mail-forward gnus-article-copy)))
1506
1507 (defun gnus-summary-resend-message (address)
1508   "Resend the current article to ADDRESS."
1509   (interactive "sResend message to: ")
1510   (gnus-summary-select-article)
1511   (save-excursion
1512     (let (resent)
1513       ;; We first set up a normal mail buffer.
1514       (nnheader-set-temp-buffer " *Gnus resend*")
1515       (gnus-mail-setup 'new address)
1516       ;; Insert our usual headers.
1517       (gnus-inews-narrow-to-headers)
1518       (let ((headers '(From Date To Message-ID Organization)))
1519         (gnus-inews-insert-headers headers))
1520       (goto-char (point-min))
1521       ;; Rename them all to "Resent-*".
1522       (while (re-search-forward "^" nil t)
1523         (insert "Resent-"))
1524       (widen)
1525       (delete-region (point) (point-max))
1526       ;; Insert the message to be resent.
1527       (insert-buffer-substring gnus-original-article-buffer)
1528       (goto-char (point-min))
1529       (search-forward "\n\n")
1530       (forward-char -1)
1531       (insert mail-header-separator)
1532       ;; Send it.
1533       (mail-send)
1534       (kill-buffer (current-buffer)))))
1535
1536 (defun gnus-summary-post-forward ()
1537   "Forward the current article to a newsgroup."
1538   (interactive)
1539   (gnus-summary-mail-forward t))
1540
1541 (defvar gnus-nastygram-message 
1542   "The following article was inappropriately posted to %s.\n"
1543   "Format string to insert in nastygrams.
1544 The current group name will be inserted at \"%s\".")
1545
1546 (defun gnus-summary-mail-nastygram (n)
1547   "Send a nastygram to the author of the current article."
1548   (interactive "P")
1549   (if (or gnus-expert-user
1550           (gnus-y-or-n-p 
1551            "Really send a nastygram to the author of the current article? "))
1552       (let ((group gnus-newsgroup-name))
1553         (gnus-summary-reply-with-original n)
1554         (set-buffer gnus-mail-buffer)
1555         (insert (format gnus-nastygram-message group))
1556         (gnus-mail-send-and-exit))))
1557
1558 (defun gnus-summary-mail-other-window ()
1559   "Compose mail in other window.
1560 Customize the variable `gnus-mail-other-window-method' to use another
1561 mailer."
1562   (interactive)
1563   (gnus-set-global-variables)
1564   (gnus-new-mail
1565    ;; We might want to prompt here.
1566    (when (and gnus-interactive-post
1567               (not gnus-expert-user))
1568      (read-string "To: ")))
1569   (gnus-configure-windows 'summary-mail 'force))
1570
1571 (defun gnus-new-mail (&optional to)
1572   (let (subject)
1573     (when (and gnus-interactive-post
1574                (not gnus-expert-user))
1575       (setq subject (read-string "Subject: ")))
1576     (pop-to-buffer gnus-mail-buffer)
1577     (erase-buffer)
1578     (gnus-mail-setup 'new to subject)
1579     (run-hooks 'gnus-mail-hook)))
1580
1581 (defun gnus-mail-reply (&optional yank to-address followup)
1582   (save-excursion
1583     (set-buffer gnus-summary-buffer)
1584     (let ((group (gnus-group-real-name gnus-newsgroup-name))
1585           (cur (cons (current-buffer) (cdr gnus-article-current)))
1586           (winconf (current-window-configuration))
1587           from subject date reply-to message-of to cc
1588           references message-id sender follow-to sendto elt new-cc new-to
1589           mct mctdo)
1590       (set-buffer (get-buffer-create gnus-mail-buffer))
1591       (mail-mode)
1592       (if (and (buffer-modified-p)
1593                (> (buffer-size) 0)
1594                (not (gnus-y-or-n-p 
1595                      "Unsent message being composed; erase it? ")))
1596           ()
1597         (erase-buffer)
1598         (save-excursion
1599           (gnus-copy-article-buffer)
1600           (save-restriction
1601             (set-buffer gnus-article-copy)
1602             (gnus-narrow-to-headers)
1603             (if (not followup)
1604                 ;; This is a regular reply.
1605                 (if (gnus-functionp gnus-reply-to-function)
1606                     (setq follow-to (funcall gnus-reply-to-function group)))
1607               ;; This is a followup.
1608               (if (gnus-functionp gnus-followup-to-function)
1609                   (save-excursion
1610                     (setq follow-to
1611                           (funcall gnus-followup-to-function group)))))
1612             (setq from (mail-fetch-field "from"))
1613             (setq date (or (mail-fetch-field "date") 
1614                            (mail-header-date gnus-current-headers)))
1615             (setq message-of (gnus-message-of from date))
1616             (setq sender (mail-fetch-field "sender"))
1617             (setq subject (or (mail-fetch-field "subject") "none"))
1618             ;; Remove any (buggy) Re:'s that are present and make a
1619             ;; proper one.
1620             (and (string-match "^[ \t]*[Re][Ee]:[ \t]*" subject)
1621                  (setq subject (substring subject (match-end 0))))
1622             (setq subject (concat "Re: " subject))
1623             (setq to (mail-fetch-field "to"))
1624             (setq cc (mail-fetch-field "cc"))
1625             (setq mct (mail-fetch-field "mail-copies-to"))
1626             (setq reply-to (mail-fetch-field "reply-to"))
1627             (setq references (mail-fetch-field "references"))
1628             (setq message-id (mail-fetch-field "message-id"))
1629             
1630             (setq mctdo (not (equal mct "never")))
1631
1632             (if (not (and followup (not to-address)))
1633                 (setq new-to (or reply-to from))
1634               (let (ccalist)
1635                 (save-excursion
1636                   (gnus-set-work-buffer)
1637                   (unless (equal mct "never")
1638                     (insert (or reply-to from "")))
1639                   (insert (if (bolp) "" ", ")
1640                           (or to "")
1641                           (if (or (not mct) (not mctdo)) ""
1642                             (concat (if (bolp) "" ", ") mct))
1643                           (if cc (concat (if (bolp) "" ", ") cc) ""))
1644                   (goto-char (point-min))
1645                   (setq ccalist
1646                         (mapcar
1647                          (lambda (addr)
1648                            (cons (mail-strip-quoted-names addr) addr))
1649                          (nreverse (mail-parse-comma-list))))
1650                   (let ((s ccalist))
1651                     (while s
1652                       (setq ccalist (delq (assoc (car (pop s)) s) ccalist)))))
1653                 (setq new-to (cdr (pop ccalist)))
1654                 (setq new-cc 
1655                       (mapconcat 
1656                        (lambda (addr) (cdr addr))
1657                        ccalist ", "))))
1658             (widen)))
1659
1660         (setq news-reply-yank-from (or from "(nobody)"))
1661         (setq news-reply-yank-message-id
1662               (or message-id "(unknown Message-ID)"))
1663
1664         ;; Gather the "to" addresses out of the follow-to list and remove
1665         ;; them as we go.
1666         (if (and follow-to (listp follow-to))
1667             (while (setq elt (assoc "To" follow-to))
1668               (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
1669               (setq follow-to (delq elt follow-to))))
1670
1671         (gnus-mail-setup 
1672          (if followup 'followup 'reply)
1673          (or to-address 
1674              (if (and follow-to (not (stringp follow-to))) sendto
1675                (or follow-to new-to sender "")))
1676          subject message-of
1677          (if (zerop (length new-cc)) nil new-cc)
1678          gnus-article-copy)
1679
1680         (make-local-variable 'gnus-article-reply)
1681         (setq gnus-article-reply cur)
1682         (make-local-variable 'gnus-prev-winconf)
1683         (setq gnus-prev-winconf winconf)
1684         (make-local-variable 'gnus-reply-subject)
1685         (setq gnus-reply-subject subject)
1686         (make-local-variable 'gnus-in-reply-to)
1687         (setq gnus-in-reply-to message-of)
1688
1689         (auto-save-mode auto-save-default)
1690
1691         (if (and follow-to (listp follow-to))
1692             (progn
1693               (goto-char (point-min))
1694               (re-search-forward "^To:" nil t)
1695               (beginning-of-line)
1696               (forward-line 1)
1697               (while follow-to
1698                 (insert (car (car follow-to)) ": " (cdr (car follow-to)) "\n")
1699                 (setq follow-to (cdr follow-to)))))
1700         (nnheader-insert-references references message-id)
1701
1702         ;; Now the headers should be ok, so we do the yanking.
1703         (goto-char (point-min))
1704         (re-search-forward
1705          (concat "^" (regexp-quote mail-header-separator) "$"))
1706         (forward-line 1)
1707         (if (not yank)
1708             (gnus-configure-windows 'reply 'force)
1709           (let ((last (point))
1710                 end)
1711             (if (not (listp yank))
1712                 (progn
1713                   ;; Just a single article being yanked.
1714                   (save-excursion
1715                     (mail-yank-original nil))
1716                   (or mail-yank-hooks mail-citation-hook
1717                       (run-hooks 'news-reply-header-hook)))
1718               (while yank
1719                 (save-window-excursion
1720                   (set-buffer gnus-summary-buffer)
1721                   (gnus-summary-select-article nil nil nil (car yank))
1722                   (gnus-summary-remove-process-mark (car yank)))
1723                 (save-excursion
1724                   (gnus-copy-article-buffer)
1725                   (mail-yank-original nil)
1726                   (save-restriction
1727                     (narrow-to-region (point-min) (point))
1728                     (goto-char (mark))
1729                     (let ((news-reply-yank-from
1730                            (save-excursion 
1731                              (set-buffer gnus-article-buffer)
1732                              (or (mail-fetch-field "from") "(nobody)")))
1733                           (news-reply-yank-message-id
1734                            (save-excursion 
1735                              (set-buffer gnus-article-buffer)
1736                              (or (mail-fetch-field "message-id")
1737                                  "(unknown Message-ID)"))))
1738                       (or mail-yank-hooks mail-citation-hook
1739                           (run-hooks 'news-reply-header-hook))
1740                       (setq end (point-max)))))
1741                 (goto-char end)
1742                 (setq yank (cdr yank))))
1743             (goto-char last))
1744           (forward-line 2)
1745           (gnus-configure-windows 'reply-yank 'force))
1746         (run-hooks 'gnus-mail-hook)))))
1747
1748 (defun gnus-new-news (&optional group inhibit-prompt)
1749   "Set up a *post-news* buffer that points to GROUP.
1750 If INHIBIT-PROMPT, never prompt for a Subject."
1751   (let ((winconf (current-window-configuration))
1752         subject)
1753     (when (and gnus-interactive-post
1754                (not inhibit-prompt)
1755                (not gnus-expert-user))
1756       (setq subject (read-string "Subject: ")))
1757     (pop-to-buffer gnus-post-news-buffer)  
1758     (erase-buffer)
1759     (news-reply-mode)
1760     ;; Let posting styles be configured.
1761     (gnus-configure-posting-styles)
1762     (news-setup nil subject nil (and group (gnus-group-real-name group)) nil)
1763     (goto-char (point-min))
1764
1765     (unless (re-search-forward 
1766              (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1767       (goto-char (point-max)))
1768     (insert "\n\n")
1769
1770     (gnus-inews-insert-bfcc)
1771     (gnus-inews-insert-signature)
1772     (and gnus-post-prepare-function
1773          (gnus-functionp gnus-post-prepare-function)
1774          (funcall gnus-post-prepare-function group))
1775     (goto-char (point-min))
1776     (if group
1777         (re-search-forward "^Subject: " nil t)
1778       (re-search-forward "^Newsgroups: " nil t))
1779     (run-hooks 'gnus-post-prepare-hook)
1780     (make-local-variable 'gnus-prev-winconf)
1781     (setq gnus-prev-winconf winconf)
1782     (gnus-inews-modify-mail-mode-map)
1783     (local-set-key "\C-c\C-c" 'gnus-inews-news)))
1784
1785 (defun gnus-news-followup (&optional yank group)
1786   (save-excursion
1787     (set-buffer gnus-summary-buffer)
1788     (if (not (or (not gnus-novice-user)
1789                  gnus-expert-user
1790                  (gnus-y-or-n-p
1791                   "Are you sure you want to post to all of USENET? ")))
1792         ()
1793       (let ((group (gnus-group-real-name (or group gnus-newsgroup-name)))
1794             (cur (cons (current-buffer) (cdr gnus-article-current)))
1795             (winconf (current-window-configuration))
1796             from subject date reply-to message-of
1797             references message-id sender follow-to sendto elt 
1798             followup-to distribution newsgroups)
1799         (set-buffer (get-buffer-create gnus-post-news-buffer))
1800         (news-reply-mode)
1801         (if (and (buffer-modified-p)
1802                  (> (buffer-size) 0)
1803                  (not (gnus-y-or-n-p 
1804                        "Unsent message being composed; erase it? ")))
1805             ()
1806           (erase-buffer)
1807           (save-excursion
1808             (gnus-copy-article-buffer)
1809             (save-restriction
1810               (set-buffer gnus-article-copy)
1811               (gnus-narrow-to-headers)
1812               (if (gnus-functionp gnus-followup-to-function)
1813                   (save-excursion
1814                     (setq follow-to
1815                           (funcall gnus-followup-to-function group))))
1816               (setq from (mail-fetch-field "from"))
1817               (setq date (or (mail-fetch-field "date") 
1818                              (mail-header-date gnus-current-headers)))
1819               (setq message-of (gnus-message-of from date))
1820               (setq subject (or (mail-fetch-field "subject") "none"))
1821               ;; Remove any (buggy) Re:'s that are present and make a
1822               ;; proper one.
1823               (and (string-match "^[ \t]*[Re][Ee]:[ \t]*" subject)
1824                    (setq subject (substring subject (match-end 0))))
1825               (setq subject (concat "Re: " subject))
1826               (setq references (mail-fetch-field "references"))
1827               (setq message-id (mail-fetch-field "message-id"))
1828               (setq followup-to (mail-fetch-field "followup-to"))
1829               (setq newsgroups (mail-fetch-field "newsgroups"))
1830               (setq distribution (mail-fetch-field "distribution"))
1831               ;; Remove bogus distribution.
1832               (and (stringp distribution)
1833                    (string-match "world" distribution)
1834                    (setq distribution nil))
1835               (widen)))
1836
1837           (setq news-reply-yank-from (or from "(nobody)"))
1838           (setq news-reply-yank-message-id
1839                 (or message-id "(unknown Message-ID)"))
1840
1841           ;; Gather the "to" addresses out of the follow-to list and remove
1842           ;; them as we go.
1843           (if (and follow-to (listp follow-to))
1844               (while (setq elt (assoc "Newsgroups" follow-to))
1845                 (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
1846                 (setq follow-to (delq elt follow-to))))
1847
1848           ;; Let posting styles be configured.
1849           (gnus-configure-posting-styles)
1850
1851           (news-setup nil subject nil 
1852                       (or sendto 
1853                           (and followup-to
1854                                gnus-use-followup-to
1855                                (or (not (eq gnus-use-followup-to 'ask))
1856                                    (gnus-y-or-n-p 
1857                                     (format
1858                                      "Use Followup-To %s? " followup-to)))
1859                                followup-to)
1860                           newsgroups group "")
1861                       gnus-article-copy)
1862
1863           (make-local-variable 'gnus-article-reply)
1864           (setq gnus-article-reply cur)
1865           (make-local-variable 'gnus-prev-winconf)
1866           (setq gnus-prev-winconf winconf)
1867           (make-local-variable 'gnus-reply-subject)
1868           (setq gnus-reply-subject (mail-header-subject gnus-current-headers))
1869           (make-local-variable 'gnus-in-reply-to)
1870           (setq gnus-in-reply-to message-of)
1871
1872           (gnus-inews-insert-signature)
1873
1874           (and gnus-post-prepare-function
1875                (gnus-functionp gnus-post-prepare-function)
1876                (funcall gnus-post-prepare-function group))
1877           (run-hooks 'gnus-post-prepare-hook)
1878
1879           (auto-save-mode auto-save-default)
1880           (gnus-inews-modify-mail-mode-map)
1881           (local-set-key "\C-c\C-c" 'gnus-inews-news)
1882
1883           (if (and follow-to (listp follow-to))
1884               (progn
1885                 (goto-char (point-min))
1886                 (and (re-search-forward "^Newsgroups:" nil t)
1887                      (forward-line 1))
1888                 (while follow-to
1889                   (insert (car (car follow-to)) ": " 
1890                           (cdr (car follow-to)) "\n")
1891                   (setq follow-to (cdr follow-to)))))
1892           
1893           ;; If a distribution existed, we use it.
1894           (if distribution
1895               (progn
1896                 (mail-position-on-field "Distribution")
1897                 (insert distribution)))
1898           
1899           (nnheader-insert-references references message-id)
1900
1901           ;; Handle `gnus-auto-mail-to-author'.
1902           ;; Suggested by Daniel Quinlan <quinlan@best.com>.
1903           ;; Revised to respect Reply-To by Ulrik Dickow <dickow@nbi.dk>.
1904           (let ((to (if (if (eq gnus-auto-mail-to-author 'ask)
1905                             (y-or-n-p "Also send mail to author? ")
1906                           gnus-auto-mail-to-author)
1907                         (or (save-excursion
1908                               (set-buffer gnus-article-copy)
1909                               (gnus-fetch-field "reply-to"))
1910                             from)))
1911                 (x-mail (save-excursion
1912                           (set-buffer gnus-article-copy)
1913                           (gnus-fetch-field "x-mail-copy-to"))))
1914             ;; Deny sending copy if there's a negative X-Mail-Copy-To
1915             ;; header. 
1916             (if x-mail
1917                 (if (and (string= x-mail "never")
1918                          (not (eq gnus-auto-mail-to-author 'force)))
1919                     (setq to nil)
1920                   (setq to x-mail)))
1921             ;; Insert a To or Cc header.
1922             (if to
1923                 (if (mail-fetch-field "To")
1924                     (progn
1925                       (beginning-of-line)
1926                       (insert "Cc: " to "\n"))
1927                   (mail-position-on-field "To")
1928                   (insert to))))
1929
1930           (gnus-inews-insert-bfcc)
1931
1932           ;; Now the headers should be ok, so we do the yanking.
1933           (goto-char (point-min))
1934           (re-search-forward
1935            (concat "^" (regexp-quote mail-header-separator) "$"))
1936           (forward-line 1)
1937           (if (not yank)
1938               (progn
1939                 (gnus-configure-windows 'followup 'force)
1940                 (insert "\n\n")
1941                 (forward-line -2))
1942             (let ((last (point))
1943                   end)
1944               (if (not (listp yank))
1945                   (progn
1946                     (save-excursion
1947                       (mail-yank-original nil))
1948                     (or mail-yank-hooks mail-citation-hook
1949                         (run-hooks 'news-reply-header-hook)))
1950                 (while yank
1951                   (save-window-excursion
1952                     (set-buffer gnus-summary-buffer)
1953                     (gnus-summary-select-article nil nil nil (car yank))
1954                     (gnus-summary-remove-process-mark (car yank)))
1955                   (save-excursion
1956                     (gnus-copy-article-buffer)
1957                     (mail-yank-original nil)
1958                     (setq end (point)))
1959                   (or mail-yank-hooks mail-citation-hook
1960                       (run-hooks 'news-reply-header-hook))
1961                   (goto-char end)
1962                   (setq yank (cdr yank))))
1963               (goto-char last))
1964             (gnus-configure-windows 'followup-yank 'force))
1965         
1966           (make-local-variable 'gnus-article-check-size)
1967           (setq gnus-article-check-size
1968                 (cons (buffer-size) (gnus-article-checksum))))))))
1969
1970 (defun gnus-message-of (from date)
1971   "Take a FROM and a DATE and create an IN-REPLY-TO."
1972   (cond 
1973    ((not from)
1974     nil)
1975    (t
1976     (let ((stop-pos 
1977            (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
1978       (concat (if stop-pos (substring from 0 stop-pos) from)
1979               "'s message of " 
1980               (if (or (not date) (string= date ""))
1981                   "(unknown date)" date))))))
1982
1983 (defun gnus-mail-yank-original ()
1984   (interactive)
1985   (save-excursion
1986     (mail-yank-original nil))
1987   (or mail-yank-hooks mail-citation-hook
1988       (run-hooks 'news-reply-header-hook)))
1989
1990 (defun gnus-mail-send-and-exit (&optional dont-send)
1991   "Send the current mail and return to Gnus."
1992   (interactive)
1993   (let* ((reply gnus-article-reply)
1994          (winconf gnus-prev-winconf)
1995          (address-group gnus-add-to-address)
1996          (to-address (and address-group
1997                           (mail-fetch-field "to"))))
1998     (setq gnus-add-to-address nil)
1999     (or dont-send (gnus-mail-send))
2000     (bury-buffer)
2001     ;; This mail group doesn't have a `to-address', so we add one
2002     ;; here.  Magic!  
2003     (and to-address
2004          (gnus-group-add-parameter 
2005           address-group (cons 'to-address to-address)))
2006     (if (get-buffer gnus-group-buffer)
2007         (progn
2008           (if (gnus-buffer-exists-p (car-safe reply))
2009               (progn
2010                 (set-buffer (car reply))
2011                 (and (cdr reply)
2012                      (gnus-summary-mark-article-as-replied 
2013                       (cdr reply)))))
2014           (and winconf (set-window-configuration winconf))))))
2015
2016 (defun gnus-put-message ()
2017   "Put the current message in some group and return to Gnus."
2018   (interactive)
2019   (let ((reply gnus-article-reply)
2020         (winconf gnus-prev-winconf)
2021         (group gnus-newsgroup-name)
2022         buf)
2023     
2024     (or (and group (not (gnus-group-read-only-p group)))
2025         (setq group (read-string "Put in group: " nil
2026                                  (gnus-writable-groups))))
2027     (and (gnus-gethash group gnus-newsrc-hashtb)
2028          (error "No such group: %s" group))
2029
2030     (save-excursion
2031       (save-restriction
2032         (widen)
2033         (gnus-inews-narrow-to-headers)
2034         (let (gnus-deletable-headers)
2035           (if (eq major-mode 'mail-mode)
2036               (gnus-inews-insert-headers gnus-required-mail-headers)
2037             (gnus-inews-insert-headers)))
2038         (goto-char (point-max))
2039         (insert "Gcc: " group "\n")
2040         (widen)))
2041
2042     (gnus-inews-do-gcc)
2043
2044     (if (get-buffer gnus-group-buffer)
2045         (progn
2046           (if (gnus-buffer-exists-p (car-safe reply))
2047               (progn
2048                 (set-buffer (car reply))
2049                 (and (cdr reply)
2050                      (gnus-summary-mark-article-as-replied 
2051                       (cdr reply)))))
2052           (and winconf (set-window-configuration winconf))))))
2053
2054 (defun gnus-forward-make-subject (buffer)
2055   (save-excursion
2056     (set-buffer buffer)
2057     (concat "[" (if (memq 'mail (assoc (symbol-name 
2058                                         (car (gnus-find-method-for-group 
2059                                               gnus-newsgroup-name)))
2060                                        gnus-valid-select-methods))
2061                     (gnus-fetch-field "From")
2062                   gnus-newsgroup-name)
2063             "] " (or (gnus-fetch-field "Subject") ""))))
2064
2065 (defun gnus-forward-insert-buffer (buffer)
2066   (save-excursion
2067     (save-restriction
2068       (if gnus-signature-before-forwarded-message
2069           (goto-char (point-max))
2070         (goto-char (point-min))
2071         (re-search-forward
2072          (concat "^" (regexp-quote mail-header-separator) "$"))
2073         (forward-line 1))
2074       ;; Narrow to the area we are to insert.
2075       (narrow-to-region (point) (point))
2076       ;; Insert the separators and the forwarded buffer.
2077       (insert gnus-forward-start-separator)
2078       (insert-buffer-substring buffer)
2079       (goto-char (point-max))
2080       (insert gnus-forward-end-separator)
2081       ;; Delete any invisible text.
2082       (goto-char (point-min))
2083       (let (beg)
2084         (while (setq beg (next-single-property-change (point) 'invisible))
2085           (goto-char beg)
2086           (delete-region beg (or (next-single-property-change 
2087                                   (point) 'invisible)
2088                                  (point-max))))))))
2089
2090 (defun gnus-mail-forward (&optional buffer)
2091   "Forward the current message to another user using mail."
2092   (let* ((forward-buffer (or buffer (current-buffer)))
2093          (winconf (current-window-configuration))
2094          (subject (gnus-forward-make-subject forward-buffer)))
2095     (set-buffer (get-buffer-create gnus-mail-buffer))
2096     (if (and (buffer-modified-p)
2097              (> (buffer-size) 0)
2098              (not (gnus-y-or-n-p 
2099                    "Unsent message being composed; erase it? ")))
2100         ()
2101       (erase-buffer)
2102       (gnus-mail-setup 'forward nil subject)
2103       (make-local-variable 'gnus-prev-winconf)
2104       (setq gnus-prev-winconf winconf)
2105       (gnus-forward-insert-buffer forward-buffer)
2106       (goto-char (point-min))
2107       (re-search-forward "^To: ?" nil t)
2108       (gnus-configure-windows 'mail-forward 'force)
2109       ;; You have a chance to arrange the message.
2110       (run-hooks 'gnus-mail-forward-hook)
2111       (run-hooks 'gnus-mail-hook))))
2112
2113 (defun gnus-forward-using-post (&optional buffer)
2114   (save-excursion
2115     (let* ((forward-buffer (or buffer (current-buffer))) 
2116            (subject (gnus-forward-make-subject forward-buffer))
2117            (gnus-newsgroup-name nil))
2118       (gnus-post-news 'post nil nil nil nil subject)
2119       (save-excursion
2120         (gnus-forward-insert-buffer forward-buffer)
2121         ;; You have a chance to arrange the message.
2122         (run-hooks 'gnus-mail-forward-hook)))))
2123
2124 (defun gnus-mail-other-window-using-mail ()
2125   "Compose mail other window using mail."
2126   (let ((winconf (current-window-configuration)))
2127     (mail-other-window nil nil nil nil nil (get-buffer gnus-article-buffer))
2128     (gnus-inews-modify-mail-mode-map)
2129     (make-local-variable 'gnus-prev-winconf)
2130     (setq gnus-prev-winconf winconf)
2131     (run-hooks 'gnus-mail-hook)
2132     (gnus-configure-windows 'summary-mail 'force)))
2133
2134 (defun gnus-article-mail (yank)
2135   "Send a reply to the address near point.
2136 If YANK is non-nil, include the original article."
2137   (interactive "P")
2138   (let ((address 
2139          (buffer-substring
2140           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
2141           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
2142     (and address
2143          (progn
2144            (switch-to-buffer gnus-summary-buffer)
2145            (gnus-mail-reply yank address)))))
2146
2147 (defun gnus-bug ()
2148   "Send a bug report to the Gnus maintainers."
2149   (interactive)
2150   (let ((winconf (current-window-configuration)))
2151     (delete-other-windows)
2152     (switch-to-buffer "*Gnus Help Bug*")
2153     (erase-buffer)
2154     (insert gnus-bug-message)
2155     (goto-char (point-min))
2156     (pop-to-buffer "*Gnus Bug*")
2157     (erase-buffer)
2158     (mail-mode)
2159     (mail-setup gnus-maintainer nil nil nil nil nil)
2160     (auto-save-mode auto-save-default)
2161     (make-local-variable 'gnus-prev-winconf)
2162     (setq gnus-prev-winconf winconf)
2163     (gnus-inews-modify-mail-mode-map)
2164     (local-set-key "\C-c\C-c" 'gnus-bug-mail-send-and-exit)
2165     (goto-char (point-min))
2166     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2167     (forward-line 1)
2168     (insert (format "%s\n%s\n\n\n\n\n" (gnus-version) (emacs-version)))
2169     (gnus-debug)
2170     (goto-char (point-min))
2171     (search-forward "Subject: " nil t)
2172     (message "")))
2173
2174 (defun gnus-bug-mail-send-and-exit ()
2175   "Send the bug message and exit."
2176   (interactive)
2177   (and (get-buffer "*Gnus Help Bug*")
2178        (kill-buffer "*Gnus Help Bug*"))
2179   (gnus-mail-send-and-exit))
2180
2181 (defun gnus-debug ()
2182   "Attemps to go through the Gnus source file and report what variables have been changed.
2183 The source file has to be in the Emacs load path."
2184   (interactive)
2185   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el"))
2186         file dirs expr olist sym)
2187     (message "Please wait while we snoop your variables...")
2188     (sit-for 0)
2189     (save-excursion
2190       (set-buffer (get-buffer-create " *gnus bug info*"))
2191       (buffer-disable-undo (current-buffer))
2192       (while files
2193         (erase-buffer)
2194         (setq dirs load-path)
2195         (while dirs
2196           (if (or (not (car dirs))
2197                   (not (stringp (car dirs)))
2198                   (not (file-exists-p 
2199                         (setq file (concat (file-name-as-directory 
2200                                             (car dirs)) (car files))))))
2201               (setq dirs (cdr dirs))
2202             (setq dirs nil)
2203             (insert-file-contents file)
2204             (goto-char (point-min))
2205             (or (re-search-forward "^;;* *Internal variables" nil t)
2206                 (error "Malformed sources in file %s" file))
2207             (narrow-to-region (point-min) (point))
2208             (goto-char (point-min))
2209             (while (setq expr (condition-case () 
2210                                   (read (current-buffer)) (error nil)))
2211               (condition-case ()
2212                   (and (eq (car expr) 'defvar)
2213                        (stringp (nth 3 expr))
2214                        (or (not (boundp (nth 1 expr)))
2215                            (not (equal (eval (nth 2 expr))
2216                                        (symbol-value (nth 1 expr)))))
2217                        (setq olist (cons (nth 1 expr) olist)))
2218                 (error nil)))))
2219         (setq files (cdr files)))
2220       (kill-buffer (current-buffer)))
2221     (insert "------------------- Environment follows -------------------\n\n")
2222     (while olist
2223       (if (boundp (car olist))
2224           (insert "(setq " (symbol-name (car olist)) 
2225                   (if (or (consp (setq sym (symbol-value (car olist))))
2226                           (and (symbolp sym)
2227                                (not (or (eq sym nil)
2228                                         (eq sym t)))))
2229                       " '" " ")
2230                   (prin1-to-string (symbol-value (car olist))) ")\n")
2231         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2232       (setq olist (cdr olist)))
2233     (insert "\n\n")
2234     ;; Remove any null chars - they seem to cause trouble for some
2235     ;; mailers. (Byte-compiled output from the stuff above.) 
2236     (goto-char (point-min))
2237     (while (re-search-forward "[\000\200]" nil t)
2238       (replace-match "" t t))))
2239
2240
2241 ;;; Treatment of rejected articles.
2242
2243
2244 ;;; Bounced mail.
2245
2246 (defun gnus-summary-resend-bounced-mail (fetch)
2247   "Re-mail the current message.
2248 This only makes sense if the current message is a bounce message than
2249 contains some mail you have written which has been bounced back to
2250 you.
2251 If FETCH, try to fetch the article that this is a reply to, if indeed
2252 this is a reply."
2253   (interactive "P")
2254   (gnus-summary-select-article t)
2255   ;; Create a mail buffer.
2256   (gnus-new-mail)
2257   (erase-buffer)
2258   (insert-buffer-substring gnus-article-buffer)
2259   (goto-char (point-min))
2260   (search-forward "\n\n")
2261   ;; We remove everything before the bounced mail.
2262   (delete-region 
2263    (point-min)
2264    (if (re-search-forward "[^ \t]*:" nil t)
2265        (match-beginning 0)
2266      (point)))
2267   (let (references)
2268     (save-excursion
2269       (save-restriction
2270         (gnus-narrow-to-headers)
2271         (nnheader-remove-header gnus-bounced-headers-junk t)
2272         (setq references (mail-fetch-field "references"))
2273         (goto-char (point-max))
2274         (insert mail-header-separator)))
2275     ;; If there are references, we fetch the article we answered to.  
2276     (and fetch 
2277          references
2278          (string-match "\\(<[^]+>\\)[ \t]*$" references)
2279          (gnus-summary-refer-article 
2280           (substring references (match-beginning 1) (match-end 1)))
2281          (progn
2282            (gnus-summary-show-all-headers)
2283            (gnus-configure-windows 'compose-bounce))))
2284   (goto-char (point-min)))
2285
2286 ;;; Sending mail.
2287
2288 (defun gnus-mail-send ()
2289   "Send the current buffer as mail.
2290 Headers will be generated before sending."
2291   (interactive)
2292   (save-excursion
2293     (save-restriction
2294       (widen)
2295       (gnus-inews-narrow-to-headers)
2296       (gnus-inews-insert-headers gnus-required-mail-headers)
2297       (gnus-inews-remove-empty-headers)))
2298   (widen)
2299   ;; Remove the header separator.
2300   (goto-char (point-min))
2301   (and (re-search-forward
2302         (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2303        (replace-match "" t t))
2304   ;; Run final inews hooks.  This hook may do FCC.
2305   (run-hooks 'gnus-inews-article-hook)
2306   (gnus-inews-do-gcc)
2307   (gnus-narrow-to-headers)
2308   (nnheader-remove-header "^[gf]cc:" t)
2309   (widen)
2310   (goto-char (point-min))
2311   (search-forward "\n\n")
2312   (forward-char -1)
2313   (insert mail-header-separator)
2314   (mail-send)
2315   (run-hooks 'gnus-message-sent-hook))
2316
2317 (defun gnus-inews-modify-mail-mode-map ()
2318   (use-local-map (copy-keymap (current-local-map)))
2319   (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
2320   (local-set-key "\C-c\C-p" 'gnus-put-message)
2321   (local-set-key "\C-c\C-d" 'gnus-enter-into-draft-group))
2322
2323 (defun gnus-mail-setup (type &optional to subject in-reply-to cc
2324                              replybuffer actions)
2325   ;; Let posting styles be configured.
2326   (gnus-configure-posting-styles)
2327   (funcall
2328    (cond
2329     ((or 
2330       (and (or (eq type 'reply) (eq type 'followup))
2331            (eq gnus-mail-reply-method 'gnus-mail-reply-using-mhe))
2332       (and (eq type 'forward)
2333            (eq gnus-mail-forward-method 'gnus-mail-forward-using-mhe))
2334       (and (eq type 'new) 
2335            (eq gnus-mail-other-window-method 
2336                'gnus-mail-other-window-using-mhe)))
2337      'gnus-mh-mail-setup)
2338     ((or 
2339       (and (or (eq type 'reply) (eq type 'followup)) 
2340            (eq gnus-mail-reply-method 'gnus-mail-reply-using-vm))
2341       (and (eq type 'forward)
2342            (eq gnus-mail-forward-method 'gnus-mail-forward-using-vm))
2343       (and (eq type 'new) 
2344            (eq gnus-mail-other-window-method 
2345                'gnus-mail-other-window-using-vm)))
2346      'gnus-vm-mail-setup)
2347     (t 'gnus-sendmail-mail-setup))
2348    to subject in-reply-to cc replybuffer actions))
2349
2350 (defun gnus-sendmail-mail-setup (to subject in-reply-to cc replybuffer actions)
2351   (mail-mode)
2352   (mail-setup to subject nil cc replybuffer actions)
2353   (goto-char (point-min))
2354   (if (re-search-forward 
2355        (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2356       (forward-line 1)
2357     (goto-char (point-max)))
2358 ;  (insert "\n\n")
2359   (gnus-inews-modify-mail-mode-map))
2360   
2361 ;;; Gcc handling.
2362
2363 ;; Do Gcc handling, which copied the message over to some group. 
2364 (defun gnus-inews-do-gcc (&optional gcc)
2365   (save-excursion
2366     (save-restriction
2367       (gnus-narrow-to-headers)
2368       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
2369             end)
2370         (if (not gcc)
2371             () ; Nothing to be done.
2372           (nnheader-remove-header "gcc")
2373           ;; Copy the article over to some group(s).
2374           (while (string-match
2375                   "^[ \t]*\\([^ \t]+\\)\\([ \t]+\\|$\\)" gcc)
2376             (setq end (match-end 0))
2377             (condition-case ()
2378                 (gnus-request-accept-article 
2379                  (substring gcc (match-beginning 1) (match-end 1)) t)
2380               (error nil))
2381             (setq gcc (substring gcc end))))))))
2382
2383 (defun gnus-inews-insert-bfcc ()
2384   "Insert Bcc and Fcc headers."
2385   (save-excursion
2386     ;; Handle author copy using BCC field.
2387     (when (and gnus-mail-self-blind
2388                (not (mail-fetch-field "bcc")))
2389       (mail-position-on-field "Bcc")
2390       (insert (if (stringp gnus-mail-self-blind)
2391                   gnus-mail-self-blind
2392                 (user-login-name))))
2393     ;; Handle author copy using FCC field.
2394     (when gnus-author-copy
2395       (mail-position-on-field "Fcc")
2396       (insert gnus-author-copy))))
2397
2398 (defun gnus-inews-insert-gcc ()
2399   (let* ((group gnus-outgoing-message-group)
2400          (gcc (cond 
2401                ((gnus-functionp group)
2402                 (funcall group))
2403                ((or (stringp group) (list group))
2404                 group))))
2405     (when gcc
2406       (insert "Gcc: "
2407               (if (stringp group) group
2408                 (mapconcat 'identity group " "))
2409               "\n"))))
2410
2411 ;;; Handling rejected (and postponed) news.
2412
2413 (defun gnus-draft-group ()
2414   "Return the name of the draft group."
2415   (gnus-group-prefixed-name 
2416    (file-name-nondirectory gnus-draft-group-directory)
2417    (list 'nndir gnus-draft-group-directory)))
2418
2419 (defun gnus-make-draft-group ()
2420   "Make the draft group or die trying."
2421   (let* ((method (` (nndir "private" 
2422                            (nndir-directory (, gnus-draft-group-directory)))))
2423          (group (gnus-group-prefixed-name 
2424                  (file-name-nondirectory gnus-draft-group-directory)
2425                  method)))
2426     (or (gnus-gethash group gnus-newsrc-hashtb)
2427         (gnus-group-make-group (gnus-group-real-name group) method)
2428         (error "Can't create the draft group"))
2429     group))
2430
2431 (defun gnus-enter-into-draft-group ()
2432   "Enter the current buffer into the draft group."
2433   (interactive)
2434   (gnus-put-in-draft-group t))
2435
2436 (defun gnus-put-in-draft-group (&optional generate silent)
2437   "Does the actual putting."
2438   (let ((group (gnus-make-draft-group))
2439         (type (list major-mode (buffer-name) gnus-newsgroup-name
2440                     (point)))
2441         (mode major-mode)
2442         (buf (current-buffer)))
2443     (widen)
2444     (save-excursion
2445       (nnheader-set-temp-buffer " *enter-draft*")
2446       (insert-buffer-substring buf)
2447       (save-restriction
2448         (widen)
2449         (gnus-inews-narrow-to-headers)
2450         (let (gnus-deletable-headers)
2451           (if (eq mode 'mail-mode)
2452               (gnus-inews-insert-headers gnus-required-mail-headers)
2453             (gnus-inews-insert-headers)))
2454         (widen))
2455
2456       (goto-char (point-min))
2457       ;; We have to store whether we are in a mail group or news group. 
2458       (insert (format "X-Gnus-Draft-Type: %S\n" type))
2459       (and (re-search-forward
2460             (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2461            (replace-match "" t t))
2462       (if (prog1
2463               (gnus-request-accept-article group t)
2464             (kill-buffer (current-buffer)))
2465           (or silent
2466               (gnus-mail-send-and-exit 'dont-send))))
2467     (set-buffer-modified-p nil)))
2468
2469 (defun gnus-summary-send-draft ()
2470   "Enter a mail/post buffer to edit and send the draft."
2471   (interactive)
2472   (gnus-set-global-variables)
2473   (gnus-summary-select-article t)
2474   ;; First we find the draft type.
2475   (let (type)
2476     (save-excursion 
2477       (set-buffer gnus-article-buffer)
2478       (widen)
2479       (gnus-narrow-to-headers)
2480       (setq type (condition-case ()
2481                      (read (mail-fetch-field "x-gnus-draft-type"))
2482                    (error nil)))
2483       (widen))
2484     (or type
2485         (error "Unknown draft type"))
2486     ;; Get to the proper buffer.
2487     (set-buffer (get-buffer-create (nth 1 type)))
2488     ;; It might be modified.
2489     (and (buffer-modified-p)
2490          (or (gnus-yes-or-no-p "Unsent message being composed; discard it? ")
2491              (error "Break")))
2492     (setq buffer-read-only nil)
2493     (buffer-enable-undo (current-buffer))
2494     (erase-buffer)
2495     ;; Set proper mode.
2496     (funcall (car type))
2497     (and (eq major-mode 'mail-mode)
2498          (gnus-inews-modify-mail-mode-map))
2499     ;; Arrange for deletion of the draft after successful sending.
2500     (make-local-variable 'gnus-message-sent-hook)
2501     (setq gnus-message-sent-hook
2502           (list
2503            (`
2504             (lambda ()
2505               (gnus-request-expire-articles 
2506                (quote (, (list (cdr gnus-article-current))))
2507                (, gnus-newsgroup-name) t)))))
2508     ;; Insert the draft.
2509     (insert-buffer-substring gnus-article-buffer)
2510     ;; Insert the separator.
2511     (goto-char (point-min))
2512     (search-forward "\n\n")
2513     (forward-char -1)
2514     (insert mail-header-separator)
2515     ;; Remove the draft header.
2516     (gnus-inews-narrow-to-headers)
2517     (nnheader-remove-header "x-gnus-draft-type")
2518     (widen)
2519     ;; Configure windows.
2520     (let ((gnus-draft-buffer (current-buffer)))
2521       (gnus-configure-windows 'draft))
2522     ;; Put point where you left it.
2523     (goto-char (nth 3 type))))
2524   
2525 (defun gnus-configure-posting-styles ()
2526   "Configure posting styles according to `gnus-posting-styles'."
2527   (let ((styles gnus-posting-styles)
2528         (gnus-newsgroup-name (or gnus-newsgroup-name ""))
2529         style match variable attribute value value-value)
2530     ;; Go through all styles and look for matches.
2531     (while styles
2532       (setq style (pop styles)
2533             match (pop style))
2534       (when (cond ((stringp match)
2535                    ;; Regexp string match on the group name.
2536                    (string-match match gnus-newsgroup-name))
2537                   ((or (symbolp match)
2538                        (gnus-functionp match))
2539                    (cond ((gnus-functionp match)
2540                           ;; Function to be called.
2541                           (funcall match))
2542                          ((boundp match)
2543                           ;; Variable to be checked.
2544                           (symbol-value match))))
2545                   ((listp match)
2546                    ;; This is a form to be evaled.
2547                    (eval match)))
2548         ;; We have a match, so we set the variables.
2549         (while style
2550           (setq attribute (pop style)
2551                 value (cdr attribute))
2552           ;; We find the variable that is to be modified.
2553           (if (and (not (stringp (car attribute)))
2554                    (not (setq variable (cdr (assq (car attribute) 
2555                                                   gnus-posting-style-alist)))))
2556               (message "Couldn't find attribute %s" (car attribute))
2557             ;; We set the variable.
2558             (setq value-value
2559                   (cond ((stringp value)
2560                          value)
2561                         ((or (symbolp value)
2562                              (gnus-functionp value))
2563                          (cond ((gnus-functionp value)
2564                                 (funcall value))
2565                                ((boundp value)
2566                                 (symbol-value value))))
2567                         ((listp value)
2568                          (eval value))))
2569             (if variable
2570                 (progn
2571                   ;; This is an ordinary variable.
2572                   (make-local-variable variable)
2573                   (set variable value-value))
2574               ;; This is a header to be added to the headers when
2575               ;; posting. 
2576               (when value-value
2577                 (make-local-variable gnus-required-headers)
2578                 (make-local-variable gnus-required-mail-headers)
2579                 (push (cons (car attribute) value-value) 
2580                       gnus-required-headers)
2581                 (push (cons (car attribute) value-value) 
2582                       gnus-required-mail-headers)))))))))
2583
2584 ;;; Allow redefinition of functions.
2585
2586 (gnus-ems-redefine)
2587
2588 (provide 'gnus-msg)
2589
2590 ;;; gnus-msg.el ends here