*** empty log message ***
[gnus] / lisp / nnheader.el
1 ;;; nnheader.el --- header access macros for Gnus and its backends
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; These macros may look very much like the ones in GNUS 4.1.  They
28 ;; are, in a way, but you should note that the indices they use have
29 ;; been changed from the internal GNUS format to the NOV format.  The
30 ;; makes it possible to read headers from XOVER much faster.
31 ;;
32 ;; The format of a header is now:
33 ;; [number subject from date id references chars lines xref]
34 ;;
35 ;; (That last entry is defined as "misc" in the NOV format, but Gnus
36 ;; uses it for xrefs.)
37
38 ;;; Code:
39
40 (require 'mail-utils)
41 (require 'sendmail)
42 (eval-when-compile (require 'cl))
43
44 (defvar nnheader-max-head-length 4096
45   "*Max length of the head of articles.")
46
47 (defvar nnheader-file-name-translation-alist nil
48   "*Alist that says how to translate characters in file names.
49 For instance, if \":\" is illegal as a file character in file names
50 on your system, you could say something like:
51
52 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
53
54 ;;; Header access macros.
55
56 (defmacro mail-header-number (header)
57   "Return article number in HEADER."
58   `(aref ,header 0))
59
60 (defmacro mail-header-set-number (header number)
61   "Set article number of HEADER to NUMBER."
62   `(aset ,header 0 ,number))
63
64 (defmacro mail-header-subject (header)
65   "Return subject string in HEADER."
66   `(aref ,header 1))
67
68 (defmacro mail-header-set-subject (header subject)
69   "Set article subject of HEADER to SUBJECT."
70   `(aset ,header 1 ,subject))
71
72 (defmacro mail-header-from (header)
73   "Return author string in HEADER."
74   `(aref ,header 2))
75
76 (defmacro mail-header-set-from (header from)
77   "Set article author of HEADER to FROM."
78   `(aset ,header 2 ,from))
79
80 (defmacro mail-header-date (header)
81   "Return date in HEADER."
82   `(aref ,header 3))
83
84 (defmacro mail-header-set-date (header date)
85   "Set article date of HEADER to DATE."
86   `(aset ,header 3 ,date))
87
88 (defalias 'mail-header-message-id 'mail-header-id)
89 (defmacro mail-header-id (header)
90   "Return Id in HEADER."
91   `(aref ,header 4))
92
93 (defalias 'mail-header-set-message-id 'mail-header-set-id)
94 (defmacro mail-header-set-id (header id)
95   "Set article Id of HEADER to ID."
96   `(aset ,header 4 ,id))
97
98 (defmacro mail-header-references (header)
99   "Return references in HEADER."
100   `(aref ,header 5))
101
102 (defmacro mail-header-set-references (header ref)
103   "Set article references of HEADER to REF."
104   `(aset ,header 5 ,ref))
105
106 (defmacro mail-header-chars (header)
107   "Return number of chars of article in HEADER."
108   `(aref ,header 6))
109
110 (defmacro mail-header-set-chars (header chars)
111   "Set number of chars in article of HEADER to CHARS."
112   `(aset ,header 6 ,chars))
113
114 (defmacro mail-header-lines (header)
115   "Return lines in HEADER."
116   `(aref ,header 7))
117
118 (defmacro mail-header-set-lines (header lines)
119   "Set article lines of HEADER to LINES."
120   `(aset ,header 7 ,lines))
121
122 (defmacro mail-header-xref (header)
123   "Return xref string in HEADER."
124   `(aref ,header 8))
125
126 (defmacro mail-header-set-xref (header xref)
127   "Set article xref of HEADER to xref."
128   `(aset ,header 8 ,xref))
129
130 (defun make-mail-header (&optional init)
131   "Create a new mail header structure initialized with INIT."
132   (make-vector 9 init))
133
134 ;; Parsing headers and NOV lines.
135
136 (defsubst nnheader-header-value ()
137   (buffer-substring (match-end 0) (gnus-point-at-eol)))
138
139 (defvar nnheader-newsgroup-none-id 1)
140
141 (defun nnheader-parse-head (&optional naked)
142   (let ((case-fold-search t)
143         (cur (current-buffer))
144         (buffer-read-only nil)
145         end ref in-reply-to lines p)
146     (goto-char (point-min))
147     (when naked
148       (insert "\n"))
149     ;; Search to the beginning of the next header. Error messages
150     ;; do not begin with 2 or 3.
151     (prog1
152         (when (or naked (re-search-forward "^[23][0-9]+ " nil t))
153           ;; This implementation of this function, with nine
154           ;; search-forwards instead of the one re-search-forward and
155           ;; a case (which basically was the old function) is actually
156           ;; about twice as fast, even though it looks messier.  You
157           ;; can't have everything, I guess.  Speed and elegance
158           ;; doesn't always go hand in hand.
159           (vector
160            ;; Number.
161            (if naked
162                (progn
163                  (setq p (point-min))
164                  0)
165              (prog1
166                  (read cur)
167                (end-of-line)
168                (setq p (point))
169                (narrow-to-region (point)
170                                  (or (and (search-forward "\n.\n" nil t)
171                                           (- (point) 2))
172                                      (point)))))
173            ;; Subject.
174            (progn
175              (goto-char p)
176              (if (search-forward "\nsubject: " nil t)
177                  (nnheader-header-value) "(none)"))
178            ;; From.
179            (progn
180              (goto-char p)
181              (if (search-forward "\nfrom: " nil t)
182                  (nnheader-header-value) "(nobody)"))
183            ;; Date.
184            (progn
185              (goto-char p)
186              (if (search-forward "\ndate: " nil t)
187                  (nnheader-header-value) ""))
188            ;; Message-ID.
189            (progn
190              (goto-char p)
191              (if (search-forward "\nmessage-id: " nil t)
192                  (nnheader-header-value)
193                ;; If there was no message-id, we just fake one to make
194                ;; subsequent routines simpler.
195                (concat "none+"
196                        (int-to-string
197                         (incf nnheader-newsgroup-none-id)))))
198            ;; References.
199            (progn
200              (goto-char p)
201              (if (search-forward "\nreferences: " nil t)
202                  (nnheader-header-value)
203                ;; Get the references from the in-reply-to header if there
204                ;; were no references and the in-reply-to header looks
205                ;; promising.
206                (if (and (search-forward "\nin-reply-to: " nil t)
207                         (setq in-reply-to (nnheader-header-value))
208                         (string-match "<[^>]+>" in-reply-to))
209                    (substring in-reply-to (match-beginning 0)
210                               (match-end 0))
211                  "")))
212            ;; Chars.
213            0
214            ;; Lines.
215            (progn
216              (goto-char p)
217              (if (search-forward "\nlines: " nil t)
218                  (if (numberp (setq lines (read cur)))
219                      lines 0)
220                0))
221            ;; Xref.
222            (progn
223              (goto-char p)
224              (and (search-forward "\nxref: " nil t)
225                   (nnheader-header-value)))))
226       (when naked
227         (goto-char (point-min))
228         (delete-char 1)))))
229
230 (defun nnheader-insert-nov (header)
231   (princ (mail-header-number header) (current-buffer))
232   (insert 
233    "\t"
234    (or (mail-header-subject header) "(none)") "\t"
235    (or (mail-header-from header) "(nobody)") "\t"
236    (or (mail-header-date header) "") "\t"
237    (or (mail-header-id header) "") "\t"
238    (or (mail-header-references header) "") "\t")
239   (princ (or (mail-header-chars header) 0) (current-buffer))
240   (insert "\t")
241   (princ (or (mail-header-lines header) 0) (current-buffer))
242   (insert "\t")
243   (when (mail-header-xref header) 
244     (insert "Xref: " (mail-header-xref header) "\t"))
245   (insert "\n"))
246
247 (defun nnheader-insert-article-line (article)
248   (goto-char (point-min))
249   (insert "220 ")
250   (princ article (current-buffer))
251   (insert " Article retrieved.\n")
252   (search-forward "\n\n" nil 'move)
253   (delete-region (point) (point-max))
254   (forward-char -1)
255   (insert "."))
256
257 ;; Various cruft the backends and Gnus need to communicate.
258
259 (defvar nntp-server-buffer nil)
260 (defvar gnus-verbose-backends 7
261   "*A number that says how talkative the Gnus backends should be.")
262 (defvar gnus-nov-is-evil nil
263   "If non-nil, Gnus backends will never output headers in the NOV format.")
264 (defvar news-reply-yank-from nil)
265 (defvar news-reply-yank-message-id nil)
266
267 (defvar nnheader-callback-function nil)
268
269 (defun nnheader-init-server-buffer ()
270   "Initialize the Gnus-backend communication buffer."
271   (save-excursion
272     (setq nntp-server-buffer (get-buffer-create " *nntpd*"))
273     (set-buffer nntp-server-buffer)
274     (buffer-disable-undo (current-buffer))
275     (erase-buffer)
276     (kill-all-local-variables)
277     (setq case-fold-search t)           ;Should ignore case.
278     t))
279
280
281 ;;; Various functions the backends use.
282
283 (defun nnheader-file-error (file)
284   "Return a string that says what is wrong with FILE."
285   (format
286    (cond
287     ((not (file-exists-p file))
288      "%s does not exist")
289     ((file-directory-p file)
290      "%s is a directory")
291     ((not (file-readable-p file))
292      "%s is not readable"))
293    file))
294
295 (defun nnheader-insert-head (file)
296   "Insert the head of the article."
297   (when (file-exists-p file)
298     (if (eq nnheader-max-head-length t)
299         ;; Just read the entire file.
300         (insert-file-contents-literally file)
301       ;; Read 1K blocks until we find a separator.
302       (let ((beg 0)
303             format-alist 
304             (chop 1024))
305         (while (and (eq chop (nth 1 (insert-file-contents
306                                      file nil beg (incf beg chop))))
307                     (prog1 (not (search-forward "\n\n" nil t)) 
308                       (goto-char (point-max)))
309                     (or (null nnheader-max-head-length)
310                         (< beg nnheader-max-head-length))))))
311     t))
312
313 (defun nnheader-article-p ()
314   "Say whether the current buffer looks like an article."
315   (goto-char (point-min))
316   (if (not (search-forward "\n\n" nil t))
317       nil
318     (narrow-to-region (point-min) (1- (point)))
319     (goto-char (point-min))
320     (while (looking-at "[A-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
321       (goto-char (match-end 0)))
322     (prog1
323         (eobp)
324       (widen))))    
325
326 (defun nnheader-insert-references (references message-id)
327   "Insert a References header based on REFERENCES and MESSAGE-ID."
328   (if (and (not references) (not message-id)) 
329       ()        ; This is illegal, but not all articles have Message-IDs.
330     (mail-position-on-field "References")
331     (let ((begin (save-excursion (beginning-of-line) (point)))
332           (fill-column 78)
333           (fill-prefix "\t"))
334       (if references (insert references))
335       (if (and references message-id) (insert " "))
336       (if message-id (insert message-id))
337       ;; Fold long References lines to conform to RFC1036 (sort of).
338       ;; The region must end with a newline to fill the region
339       ;; without inserting extra newline.
340       (fill-region-as-paragraph begin (1+ (point))))))
341
342 (defun nnheader-replace-header (header new-value)
343   "Remove HEADER and insert the NEW-VALUE."
344   (save-excursion
345     (save-restriction
346       (nnheader-narrow-to-headers)
347       (prog1
348           (message-remove-header header)
349         (goto-char (point-max))
350         (insert header ": " new-value "\n")))))
351
352 (defun nnheader-narrow-to-headers ()
353   "Narrow to the head of an article."
354   (widen)
355   (narrow-to-region
356    (goto-char (point-min))
357    (if (search-forward "\n\n" nil t)
358        (1- (point))
359      (point-max)))
360   (goto-char (point-min)))
361
362 (defun nnheader-set-temp-buffer (name)
363   "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
364   (set-buffer (get-buffer-create name))
365   (buffer-disable-undo (current-buffer))
366   (erase-buffer)
367   (current-buffer))
368
369 (defmacro nnheader-temp-write (file &rest forms)
370   "Create a new buffer, evaluate FORM there, and write the buffer to FILE."
371   `(save-excursion
372      (let ((nnheader-temp-file ,file)
373            (nnheader-temp-cur-buffer
374             (nnheader-set-temp-buffer
375              (generate-new-buffer-name " *nnheader temp*"))))
376        (when (and nnheader-temp-file 
377                   (not (file-directory-p (file-name-directory 
378                                           nnheader-temp-file))))
379          (make-directory (file-name-directory nnheader-temp-file) t))
380        (unwind-protect
381            (prog1
382                (progn
383                  ,@forms)
384              (when nnheader-temp-file
385                (set-buffer nnheader-temp-cur-buffer)
386                (write-region (point-min) (point-max) 
387                              nnheader-temp-file nil 'nomesg)))
388          (when (buffer-name nnheader-temp-cur-buffer)
389            (kill-buffer nnheader-temp-cur-buffer))))))
390
391 (put 'nnheader-temp-write 'lisp-indent-function 1)
392 (put 'nnheader-temp-write 'lisp-indent-hook 1)
393 (put 'nnheader-temp-write 'edebug-form-spec '(file &rest form))
394
395 (defvar jka-compr-compression-info-list)
396 (defvar nnheader-numerical-files
397   (if (boundp 'jka-compr-compression-info-list)
398       (concat "\\([0-9]+\\)\\(" 
399               (mapconcat (lambda (i) (aref i 0))
400                          jka-compr-compression-info-list "\\|")
401               "\\)?")
402     "[0-9]+$")
403   "Regexp that match numerical files.")
404
405 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
406   "Regexp that matches numerical file names.")
407
408 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
409   "Regexp that matches numerical full file paths.")
410
411 (defsubst nnheader-file-to-number (file)
412   "Take a file name and return the article number."
413   (if (not (boundp 'jka-compr-compression-info-list))
414       (string-to-int file)
415     (string-match nnheader-numerical-short-files file)
416     (string-to-int (match-string 0 file))))
417
418 (defun nnheader-directory-articles (dir)
419   "Return a list of all article files in a directory."
420   (mapcar 'nnheader-file-to-number
421           (directory-files dir nil nnheader-numerical-short-files t)))
422
423 (defun nnheader-article-to-file-alist (dir)
424   "Return an alist of article/file pairs in DIR."
425   (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
426           (directory-files dir nil nnheader-numerical-short-files t)))
427
428 (defun nnheader-fold-continuation-lines ()
429   "Fold continuation lines in the current buffer."
430   (goto-char (point-min))
431   (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
432     (replace-match " " t t)))
433
434 (defun nnheader-translate-file-chars (file)
435   (if (null nnheader-file-name-translation-alist)
436       ;; No translation is necessary.
437       file 
438     ;; We translate -- but only the file name.  We leave the directory
439     ;; alone.
440     (let* ((new (file-name-nondirectory file))
441            (len (length new))
442            (i 0)
443           trans)
444       (while (< i len)
445         (when (setq trans (cdr (assq (aref new i)
446                                      nnheader-file-name-translation-alist)))
447           (aset new i trans))
448         (incf i))
449       (concat (file-name-directory file) new))))
450
451 (defun nnheader-report (backend &rest args)
452   "Report an error from the BACKEND.
453 The first string in ARGS can be a format string."
454   (set (intern (format "%s-status-string" backend))
455        (if (< (length args) 2)
456            (car args)
457          (apply 'format args)))
458   nil)
459
460 (defun nnheader-get-report (backend)
461   (message "%s" (symbol-value (intern (format "%s-status-string" backend)))))
462
463 (defun nnheader-insert (format &rest args)
464   "Clear the communicaton buffer and insert FORMAT and ARGS into the buffer.
465 If FORMAT isn't a format string, it and all ARGS will be inserted
466 without formatting."
467   (save-excursion
468     (set-buffer nntp-server-buffer)
469     (erase-buffer)
470     (if (string-match "%" format)
471         (insert (apply 'format format args))
472       (apply 'insert format args))
473     t))
474
475 (defun nnheader-mail-file-mbox-p (file)
476   "Say whether FILE looks like an Unix mbox file."
477   (when (and (file-exists-p file)
478              (file-readable-p file)
479              (file-regular-p file))
480     (save-excursion
481       (nnheader-set-temp-buffer " *mail-file-mbox-p*")
482       (insert-file-contents-literally file)
483       (goto-char (point-min))
484       (prog1
485           (looking-at message-unix-mail-delimiter)
486         (kill-buffer (current-buffer))))))
487
488 (defun nnheader-replace-chars-in-string (string from to)
489   "Replace characters in STRING from FROM to TO."
490   (let ((string (substring string 0))   ;Copy string.
491         (len (length string))
492         (idx 0))
493     ;; Replace all occurrences of FROM with TO.
494     (while (< idx len)
495       (if (= (aref string idx) from)
496           (aset string idx to))
497       (setq idx (1+ idx)))
498     string))
499
500 (defun nnheader-file-to-group (file &optional top)
501   "Return a group name based on FILE and TOP."
502   (nnheader-replace-chars-in-string 
503    (if (not top)
504        file
505      (condition-case ()
506          (substring (expand-file-name file)
507                     (length 
508                      (expand-file-name
509                       (file-name-as-directory top))))
510        (error "")))
511    ?/ ?.))
512
513 (defun nnheader-message (level &rest args)
514   "Message if the Gnus backends are talkative."
515   (if (or (not (numberp gnus-verbose-backends))
516           (<= level gnus-verbose-backends))
517       (apply 'message args)
518     (apply 'format args)))
519
520 (defun nnheader-be-verbose (level)
521   "Return whether the backends should be verbose on LEVEL."
522   (or (not (numberp gnus-verbose-backends))
523       (<= level gnus-verbose-backends)))
524
525 (defun nnheader-group-pathname (group dir &optional file)
526   "Make pathname for GROUP."
527   (concat
528    (let ((dir (file-name-as-directory (expand-file-name dir))))
529      ;; If this directory exists, we use it directly.
530      (if (file-directory-p (concat dir group))
531          (concat dir group "/")
532        ;; If not, we translate dots into slashes.
533        (concat dir (nnheader-replace-chars-in-string group ?. ?/) "/")))
534    (cond ((null file) "")
535          ((numberp file) (int-to-string file))
536          (t file))))
537
538 (defun nnheader-functionp (form)
539   "Return non-nil if FORM is funcallable."
540   (or (and (symbolp form) (fboundp form))
541       (and (listp form) (eq (car form) 'lambda))))
542
543 (defun nnheader-concat (dir file)
544   "Concat DIR as directory to FILE."
545   (concat (file-name-as-directory dir) file))
546
547 (require 'nnheader-ems)
548
549 (run-hooks 'nnheader-load-hook)
550
551 (provide 'nnheader)
552
553 ;;; nnheader.el ends here