nnheader.el 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. ;;; nnheader.el --- header access macros for Gnus and its backends
  2. ;; Copyright (C) 1987-1990, 1993-1998, 2000-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
  5. ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
  6. ;; Keywords: news
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; Code:
  20. ;; For Emacs <22.2 and XEmacs.
  21. (eval-and-compile
  22. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  23. (eval-when-compile (require 'cl))
  24. (defvar nnmail-extra-headers)
  25. (defvar gnus-newsgroup-name)
  26. (defvar nnheader-file-coding-system)
  27. (defvar jka-compr-compression-info-list)
  28. ;; Requiring `gnus-util' at compile time creates a circular
  29. ;; dependency between nnheader.el and gnus-util.el.
  30. ;;(eval-when-compile (require 'gnus-util))
  31. (require 'mail-utils)
  32. (require 'mm-util)
  33. (require 'gnus-util)
  34. (autoload 'gnus-range-add "gnus-range")
  35. (autoload 'gnus-remove-from-range "gnus-range")
  36. ;; FIXME none of these are used explicitly in this file.
  37. (autoload 'gnus-sorted-intersection "gnus-range")
  38. (autoload 'gnus-intersection "gnus-range")
  39. (autoload 'gnus-sorted-complement "gnus-range")
  40. (autoload 'gnus-sorted-difference "gnus-range")
  41. (defcustom gnus-verbose-backends 7
  42. "Integer that says how verbose the Gnus backends should be.
  43. The higher the number, the more messages the Gnus backends will flash
  44. to say what it's doing. At zero, the Gnus backends will be totally
  45. mute; at five, they will display most important messages; and at ten,
  46. they will keep on jabbering all the time."
  47. :group 'gnus-start
  48. :type 'integer)
  49. (defcustom gnus-nov-is-evil nil
  50. "If non-nil, Gnus backends will never output headers in the NOV format."
  51. :group 'gnus-server
  52. :type 'boolean)
  53. (defvar nnheader-max-head-length 8192
  54. "*Max length of the head of articles.
  55. Value is an integer, nil, or t. nil means read in chunks of a file
  56. indefinitely until a complete head is found\; t means always read the
  57. entire file immediately, disregarding `nnheader-head-chop-length'.
  58. Integer values will in effect be rounded up to the nearest multiple of
  59. `nnheader-head-chop-length'.")
  60. (defvar nnheader-head-chop-length 2048
  61. "*Length of each read operation when trying to fetch HEAD headers.")
  62. (defvar nnheader-read-timeout
  63. (if (string-match "windows-nt\\|os/2\\|cygwin"
  64. (symbol-name system-type))
  65. ;; http://thread.gmane.org/v9655t3pjo.fsf@marauder.physik.uni-ulm.de
  66. ;;
  67. ;; IIRC, values lower than 1.0 didn't/don't work on Windows/DOS.
  68. ;;
  69. ;; There should probably be a runtime test to determine the timing
  70. ;; resolution, or a primitive to report it. I don't know off-hand
  71. ;; what's possible. Perhaps better, maybe the Windows/DOS primitive
  72. ;; could round up non-zero timeouts to a minimum of 1.0?
  73. 1.0
  74. ;; 2008-05-19 change by Larsi:
  75. ;; Change the default timeout from 0.1 seconds to 0.01 seconds. This will
  76. ;; make nntp and pop3 article retrieval faster in some cases, but might
  77. ;; make CPU usage larger. If this has any bad side effects, we might
  78. ;; revert this change.
  79. 0.01)
  80. ;; When changing this variable, consider changing `pop3-read-timeout' as
  81. ;; well.
  82. "How long nntp should wait between checking for the end of output.
  83. Shorter values mean quicker response, but are more CPU intensive.")
  84. (defvar nnheader-file-name-translation-alist
  85. (let ((case-fold-search t))
  86. (cond
  87. ((string-match "windows-nt\\|os/2\\|cygwin"
  88. (symbol-name system-type))
  89. (append (mapcar (lambda (c) (cons c ?_))
  90. '(?: ?* ?\" ?< ?> ??))
  91. (if (string-match "windows-nt\\|cygwin"
  92. (symbol-name system-type))
  93. nil
  94. '((?+ . ?-)))))
  95. (t nil)))
  96. "*Alist that says how to translate characters in file names.
  97. For instance, if \":\" is invalid as a file character in file names
  98. on your system, you could say something like:
  99. \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
  100. (defvar nnheader-directory-separator-character
  101. (string-to-char (substring (file-name-as-directory ".") -1))
  102. "*A character used to a directory separator.")
  103. (autoload 'nnmail-message-id "nnmail")
  104. (autoload 'mail-position-on-field "sendmail")
  105. (autoload 'gnus-buffer-live-p "gnus-util")
  106. ;;; Header access macros.
  107. ;; These macros may look very much like the ones in GNUS 4.1. They
  108. ;; are, in a way, but you should note that the indices they use have
  109. ;; been changed from the internal GNUS format to the NOV format. The
  110. ;; makes it possible to read headers from XOVER much faster.
  111. ;;
  112. ;; The format of a header is now:
  113. ;; [number subject from date id references chars lines xref extra]
  114. ;;
  115. ;; (That next-to-last entry is defined as "misc" in the NOV format,
  116. ;; but Gnus uses it for xrefs.)
  117. (defmacro mail-header-number (header)
  118. "Return article number in HEADER."
  119. `(aref ,header 0))
  120. (defmacro mail-header-set-number (header number)
  121. "Set article number of HEADER to NUMBER."
  122. `(aset ,header 0 ,number))
  123. (defmacro mail-header-subject (header)
  124. "Return subject string in HEADER."
  125. `(aref ,header 1))
  126. (defmacro mail-header-set-subject (header subject)
  127. "Set article subject of HEADER to SUBJECT."
  128. `(aset ,header 1 ,subject))
  129. (defmacro mail-header-from (header)
  130. "Return author string in HEADER."
  131. `(aref ,header 2))
  132. (defmacro mail-header-set-from (header from)
  133. "Set article author of HEADER to FROM."
  134. `(aset ,header 2 ,from))
  135. (defmacro mail-header-date (header)
  136. "Return date in HEADER."
  137. `(aref ,header 3))
  138. (defmacro mail-header-set-date (header date)
  139. "Set article date of HEADER to DATE."
  140. `(aset ,header 3 ,date))
  141. (defalias 'mail-header-message-id 'mail-header-id)
  142. (defmacro mail-header-id (header)
  143. "Return Id in HEADER."
  144. `(aref ,header 4))
  145. (defalias 'mail-header-set-message-id 'mail-header-set-id)
  146. (defmacro mail-header-set-id (header id)
  147. "Set article Id of HEADER to ID."
  148. `(aset ,header 4 ,id))
  149. (defmacro mail-header-references (header)
  150. "Return references in HEADER."
  151. `(aref ,header 5))
  152. (defmacro mail-header-set-references (header ref)
  153. "Set article references of HEADER to REF."
  154. `(aset ,header 5 ,ref))
  155. (defmacro mail-header-chars (header)
  156. "Return number of chars of article in HEADER."
  157. `(aref ,header 6))
  158. (defmacro mail-header-set-chars (header chars)
  159. "Set number of chars in article of HEADER to CHARS."
  160. `(aset ,header 6 ,chars))
  161. (defmacro mail-header-lines (header)
  162. "Return lines in HEADER."
  163. `(aref ,header 7))
  164. (defmacro mail-header-set-lines (header lines)
  165. "Set article lines of HEADER to LINES."
  166. `(aset ,header 7 ,lines))
  167. (defmacro mail-header-xref (header)
  168. "Return xref string in HEADER."
  169. `(aref ,header 8))
  170. (defmacro mail-header-set-xref (header xref)
  171. "Set article XREF of HEADER to xref."
  172. `(aset ,header 8 ,xref))
  173. (defmacro mail-header-extra (header)
  174. "Return the extra headers in HEADER."
  175. `(aref ,header 9))
  176. (defun mail-header-set-extra (header extra)
  177. "Set the extra headers in HEADER to EXTRA."
  178. (aset header 9 extra))
  179. (defsubst make-mail-header (&optional init)
  180. "Create a new mail header structure initialized with INIT."
  181. (make-vector 10 init))
  182. (defsubst make-full-mail-header (&optional number subject from date id
  183. references chars lines xref
  184. extra)
  185. "Create a new mail header structure initialized with the parameters given."
  186. (vector number subject from date id references chars lines xref extra))
  187. ;; fake message-ids: generation and detection
  188. (defvar nnheader-fake-message-id 1)
  189. (defsubst nnheader-generate-fake-message-id (&optional number)
  190. (if (numberp number)
  191. (format "fake+none+%s+%d" gnus-newsgroup-name number)
  192. (format "fake+none+%s+%s"
  193. gnus-newsgroup-name
  194. (int-to-string (incf nnheader-fake-message-id)))))
  195. (defsubst nnheader-fake-message-id-p (id)
  196. (save-match-data ; regular message-id's are <.*>
  197. (string-match "\\`fake\\+none\\+.*\\+[0-9]+\\'" id)))
  198. ;; Parsing headers and NOV lines.
  199. (defsubst nnheader-remove-cr-followed-by-lf ()
  200. (goto-char (point-max))
  201. (while (search-backward "\r\n" nil t)
  202. (delete-char 1)))
  203. (defsubst nnheader-header-value ()
  204. (skip-chars-forward " \t")
  205. (buffer-substring (point) (point-at-eol)))
  206. (autoload 'ietf-drums-unfold-fws "ietf-drums")
  207. (defun nnheader-parse-naked-head (&optional number)
  208. ;; This function unfolds continuation lines in this buffer
  209. ;; destructively. When this side effect is unwanted, use
  210. ;; `nnheader-parse-head' instead of this function.
  211. (let ((case-fold-search t)
  212. (buffer-read-only nil)
  213. (cur (current-buffer))
  214. (p (point-min))
  215. in-reply-to lines ref)
  216. (nnheader-remove-cr-followed-by-lf)
  217. (ietf-drums-unfold-fws)
  218. (subst-char-in-region (point-min) (point-max) ?\t ? )
  219. (goto-char p)
  220. (insert "\n")
  221. (prog1
  222. ;; This implementation of this function, with nine
  223. ;; search-forwards instead of the one re-search-forward and a
  224. ;; case (which basically was the old function) is actually
  225. ;; about twice as fast, even though it looks messier. You
  226. ;; can't have everything, I guess. Speed and elegance don't
  227. ;; always go hand in hand.
  228. (vector
  229. ;; Number.
  230. (or number 0)
  231. ;; Subject.
  232. (progn
  233. (goto-char p)
  234. (if (search-forward "\nsubject:" nil t)
  235. (nnheader-header-value) "(none)"))
  236. ;; From.
  237. (progn
  238. (goto-char p)
  239. (if (search-forward "\nfrom:" nil t)
  240. (nnheader-header-value) "(nobody)"))
  241. ;; Date.
  242. (progn
  243. (goto-char p)
  244. (if (search-forward "\ndate:" nil t)
  245. (nnheader-header-value) ""))
  246. ;; Message-ID.
  247. (progn
  248. (goto-char p)
  249. (if (search-forward "\nmessage-id:" nil t)
  250. (buffer-substring
  251. (1- (or (search-forward "<" (point-at-eol) t)
  252. (point)))
  253. (or (search-forward ">" (point-at-eol) t) (point)))
  254. ;; If there was no message-id, we just fake one to make
  255. ;; subsequent routines simpler.
  256. (nnheader-generate-fake-message-id number)))
  257. ;; References.
  258. (progn
  259. (goto-char p)
  260. (if (search-forward "\nreferences:" nil t)
  261. (nnheader-header-value)
  262. ;; Get the references from the in-reply-to header if
  263. ;; there were no references and the in-reply-to header
  264. ;; looks promising.
  265. (if (and (search-forward "\nin-reply-to:" nil t)
  266. (setq in-reply-to (nnheader-header-value))
  267. (string-match "<[^\n>]+>" in-reply-to))
  268. (let (ref2)
  269. (setq ref (substring in-reply-to (match-beginning 0)
  270. (match-end 0)))
  271. (while (string-match "<[^\n>]+>"
  272. in-reply-to (match-end 0))
  273. (setq ref2 (substring in-reply-to (match-beginning 0)
  274. (match-end 0)))
  275. (when (> (length ref2) (length ref))
  276. (setq ref ref2)))
  277. ref)
  278. nil)))
  279. ;; Chars.
  280. 0
  281. ;; Lines.
  282. (progn
  283. (goto-char p)
  284. (if (search-forward "\nlines: " nil t)
  285. (if (numberp (setq lines (read cur)))
  286. lines 0)
  287. 0))
  288. ;; Xref.
  289. (progn
  290. (goto-char p)
  291. (and (search-forward "\nxref:" nil t)
  292. (nnheader-header-value)))
  293. ;; Extra.
  294. (when nnmail-extra-headers
  295. (let ((extra nnmail-extra-headers)
  296. out)
  297. (while extra
  298. (goto-char p)
  299. (when (search-forward
  300. (concat "\n" (symbol-name (car extra)) ":") nil t)
  301. (push (cons (car extra) (nnheader-header-value))
  302. out))
  303. (pop extra))
  304. out)))
  305. (goto-char p)
  306. (delete-char 1))))
  307. (defun nnheader-parse-head (&optional naked)
  308. (let ((cur (current-buffer)) num beg end)
  309. (when (if naked
  310. (setq num 0
  311. beg (point-min)
  312. end (point-max))
  313. ;; Search to the beginning of the next header. Error
  314. ;; messages do not begin with 2 or 3.
  315. (when (re-search-forward "^[23][0-9]+ " nil t)
  316. (setq num (read cur)
  317. beg (point)
  318. end (if (search-forward "\n.\n" nil t)
  319. (goto-char (- (point) 2))
  320. (point)))))
  321. (with-temp-buffer
  322. (insert-buffer-substring cur beg end)
  323. (nnheader-parse-naked-head num)))))
  324. (defmacro nnheader-nov-skip-field ()
  325. '(search-forward "\t" eol 'move))
  326. (defmacro nnheader-nov-field ()
  327. '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
  328. (defmacro nnheader-nov-read-integer ()
  329. '(prog1
  330. (if (eq (char-after) ?\t)
  331. 0
  332. (let ((num (condition-case nil
  333. (read (current-buffer))
  334. (error nil))))
  335. (if (numberp num) num 0)))
  336. (or (eobp) (forward-char 1))))
  337. (defmacro nnheader-nov-parse-extra ()
  338. '(let (out string)
  339. (while (not (memq (char-after) '(?\n nil)))
  340. (setq string (nnheader-nov-field))
  341. (when (string-match "^\\([^ :]+\\): " string)
  342. (push (cons (intern (match-string 1 string))
  343. (substring string (match-end 0)))
  344. out)))
  345. out))
  346. (eval-and-compile
  347. (defvar nnheader-uniquify-message-id nil))
  348. (defmacro nnheader-nov-read-message-id (&optional number)
  349. `(let ((id (nnheader-nov-field)))
  350. (if (string-match "^<[^>]+>$" id)
  351. ,(if nnheader-uniquify-message-id
  352. `(if (string-match "__[^@]+@" id)
  353. (concat (substring id 0 (match-beginning 0))
  354. (substring id (1- (match-end 0))))
  355. id)
  356. 'id)
  357. (nnheader-generate-fake-message-id ,number))))
  358. (defun nnheader-parse-nov ()
  359. (let ((eol (point-at-eol))
  360. (number (nnheader-nov-read-integer)))
  361. (vector
  362. number ; number
  363. (nnheader-nov-field) ; subject
  364. (nnheader-nov-field) ; from
  365. (nnheader-nov-field) ; date
  366. (nnheader-nov-read-message-id number) ; id
  367. (nnheader-nov-field) ; refs
  368. (nnheader-nov-read-integer) ; chars
  369. (nnheader-nov-read-integer) ; lines
  370. (if (eq (char-after) ?\n)
  371. nil
  372. (if (looking-at "Xref: ")
  373. (goto-char (match-end 0)))
  374. (nnheader-nov-field)) ; Xref
  375. (nnheader-nov-parse-extra)))) ; extra
  376. (defun nnheader-insert-nov (header)
  377. (princ (mail-header-number header) (current-buffer))
  378. (let ((p (point)))
  379. (insert
  380. "\t"
  381. (or (mail-header-subject header) "(none)") "\t"
  382. (or (mail-header-from header) "(nobody)") "\t"
  383. (or (mail-header-date header) "") "\t"
  384. (or (mail-header-id header)
  385. (nnmail-message-id))
  386. "\t"
  387. (or (mail-header-references header) "") "\t")
  388. (princ (or (mail-header-chars header) 0) (current-buffer))
  389. (insert "\t")
  390. (princ (or (mail-header-lines header) 0) (current-buffer))
  391. (insert "\t")
  392. (when (mail-header-xref header)
  393. (insert "Xref: " (mail-header-xref header)))
  394. (when (or (mail-header-xref header)
  395. (mail-header-extra header))
  396. (insert "\t"))
  397. (when (mail-header-extra header)
  398. (let ((extra (mail-header-extra header)))
  399. (while extra
  400. (insert (symbol-name (caar extra))
  401. ": " (if (stringp (cdar extra)) (cdar extra) "") "\t")
  402. (pop extra))))
  403. (insert "\n")
  404. (backward-char 1)
  405. (while (search-backward "\n" p t)
  406. (delete-char 1))
  407. (forward-line 1)))
  408. (defun nnheader-parse-overview-file (file)
  409. "Parse FILE and return a list of headers."
  410. (mm-with-unibyte-buffer
  411. (nnheader-insert-file-contents file)
  412. (goto-char (point-min))
  413. (let (headers)
  414. (while (not (eobp))
  415. (push (nnheader-parse-nov) headers)
  416. (forward-line 1))
  417. (nreverse headers))))
  418. (defun nnheader-write-overview-file (file headers)
  419. "Write HEADERS to FILE."
  420. (with-temp-file file
  421. (mapcar 'nnheader-insert-nov headers)))
  422. (defun nnheader-insert-header (header)
  423. (insert
  424. "Subject: " (or (mail-header-subject header) "(none)") "\n"
  425. "From: " (or (mail-header-from header) "(nobody)") "\n"
  426. "Date: " (or (mail-header-date header) "") "\n"
  427. "Message-ID: " (or (mail-header-id header) (nnmail-message-id)) "\n"
  428. "References: " (or (mail-header-references header) "") "\n"
  429. "Lines: ")
  430. (princ (or (mail-header-lines header) 0) (current-buffer))
  431. (insert "\n\n"))
  432. (defun nnheader-insert-article-line (article)
  433. (goto-char (point-min))
  434. (insert "220 ")
  435. (princ article (current-buffer))
  436. (insert " Article retrieved.\n")
  437. (search-forward "\n\n" nil 'move)
  438. (delete-region (point) (point-max))
  439. (forward-char -1)
  440. (insert "."))
  441. (defun nnheader-nov-delete-outside-range (beg end)
  442. "Delete all NOV lines that lie outside the BEG to END range."
  443. ;; First we find the first wanted line.
  444. (nnheader-find-nov-line beg)
  445. (delete-region (point-min) (point))
  446. ;; Then we find the last wanted line.
  447. (when (nnheader-find-nov-line end)
  448. (forward-line 1))
  449. (delete-region (point) (point-max)))
  450. (defun nnheader-find-nov-line (article)
  451. "Put point at the NOV line that start with ARTICLE.
  452. If ARTICLE doesn't exist, put point where that line
  453. would have been. The function will return non-nil if
  454. the line could be found."
  455. ;; This function basically does a binary search.
  456. (let ((max (point-max))
  457. (min (goto-char (point-min)))
  458. (cur (current-buffer))
  459. (prev (point-min))
  460. num found)
  461. (while (not found)
  462. (goto-char (+ min (/ (- max min) 2)))
  463. (beginning-of-line)
  464. (if (or (= (point) prev)
  465. (eobp))
  466. (setq found t)
  467. (setq prev (point))
  468. (while (and (not (numberp (setq num (read cur))))
  469. (not (eobp)))
  470. (gnus-delete-line))
  471. (cond ((> num article)
  472. (setq max (point)))
  473. ((< num article)
  474. (setq min (point)))
  475. (t
  476. (setq found 'yes)))))
  477. ;; We may be at the first line.
  478. (when (and (not num)
  479. (not (eobp)))
  480. (setq num (read cur)))
  481. ;; Now we may have found the article we're looking for, or we
  482. ;; may be somewhere near it.
  483. (when (and (not (eq found 'yes))
  484. (not (eq num article)))
  485. (setq found (point))
  486. (while (and (< (point) max)
  487. (or (not (numberp num))
  488. (< num article)))
  489. (forward-line 1)
  490. (setq found (point))
  491. (or (eobp)
  492. (= (setq num (read cur)) article)))
  493. (unless (eq num article)
  494. (goto-char found)))
  495. (beginning-of-line)
  496. (eq num article)))
  497. ;; Various cruft the backends and Gnus need to communicate.
  498. (defvar nntp-server-buffer nil)
  499. (defvar nntp-process-response nil)
  500. (defvar nnheader-callback-function nil)
  501. (defun nnheader-init-server-buffer ()
  502. "Initialize the Gnus-backend communication buffer."
  503. (unless (gnus-buffer-live-p nntp-server-buffer)
  504. (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
  505. (with-current-buffer nntp-server-buffer
  506. (erase-buffer)
  507. (mm-enable-multibyte)
  508. (kill-all-local-variables)
  509. (setq case-fold-search t) ;Should ignore case.
  510. (set (make-local-variable 'nntp-process-response) nil)
  511. t))
  512. ;;; Various functions the backends use.
  513. (defun nnheader-file-error (file)
  514. "Return a string that says what is wrong with FILE."
  515. (format
  516. (cond
  517. ((not (file-exists-p file))
  518. "%s does not exist")
  519. ((file-directory-p file)
  520. "%s is a directory")
  521. ((not (file-readable-p file))
  522. "%s is not readable"))
  523. file))
  524. (defun nnheader-insert-head (file)
  525. "Insert the head of the article."
  526. (when (file-exists-p file)
  527. (if (eq nnheader-max-head-length t)
  528. ;; Just read the entire file.
  529. (nnheader-insert-file-contents file)
  530. ;; Read blocks of the size specified by `nnheader-head-chop-length'
  531. ;; until we find a separator.
  532. (let ((beg 0)
  533. (start (point))
  534. ;; Use `binary' to prevent the contents from being decoded,
  535. ;; or it will change the number of characters that
  536. ;; `insert-file-contents' returns.
  537. (coding-system-for-read 'binary))
  538. (while (and (eq nnheader-head-chop-length
  539. (nth 1 (mm-insert-file-contents
  540. file nil beg
  541. (incf beg nnheader-head-chop-length))))
  542. ;; CRLF or CR might be used for the line-break code.
  543. (prog1 (not (re-search-forward "\n\r?\n\\|\r\r" nil t))
  544. (goto-char (point-max)))
  545. (or (null nnheader-max-head-length)
  546. (< beg nnheader-max-head-length))))
  547. ;; Finally decode the contents.
  548. (when (mm-coding-system-p nnheader-file-coding-system)
  549. (mm-decode-coding-region start (point-max)
  550. nnheader-file-coding-system))))
  551. t))
  552. (defun nnheader-article-p ()
  553. "Say whether the current buffer looks like an article."
  554. (goto-char (point-min))
  555. (if (not (search-forward "\n\n" nil t))
  556. nil
  557. (narrow-to-region (point-min) (1- (point)))
  558. (goto-char (point-min))
  559. (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
  560. (goto-char (match-end 0)))
  561. (prog1
  562. (eobp)
  563. (widen))))
  564. (defun nnheader-insert-references (references message-id)
  565. "Insert a References header based on REFERENCES and MESSAGE-ID."
  566. (if (and (not references) (not message-id))
  567. ;; This is invalid, but not all articles have Message-IDs.
  568. ()
  569. (mail-position-on-field "References")
  570. (let ((begin (point-at-bol))
  571. (fill-column 78)
  572. (fill-prefix "\t"))
  573. (when references
  574. (insert references))
  575. (when (and references message-id)
  576. (insert " "))
  577. (when message-id
  578. (insert message-id))
  579. ;; Fold long References lines to conform to RFC1036 (sort of).
  580. ;; The region must end with a newline to fill the region
  581. ;; without inserting extra newline.
  582. (fill-region-as-paragraph begin (1+ (point))))))
  583. (declare-function message-remove-header "message"
  584. (header &optional is-regexp first reverse))
  585. (defun nnheader-replace-header (header new-value)
  586. "Remove HEADER and insert the NEW-VALUE."
  587. (require 'message)
  588. (save-excursion
  589. (save-restriction
  590. (nnheader-narrow-to-headers)
  591. (prog1
  592. (message-remove-header header)
  593. (goto-char (point-max))
  594. (insert header ": " new-value "\n")))))
  595. (defun nnheader-narrow-to-headers ()
  596. "Narrow to the head of an article."
  597. (widen)
  598. (narrow-to-region
  599. (goto-char (point-min))
  600. (if (search-forward "\n\n" nil t)
  601. (1- (point))
  602. (point-max)))
  603. (goto-char (point-min)))
  604. (defun nnheader-get-lines-and-char ()
  605. "Return the number of lines and chars in the article body."
  606. (goto-char (point-min))
  607. (if (not (re-search-forward "\n\r?\n" nil t))
  608. (list 0 0)
  609. (list (count-lines (point) (point-max))
  610. (- (point-max) (point)))))
  611. (defun nnheader-remove-body ()
  612. "Remove the body from an article in this current buffer."
  613. (goto-char (point-min))
  614. (when (re-search-forward "\n\r?\n" nil t)
  615. (delete-region (point) (point-max))))
  616. (defun nnheader-set-temp-buffer (name &optional noerase)
  617. "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
  618. (set-buffer (get-buffer-create name))
  619. (buffer-disable-undo)
  620. (unless noerase
  621. (erase-buffer))
  622. (current-buffer))
  623. (defvar nnheader-numerical-files
  624. (if (boundp 'jka-compr-compression-info-list)
  625. (concat "\\([0-9]+\\)\\("
  626. (mapconcat (lambda (i) (aref i 0))
  627. jka-compr-compression-info-list "\\|")
  628. "\\)?")
  629. "[0-9]+$")
  630. "Regexp that match numerical files.")
  631. (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
  632. "Regexp that matches numerical file names.")
  633. (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
  634. "Regexp that matches numerical full file names.")
  635. (defsubst nnheader-file-to-number (file)
  636. "Take a FILE name and return the article number."
  637. (if (string= nnheader-numerical-short-files "^[0-9]+$")
  638. (string-to-number file)
  639. (string-match nnheader-numerical-short-files file)
  640. (string-to-number (match-string 0 file))))
  641. (defvar nnheader-directory-files-is-safe
  642. (or (eq system-type 'windows-nt)
  643. (not (featurep 'xemacs)))
  644. "If non-nil, Gnus believes `directory-files' is safe.
  645. It has been reported numerous times that `directory-files' fails with
  646. an alarming frequency on NFS mounted file systems. If it is nil,
  647. `nnheader-directory-files-safe' is used.")
  648. (defun nnheader-directory-files-safe (&rest args)
  649. "Execute `directory-files' twice and returns the longer result."
  650. (let ((first (apply 'directory-files args))
  651. (second (apply 'directory-files args)))
  652. (if (> (length first) (length second))
  653. first
  654. second)))
  655. (defun nnheader-directory-articles (dir)
  656. "Return a list of all article files in directory DIR."
  657. (mapcar 'nnheader-file-to-number
  658. (if nnheader-directory-files-is-safe
  659. (directory-files
  660. dir nil nnheader-numerical-short-files t)
  661. (nnheader-directory-files-safe
  662. dir nil nnheader-numerical-short-files t))))
  663. (defun nnheader-article-to-file-alist (dir)
  664. "Return an alist of article/file pairs in DIR."
  665. (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
  666. (if nnheader-directory-files-is-safe
  667. (directory-files
  668. dir nil nnheader-numerical-short-files t)
  669. (nnheader-directory-files-safe
  670. dir nil nnheader-numerical-short-files t))))
  671. (defun nnheader-fold-continuation-lines ()
  672. "Fold continuation lines in the current buffer."
  673. (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
  674. (defun nnheader-translate-file-chars (file &optional full)
  675. "Translate FILE into something that can be a file name.
  676. If FULL, translate everything."
  677. (if (null nnheader-file-name-translation-alist)
  678. ;; No translation is necessary.
  679. file
  680. (let* ((i 0)
  681. trans leaf path len)
  682. (if full
  683. ;; Do complete translation.
  684. (setq leaf (copy-sequence file)
  685. path ""
  686. i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
  687. 2 0))
  688. ;; We translate -- but only the file name. We leave the directory
  689. ;; alone.
  690. (if (and (featurep 'xemacs)
  691. (memq system-type '(windows-nt cygwin)))
  692. ;; This is needed on NT and stuff, because
  693. ;; file-name-nondirectory is not enough to split
  694. ;; file names, containing ':', e.g.
  695. ;; "d:\\Work\\News\\nntp+news.fido7.ru:fido7.ru.gnu.SCORE"
  696. ;;
  697. ;; we are trying to correctly split such names:
  698. ;; "d:file.name" -> "a:" "file.name"
  699. ;; "aaa:bbb.ccc" -> "" "aaa:bbb.ccc"
  700. ;; "d:aaa\\bbb:ccc" -> "d:aaa\\" "bbb:ccc"
  701. ;; etc.
  702. ;; to translate then only the file name part.
  703. (progn
  704. (setq leaf file
  705. path "")
  706. (if (string-match "\\(^\\w:\\|[/\\]\\)\\([^/\\]+\\)$" file)
  707. (setq leaf (substring file (match-beginning 2))
  708. path (substring file 0 (match-beginning 2)))))
  709. ;; Emacs DTRT, says andrewi.
  710. (setq leaf (file-name-nondirectory file)
  711. path (file-name-directory file))))
  712. (setq len (length leaf))
  713. (while (< i len)
  714. (when (setq trans (cdr (assq (aref leaf i)
  715. nnheader-file-name-translation-alist)))
  716. (aset leaf i trans))
  717. (incf i))
  718. (concat path leaf))))
  719. (defun nnheader-report (backend &rest args)
  720. "Report an error from the BACKEND.
  721. The first string in ARGS can be a format string."
  722. (set (intern (format "%s-status-string" backend))
  723. (if (< (length args) 2)
  724. (car args)
  725. (apply 'format args)))
  726. nil)
  727. (defun nnheader-get-report-string (backend)
  728. "Get the most recent report from BACKEND."
  729. (condition-case ()
  730. (format "%s" (symbol-value (intern (format "%s-status-string"
  731. backend))))
  732. (error "")))
  733. (defun nnheader-get-report (backend)
  734. "Get the most recent report from BACKEND."
  735. (nnheader-message 5 (nnheader-get-report-string backend)))
  736. (defun nnheader-insert (format &rest args)
  737. "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
  738. If FORMAT isn't a format string, it and all ARGS will be inserted
  739. without formatting."
  740. (with-current-buffer nntp-server-buffer
  741. (erase-buffer)
  742. (if (string-match "%" format)
  743. (insert (apply 'format format args))
  744. (apply 'insert format args))
  745. t))
  746. (defsubst nnheader-replace-chars-in-string (string from to)
  747. (mm-subst-char-in-string from to string))
  748. (defun nnheader-replace-duplicate-chars-in-string (string from to)
  749. "Replace characters in STRING from FROM to TO."
  750. (let ((string (substring string 0)) ;Copy string.
  751. (len (length string))
  752. (idx 0) prev i)
  753. ;; Replace all occurrences of FROM with TO.
  754. (while (< idx len)
  755. (setq i (aref string idx))
  756. (when (and (eq prev from) (= i from))
  757. (aset string (1- idx) to)
  758. (aset string idx to))
  759. (setq prev i)
  760. (setq idx (1+ idx)))
  761. string))
  762. (defun nnheader-file-to-group (file &optional top)
  763. "Return a group name based on FILE and TOP."
  764. (nnheader-replace-chars-in-string
  765. (if (not top)
  766. file
  767. (condition-case ()
  768. (substring (expand-file-name file)
  769. (length
  770. (expand-file-name
  771. (file-name-as-directory top))))
  772. (error "")))
  773. nnheader-directory-separator-character ?.))
  774. (defun nnheader-message (level &rest args)
  775. "Message if the Gnus backends are talkative."
  776. (if (or (not (numberp gnus-verbose-backends))
  777. (<= level gnus-verbose-backends))
  778. (if gnus-add-timestamp-to-message
  779. (apply 'gnus-message-with-timestamp args)
  780. (apply 'message args))
  781. (apply 'format args)))
  782. (defun nnheader-be-verbose (level)
  783. "Return whether the backends should be verbose on LEVEL."
  784. (or (not (numberp gnus-verbose-backends))
  785. (<= level gnus-verbose-backends)))
  786. (defvar nnheader-pathname-coding-system 'iso-8859-1
  787. "*Coding system for file name.")
  788. (defun nnheader-group-pathname (group dir &optional file)
  789. "Make file name for GROUP."
  790. (concat
  791. (let ((dir (file-name-as-directory (expand-file-name dir))))
  792. ;; If this directory exists, we use it directly.
  793. (file-name-as-directory
  794. (if (file-directory-p (concat dir group))
  795. (expand-file-name group dir)
  796. ;; If not, we translate dots into slashes.
  797. (expand-file-name (mm-encode-coding-string
  798. (nnheader-replace-chars-in-string group ?. ?/)
  799. nnheader-pathname-coding-system)
  800. dir))))
  801. (cond ((null file) "")
  802. ((numberp file) (int-to-string file))
  803. (t file))))
  804. (defun nnheader-concat (dir &rest files)
  805. "Concat DIR as directory to FILES."
  806. (apply 'concat (file-name-as-directory dir) files))
  807. (defun nnheader-ms-strip-cr ()
  808. "Strip ^M from the end of all lines."
  809. (save-excursion
  810. (nnheader-remove-cr-followed-by-lf)))
  811. (defun nnheader-file-size (file)
  812. "Return the file size of FILE or 0."
  813. (or (nth 7 (file-attributes file)) 0))
  814. (defun nnheader-find-etc-directory (package &optional file first)
  815. "Go through `load-path' and find the \"../etc/PACKAGE\" directory.
  816. This function will look in the parent directory of each `load-path'
  817. entry, and look for the \"etc\" directory there.
  818. If FILE, find the \".../etc/PACKAGE\" file instead.
  819. If FIRST is non-nil, return the directory or the file found at the
  820. first. Otherwise, find the newest one, though it may take a time."
  821. (let ((path load-path)
  822. dir results)
  823. ;; We try to find the dir by looking at the load path,
  824. ;; stripping away the last component and adding "etc/".
  825. (while path
  826. (if (and (car path)
  827. (file-exists-p
  828. (setq dir (concat
  829. (file-name-directory
  830. (directory-file-name (car path)))
  831. "etc/" package
  832. (if file "" "/"))))
  833. (or file (file-directory-p dir)))
  834. (progn
  835. (or (member dir results)
  836. (push dir results))
  837. (setq path (if first nil (cdr path))))
  838. (setq path (cdr path))))
  839. (if (or first (not (cdr results)))
  840. (car results)
  841. (car (sort results 'file-newer-than-file-p)))))
  842. (defvar ange-ftp-path-format)
  843. (defvar efs-path-regexp)
  844. (defun nnheader-re-read-dir (path)
  845. "Re-read directory PATH if PATH is on a remote system."
  846. (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
  847. (when (string-match efs-path-regexp path)
  848. (efs-re-read-dir path))
  849. (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
  850. (when (string-match (car ange-ftp-path-format) path)
  851. (ange-ftp-re-read-dir path)))))
  852. (defvar nnheader-file-coding-system 'raw-text
  853. "Coding system used in file backends of Gnus.")
  854. (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
  855. "Like `insert-file-contents', q.v., but only reads in the file.
  856. A buffer may be modified in several ways after reading into the buffer due
  857. to advanced Emacs features, such as file-name-handlers, format decoding,
  858. find-file-hooks, etc.
  859. This function ensures that none of these modifications will take place."
  860. (let ((coding-system-for-read nnheader-file-coding-system))
  861. (mm-insert-file-contents filename visit beg end replace)))
  862. (defun nnheader-insert-nov-file (file first)
  863. (let ((size (nth 7 (file-attributes file)))
  864. (cutoff (* 32 1024)))
  865. (when size
  866. (if (< size cutoff)
  867. ;; If the file is small, we just load it.
  868. (nnheader-insert-file-contents file)
  869. ;; We start on the assumption that FIRST is pretty recent. If
  870. ;; not, we just insert the rest of the file as well.
  871. (let (current)
  872. (nnheader-insert-file-contents file nil (- size cutoff) size)
  873. (goto-char (point-min))
  874. (delete-region (point) (or (search-forward "\n" nil 'move) (point)))
  875. (setq current (ignore-errors (read (current-buffer))))
  876. (if (and (numberp current)
  877. (< current first))
  878. t
  879. (delete-region (point-min) (point-max))
  880. (nnheader-insert-file-contents file)))))))
  881. (defun nnheader-find-file-noselect (&rest args)
  882. "Open a file with some variables bound.
  883. See `find-file-noselect' for the arguments."
  884. (letf* ((format-alist nil)
  885. (auto-mode-alist (mm-auto-mode-alist))
  886. ((default-value 'major-mode) 'fundamental-mode)
  887. (enable-local-variables nil)
  888. (after-insert-file-functions nil)
  889. (enable-local-eval nil)
  890. (coding-system-for-read nnheader-file-coding-system)
  891. (version-control 'never)
  892. (ffh (if (boundp 'find-file-hook)
  893. 'find-file-hook
  894. 'find-file-hooks))
  895. (val (symbol-value ffh)))
  896. (set ffh nil)
  897. (unwind-protect
  898. (apply 'find-file-noselect args)
  899. (set ffh val))))
  900. (defun nnheader-directory-regular-files (dir)
  901. "Return a list of all regular files in DIR."
  902. (let ((files (directory-files dir t))
  903. out)
  904. (while files
  905. (when (file-regular-p (car files))
  906. (push (car files) out))
  907. (pop files))
  908. (nreverse out)))
  909. (defun nnheader-directory-files (&rest args)
  910. "Same as `directory-files', but prune \".\" and \"..\"."
  911. (let ((files (apply 'directory-files args))
  912. out)
  913. (while files
  914. (unless (member (file-name-nondirectory (car files)) '("." ".."))
  915. (push (car files) out))
  916. (pop files))
  917. (nreverse out)))
  918. (defmacro nnheader-skeleton-replace (from &optional to regexp)
  919. `(let ((new (generate-new-buffer " *nnheader replace*"))
  920. (cur (current-buffer))
  921. (start (point-min)))
  922. (set-buffer cur)
  923. (goto-char (point-min))
  924. (while (,(if regexp 're-search-forward 'search-forward)
  925. ,from nil t)
  926. (insert-buffer-substring
  927. cur start (prog1 (match-beginning 0) (set-buffer new)))
  928. (goto-char (point-max))
  929. ,(when to `(insert ,to))
  930. (set-buffer cur)
  931. (setq start (point)))
  932. (insert-buffer-substring
  933. cur start (prog1 (point-max) (set-buffer new)))
  934. (copy-to-buffer cur (point-min) (point-max))
  935. (kill-buffer (current-buffer))
  936. (set-buffer cur)))
  937. (defun nnheader-replace-string (from to)
  938. "Do a fast replacement of FROM to TO from point to `point-max'."
  939. (nnheader-skeleton-replace from to))
  940. (defun nnheader-replace-regexp (from to)
  941. "Do a fast regexp replacement of FROM to TO from point to `point-max'."
  942. (nnheader-skeleton-replace from to t))
  943. (defun nnheader-strip-cr ()
  944. "Strip all \r's from the current buffer."
  945. (nnheader-skeleton-replace "\r"))
  946. (defalias 'nnheader-cancel-timer 'cancel-timer)
  947. (defalias 'nnheader-cancel-function-timers 'cancel-function-timers)
  948. ;; When changing this function, consider changing `pop3-accept-process-output'
  949. ;; as well.
  950. (defun nnheader-accept-process-output (process)
  951. (accept-process-output
  952. process
  953. (truncate nnheader-read-timeout)
  954. (truncate (* (- nnheader-read-timeout
  955. (truncate nnheader-read-timeout))
  956. 1000))))
  957. (defun nnheader-update-marks-actions (backend-marks actions)
  958. (dolist (action actions)
  959. (let ((range (nth 0 action))
  960. (what (nth 1 action))
  961. (marks (nth 2 action)))
  962. (dolist (mark marks)
  963. (setq backend-marks
  964. (gnus-update-alist-soft
  965. mark
  966. (cond
  967. ((eq what 'add)
  968. (gnus-range-add (cdr (assoc mark backend-marks)) range))
  969. ((eq what 'del)
  970. (gnus-remove-from-range
  971. (cdr (assoc mark backend-marks)) range))
  972. ((eq what 'set)
  973. range))
  974. backend-marks)))))
  975. backend-marks)
  976. (defmacro nnheader-insert-buffer-substring (buffer &optional start end)
  977. "Copy string from unibyte buffer to multibyte current buffer."
  978. (if (featurep 'xemacs)
  979. `(insert-buffer-substring ,buffer ,start ,end)
  980. `(if enable-multibyte-characters
  981. (insert (with-current-buffer ,buffer
  982. (mm-string-to-multibyte
  983. ,(if (or start end)
  984. `(buffer-substring (or ,start (point-min))
  985. (or ,end (point-max)))
  986. '(buffer-string)))))
  987. (insert-buffer-substring ,buffer ,start ,end))))
  988. (defvar nnheader-last-message-time '(0 0))
  989. (defun nnheader-message-maybe (&rest args)
  990. (let ((now (current-time)))
  991. (when (> (float-time (time-subtract now nnheader-last-message-time)) 1)
  992. (setq nnheader-last-message-time now)
  993. (apply 'nnheader-message args))))
  994. (when (featurep 'xemacs)
  995. (require 'nnheaderxm))
  996. (run-hooks 'nnheader-load-hook)
  997. (provide 'nnheader)
  998. ;;; nnheader.el ends here