gnus-int.el 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. ;;; gnus-int.el --- backend interface functions for Gnus
  2. ;; Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: news
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (require 'gnus)
  20. (require 'message)
  21. (require 'gnus-range)
  22. (autoload 'gnus-run-hook-with-args "gnus-util")
  23. (autoload 'gnus-agent-expire "gnus-agent")
  24. (autoload 'gnus-agent-regenerate-group "gnus-agent")
  25. (autoload 'gnus-agent-read-servers-validate-native "gnus-agent")
  26. (autoload 'gnus-agent-possibly-synchronize-flags-server "gnus-agent")
  27. (defcustom gnus-open-server-hook nil
  28. "Hook called just before opening connection to the news server."
  29. :group 'gnus-start
  30. :type 'hook)
  31. (defcustom gnus-after-set-mark-hook nil
  32. "Hook called just after marks are set in a group."
  33. :version "24.1"
  34. :group 'gnus-start
  35. :type 'hook)
  36. (defcustom gnus-before-update-mark-hook nil
  37. "Hook called just before marks are updated in a group."
  38. :version "24.1"
  39. :group 'gnus-start
  40. :type 'hook)
  41. (defcustom gnus-server-unopen-status nil
  42. "The default status if the server is not able to open.
  43. If the server is covered by Gnus agent, the possible values are
  44. `denied', set the server denied; `offline', set the server offline;
  45. nil, ask user. If the server is not covered by Gnus agent, set the
  46. server denied."
  47. :version "22.1"
  48. :group 'gnus-start
  49. :type '(choice (const :tag "Ask" nil)
  50. (const :tag "Deny server" denied)
  51. (const :tag "Unplug Agent" offline)))
  52. (defcustom gnus-nntp-server nil
  53. "The name of the host running the NNTP server."
  54. :group 'gnus-server
  55. :type '(choice (const :tag "disable" nil)
  56. string))
  57. (make-obsolete-variable 'gnus-nntp-server 'gnus-select-method "24.1")
  58. (defvar gnus-internal-registry-spool-current-method nil
  59. "The current method, for the registry.")
  60. (defun gnus-server-opened (gnus-command-method)
  61. "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
  62. (unless (eq (gnus-server-status gnus-command-method)
  63. 'denied)
  64. (when (stringp gnus-command-method)
  65. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  66. (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
  67. (nth 1 gnus-command-method))))
  68. (defun gnus-status-message (gnus-command-method)
  69. "Return the status message from GNUS-COMMAND-METHOD.
  70. If GNUS-COMMAND-METHOD is a string, it is interpreted as a group
  71. name. The method this group uses will be queried."
  72. (let ((gnus-command-method
  73. (if (stringp gnus-command-method)
  74. (gnus-find-method-for-group gnus-command-method)
  75. gnus-command-method)))
  76. (funcall (gnus-get-function gnus-command-method 'status-message)
  77. (nth 1 gnus-command-method))))
  78. ;;;
  79. ;;; Server Communication
  80. ;;;
  81. (defun gnus-start-news-server (&optional confirm)
  82. "Open a method for getting news.
  83. If CONFIRM is non-nil, the user will be asked for an NNTP server."
  84. (let (how)
  85. (if gnus-current-select-method
  86. ;; Stream is already opened.
  87. nil
  88. ;; Open NNTP server.
  89. (when confirm
  90. ;; Read server name with completion.
  91. (setq gnus-nntp-server
  92. (gnus-completing-read "NNTP server"
  93. (cons gnus-nntp-server
  94. (if (boundp 'gnus-secondary-servers)
  95. gnus-secondary-servers))
  96. nil gnus-nntp-server)))
  97. (when (and gnus-nntp-server
  98. (stringp gnus-nntp-server)
  99. (not (string= gnus-nntp-server "")))
  100. (setq gnus-select-method
  101. (cond ((or (string= gnus-nntp-server "")
  102. (string= gnus-nntp-server "::"))
  103. (list 'nnspool (system-name)))
  104. ((string-match "^:" gnus-nntp-server)
  105. (list 'nnmh gnus-nntp-server
  106. (list 'nnmh-directory
  107. (file-name-as-directory
  108. (expand-file-name
  109. (substring gnus-nntp-server 1) "~/")))
  110. (list 'nnmh-get-new-mail nil)))
  111. (t
  112. (list 'nntp gnus-nntp-server)))))
  113. (setq how (car gnus-select-method))
  114. (cond
  115. ((eq how 'nnspool)
  116. (require 'nnspool)
  117. (gnus-message 5 "Looking up local news spool..."))
  118. ((eq how 'nnmh)
  119. (require 'nnmh)
  120. (gnus-message 5 "Looking up mh spool..."))
  121. (t
  122. (require 'nntp)))
  123. (setq gnus-current-select-method gnus-select-method)
  124. (gnus-run-hooks 'gnus-open-server-hook)
  125. ;; Partially validate agent covered methods now that the
  126. ;; gnus-select-method is known.
  127. (if gnus-agent
  128. ;; NOTE: This is here for one purpose only. By validating
  129. ;; the current select method, it converts the old 5.10.3,
  130. ;; and earlier, format to the current format. That enables
  131. ;; the agent code within gnus-open-server to function
  132. ;; correctly.
  133. (gnus-agent-read-servers-validate-native gnus-select-method))
  134. (or
  135. ;; gnus-open-server-hook might have opened it
  136. (gnus-server-opened gnus-select-method)
  137. (gnus-open-server gnus-select-method)
  138. gnus-batch-mode
  139. (gnus-y-or-n-p
  140. (gnus-format-message
  141. "%s (%s) open error: `%s'. Continue? "
  142. (car gnus-select-method) (cadr gnus-select-method)
  143. (gnus-status-message gnus-select-method)))
  144. (gnus-error 1 "Couldn't open server on %s"
  145. (nth 1 gnus-select-method))))))
  146. (defun gnus-check-group (group)
  147. "Try to make sure that the server where GROUP exists is alive."
  148. (let ((method (gnus-find-method-for-group group)))
  149. (or (gnus-server-opened method)
  150. (gnus-open-server method))))
  151. (defun gnus-check-server (&optional method silent)
  152. "Check whether the connection to METHOD is down.
  153. If METHOD is nil, use `gnus-select-method'.
  154. If it is down, start it up (again)."
  155. (let ((method (or method gnus-select-method))
  156. result)
  157. ;; Transform virtual server names into select methods.
  158. (when (stringp method)
  159. (setq method (gnus-server-to-method method)))
  160. (if (gnus-server-opened method)
  161. ;; The stream is already opened.
  162. t
  163. ;; Open the server.
  164. (unless silent
  165. (gnus-message 5 "Opening %s server%s..." (car method)
  166. (if (equal (nth 1 method) "") ""
  167. (format " on %s" (nth 1 method)))))
  168. (gnus-run-hooks 'gnus-open-server-hook)
  169. (prog1
  170. (setq result (gnus-open-server method))
  171. (unless silent
  172. (gnus-message
  173. (if result 5 3)
  174. "Opening %s server%s...%s" (car method)
  175. (if (equal (nth 1 method) "") ""
  176. (format " on %s" (nth 1 method)))
  177. (if result
  178. "done"
  179. (format "failed: %s"
  180. (nnheader-get-report-string (car method))))))))))
  181. (defun gnus-get-function (method function &optional noerror)
  182. "Return a function symbol based on METHOD and FUNCTION."
  183. ;; Translate server names into methods.
  184. (unless method
  185. (error "Attempted use of a nil select method"))
  186. (when (stringp method)
  187. (setq method (gnus-server-to-method method)))
  188. ;; Check cache of constructed names.
  189. (let* ((method-sym (if gnus-agent
  190. (inline (gnus-agent-get-function method))
  191. (car method)))
  192. (method-fns (get method-sym 'gnus-method-functions))
  193. (func (let ((method-fnlist-elt (assq function method-fns)))
  194. (unless method-fnlist-elt
  195. (setq method-fnlist-elt
  196. (cons function
  197. (intern (format "%s-%s" method-sym function))))
  198. (put method-sym 'gnus-method-functions
  199. (cons method-fnlist-elt method-fns)))
  200. (cdr method-fnlist-elt))))
  201. ;; Maybe complain if there is no function.
  202. (unless (fboundp func)
  203. (unless (car method)
  204. (error "Trying to require a method that doesn't exist"))
  205. (require (car method))
  206. (when (not (fboundp func))
  207. (if noerror
  208. (setq func nil)
  209. (error "No such function: %s" func))))
  210. func))
  211. ;;;
  212. ;;; Interface functions to the backends.
  213. ;;;
  214. (defun gnus-method-denied-p (method)
  215. (eq (nth 1 (assoc method gnus-opened-servers))
  216. 'denied))
  217. (defvar gnus-backend-trace nil)
  218. (defvar gnus-backend-trace-elapsed nil)
  219. (defun gnus-backend-trace (type form)
  220. (when gnus-backend-trace
  221. (with-current-buffer (get-buffer-create "*gnus trace*")
  222. (buffer-disable-undo)
  223. (goto-char (point-max))
  224. (insert (format-time-string "%H:%M:%S")
  225. (format " %.2fs %s %S\n"
  226. (if (numberp gnus-backend-trace-elapsed)
  227. (- (float-time) gnus-backend-trace-elapsed)
  228. 0)
  229. type form))
  230. (setq gnus-backend-trace-elapsed (float-time)))))
  231. (defun gnus-open-server (gnus-command-method)
  232. "Open a connection to GNUS-COMMAND-METHOD."
  233. (when (stringp gnus-command-method)
  234. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  235. (gnus-backend-trace :opening gnus-command-method)
  236. (let ((elem (assoc gnus-command-method gnus-opened-servers))
  237. (server (gnus-method-to-server-name gnus-command-method)))
  238. ;; If this method was previously denied, we just return nil.
  239. (if (eq (nth 1 elem) 'denied)
  240. (progn
  241. (gnus-message
  242. 1 "Server %s previously determined to be down; not retrying" server)
  243. nil)
  244. ;; Open the server.
  245. (let* ((open-server-function
  246. (gnus-get-function gnus-command-method 'open-server))
  247. (result
  248. (condition-case err
  249. (funcall open-server-function
  250. (nth 1 gnus-command-method)
  251. (nthcdr 2 gnus-command-method))
  252. (error
  253. (gnus-message 1 "Unable to open server %s due to: %s"
  254. server (error-message-string err))
  255. nil)
  256. (quit
  257. (if debug-on-quit
  258. (debug "Quit")
  259. (gnus-message 1 "Quit trying to open server %s" server))
  260. nil)))
  261. open-offline)
  262. ;; If this hasn't been opened before, we add it to the list.
  263. (unless elem
  264. (setq elem (list gnus-command-method nil)
  265. gnus-opened-servers (cons elem gnus-opened-servers)))
  266. ;; Set the status of this server.
  267. (setcar
  268. (cdr elem)
  269. (cond (result
  270. (if (eq open-server-function 'nnagent-open-server)
  271. ;; The agent's backend has a "special" status
  272. 'offline
  273. 'ok))
  274. ((and gnus-agent
  275. (gnus-agent-method-p gnus-command-method))
  276. (cond
  277. (gnus-server-unopen-status
  278. ;; Set the server's status to the unopen
  279. ;; status. If that status is offline,
  280. ;; recurse to open the agent's backend.
  281. (setq open-offline (eq gnus-server-unopen-status 'offline))
  282. gnus-server-unopen-status)
  283. ((not gnus-batch-mode)
  284. (setq open-offline t)
  285. 'offline)
  286. (t
  287. ;; This agentized server was still denied
  288. 'denied)))
  289. (t
  290. ;; This unagentized server must be denied
  291. 'denied)))
  292. ;; NOTE: I MUST set the server's status to offline before this
  293. ;; recursive call as this status will drive the
  294. ;; gnus-get-function (called above) to return the agent's
  295. ;; backend.
  296. (if open-offline
  297. ;; Recursively open this offline server to perform the
  298. ;; open-server function of the agent's backend.
  299. (let ((gnus-server-unopen-status 'denied))
  300. ;; Bind gnus-server-unopen-status to avoid recursively
  301. ;; prompting with "go offline?". This is only a concern
  302. ;; when the agent's backend fails to open the server.
  303. (gnus-open-server gnus-command-method))
  304. (when (and (eq (cadr elem) 'ok) gnus-agent
  305. (gnus-agent-method-p gnus-command-method))
  306. (save-excursion
  307. (gnus-agent-possibly-synchronize-flags-server
  308. gnus-command-method)))
  309. (gnus-backend-trace :opened gnus-command-method)
  310. result)))))
  311. (defun gnus-close-server (gnus-command-method)
  312. "Close the connection to GNUS-COMMAND-METHOD."
  313. (when (stringp gnus-command-method)
  314. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  315. (funcall (gnus-get-function gnus-command-method 'close-server)
  316. (nth 1 gnus-command-method)))
  317. (defun gnus-request-list (gnus-command-method)
  318. "Request the active file from GNUS-COMMAND-METHOD."
  319. (when (stringp gnus-command-method)
  320. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  321. (funcall (gnus-get-function gnus-command-method 'request-list)
  322. (nth 1 gnus-command-method)))
  323. (defun gnus-finish-retrieve-group-infos (gnus-command-method infos data)
  324. "Read and update infos from GNUS-COMMAND-METHOD."
  325. (when (stringp gnus-command-method)
  326. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  327. (gnus-backend-trace :finishing gnus-command-method)
  328. (prog1
  329. (funcall (gnus-get-function gnus-command-method
  330. 'finish-retrieve-group-infos)
  331. (nth 1 gnus-command-method)
  332. infos data)
  333. (gnus-backend-trace :finished gnus-command-method)))
  334. (defun gnus-retrieve-group-data-early (gnus-command-method infos)
  335. "Start early async retrieval of data from GNUS-COMMAND-METHOD."
  336. (when (stringp gnus-command-method)
  337. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  338. (funcall (gnus-get-function gnus-command-method 'retrieve-group-data-early)
  339. (nth 1 gnus-command-method)
  340. infos))
  341. (defun gnus-request-list-newsgroups (gnus-command-method)
  342. "Request the newsgroups file from GNUS-COMMAND-METHOD."
  343. (when (stringp gnus-command-method)
  344. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  345. (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
  346. (nth 1 gnus-command-method)))
  347. (defun gnus-request-newgroups (date gnus-command-method)
  348. "Request all new groups since DATE from GNUS-COMMAND-METHOD."
  349. (when (stringp gnus-command-method)
  350. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  351. (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
  352. (when func
  353. (funcall func date (nth 1 gnus-command-method)))))
  354. (defun gnus-request-regenerate (gnus-command-method)
  355. "Request a data generation from GNUS-COMMAND-METHOD."
  356. (when (stringp gnus-command-method)
  357. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  358. (funcall (gnus-get-function gnus-command-method 'request-regenerate)
  359. (nth 1 gnus-command-method)))
  360. (defun gnus-request-compact-group (group)
  361. (let* ((method (gnus-find-method-for-group group))
  362. (gnus-command-method method)
  363. (result
  364. (funcall (gnus-get-function gnus-command-method
  365. 'request-compact-group)
  366. (gnus-group-real-name group)
  367. (nth 1 gnus-command-method) t)))
  368. result))
  369. (defun gnus-request-compact (gnus-command-method)
  370. "Request groups compaction from GNUS-COMMAND-METHOD."
  371. (when (stringp gnus-command-method)
  372. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  373. (funcall (gnus-get-function gnus-command-method 'request-compact)
  374. (nth 1 gnus-command-method)))
  375. (defun gnus-request-group (group &optional dont-check gnus-command-method info)
  376. "Request GROUP. If DONT-CHECK, no information is required."
  377. (let ((gnus-command-method
  378. (or gnus-command-method (inline (gnus-find-method-for-group group)))))
  379. (when (stringp gnus-command-method)
  380. (setq gnus-command-method
  381. (inline (gnus-server-to-method gnus-command-method))))
  382. (funcall (inline (gnus-get-function gnus-command-method 'request-group))
  383. (gnus-group-real-name group) (nth 1 gnus-command-method)
  384. dont-check
  385. info)))
  386. (defun gnus-request-group-description (group)
  387. "Request a description of GROUP."
  388. (let ((gnus-command-method (gnus-find-method-for-group group))
  389. (func 'request-group-description))
  390. (when (gnus-check-backend-function func group)
  391. (funcall (gnus-get-function gnus-command-method func)
  392. (gnus-group-real-name group) (nth 1 gnus-command-method)))))
  393. (defun gnus-request-group-scan (group info)
  394. "Request that GROUP get a complete rescan."
  395. (let ((gnus-command-method (gnus-find-method-for-group group))
  396. (func 'request-group-scan))
  397. (when (gnus-check-backend-function func group)
  398. (funcall (gnus-get-function gnus-command-method func)
  399. (gnus-group-real-name group) (nth 1 gnus-command-method) info))))
  400. (defun gnus-close-group (group)
  401. "Request the GROUP be closed."
  402. (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
  403. (funcall (gnus-get-function gnus-command-method 'close-group)
  404. (gnus-group-real-name group) (nth 1 gnus-command-method))))
  405. (defun gnus-retrieve-headers (articles group &optional fetch-old)
  406. "Request headers for ARTICLES in GROUP.
  407. If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
  408. (let ((gnus-command-method (gnus-find-method-for-group group)))
  409. (cond
  410. ((and gnus-use-cache (numberp (car articles)))
  411. (gnus-cache-retrieve-headers articles group fetch-old))
  412. ((and gnus-agent (gnus-online gnus-command-method)
  413. (gnus-agent-method-p gnus-command-method))
  414. (gnus-agent-retrieve-headers articles group fetch-old))
  415. (t
  416. (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
  417. articles (gnus-group-real-name group)
  418. (nth 1 gnus-command-method) fetch-old)))))
  419. (defun gnus-retrieve-articles (articles group)
  420. "Request ARTICLES in GROUP."
  421. (let ((gnus-command-method (gnus-find-method-for-group group)))
  422. (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
  423. articles (gnus-group-real-name group)
  424. (nth 1 gnus-command-method))))
  425. (defun gnus-retrieve-groups (groups gnus-command-method)
  426. "Request active information on GROUPS from GNUS-COMMAND-METHOD."
  427. (when (stringp gnus-command-method)
  428. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  429. (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
  430. groups (nth 1 gnus-command-method)))
  431. (defun gnus-request-type (group &optional article)
  432. "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
  433. (let ((gnus-command-method (gnus-find-method-for-group group)))
  434. (if (not (gnus-check-backend-function
  435. 'request-type (car gnus-command-method)))
  436. 'unknown
  437. (funcall (gnus-get-function gnus-command-method 'request-type)
  438. (gnus-group-real-name group) article))))
  439. (defun gnus-request-update-group-status (group status)
  440. "Change the status of a group.
  441. Valid statuses include `subscribe' and `unsubscribe'."
  442. (let ((gnus-command-method (gnus-find-method-for-group group)))
  443. (if (not (gnus-check-backend-function
  444. 'request-update-group-status (car gnus-command-method)))
  445. nil
  446. (funcall
  447. (gnus-get-function gnus-command-method 'request-update-group-status)
  448. (gnus-group-real-name group) status
  449. (nth 1 gnus-command-method)))))
  450. (defun gnus-request-set-mark (group action)
  451. "Set marks on articles in the back end."
  452. (let ((gnus-command-method (gnus-find-method-for-group group)))
  453. (if (not (gnus-check-backend-function
  454. 'request-set-mark (car gnus-command-method)))
  455. action
  456. (funcall (gnus-get-function gnus-command-method 'request-set-mark)
  457. (gnus-group-real-name group) action
  458. (nth 1 gnus-command-method))
  459. (gnus-run-hook-with-args gnus-after-set-mark-hook group action))))
  460. (defun gnus-request-update-mark (group article mark)
  461. "Allow the back end to change the mark the user tries to put on an article."
  462. (let ((gnus-command-method (gnus-find-method-for-group group)))
  463. (if (not (gnus-check-backend-function
  464. 'request-update-mark (car gnus-command-method)))
  465. mark
  466. (gnus-run-hook-with-args gnus-before-update-mark-hook group article mark)
  467. (funcall (gnus-get-function gnus-command-method 'request-update-mark)
  468. (gnus-group-real-name group) article mark))))
  469. (defun gnus-request-article (article group &optional buffer)
  470. "Request the ARTICLE in GROUP.
  471. ARTICLE can either be an article number or an article Message-ID.
  472. If BUFFER, insert the article in that group."
  473. (let ((gnus-command-method (gnus-find-method-for-group group)))
  474. (funcall (gnus-get-function gnus-command-method 'request-article)
  475. article (gnus-group-real-name group)
  476. (nth 1 gnus-command-method) buffer)))
  477. (defun gnus-request-thread (header group)
  478. "Request the headers in the thread containing the article specified by HEADER."
  479. (let ((gnus-command-method (gnus-find-method-for-group group)))
  480. (funcall (gnus-get-function gnus-command-method 'request-thread)
  481. header
  482. (gnus-group-real-name group))))
  483. (defun gnus-select-group-with-message-id (group message-id)
  484. "Activate and select GROUP with the given MESSAGE-ID selected.
  485. Returns the article number of the message.
  486. If GROUP is not already selected, the message will be the only one in
  487. the group's summary.
  488. "
  489. ;; TODO: is there a way to know at this point whether the group will
  490. ;; be newly-selected? If so we could clean up the logic at the end
  491. ;;
  492. ;; save the new group's display parameter, if any, so we
  493. ;; can replace it temporarily with zero.
  494. (let ((saved-display
  495. (gnus-group-get-parameter group 'display :allow-list)))
  496. ;; Tell gnus we really don't want any articles
  497. (gnus-group-set-parameter group 'display 0)
  498. (unwind-protect
  499. (gnus-summary-read-group-1
  500. group (not :show-all) :no-article (not :kill-buffer)
  501. ;; The combination of no-display and this dummy list of
  502. ;; articles to select somehow makes it possible to open a
  503. ;; group with no articles in it. Black magic.
  504. :no-display '(-1); select-articles
  505. )
  506. ;; Restore the new group's display parameter
  507. (gnus-group-set-parameter group 'display saved-display)))
  508. ;; The summary buffer was suppressed by :no-display above.
  509. ;; Create it now and insert the message
  510. (let ((group-is-new (gnus-summary-setup-buffer group)))
  511. (condition-case err
  512. (let ((article-number
  513. (gnus-summary-insert-subject message-id)))
  514. (unless article-number
  515. (signal 'error "message-id not in group"))
  516. (gnus-summary-select-article nil nil nil article-number)
  517. article-number)
  518. ;; Clean up the new summary and propagate the error
  519. (error (when group-is-new (gnus-summary-exit))
  520. (apply 'signal err)))))
  521. (defun gnus-simplify-group-name (group)
  522. "Return the simplest representation of the name of GROUP.
  523. This is the string that Gnus uses to identify the group."
  524. (gnus-group-prefixed-name
  525. (gnus-group-real-name group)
  526. (gnus-group-method group)))
  527. (defun gnus-warp-to-article ()
  528. "Look up the current article in the group where it originated.
  529. This command only makes sense for groups shows articles gathered
  530. from other groups -- for instance, search results and the like."
  531. (interactive)
  532. (let ((gnus-command-method
  533. (gnus-find-method-for-group gnus-newsgroup-name)))
  534. (or
  535. (when (gnus-check-backend-function
  536. 'warp-to-article (car gnus-command-method))
  537. (funcall (gnus-get-function gnus-command-method 'warp-to-article)))
  538. (and (bound-and-true-p gnus-registry-enabled)
  539. (gnus-try-warping-via-registry)))))
  540. (defun gnus-request-head (article group)
  541. "Request the head of ARTICLE in GROUP."
  542. (let* ((gnus-command-method (gnus-find-method-for-group group))
  543. (head (gnus-get-function gnus-command-method 'request-head t))
  544. res clean-up)
  545. (cond
  546. ;; Check the cache.
  547. ((and gnus-use-cache
  548. (numberp article)
  549. (gnus-cache-request-article article group))
  550. (setq res (cons group article)
  551. clean-up t))
  552. ;; Check the agent cache.
  553. ((gnus-agent-request-article article group)
  554. (setq res (cons group article)
  555. clean-up t))
  556. ;; Use `head' function.
  557. ((fboundp head)
  558. (setq res (funcall head article
  559. (and (not gnus-override-method) (gnus-group-real-name group))
  560. (nth 1 gnus-command-method))))
  561. ;; Use `article' function.
  562. (t
  563. (setq res (gnus-request-article article group)
  564. clean-up t)))
  565. (when clean-up
  566. (with-current-buffer nntp-server-buffer
  567. (goto-char (point-min))
  568. (when (search-forward "\n\n" nil t)
  569. (delete-region (1- (point)) (point-max)))
  570. (nnheader-fold-continuation-lines)))
  571. res))
  572. (defun gnus-request-body (article group)
  573. "Request the body of ARTICLE in GROUP."
  574. (let* ((gnus-command-method (gnus-find-method-for-group group))
  575. (head (gnus-get-function gnus-command-method 'request-body t))
  576. res clean-up)
  577. (cond
  578. ;; Check the cache.
  579. ((and gnus-use-cache
  580. (numberp article)
  581. (gnus-cache-request-article article group))
  582. (setq res (cons group article)
  583. clean-up t))
  584. ;; Check the agent cache.
  585. ((gnus-agent-request-article article group)
  586. (setq res (cons group article)
  587. clean-up t))
  588. ;; Use `head' function.
  589. ((fboundp head)
  590. (setq res (funcall head article (gnus-group-real-name group)
  591. (nth 1 gnus-command-method))))
  592. ;; Use `article' function.
  593. (t
  594. (setq res (gnus-request-article article group)
  595. clean-up t)))
  596. (when clean-up
  597. (with-current-buffer nntp-server-buffer
  598. (goto-char (point-min))
  599. (when (search-forward "\n\n" nil t)
  600. (delete-region (point-min) (1- (point))))))
  601. res))
  602. (defun gnus-request-post (gnus-command-method)
  603. "Post the current buffer using GNUS-COMMAND-METHOD."
  604. (when (stringp gnus-command-method)
  605. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  606. (funcall (gnus-get-function gnus-command-method 'request-post)
  607. (nth 1 gnus-command-method)))
  608. (defun gnus-request-expunge-group (group gnus-command-method)
  609. "Expunge GROUP, which is removing articles that have been marked as deleted."
  610. (when (stringp gnus-command-method)
  611. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  612. (funcall (gnus-get-function gnus-command-method 'request-expunge-group)
  613. (gnus-group-real-name group)
  614. (nth 1 gnus-command-method)))
  615. (defun gnus-request-scan (group gnus-command-method)
  616. "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
  617. If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
  618. (let ((gnus-command-method
  619. (if group (gnus-find-method-for-group group) gnus-command-method))
  620. (gnus-inhibit-demon t)
  621. (mail-source-plugged gnus-plugged))
  622. (when (or gnus-plugged
  623. (not (gnus-agent-method-p gnus-command-method)))
  624. (setq gnus-internal-registry-spool-current-method gnus-command-method)
  625. (funcall (gnus-get-function gnus-command-method 'request-scan)
  626. (and group (gnus-group-real-name group))
  627. (nth 1 gnus-command-method)))))
  628. (defun gnus-request-update-info (info gnus-command-method)
  629. (when (gnus-check-backend-function
  630. 'request-update-info (car gnus-command-method))
  631. (when (stringp gnus-command-method)
  632. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  633. (funcall (gnus-get-function gnus-command-method 'request-update-info)
  634. (gnus-group-real-name (gnus-info-group info)) info
  635. (nth 1 gnus-command-method))))
  636. (defsubst gnus-request-marks (info gnus-command-method)
  637. "Request that GNUS-COMMAND-METHOD update INFO."
  638. (when (stringp gnus-command-method)
  639. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  640. (when (gnus-check-backend-function
  641. 'request-marks (car gnus-command-method))
  642. (let ((group (gnus-info-group info)))
  643. (and (funcall (gnus-get-function gnus-command-method 'request-marks)
  644. (gnus-group-real-name group)
  645. info (nth 1 gnus-command-method))
  646. ;; If the minimum article number is greater than 1, then all
  647. ;; smaller article numbers are known not to exist; we'll
  648. ;; artificially add those to the 'read range.
  649. (let* ((active (gnus-active group))
  650. (min (car active)))
  651. (when (> min 1)
  652. (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
  653. (read (gnus-info-read info))
  654. (new-read (gnus-range-add read (list range))))
  655. (gnus-info-set-read info new-read)))
  656. info)))))
  657. (defun gnus-request-expire-articles (articles group &optional force)
  658. (let* ((gnus-command-method (gnus-find-method-for-group group))
  659. ;; Filter out any negative article numbers; they can't be
  660. ;; expired here.
  661. (articles
  662. (delq nil (mapcar (lambda (n) (and (>= n 0) n)) articles)))
  663. (gnus-inhibit-demon t)
  664. (not-deleted
  665. (funcall
  666. (gnus-get-function gnus-command-method 'request-expire-articles)
  667. articles (gnus-group-real-name group) (nth 1 gnus-command-method)
  668. force)))
  669. (when (and gnus-agent
  670. (gnus-agent-method-p gnus-command-method))
  671. (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
  672. (when expired-articles
  673. (gnus-agent-expire expired-articles group 'force))))
  674. not-deleted))
  675. (defun gnus-request-move-article (article group server accept-function
  676. &optional last move-is-internal)
  677. (let* ((gnus-command-method (gnus-find-method-for-group group))
  678. (result (funcall (gnus-get-function gnus-command-method
  679. 'request-move-article)
  680. article (gnus-group-real-name group)
  681. (nth 1 gnus-command-method) accept-function
  682. last move-is-internal)))
  683. (when (and result gnus-agent
  684. (gnus-agent-method-p gnus-command-method))
  685. (gnus-agent-unfetch-articles group (list article)))
  686. result))
  687. (defun gnus-request-accept-article (group &optional gnus-command-method last
  688. no-encode)
  689. (when (stringp gnus-command-method)
  690. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  691. (when (and (not gnus-command-method)
  692. (stringp group))
  693. (setq gnus-command-method (or (gnus-find-method-for-group group)
  694. (gnus-group-name-to-method group))))
  695. (goto-char (point-max))
  696. ;; Make sure there's a newline at the end of the article.
  697. (unless (bolp)
  698. (insert "\n"))
  699. (unless no-encode
  700. (let ((message-options message-options))
  701. (message-options-set-recipient)
  702. (save-restriction
  703. (message-narrow-to-head)
  704. (let ((mail-parse-charset message-default-charset))
  705. (mail-encode-encoded-word-buffer)))
  706. (message-encode-message-body)))
  707. (let ((gnus-command-method (or gnus-command-method
  708. (gnus-find-method-for-group group)))
  709. (result
  710. (funcall
  711. (gnus-get-function gnus-command-method 'request-accept-article)
  712. (if (stringp group) (gnus-group-real-name group) group)
  713. (cadr gnus-command-method)
  714. last)))
  715. (when (and gnus-agent
  716. (gnus-agent-method-p gnus-command-method)
  717. (cdr result))
  718. (gnus-agent-regenerate-group group (list (cdr result))))
  719. result))
  720. (defun gnus-request-replace-article (article group buffer &optional no-encode)
  721. (unless no-encode
  722. (let ((message-options message-options))
  723. (message-options-set-recipient)
  724. (save-restriction
  725. (message-narrow-to-head)
  726. (let ((mail-parse-charset message-default-charset))
  727. (mail-encode-encoded-word-buffer)))
  728. (message-encode-message-body)))
  729. (let* ((func (car (gnus-group-name-to-method group)))
  730. (result (funcall (intern (format "%s-request-replace-article" func))
  731. article (gnus-group-real-name group) buffer)))
  732. (when (and gnus-agent (gnus-agent-method-p gnus-command-method))
  733. (gnus-agent-regenerate-group group (list article)))
  734. result))
  735. (defun gnus-request-restore-buffer (article group)
  736. "Request a new buffer restored to the state of ARTICLE."
  737. (let ((gnus-command-method (gnus-find-method-for-group group)))
  738. (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
  739. article (gnus-group-real-name group)
  740. (nth 1 gnus-command-method))))
  741. (defun gnus-request-create-group (group &optional gnus-command-method args)
  742. (when (stringp gnus-command-method)
  743. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  744. (let ((gnus-command-method
  745. (or gnus-command-method (gnus-find-method-for-group group))))
  746. (funcall (gnus-get-function gnus-command-method 'request-create-group)
  747. (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
  748. (defun gnus-request-delete-group (group &optional force)
  749. (let* ((gnus-command-method (gnus-find-method-for-group group))
  750. (result
  751. (funcall (gnus-get-function gnus-command-method 'request-delete-group)
  752. (gnus-group-real-name group) force (nth 1 gnus-command-method))))
  753. (when result
  754. (gnus-cache-delete-group group)
  755. (gnus-agent-delete-group group))
  756. result))
  757. (defun gnus-request-rename-group (group new-name)
  758. (let* ((gnus-command-method (gnus-find-method-for-group group))
  759. (result
  760. (funcall (gnus-get-function gnus-command-method 'request-rename-group)
  761. (gnus-group-real-name group)
  762. (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
  763. (when result
  764. (gnus-cache-rename-group group new-name)
  765. (gnus-agent-rename-group group new-name))
  766. result))
  767. (defun gnus-close-backends ()
  768. ;; Send a close request to all backends that support such a request.
  769. (let ((methods gnus-valid-select-methods)
  770. (gnus-inhibit-demon t)
  771. func gnus-command-method)
  772. (while (setq gnus-command-method (pop methods))
  773. (when (fboundp (setq func (intern
  774. (concat (car gnus-command-method)
  775. "-request-close"))))
  776. (funcall func)))))
  777. (defun gnus-asynchronous-p (gnus-command-method)
  778. (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
  779. (when (fboundp func)
  780. (funcall func))))
  781. (defun gnus-remove-denial (gnus-command-method)
  782. (when (stringp gnus-command-method)
  783. (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
  784. (let* ((elem (assoc gnus-command-method gnus-opened-servers))
  785. (status (cadr elem)))
  786. ;; If this hasn't been opened before, we add it to the list.
  787. (when (eq status 'denied)
  788. ;; Set the status of this server.
  789. (setcar (cdr elem) 'closed))))
  790. (provide 'gnus-int)
  791. ;;; gnus-int.el ends here