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 (eval-when-compile (require 'cl))
44 (defvar nnheader-max-head-length 4096
45 "*Max length of the head of articles.")
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:
52 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
54 ;;; Header access macros.
56 (defmacro mail-header-number (header)
57 "Return article number in HEADER."
60 (defmacro mail-header-set-number (header number)
61 "Set article number of HEADER to NUMBER."
62 `(aset ,header 0 ,number))
64 (defmacro mail-header-subject (header)
65 "Return subject string in HEADER."
68 (defmacro mail-header-set-subject (header subject)
69 "Set article subject of HEADER to SUBJECT."
70 `(aset ,header 1 ,subject))
72 (defmacro mail-header-from (header)
73 "Return author string in HEADER."
76 (defmacro mail-header-set-from (header from)
77 "Set article author of HEADER to FROM."
78 `(aset ,header 2 ,from))
80 (defmacro mail-header-date (header)
81 "Return date in HEADER."
84 (defmacro mail-header-set-date (header date)
85 "Set article date of HEADER to DATE."
86 `(aset ,header 3 ,date))
88 (defalias 'mail-header-message-id 'mail-header-id)
89 (defmacro mail-header-id (header)
90 "Return Id in HEADER."
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))
98 (defmacro mail-header-references (header)
99 "Return references in HEADER."
102 (defmacro mail-header-set-references (header ref)
103 "Set article references of HEADER to REF."
104 `(aset ,header 5 ,ref))
106 (defmacro mail-header-chars (header)
107 "Return number of chars of article in HEADER."
110 (defmacro mail-header-set-chars (header chars)
111 "Set number of chars in article of HEADER to CHARS."
112 `(aset ,header 6 ,chars))
114 (defmacro mail-header-lines (header)
115 "Return lines in HEADER."
118 (defmacro mail-header-set-lines (header lines)
119 "Set article lines of HEADER to LINES."
120 `(aset ,header 7 ,lines))
122 (defmacro mail-header-xref (header)
123 "Return xref string in HEADER."
126 (defmacro mail-header-set-xref (header xref)
127 "Set article xref of HEADER to xref."
128 `(aset ,header 8 ,xref))
130 (defun make-mail-header (&optional init)
131 "Create a new mail header structure initialized with INIT."
132 (make-vector 9 init))
134 ;; Parsing headers and NOV lines.
136 (defsubst nnheader-header-value ()
137 (buffer-substring (match-end 0) (gnus-point-at-eol)))
139 (defvar nnheader-newsgroup-none-id 1)
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))
149 ;; Search to the beginning of the next header. Error messages
150 ;; do not begin with 2 or 3.
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.
169 (narrow-to-region (point)
170 (or (and (search-forward "\n.\n" nil t)
176 (if (search-forward "\nsubject: " nil t)
177 (nnheader-header-value) "(none)"))
181 (if (search-forward "\nfrom: " nil t)
182 (nnheader-header-value) "(nobody)"))
186 (if (search-forward "\ndate: " nil t)
187 (nnheader-header-value) ""))
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.
197 (incf nnheader-newsgroup-none-id)))))
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
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)
217 (if (search-forward "\nlines: " nil t)
218 (if (numberp (setq lines (read cur)))
224 (and (search-forward "\nxref: " nil t)
225 (nnheader-header-value)))))
227 (goto-char (point-min))
230 (defun nnheader-insert-nov (header)
231 (princ (mail-header-number header) (current-buffer))
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))
241 (princ (or (mail-header-lines header) 0) (current-buffer))
243 (when (mail-header-xref header)
244 (insert "Xref: " (mail-header-xref header) "\t"))
247 (defun nnheader-insert-article-line (article)
248 (goto-char (point-min))
250 (princ article (current-buffer))
251 (insert " Article retrieved.\n")
252 (search-forward "\n\n" nil 'move)
253 (delete-region (point) (point-max))
257 ;; Various cruft the backends and Gnus need to communicate.
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)
267 (defvar nnheader-callback-function nil)
269 (defun nnheader-init-server-buffer ()
270 "Initialize the Gnus-backend communication buffer."
272 (setq nntp-server-buffer (get-buffer-create " *nntpd*"))
273 (set-buffer nntp-server-buffer)
274 (buffer-disable-undo (current-buffer))
276 (kill-all-local-variables)
277 (setq case-fold-search t) ;Should ignore case.
281 ;;; Various functions the backends use.
283 (defun nnheader-file-error (file)
284 "Return a string that says what is wrong with FILE."
287 ((not (file-exists-p file))
289 ((file-directory-p file)
291 ((not (file-readable-p file))
292 "%s is not readable"))
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.
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))))))
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))
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)))
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)))
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))))))
342 (defun nnheader-replace-header (header new-value)
343 "Remove HEADER and insert the NEW-VALUE."
346 (nnheader-narrow-to-headers)
348 (message-remove-header header)
349 (goto-char (point-max))
350 (insert header ": " new-value "\n")))))
352 (defun nnheader-narrow-to-headers ()
353 "Narrow to the head of an article."
356 (goto-char (point-min))
357 (if (search-forward "\n\n" nil t)
360 (goto-char (point-min)))
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))
369 (defmacro nnheader-temp-write (file &rest forms)
370 "Create a new buffer, evaluate FORM there, and write the buffer to FILE."
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))
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))))))
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))
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 "\\|")
403 "Regexp that match numerical files.")
405 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
406 "Regexp that matches numerical file names.")
408 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
409 "Regexp that matches numerical full file paths.")
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))
415 (string-match nnheader-numerical-short-files file)
416 (string-to-int (match-string 0 file))))
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)))
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)))
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)))
434 (defun nnheader-translate-file-chars (file)
435 (if (null nnheader-file-name-translation-alist)
436 ;; No translation is necessary.
438 ;; We translate -- but only the file name. We leave the directory
440 (let* ((new (file-name-nondirectory file))
445 (when (setq trans (cdr (assq (aref new i)
446 nnheader-file-name-translation-alist)))
449 (concat (file-name-directory file) new))))
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)
457 (apply 'format args)))
460 (defun nnheader-get-report (backend)
461 (message "%s" (symbol-value (intern (format "%s-status-string" backend)))))
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
468 (set-buffer nntp-server-buffer)
470 (if (string-match "%" format)
471 (insert (apply 'format format args))
472 (apply 'insert format args))
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))
481 (nnheader-set-temp-buffer " *mail-file-mbox-p*")
482 (insert-file-contents-literally file)
483 (goto-char (point-min))
485 (looking-at message-unix-mail-delimiter)
486 (kill-buffer (current-buffer))))))
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))
493 ;; Replace all occurrences of FROM with TO.
495 (if (= (aref string idx) from)
496 (aset string idx to))
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
506 (substring (expand-file-name file)
509 (file-name-as-directory top))))
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)))
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)))
525 (defun nnheader-group-pathname (group dir &optional file)
526 "Make pathname for GROUP."
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))
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))))
543 (defun nnheader-concat (dir file)
544 "Concat DIR as directory to FILE."
545 (concat (file-name-as-directory dir) file))
547 (require 'nnheader-es)
549 (run-hooks 'nnheader-load-hook)
553 ;;; nnheader.el ends here