nntp.el 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  1. ;;; nntp.el --- nntp access for Gnus
  2. ;; Copyright (C) 1987-1990, 1992-1998, 2000-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  5. ;; Keywords: news
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. ;; For Emacs <22.2 and XEmacs.
  20. (eval-and-compile
  21. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
  22. ;; In Emacs 24, `open-protocol-stream' is an autoloaded alias for
  23. ;; `make-network-stream'.
  24. (unless (fboundp 'open-protocol-stream)
  25. (require 'proto-stream)))
  26. (require 'nnheader)
  27. (require 'nnoo)
  28. (require 'gnus-util)
  29. (require 'gnus)
  30. (require 'gnus-group) ;; gnus-group-name-charset
  31. (nnoo-declare nntp)
  32. (eval-when-compile (require 'cl))
  33. (autoload 'auth-source-search "auth-source")
  34. (defgroup nntp nil
  35. "NNTP access for Gnus."
  36. :group 'gnus)
  37. (defvoo nntp-address nil
  38. "Address of the physical nntp server.")
  39. (defvoo nntp-port-number "nntp"
  40. "Port number on the physical nntp server.")
  41. (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
  42. "*Hook used for sending commands to the server at startup.
  43. The default value is `nntp-send-mode-reader', which makes an innd
  44. server spawn an nnrpd server.")
  45. (defvoo nntp-authinfo-function 'nntp-send-authinfo
  46. "Function used to send AUTHINFO to the server.
  47. It is called with no parameters.")
  48. (defvoo nntp-server-action-alist
  49. '(("nntpd 1\\.5\\.11t"
  50. (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
  51. ("NNRP server Netscape"
  52. (setq nntp-server-list-active-group nil)))
  53. "Alist of regexps to match on server types and actions to be taken.
  54. For instance, if you want Gnus to beep every time you connect
  55. to innd, you could say something like:
  56. \(setq nntp-server-action-alist
  57. '((\"innd\" (ding))))
  58. You probably don't want to do that, though.")
  59. (defvoo nntp-open-connection-function 'nntp-open-network-stream
  60. "Method for connecting to a remote system.
  61. It should be a function, which is called with the output buffer
  62. as its single argument, or one of the following special values:
  63. - `nntp-open-network-stream' specifies a network connection,
  64. upgrading to a TLS connection via STARTTLS if possible.
  65. - `nntp-open-plain-stream' specifies an unencrypted network
  66. connection (no STARTTLS upgrade is attempted).
  67. - `nntp-open-ssl-stream' or `nntp-open-tls-stream' specify a TLS
  68. network connection.
  69. Apart from the above special values, valid functions are as
  70. follows; please refer to their respective doc string for more
  71. information.
  72. For direct connections:
  73. - `nntp-open-netcat-stream'
  74. - `nntp-open-telnet-stream'
  75. For indirect connections:
  76. - `nntp-open-via-rlogin-and-netcat'
  77. - `nntp-open-via-rlogin-and-telnet'
  78. - `nntp-open-via-telnet-and-telnet'")
  79. (defvoo nntp-never-echoes-commands nil
  80. "*Non-nil means the nntp server never echoes commands.
  81. It is reported that some nntps server doesn't echo commands. So, you
  82. may want to set this to non-nil in the method for such a server setting
  83. `nntp-open-connection-function' to `nntp-open-ssl-stream' for example.
  84. Note that the `nntp-open-connection-functions-never-echo-commands'
  85. variable overrides the nil value of this variable.")
  86. (defvoo nntp-open-connection-functions-never-echo-commands
  87. '(nntp-open-network-stream)
  88. "*List of functions that never echo commands.
  89. Add or set a function which you set to `nntp-open-connection-function'
  90. to this list if it does not echo commands. Note that a non-nil value
  91. of the `nntp-never-echoes-commands' variable overrides this variable.")
  92. (defvoo nntp-pre-command nil
  93. "*Pre-command to use with the various nntp-open-via-* methods.
  94. This is where you would put \"runsocks\" or stuff like that.")
  95. (defvoo nntp-telnet-command "telnet"
  96. "*Telnet command used to connect to the nntp server.
  97. This command is used by the methods `nntp-open-telnet-stream',
  98. `nntp-open-via-rlogin-and-telnet' and `nntp-open-via-telnet-and-telnet'.")
  99. (defvoo nntp-telnet-switches '("-8")
  100. "*Switches given to the telnet command `nntp-telnet-command'.")
  101. (defvoo nntp-end-of-line "\r\n"
  102. "*String to use on the end of lines when talking to the NNTP server.
  103. This is \"\\r\\n\" by default, but should be \"\\n\" when using an indirect
  104. connection method (nntp-open-via-*).")
  105. (defvoo nntp-via-rlogin-command "rsh"
  106. "*Rlogin command used to connect to an intermediate host.
  107. This command is used by the methods `nntp-open-via-rlogin-and-telnet'
  108. and `nntp-open-via-rlogin-and-netcat'. The default is \"rsh\", but \"ssh\"
  109. is a popular alternative.")
  110. (defvoo nntp-via-rlogin-command-switches nil
  111. "*Switches given to the rlogin command `nntp-via-rlogin-command'.
  112. If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
  113. \(\"-C\") in order to compress all data connections, otherwise set this
  114. to \(\"-t\" \"-e\" \"none\") or (\"-C\" \"-t\" \"-e\" \"none\") if the telnet
  115. command requires a pseudo-tty allocation on an intermediate host.")
  116. (defvoo nntp-via-telnet-command "telnet"
  117. "*Telnet command used to connect to an intermediate host.
  118. This command is used by the `nntp-open-via-telnet-and-telnet' method.")
  119. (defvoo nntp-via-telnet-switches '("-8")
  120. "*Switches given to the telnet command `nntp-via-telnet-command'.")
  121. (defvoo nntp-netcat-command "nc"
  122. "*Netcat command used to connect to the nntp server.
  123. This command is used by the `nntp-open-netcat-stream' and
  124. `nntp-open-via-rlogin-and-netcat' methods.")
  125. (defvoo nntp-netcat-switches nil
  126. "*Switches given to the netcat command `nntp-netcat-command'.")
  127. (defvoo nntp-via-user-name nil
  128. "*User name to log in on an intermediate host with.
  129. This variable is used by the various nntp-open-via-* methods.")
  130. (defvoo nntp-via-user-password nil
  131. "*Password to use to log in on an intermediate host with.
  132. This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
  133. (defvoo nntp-via-address nil
  134. "*Address of an intermediate host to connect to.
  135. This variable is used by the various nntp-open-via-* methods.")
  136. (defvoo nntp-via-envuser nil
  137. "*Whether both telnet client and server support the ENVIRON option.
  138. If non-nil, there will be no prompt for a login name.")
  139. (defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
  140. "*Regular expression to match the shell prompt on an intermediate host.
  141. This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
  142. (defvoo nntp-large-newsgroup 50
  143. "*The number of articles which indicates a large newsgroup.
  144. If the number of articles is greater than the value, verbose
  145. messages will be shown to indicate the current status.")
  146. (defvoo nntp-maximum-request 400
  147. "*The maximum number of the requests sent to the NNTP server at one time.
  148. If Emacs hangs up while retrieving headers, set the variable to a
  149. lower value.")
  150. (defvoo nntp-nov-is-evil nil
  151. "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
  152. (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
  153. "*List of strings that are used as commands to fetch NOV lines from a server.
  154. The strings are tried in turn until a positive response is gotten. If
  155. none of the commands are successful, nntp will just grab headers one
  156. by one.")
  157. (defvoo nntp-nov-gap 5
  158. "*Maximum allowed gap between two articles.
  159. If the gap between two consecutive articles is bigger than this
  160. variable, split the XOVER request into two requests.")
  161. (defvoo nntp-xref-number-is-evil nil
  162. "*If non-nil, Gnus never trusts article numbers in the Xref header.
  163. Some news servers, e.g., ones running Diablo, run multiple engines
  164. having the same articles but article numbers are not kept synchronized
  165. between them. If you connect to such a server, set this to a non-nil
  166. value, and Gnus never uses article numbers (that appear in the Xref
  167. header and vary by which engine is chosen) to refer to articles.")
  168. (defvoo nntp-prepare-server-hook nil
  169. "*Hook run before a server is opened.
  170. If can be used to set up a server remotely, for instance. Say you
  171. have an account at the machine \"other.machine\". This machine has
  172. access to an NNTP server that you can't access locally. You could
  173. then use this hook to rsh to the remote machine and start a proxy NNTP
  174. server there that you can connect to. See also
  175. `nntp-open-connection-function'")
  176. (defvoo nntp-coding-system-for-read 'binary
  177. "*Coding system to read from NNTP.")
  178. (defvoo nntp-coding-system-for-write 'binary
  179. "*Coding system to write to NNTP.")
  180. ;; Marks
  181. (defvoo nntp-marks-is-evil nil
  182. "*If non-nil, Gnus will never generate and use marks file for nntp groups.
  183. See `nnml-marks-is-evil' for more information.")
  184. (defvoo nntp-marks-file-name ".marks")
  185. (defvoo nntp-marks nil)
  186. (defvar nntp-marks-modtime (gnus-make-hashtable))
  187. (defcustom nntp-marks-directory
  188. (nnheader-concat gnus-directory "marks/")
  189. "*The directory where marks for nntp groups will be stored."
  190. :group 'nntp
  191. :type 'directory)
  192. (defcustom nntp-authinfo-file "~/.authinfo"
  193. ".netrc-like file that holds nntp authinfo passwords."
  194. :group 'nntp
  195. :type
  196. '(choice file
  197. (repeat :tag "Entries"
  198. :menu-tag "Inline"
  199. (list :format "%v"
  200. :value ("" ("login" . "") ("password" . ""))
  201. (string :tag "Host")
  202. (checklist :inline t
  203. (cons :format "%v"
  204. (const :format "" "login")
  205. (string :format "Login: %v"))
  206. (cons :format "%v"
  207. (const :format "" "password")
  208. (string :format "Password: %v")))))))
  209. (make-obsolete 'nntp-authinfo-file nil "Emacs 24.1")
  210. (defvoo nntp-connection-timeout nil
  211. "*Number of seconds to wait before an nntp connection times out.
  212. If this variable is nil, which is the default, no timers are set.
  213. NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
  214. (defvoo nntp-prepare-post-hook nil
  215. "*Hook run just before posting an article. It is supposed to be used
  216. to insert Cancel-Lock headers.")
  217. (defvoo nntp-server-list-active-group 'try
  218. "If nil, then always use GROUP instead of LIST ACTIVE.
  219. This is usually slower, but on misconfigured servers that don't
  220. update their active files often, this can help.")
  221. ;;; Internal variables.
  222. (defvoo nntp-retrieval-in-progress nil)
  223. (defvar nntp-record-commands nil
  224. "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
  225. (defvar nntp-have-messaged nil)
  226. (defvar nntp-process-wait-for nil)
  227. (defvar nntp-process-to-buffer nil)
  228. (defvar nntp-process-callback nil)
  229. (defvar nntp-process-decode nil)
  230. (defvar nntp-process-start-point nil)
  231. (defvar nntp-inside-change-function nil)
  232. (defvoo nntp-last-command-time nil)
  233. (defvoo nntp-last-command nil)
  234. (defvoo nntp-authinfo-password nil)
  235. (defvoo nntp-authinfo-user nil)
  236. (defvoo nntp-authinfo-force nil)
  237. (defvar nntp-connection-list nil)
  238. (defvoo nntp-server-type nil)
  239. (defvoo nntp-connection-alist nil)
  240. (defvoo nntp-status-string "")
  241. (defconst nntp-version "nntp 5.0")
  242. (defvoo nntp-inhibit-erase nil)
  243. (defvoo nntp-inhibit-output nil)
  244. (defvoo nntp-server-xover 'try)
  245. (defvar nntp-async-timer nil)
  246. (defvar nntp-async-process-list nil)
  247. (defvar nntp-authinfo-rejected nil
  248. "A custom error condition used to report 'Authentication Rejected' errors.
  249. Condition handlers that match just this condition ensure that the nntp
  250. backend doesn't catch this error.")
  251. (put 'nntp-authinfo-rejected 'error-conditions '(error nntp-authinfo-rejected))
  252. (put 'nntp-authinfo-rejected 'error-message "Authorization Rejected")
  253. ;;; Internal functions.
  254. (defsubst nntp-send-string (process string)
  255. "Send STRING to PROCESS."
  256. ;; We need to store the time to provide timeouts, and
  257. ;; to store the command so the we can replay the command
  258. ;; if the server gives us an AUTHINFO challenge.
  259. (setq nntp-last-command-time (current-time)
  260. nntp-last-command string)
  261. (when nntp-record-commands
  262. (nntp-record-command string))
  263. (process-send-string process (concat string nntp-end-of-line))
  264. (or (memq (process-status process) '(open run))
  265. (nntp-report "Server closed connection")))
  266. (defun nntp-record-command (string)
  267. "Record the command STRING."
  268. (with-current-buffer (get-buffer-create "*nntp-log*")
  269. (goto-char (point-max))
  270. (insert (format-time-string "%Y%m%dT%H%M%S.%3N")
  271. " " nntp-address " " string "\n")))
  272. (defun nntp-report (&rest args)
  273. "Report an error from the nntp backend. The first string in ARGS
  274. can be a format string. For some commands, the failed command may be
  275. retried once before actually displaying the error report."
  276. (when nntp-record-commands
  277. (nntp-record-command "*** CALLED nntp-report ***"))
  278. (nnheader-report 'nntp args)
  279. (apply 'error args))
  280. (defun nntp-report-1 (&rest args)
  281. "Throws out to nntp-with-open-group-error so that the connection may
  282. be restored and the command retried."
  283. (when nntp-record-commands
  284. (nntp-record-command "*** CONNECTION LOST ***"))
  285. (throw 'nntp-with-open-group-error t))
  286. (defmacro nntp-copy-to-buffer (buffer start end)
  287. "Copy string from unibyte current buffer to multibyte buffer."
  288. (if (featurep 'xemacs)
  289. `(copy-to-buffer ,buffer ,start ,end)
  290. `(let ((string (buffer-substring ,start ,end)))
  291. (with-current-buffer ,buffer
  292. (erase-buffer)
  293. (insert (if enable-multibyte-characters
  294. (mm-string-to-multibyte string)
  295. string))
  296. (goto-char (point-min))
  297. nil))))
  298. (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
  299. "Wait for WAIT-FOR to arrive from PROCESS."
  300. (with-current-buffer (process-buffer process)
  301. (goto-char (point-min))
  302. (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
  303. (looking-at "48[02]"))
  304. (memq (process-status process) '(open run)))
  305. (cond ((looking-at "480")
  306. (nntp-handle-authinfo process))
  307. ((looking-at "482")
  308. (nnheader-report 'nntp "%s"
  309. (get 'nntp-authinfo-rejected 'error-message))
  310. (signal 'nntp-authinfo-rejected nil))
  311. ((looking-at "^.*\n")
  312. (delete-region (point) (progn (forward-line 1) (point)))))
  313. (nntp-accept-process-output process)
  314. (goto-char (point-min)))
  315. (prog1
  316. (cond
  317. ((looking-at "[45]")
  318. (progn
  319. (nntp-snarf-error-message)
  320. nil))
  321. ((not (memq (process-status process) '(open run)))
  322. (nntp-report "Server closed connection"))
  323. (t
  324. (goto-char (point-max))
  325. (let ((limit (point-min))
  326. response)
  327. (while (not (re-search-backward wait-for limit t))
  328. (nntp-accept-process-output process)
  329. ;; We assume that whatever we wait for is less than 1000
  330. ;; characters long.
  331. (setq limit (max (- (point-max) 1000) (point-min)))
  332. (goto-char (point-max)))
  333. (setq response (match-string 0))
  334. (with-current-buffer nntp-server-buffer
  335. (setq nntp-process-response response)))
  336. (nntp-decode-text (not decode))
  337. (unless discard
  338. (with-current-buffer buffer
  339. (goto-char (point-max))
  340. (nnheader-insert-buffer-substring (process-buffer process))
  341. ;; Nix out "nntp reading...." message.
  342. (when nntp-have-messaged
  343. (setq nntp-have-messaged nil)
  344. (nnheader-message 5 ""))))
  345. t))
  346. (unless discard
  347. (erase-buffer)))))
  348. (defun nntp-kill-buffer (buffer)
  349. (when (buffer-name buffer)
  350. (let ((process (get-buffer-process buffer)))
  351. (when process
  352. (delete-process process)))
  353. (kill-buffer buffer)
  354. (nnheader-init-server-buffer)))
  355. (defun nntp-erase-buffer (buffer)
  356. "Erase contents of BUFFER."
  357. (with-current-buffer buffer
  358. (erase-buffer)))
  359. (defsubst nntp-find-connection (buffer)
  360. "Find the connection delivering to BUFFER."
  361. (let ((alist nntp-connection-alist)
  362. (buffer (if (stringp buffer) (get-buffer buffer) buffer))
  363. process entry)
  364. (while (and alist (setq entry (pop alist)))
  365. (when (eq buffer (cadr entry))
  366. (setq process (car entry)
  367. alist nil)))
  368. (when process
  369. (if (memq (process-status process) '(open run))
  370. process
  371. (nntp-kill-buffer (process-buffer process))
  372. (setq nntp-connection-alist (delq entry nntp-connection-alist))
  373. nil))))
  374. (defsubst nntp-find-connection-entry (buffer)
  375. "Return the entry for the connection to BUFFER."
  376. (assq (nntp-find-connection buffer) nntp-connection-alist))
  377. (defun nntp-find-connection-buffer (buffer)
  378. "Return the process connection buffer tied to BUFFER."
  379. (let ((process (nntp-find-connection buffer)))
  380. (when process
  381. (process-buffer process))))
  382. (defsubst nntp-retrieve-data (command address port buffer
  383. &optional wait-for callback decode)
  384. "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
  385. (let ((process (or (nntp-find-connection buffer)
  386. (nntp-open-connection buffer))))
  387. (if process
  388. (progn
  389. (unless (or nntp-inhibit-erase nnheader-callback-function)
  390. (nntp-erase-buffer (process-buffer process)))
  391. (condition-case err
  392. (progn
  393. (when command
  394. (nntp-send-string process command))
  395. (cond
  396. ((eq callback 'ignore)
  397. t)
  398. ((and callback wait-for)
  399. (nntp-async-wait process wait-for buffer decode callback)
  400. t)
  401. (wait-for
  402. (nntp-wait-for process wait-for buffer decode))
  403. (t t)))
  404. (nntp-authinfo-rejected
  405. (signal 'nntp-authinfo-rejected (cdr err)))
  406. (error
  407. (nnheader-report 'nntp "Couldn't open connection to %s: %s"
  408. address err))
  409. (quit
  410. (message "Quit retrieving data from nntp")
  411. (signal 'quit nil)
  412. nil)))
  413. (nnheader-report 'nntp "Couldn't open connection to %s" address))))
  414. (defsubst nntp-send-command (wait-for &rest strings)
  415. "Send STRINGS to server and wait until WAIT-FOR returns."
  416. (when (and (not nnheader-callback-function)
  417. (not nntp-inhibit-output))
  418. (nntp-erase-buffer nntp-server-buffer))
  419. (let* ((command (mapconcat 'identity strings " "))
  420. (process (nntp-find-connection nntp-server-buffer))
  421. (buffer (and process (process-buffer process)))
  422. (pos (and buffer (with-current-buffer buffer (point)))))
  423. (if process
  424. (prog1
  425. (nntp-retrieve-data command
  426. nntp-address nntp-port-number
  427. nntp-server-buffer
  428. wait-for nnheader-callback-function)
  429. ;; If nothing to wait for, still remove possibly echo'ed commands.
  430. ;; We don't have echoes if `nntp-never-echoes-commands' is non-nil
  431. ;; or the value of `nntp-open-connection-function' is in
  432. ;; `nntp-open-connection-functions-never-echo-commands', so we
  433. ;; skip this in that cases.
  434. (unless (or wait-for
  435. nntp-never-echoes-commands
  436. (memq
  437. nntp-open-connection-function
  438. nntp-open-connection-functions-never-echo-commands))
  439. (nntp-accept-response)
  440. (with-current-buffer buffer
  441. (goto-char pos)
  442. (if (looking-at (regexp-quote command))
  443. (delete-region pos (progn (forward-line 1)
  444. (point-at-bol)))))))
  445. (nnheader-report 'nntp "Couldn't open connection to %s."
  446. nntp-address))))
  447. (defun nntp-send-command-nodelete (wait-for &rest strings)
  448. "Send STRINGS to server and wait until WAIT-FOR returns."
  449. (let* ((command (mapconcat 'identity strings " "))
  450. (process (nntp-find-connection nntp-server-buffer))
  451. (buffer (and process (process-buffer process)))
  452. (pos (and buffer (with-current-buffer buffer (point)))))
  453. (if process
  454. (prog1
  455. (nntp-retrieve-data command
  456. nntp-address nntp-port-number
  457. nntp-server-buffer
  458. wait-for nnheader-callback-function)
  459. ;; If nothing to wait for, still remove possibly echo'ed commands
  460. (unless wait-for
  461. (nntp-accept-response)
  462. (with-current-buffer buffer
  463. (goto-char pos)
  464. (if (looking-at (regexp-quote command))
  465. (delete-region pos (progn (forward-line 1)
  466. (point-at-bol)))))))
  467. (nnheader-report 'nntp "Couldn't open connection to %s."
  468. nntp-address))))
  469. (defun nntp-send-command-and-decode (wait-for &rest strings)
  470. "Send STRINGS to server and wait until WAIT-FOR returns."
  471. (when (and (not nnheader-callback-function)
  472. (not nntp-inhibit-output))
  473. (nntp-erase-buffer nntp-server-buffer))
  474. (let* ((command (mapconcat 'identity strings " "))
  475. (process (nntp-find-connection nntp-server-buffer))
  476. (buffer (and process (process-buffer process)))
  477. (pos (and buffer (with-current-buffer buffer (point)))))
  478. (if process
  479. (prog1
  480. (nntp-retrieve-data command
  481. nntp-address nntp-port-number
  482. nntp-server-buffer
  483. wait-for nnheader-callback-function t)
  484. ;; If nothing to wait for, still remove possibly echo'ed commands
  485. (unless wait-for
  486. (nntp-accept-response)
  487. (with-current-buffer buffer
  488. (goto-char pos)
  489. (if (looking-at (regexp-quote command))
  490. (delete-region pos (progn (forward-line 1) (point-at-bol))))
  491. )))
  492. (nnheader-report 'nntp "Couldn't open connection to %s."
  493. nntp-address))))
  494. (defun nntp-send-buffer (wait-for)
  495. "Send the current buffer to server and wait until WAIT-FOR returns."
  496. (when (and (not nnheader-callback-function)
  497. (not nntp-inhibit-output))
  498. (nntp-erase-buffer
  499. (nntp-find-connection-buffer nntp-server-buffer)))
  500. (nntp-encode-text)
  501. ;; Make sure we did not forget to encode some of the content.
  502. (assert (save-excursion (goto-char (point-min))
  503. (not (re-search-forward "[^\000-\377]" nil t))))
  504. (mm-disable-multibyte)
  505. (process-send-region (nntp-find-connection nntp-server-buffer)
  506. (point-min) (point-max))
  507. (nntp-retrieve-data
  508. nil nntp-address nntp-port-number nntp-server-buffer
  509. wait-for nnheader-callback-function))
  510. ;;; Interface functions.
  511. (nnoo-define-basics nntp)
  512. (defsubst nntp-next-result-arrived-p ()
  513. (cond
  514. ;; A result that starts with a 2xx code is terminated by
  515. ;; a line with only a "." on it.
  516. ((eq (char-after) ?2)
  517. (if (re-search-forward "\n\\.\r?\n" nil t)
  518. (progn
  519. ;; Some broken news servers add another dot at the end.
  520. ;; Protect against inflooping there.
  521. (while (looking-at "^\\.\r?\n")
  522. (forward-line 1))
  523. t)
  524. nil))
  525. ;; A result that starts with a 3xx or 4xx code is terminated
  526. ;; by a newline.
  527. ((looking-at "[34]")
  528. (if (search-forward "\n" nil t)
  529. t
  530. nil))
  531. ;; No result here.
  532. (t
  533. nil)))
  534. (eval-when-compile
  535. (defvar nntp-with-open-group-internal nil)
  536. (defvar nntp-report-n nil))
  537. (defun nntp-with-open-group-function (-group -server -connectionless -bodyfun)
  538. "Protect against servers that don't like clients that keep idle connections opens.
  539. The problem being that these servers may either close a connection or
  540. simply ignore any further requests on a connection. Closed
  541. connections are not detected until `accept-process-output' has updated
  542. the `process-status'. Dropped connections are not detected until the
  543. connection timeouts (which may be several minutes) or
  544. `nntp-connection-timeout' has expired. When these occur
  545. `nntp-with-open-group', opens a new connection then re-issues the NNTP
  546. command whose response triggered the error."
  547. (letf ((nntp-report-n (symbol-function 'nntp-report))
  548. ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1))
  549. (nntp-with-open-group-internal nil))
  550. (while (catch 'nntp-with-open-group-error
  551. ;; Open the connection to the server
  552. ;; NOTE: Existing connections are NOT tested.
  553. (nntp-possibly-change-group -group -server -connectionless)
  554. (let ((-timer
  555. (and nntp-connection-timeout
  556. (run-at-time
  557. nntp-connection-timeout nil
  558. (lambda ()
  559. (let* ((-process (nntp-find-connection
  560. nntp-server-buffer))
  561. (-buffer (and -process
  562. (process-buffer -process))))
  563. ;; When I an able to identify the
  564. ;; connection to the server AND I've
  565. ;; received NO response for
  566. ;; nntp-connection-timeout seconds.
  567. (when (and -buffer (eq 0 (buffer-size -buffer)))
  568. ;; Close the connection. Take no
  569. ;; other action as the accept input
  570. ;; code will handle the closed
  571. ;; connection.
  572. (nntp-kill-buffer -buffer))))))))
  573. (unwind-protect
  574. (setq nntp-with-open-group-internal
  575. (condition-case nil
  576. (funcall -bodyfun)
  577. (quit
  578. (unless debug-on-quit
  579. (nntp-close-server))
  580. (signal 'quit nil))))
  581. (when -timer
  582. (nnheader-cancel-timer -timer)))
  583. nil))
  584. (setf (symbol-function 'nntp-report) nntp-report-n))
  585. nntp-with-open-group-internal))
  586. (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
  587. "Protect against servers that don't like clients that keep idle connections opens.
  588. The problem being that these servers may either close a connection or
  589. simply ignore any further requests on a connection. Closed
  590. connections are not detected until `accept-process-output' has updated
  591. the `process-status'. Dropped connections are not detected until the
  592. connection timeouts (which may be several minutes) or
  593. `nntp-connection-timeout' has expired. When these occur
  594. `nntp-with-open-group', opens a new connection then re-issues the NNTP
  595. command whose response triggered the error."
  596. (declare (indent 2) (debug (form form [&optional symbolp] def-body)))
  597. (when (and (listp connectionless)
  598. (not (eq connectionless nil)))
  599. (setq forms (cons connectionless forms)
  600. connectionless nil))
  601. `(nntp-with-open-group-function ,group ,server ,connectionless (lambda () ,@forms)))
  602. (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
  603. "Retrieve the headers of ARTICLES."
  604. (nntp-with-open-group
  605. group server
  606. (with-current-buffer (nntp-find-connection-buffer nntp-server-buffer)
  607. (erase-buffer)
  608. (if (and (not gnus-nov-is-evil)
  609. (not nntp-nov-is-evil)
  610. (nntp-retrieve-headers-with-xover articles fetch-old))
  611. ;; We successfully retrieved the headers via XOVER.
  612. 'nov
  613. ;; XOVER didn't work, so we do it the hard, slow and inefficient
  614. ;; way.
  615. (let ((number (length articles))
  616. (articles articles)
  617. (count 0)
  618. (received 0)
  619. (last-point (point-min))
  620. (buf (nntp-find-connection-buffer nntp-server-buffer))
  621. (nntp-inhibit-erase t)
  622. article)
  623. ;; Send HEAD commands.
  624. (while (setq article (pop articles))
  625. (nntp-send-command
  626. nil
  627. "HEAD" (if (numberp article)
  628. (int-to-string article)
  629. ;; `articles' is either a list of article numbers
  630. ;; or a list of article IDs.
  631. article))
  632. (incf count)
  633. ;; Every 400 requests we have to read the stream in
  634. ;; order to avoid deadlocks.
  635. (when (or (null articles) ;All requests have been sent.
  636. (zerop (% count nntp-maximum-request)))
  637. (nntp-accept-response)
  638. (while (progn
  639. (set-buffer buf)
  640. (goto-char last-point)
  641. ;; Count replies.
  642. (while (nntp-next-result-arrived-p)
  643. (setq last-point (point))
  644. (incf received))
  645. (< received count))
  646. ;; If number of headers is greater than 100, give
  647. ;; informative messages.
  648. (and (numberp nntp-large-newsgroup)
  649. (> number nntp-large-newsgroup)
  650. (zerop (% received 20))
  651. (nnheader-message 6 "NNTP: Receiving headers... %d%%"
  652. (/ (* received 100) number)))
  653. (nntp-accept-response))))
  654. (and (numberp nntp-large-newsgroup)
  655. (> number nntp-large-newsgroup)
  656. (nnheader-message 6 "NNTP: Receiving headers...done"))
  657. ;; Now all of replies are received. Fold continuation lines.
  658. (nnheader-fold-continuation-lines)
  659. ;; Remove all "\r"'s.
  660. (nnheader-strip-cr)
  661. (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
  662. 'headers)))))
  663. (deffoo nntp-retrieve-group-data-early (server infos)
  664. "Retrieve group info on INFOS."
  665. (nntp-with-open-group nil server
  666. (let ((buffer (nntp-find-connection-buffer nntp-server-buffer)))
  667. (unless infos
  668. (with-current-buffer buffer
  669. (setq nntp-retrieval-in-progress nil)))
  670. (when (and buffer
  671. infos
  672. (with-current-buffer buffer
  673. (not nntp-retrieval-in-progress)))
  674. ;; The first time this is run, this variable is `try'. So we
  675. ;; try.
  676. (when (eq nntp-server-list-active-group 'try)
  677. (nntp-try-list-active
  678. (gnus-group-real-name (gnus-info-group (car infos)))))
  679. (with-current-buffer buffer
  680. (erase-buffer)
  681. ;; Mark this buffer as "in use" in case we try to issue two
  682. ;; retrievals from the same server. This shouldn't happen,
  683. ;; so this is mostly a sanity check.
  684. (setq nntp-retrieval-in-progress t)
  685. (let ((nntp-inhibit-erase t)
  686. (command (if nntp-server-list-active-group
  687. "LIST ACTIVE" "GROUP")))
  688. (dolist (info infos)
  689. (nntp-send-command
  690. nil command (gnus-group-real-name (gnus-info-group info)))))
  691. (length infos))))))
  692. (deffoo nntp-finish-retrieve-group-infos (server infos count)
  693. (nntp-with-open-group nil server
  694. (let ((buf (nntp-find-connection-buffer nntp-server-buffer))
  695. (method (gnus-find-method-for-group
  696. (gnus-info-group (car infos))
  697. (car infos)))
  698. (received 0)
  699. (last-point 1))
  700. (with-current-buffer buf
  701. (setq nntp-retrieval-in-progress nil))
  702. (when (and buf
  703. count)
  704. (with-current-buffer buf
  705. (while (and (gnus-buffer-live-p buf)
  706. (progn
  707. (goto-char last-point)
  708. ;; Count replies.
  709. (while (re-search-forward
  710. (if nntp-server-list-active-group
  711. "^[.]"
  712. "^[0-9]")
  713. nil t)
  714. (incf received))
  715. (setq last-point (point))
  716. (< received count)))
  717. (nntp-accept-response))
  718. ;; We now have all the entries. Remove CRs.
  719. (nnheader-strip-cr)
  720. (if (not nntp-server-list-active-group)
  721. (progn
  722. (nntp-copy-to-buffer nntp-server-buffer
  723. (point-min) (point-max))
  724. (gnus-groups-to-gnus-format method gnus-active-hashtb t))
  725. ;; We have read active entries, so we just delete the
  726. ;; superfluous gunk.
  727. (goto-char (point-min))
  728. (while (re-search-forward "^[.2-5]" nil t)
  729. (delete-region (match-beginning 0)
  730. (progn (forward-line 1) (point))))
  731. (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
  732. (with-current-buffer nntp-server-buffer
  733. (gnus-active-to-gnus-format
  734. ;; Kludge to use the extended method name if you have
  735. ;; an extended one.
  736. (if (consp (gnus-info-method (car infos)))
  737. (gnus-info-method (car infos))
  738. method)
  739. gnus-active-hashtb nil t))))))))
  740. (deffoo nntp-retrieve-groups (groups &optional server)
  741. "Retrieve group info on GROUPS."
  742. (nntp-with-open-group
  743. nil server
  744. (when (and (nntp-find-connection-buffer nntp-server-buffer)
  745. (with-current-buffer
  746. (nntp-find-connection-buffer nntp-server-buffer)
  747. (if (not nntp-retrieval-in-progress)
  748. t
  749. (message "Warning: Refusing to do retrieval from %s because a retrieval is already happening"
  750. server)
  751. nil)))
  752. (catch 'done
  753. (save-excursion
  754. ;; Erase nntp-server-buffer before nntp-inhibit-erase.
  755. (nntp-erase-buffer nntp-server-buffer)
  756. (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
  757. ;; The first time this is run, this variable is `try'. So we
  758. ;; try.
  759. (when (eq nntp-server-list-active-group 'try)
  760. (nntp-try-list-active (car groups)))
  761. (erase-buffer)
  762. (let ((count 0)
  763. (groups groups)
  764. (received 0)
  765. (last-point (point-min))
  766. (nntp-inhibit-erase t)
  767. (buf (nntp-find-connection-buffer nntp-server-buffer))
  768. (command (if nntp-server-list-active-group
  769. "LIST ACTIVE" "GROUP")))
  770. (while groups
  771. ;; Timeout may have killed the buffer.
  772. (unless (gnus-buffer-live-p buf)
  773. (nnheader-report 'nntp "Connection to %s is closed." server)
  774. (throw 'done nil))
  775. ;; Send the command to the server.
  776. (nntp-send-command nil command (pop groups))
  777. (incf count)
  778. ;; Every 400 requests we have to read the stream in
  779. ;; order to avoid deadlocks.
  780. (when (or (null groups) ;All requests have been sent.
  781. (zerop (% count nntp-maximum-request)))
  782. (nntp-accept-response)
  783. (while (and (gnus-buffer-live-p buf)
  784. (progn
  785. ;; Search `blue moon' in this file for the
  786. ;; reason why set-buffer here.
  787. (set-buffer buf)
  788. (goto-char last-point)
  789. ;; Count replies.
  790. (while (re-search-forward "^[0-9]" nil t)
  791. (incf received))
  792. (setq last-point (point))
  793. (< received count)))
  794. (nntp-accept-response))))
  795. ;; Wait for the reply from the final command.
  796. (unless (gnus-buffer-live-p buf)
  797. (nnheader-report 'nntp "Connection to %s is closed." server)
  798. (throw 'done nil))
  799. (set-buffer buf)
  800. (goto-char (point-max))
  801. (re-search-backward "^[0-9]" nil t)
  802. (when (looking-at "^[23]")
  803. (while (and (gnus-buffer-live-p buf)
  804. (progn
  805. (set-buffer buf)
  806. (goto-char (point-max))
  807. (if (not nntp-server-list-active-group)
  808. (not (re-search-backward "\r?\n"
  809. (- (point) 3) t))
  810. (not (re-search-backward "^\\.\r?\n"
  811. (- (point) 4) t)))))
  812. (nntp-accept-response)))
  813. ;; Now all replies are received. We remove CRs.
  814. (unless (gnus-buffer-live-p buf)
  815. (nnheader-report 'nntp "Connection to %s is closed." server)
  816. (throw 'done nil))
  817. (set-buffer buf)
  818. (goto-char (point-min))
  819. (while (search-forward "\r" nil t)
  820. (replace-match "" t t))
  821. (if (not nntp-server-list-active-group)
  822. (progn
  823. (nntp-copy-to-buffer nntp-server-buffer
  824. (point-min) (point-max))
  825. 'group)
  826. ;; We have read active entries, so we just delete the
  827. ;; superfluous gunk.
  828. (goto-char (point-min))
  829. (while (re-search-forward "^[.2-5]" nil t)
  830. (delete-region (match-beginning 0)
  831. (progn (forward-line 1) (point))))
  832. (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
  833. 'active)))))))
  834. (deffoo nntp-retrieve-articles (articles &optional group server)
  835. (nntp-with-open-group
  836. group server
  837. (save-excursion
  838. (let ((number (length articles))
  839. (articles articles)
  840. (count 0)
  841. (received 0)
  842. (last-point (point-min))
  843. (buf (nntp-find-connection-buffer nntp-server-buffer))
  844. (nntp-inhibit-erase t)
  845. (map (apply 'vector articles))
  846. (point 1)
  847. article)
  848. (set-buffer buf)
  849. (erase-buffer)
  850. ;; Send ARTICLE command.
  851. (while (setq article (pop articles))
  852. (nntp-send-command
  853. nil
  854. "ARTICLE" (if (numberp article)
  855. (int-to-string article)
  856. ;; `articles' is either a list of article numbers
  857. ;; or a list of article IDs.
  858. article))
  859. (incf count)
  860. ;; Every 400 requests we have to read the stream in
  861. ;; order to avoid deadlocks.
  862. (when (or (null articles) ;All requests have been sent.
  863. (zerop (% count nntp-maximum-request)))
  864. (nntp-accept-response)
  865. (while (progn
  866. (set-buffer buf)
  867. (goto-char last-point)
  868. ;; Count replies.
  869. (while (nntp-next-result-arrived-p)
  870. (aset map received (cons (aref map received) (point)))
  871. (setq last-point (point))
  872. (incf received))
  873. (< received count))
  874. ;; If number of headers is greater than 100, give
  875. ;; informative messages.
  876. (and (numberp nntp-large-newsgroup)
  877. (> number nntp-large-newsgroup)
  878. (zerop (% received 20))
  879. (nnheader-message 6 "NNTP: Receiving articles... %d%%"
  880. (/ (* received 100) number)))
  881. (nntp-accept-response))))
  882. (and (numberp nntp-large-newsgroup)
  883. (> number nntp-large-newsgroup)
  884. (nnheader-message 6 "NNTP: Receiving articles...done"))
  885. ;; Now we have all the responses. We go through the results,
  886. ;; wash it and copy it over to the server buffer.
  887. (set-buffer nntp-server-buffer)
  888. (erase-buffer)
  889. (setq last-point (point-min))
  890. (mapcar
  891. (lambda (entry)
  892. (narrow-to-region
  893. (setq point (goto-char (point-max)))
  894. (progn
  895. (nnheader-insert-buffer-substring buf last-point (cdr entry))
  896. (point-max)))
  897. (setq last-point (cdr entry))
  898. (nntp-decode-text)
  899. (widen)
  900. (cons (car entry) point))
  901. map)))))
  902. (defun nntp-try-list-active (group)
  903. (nntp-list-active-group group)
  904. (with-current-buffer nntp-server-buffer
  905. (goto-char (point-min))
  906. (cond ((or (eobp)
  907. (looking-at "5[0-9]+"))
  908. (setq nntp-server-list-active-group nil))
  909. (t
  910. (setq nntp-server-list-active-group t)))))
  911. (deffoo nntp-list-active-group (group &optional server)
  912. "Return the active info on GROUP (which can be a regexp)."
  913. (nntp-with-open-group
  914. nil server
  915. (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
  916. (deffoo nntp-request-group-articles (group &optional server)
  917. "Return the list of existing articles in GROUP."
  918. (nntp-with-open-group
  919. nil server
  920. (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
  921. (deffoo nntp-request-article (article &optional group server buffer command)
  922. (nntp-with-open-group
  923. group server
  924. (when (nntp-send-command-and-decode
  925. "\r?\n\\.\r?\n" "ARTICLE"
  926. (if (numberp article) (int-to-string article) article))
  927. (when (and buffer
  928. (not (equal buffer nntp-server-buffer)))
  929. (with-current-buffer nntp-server-buffer
  930. (copy-to-buffer buffer (point-min) (point-max))))
  931. (nntp-find-group-and-number group))))
  932. (deffoo nntp-request-head (article &optional group server)
  933. (nntp-with-open-group
  934. group server
  935. (when (nntp-send-command
  936. "\r?\n\\.\r?\n" "HEAD"
  937. (if (numberp article) (int-to-string article) article))
  938. (prog1
  939. (nntp-find-group-and-number group)
  940. (nntp-decode-text)))))
  941. (deffoo nntp-request-body (article &optional group server)
  942. (nntp-with-open-group
  943. group server
  944. (nntp-send-command-and-decode
  945. "\r?\n\\.\r?\n" "BODY"
  946. (if (numberp article) (int-to-string article) article))))
  947. (deffoo nntp-request-group (group &optional server dont-check info)
  948. (nntp-with-open-group
  949. nil server
  950. (when (nntp-send-command "^[245].*\n" "GROUP" group)
  951. (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
  952. (setcar (cddr entry) group)))))
  953. (deffoo nntp-close-group (group &optional server)
  954. t)
  955. (deffoo nntp-server-opened (&optional server)
  956. "Say whether a connection to SERVER has been opened."
  957. (and (nnoo-current-server-p 'nntp server)
  958. nntp-server-buffer
  959. (gnus-buffer-live-p nntp-server-buffer)
  960. (nntp-find-connection nntp-server-buffer)))
  961. (deffoo nntp-open-server (server &optional defs connectionless)
  962. (nnheader-init-server-buffer)
  963. (if (nntp-server-opened server)
  964. t
  965. (when (or (stringp (car defs))
  966. (numberp (car defs)))
  967. (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
  968. (unless (assq 'nntp-address defs)
  969. (setq defs (append defs (list (list 'nntp-address server)))))
  970. (nnoo-change-server 'nntp server defs)
  971. (if connectionless
  972. t
  973. (or (nntp-find-connection nntp-server-buffer)
  974. (nntp-open-connection nntp-server-buffer)))))
  975. (deffoo nntp-close-server (&optional server)
  976. (nntp-possibly-change-group nil server t)
  977. (let ((process (nntp-find-connection nntp-server-buffer)))
  978. (while process
  979. (when (memq (process-status process) '(open run))
  980. (ignore-errors
  981. (nntp-send-string process "QUIT")
  982. (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
  983. ;; Ok, this is evil, but when using telnet and stuff
  984. ;; as the connection method, it's important that the
  985. ;; QUIT command actually is sent out before we kill
  986. ;; the process.
  987. (sleep-for 1))))
  988. (nntp-kill-buffer (process-buffer process))
  989. (setq process (car (pop nntp-connection-alist))))
  990. (nnoo-close-server 'nntp)))
  991. (deffoo nntp-request-close ()
  992. (let (process)
  993. (while (setq process (pop nntp-connection-list))
  994. (when (memq (process-status process) '(open run))
  995. (ignore-errors
  996. (nntp-send-string process "QUIT")
  997. (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
  998. ;; Ok, this is evil, but when using telnet and stuff
  999. ;; as the connection method, it's important that the
  1000. ;; QUIT command actually is sent out before we kill
  1001. ;; the process.
  1002. (sleep-for 1))))
  1003. (nntp-kill-buffer (process-buffer process)))))
  1004. (deffoo nntp-request-list (&optional server)
  1005. (nntp-with-open-group
  1006. nil server
  1007. (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
  1008. (deffoo nntp-request-list-newsgroups (&optional server)
  1009. (nntp-with-open-group
  1010. nil server
  1011. (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
  1012. (deffoo nntp-request-newgroups (date &optional server)
  1013. (nntp-with-open-group
  1014. nil server
  1015. (with-current-buffer nntp-server-buffer
  1016. (let* ((time (date-to-time date))
  1017. (ls (- (cadr time) (nth 8 (decode-time time)))))
  1018. (cond ((< ls 0)
  1019. (setcar time (1- (car time)))
  1020. (setcar (cdr time) (+ ls 65536)))
  1021. ((>= ls 65536)
  1022. (setcar time (1+ (car time)))
  1023. (setcar (cdr time) (- ls 65536)))
  1024. (t
  1025. (setcar (cdr time) ls)))
  1026. (prog1
  1027. (nntp-send-command
  1028. "^\\.\r?\n" "NEWGROUPS"
  1029. (format-time-string "%y%m%d %H%M%S" time)
  1030. "GMT")
  1031. (nntp-decode-text))))))
  1032. (deffoo nntp-request-post (&optional server)
  1033. (nntp-with-open-group
  1034. nil server
  1035. (when (nntp-send-command "^[23].*\r?\n" "POST")
  1036. (let ((response (with-current-buffer nntp-server-buffer
  1037. nntp-process-response))
  1038. server-id)
  1039. (when (and response
  1040. (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
  1041. response))
  1042. (setq server-id (match-string 1 response))
  1043. (narrow-to-region (goto-char (point-min))
  1044. (if (search-forward "\n\n" nil t)
  1045. (1- (point))
  1046. (point-max)))
  1047. (unless (mail-fetch-field "Message-ID")
  1048. (goto-char (point-min))
  1049. (insert "Message-ID: " server-id "\n"))
  1050. (widen))
  1051. (run-hooks 'nntp-prepare-post-hook)
  1052. (nntp-send-buffer "^[23].*\n")))))
  1053. (deffoo nntp-request-type (group article)
  1054. 'news)
  1055. (deffoo nntp-asynchronous-p ()
  1056. t)
  1057. (deffoo nntp-request-set-mark (group actions &optional server)
  1058. (when (and (not nntp-marks-is-evil)
  1059. nntp-marks-file-name)
  1060. (nntp-possibly-create-directory group server)
  1061. (nntp-open-marks group server)
  1062. (setq nntp-marks (nnheader-update-marks-actions nntp-marks actions))
  1063. (nntp-save-marks group server))
  1064. nil)
  1065. (deffoo nntp-request-marks (group info &optional server)
  1066. (when (and (not nntp-marks-is-evil)
  1067. nntp-marks-file-name)
  1068. (nntp-possibly-create-directory group server)
  1069. (when (nntp-marks-changed-p group server)
  1070. (nnheader-message 8 "Updating marks for %s..." group)
  1071. (nntp-open-marks group server)
  1072. ;; Update info using `nntp-marks'.
  1073. (mapc (lambda (pred)
  1074. (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
  1075. (gnus-info-set-marks
  1076. info
  1077. (gnus-update-alist-soft
  1078. (cdr pred)
  1079. (cdr (assq (cdr pred) nntp-marks))
  1080. (gnus-info-marks info))
  1081. t)))
  1082. gnus-article-mark-lists)
  1083. (let ((seen (cdr (assq 'read nntp-marks))))
  1084. (gnus-info-set-read info
  1085. (if (and (integerp (car seen))
  1086. (null (cdr seen)))
  1087. (list (cons (car seen) (car seen)))
  1088. seen)))
  1089. (nnheader-message 8 "Updating marks for %s...done" group)))
  1090. nil)
  1091. ;;; Hooky functions.
  1092. (defun nntp-send-mode-reader ()
  1093. "Send the MODE READER command to the nntp server.
  1094. This function is supposed to be called from `nntp-server-opened-hook'.
  1095. It will make innd servers spawn an nnrpd process to allow actual article
  1096. reading."
  1097. (nntp-send-command "^.*\n" "MODE READER"))
  1098. (declare-function netrc-parse "netrc" (&optional file))
  1099. (declare-function netrc-machine "netrc"
  1100. (list machine &optional port defaultport))
  1101. (declare-function netrc-get "netrc" (alist type))
  1102. (defun nntp-send-authinfo (&optional send-if-force)
  1103. "Send the AUTHINFO to the nntp server.
  1104. It will look in the \"~/.authinfo\" file for matching entries. If
  1105. nothing suitable is found there, it will prompt for a user name
  1106. and a password.
  1107. If SEND-IF-FORCE, only send authinfo to the server if the
  1108. .authinfo file has the FORCE token."
  1109. (require 'netrc)
  1110. (let* ((list (netrc-parse nntp-authinfo-file))
  1111. (alist (netrc-machine list nntp-address "nntp"))
  1112. (auth-info
  1113. (nth 0 (auth-source-search
  1114. :max 1
  1115. :host (list nntp-address (nnoo-current-server 'nntp))
  1116. :port `("119" "nntp" ,(format "%s" nntp-port-number)
  1117. "563" "nntps" "snews"))))
  1118. (auth-user (plist-get auth-info :user))
  1119. (auth-force (plist-get auth-info :force))
  1120. (auth-passwd (plist-get auth-info :secret))
  1121. (auth-passwd (if (functionp auth-passwd)
  1122. (funcall auth-passwd)
  1123. auth-passwd))
  1124. (force (or (netrc-get alist "force")
  1125. nntp-authinfo-force
  1126. auth-force))
  1127. (user (or
  1128. ;; this is preferred to netrc-*
  1129. auth-user
  1130. (netrc-get alist "login")
  1131. nntp-authinfo-user))
  1132. (passwd (or
  1133. ;; this is preferred to netrc-*
  1134. auth-passwd
  1135. (netrc-get alist "password"))))
  1136. (when (or (not send-if-force)
  1137. force)
  1138. (unless user
  1139. (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
  1140. nntp-authinfo-user user))
  1141. (unless (member user '(nil ""))
  1142. (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
  1143. (when t ;???Should check if AUTHINFO succeeded
  1144. (nntp-send-command
  1145. "^2.*\r?\n" "AUTHINFO PASS"
  1146. (or passwd
  1147. nntp-authinfo-password
  1148. (setq nntp-authinfo-password
  1149. (read-passwd (format "NNTP (%s@%s) password: "
  1150. user nntp-address))))))))))
  1151. (defun nntp-send-nosy-authinfo ()
  1152. "Send the AUTHINFO to the nntp server."
  1153. (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
  1154. (unless (member user '(nil ""))
  1155. (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
  1156. (when t ;???Should check if AUTHINFO succeeded
  1157. (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
  1158. (read-passwd (format "NNTP (%s@%s) password: "
  1159. user nntp-address)))))))
  1160. (defun nntp-send-authinfo-from-file ()
  1161. "Send the AUTHINFO to the nntp server.
  1162. The authinfo login name is taken from the user's login name and the
  1163. password contained in '~/.nntp-authinfo'."
  1164. (when (file-exists-p "~/.nntp-authinfo")
  1165. (with-temp-buffer
  1166. (insert-file-contents "~/.nntp-authinfo")
  1167. (goto-char (point-min))
  1168. (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
  1169. (nntp-send-command
  1170. "^2.*\r?\n" "AUTHINFO PASS"
  1171. (buffer-substring (point) (point-at-eol))))))
  1172. ;;; Internal functions.
  1173. (defun nntp-handle-authinfo (process)
  1174. "Take care of an authinfo response from the server."
  1175. (let ((last nntp-last-command))
  1176. (funcall nntp-authinfo-function)
  1177. ;; We have to re-send the function that was interrupted by
  1178. ;; the authinfo request.
  1179. (nntp-erase-buffer nntp-server-buffer)
  1180. (nntp-send-string process last)))
  1181. (defun nntp-make-process-buffer (buffer)
  1182. "Create a new, fresh buffer usable for nntp process connections."
  1183. (with-current-buffer
  1184. (generate-new-buffer
  1185. (format " *server %s %s %s*"
  1186. nntp-address nntp-port-number
  1187. (gnus-buffer-exists-p buffer)))
  1188. (mm-disable-multibyte)
  1189. (set (make-local-variable 'after-change-functions) nil)
  1190. (set (make-local-variable 'nntp-process-wait-for) nil)
  1191. (set (make-local-variable 'nntp-process-callback) nil)
  1192. (set (make-local-variable 'nntp-process-to-buffer) nil)
  1193. (set (make-local-variable 'nntp-process-start-point) nil)
  1194. (set (make-local-variable 'nntp-process-decode) nil)
  1195. (set (make-local-variable 'nntp-retrieval-in-progress) nil)
  1196. (current-buffer)))
  1197. (defun nntp-open-connection (buffer)
  1198. "Open a connection to PORT on ADDRESS delivering output to BUFFER."
  1199. (run-hooks 'nntp-prepare-server-hook)
  1200. (let* ((pbuffer (nntp-make-process-buffer buffer))
  1201. (timer
  1202. (and nntp-connection-timeout
  1203. (run-at-time
  1204. nntp-connection-timeout nil
  1205. `(lambda ()
  1206. (nntp-kill-buffer ,pbuffer)))))
  1207. (process
  1208. (condition-case err
  1209. (let ((coding-system-for-read nntp-coding-system-for-read)
  1210. (coding-system-for-write nntp-coding-system-for-write)
  1211. (map '((nntp-open-network-stream network)
  1212. (network-only plain) ; compat
  1213. (nntp-open-plain-stream plain)
  1214. (nntp-open-ssl-stream tls)
  1215. (nntp-open-tls-stream tls))))
  1216. (if (assoc nntp-open-connection-function map)
  1217. (open-protocol-stream
  1218. "nntpd" pbuffer nntp-address nntp-port-number
  1219. :type (cadr (assoc nntp-open-connection-function map))
  1220. :end-of-command "^\\([2345]\\|[.]\\).*\n"
  1221. :capability-command "CAPABILITIES\r\n"
  1222. :success "^3"
  1223. :starttls-function
  1224. (lambda (capabilities)
  1225. (if (not (string-match "STARTTLS" capabilities))
  1226. nil
  1227. "STARTTLS\r\n")))
  1228. (funcall nntp-open-connection-function pbuffer)))
  1229. (error
  1230. (nnheader-report 'nntp ">>> %s" err))
  1231. (quit
  1232. (message "Quit opening connection to %s" nntp-address)
  1233. (nntp-kill-buffer pbuffer)
  1234. (signal 'quit nil)
  1235. nil))))
  1236. (when timer
  1237. (nnheader-cancel-timer timer))
  1238. (when (and process
  1239. (not (memq (process-status process) '(open run))))
  1240. (with-current-buffer pbuffer
  1241. (goto-char (point-min))
  1242. (nnheader-report 'nntp "Error when connecting: %s"
  1243. (buffer-substring (point) (line-end-position))))
  1244. (setq process nil))
  1245. (unless process
  1246. (nntp-kill-buffer pbuffer))
  1247. (when (and (buffer-name pbuffer)
  1248. process)
  1249. (when (and (fboundp 'set-network-process-option) ;; Unavailable in XEmacs.
  1250. (fboundp 'process-type) ;; Emacs 22 doesn't provide it.
  1251. (eq (process-type process) 'network))
  1252. ;; Use TCP-keepalive so that connections that pass through a NAT router
  1253. ;; don't hang when left idle.
  1254. (set-network-process-option process :keepalive t))
  1255. (gnus-set-process-query-on-exit-flag process nil)
  1256. (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
  1257. (memq (process-status process) '(open run)))
  1258. (prog1
  1259. (caar (push (list process buffer nil) nntp-connection-alist))
  1260. (push process nntp-connection-list)
  1261. (with-current-buffer pbuffer
  1262. (nntp-read-server-type)
  1263. (erase-buffer)
  1264. (set-buffer nntp-server-buffer)
  1265. (let ((nnheader-callback-function nil))
  1266. (run-hooks 'nntp-server-opened-hook)
  1267. (nntp-send-authinfo t))))
  1268. (nntp-kill-buffer (process-buffer process))
  1269. nil))))
  1270. (defun nntp-read-server-type ()
  1271. "Find out what the name of the server we have connected to is."
  1272. ;; Wait for the status string to arrive.
  1273. (setq nntp-server-type (buffer-string))
  1274. (let ((case-fold-search t))
  1275. ;; Run server-specific commands.
  1276. (dolist (entry nntp-server-action-alist)
  1277. (when (string-match (car entry) nntp-server-type)
  1278. (if (and (listp (cadr entry))
  1279. (not (eq 'lambda (caadr entry))))
  1280. (eval (cadr entry))
  1281. (funcall (cadr entry)))))))
  1282. (defun nntp-async-wait (process wait-for buffer decode callback)
  1283. (with-current-buffer (process-buffer process)
  1284. (unless nntp-inside-change-function
  1285. (erase-buffer))
  1286. (setq nntp-process-wait-for wait-for
  1287. nntp-process-to-buffer buffer
  1288. nntp-process-decode decode
  1289. nntp-process-callback callback
  1290. nntp-process-start-point (point-max))
  1291. (setq after-change-functions '(nntp-after-change-function))))
  1292. (defun nntp-async-timer-handler ()
  1293. (mapcar
  1294. (lambda (proc)
  1295. (if (memq (process-status proc) '(open run))
  1296. (nntp-async-trigger proc)
  1297. (nntp-async-stop proc)))
  1298. nntp-async-process-list))
  1299. (defun nntp-async-stop (proc)
  1300. (setq nntp-async-process-list (delq proc nntp-async-process-list))
  1301. (when (and nntp-async-timer (not nntp-async-process-list))
  1302. (nnheader-cancel-timer nntp-async-timer)
  1303. (setq nntp-async-timer nil)))
  1304. (defun nntp-after-change-function (beg end len)
  1305. (unwind-protect
  1306. ;; we only care about insertions at eob
  1307. (when (and (eq 0 len) (eq (point-max) end))
  1308. (save-match-data
  1309. (let ((proc (get-buffer-process (current-buffer))))
  1310. (when proc
  1311. (nntp-async-trigger proc)))))
  1312. ;; any throw from after-change-functions will leave it
  1313. ;; set to nil. so we reset it here, if necessary.
  1314. (when quit-flag
  1315. (setq after-change-functions '(nntp-after-change-function)))))
  1316. (defun nntp-async-trigger (process)
  1317. (with-current-buffer (process-buffer process)
  1318. (when nntp-process-callback
  1319. ;; do we have an error message?
  1320. (goto-char nntp-process-start-point)
  1321. (if (memq (following-char) '(?4 ?5))
  1322. ;; wants credentials?
  1323. (if (looking-at "480")
  1324. (nntp-handle-authinfo process)
  1325. ;; report error message.
  1326. (nntp-snarf-error-message)
  1327. (nntp-do-callback nil))
  1328. ;; got what we expect?
  1329. (goto-char (point-max))
  1330. (when (re-search-backward
  1331. nntp-process-wait-for nntp-process-start-point t)
  1332. (let ((response (match-string 0)))
  1333. (with-current-buffer nntp-server-buffer
  1334. (setq nntp-process-response response)))
  1335. (nntp-async-stop process)
  1336. ;; convert it.
  1337. (when (gnus-buffer-exists-p nntp-process-to-buffer)
  1338. (let ((buf (current-buffer))
  1339. (start nntp-process-start-point)
  1340. (decode nntp-process-decode))
  1341. (with-current-buffer nntp-process-to-buffer
  1342. (goto-char (point-max))
  1343. (save-restriction
  1344. (narrow-to-region (point) (point))
  1345. (nnheader-insert-buffer-substring buf start)
  1346. (when decode
  1347. (nntp-decode-text))))))
  1348. ;; report it.
  1349. (goto-char (point-max))
  1350. (nntp-do-callback
  1351. (buffer-name (get-buffer nntp-process-to-buffer))))))))
  1352. (defun nntp-do-callback (arg)
  1353. (let ((callback nntp-process-callback)
  1354. (nntp-inside-change-function t))
  1355. (setq nntp-process-callback nil)
  1356. (funcall callback arg)))
  1357. (defun nntp-snarf-error-message ()
  1358. "Save the error message in the current buffer."
  1359. (let ((message (buffer-string)))
  1360. (while (string-match "[\r\n]+" message)
  1361. (setq message (replace-match " " t t message)))
  1362. (nnheader-report 'nntp "%s" message)
  1363. message))
  1364. (defun nntp-accept-process-output (process)
  1365. "Wait for output from PROCESS and message some dots."
  1366. (with-current-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
  1367. nntp-server-buffer)
  1368. (let ((len (/ (buffer-size) 1024))
  1369. message-log-max)
  1370. (unless (< len 10)
  1371. (setq nntp-have-messaged t)
  1372. (nnheader-message 7 "nntp read: %dk" len)))
  1373. (prog1
  1374. (nnheader-accept-process-output process)
  1375. ;; accept-process-output may update status of process to indicate
  1376. ;; that the server has closed the connection. This MUST be
  1377. ;; handled here as the buffer restored by the save-excursion may
  1378. ;; be the process's former output buffer (i.e. now killed)
  1379. (or (and process
  1380. (memq (process-status process) '(open run)))
  1381. (nntp-report "Server closed connection")))))
  1382. (defun nntp-accept-response ()
  1383. "Wait for output from the process that outputs to BUFFER."
  1384. (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
  1385. (defun nntp-possibly-change-group (group server &optional connectionless)
  1386. (let ((nnheader-callback-function nil))
  1387. (when server
  1388. (or (nntp-server-opened server)
  1389. (nntp-open-server server nil connectionless)))
  1390. (unless connectionless
  1391. (or (nntp-find-connection nntp-server-buffer)
  1392. (nntp-open-connection nntp-server-buffer))))
  1393. (when group
  1394. (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
  1395. (cond ((not entry)
  1396. (nntp-report "Server closed connection"))
  1397. ((not (equal group (caddr entry)))
  1398. (with-current-buffer (process-buffer (car entry))
  1399. (erase-buffer)
  1400. (nntp-send-command "^[245].*\n" "GROUP" group)
  1401. (setcar (cddr entry) group)
  1402. (erase-buffer)
  1403. (nntp-erase-buffer nntp-server-buffer)))))))
  1404. (defun nntp-decode-text (&optional cr-only)
  1405. "Decode the text in the current buffer."
  1406. (goto-char (point-min))
  1407. (while (search-forward "\r" nil t)
  1408. (delete-char -1))
  1409. (unless cr-only
  1410. ;; Remove trailing ".\n" end-of-transfer marker.
  1411. (goto-char (point-max))
  1412. (forward-line -1)
  1413. (when (looking-at ".\n")
  1414. (delete-char 2))
  1415. ;; Delete status line.
  1416. (goto-char (point-min))
  1417. (while (looking-at "[1-5][0-9][0-9] .*\n")
  1418. ;; For some unknown reason, there is more than one status line.
  1419. (delete-region (point) (progn (forward-line 1) (point))))
  1420. ;; Remove "." -> ".." encoding.
  1421. (while (search-forward "\n.." nil t)
  1422. (delete-char -1))))
  1423. (defun nntp-encode-text ()
  1424. "Encode the text in the current buffer."
  1425. (save-excursion
  1426. ;; Replace "." at beginning of line with "..".
  1427. (goto-char (point-min))
  1428. (while (re-search-forward "^\\." nil t)
  1429. (insert "."))
  1430. (goto-char (point-max))
  1431. ;; Insert newline at the end of the buffer.
  1432. (unless (bolp)
  1433. (insert "\n"))
  1434. ;; Insert `.' at end of buffer (end of text mark).
  1435. (goto-char (point-max))
  1436. (insert ".\n")
  1437. (goto-char (point-min))
  1438. (while (not (eobp))
  1439. (end-of-line)
  1440. (delete-char 1)
  1441. (insert nntp-end-of-line))))
  1442. (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
  1443. (set-buffer nntp-server-buffer)
  1444. (erase-buffer)
  1445. (cond
  1446. ;; This server does not talk NOV.
  1447. ((not nntp-server-xover)
  1448. nil)
  1449. ;; We don't care about gaps.
  1450. ((or (not nntp-nov-gap)
  1451. fetch-old)
  1452. (nntp-send-xover-command
  1453. (if fetch-old
  1454. (if (numberp fetch-old)
  1455. (max 1 (- (car articles) fetch-old))
  1456. 1)
  1457. (car articles))
  1458. (car (last articles)) 'wait)
  1459. (goto-char (point-min))
  1460. (when (looking-at "[1-5][0-9][0-9] .*\n")
  1461. (delete-region (point) (progn (forward-line 1) (point))))
  1462. (while (search-forward "\r" nil t)
  1463. (replace-match "" t t))
  1464. (goto-char (point-max))
  1465. (forward-line -1)
  1466. (when (looking-at "\\.")
  1467. (delete-region (point) (progn (forward-line 1) (point)))))
  1468. ;; We do it the hard way. For each gap, an XOVER command is sent
  1469. ;; to the server. We do not wait for a reply from the server, we
  1470. ;; just send them off as fast as we can. That means that we have
  1471. ;; to count the number of responses we get back to find out when we
  1472. ;; have gotten all we asked for.
  1473. ((numberp nntp-nov-gap)
  1474. (let ((count 0)
  1475. (received 0)
  1476. last-point
  1477. in-process-buffer-p
  1478. (buf nntp-server-buffer)
  1479. (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
  1480. first last status)
  1481. ;; We have to check `nntp-server-xover'. If it gets set to nil,
  1482. ;; that means that the server does not understand XOVER, but we
  1483. ;; won't know that until we try.
  1484. (while (and nntp-server-xover articles)
  1485. (setq first (car articles))
  1486. ;; Search forward until we find a gap, or until we run out of
  1487. ;; articles.
  1488. (while (and (cdr articles)
  1489. (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
  1490. (setq articles (cdr articles)))
  1491. (setq in-process-buffer-p (stringp nntp-server-xover))
  1492. (nntp-send-xover-command first (setq last (car articles)))
  1493. (setq articles (cdr articles))
  1494. (when (and nntp-server-xover in-process-buffer-p)
  1495. ;; Don't count tried request.
  1496. (setq count (1+ count))
  1497. ;; Every 400 requests we have to read the stream in
  1498. ;; order to avoid deadlocks.
  1499. (when (or (null articles) ;All requests have been sent.
  1500. (= 1 (% count nntp-maximum-request)))
  1501. (nntp-accept-response)
  1502. ;; On some Emacs versions the preceding function has a
  1503. ;; tendency to change the buffer. Perhaps. It's quite
  1504. ;; difficult to reproduce, because it only seems to happen
  1505. ;; once in a blue moon.
  1506. (set-buffer process-buffer)
  1507. (while (progn
  1508. (goto-char (or last-point (point-min)))
  1509. ;; Count replies.
  1510. (while (re-search-forward "^\\([0-9][0-9][0-9]\\) .*\n"
  1511. nil t)
  1512. (incf received)
  1513. (setq status (match-string 1))
  1514. (if (string-match "^[45]" status)
  1515. (setq status 'error)
  1516. (setq status 'ok)))
  1517. (setq last-point (point))
  1518. (or (< received count)
  1519. (if (eq status 'error)
  1520. nil
  1521. ;; I haven't started reading the final response
  1522. (progn
  1523. (goto-char (point-max))
  1524. (forward-line -1)
  1525. (not (looking-at "^\\.\r?\n"))))))
  1526. ;; I haven't read the end of the final response
  1527. (nntp-accept-response)
  1528. (set-buffer process-buffer))))
  1529. ;; Some nntp servers seem to have an extension to the XOVER
  1530. ;; extension. On these servers, requesting an article range
  1531. ;; preceding the active range does not return an error as
  1532. ;; specified in the RFC. What we instead get is the NOV entry
  1533. ;; for the first available article. Obviously, a client can
  1534. ;; use that entry to avoid making unnecessary requests. The
  1535. ;; only problem is for a client that assumes that the response
  1536. ;; will always be within the requested range. For such a
  1537. ;; client, we can get N copies of the same entry (one for each
  1538. ;; XOVER command sent to the server).
  1539. (when (<= count 1)
  1540. (goto-char (point-min))
  1541. (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
  1542. (let ((low-limit (string-to-number
  1543. (buffer-substring (match-beginning 1)
  1544. (match-end 1)))))
  1545. (while (and articles (<= (car articles) low-limit))
  1546. (setq articles (cdr articles))))))
  1547. (set-buffer buf))
  1548. (when nntp-server-xover
  1549. (when in-process-buffer-p
  1550. (set-buffer buf)
  1551. (goto-char (point-max))
  1552. (nnheader-insert-buffer-substring process-buffer)
  1553. (set-buffer process-buffer)
  1554. (erase-buffer)
  1555. (set-buffer buf))
  1556. ;; We remove any "." lines and status lines.
  1557. (goto-char (point-min))
  1558. (while (search-forward "\r" nil t)
  1559. (delete-char -1))
  1560. (goto-char (point-min))
  1561. (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
  1562. t))))
  1563. nntp-server-xover)
  1564. (defun nntp-send-xover-command (beg end &optional wait-for-reply)
  1565. "Send the XOVER command to the server."
  1566. (let ((range (format "%d-%d" beg end))
  1567. (nntp-inhibit-erase t))
  1568. (if (stringp nntp-server-xover)
  1569. ;; If `nntp-server-xover' is a string, then we just send this
  1570. ;; command.
  1571. (if wait-for-reply
  1572. (nntp-send-command-nodelete
  1573. "\r?\n\\.\r?\n" nntp-server-xover range)
  1574. ;; We do not wait for the reply.
  1575. (nntp-send-command-nodelete nil nntp-server-xover range))
  1576. (let ((commands nntp-xover-commands))
  1577. ;; `nntp-xover-commands' is a list of possible XOVER commands.
  1578. ;; We try them all until we get at positive response.
  1579. (while (and commands (eq nntp-server-xover 'try))
  1580. (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
  1581. (with-current-buffer nntp-server-buffer
  1582. (goto-char (point-min))
  1583. (and (looking-at "[23]") ; No error message.
  1584. ;; We also have to look at the lines. Some buggy
  1585. ;; servers give back simple lines with just the
  1586. ;; article number. How... helpful.
  1587. (progn
  1588. (forward-line 1)
  1589. ;; More text after number, or a dot.
  1590. (looking-at "[0-9]+\t...\\|\\.\r?\n"))
  1591. (setq nntp-server-xover (car commands))))
  1592. (setq commands (cdr commands)))
  1593. ;; If none of the commands worked, we disable XOVER.
  1594. (when (eq nntp-server-xover 'try)
  1595. (nntp-erase-buffer nntp-server-buffer)
  1596. (setq nntp-server-xover nil))
  1597. nntp-server-xover))))
  1598. (defun nntp-find-group-and-number (&optional group)
  1599. (save-excursion
  1600. (save-restriction
  1601. ;; FIXME: This is REALLY FISHY: set-buffer after save-restriction?!?
  1602. (set-buffer nntp-server-buffer)
  1603. (narrow-to-region (goto-char (point-min))
  1604. (or (search-forward "\n\n" nil t) (point-max)))
  1605. (goto-char (point-min))
  1606. ;; We first find the number by looking at the status line.
  1607. (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
  1608. (string-to-number
  1609. (buffer-substring (match-beginning 1)
  1610. (match-end 1)))))
  1611. newsgroups xref)
  1612. (and number (zerop number) (setq number nil))
  1613. (if number
  1614. ;; Then we find the group name.
  1615. (setq group
  1616. (cond
  1617. ;; If there is only one group in the Newsgroups
  1618. ;; header, then it seems quite likely that this
  1619. ;; article comes from that group, I'd say.
  1620. ((and (setq newsgroups
  1621. (mail-fetch-field "newsgroups"))
  1622. (not (string-match "," newsgroups)))
  1623. newsgroups)
  1624. ;; If there is more than one group in the
  1625. ;; Newsgroups header, then the Xref header should
  1626. ;; be filled out. We hazard a guess that the group
  1627. ;; that has this article number in the Xref header
  1628. ;; is the one we are looking for. This might very
  1629. ;; well be wrong if this article happens to have
  1630. ;; the same number in several groups, but that's
  1631. ;; life.
  1632. ((and (setq xref (mail-fetch-field "xref"))
  1633. number
  1634. (string-match
  1635. (format "\\([^ :]+\\):%d" number) xref))
  1636. (match-string 1 xref))
  1637. (t "")))
  1638. (cond
  1639. ((and (not nntp-xref-number-is-evil)
  1640. (setq xref (mail-fetch-field "xref"))
  1641. (string-match
  1642. (if group
  1643. (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
  1644. "\\([^ :]+\\):\\([0-9]+\\)")
  1645. xref))
  1646. (setq group (match-string 1 xref)
  1647. number (string-to-number (match-string 2 xref))))
  1648. ((and (setq newsgroups
  1649. (mail-fetch-field "newsgroups"))
  1650. (not (string-match "," newsgroups)))
  1651. (setq group newsgroups))
  1652. (group)
  1653. (t (setq group ""))))
  1654. (when (string-match "\r" group)
  1655. (setq group (substring group 0 (match-beginning 0))))
  1656. (cons group number)))))
  1657. (defun nntp-wait-for-string (regexp)
  1658. "Wait until string arrives in the buffer."
  1659. (let ((buf (current-buffer))
  1660. proc)
  1661. (goto-char (point-min))
  1662. (while (and (setq proc (get-buffer-process buf))
  1663. (memq (process-status proc) '(open run))
  1664. (not (re-search-forward regexp nil t)))
  1665. (accept-process-output proc 0.1)
  1666. (set-buffer buf)
  1667. (goto-char (point-min)))))
  1668. ;; ==========================================================================
  1669. ;; Obsolete nntp-open-* connection methods -- drv
  1670. ;; ==========================================================================
  1671. (defvoo nntp-open-telnet-envuser nil
  1672. "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
  1673. (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
  1674. "*Regular expression to match the shell prompt on the remote machine.")
  1675. (defvoo nntp-rlogin-program "rsh"
  1676. "*Program used to log in on remote machines.
  1677. The default is \"rsh\", but \"ssh\" is a popular alternative.")
  1678. (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
  1679. "*Parameters to `nntp-open-rlogin'.
  1680. That function may be used as `nntp-open-connection-function'. In that
  1681. case, this list will be used as the parameter list given to rsh.")
  1682. (defvoo nntp-rlogin-user-name nil
  1683. "*User name on remote system when using the rlogin connect method.")
  1684. (defvoo nntp-telnet-parameters
  1685. '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
  1686. "*Parameters to `nntp-open-telnet'.
  1687. That function may be used as `nntp-open-connection-function'. In that
  1688. case, this list will be executed as a command after logging in
  1689. via telnet.")
  1690. (defvoo nntp-telnet-user-name nil
  1691. "User name to log in via telnet with.")
  1692. (defvoo nntp-telnet-passwd nil
  1693. "Password to use to log in via telnet with.")
  1694. (defun nntp-service-to-port (svc)
  1695. (cond
  1696. ((integerp svc) (number-to-string svc))
  1697. ((string-match "\\`[0-9]+\\'" svc) svc)
  1698. (t
  1699. (with-temp-buffer
  1700. (ignore-errors (insert-file-contents "/etc/services"))
  1701. (goto-char (point-min))
  1702. (if (re-search-forward (concat "^" (regexp-quote svc)
  1703. "[ \t]+\\([0-9]+\\)/tcp"))
  1704. (match-string 1)
  1705. svc)))))
  1706. (defun nntp-open-telnet (buffer)
  1707. (with-current-buffer buffer
  1708. (erase-buffer)
  1709. (let ((proc (apply
  1710. 'start-process
  1711. "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
  1712. (case-fold-search t))
  1713. (when (memq (process-status proc) '(open run))
  1714. (nntp-wait-for-string "^r?telnet")
  1715. (process-send-string proc "set escape \^X\n")
  1716. (cond
  1717. ((and nntp-open-telnet-envuser nntp-telnet-user-name)
  1718. (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
  1719. nntp-address "\n")))
  1720. (t
  1721. (process-send-string proc (concat "open " nntp-address "\n"))))
  1722. (cond
  1723. ((not nntp-open-telnet-envuser)
  1724. (nntp-wait-for-string "^\r*.?login:")
  1725. (process-send-string
  1726. proc (concat
  1727. (or nntp-telnet-user-name
  1728. (setq nntp-telnet-user-name (read-string "login: ")))
  1729. "\n"))))
  1730. (nntp-wait-for-string "^\r*.?password:")
  1731. (process-send-string
  1732. proc (concat
  1733. (or nntp-telnet-passwd
  1734. (setq nntp-telnet-passwd
  1735. (read-passwd "Password: ")))
  1736. "\n"))
  1737. (nntp-wait-for-string nntp-telnet-shell-prompt)
  1738. (process-send-string
  1739. proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
  1740. (nntp-wait-for-string "^\r*20[01]")
  1741. (beginning-of-line)
  1742. (delete-region (point-min) (point))
  1743. (process-send-string proc "\^]")
  1744. (nntp-wait-for-string "^r?telnet")
  1745. (process-send-string proc "mode character\n")
  1746. (accept-process-output proc 1)
  1747. (sit-for 1)
  1748. (goto-char (point-min))
  1749. (forward-line 1)
  1750. (delete-region (point) (point-max)))
  1751. proc)))
  1752. (defun nntp-open-rlogin (buffer)
  1753. "Open a connection to SERVER using rsh."
  1754. (let ((proc (if nntp-rlogin-user-name
  1755. (apply 'start-process
  1756. "nntpd" buffer nntp-rlogin-program
  1757. nntp-address "-l" nntp-rlogin-user-name
  1758. nntp-rlogin-parameters)
  1759. (apply 'start-process
  1760. "nntpd" buffer nntp-rlogin-program nntp-address
  1761. nntp-rlogin-parameters))))
  1762. (with-current-buffer buffer
  1763. (nntp-wait-for-string "^\r*20[01]")
  1764. (beginning-of-line)
  1765. (delete-region (point-min) (point))
  1766. proc)))
  1767. ;; ==========================================================================
  1768. ;; Replacements for the nntp-open-* functions -- drv
  1769. ;; ==========================================================================
  1770. (defun nntp-open-telnet-stream (buffer)
  1771. "Open a nntp connection by telnet'ing the news server.
  1772. `nntp-open-netcat-stream' is recommended in place of this function
  1773. because it is more reliable.
  1774. Please refer to the following variables to customize the connection:
  1775. - `nntp-pre-command',
  1776. - `nntp-telnet-command',
  1777. - `nntp-telnet-switches',
  1778. - `nntp-address',
  1779. - `nntp-port-number',
  1780. - `nntp-end-of-line'."
  1781. (let ((command `(,nntp-telnet-command
  1782. ,@nntp-telnet-switches
  1783. ,nntp-address
  1784. ,(nntp-service-to-port nntp-port-number)))
  1785. proc)
  1786. (and nntp-pre-command
  1787. (push nntp-pre-command command))
  1788. (setq proc (apply 'start-process "nntpd" buffer command))
  1789. (with-current-buffer buffer
  1790. (nntp-wait-for-string "^\r*20[01]")
  1791. (beginning-of-line)
  1792. (delete-region (point-min) (point))
  1793. proc)))
  1794. (defun nntp-open-via-rlogin-and-telnet (buffer)
  1795. "Open a connection to an nntp server through an intermediate host.
  1796. First rlogin to the remote host, and then telnet the real news server
  1797. from there.
  1798. `nntp-open-via-rlogin-and-netcat' is recommended in place of this function
  1799. because it is more reliable.
  1800. Please refer to the following variables to customize the connection:
  1801. - `nntp-pre-command',
  1802. - `nntp-via-rlogin-command',
  1803. - `nntp-via-rlogin-command-switches',
  1804. - `nntp-via-user-name',
  1805. - `nntp-via-address',
  1806. - `nntp-telnet-command',
  1807. - `nntp-telnet-switches',
  1808. - `nntp-address',
  1809. - `nntp-port-number',
  1810. - `nntp-end-of-line'."
  1811. (let ((command `(,nntp-via-address
  1812. ,nntp-telnet-command
  1813. ,@nntp-telnet-switches))
  1814. proc)
  1815. (when nntp-via-user-name
  1816. (setq command `("-l" ,nntp-via-user-name ,@command)))
  1817. (when nntp-via-rlogin-command-switches
  1818. (setq command (append nntp-via-rlogin-command-switches command)))
  1819. (push nntp-via-rlogin-command command)
  1820. (and nntp-pre-command
  1821. (push nntp-pre-command command))
  1822. (setq proc (apply 'start-process "nntpd" buffer command))
  1823. (with-current-buffer buffer
  1824. (nntp-wait-for-string "^r?telnet")
  1825. (process-send-string proc (concat "open " nntp-address " "
  1826. (nntp-service-to-port nntp-port-number)
  1827. "\n"))
  1828. (nntp-wait-for-string "^\r*20[01]")
  1829. (beginning-of-line)
  1830. (delete-region (point-min) (point))
  1831. (process-send-string proc "\^]")
  1832. (nntp-wait-for-string "^r?telnet")
  1833. (process-send-string proc "mode character\n")
  1834. (accept-process-output proc 1)
  1835. (sit-for 1)
  1836. (goto-char (point-min))
  1837. (forward-line 1)
  1838. (delete-region (point) (point-max)))
  1839. proc))
  1840. (defun nntp-open-via-rlogin-and-netcat (buffer)
  1841. "Open a connection to an nntp server through an intermediate host.
  1842. First rlogin to the remote host, and then connect to the real news
  1843. server from there using the netcat command.
  1844. Please refer to the following variables to customize the connection:
  1845. - `nntp-pre-command',
  1846. - `nntp-via-rlogin-command',
  1847. - `nntp-via-rlogin-command-switches',
  1848. - `nntp-via-user-name',
  1849. - `nntp-via-address',
  1850. - `nntp-netcat-command',
  1851. - `nntp-netcat-switches',
  1852. - `nntp-address',
  1853. - `nntp-port-number'."
  1854. (let ((command `(,@(when nntp-pre-command
  1855. (list nntp-pre-command))
  1856. ,nntp-via-rlogin-command
  1857. ,@nntp-via-rlogin-command-switches
  1858. ,@(when nntp-via-user-name
  1859. (list "-l" nntp-via-user-name))
  1860. ,nntp-via-address
  1861. ,nntp-netcat-command
  1862. ,@nntp-netcat-switches
  1863. ,nntp-address
  1864. ,(nntp-service-to-port nntp-port-number))))
  1865. ;; A non-nil connection type results in mightily odd behavior where
  1866. ;; (process-send-string proc "\^M") ends up sending a "\n" to the
  1867. ;; ssh process. --Stef
  1868. ;; Also a nil connection allow ssh-askpass to work under X11.
  1869. (let ((process-connection-type nil))
  1870. (apply 'start-process "nntpd" buffer command))))
  1871. (defun nntp-open-netcat-stream (buffer)
  1872. "Open a connection to an nntp server through netcat.
  1873. I.e. use the `nc' command rather than Emacs's builtin networking code.
  1874. Please refer to the following variables to customize the connection:
  1875. - `nntp-pre-command',
  1876. - `nntp-netcat-command',
  1877. - `nntp-netcat-switches',
  1878. - `nntp-address',
  1879. - `nntp-port-number'."
  1880. (let ((command `(,nntp-netcat-command
  1881. ,@nntp-netcat-switches
  1882. ,nntp-address
  1883. ,(nntp-service-to-port nntp-port-number))))
  1884. (and nntp-pre-command (push nntp-pre-command command))
  1885. (let ((process-connection-type nil)) ;See `nntp-open-via-rlogin-and-netcat'.
  1886. (apply 'start-process "nntpd" buffer command))))
  1887. (defun nntp-open-via-telnet-and-telnet (buffer)
  1888. "Open a connection to an nntp server through an intermediate host.
  1889. First telnet the remote host, and then telnet the real news server
  1890. from there.
  1891. Please refer to the following variables to customize the connection:
  1892. - `nntp-pre-command',
  1893. - `nntp-via-telnet-command',
  1894. - `nntp-via-telnet-switches',
  1895. - `nntp-via-address',
  1896. - `nntp-via-envuser',
  1897. - `nntp-via-user-name',
  1898. - `nntp-via-user-password',
  1899. - `nntp-via-shell-prompt',
  1900. - `nntp-telnet-command',
  1901. - `nntp-telnet-switches',
  1902. - `nntp-address',
  1903. - `nntp-port-number',
  1904. - `nntp-end-of-line'."
  1905. (with-current-buffer buffer
  1906. (erase-buffer)
  1907. (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
  1908. (case-fold-search t)
  1909. proc)
  1910. (and nntp-pre-command (push nntp-pre-command command))
  1911. (setq proc (apply 'start-process "nntpd" buffer command))
  1912. (when (memq (process-status proc) '(open run))
  1913. (nntp-wait-for-string "^r?telnet")
  1914. (process-send-string proc "set escape \^X\n")
  1915. (cond
  1916. ((and nntp-via-envuser nntp-via-user-name)
  1917. (process-send-string proc (concat "open " "-l" nntp-via-user-name
  1918. nntp-via-address "\n")))
  1919. (t
  1920. (process-send-string proc (concat "open " nntp-via-address
  1921. "\n"))))
  1922. (when (not nntp-via-envuser)
  1923. (nntp-wait-for-string "^\r*.?login:")
  1924. (process-send-string proc
  1925. (concat
  1926. (or nntp-via-user-name
  1927. (setq nntp-via-user-name
  1928. (read-string "login: ")))
  1929. "\n")))
  1930. (nntp-wait-for-string "^\r*.?password:")
  1931. (process-send-string proc
  1932. (concat
  1933. (or nntp-via-user-password
  1934. (setq nntp-via-user-password
  1935. (read-passwd "Password: ")))
  1936. "\n"))
  1937. (nntp-wait-for-string nntp-via-shell-prompt)
  1938. (let ((real-telnet-command `("exec"
  1939. ,nntp-telnet-command
  1940. ,@nntp-telnet-switches
  1941. ,nntp-address
  1942. ,(nntp-service-to-port nntp-port-number))))
  1943. (process-send-string proc
  1944. (concat (mapconcat 'identity
  1945. real-telnet-command " ")
  1946. "\n")))
  1947. (nntp-wait-for-string "^\r*20[01]")
  1948. (beginning-of-line)
  1949. (delete-region (point-min) (point))
  1950. (process-send-string proc "\^]")
  1951. (nntp-wait-for-string "^r?telnet")
  1952. (process-send-string proc "mode character\n")
  1953. (accept-process-output proc 1)
  1954. (sit-for 1)
  1955. (goto-char (point-min))
  1956. (forward-line 1)
  1957. (delete-region (point) (point-max)))
  1958. proc)))
  1959. ;; Marks handling
  1960. (defun nntp-marks-directory (server)
  1961. (expand-file-name server nntp-marks-directory))
  1962. (defvar nntp-server-to-method-cache nil
  1963. "Alist of servers and select methods.")
  1964. (defun nntp-group-pathname (server group &optional file)
  1965. "Return an absolute file name of FILE for GROUP on SERVER."
  1966. (let ((method (cdr (assoc server nntp-server-to-method-cache))))
  1967. (unless method
  1968. (push (cons server (setq method (or (gnus-server-to-method server)
  1969. (gnus-find-method-for-group group))))
  1970. nntp-server-to-method-cache))
  1971. (nnmail-group-pathname
  1972. (mm-decode-coding-string group
  1973. (inline (gnus-group-name-charset method group)))
  1974. (nntp-marks-directory server)
  1975. file)))
  1976. (defun nntp-possibly-create-directory (group server)
  1977. (let ((dir (nntp-group-pathname server group))
  1978. (file-name-coding-system nnmail-pathname-coding-system))
  1979. (unless (file-exists-p dir)
  1980. (make-directory (directory-file-name dir) t)
  1981. (nnheader-message 5 "Creating nntp marks directory %s" dir))))
  1982. (autoload 'time-less-p "time-date")
  1983. (defun nntp-marks-changed-p (group server)
  1984. (let ((file (nntp-group-pathname server group nntp-marks-file-name))
  1985. (file-name-coding-system nnmail-pathname-coding-system))
  1986. (if (null (gnus-gethash file nntp-marks-modtime))
  1987. t ;; never looked at marks file, assume it has changed
  1988. (time-less-p (gnus-gethash file nntp-marks-modtime)
  1989. (nth 5 (file-attributes file))))))
  1990. (defun nntp-save-marks (group server)
  1991. (let ((file-name-coding-system nnmail-pathname-coding-system)
  1992. (file (nntp-group-pathname server group nntp-marks-file-name)))
  1993. (condition-case err
  1994. (progn
  1995. (nntp-possibly-create-directory group server)
  1996. (with-temp-file file
  1997. (erase-buffer)
  1998. (gnus-prin1 nntp-marks)
  1999. (insert "\n"))
  2000. (gnus-sethash file
  2001. (nth 5 (file-attributes file))
  2002. nntp-marks-modtime))
  2003. (error (or (gnus-yes-or-no-p
  2004. (format "Could not write to %s (%s). Continue? " file err))
  2005. (error "Cannot write to %s (%s)" file err))))))
  2006. (defun nntp-open-marks (group server)
  2007. (let ((file (nntp-group-pathname server group nntp-marks-file-name))
  2008. (file-name-coding-system nnmail-pathname-coding-system))
  2009. (if (file-exists-p file)
  2010. (condition-case err
  2011. (with-temp-buffer
  2012. (gnus-sethash file (nth 5 (file-attributes file))
  2013. nntp-marks-modtime)
  2014. (nnheader-insert-file-contents file)
  2015. (setq nntp-marks (read (current-buffer)))
  2016. (dolist (el gnus-article-unpropagated-mark-lists)
  2017. (setq nntp-marks (gnus-remassoc el nntp-marks))))
  2018. (error (or (gnus-yes-or-no-p
  2019. (format "Error reading nntp marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
  2020. (error "Cannot read nntp marks file %s (%s)" file err))))
  2021. ;; User didn't have a .marks file. Probably first time
  2022. ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
  2023. (let ((info (gnus-get-info
  2024. (gnus-group-prefixed-name
  2025. group
  2026. (gnus-server-to-method (format "nntp:%s" server)))))
  2027. (decoded-name (mm-decode-coding-string
  2028. group
  2029. (gnus-group-name-charset
  2030. (gnus-server-to-method server) group))))
  2031. (nnheader-message 7 "Bootstrapping marks for %s..." decoded-name)
  2032. (setq nntp-marks (gnus-info-marks info))
  2033. (push (cons 'read (gnus-info-read info)) nntp-marks)
  2034. (dolist (el gnus-article-unpropagated-mark-lists)
  2035. (setq nntp-marks (gnus-remassoc el nntp-marks)))
  2036. (nntp-save-marks group server)
  2037. (nnheader-message 7 "Bootstrapping marks for %s...done"
  2038. decoded-name)))))
  2039. (provide 'nntp)
  2040. ;;; nntp.el ends here