*** 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
42 (defvar nnheader-max-head-length 4096
43   "*Max length of the head of articles.")
44
45 (defvar nnheader-file-name-translation-alist nil
46   "*Alist that says how to translate characters in file names.
47 For instance, if \":\" is illegal as a file character in file names
48 on your system, you could say something like:
49
50 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
51
52 (eval-and-compile
53  (autoload 'nnmail-message-id "nnmail")
54  (autoload 'mail-position-on-field "sendmail")
55  (autoload 'message-remove-header "message"))
56
57 ;;; Header access macros.
58
59 (defmacro mail-header-number (header)
60   "Return article number in HEADER."
61   `(aref ,header 0))
62
63 (defmacro mail-header-set-number (header number)
64   "Set article number of HEADER to NUMBER."
65   `(aset ,header 0 ,number))
66
67 (defmacro mail-header-subject (header)
68   "Return subject string in HEADER."
69   `(aref ,header 1))
70
71 (defmacro mail-header-set-subject (header subject)
72   "Set article subject of HEADER to SUBJECT."
73   `(aset ,header 1 ,subject))
74
75 (defmacro mail-header-from (header)
76   "Return author string in HEADER."
77   `(aref ,header 2))
78
79 (defmacro mail-header-set-from (header from)
80   "Set article author of HEADER to FROM."
81   `(aset ,header 2 ,from))
82
83 (defmacro mail-header-date (header)
84   "Return date in HEADER."
85   `(aref ,header 3))
86
87 (defmacro mail-header-set-date (header date)
88   "Set article date of HEADER to DATE."
89   `(aset ,header 3 ,date))
90
91 (defalias 'mail-header-message-id 'mail-header-id)
92 (defmacro mail-header-id (header)
93   "Return Id in HEADER."
94   `(aref ,header 4))
95
96 (defalias 'mail-header-set-message-id 'mail-header-set-id)
97 (defmacro mail-header-set-id (header id)
98   "Set article Id of HEADER to ID."
99   `(aset ,header 4 ,id))
100
101 (defmacro mail-header-references (header)
102   "Return references in HEADER."
103   `(aref ,header 5))
104
105 (defmacro mail-header-set-references (header ref)
106   "Set article references of HEADER to REF."
107   `(aset ,header 5 ,ref))
108
109 (defmacro mail-header-chars (header)
110   "Return number of chars of article in HEADER."
111   `(aref ,header 6))
112
113 (defmacro mail-header-set-chars (header chars)
114   "Set number of chars in article of HEADER to CHARS."
115   `(aset ,header 6 ,chars))
116
117 (defmacro mail-header-lines (header)
118   "Return lines in HEADER."
119   `(aref ,header 7))
120
121 (defmacro mail-header-set-lines (header lines)
122   "Set article lines of HEADER to LINES."
123   `(aset ,header 7 ,lines))
124
125 (defmacro mail-header-xref (header)
126   "Return xref string in HEADER."
127   `(aref ,header 8))
128
129 (defmacro mail-header-set-xref (header xref)
130   "Set article xref of HEADER to xref."
131   `(aset ,header 8 ,xref))
132
133 (defun make-mail-header (&optional init)
134   "Create a new mail header structure initialized with INIT."
135   (make-vector 9 init))
136
137 (defun make-full-mail-header (&optional number subject from date id
138                                         references chars lines xref)
139   "Create a new mail header structure initialized with the parameters given."
140   (vector number subject from date id references chars lines xref))
141   
142 ;; Parsing headers and NOV lines.
143
144 (defsubst nnheader-header-value ()
145   (buffer-substring (match-end 0) (gnus-point-at-eol)))
146
147 (defvar nnheader-newsgroup-none-id 1)
148
149 (defun nnheader-parse-head (&optional naked)
150   (let ((case-fold-search t)
151         (cur (current-buffer))
152         (buffer-read-only nil)
153         end ref in-reply-to lines p)
154     (goto-char (point-min))
155     (when naked
156       (insert "\n"))
157     ;; Search to the beginning of the next header. Error messages
158     ;; do not begin with 2 or 3.
159     (prog1
160         (when (or naked (re-search-forward "^[23][0-9]+ " nil t))
161           ;; This implementation of this function, with nine
162           ;; search-forwards instead of the one re-search-forward and
163           ;; a case (which basically was the old function) is actually
164           ;; about twice as fast, even though it looks messier.  You
165           ;; can't have everything, I guess.  Speed and elegance
166           ;; doesn't always go hand in hand.
167           (vector
168            ;; Number.
169            (if naked
170                (progn
171                  (setq p (point-min))
172                  0)
173              (prog1
174                  (read cur)
175                (end-of-line)
176                (setq p (point))
177                (narrow-to-region (point)
178                                  (or (and (search-forward "\n.\n" nil t)
179                                           (- (point) 2))
180                                      (point)))))
181            ;; Subject.
182            (progn
183              (goto-char p)
184              (if (search-forward "\nsubject: " nil t)
185                  (nnheader-header-value) "(none)"))
186            ;; From.
187            (progn
188              (goto-char p)
189              (if (search-forward "\nfrom: " nil t)
190                  (nnheader-header-value) "(nobody)"))
191            ;; Date.
192            (progn
193              (goto-char p)
194              (if (search-forward "\ndate: " nil t)
195                  (nnheader-header-value) ""))
196            ;; Message-ID.
197            (progn
198              (goto-char p)
199              (if (search-forward "\nmessage-id: " nil t)
200                  (nnheader-header-value)
201                ;; If there was no message-id, we just fake one to make
202                ;; subsequent routines simpler.
203                (concat "none+"
204                        (int-to-string
205                         (incf nnheader-newsgroup-none-id)))))
206            ;; References.
207            (progn
208              (goto-char p)
209              (if (search-forward "\nreferences: " nil t)
210                  (nnheader-header-value)
211                ;; Get the references from the in-reply-to header if there
212                ;; were no references and the in-reply-to header looks
213                ;; promising.
214                (if (and (search-forward "\nin-reply-to: " nil t)
215                         (setq in-reply-to (nnheader-header-value))
216                         (string-match "<[^>]+>" in-reply-to))
217                    (substring in-reply-to (match-beginning 0)
218                               (match-end 0))
219                  "")))
220            ;; Chars.
221            0
222            ;; Lines.
223            (progn
224              (goto-char p)
225              (if (search-forward "\nlines: " nil t)
226                  (if (numberp (setq lines (read cur)))
227                      lines 0)
228                0))
229            ;; Xref.
230            (progn
231              (goto-char p)
232              (and (search-forward "\nxref: " nil t)
233                   (nnheader-header-value)))))
234       (when naked
235         (goto-char (point-min))
236         (delete-char 1)))))
237
238 (defun nnheader-insert-nov (header)
239   (princ (mail-header-number header) (current-buffer))
240   (insert 
241    "\t"
242    (or (mail-header-subject header) "(none)") "\t"
243    (or (mail-header-from header) "(nobody)") "\t"
244    (or (mail-header-date header) "") "\t"
245    (or (mail-header-id header) 
246        (nnmail-message-id)) "\t"
247    (or (mail-header-references header) "") "\t")
248   (princ (or (mail-header-chars header) 0) (current-buffer))
249   (insert "\t")
250   (princ (or (mail-header-lines header) 0) (current-buffer))
251   (insert "\t")
252   (when (mail-header-xref header) 
253     (insert "Xref: " (mail-header-xref header) "\t"))
254   (insert "\n"))
255
256 (defun nnheader-insert-article-line (article)
257   (goto-char (point-min))
258   (insert "220 ")
259   (princ article (current-buffer))
260   (insert " Article retrieved.\n")
261   (search-forward "\n\n" nil 'move)
262   (delete-region (point) (point-max))
263   (forward-char -1)
264   (insert "."))
265
266 ;; Various cruft the backends and Gnus need to communicate.
267
268 (defvar nntp-server-buffer nil)
269 (defvar gnus-verbose-backends 7
270   "*A number that says how talkative the Gnus backends should be.")
271 (defvar gnus-nov-is-evil nil
272   "If non-nil, Gnus backends will never output headers in the NOV format.")
273 (defvar news-reply-yank-from nil)
274 (defvar news-reply-yank-message-id nil)
275
276 (defvar nnheader-callback-function nil)
277
278 (defun nnheader-init-server-buffer ()
279   "Initialize the Gnus-backend communication buffer."
280   (save-excursion
281     (unless (gnus-buffer-live-p nntp-server-buffer)
282       (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
283     (set-buffer nntp-server-buffer)
284     (buffer-disable-undo (current-buffer))
285     (erase-buffer)
286     (kill-all-local-variables)
287     (setq case-fold-search t)           ;Should ignore case.
288     t))
289
290 ;;; Various functions the backends use.
291
292 (defun nnheader-file-error (file)
293   "Return a string that says what is wrong with FILE."
294   (format
295    (cond
296     ((not (file-exists-p file))
297      "%s does not exist")
298     ((file-directory-p file)
299      "%s is a directory")
300     ((not (file-readable-p file))
301      "%s is not readable"))
302    file))
303
304 (defun nnheader-insert-head (file)
305   "Insert the head of the article."
306   (when (file-exists-p file)
307     (if (eq nnheader-max-head-length t)
308         ;; Just read the entire file.
309         (nnheader-insert-file-contents-literally file)
310       ;; Read 1K blocks until we find a separator.
311       (let ((beg 0)
312             format-alist 
313             (chop 1024))
314         (while (and (eq chop (nth 1 (insert-file-contents
315                                      file nil beg (incf beg chop))))
316                     (prog1 (not (search-forward "\n\n" nil t)) 
317                       (goto-char (point-max)))
318                     (or (null nnheader-max-head-length)
319                         (< beg nnheader-max-head-length))))))
320     t))
321
322 (defun nnheader-article-p ()
323   "Say whether the current buffer looks like an article."
324   (goto-char (point-min))
325   (if (not (search-forward "\n\n" nil t))
326       nil
327     (narrow-to-region (point-min) (1- (point)))
328     (goto-char (point-min))
329     (while (looking-at "[A-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
330       (goto-char (match-end 0)))
331     (prog1
332         (eobp)
333       (widen))))    
334
335 (defun nnheader-insert-references (references message-id)
336   "Insert a References header based on REFERENCES and MESSAGE-ID."
337   (if (and (not references) (not message-id)) 
338       ()        ; This is illegal, but not all articles have Message-IDs.
339     (mail-position-on-field "References")
340     (let ((begin (save-excursion (beginning-of-line) (point)))
341           (fill-column 78)
342           (fill-prefix "\t"))
343       (if references (insert references))
344       (if (and references message-id) (insert " "))
345       (if message-id (insert message-id))
346       ;; Fold long References lines to conform to RFC1036 (sort of).
347       ;; The region must end with a newline to fill the region
348       ;; without inserting extra newline.
349       (fill-region-as-paragraph begin (1+ (point))))))
350
351 (defun nnheader-replace-header (header new-value)
352   "Remove HEADER and insert the NEW-VALUE."
353   (save-excursion
354     (save-restriction
355       (nnheader-narrow-to-headers)
356       (prog1
357           (message-remove-header header)
358         (goto-char (point-max))
359         (insert header ": " new-value "\n")))))
360
361 (defun nnheader-narrow-to-headers ()
362   "Narrow to the head of an article."
363   (widen)
364   (narrow-to-region
365    (goto-char (point-min))
366    (if (search-forward "\n\n" nil t)
367        (1- (point))
368      (point-max)))
369   (goto-char (point-min)))
370
371 (defun nnheader-set-temp-buffer (name &optional noerase)
372   "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
373   (set-buffer (get-buffer-create name))
374   (buffer-disable-undo (current-buffer))
375   (unless noerase
376     (erase-buffer))
377   (current-buffer))
378
379 (defmacro nnheader-temp-write (file &rest forms)
380   "Create a new buffer, evaluate FORM there, and write the buffer to FILE."
381   `(save-excursion
382      (let* ((nnheader-temp-file ,file)
383             (default-major-mode 'fundamental-mode)
384             (nnheader-temp-cur-buffer
385              (nnheader-set-temp-buffer
386               (generate-new-buffer-name " *nnheader temp*"))))
387        (when (and nnheader-temp-file 
388                   (not (file-directory-p (file-name-directory 
389                                           nnheader-temp-file))))
390          (make-directory (file-name-directory nnheader-temp-file) t))
391        (unwind-protect
392            (prog1
393                (progn
394                  ,@forms)
395              (when nnheader-temp-file
396                (set-buffer nnheader-temp-cur-buffer)
397                (write-region (point-min) (point-max) 
398                              nnheader-temp-file nil 'nomesg)))
399          (when (buffer-name nnheader-temp-cur-buffer)
400            (kill-buffer nnheader-temp-cur-buffer))))))
401
402 (put 'nnheader-temp-write 'lisp-indent-function 1)
403 (put 'nnheader-temp-write 'lisp-indent-hook 1)
404 (put 'nnheader-temp-write 'edebug-form-spec '(form body))
405
406 (defvar jka-compr-compression-info-list)
407 (defvar nnheader-numerical-files
408   (if (boundp 'jka-compr-compression-info-list)
409       (concat "\\([0-9]+\\)\\(" 
410               (mapconcat (lambda (i) (aref i 0))
411                          jka-compr-compression-info-list "\\|")
412               "\\)?")
413     "[0-9]+$")
414   "Regexp that match numerical files.")
415
416 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
417   "Regexp that matches numerical file names.")
418
419 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
420   "Regexp that matches numerical full file paths.")
421
422 (defsubst nnheader-file-to-number (file)
423   "Take a file name and return the article number."
424   (if (not (boundp 'jka-compr-compression-info-list))
425       (string-to-int file)
426     (string-match nnheader-numerical-short-files file)
427     (string-to-int (match-string 0 file))))
428
429 (defun nnheader-directory-files-safe (&rest args)
430   ;; It has been reported numerous times that `directory-files'
431   ;; fails with an alarming frequency on NFS mounted file systems.
432   ;; This function executes that function twice and returns 
433   ;; the longest result.
434   (let ((first (apply 'directory-files args))
435         (second (apply 'directory-files args)))
436     (if (> (length first) (length second))
437         first
438       second)))
439
440 (defun nnheader-directory-articles (dir)
441   "Return a list of all article files in a directory."
442   (mapcar 'nnheader-file-to-number
443           (nnheader-directory-files-safe
444            dir nil nnheader-numerical-short-files t)))
445
446 (defun nnheader-article-to-file-alist (dir)
447   "Return an alist of article/file pairs in DIR."
448   (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
449           (nnheader-directory-files-safe
450            dir nil nnheader-numerical-short-files t)))
451
452 (defun nnheader-fold-continuation-lines ()
453   "Fold continuation lines in the current buffer."
454   (goto-char (point-min))
455   (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
456     (replace-match " " t t)))
457
458 (defun nnheader-translate-file-chars (file)
459   (if (null nnheader-file-name-translation-alist)
460       ;; No translation is necessary.
461       file 
462     ;; We translate -- but only the file name.  We leave the directory
463     ;; alone.
464     (let* ((i 0)
465            trans leaf path len)
466       (if (string-match "/[^/]+\\'" file)
467           ;; This is needed on NT's and stuff.
468           (setq leaf (substring file (1+ (match-beginning 0)))
469                 path (substring file 0 (1+ (match-beginning 0))))
470         ;; Fall back on this.
471         (setq leaf (file-name-nondirectory file)
472               path (file-name-directory file)))
473       (setq len (length leaf))
474       (while (< i len)
475         (when (setq trans (cdr (assq (aref leaf i)
476                                      nnheader-file-name-translation-alist)))
477           (aset leaf i trans))
478         (incf i))
479       (concat path leaf))))
480
481 (defun nnheader-report (backend &rest args)
482   "Report an error from the BACKEND.
483 The first string in ARGS can be a format string."
484   (set (intern (format "%s-status-string" backend))
485        (if (< (length args) 2)
486            (car args)
487          (apply 'format args)))
488   nil)
489
490 (defun nnheader-get-report (backend)
491   "Get the most recent report from BACKEND."
492   (condition-case ()
493       (message "%s" (symbol-value (intern (format "%s-status-string"
494                                                   backend))))
495     (error (message ""))))
496
497 (defun nnheader-insert (format &rest args)
498   "Clear the communicaton buffer and insert FORMAT and ARGS into the buffer.
499 If FORMAT isn't a format string, it and all ARGS will be inserted
500 without formatting."
501   (save-excursion
502     (set-buffer nntp-server-buffer)
503     (erase-buffer)
504     (if (string-match "%" format)
505         (insert (apply 'format format args))
506       (apply 'insert format args))
507     t))
508
509 (defun nnheader-mail-file-mbox-p (file)
510   "Say whether FILE looks like an Unix mbox file."
511   (when (and (file-exists-p file)
512              (file-readable-p file)
513              (file-regular-p file))
514     (save-excursion
515       (nnheader-set-temp-buffer " *mail-file-mbox-p*")
516       (nnheader-insert-file-contents-literally file)
517       (goto-char (point-min))
518       (prog1
519           (looking-at message-unix-mail-delimiter)
520         (kill-buffer (current-buffer))))))
521
522 (defun nnheader-replace-chars-in-string (string from to)
523   "Replace characters in STRING from FROM to TO."
524   (let ((string (substring string 0))   ;Copy string.
525         (len (length string))
526         (idx 0))
527     ;; Replace all occurrences of FROM with TO.
528     (while (< idx len)
529       (if (= (aref string idx) from)
530           (aset string idx to))
531       (setq idx (1+ idx)))
532     string))
533
534 (defun nnheader-file-to-group (file &optional top)
535   "Return a group name based on FILE and TOP."
536   (nnheader-replace-chars-in-string 
537    (if (not top)
538        file
539      (condition-case ()
540          (substring (expand-file-name file)
541                     (length 
542                      (expand-file-name
543                       (file-name-as-directory top))))
544        (error "")))
545    ?/ ?.))
546
547 (defun nnheader-message (level &rest args)
548   "Message if the Gnus backends are talkative."
549   (if (or (not (numberp gnus-verbose-backends))
550           (<= level gnus-verbose-backends))
551       (apply 'message args)
552     (apply 'format args)))
553
554 (defun nnheader-be-verbose (level)
555   "Return whether the backends should be verbose on LEVEL."
556   (or (not (numberp gnus-verbose-backends))
557       (<= level gnus-verbose-backends)))
558
559 (defun nnheader-group-pathname (group dir &optional file)
560   "Make pathname for GROUP."
561   (concat
562    (let ((dir (file-name-as-directory (expand-file-name dir))))
563      ;; If this directory exists, we use it directly.
564      (if (file-directory-p (concat dir group))
565          (concat dir group "/")
566        ;; If not, we translate dots into slashes.
567        (concat dir (nnheader-replace-chars-in-string group ?. ?/) "/")))
568    (cond ((null file) "")
569          ((numberp file) (int-to-string file))
570          (t file))))
571
572 (defun nnheader-functionp (form)
573   "Return non-nil if FORM is funcallable."
574   (or (and (symbolp form) (fboundp form))
575       (and (listp form) (eq (car form) 'lambda))))
576
577 (defun nnheader-concat (dir file)
578   "Concat DIR as directory to FILE."
579   (concat (file-name-as-directory dir) file))
580
581 (defun nnheader-ms-strip-cr ()
582   "Strip ^M from the end of all lines."
583   (save-excursion
584     (goto-char (point-min))
585     (while (re-search-forward "\r$" nil t)
586       (delete-backward-char 1))))
587
588 (defun nnheader-file-size (file)
589   "Return the file size of FILE or 0."
590   (or (nth 7 (file-attributes file)) 0))
591
592 (defun nnheader-find-etc-directory (package)
593   "Go through the path and find the \".../etc/PACKAGE\" directory."
594   (let ((path load-path)
595         dir result)
596     ;; We try to find the dir by looking at the load path,
597     ;; stripping away the last component and adding "etc/".
598     (while path
599       (if (and (car path)
600                (file-exists-p
601                 (setq dir (concat
602                            (file-name-directory
603                             (directory-file-name (car path)))
604                            "etc/" package "/")))
605                (file-directory-p dir))
606           (setq result dir
607                 path nil)
608         (setq path (cdr path))))
609     result))
610
611 (defvar ange-ftp-path-format)
612 (defvar efs-path-regexp)
613 (defun nnheader-re-read-dir (path)
614   "Re-read directory PATH if PATH is on a remote system."
615     (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
616         (when (string-match efs-path-regexp path)
617           (efs-re-read-dir path))
618      (if (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
619       (when (string-match (car ange-ftp-path-format) path)
620         (ange-ftp-re-read-dir path)))))
621
622 (fset 'nnheader-run-at-time 'run-at-time)
623 (fset 'nnheader-cancel-timer 'cancel-timer)
624 (fset 'nnheader-cancel-function-timers 'cancel-function-timers)
625 (fset 'nnheader-find-file-noselect 'find-file-noselect)
626 (fset 'nnheader-insert-file-contents-literally
627       'insert-file-contents-literally)
628
629 (when (string-match "XEmacs\\|Lucid" emacs-version)
630   (require 'nnheaderxm))
631
632 (run-hooks 'nnheader-load-hook)
633
634 (provide 'nnheader)
635
636 ;;; nnheader.el ends here