Commit | Line | Data |
---|---|---|
eec82323 | 1 | ;;; nnsoup.el --- SOUP access for Gnus |
16409b0b | 2 | |
8f688cb0 | 3 | ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002 |
16409b0b | 4 | ;; Free Software Foundation, Inc. |
eec82323 | 5 | |
6748645f | 6 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> |
eec82323 LMI |
7 | ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet> |
8 | ;; Keywords: news, mail | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
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 2, or (at your option) | |
15 | ;; any later version. | |
16 | ||
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. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
26 | ||
27 | ;;; Commentary: | |
28 | ||
29 | ;;; Code: | |
30 | ||
31 | (require 'nnheader) | |
32 | (require 'nnmail) | |
33 | (require 'gnus-soup) | |
34 | (require 'gnus-msg) | |
35 | (require 'nnoo) | |
36 | (eval-when-compile (require 'cl)) | |
37 | ||
38 | (nnoo-declare nnsoup) | |
39 | ||
40 | (defvoo nnsoup-directory "~/SOUP/" | |
41 | "*SOUP packet directory.") | |
42 | ||
16409b0b GM |
43 | (defvoo nnsoup-tmp-directory |
44 | (cond ((fboundp 'temp-directory) (temp-directory)) | |
45 | ((boundp 'temporary-file-directory) temporary-file-directory) | |
46 | ("/tmp/")) | |
eec82323 LMI |
47 | "*Where nnsoup will store temporary files.") |
48 | ||
16409b0b | 49 | (defvoo nnsoup-replies-directory (expand-file-name "replies/" nnsoup-directory) |
eec82323 LMI |
50 | "*Directory where outgoing packets will be composed.") |
51 | ||
16409b0b | 52 | (defvoo nnsoup-replies-format-type ?u ;; u is USENET news format. |
eec82323 LMI |
53 | "*Format of the replies packages.") |
54 | ||
55 | (defvoo nnsoup-replies-index-type ?n | |
56 | "*Index type of the replies packages.") | |
57 | ||
16409b0b | 58 | (defvoo nnsoup-active-file (expand-file-name "active" nnsoup-directory) |
eec82323 LMI |
59 | "Active file.") |
60 | ||
61 | (defvoo nnsoup-packer "tar cf - %s | gzip > $HOME/Soupin%d.tgz" | |
62 | "Format string command for packing a SOUP packet. | |
63 | The SOUP files will be inserted where the %s is in the string. | |
64 | This string MUST contain both %s and %d. The file number will be | |
65 | inserted where %d appears.") | |
66 | ||
67 | (defvoo nnsoup-unpacker "gunzip -c %s | tar xvf -" | |
68 | "*Format string command for unpacking a SOUP packet. | |
69 | The SOUP packet file name will be inserted at the %s.") | |
70 | ||
71 | (defvoo nnsoup-packet-directory "~/" | |
72 | "*Where nnsoup will look for incoming packets.") | |
73 | ||
74 | (defvoo nnsoup-packet-regexp "Soupout" | |
75 | "*Regular expression matching SOUP packets in `nnsoup-packet-directory'.") | |
76 | ||
6748645f | 77 | (defvoo nnsoup-always-save t |
16409b0b GM |
78 | "If non nil commit the reply buffer on each message send. |
79 | This is necessary if using message mode outside Gnus with nnsoup as a | |
6748645f LMI |
80 | backend for the messages.") |
81 | ||
eec82323 LMI |
82 | \f |
83 | ||
84 | (defconst nnsoup-version "nnsoup 0.0" | |
85 | "nnsoup version.") | |
86 | ||
87 | (defvoo nnsoup-status-string "") | |
88 | (defvoo nnsoup-group-alist nil) | |
89 | (defvoo nnsoup-current-prefix 0) | |
90 | (defvoo nnsoup-replies-list nil) | |
91 | (defvoo nnsoup-buffers nil) | |
92 | (defvoo nnsoup-current-group nil) | |
93 | (defvoo nnsoup-group-alist-touched nil) | |
94 | (defvoo nnsoup-article-alist nil) | |
eec82323 LMI |
95 | \f |
96 | ||
97 | ;;; Interface functions. | |
98 | ||
99 | (nnoo-define-basics nnsoup) | |
100 | ||
101 | (deffoo nnsoup-retrieve-headers (sequence &optional group server fetch-old) | |
102 | (nnsoup-possibly-change-group group) | |
103 | (save-excursion | |
104 | (set-buffer nntp-server-buffer) | |
105 | (erase-buffer) | |
106 | (let ((areas (cddr (assoc nnsoup-current-group nnsoup-group-alist))) | |
107 | (articles sequence) | |
108 | (use-nov t) | |
109 | useful-areas this-area-seq msg-buf) | |
110 | (if (stringp (car sequence)) | |
111 | ;; We don't support fetching by Message-ID. | |
112 | 'headers | |
113 | ;; We go through all the areas and find which files the | |
114 | ;; articles in SEQUENCE come from. | |
115 | (while (and areas sequence) | |
116 | ;; Peel off areas that are below sequence. | |
d942a83d | 117 | (while (and areas (< (cdar (car areas)) (car sequence))) |
eec82323 LMI |
118 | (setq areas (cdr areas))) |
119 | (when areas | |
120 | ;; This is a useful area. | |
121 | (push (car areas) useful-areas) | |
122 | (setq this-area-seq nil) | |
123 | ;; We take note whether this MSG has a corresponding IDX | |
124 | ;; for later use. | |
125 | (when (or (= (gnus-soup-encoding-index | |
126 | (gnus-soup-area-encoding (nth 1 (car areas)))) ?n) | |
127 | (not (file-exists-p | |
128 | (nnsoup-file | |
129 | (gnus-soup-area-prefix (nth 1 (car areas))))))) | |
130 | (setq use-nov nil)) | |
131 | ;; We assign the portion of `sequence' that is relevant to | |
132 | ;; this MSG packet to this packet. | |
d942a83d | 133 | (while (and sequence (<= (car sequence) (cdar (car areas)))) |
eec82323 LMI |
134 | (push (car sequence) this-area-seq) |
135 | (setq sequence (cdr sequence))) | |
136 | (setcar useful-areas (cons (nreverse this-area-seq) | |
137 | (car useful-areas))))) | |
138 | ||
139 | ;; We now have a list of article numbers and corresponding | |
140 | ;; areas. | |
141 | (setq useful-areas (nreverse useful-areas)) | |
142 | ||
143 | ;; Two different approaches depending on whether all the MSG | |
144 | ;; files have corresponding IDX files. If they all do, we | |
145 | ;; simply return the relevant IDX files and let Gnus sort out | |
146 | ;; what lines are relevant. If some of the IDX files are | |
147 | ;; missing, we must return HEADs for all the articles. | |
148 | (if use-nov | |
149 | ;; We have IDX files for all areas. | |
150 | (progn | |
151 | (while useful-areas | |
152 | (goto-char (point-max)) | |
153 | (let ((b (point)) | |
154 | (number (car (nth 1 (car useful-areas)))) | |
155 | (index-buffer (nnsoup-index-buffer | |
156 | (gnus-soup-area-prefix | |
157 | (nth 2 (car useful-areas)))))) | |
158 | (when index-buffer | |
159 | (insert-buffer-substring index-buffer) | |
160 | (goto-char b) | |
8f688cb0 | 161 | ;; We have to remove the index number entries and |
eec82323 LMI |
162 | ;; insert article numbers instead. |
163 | (while (looking-at "[0-9]+") | |
164 | (replace-match (int-to-string number) t t) | |
165 | (incf number) | |
166 | (forward-line 1)))) | |
167 | (setq useful-areas (cdr useful-areas))) | |
168 | 'nov) | |
169 | ;; We insert HEADs. | |
170 | (while useful-areas | |
171 | (setq articles (caar useful-areas) | |
172 | useful-areas (cdr useful-areas)) | |
173 | (while articles | |
174 | (when (setq msg-buf | |
175 | (nnsoup-narrow-to-article | |
176 | (car articles) (cdar useful-areas) 'head)) | |
177 | (goto-char (point-max)) | |
178 | (insert (format "221 %d Article retrieved.\n" (car articles))) | |
179 | (insert-buffer-substring msg-buf) | |
180 | (goto-char (point-max)) | |
181 | (insert ".\n")) | |
182 | (setq articles (cdr articles)))) | |
183 | ||
184 | (nnheader-fold-continuation-lines) | |
185 | 'headers))))) | |
186 | ||
187 | (deffoo nnsoup-open-server (server &optional defs) | |
188 | (nnoo-change-server 'nnsoup server defs) | |
189 | (when (not (file-exists-p nnsoup-directory)) | |
190 | (condition-case () | |
191 | (make-directory nnsoup-directory t) | |
192 | (error t))) | |
193 | (cond | |
194 | ((not (file-exists-p nnsoup-directory)) | |
195 | (nnsoup-close-server) | |
196 | (nnheader-report 'nnsoup "Couldn't create directory: %s" nnsoup-directory)) | |
197 | ((not (file-directory-p (file-truename nnsoup-directory))) | |
198 | (nnsoup-close-server) | |
199 | (nnheader-report 'nnsoup "Not a directory: %s" nnsoup-directory)) | |
200 | (t | |
201 | (nnsoup-read-active-file) | |
202 | (nnheader-report 'nnsoup "Opened server %s using directory %s" | |
203 | server nnsoup-directory) | |
204 | t))) | |
205 | ||
206 | (deffoo nnsoup-request-close () | |
207 | (nnsoup-write-active-file) | |
208 | (nnsoup-write-replies) | |
209 | (gnus-soup-save-areas) | |
210 | ;; Kill all nnsoup buffers. | |
211 | (let (buffer) | |
212 | (while nnsoup-buffers | |
213 | (setq buffer (cdr (pop nnsoup-buffers))) | |
214 | (and buffer | |
215 | (buffer-name buffer) | |
216 | (kill-buffer buffer)))) | |
217 | (setq nnsoup-group-alist nil | |
218 | nnsoup-group-alist-touched nil | |
219 | nnsoup-current-group nil | |
220 | nnsoup-replies-list nil) | |
221 | (nnoo-close-server 'nnoo) | |
222 | t) | |
223 | ||
224 | (deffoo nnsoup-request-article (id &optional newsgroup server buffer) | |
225 | (nnsoup-possibly-change-group newsgroup) | |
226 | (let (buf) | |
227 | (save-excursion | |
228 | (set-buffer (or buffer nntp-server-buffer)) | |
229 | (erase-buffer) | |
230 | (when (and (not (stringp id)) | |
231 | (setq buf (nnsoup-narrow-to-article id))) | |
232 | (insert-buffer-substring buf) | |
233 | t)))) | |
234 | ||
235 | (deffoo nnsoup-request-group (group &optional server dont-check) | |
236 | (nnsoup-possibly-change-group group) | |
237 | (if dont-check | |
238 | t | |
239 | (let ((active (cadr (assoc group nnsoup-group-alist)))) | |
240 | (if (not active) | |
241 | (nnheader-report 'nnsoup "No such group: %s" group) | |
242 | (nnheader-insert | |
243 | "211 %d %d %d %s\n" | |
244 | (max (1+ (- (cdr active) (car active))) 0) | |
245 | (car active) (cdr active) group))))) | |
246 | ||
247 | (deffoo nnsoup-request-type (group &optional article) | |
248 | (nnsoup-possibly-change-group group) | |
a8151ef7 | 249 | ;; Try to guess the type based on the first article in the group. |
eec82323 LMI |
250 | (when (not article) |
251 | (setq article | |
d942a83d | 252 | (cdar (car (cddr (assoc group nnsoup-group-alist)))))) |
eec82323 LMI |
253 | (if (not article) |
254 | 'unknown | |
255 | (let ((kind (gnus-soup-encoding-kind | |
256 | (gnus-soup-area-encoding | |
257 | (nth 1 (nnsoup-article-to-area | |
258 | article nnsoup-current-group)))))) | |
259 | (cond ((= kind ?m) 'mail) | |
a1506d29 | 260 | ((= kind ?n) 'news) |
eec82323 LMI |
261 | (t 'unknown))))) |
262 | ||
263 | (deffoo nnsoup-close-group (group &optional server) | |
264 | ;; Kill all nnsoup buffers. | |
265 | (let ((buffers nnsoup-buffers) | |
266 | elem) | |
267 | (while buffers | |
268 | (when (equal (car (setq elem (pop buffers))) group) | |
269 | (setq nnsoup-buffers (delq elem nnsoup-buffers)) | |
270 | (and (cdr elem) (buffer-name (cdr elem)) | |
271 | (kill-buffer (cdr elem)))))) | |
272 | t) | |
273 | ||
274 | (deffoo nnsoup-request-list (&optional server) | |
275 | (save-excursion | |
276 | (set-buffer nntp-server-buffer) | |
277 | (erase-buffer) | |
278 | (unless nnsoup-group-alist | |
279 | (nnsoup-read-active-file)) | |
280 | (let ((alist nnsoup-group-alist) | |
281 | (standard-output (current-buffer)) | |
282 | entry) | |
283 | (while (setq entry (pop alist)) | |
284 | (insert (car entry) " ") | |
285 | (princ (cdadr entry)) | |
286 | (insert " ") | |
287 | (princ (caadr entry)) | |
288 | (insert " y\n")) | |
289 | t))) | |
290 | ||
291 | (deffoo nnsoup-request-scan (group &optional server) | |
292 | (nnsoup-unpack-packets)) | |
293 | ||
294 | (deffoo nnsoup-request-newgroups (date &optional server) | |
295 | (nnsoup-request-list)) | |
296 | ||
297 | (deffoo nnsoup-request-list-newsgroups (&optional server) | |
298 | nil) | |
299 | ||
300 | (deffoo nnsoup-request-post (&optional server) | |
301 | (nnsoup-store-reply "news") | |
302 | t) | |
303 | ||
304 | (deffoo nnsoup-request-mail (&optional server) | |
305 | (nnsoup-store-reply "mail") | |
306 | t) | |
307 | ||
308 | (deffoo nnsoup-request-expire-articles (articles group &optional server force) | |
309 | (nnsoup-possibly-change-group group) | |
310 | (let* ((total-infolist (assoc group nnsoup-group-alist)) | |
311 | (active (cadr total-infolist)) | |
312 | (infolist (cddr total-infolist)) | |
313 | info range-list mod-time prefix) | |
314 | (while infolist | |
315 | (setq info (pop infolist) | |
316 | range-list (gnus-uncompress-range (car info)) | |
317 | prefix (gnus-soup-area-prefix (nth 1 info))) | |
16409b0b | 318 | (when;; All the articles in this file are marked for expiry. |
eec82323 LMI |
319 | (and (or (setq mod-time (nth 5 (file-attributes |
320 | (nnsoup-file prefix)))) | |
321 | (setq mod-time (nth 5 (file-attributes | |
322 | (nnsoup-file prefix t))))) | |
323 | (gnus-sublist-p articles range-list) | |
324 | ;; This file is old enough. | |
325 | (nnmail-expired-article-p group mod-time force)) | |
326 | ;; Ok, we delete this file. | |
327 | (when (ignore-errors | |
328 | (nnheader-message | |
329 | 5 "Deleting %s in group %s..." (nnsoup-file prefix) | |
330 | group) | |
331 | (when (file-exists-p (nnsoup-file prefix)) | |
332 | (delete-file (nnsoup-file prefix))) | |
333 | (nnheader-message | |
334 | 5 "Deleting %s in group %s..." (nnsoup-file prefix t) | |
335 | group) | |
336 | (when (file-exists-p (nnsoup-file prefix t)) | |
337 | (delete-file (nnsoup-file prefix t))) | |
338 | t) | |
339 | (setcdr (cdr total-infolist) (delq info (cddr total-infolist))) | |
340 | (setq articles (gnus-sorted-complement articles range-list)))) | |
341 | (when (not mod-time) | |
342 | (setcdr (cdr total-infolist) (delq info (cddr total-infolist))))) | |
343 | (if (cddr total-infolist) | |
344 | (setcar active (caaadr (cdr total-infolist))) | |
345 | (setcar active (1+ (cdr active)))) | |
346 | (nnsoup-write-active-file t) | |
347 | ;; Return the articles that weren't expired. | |
348 | articles)) | |
349 | ||
350 | \f | |
351 | ;;; Internal functions | |
352 | ||
353 | (defun nnsoup-possibly-change-group (group &optional force) | |
354 | (when (and group | |
355 | (not (equal nnsoup-current-group group))) | |
356 | (setq nnsoup-article-alist nil) | |
357 | (setq nnsoup-current-group group)) | |
358 | t) | |
359 | ||
360 | (defun nnsoup-read-active-file () | |
361 | (setq nnsoup-group-alist nil) | |
362 | (when (file-exists-p nnsoup-active-file) | |
363 | (ignore-errors | |
364 | (load nnsoup-active-file t t t)) | |
365 | ;; Be backwards compatible. | |
366 | (when (and nnsoup-group-alist | |
367 | (not (atom (caadar nnsoup-group-alist)))) | |
368 | (let ((alist nnsoup-group-alist) | |
369 | entry e min max) | |
370 | (while (setq e (cdr (setq entry (pop alist)))) | |
371 | (setq min (caaar e)) | |
372 | (while (cdr e) | |
373 | (setq e (cdr e))) | |
d942a83d | 374 | (setq max (cdar (car e))) |
eec82323 LMI |
375 | (setcdr entry (cons (cons min max) (cdr entry))))) |
376 | (setq nnsoup-group-alist-touched t)) | |
377 | nnsoup-group-alist)) | |
378 | ||
379 | (defun nnsoup-write-active-file (&optional force) | |
380 | (when (and nnsoup-group-alist | |
381 | (or force | |
382 | nnsoup-group-alist-touched)) | |
383 | (setq nnsoup-group-alist-touched nil) | |
16409b0b | 384 | (with-temp-file nnsoup-active-file |
eec82323 LMI |
385 | (gnus-prin1 `(setq nnsoup-group-alist ',nnsoup-group-alist)) |
386 | (insert "\n") | |
387 | (gnus-prin1 `(setq nnsoup-current-prefix ,nnsoup-current-prefix)) | |
388 | (insert "\n")))) | |
389 | ||
390 | (defun nnsoup-next-prefix () | |
391 | "Return the next free prefix." | |
392 | (let (prefix) | |
393 | (while (or (file-exists-p | |
394 | (nnsoup-file (setq prefix (int-to-string | |
395 | nnsoup-current-prefix)))) | |
396 | (file-exists-p (nnsoup-file prefix t))) | |
397 | (incf nnsoup-current-prefix)) | |
398 | (incf nnsoup-current-prefix) | |
399 | prefix)) | |
400 | ||
401 | (defun nnsoup-file-name (dir file) | |
35ef97a5 | 402 | "Return the full name of FILE (in any case) in DIR." |
eec82323 LMI |
403 | (let* ((case-fold-search t) |
404 | (files (directory-files dir t)) | |
405 | (regexp (concat (regexp-quote file) "$"))) | |
406 | (car (delq nil | |
407 | (mapcar | |
408 | (lambda (file) | |
409 | (if (string-match regexp file) | |
410 | file | |
411 | nil)) | |
412 | files))))) | |
413 | ||
414 | (defun nnsoup-read-areas () | |
415 | (let ((areas-file (nnsoup-file-name nnsoup-tmp-directory "areas"))) | |
416 | (when areas-file | |
417 | (save-excursion | |
418 | (set-buffer nntp-server-buffer) | |
419 | (let ((areas (gnus-soup-parse-areas areas-file)) | |
420 | entry number area lnum cur-prefix file) | |
421 | ;; Go through all areas in the new AREAS file. | |
422 | (while (setq area (pop areas)) | |
423 | ;; Change the name to the permanent name and move the files. | |
424 | (setq cur-prefix (nnsoup-next-prefix)) | |
6748645f | 425 | (nnheader-message 5 "Incorporating file %s..." cur-prefix) |
eec82323 | 426 | (when (file-exists-p |
16409b0b GM |
427 | (setq file |
428 | (expand-file-name | |
429 | (concat (gnus-soup-area-prefix area) ".IDX") | |
430 | nnsoup-tmp-directory))) | |
eec82323 LMI |
431 | (rename-file file (nnsoup-file cur-prefix))) |
432 | (when (file-exists-p | |
16409b0b GM |
433 | (setq file (expand-file-name |
434 | (concat (gnus-soup-area-prefix area) ".MSG") | |
435 | nnsoup-tmp-directory))) | |
eec82323 LMI |
436 | (rename-file file (nnsoup-file cur-prefix t)) |
437 | (gnus-soup-set-area-prefix area cur-prefix) | |
438 | ;; Find the number of new articles in this area. | |
439 | (setq number (nnsoup-number-of-articles area)) | |
440 | (if (not (setq entry (assoc (gnus-soup-area-name area) | |
441 | nnsoup-group-alist))) | |
442 | ;; If this is a new area (group), we just add this info to | |
443 | ;; the group alist. | |
444 | (push (list (gnus-soup-area-name area) | |
445 | (cons 1 number) | |
446 | (list (cons 1 number) area)) | |
447 | nnsoup-group-alist) | |
448 | ;; There are already articles in this group, so we add this | |
449 | ;; info to the end of the entry. | |
450 | (nconc entry (list (list (cons (1+ (setq lnum (cdadr entry))) | |
451 | (+ lnum number)) | |
452 | area))) | |
453 | (setcdr (cadr entry) (+ lnum number)))))) | |
454 | (nnsoup-write-active-file t) | |
455 | (delete-file areas-file))))) | |
456 | ||
457 | (defun nnsoup-number-of-articles (area) | |
458 | (save-excursion | |
459 | (cond | |
460 | ;; If the number is in the area info, we just return it. | |
461 | ((gnus-soup-area-number area) | |
462 | (gnus-soup-area-number area)) | |
463 | ;; If there is an index file, we just count the lines. | |
464 | ((/= (gnus-soup-encoding-index (gnus-soup-area-encoding area)) ?n) | |
465 | (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area))) | |
466 | (count-lines (point-min) (point-max))) | |
467 | ;; We do it the hard way - re-searching through the message | |
468 | ;; buffer. | |
469 | (t | |
470 | (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area))) | |
471 | (unless (assoc (gnus-soup-area-prefix area) nnsoup-article-alist) | |
472 | (nnsoup-dissect-buffer area)) | |
473 | (length (cdr (assoc (gnus-soup-area-prefix area) | |
474 | nnsoup-article-alist))))))) | |
475 | ||
476 | (defun nnsoup-dissect-buffer (area) | |
477 | (let ((mbox-delim (concat "^" message-unix-mail-delimiter)) | |
478 | (format (gnus-soup-encoding-format (gnus-soup-area-encoding area))) | |
479 | (i 0) | |
480 | alist len) | |
481 | (goto-char (point-min)) | |
482 | (cond | |
483 | ;; rnews batch format | |
16409b0b GM |
484 | ((or (= format ?u) |
485 | (= format ?n)) ;; Gnus back compatibility. | |
eec82323 LMI |
486 | (while (looking-at "^#! *rnews \\(+[0-9]+\\) *$") |
487 | (forward-line 1) | |
488 | (push (list | |
489 | (incf i) (point) | |
490 | (progn | |
491 | (forward-char (string-to-number (match-string 1))) | |
492 | (point))) | |
493 | alist))) | |
494 | ;; Unix mbox format | |
495 | ((= format ?m) | |
496 | (while (looking-at mbox-delim) | |
497 | (forward-line 1) | |
498 | (push (list | |
499 | (incf i) (point) | |
500 | (progn | |
501 | (if (re-search-forward mbox-delim nil t) | |
502 | (beginning-of-line) | |
503 | (goto-char (point-max))) | |
504 | (point))) | |
505 | alist))) | |
506 | ;; MMDF format | |
507 | ((= format ?M) | |
508 | (while (looking-at "\^A\^A\^A\^A\n") | |
509 | (forward-line 1) | |
510 | (push (list | |
511 | (incf i) (point) | |
512 | (progn | |
513 | (if (search-forward "\n\^A\^A\^A\^A\n" nil t) | |
514 | (beginning-of-line) | |
515 | (goto-char (point-max))) | |
516 | (point))) | |
517 | alist))) | |
518 | ;; Binary format | |
519 | ((or (= format ?B) (= format ?b)) | |
520 | (while (not (eobp)) | |
521 | (setq len (+ (* (char-after (point)) (expt 2.0 24)) | |
522 | (* (char-after (+ (point) 1)) (expt 2 16)) | |
523 | (* (char-after (+ (point) 2)) (expt 2 8)) | |
524 | (char-after (+ (point) 3)))) | |
525 | (push (list | |
526 | (incf i) (+ (point) 4) | |
527 | (progn | |
528 | (forward-char (floor (+ len 4))) | |
529 | (point))) | |
530 | alist))) | |
531 | (t | |
532 | (error "Unknown format: %c" format))) | |
533 | (push (cons (gnus-soup-area-prefix area) alist) nnsoup-article-alist))) | |
534 | ||
535 | (defun nnsoup-index-buffer (prefix &optional message) | |
536 | (let* ((file (concat prefix (if message ".MSG" ".IDX"))) | |
537 | (buffer-name (concat " *nnsoup " file "*"))) | |
538 | (or (get-buffer buffer-name) ; File already loaded. | |
16409b0b | 539 | (when (file-exists-p (expand-file-name file nnsoup-directory)) |
eec82323 LMI |
540 | (save-excursion ; Load the file. |
541 | (set-buffer (get-buffer-create buffer-name)) | |
16409b0b | 542 | (buffer-disable-undo) |
eec82323 | 543 | (push (cons nnsoup-current-group (current-buffer)) nnsoup-buffers) |
16409b0b GM |
544 | (nnheader-insert-file-contents |
545 | (expand-file-name file nnsoup-directory)) | |
eec82323 LMI |
546 | (current-buffer)))))) |
547 | ||
548 | (defun nnsoup-file (prefix &optional message) | |
549 | (expand-file-name | |
16409b0b GM |
550 | (concat prefix (if message ".MSG" ".IDX")) |
551 | nnsoup-directory)) | |
eec82323 LMI |
552 | |
553 | (defun nnsoup-message-buffer (prefix) | |
554 | (nnsoup-index-buffer prefix 'msg)) | |
555 | ||
556 | (defun nnsoup-unpack-packets () | |
557 | "Unpack all packets in `nnsoup-packet-directory'." | |
558 | (let ((packets (directory-files | |
559 | nnsoup-packet-directory t nnsoup-packet-regexp)) | |
560 | packet) | |
561 | (while (setq packet (pop packets)) | |
6748645f | 562 | (nnheader-message 5 "nnsoup: unpacking %s..." packet) |
eec82323 LMI |
563 | (if (not (gnus-soup-unpack-packet |
564 | nnsoup-tmp-directory nnsoup-unpacker packet)) | |
6748645f | 565 | (nnheader-message 5 "Couldn't unpack %s" packet) |
eec82323 LMI |
566 | (delete-file packet) |
567 | (nnsoup-read-areas) | |
6748645f | 568 | (nnheader-message 5 "Unpacking...done"))))) |
eec82323 LMI |
569 | |
570 | (defun nnsoup-narrow-to-article (article &optional area head) | |
571 | (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group))) | |
572 | (prefix (and area (gnus-soup-area-prefix (nth 1 area)))) | |
573 | (msg-buf (and prefix (nnsoup-index-buffer prefix 'msg))) | |
574 | beg end) | |
575 | (when area | |
576 | (save-excursion | |
577 | (cond | |
578 | ;; There is no MSG file. | |
579 | ((null msg-buf) | |
580 | nil) | |
581 | ;; We use the index file to find out where the article | |
582 | ;; begins and ends. | |
583 | ((and (= (gnus-soup-encoding-index | |
584 | (gnus-soup-area-encoding (nth 1 area))) | |
585 | ?c) | |
586 | (file-exists-p (nnsoup-file prefix))) | |
587 | (set-buffer (nnsoup-index-buffer prefix)) | |
588 | (widen) | |
589 | (goto-char (point-min)) | |
590 | (forward-line (- article (caar area))) | |
591 | (setq beg (read (current-buffer))) | |
592 | (forward-line 1) | |
593 | (if (looking-at "[0-9]+") | |
594 | (progn | |
595 | (setq end (read (current-buffer))) | |
596 | (set-buffer msg-buf) | |
597 | (widen) | |
598 | (let ((format (gnus-soup-encoding-format | |
599 | (gnus-soup-area-encoding (nth 1 area))))) | |
600 | (goto-char end) | |
16409b0b | 601 | (when (or (= format ?u) (= format ?n) (= format ?m)) |
eec82323 LMI |
602 | (setq end (progn (forward-line -1) (point)))))) |
603 | (set-buffer msg-buf)) | |
604 | (widen) | |
605 | (narrow-to-region beg (or end (point-max)))) | |
606 | (t | |
607 | (set-buffer msg-buf) | |
608 | (widen) | |
609 | (unless (assoc (gnus-soup-area-prefix (nth 1 area)) | |
610 | nnsoup-article-alist) | |
611 | (nnsoup-dissect-buffer (nth 1 area))) | |
612 | (let ((entry (assq article (cdr (assoc (gnus-soup-area-prefix | |
613 | (nth 1 area)) | |
614 | nnsoup-article-alist))))) | |
615 | (when entry | |
616 | (narrow-to-region (cadr entry) (caddr entry)))))) | |
617 | (goto-char (point-min)) | |
618 | (if (not head) | |
619 | () | |
620 | (narrow-to-region | |
621 | (point-min) | |
622 | (if (search-forward "\n\n" nil t) | |
623 | (1- (point)) | |
624 | (point-max)))) | |
625 | msg-buf)))) | |
626 | ||
627 | ;;;###autoload | |
628 | (defun nnsoup-pack-replies () | |
629 | "Make an outbound package of SOUP replies." | |
630 | (interactive) | |
631 | (unless (file-exists-p nnsoup-replies-directory) | |
6748645f | 632 | (nnheader-message 5 "No such directory: %s" nnsoup-replies-directory)) |
eec82323 LMI |
633 | ;; Write all data buffers. |
634 | (gnus-soup-save-areas) | |
635 | ;; Write the active file. | |
636 | (nnsoup-write-active-file) | |
637 | ;; Write the REPLIES file. | |
638 | (nnsoup-write-replies) | |
639 | ;; Check whether there is anything here. | |
640 | (when (null (directory-files nnsoup-replies-directory nil "\\.MSG$")) | |
a8151ef7 | 641 | (error "No files to pack")) |
eec82323 LMI |
642 | ;; Pack all these files into a SOUP packet. |
643 | (gnus-soup-pack nnsoup-replies-directory nnsoup-packer)) | |
644 | ||
645 | (defun nnsoup-write-replies () | |
646 | "Write the REPLIES file." | |
647 | (when nnsoup-replies-list | |
648 | (gnus-soup-write-replies nnsoup-replies-directory nnsoup-replies-list) | |
649 | (setq nnsoup-replies-list nil))) | |
650 | ||
651 | (defun nnsoup-article-to-area (article group) | |
652 | "Return the area that ARTICLE in GROUP is located in." | |
653 | (let ((areas (cddr (assoc group nnsoup-group-alist)))) | |
d942a83d | 654 | (while (and areas (< (cdar (car areas)) article)) |
eec82323 LMI |
655 | (setq areas (cdr areas))) |
656 | (and areas (car areas)))) | |
657 | ||
658 | (defvar nnsoup-old-functions | |
659 | (list message-send-mail-function message-send-news-function)) | |
660 | ||
661 | ;;;###autoload | |
662 | (defun nnsoup-set-variables () | |
663 | "Use the SOUP methods for posting news and mailing mail." | |
664 | (interactive) | |
665 | (setq message-send-news-function 'nnsoup-request-post) | |
666 | (setq message-send-mail-function 'nnsoup-request-mail)) | |
667 | ||
668 | ;;;###autoload | |
669 | (defun nnsoup-revert-variables () | |
670 | "Revert posting and mailing methods to the standard Emacs methods." | |
671 | (interactive) | |
672 | (setq message-send-mail-function (car nnsoup-old-functions)) | |
673 | (setq message-send-news-function (cadr nnsoup-old-functions))) | |
674 | ||
675 | (defun nnsoup-store-reply (kind) | |
676 | ;; Mostly stolen from `message.el'. | |
677 | (require 'mail-utils) | |
678 | (let ((tembuf (generate-new-buffer " message temp")) | |
679 | (case-fold-search nil) | |
680 | delimline | |
681 | (mailbuf (current-buffer))) | |
682 | (unwind-protect | |
683 | (save-excursion | |
684 | (save-restriction | |
685 | (message-narrow-to-headers) | |
686 | (if (equal kind "mail") | |
687 | (message-generate-headers message-required-mail-headers) | |
688 | (message-generate-headers message-required-news-headers))) | |
689 | (set-buffer tembuf) | |
690 | (erase-buffer) | |
691 | (insert-buffer-substring mailbuf) | |
692 | ;; Remove some headers. | |
693 | (save-restriction | |
694 | (message-narrow-to-headers) | |
695 | ;; Remove some headers. | |
696 | (message-remove-header message-ignored-mail-headers t)) | |
697 | (goto-char (point-max)) | |
698 | ;; require one newline at the end. | |
699 | (or (= (preceding-char) ?\n) | |
700 | (insert ?\n)) | |
701 | (let ((case-fold-search t)) | |
702 | ;; Change header-delimiter to be what sendmail expects. | |
703 | (goto-char (point-min)) | |
704 | (re-search-forward | |
16409b0b | 705 | (concat "^" (regexp-quote mail-header-separator) "\n")) |
eec82323 LMI |
706 | (replace-match "\n") |
707 | (backward-char 1) | |
708 | (setq delimline (point-marker)) | |
eec82323 | 709 | (goto-char (1+ delimline)) |
eec82323 LMI |
710 | (let ((msg-buf |
711 | (gnus-soup-store | |
712 | nnsoup-replies-directory | |
713 | (nnsoup-kind-to-prefix kind) nil nnsoup-replies-format-type | |
714 | nnsoup-replies-index-type)) | |
715 | (num 0)) | |
716 | (when (and msg-buf (bufferp msg-buf)) | |
717 | (save-excursion | |
718 | (set-buffer msg-buf) | |
719 | (goto-char (point-min)) | |
720 | (while (re-search-forward "^#! *rnews" nil t) | |
6748645f LMI |
721 | (incf num)) |
722 | (when nnsoup-always-save | |
723 | (save-buffer))) | |
724 | (nnheader-message 5 "Stored %d messages" num))) | |
eec82323 LMI |
725 | (nnsoup-write-replies) |
726 | (kill-buffer tembuf)))))) | |
727 | ||
728 | (defun nnsoup-kind-to-prefix (kind) | |
729 | (unless nnsoup-replies-list | |
730 | (setq nnsoup-replies-list | |
731 | (gnus-soup-parse-replies | |
16409b0b | 732 | (expand-file-name "REPLIES" nnsoup-replies-directory)))) |
eec82323 LMI |
733 | (let ((replies nnsoup-replies-list)) |
734 | (while (and replies | |
735 | (not (string= kind (gnus-soup-reply-kind (car replies))))) | |
736 | (setq replies (cdr replies))) | |
737 | (if replies | |
738 | (gnus-soup-reply-prefix (car replies)) | |
739 | (push (vector (gnus-soup-unique-prefix nnsoup-replies-directory) | |
740 | kind | |
741 | (format "%c%c%c" | |
742 | nnsoup-replies-format-type | |
743 | nnsoup-replies-index-type | |
744 | (if (string= kind "news") | |
745 | ?n ?m))) | |
746 | nnsoup-replies-list) | |
747 | (gnus-soup-reply-prefix (car nnsoup-replies-list))))) | |
748 | ||
749 | (defun nnsoup-make-active () | |
750 | "(Re-)create the SOUP active file." | |
751 | (interactive) | |
752 | (let ((files (sort (directory-files nnsoup-directory t "IDX$") | |
753 | (lambda (f1 f2) | |
754 | (< (progn (string-match "/\\([0-9]+\\)\\." f1) | |
755 | (string-to-int (match-string 1 f1))) | |
756 | (progn (string-match "/\\([0-9]+\\)\\." f2) | |
757 | (string-to-int (match-string 1 f2))))))) | |
758 | active group lines ident elem min) | |
759 | (set-buffer (get-buffer-create " *nnsoup work*")) | |
eec82323 | 760 | (while files |
6748645f | 761 | (nnheader-message 5 "Doing %s..." (car files)) |
eec82323 LMI |
762 | (erase-buffer) |
763 | (nnheader-insert-file-contents (car files)) | |
764 | (goto-char (point-min)) | |
765 | (if (not (re-search-forward "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t *\\(Xref: \\)? *[^ ]* \\([^ ]+\\):[0-9]" nil t)) | |
766 | (setq group "unknown") | |
767 | (setq group (match-string 2))) | |
768 | (setq lines (count-lines (point-min) (point-max))) | |
769 | (setq ident (progn (string-match | |
770 | "/\\([0-9]+\\)\\." (car files)) | |
771 | (substring | |
772 | (car files) (match-beginning 1) | |
773 | (match-end 1)))) | |
774 | (if (not (setq elem (assoc group active))) | |
775 | (push (list group (cons 1 lines) | |
776 | (list (cons 1 lines) | |
16409b0b | 777 | (vector ident group "ucm" "" lines))) |
eec82323 LMI |
778 | active) |
779 | (nconc elem | |
780 | (list | |
781 | (list (cons (1+ (setq min (cdadr elem))) | |
782 | (+ min lines)) | |
16409b0b | 783 | (vector ident group "ucm" "" lines)))) |
eec82323 LMI |
784 | (setcdr (cadr elem) (+ min lines))) |
785 | (setq files (cdr files))) | |
6748645f | 786 | (nnheader-message 5 "") |
eec82323 LMI |
787 | (setq nnsoup-group-alist active) |
788 | (nnsoup-write-active-file t))) | |
789 | ||
790 | (defun nnsoup-delete-unreferenced-message-files () | |
791 | "Delete any *.MSG and *.IDX files that aren't known by nnsoup." | |
792 | (interactive) | |
793 | (let* ((known (apply 'nconc (mapcar | |
794 | (lambda (ga) | |
795 | (mapcar | |
796 | (lambda (area) | |
797 | (gnus-soup-area-prefix (cadr area))) | |
798 | (cddr ga))) | |
799 | nnsoup-group-alist))) | |
800 | (regexp "\\.MSG$\\|\\.IDX$") | |
801 | (files (directory-files nnsoup-directory nil regexp)) | |
802 | non-files file) | |
803 | ;; Find all files that aren't known by nnsoup. | |
804 | (while (setq file (pop files)) | |
805 | (string-match regexp file) | |
806 | (unless (member (substring file 0 (match-beginning 0)) known) | |
807 | (push file non-files))) | |
808 | ;; Sort and delete the files. | |
809 | (setq non-files (sort non-files 'string<)) | |
810 | (map-y-or-n-p "Delete file %s? " | |
16409b0b GM |
811 | (lambda (file) (delete-file |
812 | (expand-file-name file nnsoup-directory))) | |
eec82323 LMI |
813 | non-files))) |
814 | ||
815 | (provide 'nnsoup) | |
816 | ||
ab5796a9 | 817 | ;;; arch-tag: b0451389-5703-4450-9425-f66f6b38c828 |
eec82323 | 818 | ;;; nnsoup.el ends here |