(Fxw_color_values): Return 3-element list. Doc fix.
[emacs.git] / lisp / gnus / gnus-soup.el
blobb63892019dd3a8e0d867ef90020bb81a02d77a37
1 ;;; gnus-soup.el --- SOUP packet writing support for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; Keywords: news, mail
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile (require 'cl))
31 (require 'gnus)
32 (require 'gnus-art)
33 (require 'message)
34 (require 'gnus-start)
35 (require 'gnus-range)
37 (defgroup gnus-soup nil
38 "SOUP packet writing support for Gnus."
39 :group 'gnus)
41 ;;; User Variables:
43 (defcustom gnus-soup-directory (nnheader-concat gnus-home-directory "SoupBrew/")
44 "Directory containing an unpacked SOUP packet."
45 :version "22.1" ;; Gnus 5.10.9
46 :type 'directory
47 :group 'gnus-soup)
49 (defcustom gnus-soup-replies-directory
50 (nnheader-concat gnus-soup-directory "SoupReplies/")
51 "Directory where Gnus will do processing of replies."
52 :version "22.1" ;; Gnus 5.10.9
53 :type 'directory
54 :group 'gnus-soup)
56 (defcustom gnus-soup-prefix-file "gnus-prefix"
57 "Name of the file where Gnus stores the last used prefix."
58 :version "22.1" ;; Gnus 5.10.9
59 :type 'file
60 :group 'gnus-soup)
62 (defcustom gnus-soup-packer "tar cf - %s | gzip > $HOME/Soupout%d.tgz"
63 "Format string command for packing a SOUP packet.
64 The SOUP files will be inserted where the %s is in the string.
65 This string MUST contain both %s and %d. The file number will be
66 inserted where %d appears."
67 :version "22.1" ;; Gnus 5.10.9
68 :type 'string
69 :group 'gnus-soup)
71 (defcustom gnus-soup-unpacker "gunzip -c %s | tar xvf -"
72 "Format string command for unpacking a SOUP packet.
73 The SOUP packet file name will be inserted at the %s."
74 :version "22.1" ;; Gnus 5.10.9
75 :type 'string
76 :group 'gnus-soup)
78 (defcustom gnus-soup-packet-directory gnus-home-directory
79 "Where gnus-soup will look for REPLIES packets."
80 :version "22.1" ;; Gnus 5.10.9
81 :type 'directory
82 :group 'gnus-soup)
84 (defcustom gnus-soup-packet-regexp "Soupin"
85 "Regular expression matching SOUP REPLIES packets in `gnus-soup-packet-directory'."
86 :version "22.1" ;; Gnus 5.10.9
87 :type 'regexp
88 :group 'gnus-soup)
90 (defcustom gnus-soup-ignored-headers "^Xref:"
91 "Regexp to match headers to be removed when brewing SOUP packets."
92 :version "22.1" ;; Gnus 5.10.9
93 :type 'regexp
94 :group 'gnus-soup)
96 ;;; Internal Variables:
98 (defvar gnus-soup-encoding-type ?u
99 "*Soup encoding type.
100 `u' is USENET news format, `m' is Unix mbox format, and `M' is MMDF mailbox
101 format.")
103 (defvar gnus-soup-index-type ?c
104 "*Soup index type.
105 `n' means no index file and `c' means standard Cnews overview
106 format.")
108 (defvar gnus-soup-areas nil)
109 (defvar gnus-soup-last-prefix nil)
110 (defvar gnus-soup-prev-prefix nil)
111 (defvar gnus-soup-buffers nil)
113 ;;; Access macros:
115 (defmacro gnus-soup-area-prefix (area)
116 `(aref ,area 0))
117 (defmacro gnus-soup-set-area-prefix (area prefix)
118 `(aset ,area 0 ,prefix))
119 (defmacro gnus-soup-area-name (area)
120 `(aref ,area 1))
121 (defmacro gnus-soup-area-encoding (area)
122 `(aref ,area 2))
123 (defmacro gnus-soup-area-description (area)
124 `(aref ,area 3))
125 (defmacro gnus-soup-area-number (area)
126 `(aref ,area 4))
127 (defmacro gnus-soup-area-set-number (area value)
128 `(aset ,area 4 ,value))
130 (defmacro gnus-soup-encoding-format (encoding)
131 `(aref ,encoding 0))
132 (defmacro gnus-soup-encoding-index (encoding)
133 `(aref ,encoding 1))
134 (defmacro gnus-soup-encoding-kind (encoding)
135 `(aref ,encoding 2))
137 (defmacro gnus-soup-reply-prefix (reply)
138 `(aref ,reply 0))
139 (defmacro gnus-soup-reply-kind (reply)
140 `(aref ,reply 1))
141 (defmacro gnus-soup-reply-encoding (reply)
142 `(aref ,reply 2))
144 ;;; Commands:
146 (defun gnus-soup-send-replies ()
147 "Unpack and send all replies in the reply packet."
148 (interactive)
149 (let ((packets (directory-files
150 gnus-soup-packet-directory t gnus-soup-packet-regexp)))
151 (while packets
152 (when (gnus-soup-send-packet (car packets))
153 (delete-file (car packets)))
154 (setq packets (cdr packets)))))
156 (defun gnus-soup-add-article (n)
157 "Add the current article to SOUP packet.
158 If N is a positive number, add the N next articles.
159 If N is a negative number, add the N previous articles.
160 If N is nil and any articles have been marked with the process mark,
161 move those articles instead."
162 (interactive "P")
163 (let* ((articles (gnus-summary-work-articles n))
164 (tmp-buf (gnus-get-buffer-create "*soup work*"))
165 (area (gnus-soup-area gnus-newsgroup-name))
166 (prefix (gnus-soup-area-prefix area))
167 headers)
168 (buffer-disable-undo tmp-buf)
169 (save-excursion
170 (while articles
171 ;; Put the article in a buffer.
172 (set-buffer tmp-buf)
173 (when (gnus-request-article-this-buffer
174 (car articles) gnus-newsgroup-name)
175 (setq headers (nnheader-parse-head t))
176 (save-restriction
177 (message-narrow-to-head)
178 (message-remove-header gnus-soup-ignored-headers t))
179 (gnus-soup-store gnus-soup-directory prefix headers
180 gnus-soup-encoding-type
181 gnus-soup-index-type)
182 (gnus-soup-area-set-number
183 area (1+ (or (gnus-soup-area-number area) 0)))
184 ;; Mark article as read.
185 (set-buffer gnus-summary-buffer)
186 (gnus-summary-mark-as-read (car articles) gnus-souped-mark))
187 (gnus-summary-remove-process-mark (car articles))
188 (setq articles (cdr articles)))
189 (kill-buffer tmp-buf))
190 (gnus-soup-save-areas)
191 (gnus-set-mode-line 'summary)))
193 (defun gnus-soup-pack-packet ()
194 "Make a SOUP packet from the SOUP areas."
195 (interactive)
196 (gnus-soup-read-areas)
197 (if (file-exists-p gnus-soup-directory)
198 (if (directory-files gnus-soup-directory nil "\\.MSG$")
199 (gnus-soup-pack gnus-soup-directory gnus-soup-packer)
200 (message "No files to pack."))
201 (message "No such directory: %s" gnus-soup-directory)))
203 (defun gnus-group-brew-soup (n)
204 "Make a soup packet from the current group.
205 Uses the process/prefix convention."
206 (interactive "P")
207 (let ((groups (gnus-group-process-prefix n)))
208 (while groups
209 (gnus-group-remove-mark (car groups))
210 (gnus-soup-group-brew (car groups) t)
211 (setq groups (cdr groups)))
212 (gnus-soup-save-areas)))
214 (defun gnus-brew-soup (&optional level)
215 "Go through all groups on LEVEL or less and make a soup packet."
216 (interactive "P")
217 (let ((level (or level gnus-level-subscribed))
218 (newsrc (cdr gnus-newsrc-alist)))
219 (while newsrc
220 (when (<= (nth 1 (car newsrc)) level)
221 (gnus-soup-group-brew (caar newsrc) t))
222 (setq newsrc (cdr newsrc)))
223 (gnus-soup-save-areas)))
225 ;;;###autoload
226 (defun gnus-batch-brew-soup ()
227 "Brew a SOUP packet from groups mention on the command line.
228 Will use the remaining command line arguments as regular expressions
229 for matching on group names.
231 For instance, if you want to brew on all the nnml groups, as well as
232 groups with \"emacs\" in the name, you could say something like:
234 $ emacs -batch -f gnus-batch-brew-soup ^nnml \".*emacs.*\"
236 Note -- this function hasn't been implemented yet."
237 (interactive)
238 nil)
240 ;;; Internal Functions:
242 ;; Store the current buffer.
243 (defun gnus-soup-store (directory prefix headers format index)
244 ;; Create the directory, if needed.
245 (gnus-make-directory directory)
246 (let* ((msg-buf (nnheader-find-file-noselect
247 (concat directory prefix ".MSG")))
248 (idx-buf (if (= index ?n)
250 (nnheader-find-file-noselect
251 (concat directory prefix ".IDX"))))
252 (article-buf (current-buffer))
253 from head-line beg type)
254 (setq gnus-soup-buffers (cons msg-buf (delq msg-buf gnus-soup-buffers)))
255 (buffer-disable-undo msg-buf)
256 (when idx-buf
257 (push idx-buf gnus-soup-buffers)
258 (buffer-disable-undo idx-buf))
259 (save-excursion
260 ;; Make sure the last char in the buffer is a newline.
261 (goto-char (point-max))
262 (unless (= (current-column) 0)
263 (insert "\n"))
264 ;; Find the "from".
265 (goto-char (point-min))
266 (setq from
267 (gnus-mail-strip-quoted-names
268 (or (mail-fetch-field "from")
269 (mail-fetch-field "really-from")
270 (mail-fetch-field "sender"))))
271 (goto-char (point-min))
272 ;; Depending on what encoding is supposed to be used, we make
273 ;; a soup header.
274 (setq head-line
275 (cond
276 ((or (= gnus-soup-encoding-type ?u)
277 (= gnus-soup-encoding-type ?n)) ;;Gnus back compatibility.
278 (format "#! rnews %d\n" (buffer-size)))
279 ((= gnus-soup-encoding-type ?m)
280 (while (search-forward "\nFrom " nil t)
281 (replace-match "\n>From " t t))
282 (concat "From " (or from "unknown")
283 " " (current-time-string) "\n"))
284 ((= gnus-soup-encoding-type ?M)
285 "\^a\^a\^a\^a\n")
286 (t (error "Unsupported type: %c" gnus-soup-encoding-type))))
287 ;; Insert the soup header and the article in the MSG buf.
288 (set-buffer msg-buf)
289 (goto-char (point-max))
290 (insert head-line)
291 (setq beg (point))
292 (insert-buffer-substring article-buf)
293 ;; Insert the index in the IDX buf.
294 (cond ((= index ?c)
295 (set-buffer idx-buf)
296 (gnus-soup-insert-idx beg headers))
297 ((/= index ?n)
298 (error "Unknown index type: %c" type)))
299 ;; Return the MSG buf.
300 msg-buf)))
302 (defun gnus-soup-group-brew (group &optional not-all)
303 "Enter GROUP and add all articles to a SOUP package.
304 If NOT-ALL, don't pack ticked articles."
305 (let ((gnus-expert-user t)
306 (gnus-large-newsgroup nil)
307 (entry (gnus-group-entry group)))
308 (when (or (null entry)
309 (eq (car entry) t)
310 (and (car entry)
311 (> (car entry) 0))
312 (and (not not-all)
313 (gnus-range-length (cdr (assq 'tick (gnus-info-marks
314 (nth 2 entry)))))))
315 (when (gnus-summary-read-group group nil t)
316 (setq gnus-newsgroup-processable
317 (reverse
318 (if (not not-all)
319 (append gnus-newsgroup-marked gnus-newsgroup-unreads)
320 gnus-newsgroup-unreads)))
321 (gnus-soup-add-article nil)
322 (gnus-summary-exit)))))
324 (defun gnus-soup-insert-idx (offset header)
325 ;; [number subject from date id references chars lines xref]
326 (goto-char (point-max))
327 (insert
328 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t\t\n"
329 offset
330 (or (mail-header-subject header) "(none)")
331 (or (mail-header-from header) "(nobody)")
332 (or (mail-header-date header) "")
333 (or (mail-header-id header)
334 (concat "soup-dummy-id-"
335 (mapconcat
336 (lambda (time) (int-to-string time))
337 (current-time) "-")))
338 (or (mail-header-references header) "")
339 (or (mail-header-chars header) 0)
340 (or (mail-header-lines header) "0"))))
342 (defun gnus-soup-save-areas ()
343 "Write all SOUP buffers."
344 (interactive)
345 (gnus-soup-write-areas)
346 (save-excursion
347 (let (buf)
348 (while gnus-soup-buffers
349 (setq buf (car gnus-soup-buffers)
350 gnus-soup-buffers (cdr gnus-soup-buffers))
351 (if (not (buffer-name buf))
353 (set-buffer buf)
354 (when (buffer-modified-p)
355 (save-buffer))
356 (kill-buffer (current-buffer)))))
357 (gnus-soup-write-prefixes)))
359 (defun gnus-soup-write-prefixes ()
360 (let ((prefixes gnus-soup-last-prefix)
361 prefix)
362 (save-excursion
363 (gnus-set-work-buffer)
364 (while (setq prefix (pop prefixes))
365 (erase-buffer)
366 (insert (format "(setq gnus-soup-prev-prefix %d)\n" (cdr prefix)))
367 (let ((coding-system-for-write mm-text-coding-system))
368 (gnus-write-buffer (concat (car prefix) gnus-soup-prefix-file)))))))
370 (defun gnus-soup-pack (dir packer)
371 (let* ((files (mapconcat 'identity
372 '("AREAS" "*.MSG" "*.IDX" "INFO"
373 "LIST" "REPLIES" "COMMANDS" "ERRORS")
374 " "))
375 (packer (if (< (string-match "%s" packer)
376 (string-match "%d" packer))
377 (format packer files
378 (string-to-number (gnus-soup-unique-prefix dir)))
379 (format packer
380 (string-to-number (gnus-soup-unique-prefix dir))
381 files)))
382 (dir (expand-file-name dir)))
383 (gnus-make-directory dir)
384 (setq gnus-soup-areas nil)
385 (gnus-message 4 "Packing %s..." packer)
386 (if (eq 0 (call-process shell-file-name
387 nil nil nil shell-command-switch
388 (concat "cd " dir " ; " packer)))
389 (progn
390 (call-process shell-file-name nil nil nil shell-command-switch
391 (concat "cd " dir " ; rm " files))
392 (gnus-message 4 "Packing...done" packer))
393 (error "Couldn't pack packet"))))
395 (defun gnus-soup-parse-areas (file)
396 "Parse soup area file FILE.
397 The result is a of vectors, each containing one entry from the AREA file.
398 The vector contain five strings,
399 [prefix name encoding description number]
400 though the two last may be nil if they are missing."
401 (let (areas)
402 (when (file-exists-p file)
403 (save-excursion
404 (set-buffer (nnheader-find-file-noselect file 'force))
405 (buffer-disable-undo)
406 (goto-char (point-min))
407 (while (not (eobp))
408 (push (vector (gnus-soup-field)
409 (gnus-soup-field)
410 (gnus-soup-field)
411 (and (eq (preceding-char) ?\t)
412 (gnus-soup-field))
413 (and (eq (preceding-char) ?\t)
414 (string-to-number (gnus-soup-field))))
415 areas)
416 (when (eq (preceding-char) ?\t)
417 (beginning-of-line 2)))
418 (kill-buffer (current-buffer))))
419 areas))
421 (defun gnus-soup-parse-replies (file)
422 "Parse soup REPLIES file FILE.
423 The result is a of vectors, each containing one entry from the REPLIES
424 file. The vector contain three strings, [prefix name encoding]."
425 (let (replies)
426 (save-excursion
427 (set-buffer (nnheader-find-file-noselect file))
428 (buffer-disable-undo)
429 (goto-char (point-min))
430 (while (not (eobp))
431 (push (vector (gnus-soup-field) (gnus-soup-field)
432 (gnus-soup-field))
433 replies)
434 (when (eq (preceding-char) ?\t)
435 (beginning-of-line 2)))
436 (kill-buffer (current-buffer)))
437 replies))
439 (defun gnus-soup-field ()
440 (prog1
441 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
442 (forward-char 1)))
444 (defun gnus-soup-read-areas ()
445 (or gnus-soup-areas
446 (setq gnus-soup-areas
447 (gnus-soup-parse-areas (concat gnus-soup-directory "AREAS")))))
449 (defun gnus-soup-write-areas ()
450 "Write the AREAS file."
451 (interactive)
452 (when gnus-soup-areas
453 (with-temp-file (concat gnus-soup-directory "AREAS")
454 (let ((areas gnus-soup-areas)
455 area)
456 (while (setq area (pop areas))
457 (insert
458 (format
459 "%s\t%s\t%s%s\n"
460 (gnus-soup-area-prefix area)
461 (gnus-soup-area-name area)
462 (gnus-soup-area-encoding area)
463 (if (or (gnus-soup-area-description area)
464 (gnus-soup-area-number area))
465 (concat "\t" (or (gnus-soup-area-description
466 area) "")
467 (if (gnus-soup-area-number area)
468 (concat "\t" (int-to-string
469 (gnus-soup-area-number area)))
470 "")) ""))))))))
472 (defun gnus-soup-write-replies (dir areas)
473 "Write a REPLIES file in DIR containing AREAS."
474 (with-temp-file (concat dir "REPLIES")
475 (let (area)
476 (while (setq area (pop areas))
477 (insert (format "%s\t%s\t%s\n"
478 (gnus-soup-reply-prefix area)
479 (gnus-soup-reply-kind area)
480 (gnus-soup-reply-encoding area)))))))
482 (defun gnus-soup-area (group)
483 (gnus-soup-read-areas)
484 (let ((areas gnus-soup-areas)
485 (real-group (gnus-group-real-name group))
486 area result)
487 (while areas
488 (setq area (car areas)
489 areas (cdr areas))
490 (when (equal (gnus-soup-area-name area) real-group)
491 (setq result area)))
492 (unless result
493 (setq result
494 (vector (gnus-soup-unique-prefix)
495 real-group
496 (format "%c%c%c"
497 gnus-soup-encoding-type
498 gnus-soup-index-type
499 (if (gnus-member-of-valid 'mail group) ?m ?n))
500 nil nil)
501 gnus-soup-areas (cons result gnus-soup-areas)))
502 result))
504 (defun gnus-soup-unique-prefix (&optional dir)
505 (let* ((dir (file-name-as-directory (or dir gnus-soup-directory)))
506 (entry (assoc dir gnus-soup-last-prefix))
507 gnus-soup-prev-prefix)
508 (if entry
510 (when (file-exists-p (concat dir gnus-soup-prefix-file))
511 (ignore-errors
512 (load (concat dir gnus-soup-prefix-file) nil t t)))
513 (push (setq entry (cons dir (or gnus-soup-prev-prefix 0)))
514 gnus-soup-last-prefix))
515 (setcdr entry (1+ (cdr entry)))
516 (gnus-soup-write-prefixes)
517 (int-to-string (cdr entry))))
519 (defun gnus-soup-unpack-packet (dir unpacker packet)
520 "Unpack PACKET into DIR using UNPACKER.
521 Return whether the unpacking was successful."
522 (gnus-make-directory dir)
523 (gnus-message 4 "Unpacking: %s" (format unpacker packet))
524 (prog1
525 (eq 0 (call-process
526 shell-file-name nil nil nil shell-command-switch
527 (format "cd %s ; %s" (expand-file-name dir)
528 (format unpacker packet))))
529 (gnus-message 4 "Unpacking...done")))
531 (defun gnus-soup-send-packet (packet)
532 (gnus-soup-unpack-packet
533 gnus-soup-replies-directory gnus-soup-unpacker packet)
534 (let ((replies (gnus-soup-parse-replies
535 (concat gnus-soup-replies-directory "REPLIES"))))
536 (save-excursion
537 (while replies
538 (let* ((msg-file (concat gnus-soup-replies-directory
539 (gnus-soup-reply-prefix (car replies))
540 ".MSG"))
541 (msg-buf (and (file-exists-p msg-file)
542 (nnheader-find-file-noselect msg-file)))
543 (tmp-buf (gnus-get-buffer-create " *soup send*"))
544 beg end)
545 (cond
546 ((and (/= (gnus-soup-encoding-format
547 (gnus-soup-reply-encoding (car replies)))
549 (/= (gnus-soup-encoding-format
550 (gnus-soup-reply-encoding (car replies)))
551 ?n)) ;; Gnus back compatibility.
552 (error "Unsupported encoding"))
553 ((null msg-buf)
556 (buffer-disable-undo msg-buf)
557 (set-buffer msg-buf)
558 (goto-char (point-min))
559 (while (not (eobp))
560 (unless (looking-at "#! *rnews +\\([0-9]+\\)")
561 (error "Bad header"))
562 (forward-line 1)
563 (setq beg (point)
564 end (+ (point) (string-to-number
565 (buffer-substring
566 (match-beginning 1) (match-end 1)))))
567 (switch-to-buffer tmp-buf)
568 (erase-buffer)
569 (mm-disable-multibyte)
570 (insert-buffer-substring msg-buf beg end)
571 (cond
572 ((string= (gnus-soup-reply-kind (car replies)) "news")
573 (gnus-message 5 "Sending news message to %s..."
574 (mail-fetch-field "newsgroups"))
575 (sit-for 1)
576 (let ((message-syntax-checks
577 'dont-check-for-anything-just-trust-me)
578 (method (if (functionp message-post-method)
579 (funcall message-post-method)
580 message-post-method))
581 result)
582 (run-hooks 'message-send-news-hook)
583 (gnus-open-server method)
584 (message "Sending news via %s..."
585 (gnus-server-string method))
586 (unless (let ((mail-header-separator ""))
587 (gnus-request-post method))
588 (message "Couldn't send message via news: %s"
589 (nnheader-get-report (car method))))))
590 ((string= (gnus-soup-reply-kind (car replies)) "mail")
591 (gnus-message 5 "Sending mail to %s..."
592 (mail-fetch-field "to"))
593 (sit-for 1)
594 (let ((mail-header-separator ""))
595 (funcall (or message-send-mail-real-function
596 message-send-mail-function))))
598 (error "Unknown reply kind")))
599 (set-buffer msg-buf)
600 (goto-char end))
601 (delete-file (buffer-file-name))
602 (kill-buffer msg-buf)
603 (kill-buffer tmp-buf)
604 (gnus-message 4 "Sent packet"))))
605 (setq replies (cdr replies)))
606 t)))
608 (provide 'gnus-soup)
610 ;; arch-tag: eddfa69d-13e8-4aea-84ef-62a526ef185c
611 ;;; gnus-soup.el ends here