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/>.
29 (eval-when-compile (require 'cl
))
37 (defgroup gnus-soup nil
38 "SOUP packet writing support for Gnus."
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
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
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
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
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
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
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
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
96 ;;; Internal Variables:
98 (defvar gnus-soup-encoding-type ?u
100 `u' is USENET news format, `m' is Unix mbox format, and `M' is MMDF mailbox
103 (defvar gnus-soup-index-type ?c
105 `n' means no index file and `c' means standard Cnews overview
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
)
115 (defmacro gnus-soup-area-prefix
(area)
117 (defmacro gnus-soup-set-area-prefix
(area prefix
)
118 `(aset ,area
0 ,prefix
))
119 (defmacro gnus-soup-area-name
(area)
121 (defmacro gnus-soup-area-encoding
(area)
123 (defmacro gnus-soup-area-description
(area)
125 (defmacro gnus-soup-area-number
(area)
127 (defmacro gnus-soup-area-set-number
(area value
)
128 `(aset ,area
4 ,value
))
130 (defmacro gnus-soup-encoding-format
(encoding)
132 (defmacro gnus-soup-encoding-index
(encoding)
134 (defmacro gnus-soup-encoding-kind
(encoding)
137 (defmacro gnus-soup-reply-prefix
(reply)
139 (defmacro gnus-soup-reply-kind
(reply)
141 (defmacro gnus-soup-reply-encoding
(reply)
146 (defun gnus-soup-send-replies ()
147 "Unpack and send all replies in the reply packet."
149 (let ((packets (directory-files
150 gnus-soup-packet-directory t gnus-soup-packet-regexp
)))
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."
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
))
168 (buffer-disable-undo tmp-buf
)
171 ;; Put the article in a buffer.
173 (when (gnus-request-article-this-buffer
174 (car articles
) gnus-newsgroup-name
)
175 (setq headers
(nnheader-parse-head t
))
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."
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."
207 (let ((groups (gnus-group-process-prefix n
)))
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."
217 (let ((level (or level gnus-level-subscribed
))
218 (newsrc (cdr gnus-newsrc-alist
)))
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)))
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."
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
)
257 (push idx-buf gnus-soup-buffers
)
258 (buffer-disable-undo idx-buf
))
260 ;; Make sure the last char in the buffer is a newline.
261 (goto-char (point-max))
262 (unless (= (current-column) 0)
265 (goto-char (point-min))
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
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
)
286 (t (error "Unsupported type: %c" gnus-soup-encoding-type
))))
287 ;; Insert the soup header and the article in the MSG buf.
289 (goto-char (point-max))
292 (insert-buffer-substring article-buf
)
293 ;; Insert the index in the IDX buf.
296 (gnus-soup-insert-idx beg headers
))
298 (error "Unknown index type: %c" type
)))
299 ;; Return the 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
)
313 (gnus-range-length (cdr (assq 'tick
(gnus-info-marks
315 (when (gnus-summary-read-group group nil t
)
316 (setq gnus-newsgroup-processable
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))
328 (format "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t\t\n"
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-"
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."
345 (gnus-soup-write-areas)
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
))
354 (when (buffer-modified-p)
356 (kill-buffer (current-buffer)))))
357 (gnus-soup-write-prefixes)))
359 (defun gnus-soup-write-prefixes ()
360 (let ((prefixes gnus-soup-last-prefix
)
363 (gnus-set-work-buffer)
364 (while (setq prefix
(pop prefixes
))
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")
375 (packer (if (< (string-match "%s" packer
)
376 (string-match "%d" packer
))
378 (string-to-number (gnus-soup-unique-prefix dir
)))
380 (string-to-number (gnus-soup-unique-prefix dir
))
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
)))
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."
402 (when (file-exists-p file
)
404 (set-buffer (nnheader-find-file-noselect file
'force
))
405 (buffer-disable-undo)
406 (goto-char (point-min))
408 (push (vector (gnus-soup-field)
411 (and (eq (preceding-char) ?
\t)
413 (and (eq (preceding-char) ?
\t)
414 (string-to-number (gnus-soup-field))))
416 (when (eq (preceding-char) ?
\t)
417 (beginning-of-line 2)))
418 (kill-buffer (current-buffer))))
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]."
427 (set-buffer (nnheader-find-file-noselect file
))
428 (buffer-disable-undo)
429 (goto-char (point-min))
431 (push (vector (gnus-soup-field) (gnus-soup-field)
434 (when (eq (preceding-char) ?
\t)
435 (beginning-of-line 2)))
436 (kill-buffer (current-buffer)))
439 (defun gnus-soup-field ()
441 (buffer-substring (point) (progn (skip-chars-forward "^\t\n") (point)))
444 (defun gnus-soup-read-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."
452 (when gnus-soup-areas
453 (with-temp-file (concat gnus-soup-directory
"AREAS")
454 (let ((areas gnus-soup-areas
)
456 (while (setq area
(pop areas
))
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
467 (if (gnus-soup-area-number area
)
468 (concat "\t" (int-to-string
469 (gnus-soup-area-number area
)))
472 (defun gnus-soup-write-replies (dir areas
)
473 "Write a REPLIES file in DIR containing AREAS."
474 (with-temp-file (concat dir
"REPLIES")
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
))
488 (setq area
(car areas
)
490 (when (equal (gnus-soup-area-name area
) real-group
)
494 (vector (gnus-soup-unique-prefix)
497 gnus-soup-encoding-type
499 (if (gnus-member-of-valid 'mail group
) ?m ?n
))
501 gnus-soup-areas
(cons result gnus-soup-areas
)))
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
)
510 (when (file-exists-p (concat dir gnus-soup-prefix-file
))
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
))
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"))))
538 (let* ((msg-file (concat gnus-soup-replies-directory
539 (gnus-soup-reply-prefix (car replies
))
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*"))
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"))
556 (buffer-disable-undo msg-buf
)
558 (goto-char (point-min))
560 (unless (looking-at "#! *rnews +\\([0-9]+\\)")
561 (error "Bad header"))
564 end
(+ (point) (string-to-number
566 (match-beginning 1) (match-end 1)))))
567 (switch-to-buffer tmp-buf
)
569 (mm-disable-multibyte)
570 (insert-buffer-substring msg-buf beg end
)
572 ((string= (gnus-soup-reply-kind (car replies
)) "news")
573 (gnus-message 5 "Sending news message to %s..."
574 (mail-fetch-field "newsgroups"))
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
))
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"))
594 (let ((mail-header-separator ""))
595 (funcall (or message-send-mail-real-function
596 message-send-mail-function
))))
598 (error "Unknown reply kind")))
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
)))
610 ;; arch-tag: eddfa69d-13e8-4aea-84ef-62a526ef185c
611 ;;; gnus-soup.el ends here