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