Commit | Line | Data |
---|---|---|
eec82323 | 1 | ;;; nnmbox.el --- mail mbox access for Gnus |
16409b0b | 2 | |
e84b4b86 | 3 | ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, |
114f9c96 | 4 | ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
eec82323 | 5 | |
6748645f | 6 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> |
23f87bed | 7 | ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet> |
eec82323 LMI |
8 | ;; Keywords: news, mail |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
5e809f55 | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
eec82323 | 13 | ;; it under the terms of the GNU General Public License as published by |
5e809f55 GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) 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. | |
eec82323 | 21 | |
eec82323 | 22 | ;; You should have received a copy of the GNU General Public License |
5e809f55 | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
eec82323 LMI |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; For an overview of what the interface functions do, please see the | |
28 | ;; Gnus sources. | |
29 | ||
30 | ;;; Code: | |
31 | ||
32 | (require 'nnheader) | |
33 | (require 'message) | |
34 | (require 'nnmail) | |
35 | (require 'nnoo) | |
23f87bed | 36 | (require 'gnus-range) |
eec82323 LMI |
37 | (eval-when-compile (require 'cl)) |
38 | ||
39 | (nnoo-declare nnmbox) | |
40 | ||
41 | (defvoo nnmbox-mbox-file (expand-file-name "~/mbox") | |
42 | "The name of the mail box file in the user's home directory.") | |
43 | ||
44 | (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active") | |
45 | "The name of the active file for the mail box.") | |
46 | ||
47 | (defvoo nnmbox-get-new-mail t | |
48 | "If non-nil, nnmbox will check the incoming mail file and split the mail.") | |
49 | ||
50 | (defvoo nnmbox-prepare-save-mail-hook nil | |
51 | "Hook run narrowed to an article before saving.") | |
52 | ||
53 | \f | |
54 | ||
55 | (defconst nnmbox-version "nnmbox 1.0" | |
56 | "nnmbox version.") | |
57 | ||
58 | (defvoo nnmbox-current-group nil | |
59 | "Current nnmbox news group directory.") | |
60 | ||
23f87bed | 61 | (defvar nnmbox-mbox-buffer nil) |
eec82323 LMI |
62 | |
63 | (defvoo nnmbox-status-string "") | |
64 | ||
65 | (defvoo nnmbox-group-alist nil) | |
66 | (defvoo nnmbox-active-timestamp nil) | |
67 | ||
158d6e07 | 68 | (defvoo nnmbox-file-coding-system mm-binary-coding-system) |
16409b0b | 69 | (defvoo nnmbox-file-coding-system-for-write nil) |
158d6e07 | 70 | (defvoo nnmbox-active-file-coding-system mm-binary-coding-system) |
16409b0b GM |
71 | (defvoo nnmbox-active-file-coding-system-for-write nil) |
72 | ||
23f87bed MB |
73 | (defvar nnmbox-group-building-active-articles nil) |
74 | (defvar nnmbox-group-active-articles nil) | |
eec82323 LMI |
75 | \f |
76 | ||
77 | ;;; Interface functions | |
78 | ||
79 | (nnoo-define-basics nnmbox) | |
80 | ||
81 | (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old) | |
82 | (save-excursion | |
83 | (set-buffer nntp-server-buffer) | |
84 | (erase-buffer) | |
85 | (let ((number (length sequence)) | |
86 | (count 0) | |
23f87bed | 87 | article start stop) |
eec82323 LMI |
88 | (nnmbox-possibly-change-newsgroup newsgroup server) |
89 | (while sequence | |
90 | (setq article (car sequence)) | |
eec82323 | 91 | (set-buffer nnmbox-mbox-buffer) |
23f87bed | 92 | (when (nnmbox-find-article article) |
eec82323 LMI |
93 | (setq start |
94 | (save-excursion | |
95 | (re-search-backward | |
96 | (concat "^" message-unix-mail-delimiter) nil t) | |
97 | (point))) | |
98 | (search-forward "\n\n" nil t) | |
99 | (setq stop (1- (point))) | |
100 | (set-buffer nntp-server-buffer) | |
101 | (insert (format "221 %d Article retrieved.\n" article)) | |
102 | (insert-buffer-substring nnmbox-mbox-buffer start stop) | |
103 | (goto-char (point-max)) | |
104 | (insert ".\n")) | |
105 | (setq sequence (cdr sequence)) | |
106 | (setq count (1+ count)) | |
107 | (and (numberp nnmail-large-newsgroup) | |
108 | (> number nnmail-large-newsgroup) | |
109 | (zerop (% count 20)) | |
110 | (nnheader-message 5 "nnmbox: Receiving headers... %d%%" | |
111 | (/ (* count 100) number)))) | |
112 | ||
113 | (and (numberp nnmail-large-newsgroup) | |
114 | (> number nnmail-large-newsgroup) | |
115 | (nnheader-message 5 "nnmbox: Receiving headers...done")) | |
116 | ||
117 | (set-buffer nntp-server-buffer) | |
118 | (nnheader-fold-continuation-lines) | |
119 | 'headers))) | |
120 | ||
121 | (deffoo nnmbox-open-server (server &optional defs) | |
122 | (nnoo-change-server 'nnmbox server defs) | |
123 | (nnmbox-create-mbox) | |
124 | (cond | |
125 | ((not (file-exists-p nnmbox-mbox-file)) | |
126 | (nnmbox-close-server) | |
127 | (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file)) | |
128 | ((file-directory-p nnmbox-mbox-file) | |
129 | (nnmbox-close-server) | |
130 | (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file)) | |
131 | (t | |
132 | (nnheader-report 'nnmbox "Opened server %s using mbox %s" server | |
133 | nnmbox-mbox-file) | |
134 | t))) | |
135 | ||
136 | (deffoo nnmbox-close-server (&optional server) | |
137 | (when (and nnmbox-mbox-buffer | |
138 | (buffer-name nnmbox-mbox-buffer)) | |
139 | (kill-buffer nnmbox-mbox-buffer)) | |
140 | (nnoo-close-server 'nnmbox server) | |
141 | t) | |
142 | ||
143 | (deffoo nnmbox-server-opened (&optional server) | |
144 | (and (nnoo-current-server-p 'nnmbox server) | |
145 | nnmbox-mbox-buffer | |
146 | (buffer-name nnmbox-mbox-buffer) | |
147 | nntp-server-buffer | |
148 | (buffer-name nntp-server-buffer))) | |
149 | ||
150 | (deffoo nnmbox-request-article (article &optional newsgroup server buffer) | |
151 | (nnmbox-possibly-change-newsgroup newsgroup server) | |
152 | (save-excursion | |
153 | (set-buffer nnmbox-mbox-buffer) | |
23f87bed | 154 | (when (nnmbox-find-article article) |
eec82323 LMI |
155 | (let (start stop) |
156 | (re-search-backward (concat "^" message-unix-mail-delimiter) nil t) | |
157 | (setq start (point)) | |
158 | (forward-line 1) | |
4b70e299 MB |
159 | (setq stop (if (re-search-forward (concat "^" |
160 | message-unix-mail-delimiter) | |
161 | nil 'move) | |
162 | (match-beginning 0) | |
163 | (point))) | |
eec82323 LMI |
164 | (let ((nntp-server-buffer (or buffer nntp-server-buffer))) |
165 | (set-buffer nntp-server-buffer) | |
166 | (erase-buffer) | |
167 | (insert-buffer-substring nnmbox-mbox-buffer start stop) | |
168 | (goto-char (point-min)) | |
169 | (while (looking-at "From ") | |
170 | (delete-char 5) | |
171 | (insert "X-From-Line: ") | |
172 | (forward-line 1)) | |
173 | (if (numberp article) | |
174 | (cons nnmbox-current-group article) | |
23f87bed | 175 | (nnmbox-article-group-number nil))))))) |
eec82323 LMI |
176 | |
177 | (deffoo nnmbox-request-group (group &optional server dont-check) | |
16409b0b | 178 | (nnmbox-possibly-change-newsgroup nil server) |
eec82323 LMI |
179 | (let ((active (cadr (assoc group nnmbox-group-alist)))) |
180 | (cond | |
181 | ((or (null active) | |
182 | (null (nnmbox-possibly-change-newsgroup group server))) | |
183 | (nnheader-report 'nnmbox "No such group: %s" group)) | |
184 | (dont-check | |
185 | (nnheader-report 'nnmbox "Selected group %s" group) | |
186 | (nnheader-insert "")) | |
187 | (t | |
188 | (nnheader-report 'nnmbox "Selected group %s" group) | |
189 | (nnheader-insert "211 %d %d %d %s\n" | |
190 | (1+ (- (cdr active) (car active))) | |
191 | (car active) (cdr active) group))))) | |
192 | ||
16409b0b | 193 | (defun nnmbox-save-buffer () |
a1506d29 | 194 | (let ((coding-system-for-write |
16409b0b GM |
195 | (or nnmbox-file-coding-system-for-write |
196 | nnmbox-file-coding-system))) | |
197 | (save-buffer))) | |
198 | ||
199 | (defun nnmbox-save-active (group-alist active-file) | |
200 | (let ((nnmail-active-file-coding-system | |
201 | (or nnmbox-active-file-coding-system-for-write | |
202 | nnmbox-active-file-coding-system))) | |
203 | (nnmail-save-active group-alist active-file))) | |
204 | ||
eec82323 LMI |
205 | (deffoo nnmbox-request-scan (&optional group server) |
206 | (nnmbox-possibly-change-newsgroup group server) | |
207 | (nnmbox-read-mbox) | |
208 | (nnmail-get-new-mail | |
209 | 'nnmbox | |
210 | (lambda () | |
211 | (save-excursion | |
212 | (set-buffer nnmbox-mbox-buffer) | |
16409b0b | 213 | (nnmbox-save-buffer))) |
eec82323 LMI |
214 | (file-name-directory nnmbox-mbox-file) |
215 | group | |
216 | (lambda () | |
217 | (save-excursion | |
218 | (let ((in-buf (current-buffer))) | |
219 | (set-buffer nnmbox-mbox-buffer) | |
220 | (goto-char (point-max)) | |
221 | (insert-buffer-substring in-buf))) | |
16409b0b | 222 | (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)))) |
eec82323 LMI |
223 | |
224 | (deffoo nnmbox-close-group (group &optional server) | |
225 | t) | |
226 | ||
6748645f LMI |
227 | (deffoo nnmbox-request-create-group (group &optional server args) |
228 | (nnmail-activate 'nnmbox) | |
229 | (unless (assoc group nnmbox-group-alist) | |
230 | (push (list group (cons 1 0)) | |
231 | nnmbox-group-alist) | |
16409b0b | 232 | (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)) |
6748645f LMI |
233 | t) |
234 | ||
eec82323 LMI |
235 | (deffoo nnmbox-request-list (&optional server) |
236 | (save-excursion | |
16409b0b GM |
237 | (let ((nnmail-file-coding-system |
238 | nnmbox-active-file-coding-system)) | |
239 | (nnmail-find-file nnmbox-active-file)) | |
eec82323 LMI |
240 | (setq nnmbox-group-alist (nnmail-get-active)) |
241 | t)) | |
242 | ||
243 | (deffoo nnmbox-request-newgroups (date &optional server) | |
244 | (nnmbox-request-list server)) | |
245 | ||
246 | (deffoo nnmbox-request-list-newsgroups (&optional server) | |
247 | (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented.")) | |
248 | ||
249 | (deffoo nnmbox-request-expire-articles | |
16409b0b | 250 | (articles newsgroup &optional server force) |
eec82323 LMI |
251 | (nnmbox-possibly-change-newsgroup newsgroup server) |
252 | (let* ((is-old t) | |
253 | rest) | |
254 | (nnmail-activate 'nnmbox) | |
255 | ||
256 | (save-excursion | |
257 | (set-buffer nnmbox-mbox-buffer) | |
258 | (while (and articles is-old) | |
23f87bed | 259 | (when (nnmbox-find-article (car articles)) |
eec82323 LMI |
260 | (if (setq is-old |
261 | (nnmail-expired-article-p | |
262 | newsgroup | |
263 | (buffer-substring | |
264 | (point) (progn (end-of-line) (point))) force)) | |
265 | (progn | |
0d972486 SZ |
266 | (unless (eq nnmail-expiry-target 'delete) |
267 | (with-temp-buffer | |
a1506d29 JB |
268 | (nnmbox-request-article (car articles) |
269 | newsgroup server | |
0d972486 SZ |
270 | (current-buffer)) |
271 | (let ((nnml-current-directory nil)) | |
272 | (nnmail-expiry-target-group | |
23f87bed MB |
273 | nnmail-expiry-target newsgroup))) |
274 | (nnmbox-possibly-change-newsgroup newsgroup server)) | |
eec82323 LMI |
275 | (nnheader-message 5 "Deleting article %d in %s..." |
276 | (car articles) newsgroup) | |
277 | (nnmbox-delete-mail)) | |
278 | (push (car articles) rest))) | |
279 | (setq articles (cdr articles))) | |
16409b0b | 280 | (nnmbox-save-buffer) |
eec82323 LMI |
281 | ;; Find the lowest active article in this group. |
282 | (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist)))) | |
23f87bed | 283 | (while (and (not (nnmbox-find-article (car active))) |
eec82323 | 284 | (<= (car active) (cdr active))) |
23f87bed | 285 | (setcar active (1+ (car active))))) |
16409b0b | 286 | (nnmbox-save-active nnmbox-group-alist nnmbox-active-file) |
eec82323 LMI |
287 | (nconc rest articles)))) |
288 | ||
289 | (deffoo nnmbox-request-move-article | |
01c52d31 | 290 | (article group server accept-form &optional last move-is-internal) |
eec82323 LMI |
291 | (let ((buf (get-buffer-create " *nnmbox move*")) |
292 | result) | |
293 | (and | |
294 | (nnmbox-request-article article group server) | |
295 | (save-excursion | |
296 | (set-buffer buf) | |
eec82323 LMI |
297 | (erase-buffer) |
298 | (insert-buffer-substring nntp-server-buffer) | |
299 | (goto-char (point-min)) | |
300 | (while (re-search-forward | |
301 | "^X-Gnus-Newsgroup:" | |
302 | (save-excursion (search-forward "\n\n" nil t) (point)) t) | |
23f87bed | 303 | (gnus-delete-line)) |
eec82323 LMI |
304 | (setq result (eval accept-form)) |
305 | (kill-buffer buf) | |
306 | result) | |
307 | (save-excursion | |
308 | (nnmbox-possibly-change-newsgroup group server) | |
309 | (set-buffer nnmbox-mbox-buffer) | |
23f87bed | 310 | (when (nnmbox-find-article article) |
eec82323 | 311 | (nnmbox-delete-mail)) |
16409b0b | 312 | (and last (nnmbox-save-buffer)))) |
eec82323 LMI |
313 | result)) |
314 | ||
315 | (deffoo nnmbox-request-accept-article (group &optional server last) | |
316 | (nnmbox-possibly-change-newsgroup group server) | |
317 | (nnmail-check-syntax) | |
318 | (let ((buf (current-buffer)) | |
4b70e299 | 319 | result cont) |
eec82323 LMI |
320 | (and |
321 | (nnmail-activate 'nnmbox) | |
4b70e299 MB |
322 | (with-temp-buffer |
323 | (insert-buffer-substring buf) | |
eec82323 | 324 | (goto-char (point-min)) |
4b70e299 MB |
325 | (cond (;; The From line may have been quoted by movemail. |
326 | (looking-at (concat ">" message-unix-mail-delimiter)) | |
327 | (delete-char 1) | |
328 | (forward-line 1)) | |
329 | ((looking-at "X-From-Line: ") | |
330 | (replace-match "From ") | |
331 | (forward-line 1)) | |
332 | (t | |
333 | (insert "From nobody " (current-time-string) "\n"))) | |
334 | (narrow-to-region (point) | |
335 | (if (search-forward "\n\n" nil 'move) | |
336 | (1- (point)) | |
337 | (point))) | |
eec82323 LMI |
338 | (while (re-search-backward "^X-Gnus-Newsgroup: " nil t) |
339 | (delete-region (point) (progn (forward-line 1) (point)))) | |
340 | (when nnmail-cache-accepted-message-ids | |
4b70e299 | 341 | (nnmail-cache-insert (message-fetch-field "message-id") |
23f87bed | 342 | group |
4b70e299 MB |
343 | (message-fetch-field "subject") |
344 | (message-fetch-field "from"))) | |
345 | (widen) | |
eec82323 LMI |
346 | (setq result (if (stringp group) |
347 | (list (cons group (nnmbox-active-number group))) | |
348 | (nnmail-article-group 'nnmbox-active-number))) | |
4b70e299 MB |
349 | (prog1 |
350 | (if (and (null result) | |
351 | (yes-or-no-p "Moved to `junk' group; delete article? ")) | |
352 | (setq result 'junk) | |
353 | (setq result (car (nnmbox-save-mail result)))) | |
354 | (setq cont (buffer-string)))) | |
355 | (with-current-buffer nnmbox-mbox-buffer | |
eec82323 | 356 | (goto-char (point-max)) |
4b70e299 | 357 | (insert cont) |
eec82323 LMI |
358 | (when last |
359 | (when nnmail-cache-accepted-message-ids | |
360 | (nnmail-cache-close)) | |
16409b0b GM |
361 | (nnmbox-save-active nnmbox-group-alist nnmbox-active-file) |
362 | (nnmbox-save-buffer)))) | |
eec82323 LMI |
363 | result)) |
364 | ||
365 | (deffoo nnmbox-request-replace-article (article group buffer) | |
366 | (nnmbox-possibly-change-newsgroup group) | |
367 | (save-excursion | |
368 | (set-buffer nnmbox-mbox-buffer) | |
23f87bed | 369 | (if (not (nnmbox-find-article article)) |
eec82323 LMI |
370 | nil |
371 | (nnmbox-delete-mail t t) | |
4b70e299 MB |
372 | (insert |
373 | (with-temp-buffer | |
374 | (insert-buffer-substring buffer) | |
375 | (goto-char (point-min)) | |
376 | (when (looking-at "X-From-Line:") | |
377 | (delete-region (point) (progn (forward-line 1) (point)))) | |
378 | (while (re-search-forward (concat "^" message-unix-mail-delimiter) | |
379 | nil t) | |
380 | (goto-char (match-beginning 0)) | |
381 | (insert ">")) | |
382 | (goto-char (point-max)) | |
383 | (unless (bolp) | |
384 | (insert "\n")) | |
385 | (buffer-string))) | |
16409b0b | 386 | (nnmbox-save-buffer) |
eec82323 LMI |
387 | t))) |
388 | ||
389 | (deffoo nnmbox-request-delete-group (group &optional force server) | |
390 | (nnmbox-possibly-change-newsgroup group server) | |
391 | ;; Delete all articles in GROUP. | |
392 | (if (not force) | |
393 | () ; Don't delete the articles. | |
394 | (save-excursion | |
395 | (set-buffer nnmbox-mbox-buffer) | |
396 | (goto-char (point-min)) | |
397 | ;; Delete all articles in this group. | |
398 | (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":")) | |
399 | found) | |
400 | (while (search-forward ident nil t) | |
401 | (setq found t) | |
402 | (nnmbox-delete-mail)) | |
403 | (when found | |
16409b0b | 404 | (nnmbox-save-buffer))))) |
eec82323 LMI |
405 | ;; Remove the group from all structures. |
406 | (setq nnmbox-group-alist | |
407 | (delq (assoc group nnmbox-group-alist) nnmbox-group-alist) | |
408 | nnmbox-current-group nil) | |
409 | ;; Save the active file. | |
16409b0b | 410 | (nnmbox-save-active nnmbox-group-alist nnmbox-active-file) |
eec82323 LMI |
411 | t) |
412 | ||
413 | (deffoo nnmbox-request-rename-group (group new-name &optional server) | |
414 | (nnmbox-possibly-change-newsgroup group server) | |
415 | (save-excursion | |
416 | (set-buffer nnmbox-mbox-buffer) | |
417 | (goto-char (point-min)) | |
418 | (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":")) | |
419 | (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":")) | |
420 | found) | |
421 | (while (search-forward ident nil t) | |
422 | (replace-match new-ident t t) | |
423 | (setq found t)) | |
424 | (when found | |
16409b0b | 425 | (nnmbox-save-buffer)))) |
23f87bed MB |
426 | (let ((entry (assoc group nnmbox-group-active-articles))) |
427 | (when entry | |
428 | (setcar entry new-name))) | |
eec82323 LMI |
429 | (let ((entry (assoc group nnmbox-group-alist))) |
430 | (when entry | |
431 | (setcar entry new-name)) | |
432 | (setq nnmbox-current-group nil) | |
433 | ;; Save the new group alist. | |
16409b0b | 434 | (nnmbox-save-active nnmbox-group-alist nnmbox-active-file) |
eec82323 LMI |
435 | t)) |
436 | ||
437 | \f | |
438 | ;;; Internal functions. | |
439 | ||
440 | ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup | |
441 | ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox | |
442 | ;; delimiter line. | |
443 | (defun nnmbox-delete-mail (&optional force leave-delim) | |
444 | ;; Delete the current X-Gnus-Newsgroup line. | |
23f87bed MB |
445 | ;; First delete record of active article, unless the article is being |
446 | ;; replaced, indicated by FORCE being non-nil. | |
447 | (if (not force) | |
448 | (nnmbox-record-deleted-article (nnmbox-article-group-number t))) | |
eec82323 | 449 | (or force |
23f87bed | 450 | (gnus-delete-line)) |
eec82323 LMI |
451 | ;; Beginning of the article. |
452 | (save-excursion | |
453 | (save-restriction | |
454 | (narrow-to-region | |
4b70e299 MB |
455 | (prog2 |
456 | (re-search-backward (concat "^" message-unix-mail-delimiter) nil t) | |
457 | (if leave-delim (progn (forward-line 1) (point)) | |
458 | (match-beginning 0)) | |
459 | (forward-line 1)) | |
460 | (or (and (re-search-forward (concat "^" message-unix-mail-delimiter) | |
461 | nil t) | |
462 | (match-beginning 0)) | |
463 | (point-max))) | |
eec82323 | 464 | (goto-char (point-min)) |
23f87bed | 465 | ;; Only delete the article if no other group owns it as well. |
4b70e299 MB |
466 | (when (or force |
467 | (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)) | |
468 | (search-backward "\n\n" nil t)) | |
eec82323 LMI |
469 | (delete-region (point-min) (point-max)))))) |
470 | ||
471 | (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server) | |
472 | (when (and server | |
473 | (not (nnmbox-server-opened server))) | |
474 | (nnmbox-open-server server)) | |
475 | (when (or (not nnmbox-mbox-buffer) | |
476 | (not (buffer-name nnmbox-mbox-buffer))) | |
23f87bed | 477 | (nnmbox-read-mbox)) |
eec82323 LMI |
478 | (when (not nnmbox-group-alist) |
479 | (nnmail-activate 'nnmbox)) | |
480 | (if newsgroup | |
481 | (when (assoc newsgroup nnmbox-group-alist) | |
482 | (setq nnmbox-current-group newsgroup)) | |
483 | t)) | |
484 | ||
485 | (defun nnmbox-article-string (article) | |
486 | (if (numberp article) | |
487 | (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":" | |
488 | (int-to-string article) " ") | |
489 | (concat "\nMessage-ID: " article))) | |
490 | ||
23f87bed | 491 | (defun nnmbox-article-group-number (this-line) |
eec82323 | 492 | (save-excursion |
23f87bed MB |
493 | (if this-line |
494 | (beginning-of-line) | |
495 | (goto-char (point-min))) | |
eec82323 LMI |
496 | (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) " |
497 | nil t) | |
498 | (cons (buffer-substring (match-beginning 1) (match-end 1)) | |
e9bd5782 | 499 | (string-to-number |
eec82323 LMI |
500 | (buffer-substring (match-beginning 2) (match-end 2))))))) |
501 | ||
23f87bed MB |
502 | (defun nnmbox-in-header-p (pos) |
503 | "Return non-nil if POS is in the header of an article." | |
504 | (save-excursion | |
505 | (goto-char pos) | |
506 | (re-search-backward (concat "^" message-unix-mail-delimiter) nil t) | |
507 | (search-forward "\n\n" nil t) | |
508 | (< pos (point)))) | |
509 | ||
510 | (defun nnmbox-find-article (article) | |
511 | "Leaves point on the relevant X-Gnus-Newsgroup line if found." | |
512 | ;; Check that article is in the active range first, to avoid an | |
513 | ;; expensive exhaustive search if it isn't. | |
514 | (if (and (numberp article) | |
515 | (not (nnmbox-is-article-active-p article))) | |
516 | nil | |
517 | (let ((art-string (nnmbox-article-string article)) | |
518 | (found nil)) | |
519 | ;; There is the possibility that the X-Gnus-Newsgroup line appears | |
520 | ;; in the body of an article (for instance, if an article has been | |
521 | ;; forwarded from someone using Gnus as their mailer), so check | |
522 | ;; that the line is actually part of the article header. | |
523 | (or (and (search-forward art-string nil t) | |
524 | (nnmbox-in-header-p (point))) | |
525 | (progn | |
526 | (goto-char (point-min)) | |
527 | (while (and (not found) | |
528 | (search-forward art-string nil t)) | |
529 | (setq found (nnmbox-in-header-p (point)))) | |
530 | found))))) | |
531 | ||
532 | (defun nnmbox-record-active-article (group-art) | |
533 | (let* ((group (car group-art)) | |
534 | (article (cdr group-art)) | |
535 | (entry | |
536 | (or (assoc group nnmbox-group-active-articles) | |
537 | (progn | |
538 | (push (list group) | |
539 | nnmbox-group-active-articles) | |
540 | (car nnmbox-group-active-articles))))) | |
541 | ;; add article to index, either by building complete list | |
542 | ;; in reverse order, or as a list of ranges. | |
543 | (if (not nnmbox-group-building-active-articles) | |
544 | (setcdr entry (gnus-add-to-range (cdr entry) (list article))) | |
545 | (when (memq article (cdr entry)) | |
546 | (switch-to-buffer nnmbox-mbox-buffer) | |
547 | (error "Article %s:%d already exists!" group article)) | |
548 | (when (and (cadr entry) (< article (cadr entry))) | |
549 | (switch-to-buffer nnmbox-mbox-buffer) | |
550 | (error "Article %s:%d out of order" group article)) | |
551 | (setcdr entry (cons article (cdr entry)))))) | |
552 | ||
553 | (defun nnmbox-record-deleted-article (group-art) | |
554 | (let* ((group (car group-art)) | |
555 | (article (cdr group-art)) | |
556 | (entry | |
557 | (or (assoc group nnmbox-group-active-articles) | |
558 | (progn | |
559 | (push (list group) | |
560 | nnmbox-group-active-articles) | |
561 | (car nnmbox-group-active-articles))))) | |
562 | ;; remove article from index | |
563 | (setcdr entry (gnus-remove-from-range (cdr entry) (list article))))) | |
564 | ||
565 | (defun nnmbox-is-article-active-p (article) | |
566 | (gnus-member-of-range | |
567 | article | |
568 | (cdr (assoc nnmbox-current-group | |
569 | nnmbox-group-active-articles)))) | |
570 | ||
eec82323 LMI |
571 | (defun nnmbox-save-mail (group-art) |
572 | "Called narrowed to an article." | |
573 | (let ((delim (concat "^" message-unix-mail-delimiter))) | |
574 | (goto-char (point-min)) | |
575 | ;; This might come from somewhere else. | |
4b70e299 MB |
576 | (if (looking-at delim) |
577 | (forward-line 1) | |
578 | (insert "From nobody " (current-time-string) "\n")) | |
eec82323 | 579 | ;; Quote all "From " lines in the article. |
eec82323 | 580 | (while (re-search-forward delim nil t) |
4b70e299 MB |
581 | (goto-char (match-beginning 0)) |
582 | (insert ">"))) | |
583 | (goto-char (point-max)) | |
584 | (unless (bolp) | |
585 | (insert "\n")) | |
586 | (nnmail-insert-lines) | |
587 | (nnmail-insert-xref group-art) | |
588 | (nnmbox-insert-newsgroup-line group-art) | |
589 | (let ((alist group-art)) | |
590 | (while alist | |
591 | (nnmbox-record-active-article (car alist)) | |
592 | (setq alist (cdr alist)))) | |
593 | (run-hooks 'nnmail-prepare-save-mail-hook) | |
594 | (run-hooks 'nnmbox-prepare-save-mail-hook) | |
595 | group-art) | |
eec82323 LMI |
596 | |
597 | (defun nnmbox-insert-newsgroup-line (group-art) | |
598 | (save-excursion | |
599 | (goto-char (point-min)) | |
600 | (when (search-forward "\n\n" nil t) | |
601 | (forward-char -1) | |
602 | (while group-art | |
603 | (insert (format "X-Gnus-Newsgroup: %s:%d %s\n" | |
604 | (caar group-art) (cdar group-art) | |
605 | (current-time-string))) | |
606 | (setq group-art (cdr group-art)))) | |
607 | t)) | |
608 | ||
609 | (defun nnmbox-active-number (group) | |
610 | ;; Find the next article number in GROUP. | |
611 | (let ((active (cadr (assoc group nnmbox-group-alist)))) | |
612 | (if active | |
613 | (setcdr active (1+ (cdr active))) | |
614 | ;; This group is new, so we create a new entry for it. | |
615 | ;; This might be a bit naughty... creating groups on the drop of | |
616 | ;; a hat, but I don't know... | |
617 | (push (list group (setq active (cons 1 1))) | |
618 | nnmbox-group-alist)) | |
619 | (cdr active))) | |
620 | ||
621 | (defun nnmbox-create-mbox () | |
622 | (when (not (file-exists-p nnmbox-mbox-file)) | |
16409b0b GM |
623 | (let ((nnmail-file-coding-system |
624 | (or nnmbox-file-coding-system-for-write | |
23f87bed MB |
625 | nnmbox-file-coding-system)) |
626 | (dir (file-name-directory nnmbox-mbox-file))) | |
627 | (and dir (gnus-make-directory dir)) | |
a76a4381 SM |
628 | (nnmail-write-region (point-min) (point-min) |
629 | nnmbox-mbox-file t 'nomesg)))) | |
eec82323 LMI |
630 | |
631 | (defun nnmbox-read-mbox () | |
632 | (nnmail-activate 'nnmbox) | |
633 | (nnmbox-create-mbox) | |
634 | (if (and nnmbox-mbox-buffer | |
635 | (buffer-name nnmbox-mbox-buffer) | |
636 | (save-excursion | |
637 | (set-buffer nnmbox-mbox-buffer) | |
638 | (= (buffer-size) (nnheader-file-size nnmbox-mbox-file)))) | |
639 | () | |
640 | (save-excursion | |
641 | (let ((delim (concat "^" message-unix-mail-delimiter)) | |
642 | (alist nnmbox-group-alist) | |
23f87bed MB |
643 | (nnmbox-group-building-active-articles t) |
644 | start end end-header number) | |
eec82323 | 645 | (set-buffer (setq nnmbox-mbox-buffer |
16409b0b GM |
646 | (let ((nnheader-file-coding-system |
647 | nnmbox-file-coding-system)) | |
648 | (nnheader-find-file-noselect | |
23f87bed | 649 | nnmbox-mbox-file t t)))) |
16409b0b GM |
650 | (mm-enable-multibyte) |
651 | (buffer-disable-undo) | |
eec82323 | 652 | |
23f87bed | 653 | ;; Go through the group alist and compare against the mbox file. |
eec82323 LMI |
654 | (while alist |
655 | (goto-char (point-max)) | |
656 | (when (and (re-search-backward | |
657 | (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) " | |
658 | (caar alist)) nil t) | |
16409b0b GM |
659 | (> (setq number |
660 | (string-to-number | |
661 | (buffer-substring | |
662 | (match-beginning 1) (match-end 1)))) | |
663 | (cdadar alist))) | |
664 | (setcdr (cadar alist) number)) | |
eec82323 LMI |
665 | (setq alist (cdr alist))) |
666 | ||
23f87bed MB |
667 | ;; Examine all articles for our private X-Gnus-Newsgroup |
668 | ;; headers. This is done primarily as a consistency check, but | |
669 | ;; it is convenient for building an index of the articles | |
670 | ;; present, to avoid costly searches for missing articles | |
671 | ;; (eg. when expiring articles). | |
eec82323 | 672 | (goto-char (point-min)) |
23f87bed | 673 | (setq nnmbox-group-active-articles nil) |
eec82323 LMI |
674 | (while (re-search-forward delim nil t) |
675 | (setq start (match-beginning 0)) | |
23f87bed MB |
676 | (save-excursion |
677 | (search-forward "\n\n" nil t) | |
678 | (setq end-header (point)) | |
679 | (setq end (or (and | |
680 | (re-search-forward delim nil t) | |
681 | (match-beginning 0)) | |
682 | (point-max)))) | |
683 | (if (search-forward "\nX-Gnus-Newsgroup: " end-header t) | |
684 | ;; Build a list of articles in each group, remembering | |
685 | ;; that each article may be in more than one group. | |
686 | (progn | |
687 | (nnmbox-record-active-article (nnmbox-article-group-number t)) | |
688 | (while (search-forward "\nX-Gnus-Newsgroup: " end-header t) | |
689 | (nnmbox-record-active-article (nnmbox-article-group-number t)))) | |
690 | ;; The article is either new, or for some other reason | |
691 | ;; hasn't got our private headers, so add them now. The | |
692 | ;; only situation I've encountered when the X-Gnus-Newsgroup | |
693 | ;; header is missing is if the article contains a forwarded | |
694 | ;; message which does contain that header line (earlier | |
695 | ;; versions of Gnus didn't restrict their search to the | |
696 | ;; headers). In this case, there is an Xref line which | |
697 | ;; provides the relevant information to construct the | |
698 | ;; missing header(s). | |
eec82323 LMI |
699 | (save-excursion |
700 | (save-restriction | |
701 | (narrow-to-region start end) | |
23f87bed MB |
702 | (if (re-search-forward "\nXref: [^ ]+" end-header t) |
703 | ;; generate headers from Xref: | |
704 | (let (alist) | |
705 | (while (re-search-forward " \\([^:]+\\):\\([0-9]+\\)" end-header t) | |
706 | (push (cons (match-string 1) | |
e9bd5782 | 707 | (string-to-number (match-string 2))) alist)) |
23f87bed MB |
708 | (nnmbox-insert-newsgroup-line alist)) |
709 | ;; this is really a new article | |
710 | (nnmbox-save-mail | |
711 | (nnmail-article-group 'nnmbox-active-number)))))) | |
712 | (goto-char end)) | |
713 | ;; put article lists in order | |
714 | (setq alist nnmbox-group-active-articles) | |
715 | (while alist | |
716 | (setcdr (car alist) (gnus-compress-sequence (nreverse (cdar alist)))) | |
717 | (setq alist (cdr alist))))))) | |
eec82323 LMI |
718 | |
719 | (provide 'nnmbox) | |
720 | ||
cbee283d | 721 | ;; arch-tag: 611dd95f-be37-413a-b3ae-8b059ba93659 |
eec82323 | 722 | ;;; nnmbox.el ends here |