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.
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
32 ;; The format of a header is now:
33 ;; [number subject from date id references chars lines xref]
35 ;; (That last entry is defined as "misc" in the NOV format, but Gnus
36 ;; uses it for xrefs.)
42 (defvar nnheader-max-head-length 4096
43 "*Max length of the head of articles.")
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:
50 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
53 (autoload 'nnmail-message-id "nnmail")
54 (autoload 'mail-position-on-field "sendmail")
55 (autoload 'message-remove-header "message"))
57 ;;; Header access macros.
59 (defmacro mail-header-number (header)
60 "Return article number in HEADER."
63 (defmacro mail-header-set-number (header number)
64 "Set article number of HEADER to NUMBER."
65 `(aset ,header 0 ,number))
67 (defmacro mail-header-subject (header)
68 "Return subject string in HEADER."
71 (defmacro mail-header-set-subject (header subject)
72 "Set article subject of HEADER to SUBJECT."
73 `(aset ,header 1 ,subject))
75 (defmacro mail-header-from (header)
76 "Return author string in HEADER."
79 (defmacro mail-header-set-from (header from)
80 "Set article author of HEADER to FROM."
81 `(aset ,header 2 ,from))
83 (defmacro mail-header-date (header)
84 "Return date in HEADER."
87 (defmacro mail-header-set-date (header date)
88 "Set article date of HEADER to DATE."
89 `(aset ,header 3 ,date))
91 (defalias 'mail-header-message-id 'mail-header-id)
92 (defmacro mail-header-id (header)
93 "Return Id in HEADER."
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))
101 (defmacro mail-header-references (header)
102 "Return references in HEADER."
105 (defmacro mail-header-set-references (header ref)
106 "Set article references of HEADER to REF."
107 `(aset ,header 5 ,ref))
109 (defmacro mail-header-chars (header)
110 "Return number of chars of article in HEADER."
113 (defmacro mail-header-set-chars (header chars)
114 "Set number of chars in article of HEADER to CHARS."
115 `(aset ,header 6 ,chars))
117 (defmacro mail-header-lines (header)
118 "Return lines in HEADER."
121 (defmacro mail-header-set-lines (header lines)
122 "Set article lines of HEADER to LINES."
123 `(aset ,header 7 ,lines))
125 (defmacro mail-header-xref (header)
126 "Return xref string in HEADER."
129 (defmacro mail-header-set-xref (header xref)
130 "Set article xref of HEADER to xref."
131 `(aset ,header 8 ,xref))
133 (defun make-mail-header (&optional init)
134 "Create a new mail header structure initialized with INIT."
135 (make-vector 9 init))
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))
142 ;; Parsing headers and NOV lines.
144 (defsubst nnheader-header-value ()
145 (buffer-substring (match-end 0) (gnus-point-at-eol)))
147 (defvar nnheader-newsgroup-none-id 1)
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))
157 ;; Search to the beginning of the next header. Error messages
158 ;; do not begin with 2 or 3.
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.
177 (narrow-to-region (point)
178 (or (and (search-forward "\n.\n" nil t)
184 (if (search-forward "\nsubject: " nil t)
185 (nnheader-header-value) "(none)"))
189 (if (search-forward "\nfrom: " nil t)
190 (nnheader-header-value) "(nobody)"))
194 (if (search-forward "\ndate: " nil t)
195 (nnheader-header-value) ""))
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.
205 (incf nnheader-newsgroup-none-id)))))
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
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)
225 (if (search-forward "\nlines: " nil t)
226 (if (numberp (setq lines (read cur)))
232 (and (search-forward "\nxref: " nil t)
233 (nnheader-header-value)))))
235 (goto-char (point-min))
238 (defun nnheader-insert-nov (header)
239 (princ (mail-header-number header) (current-buffer))
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))
250 (princ (or (mail-header-lines header) 0) (current-buffer))
252 (when (mail-header-xref header)
253 (insert "Xref: " (mail-header-xref header) "\t"))
256 (defun nnheader-insert-article-line (article)
257 (goto-char (point-min))
259 (princ article (current-buffer))
260 (insert " Article retrieved.\n")
261 (search-forward "\n\n" nil 'move)
262 (delete-region (point) (point-max))
266 ;; Various cruft the backends and Gnus need to communicate.
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)
276 (defvar nnheader-callback-function nil)
278 (defun nnheader-init-server-buffer ()
279 "Initialize the Gnus-backend communication buffer."
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))
286 (kill-all-local-variables)
287 (setq case-fold-search t) ;Should ignore case.
290 ;;; Various functions the backends use.
292 (defun nnheader-file-error (file)
293 "Return a string that says what is wrong with FILE."
296 ((not (file-exists-p file))
298 ((file-directory-p file)
300 ((not (file-readable-p file))
301 "%s is not readable"))
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.
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))))))
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))
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)))
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)))
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))))))
351 (defun nnheader-replace-header (header new-value)
352 "Remove HEADER and insert the NEW-VALUE."
355 (nnheader-narrow-to-headers)
357 (message-remove-header header)
358 (goto-char (point-max))
359 (insert header ": " new-value "\n")))))
361 (defun nnheader-narrow-to-headers ()
362 "Narrow to the head of an article."
365 (goto-char (point-min))
366 (if (search-forward "\n\n" nil t)
369 (goto-char (point-min)))
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))
379 (defmacro nnheader-temp-write (file &rest forms)
380 "Create a new buffer, evaluate FORM there, and write the buffer to FILE."
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))
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))))))
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))
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 "\\|")
414 "Regexp that match numerical files.")
416 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
417 "Regexp that matches numerical file names.")
419 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
420 "Regexp that matches numerical full file paths.")
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))
426 (string-match nnheader-numerical-short-files file)
427 (string-to-int (match-string 0 file))))
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))
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)))
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)))
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)))
458 (defun nnheader-translate-file-chars (file)
459 (if (null nnheader-file-name-translation-alist)
460 ;; No translation is necessary.
462 ;; We translate -- but only the file name. We leave the directory
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))
475 (when (setq trans (cdr (assq (aref leaf i)
476 nnheader-file-name-translation-alist)))
479 (concat path leaf))))
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)
487 (apply 'format args)))
490 (defun nnheader-get-report (backend)
491 "Get the most recent report from BACKEND."
493 (message "%s" (symbol-value (intern (format "%s-status-string"
495 (error (message ""))))
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
502 (set-buffer nntp-server-buffer)
504 (if (string-match "%" format)
505 (insert (apply 'format format args))
506 (apply 'insert format args))
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))
515 (nnheader-set-temp-buffer " *mail-file-mbox-p*")
516 (nnheader-insert-file-contents-literally file)
517 (goto-char (point-min))
519 (looking-at message-unix-mail-delimiter)
520 (kill-buffer (current-buffer))))))
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))
527 ;; Replace all occurrences of FROM with TO.
529 (if (= (aref string idx) from)
530 (aset string idx to))
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
540 (substring (expand-file-name file)
543 (file-name-as-directory top))))
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)))
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)))
559 (defun nnheader-group-pathname (group dir &optional file)
560 "Make pathname for GROUP."
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))
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))))
577 (defun nnheader-concat (dir file)
578 "Concat DIR as directory to FILE."
579 (concat (file-name-as-directory dir) file))
581 (defun nnheader-ms-strip-cr ()
582 "Strip ^M from the end of all lines."
584 (goto-char (point-min))
585 (while (re-search-forward "\r$" nil t)
586 (delete-backward-char 1))))
588 (defun nnheader-file-size (file)
589 "Return the file size of FILE or 0."
590 (or (nth 7 (file-attributes file)) 0))
592 (defun nnheader-find-etc-directory (package)
593 "Go through the path and find the \".../etc/PACKAGE\" directory."
594 (let ((path load-path)
596 ;; We try to find the dir by looking at the load path,
597 ;; stripping away the last component and adding "etc/".
603 (directory-file-name (car path)))
604 "etc/" package "/")))
605 (file-directory-p dir))
608 (setq path (cdr path))))
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)))))
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)
629 (when (string-match "XEmacs\\|Lucid" emacs-version)
630 (require 'nnheaderxm))
632 (run-hooks 'nnheader-load-hook)
636 ;;; nnheader.el ends here