nntp.el 75 KB

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