nnimap.el 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227
  1. ;;; nnimap.el --- IMAP interface for Gnus
  2. ;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Simon Josefsson <simon@josefsson.org>
  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. ;; nnimap interfaces Gnus with IMAP servers.
  18. ;;; Code:
  19. (eval-and-compile
  20. (require 'nnheader)
  21. ;; In Emacs 24, `open-protocol-stream' is an autoloaded alias for
  22. ;; `make-network-stream'.
  23. (unless (fboundp 'open-protocol-stream)
  24. (require 'proto-stream)))
  25. (eval-when-compile
  26. (require 'cl))
  27. (require 'nnheader)
  28. (require 'gnus-util)
  29. (require 'gnus)
  30. (require 'nnoo)
  31. (require 'netrc)
  32. (require 'utf7)
  33. (require 'tls)
  34. (require 'parse-time)
  35. (require 'nnmail)
  36. (autoload 'auth-source-forget+ "auth-source")
  37. (autoload 'auth-source-search "auth-source")
  38. (nnoo-declare nnimap)
  39. (defvoo nnimap-address nil
  40. "The address of the IMAP server.")
  41. (defvoo nnimap-user nil
  42. "Username to use for authentication to the IMAP server.")
  43. (defvoo nnimap-server-port nil
  44. "The IMAP port used.
  45. If nnimap-stream is `ssl', this will default to `imaps'. If not,
  46. it will default to `imap'.")
  47. (defvoo nnimap-stream 'undecided
  48. "How nnimap talks to the IMAP server.
  49. The value should be either `undecided', `ssl' or `tls',
  50. `network', `starttls', `plain', or `shell'.
  51. If the value is `undecided', nnimap tries `ssl' first, then falls
  52. back on `network'.")
  53. (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
  54. (if (listp imap-shell-program)
  55. (car imap-shell-program)
  56. imap-shell-program)
  57. "ssh %s imapd"))
  58. (defvoo nnimap-inbox nil
  59. "The mail box where incoming mail arrives and should be split out of.
  60. This can be a string or a list of strings
  61. For example, \"INBOX\" or (\"INBOX\" \"SENT\").")
  62. (defvoo nnimap-split-methods nil
  63. "How mail is split.
  64. Uses the same syntax as `nnmail-split-methods'.")
  65. (defvoo nnimap-split-fancy nil
  66. "Uses the same syntax as `nnmail-split-fancy'.")
  67. (defvoo nnimap-unsplittable-articles '(%Deleted %Seen)
  68. "Articles with the flags in the list will not be considered when splitting.")
  69. (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'."
  70. "Emacs 24.1")
  71. (defvoo nnimap-authenticator nil
  72. "How nnimap authenticate itself to the server.
  73. Possible choices are nil (use default methods), `anonymous',
  74. `login', `plain' and `cram-md5'.")
  75. (defvoo nnimap-expunge t
  76. "If non-nil, expunge articles after deleting them.
  77. This is always done if the server supports UID EXPUNGE, but it's
  78. not done by default on servers that doesn't support that command.")
  79. (defvoo nnimap-streaming t
  80. "If non-nil, try to use streaming commands with IMAP servers.
  81. Switching this off will make nnimap slower, but it helps with
  82. some servers.")
  83. (defvoo nnimap-connection-alist nil)
  84. (defvoo nnimap-current-infos nil)
  85. (defvoo nnimap-fetch-partial-articles nil
  86. "If non-nil, Gnus will fetch partial articles.
  87. If t, Gnus will fetch only the first part. If a string, it
  88. will fetch all parts that have types that match that string. A
  89. likely value would be \"text/\" to automatically fetch all
  90. textual parts.")
  91. (defgroup nnimap nil
  92. "IMAP for Gnus."
  93. :group 'gnus)
  94. (defcustom nnimap-request-articles-find-limit nil
  95. "Limit the number of articles to look for after moving an article."
  96. :type '(choice (const nil) integer)
  97. :version "24.4"
  98. :group 'nnimap)
  99. (defvar nnimap-process nil)
  100. (defvar nnimap-status-string "")
  101. (defvar nnimap-split-download-body-default nil
  102. "Internal variable with default value for `nnimap-split-download-body'.")
  103. (defvar nnimap-keepalive-timer nil)
  104. (defvar nnimap-process-buffers nil)
  105. (defstruct nnimap
  106. group process commands capabilities select-result newlinep server
  107. last-command-time greeting examined stream-type initial-resync)
  108. (defvar nnimap-object nil)
  109. (defvar nnimap-mark-alist
  110. '((read "\\Seen" %Seen)
  111. (tick "\\Flagged" %Flagged)
  112. (reply "\\Answered" %Answered)
  113. (expire "gnus-expire")
  114. (dormant "gnus-dormant")
  115. (score "gnus-score")
  116. (save "gnus-save")
  117. (download "gnus-download")
  118. (forward "gnus-forward")))
  119. (defvar nnimap-quirks
  120. '(("QRESYNC" "Zimbra" "QRESYNC ")))
  121. (defvar nnimap-inhibit-logging nil)
  122. (defun nnimap-buffer ()
  123. (nnimap-find-process-buffer nntp-server-buffer))
  124. (defun nnimap-header-parameters ()
  125. (let (params)
  126. (push "UID" params)
  127. (push "RFC822.SIZE" params)
  128. (when (nnimap-capability "X-GM-EXT-1")
  129. (push "X-GM-LABELS" params))
  130. (push "BODYSTRUCTURE" params)
  131. (push (format
  132. (if (nnimap-ver4-p)
  133. "BODY.PEEK[HEADER.FIELDS %s]"
  134. "RFC822.HEADER.LINES %s")
  135. (append '(Subject From Date Message-Id
  136. References In-Reply-To Xref)
  137. nnmail-extra-headers))
  138. params)
  139. (format "%s" (nreverse params))))
  140. (deffoo nnimap-retrieve-headers (articles &optional group server _fetch-old)
  141. (when group
  142. (setq group (nnimap-decode-gnus-group group)))
  143. (with-current-buffer nntp-server-buffer
  144. (erase-buffer)
  145. (when (nnimap-change-group group server)
  146. (with-current-buffer (nnimap-buffer)
  147. (erase-buffer)
  148. (nnimap-wait-for-response
  149. (nnimap-send-command
  150. "UID FETCH %s %s"
  151. (nnimap-article-ranges (gnus-compress-sequence articles))
  152. (nnimap-header-parameters))
  153. t)
  154. (unless (process-live-p (get-buffer-process (current-buffer)))
  155. (error "Server closed connection"))
  156. (nnimap-transform-headers)
  157. (nnheader-remove-cr-followed-by-lf))
  158. (insert-buffer-substring
  159. (nnimap-find-process-buffer (current-buffer))))
  160. 'headers))
  161. (defun nnimap-transform-headers ()
  162. (goto-char (point-min))
  163. (let (article lines size string labels)
  164. (block nil
  165. (while (not (eobp))
  166. (while (not (looking-at "\\* [0-9]+ FETCH"))
  167. (delete-region (point) (progn (forward-line 1) (point)))
  168. (when (eobp)
  169. (return)))
  170. (goto-char (match-end 0))
  171. ;; Unfold quoted {number} strings.
  172. (while (re-search-forward
  173. "[^]][ (]{\\([0-9]+\\)}\r?\n"
  174. (save-excursion
  175. ;; Start of the header section.
  176. (or (re-search-forward "] {[0-9]+}\r?\n" nil t)
  177. ;; Start of the next FETCH.
  178. (re-search-forward "\\* [0-9]+ FETCH" nil t)
  179. (point-max)))
  180. t)
  181. (setq size (string-to-number (match-string 1)))
  182. (delete-region (+ (match-beginning 0) 2) (point))
  183. (setq string (buffer-substring (point) (+ (point) size)))
  184. (delete-region (point) (+ (point) size))
  185. (insert (format "%S" (mm-subst-char-in-string ?\n ?\s string))))
  186. (beginning-of-line)
  187. (setq article
  188. (and (re-search-forward "UID \\([0-9]+\\)" (line-end-position)
  189. t)
  190. (match-string 1)))
  191. (setq lines nil)
  192. (beginning-of-line)
  193. (setq size
  194. (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
  195. (line-end-position)
  196. t)
  197. (match-string 1)))
  198. (beginning-of-line)
  199. (when (search-forward "X-GM-LABELS" (line-end-position) t)
  200. (setq labels (ignore-errors (read (current-buffer)))))
  201. (beginning-of-line)
  202. (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
  203. (let ((structure (ignore-errors
  204. (read (current-buffer)))))
  205. (while (and (consp structure)
  206. (not (atom (car structure))))
  207. (setq structure (car structure)))
  208. (setq lines (if (and
  209. (stringp (car structure))
  210. (equal (upcase (nth 0 structure)) "MESSAGE")
  211. (equal (upcase (nth 1 structure)) "RFC822"))
  212. (nth 9 structure)
  213. (nth 7 structure)))))
  214. (delete-region (line-beginning-position) (line-end-position))
  215. (insert (format "211 %s Article retrieved." article))
  216. (forward-line 1)
  217. (when size
  218. (insert (format "Chars: %s\n" size)))
  219. (when lines
  220. (insert (format "Lines: %s\n" lines)))
  221. (when labels
  222. (insert (format "X-GM-LABELS: %s\n" labels)))
  223. ;; Most servers have a blank line after the headers, but
  224. ;; Davmail doesn't.
  225. (unless (re-search-forward "^\r$\\|^)\r?$" nil t)
  226. (goto-char (point-max)))
  227. (delete-region (line-beginning-position) (line-end-position))
  228. (insert ".")
  229. (forward-line 1)))))
  230. (defun nnimap-unfold-quoted-lines ()
  231. ;; Unfold quoted {number} strings.
  232. (let (size string)
  233. (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t)
  234. (setq size (string-to-number (match-string 1)))
  235. (delete-region (1+ (match-beginning 0)) (point))
  236. (setq string (buffer-substring (point) (+ (point) size)))
  237. (delete-region (point) (+ (point) size))
  238. (insert (format "%S" string)))))
  239. (defun nnimap-get-length ()
  240. (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
  241. (string-to-number (match-string 1))))
  242. (defun nnimap-article-ranges (ranges)
  243. (let (result)
  244. (cond
  245. ((numberp ranges)
  246. (number-to-string ranges))
  247. ((numberp (cdr ranges))
  248. (format "%d:%d" (car ranges) (cdr ranges)))
  249. (t
  250. (dolist (elem ranges)
  251. (push
  252. (if (consp elem)
  253. (format "%d:%d" (car elem) (cdr elem))
  254. (number-to-string elem))
  255. result))
  256. (mapconcat #'identity (nreverse result) ",")))))
  257. (deffoo nnimap-open-server (server &optional defs no-reconnect)
  258. (if (nnimap-server-opened server)
  259. t
  260. (unless (assq 'nnimap-address defs)
  261. (setq defs (append defs (list (list 'nnimap-address server)))))
  262. (nnoo-change-server 'nnimap server defs)
  263. (if no-reconnect
  264. (nnimap-find-connection nntp-server-buffer)
  265. (or (nnimap-find-connection nntp-server-buffer)
  266. (nnimap-open-connection nntp-server-buffer)))))
  267. (defun nnimap-make-process-buffer (buffer)
  268. (with-current-buffer
  269. (generate-new-buffer (format " *nnimap %s %s %s*"
  270. nnimap-address nnimap-server-port
  271. (gnus-buffer-exists-p buffer)))
  272. (mm-disable-multibyte)
  273. (buffer-disable-undo)
  274. (gnus-add-buffer)
  275. (set (make-local-variable 'after-change-functions) nil)
  276. (set (make-local-variable 'nnimap-object)
  277. (make-nnimap :server (nnoo-current-server 'nnimap)
  278. :initial-resync 0))
  279. (push (list buffer (current-buffer)) nnimap-connection-alist)
  280. (push (current-buffer) nnimap-process-buffers)
  281. (current-buffer)))
  282. (defvar auth-source-creation-prompts)
  283. (defun nnimap-credentials (address ports user)
  284. (let* ((auth-source-creation-prompts
  285. '((user . "IMAP user at %h: ")
  286. (secret . "IMAP password for %u@%h: ")))
  287. (found (nth 0 (auth-source-search :max 1
  288. :host address
  289. :port ports
  290. :user user
  291. :require '(:user :secret)
  292. :create t))))
  293. (if found
  294. (list (plist-get found :user)
  295. (let ((secret (plist-get found :secret)))
  296. (if (functionp secret)
  297. (funcall secret)
  298. secret))
  299. (plist-get found :save-function))
  300. nil)))
  301. (defun nnimap-keepalive ()
  302. (let ((now (current-time)))
  303. (dolist (buffer nnimap-process-buffers)
  304. (when (buffer-name buffer)
  305. (with-current-buffer buffer
  306. (when (and nnimap-object
  307. (nnimap-last-command-time nnimap-object)
  308. (> (gnus-float-time
  309. (time-subtract
  310. now
  311. (nnimap-last-command-time nnimap-object)))
  312. ;; More than five minutes since the last command.
  313. (* 5 60)))
  314. (ignore-errors ;E.g. "buffer foo has no process".
  315. (nnimap-send-command "NOOP"))))))))
  316. (defun nnimap-open-connection (buffer)
  317. ;; Be backwards-compatible -- the earlier value of nnimap-stream was
  318. ;; `ssl' when nnimap-server-port was nil. Sort of.
  319. (when (and nnimap-server-port
  320. (eq nnimap-stream 'undecided))
  321. (setq nnimap-stream 'ssl))
  322. (let ((stream
  323. (if (eq nnimap-stream 'undecided)
  324. (loop for type in '(ssl network)
  325. for stream = (let ((nnimap-stream type))
  326. (nnimap-open-connection-1 buffer))
  327. while (eq stream 'no-connect)
  328. finally (return stream))
  329. (nnimap-open-connection-1 buffer))))
  330. (if (eq stream 'no-connect)
  331. nil
  332. stream)))
  333. (defun nnimap-map-port (port)
  334. (if (equal port "imaps")
  335. "993"
  336. port))
  337. (defun nnimap-open-connection-1 (buffer)
  338. (unless nnimap-keepalive-timer
  339. (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
  340. #'nnimap-keepalive)))
  341. (with-current-buffer (nnimap-make-process-buffer buffer)
  342. (let* ((coding-system-for-read 'binary)
  343. (coding-system-for-write 'binary)
  344. (ports
  345. (cond
  346. ((memq nnimap-stream '(network plain starttls))
  347. (nnheader-message 7 "Opening connection to %s..."
  348. nnimap-address)
  349. '("imap" "143"))
  350. ((eq nnimap-stream 'shell)
  351. (nnheader-message 7 "Opening connection to %s via shell..."
  352. nnimap-address)
  353. '("imap"))
  354. ((memq nnimap-stream '(ssl tls))
  355. (nnheader-message 7 "Opening connection to %s via tls..."
  356. nnimap-address)
  357. '("imaps" "imap" "993" "143"))
  358. (t
  359. (error "Unknown stream type: %s" nnimap-stream))))
  360. login-result credentials)
  361. (when nnimap-server-port
  362. (push nnimap-server-port ports))
  363. (let* ((stream-list
  364. (open-protocol-stream
  365. "*nnimap*" (current-buffer) nnimap-address
  366. (nnimap-map-port (car ports))
  367. :type nnimap-stream
  368. :warn-unless-encrypted t
  369. :return-list t
  370. :shell-command nnimap-shell-program
  371. :capability-command "1 CAPABILITY\r\n"
  372. :always-query-capabilities t
  373. :end-of-command "\r\n"
  374. :success " OK "
  375. :starttls-function
  376. (lambda (capabilities)
  377. (when (gnus-string-match-p "STARTTLS" capabilities)
  378. "1 STARTTLS\r\n"))))
  379. (stream (car stream-list))
  380. (props (cdr stream-list))
  381. (greeting (plist-get props :greeting))
  382. (capabilities (plist-get props :capabilities))
  383. (stream-type (plist-get props :type)))
  384. (when (and stream (not (memq (process-status stream) '(open run))))
  385. (setq stream nil))
  386. (when (and (fboundp 'set-network-process-option) ;; Not in XEmacs.
  387. (fboundp 'process-type) ;; Emacs 22 doesn't provide it.
  388. (eq (process-type stream) 'network))
  389. ;; Use TCP-keepalive so that connections that pass through a NAT
  390. ;; router don't hang when left idle.
  391. (set-network-process-option stream :keepalive t))
  392. (setf (nnimap-process nnimap-object) stream)
  393. (setf (nnimap-stream-type nnimap-object) stream-type)
  394. (if (not stream)
  395. (progn
  396. (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
  397. nnimap-address (car ports) nnimap-stream)
  398. 'no-connect)
  399. (gnus-set-process-query-on-exit-flag stream nil)
  400. (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting))
  401. (nnheader-report 'nnimap "%s" greeting)
  402. ;; Store the greeting (for debugging purposes).
  403. (setf (nnimap-greeting nnimap-object) greeting)
  404. (setf (nnimap-capabilities nnimap-object)
  405. (mapcar #'upcase
  406. (split-string capabilities)))
  407. (unless (gnus-string-match-p "[*.] PREAUTH" greeting)
  408. (if (not (setq credentials
  409. (if (eq nnimap-authenticator 'anonymous)
  410. (list "anonymous"
  411. (message-make-address))
  412. ;; Look for the credentials based on
  413. ;; the virtual server name and the address
  414. (nnimap-credentials
  415. (gnus-delete-duplicates
  416. (list
  417. (nnoo-current-server 'nnimap)
  418. nnimap-address))
  419. ports
  420. nnimap-user))))
  421. (setq nnimap-object nil)
  422. (let ((nnimap-inhibit-logging t))
  423. (setq login-result
  424. (nnimap-login (car credentials) (cadr credentials))))
  425. (if (car login-result)
  426. (progn
  427. ;; Save the credentials if a save function exists
  428. ;; (such a function will only be passed if a new
  429. ;; token was created).
  430. (when (functionp (nth 2 credentials))
  431. (funcall (nth 2 credentials)))
  432. ;; See if CAPABILITY is set as part of login
  433. ;; response.
  434. (dolist (response (cddr (nnimap-command "CAPABILITY")))
  435. (when (string= "CAPABILITY" (upcase (car response)))
  436. (setf (nnimap-capabilities nnimap-object)
  437. (mapcar #'upcase (cdr response))))))
  438. ;; If the login failed, then forget the credentials
  439. ;; that are now possibly cached.
  440. (dolist (host (list (nnoo-current-server 'nnimap)
  441. nnimap-address))
  442. (dolist (port ports)
  443. (auth-source-forget+ :host host :port port)))
  444. (delete-process (nnimap-process nnimap-object))
  445. (setq nnimap-object nil))))
  446. (when nnimap-object
  447. (when (nnimap-capability "QRESYNC")
  448. (nnimap-command "ENABLE QRESYNC"))
  449. (nnheader-message 7 "Opening connection to %s...done"
  450. nnimap-address)
  451. (nnimap-process nnimap-object))))))))
  452. (autoload 'rfc2104-hash "rfc2104")
  453. (defun nnimap-login (user password)
  454. (cond
  455. ;; Prefer plain LOGIN if it's enabled (since it requires fewer
  456. ;; round trips than CRAM-MD5, and it's less likely to be buggy),
  457. ;; and we're using an encrypted connection.
  458. ((and (not (nnimap-capability "LOGINDISABLED"))
  459. (eq (nnimap-stream-type nnimap-object) 'tls)
  460. (or (null nnimap-authenticator)
  461. (eq nnimap-authenticator 'login)))
  462. (nnimap-command "LOGIN %S %S" user password))
  463. ((and (nnimap-capability "AUTH=CRAM-MD5")
  464. (or (null nnimap-authenticator)
  465. (eq nnimap-authenticator 'cram-md5)))
  466. (erase-buffer)
  467. (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
  468. (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
  469. (process-send-string
  470. (get-buffer-process (current-buffer))
  471. (concat
  472. (base64-encode-string
  473. (concat user " "
  474. (rfc2104-hash 'md5 64 16 password
  475. (base64-decode-string challenge))))
  476. "\r\n"))
  477. (nnimap-wait-for-response sequence)))
  478. ((and (not (nnimap-capability "LOGINDISABLED"))
  479. (or (null nnimap-authenticator)
  480. (eq nnimap-authenticator 'login)))
  481. (nnimap-command "LOGIN %S %S" user password))
  482. ((and (nnimap-capability "AUTH=PLAIN")
  483. (or (null nnimap-authenticator)
  484. (eq nnimap-authenticator 'plain)))
  485. (nnimap-command
  486. "AUTHENTICATE PLAIN %s"
  487. (base64-encode-string
  488. (format "\000%s\000%s"
  489. (nnimap-quote-specials user)
  490. (nnimap-quote-specials password)))))))
  491. (defun nnimap-quote-specials (string)
  492. (with-temp-buffer
  493. (insert string)
  494. (goto-char (point-min))
  495. (while (re-search-forward "[\\\"]" nil t)
  496. (forward-char -1)
  497. (insert "\\")
  498. (forward-char 1))
  499. (buffer-string)))
  500. (defun nnimap-find-parameter (parameter elems)
  501. (let (result)
  502. (dolist (elem elems)
  503. (cond
  504. ((equal (car elem) parameter)
  505. (setq result (cdr elem)))
  506. ((and (equal (car elem) "OK")
  507. (consp (cadr elem))
  508. (equal (caadr elem) parameter))
  509. (setq result (cdr (cadr elem))))))
  510. result))
  511. (deffoo nnimap-close-server (&optional server)
  512. (when (nnoo-change-server 'nnimap server nil)
  513. (ignore-errors
  514. (delete-process (get-buffer-process (nnimap-buffer))))
  515. (nnoo-close-server 'nnimap server)
  516. t))
  517. (deffoo nnimap-request-close ()
  518. t)
  519. (deffoo nnimap-server-opened (&optional server)
  520. (and (nnoo-current-server-p 'nnimap server)
  521. nntp-server-buffer
  522. (gnus-buffer-live-p nntp-server-buffer)
  523. (nnimap-find-connection nntp-server-buffer)))
  524. (deffoo nnimap-status-message (&optional _server)
  525. nnimap-status-string)
  526. (deffoo nnimap-request-article (article &optional group server to-buffer)
  527. (when group
  528. (setq group (nnimap-decode-gnus-group group)))
  529. (with-current-buffer nntp-server-buffer
  530. (let ((result (nnimap-change-group group server))
  531. parts structure)
  532. (when (stringp article)
  533. (setq article (nnimap-find-article-by-message-id group server article)))
  534. (when (and result
  535. article)
  536. (erase-buffer)
  537. (with-current-buffer (nnimap-buffer)
  538. (erase-buffer)
  539. (when nnimap-fetch-partial-articles
  540. (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
  541. (goto-char (point-min))
  542. (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
  543. (setq structure (ignore-errors
  544. (let ((start (point)))
  545. (forward-sexp 1)
  546. (downcase-region start (point))
  547. (goto-char start)
  548. (read (current-buffer))))
  549. parts (nnimap-find-wanted-parts structure))))
  550. (when (if parts
  551. (nnimap-get-partial-article article parts structure)
  552. (nnimap-get-whole-article article))
  553. (let ((buffer (current-buffer)))
  554. (with-current-buffer (or to-buffer nntp-server-buffer)
  555. (nnheader-insert-buffer-substring buffer)
  556. (nnheader-ms-strip-cr)))
  557. (cons group article)))))))
  558. (deffoo nnimap-request-head (article &optional group server to-buffer)
  559. (when group
  560. (setq group (nnimap-decode-gnus-group group)))
  561. (when (nnimap-change-group group server)
  562. (with-current-buffer (nnimap-buffer)
  563. (when (stringp article)
  564. (setq article (nnimap-find-article-by-message-id group server article)))
  565. (if (null article)
  566. nil
  567. (nnimap-get-whole-article
  568. article (format "UID FETCH %%d %s"
  569. (nnimap-header-parameters)))
  570. (let ((buffer (current-buffer)))
  571. (with-current-buffer (or to-buffer nntp-server-buffer)
  572. (erase-buffer)
  573. (insert-buffer-substring buffer)
  574. (nnheader-ms-strip-cr)
  575. (cons group article)))))))
  576. (deffoo nnimap-request-articles (articles &optional group server)
  577. (when group
  578. (setq group (nnimap-decode-gnus-group group)))
  579. (with-current-buffer nntp-server-buffer
  580. (let ((result (nnimap-change-group group server)))
  581. (when result
  582. (erase-buffer)
  583. (with-current-buffer (nnimap-buffer)
  584. (erase-buffer)
  585. (when (nnimap-command
  586. (if (nnimap-ver4-p)
  587. "UID FETCH %s BODY.PEEK[]"
  588. "UID FETCH %s RFC822.PEEK")
  589. (nnimap-article-ranges (gnus-compress-sequence articles)))
  590. (let ((buffer (current-buffer)))
  591. (with-current-buffer nntp-server-buffer
  592. (nnheader-insert-buffer-substring buffer)
  593. (nnheader-ms-strip-cr)))
  594. t))))))
  595. (defun nnimap-get-whole-article (article &optional command)
  596. (let ((result
  597. (nnimap-command
  598. (or command
  599. (if (nnimap-ver4-p)
  600. "UID FETCH %d BODY.PEEK[]"
  601. "UID FETCH %d RFC822.PEEK"))
  602. article)))
  603. ;; Check that we really got an article.
  604. (goto-char (point-min))
  605. (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
  606. (setq result nil))
  607. (when result
  608. ;; Remove any data that may have arrived before the FETCH data.
  609. (beginning-of-line)
  610. (unless (bobp)
  611. (delete-region (point-min) (point)))
  612. (let ((bytes (nnimap-get-length)))
  613. (delete-region (line-beginning-position)
  614. (progn (forward-line 1) (point)))
  615. (goto-char (+ (point) bytes))
  616. (delete-region (point) (point-max)))
  617. t)))
  618. (defun nnimap-capability (capability)
  619. (member capability (nnimap-capabilities nnimap-object)))
  620. (defun nnimap-ver4-p ()
  621. (nnimap-capability "IMAP4REV1"))
  622. (defun nnimap-get-partial-article (article parts structure)
  623. (let ((result
  624. (nnimap-command
  625. "UID FETCH %d (%s %s)"
  626. article
  627. (if (nnimap-ver4-p)
  628. "BODY.PEEK[HEADER]"
  629. "RFC822.HEADER")
  630. (if (nnimap-ver4-p)
  631. (mapconcat (lambda (part)
  632. (format "BODY.PEEK[%s]" part))
  633. parts " ")
  634. (mapconcat (lambda (part)
  635. (format "RFC822.PEEK[%s]" part))
  636. parts " ")))))
  637. (when result
  638. (nnimap-convert-partial-article structure))))
  639. (defun nnimap-convert-partial-article (structure)
  640. ;; First just skip past the headers.
  641. (goto-char (point-min))
  642. (let ((bytes (nnimap-get-length))
  643. id parts)
  644. ;; Delete "FETCH" line.
  645. (delete-region (line-beginning-position)
  646. (progn (forward-line 1) (point)))
  647. (goto-char (+ (point) bytes))
  648. ;; Collect all the body parts.
  649. (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
  650. (setq id (match-string 1)
  651. bytes (or (nnimap-get-length) 0))
  652. (beginning-of-line)
  653. (delete-region (point) (progn (forward-line 1) (point)))
  654. (push (list id (buffer-substring (point) (+ (point) bytes)))
  655. parts)
  656. (delete-region (point) (+ (point) bytes)))
  657. ;; Delete trailing junk.
  658. (delete-region (point) (point-max))
  659. ;; Now insert all the parts again where they fit in the structure.
  660. (nnimap-insert-partial-structure structure parts)
  661. t))
  662. (defun nnimap-insert-partial-structure (structure parts &optional subp)
  663. (let (type boundary)
  664. (let ((bstruc structure))
  665. (while (consp (car bstruc))
  666. (pop bstruc))
  667. (setq type (car bstruc))
  668. (setq bstruc (car (cdr bstruc)))
  669. (let ((has-boundary (member "boundary" bstruc)))
  670. (when has-boundary
  671. (setq boundary (cadr has-boundary)))))
  672. (when subp
  673. (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
  674. (downcase type) boundary)))
  675. (while (not (stringp (car structure)))
  676. (insert "\n--" boundary "\n")
  677. (if (consp (caar structure))
  678. (nnimap-insert-partial-structure (pop structure) parts t)
  679. (let ((bit (pop structure)))
  680. (insert (format "Content-type: %s/%s"
  681. (downcase (nth 0 bit))
  682. (downcase (nth 1 bit))))
  683. (if (member-ignore-case "CHARSET" (nth 2 bit))
  684. (insert (format
  685. "; charset=%S\n"
  686. (cadr (member-ignore-case "CHARSET" (nth 2 bit)))))
  687. (insert "\n"))
  688. (insert (format "Content-transfer-encoding: %s\n"
  689. (nth 5 bit)))
  690. (insert "\n")
  691. (when (assoc (nth 9 bit) parts)
  692. (insert (cadr (assoc (nth 9 bit) parts)))))))
  693. (insert "\n--" boundary "--\n")))
  694. (defun nnimap-find-wanted-parts (structure)
  695. (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
  696. (defun nnimap-find-wanted-parts-1 (structure prefix)
  697. (let ((num 1)
  698. parts)
  699. (while (consp (car structure))
  700. (let ((sub (pop structure)))
  701. (if (consp (car sub))
  702. (push (nnimap-find-wanted-parts-1
  703. sub (if (string= prefix "")
  704. (number-to-string num)
  705. (format "%s.%s" prefix num)))
  706. parts)
  707. (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
  708. (id (if (string= prefix "")
  709. (number-to-string num)
  710. (format "%s.%s" prefix num))))
  711. (setcar (nthcdr 9 sub) id)
  712. (when (if (eq nnimap-fetch-partial-articles t)
  713. (equal id "1")
  714. (string-match nnimap-fetch-partial-articles type))
  715. (push id parts))))
  716. (incf num)))
  717. (nreverse parts)))
  718. (defun nnimap-decode-gnus-group (group)
  719. (decode-coding-string group 'utf-8))
  720. (deffoo nnimap-request-group (group &optional server dont-check info)
  721. (setq group (nnimap-decode-gnus-group group))
  722. (let ((result (nnimap-change-group
  723. ;; Don't SELECT the group if we're going to select it
  724. ;; later, anyway.
  725. (if (and (not dont-check)
  726. (assoc group nnimap-current-infos))
  727. nil
  728. group)
  729. server))
  730. active)
  731. (with-current-buffer nntp-server-buffer
  732. (when result
  733. (when (or (not dont-check)
  734. (not (setq active
  735. (nth 2 (assoc group nnimap-current-infos)))))
  736. (let ((sequences (nnimap-retrieve-group-data-early
  737. server (list info))))
  738. (nnimap-finish-retrieve-group-infos server (list info) sequences
  739. t)
  740. (setq active (nth 2 (assoc group nnimap-current-infos)))))
  741. (erase-buffer)
  742. (insert (format "211 %d %d %d %S\n"
  743. (- (cdr active) (car active))
  744. (car active)
  745. (cdr active)
  746. group))
  747. t))))
  748. (deffoo nnimap-request-group-scan (group &optional server info)
  749. (setq group (nnimap-decode-gnus-group group))
  750. (when (nnimap-change-group nil server)
  751. (let (marks high low)
  752. (with-current-buffer (nnimap-buffer)
  753. (erase-buffer)
  754. (let ((group-sequence
  755. (nnimap-send-command "SELECT %S" (utf7-encode group t)))
  756. (flag-sequence
  757. (nnimap-send-command "UID FETCH 1:* FLAGS")))
  758. (setf (nnimap-group nnimap-object) group)
  759. (nnimap-wait-for-response flag-sequence)
  760. (setq marks
  761. (nnimap-flags-to-marks
  762. (nnimap-parse-flags
  763. (list (list group-sequence flag-sequence
  764. 1 group "SELECT")))))
  765. (when (and info
  766. marks)
  767. (nnimap-update-infos marks (list info))
  768. (nnimap-store-info info (gnus-active (gnus-info-group info))))
  769. (goto-char (point-max))
  770. (let ((uidnext (nth 5 (car marks))))
  771. (setq high (or (if uidnext
  772. (1- uidnext)
  773. (nth 3 (car marks)))
  774. 0)
  775. low (or (nth 4 (car marks)) uidnext 1)))))
  776. (with-current-buffer nntp-server-buffer
  777. (erase-buffer)
  778. (insert
  779. (format
  780. "211 %d %d %d %S\n" (1+ (- high low)) low high group))
  781. t))))
  782. (deffoo nnimap-request-create-group (group &optional server _args)
  783. (setq group (nnimap-decode-gnus-group group))
  784. (when (nnimap-change-group nil server)
  785. (with-current-buffer (nnimap-buffer)
  786. (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
  787. (deffoo nnimap-request-delete-group (group &optional _force server)
  788. (setq group (nnimap-decode-gnus-group group))
  789. (when (nnimap-change-group nil server)
  790. (with-current-buffer (nnimap-buffer)
  791. (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
  792. (deffoo nnimap-request-rename-group (group new-name &optional server)
  793. (setq group (nnimap-decode-gnus-group group))
  794. (when (nnimap-change-group nil server)
  795. (with-current-buffer (nnimap-buffer)
  796. (nnimap-unselect-group)
  797. (car (nnimap-command "RENAME %S %S"
  798. (utf7-encode group t) (utf7-encode new-name t))))))
  799. (defun nnimap-unselect-group ()
  800. ;; Make sure we don't have this group open read/write by asking
  801. ;; to examine a mailbox that doesn't exist. This seems to be
  802. ;; the only way that allows us to reliably go back to unselected
  803. ;; state on Courier.
  804. (nnimap-command "EXAMINE DOES.NOT.EXIST"))
  805. (deffoo nnimap-request-expunge-group (group &optional server)
  806. (setq group (nnimap-decode-gnus-group group))
  807. (when (nnimap-change-group group server)
  808. (with-current-buffer (nnimap-buffer)
  809. (car (nnimap-command "EXPUNGE")))))
  810. (defun nnimap-get-flags (spec)
  811. (let ((articles nil)
  812. elems end)
  813. (with-current-buffer (nnimap-buffer)
  814. (erase-buffer)
  815. (nnimap-wait-for-response (nnimap-send-command
  816. "UID FETCH %s FLAGS" spec))
  817. (setq end (point))
  818. (subst-char-in-region (point-min) (point-max)
  819. ?\\ ?% t)
  820. (goto-char (point-min))
  821. (while (search-forward " FETCH " end t)
  822. (setq elems (read (current-buffer)))
  823. (push (cons (cadr (memq 'UID elems))
  824. (cadr (memq 'FLAGS elems)))
  825. articles)))
  826. (nreverse articles)))
  827. (deffoo nnimap-close-group (_group &optional _server)
  828. t)
  829. (deffoo nnimap-request-move-article (article group server accept-form
  830. &optional _last internal-move-group)
  831. (setq group (nnimap-decode-gnus-group group))
  832. (when internal-move-group
  833. (setq internal-move-group (nnimap-decode-gnus-group internal-move-group)))
  834. (with-temp-buffer
  835. (mm-disable-multibyte)
  836. (when (funcall (if internal-move-group
  837. 'nnimap-request-head
  838. 'nnimap-request-article)
  839. article group server (current-buffer))
  840. ;; If the move is internal (on the same server), just do it the easy
  841. ;; way.
  842. (let ((message-id (message-field-value "message-id")))
  843. (if internal-move-group
  844. (with-current-buffer (nnimap-buffer)
  845. (let* ((can-move (nnimap-capability "MOVE"))
  846. (command (if can-move
  847. "UID MOVE %d %S"
  848. "UID COPY %d %S"))
  849. (result (nnimap-command command article
  850. (utf7-encode internal-move-group t))))
  851. (when (and (car result) (not can-move))
  852. (nnimap-delete-article article))
  853. (cons internal-move-group
  854. (or (nnimap-find-uid-response "COPYUID" (caddr result))
  855. (nnimap-find-article-by-message-id
  856. internal-move-group server message-id
  857. nnimap-request-articles-find-limit)))))
  858. ;; Move the article to a different method.
  859. (let ((result (eval accept-form)))
  860. (when result
  861. (nnimap-change-group group server)
  862. (nnimap-delete-article article)
  863. result)))))))
  864. (deffoo nnimap-request-expire-articles (articles group &optional server force)
  865. (setq group (nnimap-decode-gnus-group group))
  866. (cond
  867. ((null articles)
  868. nil)
  869. ((not (nnimap-change-group group server))
  870. articles)
  871. ((and force
  872. (eq nnmail-expiry-target 'delete))
  873. (unless (nnimap-delete-article (gnus-compress-sequence articles))
  874. (nnheader-message 7 "Article marked for deletion, but not expunged."))
  875. nil)
  876. (t
  877. (let ((deletable-articles
  878. (if (or force
  879. (eq nnmail-expiry-wait 'immediate))
  880. articles
  881. (gnus-sorted-intersection
  882. articles
  883. (nnimap-find-expired-articles group)))))
  884. (if (null deletable-articles)
  885. articles
  886. (if (eq nnmail-expiry-target 'delete)
  887. (nnimap-delete-article (gnus-compress-sequence deletable-articles))
  888. (setq deletable-articles
  889. (nnimap-process-expiry-targets
  890. deletable-articles group server)))
  891. ;; Return the articles we didn't delete.
  892. (gnus-sorted-complement articles deletable-articles))))))
  893. (defun nnimap-process-expiry-targets (articles group server)
  894. (let ((deleted-articles nil)
  895. (articles-to-delete nil))
  896. (cond
  897. ;; shortcut further processing if we're going to delete the articles
  898. ((eq nnmail-expiry-target 'delete)
  899. (setq articles-to-delete articles)
  900. t)
  901. ;; or just move them to another folder on the same IMAP server
  902. ((and (not (functionp nnmail-expiry-target))
  903. (gnus-server-equal (gnus-group-method nnmail-expiry-target)
  904. (gnus-server-to-method
  905. (format "nnimap:%s" server))))
  906. (and (nnimap-change-group group server)
  907. (with-current-buffer (nnimap-buffer)
  908. (nnheader-message 7 "Expiring articles from %s: %s" group articles)
  909. (let ((can-move (nnimap-capability "MOVE")))
  910. (nnimap-command
  911. (if can-move
  912. "UID MOVE %s %S"
  913. "UID COPY %s %S")
  914. (nnimap-article-ranges (gnus-compress-sequence articles))
  915. (utf7-encode (gnus-group-real-name nnmail-expiry-target) t))
  916. (set (if can-move 'deleted-articles 'articles-to-delete) articles))))
  917. t)
  918. (t
  919. (dolist (article articles)
  920. (let ((target nnmail-expiry-target))
  921. (with-temp-buffer
  922. (mm-disable-multibyte)
  923. (when (nnimap-request-article article group server (current-buffer))
  924. (when (functionp target)
  925. (setq target (funcall target group)))
  926. (if (and target
  927. (not (eq target 'delete)))
  928. (if (or (gnus-request-group target t)
  929. (gnus-request-create-group target))
  930. (progn
  931. (nnmail-expiry-target-group target group)
  932. (nnheader-message 7 "Expiring article %s:%d to %s"
  933. group article target))
  934. (setq target nil))
  935. (nnheader-message 7 "Expiring article %s:%d" group article))
  936. (when target
  937. (push article articles-to-delete))))))
  938. (setq articles-to-delete (nreverse articles-to-delete))))
  939. ;; Change back to the current group again.
  940. (nnimap-change-group group server)
  941. (when articles-to-delete
  942. (nnimap-delete-article (gnus-compress-sequence articles-to-delete))
  943. (setq deleted-articles articles-to-delete))
  944. deleted-articles))
  945. (defun nnimap-find-expired-articles (group)
  946. (let ((cutoff (nnmail-expired-article-p group nil nil)))
  947. (when cutoff
  948. (with-current-buffer (nnimap-buffer)
  949. (let ((result
  950. (nnimap-command
  951. "UID SEARCH SENTBEFORE %s"
  952. (format-time-string
  953. (format "%%d-%s-%%Y"
  954. (upcase
  955. (car (rassoc (nth 4 (decode-time cutoff))
  956. parse-time-months))))
  957. cutoff))))
  958. (and (car result)
  959. (delete 0 (mapcar #'string-to-number
  960. (cdr (assoc "SEARCH" (cdr result)))))))))))
  961. (defun nnimap-find-article-by-message-id (group server message-id
  962. &optional limit)
  963. "Search for message with MESSAGE-ID in GROUP from SERVER.
  964. If LIMIT, first try to limit the search to the N last articles."
  965. (with-current-buffer (nnimap-buffer)
  966. (erase-buffer)
  967. (let* ((change-group-result (nnimap-change-group group server nil t))
  968. (number-of-article
  969. (and (listp change-group-result)
  970. (catch 'found
  971. (dolist (result (cdr change-group-result))
  972. (when (equal "EXISTS" (cadr result))
  973. (throw 'found (car result)))))))
  974. (sequence
  975. (nnimap-send-command
  976. "UID SEARCH%s HEADER Message-Id %S"
  977. (if (and limit number-of-article)
  978. ;; The -1 is because IMAP message
  979. ;; numbers are one-based rather than
  980. ;; zero-based.
  981. (format " %s:*" (- (string-to-number number-of-article)
  982. limit -1))
  983. "")
  984. message-id)))
  985. (when (nnimap-wait-for-response sequence)
  986. (let ((article (car (last (cdr (assoc "SEARCH"
  987. (nnimap-parse-response)))))))
  988. (if article
  989. (string-to-number article)
  990. (when (and limit number-of-article)
  991. (nnimap-find-article-by-message-id group server message-id))))))))
  992. (defun nnimap-delete-article (articles)
  993. (with-current-buffer (nnimap-buffer)
  994. (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
  995. (nnimap-article-ranges articles))
  996. (cond
  997. ((nnimap-capability "UIDPLUS")
  998. (nnimap-command "UID EXPUNGE %s"
  999. (nnimap-article-ranges articles))
  1000. t)
  1001. (nnimap-expunge
  1002. (nnimap-command "EXPUNGE")
  1003. t)
  1004. (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
  1005. "server doesn't support UIDPLUS, so we won't "
  1006. "delete this article now"))))))
  1007. (deffoo nnimap-request-scan (&optional group server)
  1008. (when group
  1009. (setq group (nnimap-decode-gnus-group group)))
  1010. (when (and (nnimap-change-group nil server)
  1011. nnimap-inbox
  1012. nnimap-split-methods)
  1013. (nnheader-message 7 "nnimap %s splitting mail..." server)
  1014. (if (listp nnimap-inbox)
  1015. (dolist (nnimap-inbox nnimap-inbox)
  1016. (nnimap-split-incoming-mail))
  1017. (nnimap-split-incoming-mail))
  1018. (nnheader-message 7 "nnimap %s splitting mail...done" server)))
  1019. (defun nnimap-marks-to-flags (marks)
  1020. (let (flags flag)
  1021. (dolist (mark marks)
  1022. (when (setq flag (cadr (assq mark nnimap-mark-alist)))
  1023. (push flag flags)))
  1024. flags))
  1025. (deffoo nnimap-request-update-group-status (group status &optional server)
  1026. (setq group (nnimap-decode-gnus-group group))
  1027. (when (nnimap-change-group nil server)
  1028. (let ((command (assoc
  1029. status
  1030. '((subscribe "SUBSCRIBE")
  1031. (unsubscribe "UNSUBSCRIBE")))))
  1032. (when command
  1033. (with-current-buffer (nnimap-buffer)
  1034. (nnimap-command "%s %S" (cadr command) (utf7-encode group t)))))))
  1035. (deffoo nnimap-request-set-mark (group actions &optional server)
  1036. (setq group (nnimap-decode-gnus-group group))
  1037. (when (nnimap-change-group group server)
  1038. (let (sequence)
  1039. (with-current-buffer (nnimap-buffer)
  1040. (erase-buffer)
  1041. ;; Just send all the STORE commands without waiting for
  1042. ;; response. If they're successful, they're successful.
  1043. (dolist (action actions)
  1044. (destructuring-bind (range action marks) action
  1045. (let ((flags (nnimap-marks-to-flags marks)))
  1046. (when flags
  1047. (setq sequence (nnimap-send-command
  1048. "UID STORE %s %sFLAGS.SILENT (%s)"
  1049. (nnimap-article-ranges range)
  1050. (cond
  1051. ((eq action 'del) "-")
  1052. ((eq action 'add) "+")
  1053. ((eq action 'set) ""))
  1054. (mapconcat #'identity flags " ")))))))
  1055. ;; Wait for the last command to complete to avoid later
  1056. ;; synchronization problems with the stream.
  1057. (when sequence
  1058. (nnimap-wait-for-response sequence))))))
  1059. (deffoo nnimap-request-accept-article (group &optional server _last)
  1060. (unless group
  1061. ;; We're respooling. Find out where mail splitting would place
  1062. ;; this article.
  1063. (setq group
  1064. (caar
  1065. (nnmail-article-group
  1066. ;; We don't really care about the article number, because
  1067. ;; that's determined by the IMAP server later. So just
  1068. ;; return the group name.
  1069. `(lambda (group)
  1070. (list (list group)))))))
  1071. (setq group (nnimap-decode-gnus-group group))
  1072. (when (nnimap-change-group nil server)
  1073. (nnmail-check-syntax)
  1074. (let ((message-id (message-field-value "message-id"))
  1075. sequence message)
  1076. (nnimap-add-cr)
  1077. (setq message (buffer-substring-no-properties (point-min) (point-max)))
  1078. (with-current-buffer (nnimap-buffer)
  1079. (when (setq message (or (nnimap-process-quirk "OK Gimap " 'append message)
  1080. message))
  1081. ;; If we have this group open read-only, then unselect it
  1082. ;; before appending to it.
  1083. (when (equal (nnimap-examined nnimap-object) group)
  1084. (nnimap-unselect-group))
  1085. (erase-buffer)
  1086. (setq sequence (nnimap-send-command
  1087. "APPEND %S {%d}" (utf7-encode group t)
  1088. (length message)))
  1089. (unless nnimap-streaming
  1090. (nnimap-wait-for-connection "^[+]"))
  1091. (process-send-string (get-buffer-process (current-buffer)) message)
  1092. (process-send-string (get-buffer-process (current-buffer))
  1093. (if (nnimap-newlinep nnimap-object)
  1094. "\n"
  1095. "\r\n"))
  1096. (let ((result (nnimap-get-response sequence)))
  1097. (if (not (nnimap-ok-p result))
  1098. (progn
  1099. (nnheader-report 'nnimap "%s" result)
  1100. nil)
  1101. (cons group
  1102. (or (nnimap-find-uid-response "APPENDUID" (car result))
  1103. (nnimap-find-article-by-message-id
  1104. group server message-id
  1105. nnimap-request-articles-find-limit))))))))))
  1106. (defun nnimap-process-quirk (greeting-match type data)
  1107. (when (and (nnimap-greeting nnimap-object)
  1108. (string-match greeting-match (nnimap-greeting nnimap-object))
  1109. (eq type 'append)
  1110. (string-match "\000" data))
  1111. (let ((choice (gnus-multiple-choice
  1112. "Message contains NUL characters. Delete, continue, abort? "
  1113. '((?d "Delete NUL characters")
  1114. (?c "Try to APPEND the message as is")
  1115. (?a "Abort")))))
  1116. (cond
  1117. ((eq choice ?a)
  1118. (nnheader-report 'nnimap "Aborted APPEND due to NUL characters"))
  1119. ((eq choice ?c)
  1120. data)
  1121. (t
  1122. (with-temp-buffer
  1123. (insert data)
  1124. (goto-char (point-min))
  1125. (while (search-forward "\000" nil t)
  1126. (replace-match "" t t))
  1127. (buffer-string)))))))
  1128. (defun nnimap-ok-p (value)
  1129. (and (consp value)
  1130. (consp (car value))
  1131. (equal (caar value) "OK")))
  1132. (defun nnimap-find-uid-response (name list)
  1133. (let ((result (car (last (nnimap-find-response-element name list)))))
  1134. (and result
  1135. (string-to-number result))))
  1136. (defun nnimap-find-response-element (name list)
  1137. (let (result)
  1138. (dolist (elem list)
  1139. (when (and (consp elem)
  1140. (equal name (car elem)))
  1141. (setq result elem)))
  1142. result))
  1143. (deffoo nnimap-request-replace-article (article group buffer)
  1144. (setq group (nnimap-decode-gnus-group group))
  1145. (let (group-art)
  1146. (when (and (nnimap-change-group group)
  1147. ;; Put the article into the group.
  1148. (with-current-buffer buffer
  1149. (setq group-art
  1150. (nnimap-request-accept-article group nil t))))
  1151. (nnimap-delete-article (list article))
  1152. ;; Return the new article number.
  1153. (cdr group-art))))
  1154. (defun nnimap-add-cr ()
  1155. (goto-char (point-min))
  1156. (while (re-search-forward "\r?\n" nil t)
  1157. (replace-match "\r\n" t t)))
  1158. (defun nnimap-get-groups ()
  1159. (erase-buffer)
  1160. (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
  1161. groups)
  1162. (nnimap-wait-for-response sequence)
  1163. (subst-char-in-region (point-min) (point-max)
  1164. ?\\ ?% t)
  1165. (goto-char (point-min))
  1166. (nnimap-unfold-quoted-lines)
  1167. (goto-char (point-min))
  1168. (while (search-forward "* LIST " nil t)
  1169. (let ((flags (read (current-buffer)))
  1170. (_separator (read (current-buffer)))
  1171. (group (buffer-substring-no-properties
  1172. (progn (skip-chars-forward " \"")
  1173. (point))
  1174. (progn (end-of-line)
  1175. (skip-chars-backward " \r\"")
  1176. (point)))))
  1177. (unless (member '%NoSelect flags)
  1178. (push (utf7-decode (if (stringp group)
  1179. group
  1180. (format "%s" group))
  1181. t)
  1182. groups))))
  1183. (nreverse groups)))
  1184. (defun nnimap-get-responses (sequences)
  1185. (let (responses)
  1186. (dolist (sequence sequences)
  1187. (goto-char (point-min))
  1188. (when (re-search-forward (format "^%d " sequence) nil t)
  1189. (push (list sequence (nnimap-parse-response))
  1190. responses)))
  1191. responses))
  1192. (deffoo nnimap-request-list (&optional server)
  1193. (when (nnimap-change-group nil server)
  1194. (with-current-buffer nntp-server-buffer
  1195. (erase-buffer)
  1196. (let ((groups
  1197. (with-current-buffer (nnimap-buffer)
  1198. (nnimap-get-groups)))
  1199. sequences responses)
  1200. (when groups
  1201. (with-current-buffer (nnimap-buffer)
  1202. (setf (nnimap-group nnimap-object) nil)
  1203. (dolist (group groups)
  1204. (setf (nnimap-examined nnimap-object) group)
  1205. (push (list (nnimap-send-command "EXAMINE %S"
  1206. (utf7-encode group t))
  1207. group)
  1208. sequences))
  1209. (nnimap-wait-for-response (caar sequences))
  1210. (setq responses
  1211. (nnimap-get-responses (mapcar #'car sequences))))
  1212. (dolist (response responses)
  1213. (let* ((sequence (car response))
  1214. (response (cadr response))
  1215. (group (cadr (assoc sequence sequences)))
  1216. (egroup (encode-coding-string group 'utf-8)))
  1217. (when (and group
  1218. (equal (caar response) "OK"))
  1219. (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
  1220. highest exists)
  1221. (dolist (elem response)
  1222. (when (equal (cadr elem) "EXISTS")
  1223. (setq exists (string-to-number (car elem)))))
  1224. (when uidnext
  1225. (setq highest (1- (string-to-number (car uidnext)))))
  1226. (cond
  1227. ((null highest)
  1228. (insert (format "%S 0 1 y\n" egroup)))
  1229. ((zerop exists)
  1230. ;; Empty group.
  1231. (insert (format "%S %d %d y\n" egroup
  1232. highest (1+ highest))))
  1233. (t
  1234. ;; Return the widest possible range.
  1235. (insert (format "%S %d 1 y\n" egroup
  1236. (or highest exists)))))))))
  1237. t)))))
  1238. (deffoo nnimap-request-newgroups (_date &optional server)
  1239. (when (nnimap-change-group nil server)
  1240. (with-current-buffer nntp-server-buffer
  1241. (erase-buffer)
  1242. (dolist (group (with-current-buffer (nnimap-buffer)
  1243. (nnimap-get-groups)))
  1244. (unless (assoc group nnimap-current-infos)
  1245. ;; Insert dummy numbers here -- they don't matter.
  1246. (insert (format "%S 0 1 y\n" (encode-coding-string group 'utf-8)))))
  1247. t)))
  1248. (deffoo nnimap-retrieve-group-data-early (server infos)
  1249. (when (and (nnimap-change-group nil server)
  1250. infos)
  1251. (with-current-buffer (nnimap-buffer)
  1252. (erase-buffer)
  1253. (setf (nnimap-group nnimap-object) nil)
  1254. (setf (nnimap-initial-resync nnimap-object) 0)
  1255. (let ((qresyncp (nnimap-capability "QRESYNC"))
  1256. params sequences active uidvalidity modseq group
  1257. unexist)
  1258. ;; Go through the infos and gather the data needed to know
  1259. ;; what and how to request the data.
  1260. (dolist (info infos)
  1261. (setq params (gnus-info-params info)
  1262. group (nnimap-decode-gnus-group
  1263. (gnus-group-real-name (gnus-info-group info)))
  1264. active (cdr (assq 'active params))
  1265. unexist (assq 'unexist (gnus-info-marks info))
  1266. uidvalidity (cdr (assq 'uidvalidity params))
  1267. modseq (cdr (assq 'modseq params)))
  1268. (setf (nnimap-examined nnimap-object) group)
  1269. (if (and qresyncp
  1270. uidvalidity
  1271. active
  1272. modseq
  1273. unexist)
  1274. (push
  1275. (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
  1276. (utf7-encode group t)
  1277. (nnimap-quirk "QRESYNC")
  1278. uidvalidity modseq)
  1279. 'qresync
  1280. nil group 'qresync)
  1281. sequences)
  1282. (let ((command
  1283. (if uidvalidity
  1284. "EXAMINE"
  1285. ;; If we don't have a UIDVALIDITY, then this is
  1286. ;; the first time we've seen the group, so we
  1287. ;; have to do a SELECT (which is slower than an
  1288. ;; examine), but will tell us whether the group
  1289. ;; is read-only or not.
  1290. "SELECT"))
  1291. start)
  1292. (if (and active uidvalidity unexist)
  1293. ;; Fetch the last 100 flags.
  1294. (setq start (max 1 (- (cdr active) 100)))
  1295. (incf (nnimap-initial-resync nnimap-object))
  1296. (setq start 1))
  1297. (push (list (nnimap-send-command "%s %S" command
  1298. (utf7-encode group t))
  1299. (nnimap-send-command "UID FETCH %d:* FLAGS" start)
  1300. start group command)
  1301. sequences))))
  1302. sequences))))
  1303. (defun nnimap-quirk (command)
  1304. (let ((quirk (assoc command nnimap-quirks)))
  1305. ;; If this server is of a type that matches a quirk, then return
  1306. ;; the "quirked" command instead of the proper one.
  1307. (if (or (null quirk)
  1308. (not (string-match (nth 1 quirk) (nnimap-greeting nnimap-object))))
  1309. command
  1310. (nth 2 quirk))))
  1311. (deffoo nnimap-finish-retrieve-group-infos (server infos sequences
  1312. &optional dont-insert)
  1313. (when (and sequences
  1314. (nnimap-change-group nil server t)
  1315. ;; Check that the process is still alive.
  1316. (get-buffer-process (nnimap-buffer))
  1317. (memq (process-status (get-buffer-process (nnimap-buffer)))
  1318. '(open run)))
  1319. (with-current-buffer (nnimap-buffer)
  1320. ;; Wait for the final data to trickle in.
  1321. (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
  1322. (caar sequences)
  1323. (cadar sequences))
  1324. t)
  1325. ;; Now we should have most of the data we need, no matter
  1326. ;; whether we're QRESYNCING, fetching all the flags from
  1327. ;; scratch, or just fetching the last 100 flags per group.
  1328. (nnimap-update-infos (nnimap-flags-to-marks
  1329. (nnimap-parse-flags
  1330. (nreverse sequences)))
  1331. infos)
  1332. (unless dont-insert
  1333. ;; Finally, just return something resembling an active file in
  1334. ;; the nntp buffer, so that the agent can save the info, too.
  1335. (with-current-buffer nntp-server-buffer
  1336. (erase-buffer)
  1337. (dolist (info infos)
  1338. (let* ((group (gnus-info-group info))
  1339. (active (gnus-active group)))
  1340. (when active
  1341. (insert (format "%S %d %d y\n"
  1342. (decode-coding-string
  1343. (gnus-group-real-name group) 'utf-8)
  1344. (cdr active)
  1345. (car active))))))))))))
  1346. (defun nnimap-update-infos (flags infos)
  1347. (dolist (info infos)
  1348. (let* ((group (nnimap-decode-gnus-group
  1349. (gnus-group-real-name (gnus-info-group info))))
  1350. (marks (cdr (assoc group flags))))
  1351. (when marks
  1352. (nnimap-update-info info marks)))))
  1353. (defun nnimap-update-info (info marks)
  1354. (destructuring-bind (existing flags high low uidnext start-article
  1355. permanent-flags uidvalidity
  1356. vanished highestmodseq) marks
  1357. (cond
  1358. ;; Ignore groups with no UIDNEXT/marks. This happens for
  1359. ;; completely empty groups.
  1360. ((and (not existing)
  1361. (not uidnext))
  1362. (let ((active (cdr (assq 'active (gnus-info-params info)))))
  1363. (when active
  1364. (gnus-set-active (gnus-info-group info) active))))
  1365. ;; We have a mismatch between the old and new UIDVALIDITY
  1366. ;; identifiers, so we have to re-request the group info (the next
  1367. ;; time). This virtually never happens.
  1368. ((let ((old-uidvalidity
  1369. (cdr (assq 'uidvalidity (gnus-info-params info)))))
  1370. (and old-uidvalidity
  1371. (not (equal old-uidvalidity uidvalidity))
  1372. (or (not start-article)
  1373. (> start-article 1))))
  1374. (gnus-group-remove-parameter info 'uidvalidity)
  1375. (gnus-group-remove-parameter info 'modseq))
  1376. ;; We have the data needed to update.
  1377. (t
  1378. (let* ((group (gnus-info-group info))
  1379. (completep (and start-article
  1380. (= start-article 1)))
  1381. (active (or (gnus-active group)
  1382. (cdr (assq 'active (gnus-info-params info))))))
  1383. (when uidnext
  1384. (setq high (1- uidnext)))
  1385. ;; First set the active ranges based on high/low.
  1386. (if (or completep
  1387. (not (gnus-active group)))
  1388. (gnus-set-active group
  1389. (cond
  1390. (active
  1391. (cons (min (or low (car active))
  1392. (car active))
  1393. (max (or high (cdr active))
  1394. (cdr active))))
  1395. ((and low high)
  1396. (cons low high))
  1397. (uidnext
  1398. ;; No articles in this group.
  1399. (cons uidnext (1- uidnext)))
  1400. (start-article
  1401. (cons start-article (1- start-article)))
  1402. (t
  1403. ;; No articles and no uidnext.
  1404. nil)))
  1405. (gnus-set-active group
  1406. (cons (car active)
  1407. (or high (1- uidnext)))))
  1408. ;; See whether this is a read-only group.
  1409. (unless (eq permanent-flags 'not-scanned)
  1410. (gnus-group-set-parameter
  1411. info 'permanent-flags
  1412. (and (or (memq '%* permanent-flags)
  1413. (memq '%Seen permanent-flags))
  1414. permanent-flags)))
  1415. ;; Update marks and read articles if this isn't a
  1416. ;; read-only IMAP group.
  1417. (when (setq permanent-flags
  1418. (cdr (assq 'permanent-flags (gnus-info-params info))))
  1419. (if (and highestmodseq
  1420. (not start-article))
  1421. ;; We've gotten the data by QRESYNCing.
  1422. (nnimap-update-qresync-info
  1423. info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
  1424. ;; Do normal non-QRESYNC flag updates.
  1425. ;; Update the list of read articles.
  1426. (let* ((unread
  1427. (gnus-compress-sequence
  1428. (gnus-set-difference
  1429. (gnus-set-difference
  1430. existing
  1431. (gnus-sorted-union
  1432. (cdr (assoc '%Seen flags))
  1433. (cdr (assoc '%Deleted flags))))
  1434. (cdr (assoc '%Flagged flags)))))
  1435. (read (gnus-range-difference
  1436. (cons start-article high) unread)))
  1437. (when (> start-article 1)
  1438. (setq read
  1439. (gnus-range-nconcat
  1440. (if (> start-article 1)
  1441. (gnus-sorted-range-intersection
  1442. (cons 1 (1- start-article))
  1443. (gnus-info-read info))
  1444. (gnus-info-read info))
  1445. read)))
  1446. (when (or (not (listp permanent-flags))
  1447. (memq '%Seen permanent-flags))
  1448. (gnus-info-set-read info read))
  1449. ;; Update the marks.
  1450. (setq marks (gnus-info-marks info))
  1451. (dolist (type (cdr nnimap-mark-alist))
  1452. (when (or (not (listp permanent-flags))
  1453. (memq (car (assoc (caddr type) flags))
  1454. permanent-flags)
  1455. (memq '%* permanent-flags))
  1456. (let ((old-marks (assoc (car type) marks))
  1457. (new-marks
  1458. (gnus-compress-sequence
  1459. (cdr (or (assoc (caddr type) flags) ; %Flagged
  1460. (assoc (intern (cadr type) obarray) flags)
  1461. (assoc (cadr type) flags)))))) ; "\Flagged"
  1462. (setq marks (delq old-marks marks))
  1463. (pop old-marks)
  1464. (when (and old-marks
  1465. (> start-article 1))
  1466. (setq old-marks (gnus-range-difference
  1467. old-marks
  1468. (cons start-article high)))
  1469. (setq new-marks (gnus-range-nconcat old-marks new-marks)))
  1470. (when new-marks
  1471. (push (cons (car type) new-marks) marks)))))
  1472. ;; Keep track of non-existing articles.
  1473. (let* ((old-unexists (assq 'unexist marks))
  1474. (active (gnus-active group))
  1475. (unexists
  1476. (if completep
  1477. (gnus-range-difference
  1478. active
  1479. (gnus-compress-sequence existing))
  1480. (gnus-add-to-range
  1481. (cdr old-unexists)
  1482. (gnus-list-range-difference
  1483. existing (gnus-active group))))))
  1484. (when (> (car active) 1)
  1485. (setq unexists (gnus-range-add
  1486. (cons 1 (1- (car active)))
  1487. unexists)))
  1488. (if old-unexists
  1489. (setcdr old-unexists unexists)
  1490. (push (cons 'unexist unexists) marks)))
  1491. (gnus-info-set-marks info marks t))))
  1492. ;; Tell Gnus whether there are any \Recent messages in any of
  1493. ;; the groups.
  1494. (let ((recent (cdr (assoc '%Recent flags))))
  1495. (when (and active
  1496. recent
  1497. (> (car (last recent)) (cdr active)))
  1498. (push (list (cons (gnus-group-real-name group) 0))
  1499. nnmail-split-history)))
  1500. ;; Note the active level for the next run-through.
  1501. (gnus-group-set-parameter info 'active (gnus-active group))
  1502. (gnus-group-set-parameter info 'uidvalidity uidvalidity)
  1503. (gnus-group-set-parameter info 'modseq highestmodseq)
  1504. (nnimap-store-info info (gnus-active group)))))))
  1505. (defun nnimap-update-qresync-info (info existing vanished flags)
  1506. ;; Add all the vanished articles to the list of read articles.
  1507. (gnus-info-set-read
  1508. info
  1509. (gnus-add-to-range
  1510. (gnus-add-to-range
  1511. (gnus-range-add (gnus-info-read info)
  1512. vanished)
  1513. (cdr (assq '%Flagged flags)))
  1514. (cdr (assq '%Seen flags))))
  1515. (let ((marks (gnus-info-marks info)))
  1516. (dolist (type (cdr nnimap-mark-alist))
  1517. (let ((ticks (assoc (car type) marks))
  1518. (new-marks
  1519. (cdr (or (assoc (caddr type) flags) ; %Flagged
  1520. (assoc (intern (cadr type) obarray) flags)
  1521. (assoc (cadr type) flags))))) ; "\Flagged"
  1522. (setq marks (delq ticks marks))
  1523. (pop ticks)
  1524. ;; Add the new marks we got.
  1525. (setq ticks (gnus-add-to-range ticks new-marks))
  1526. ;; Remove the marks from messages that don't have them.
  1527. (setq ticks (gnus-remove-from-range
  1528. ticks
  1529. (gnus-compress-sequence
  1530. (gnus-sorted-complement existing new-marks))))
  1531. (when ticks
  1532. (push (cons (car type) ticks) marks)))
  1533. (gnus-info-set-marks info marks t))
  1534. ;; Add vanished to the list of unexisting articles.
  1535. (when vanished
  1536. (let* ((old-unexists (assq 'unexist marks))
  1537. (unexists (gnus-range-add (cdr old-unexists) vanished)))
  1538. (if old-unexists
  1539. (setcdr old-unexists unexists)
  1540. (push (cons 'unexist unexists) marks)))
  1541. (gnus-info-set-marks info marks t))))
  1542. (defun nnimap-imap-ranges-to-gnus-ranges (irange)
  1543. (if (zerop (length irange))
  1544. nil
  1545. (let ((result nil))
  1546. (dolist (elem (split-string irange ","))
  1547. (push
  1548. (if (string-match ":" elem)
  1549. (let ((numbers (split-string elem ":")))
  1550. (cons (string-to-number (car numbers))
  1551. (string-to-number (cadr numbers))))
  1552. (string-to-number elem))
  1553. result))
  1554. (nreverse result))))
  1555. (defun nnimap-store-info (info active)
  1556. (let* ((group (gnus-group-real-name (gnus-info-group info)))
  1557. (entry (assoc group nnimap-current-infos)))
  1558. (if entry
  1559. (setcdr entry (list info active))
  1560. (push (list group info active) nnimap-current-infos))))
  1561. (defun nnimap-flags-to-marks (groups)
  1562. (let (data group uidnext articles start-article mark permanent-flags
  1563. uidvalidity vanished highestmodseq)
  1564. (dolist (elem groups)
  1565. (setq group (car elem)
  1566. uidnext (nth 1 elem)
  1567. start-article (nth 2 elem)
  1568. permanent-flags (nth 3 elem)
  1569. uidvalidity (nth 4 elem)
  1570. vanished (nth 5 elem)
  1571. highestmodseq (nth 6 elem)
  1572. articles (nthcdr 7 elem))
  1573. (let ((high (caar articles))
  1574. marks low existing)
  1575. (dolist (article articles)
  1576. (setq low (car article))
  1577. (push (car article) existing)
  1578. (dolist (flag (cdr article))
  1579. (setq mark (assoc flag marks))
  1580. (if (not mark)
  1581. (push (list flag (car article)) marks)
  1582. (setcdr mark (cons (car article) (cdr mark))))))
  1583. (push (list group existing marks high low uidnext start-article
  1584. permanent-flags uidvalidity vanished highestmodseq)
  1585. data)))
  1586. data))
  1587. (defun nnimap-parse-flags (sequences)
  1588. (goto-char (point-min))
  1589. ;; Change \Delete etc to %Delete, so that the Emacs Lisp reader can
  1590. ;; read it.
  1591. (subst-char-in-region (point-min) (point-max)
  1592. ?\\ ?% t)
  1593. ;; Remove any MODSEQ entries in the buffer, because they may contain
  1594. ;; numbers that are too large for 32-bit Emacsen.
  1595. (while (re-search-forward " MODSEQ ([0-9]+)" nil t)
  1596. (replace-match "" t t))
  1597. (goto-char (point-min))
  1598. (let (start end articles groups uidnext elems permanent-flags
  1599. uidvalidity vanished highestmodseq)
  1600. (dolist (elem sequences)
  1601. (destructuring-bind (group-sequence flag-sequence totalp group command)
  1602. elem
  1603. (setq start (point))
  1604. (when (and
  1605. ;; The EXAMINE was successful.
  1606. (search-forward (format "\n%d OK " group-sequence) nil t)
  1607. (progn
  1608. (forward-line 1)
  1609. (setq end (point))
  1610. (goto-char start)
  1611. (setq permanent-flags
  1612. (if (equal command "SELECT")
  1613. (and (search-forward "PERMANENTFLAGS "
  1614. (or end (point-min)) t)
  1615. (read (current-buffer)))
  1616. 'not-scanned))
  1617. (goto-char start)
  1618. (setq uidnext
  1619. (and (search-forward "UIDNEXT "
  1620. (or end (point-min)) t)
  1621. (read (current-buffer))))
  1622. (goto-char start)
  1623. (setq uidvalidity
  1624. (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
  1625. (or end (point-min)) t)
  1626. ;; Store UIDVALIDITY as a string, as it's
  1627. ;; too big for 32-bit Emacsen, usually.
  1628. (match-string 1)))
  1629. (goto-char start)
  1630. (setq vanished
  1631. (and (eq flag-sequence 'qresync)
  1632. (re-search-forward "^\\* VANISHED .*? \\([0-9:,]+\\)"
  1633. (or end (point-min)) t)
  1634. (match-string 1)))
  1635. (goto-char start)
  1636. (setq highestmodseq
  1637. (and (re-search-forward "HIGHESTMODSEQ \\([0-9]+\\)"
  1638. (or end (point-min)) t)
  1639. (match-string 1)))
  1640. (goto-char end)
  1641. (forward-line -1))
  1642. ;; The UID FETCH FLAGS was successful.
  1643. (or (eq flag-sequence 'qresync)
  1644. (search-forward (format "\n%d OK " flag-sequence) nil t)))
  1645. (if (eq flag-sequence 'qresync)
  1646. (progn
  1647. (goto-char start)
  1648. (setq start end))
  1649. (setq start (point))
  1650. (goto-char end))
  1651. (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
  1652. (progn
  1653. (setq elems (read (current-buffer)))
  1654. (push (cons (cadr (memq 'UID elems))
  1655. (cadr (memq 'FLAGS elems)))
  1656. articles)))
  1657. (push (nconc (list group uidnext totalp permanent-flags uidvalidity
  1658. vanished highestmodseq)
  1659. articles)
  1660. groups)
  1661. (if (eq flag-sequence 'qresync)
  1662. (goto-char end)
  1663. (setq end (point)))
  1664. (setq articles nil))))
  1665. groups))
  1666. (defun nnimap-find-process-buffer (buffer)
  1667. (cadr (assoc buffer nnimap-connection-alist)))
  1668. (deffoo nnimap-request-post (&optional _server)
  1669. (setq nnimap-status-string "Read-only server")
  1670. nil)
  1671. (defvar gnus-refer-thread-use-nnir) ;; gnus-sum.el
  1672. (declare-function gnus-fetch-headers "gnus-sum"
  1673. (articles &optional limit force-new dependencies))
  1674. (autoload 'nnir-search-thread "nnir")
  1675. (deffoo nnimap-request-thread (header &optional group server)
  1676. (when group
  1677. (setq group (nnimap-decode-gnus-group group)))
  1678. (if gnus-refer-thread-use-nnir
  1679. (nnir-search-thread header)
  1680. (when (nnimap-change-group group server)
  1681. (let* ((cmd (nnimap-make-thread-query header))
  1682. (result (with-current-buffer (nnimap-buffer)
  1683. (nnimap-command "UID SEARCH %s" cmd))))
  1684. (when result
  1685. (gnus-fetch-headers
  1686. (and (car result)
  1687. (delete 0 (mapcar #'string-to-number
  1688. (cdr (assoc "SEARCH" (cdr result))))))
  1689. nil t))))))
  1690. (defun nnimap-change-group (group &optional server no-reconnect read-only)
  1691. "Change group to GROUP if non-nil.
  1692. If SERVER is set, check that server is connected, otherwise retry
  1693. to reconnect, unless NO-RECONNECT is set to t. Return nil if
  1694. unsuccessful in connecting.
  1695. If GROUP is nil, return t.
  1696. If READ-ONLY is set, send EXAMINE rather than SELECT to the server.
  1697. Return the server's response to the SELECT or EXAMINE command."
  1698. (let ((open-result t))
  1699. (when (and server
  1700. (not (nnimap-server-opened server)))
  1701. (setq open-result (nnimap-open-server server nil no-reconnect)))
  1702. (cond
  1703. ((not open-result)
  1704. nil)
  1705. ((not group)
  1706. t)
  1707. (t
  1708. (with-current-buffer (nnimap-buffer)
  1709. (let ((result (nnimap-command "%s %S"
  1710. (if read-only
  1711. "EXAMINE"
  1712. "SELECT")
  1713. (utf7-encode group t))))
  1714. (when (car result)
  1715. (setf (nnimap-group nnimap-object) group
  1716. (nnimap-select-result nnimap-object) result)
  1717. result)))))))
  1718. (defun nnimap-find-connection (buffer)
  1719. "Find the connection delivering to BUFFER."
  1720. (let ((entry (assoc buffer nnimap-connection-alist)))
  1721. (when entry
  1722. (if (and (buffer-name (cadr entry))
  1723. (get-buffer-process (cadr entry))
  1724. (memq (process-status (get-buffer-process (cadr entry)))
  1725. '(open run)))
  1726. (get-buffer-process (cadr entry))
  1727. (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
  1728. nil))))
  1729. (defvar nnimap-sequence 0)
  1730. (defun nnimap-send-command (&rest args)
  1731. (setf (nnimap-last-command-time nnimap-object) (current-time))
  1732. (process-send-string
  1733. (get-buffer-process (current-buffer))
  1734. (nnimap-log-command
  1735. (format "%d %s%s\n"
  1736. (incf nnimap-sequence)
  1737. (apply #'format args)
  1738. (if (nnimap-newlinep nnimap-object)
  1739. ""
  1740. "\r"))))
  1741. ;; Some servers apparently can't have many outstanding
  1742. ;; commands, so throttle them.
  1743. (unless nnimap-streaming
  1744. (nnimap-wait-for-response nnimap-sequence))
  1745. nnimap-sequence)
  1746. (defvar nnimap-record-commands nil
  1747. "If non-nil, log commands to the \"*imap log*\" buffer.")
  1748. (defun nnimap-log-buffer ()
  1749. (let ((name "*imap log*"))
  1750. (or (get-buffer name)
  1751. (with-current-buffer (get-buffer-create name)
  1752. (when (boundp 'window-point-insertion-type)
  1753. (make-local-variable 'window-point-insertion-type)
  1754. (setq window-point-insertion-type t))
  1755. (current-buffer)))))
  1756. (defun nnimap-log-command (command)
  1757. (when nnimap-record-commands
  1758. (with-current-buffer (nnimap-log-buffer)
  1759. (goto-char (point-max))
  1760. (insert (format-time-string "%H:%M:%S")
  1761. " [" nnimap-address "] "
  1762. (if nnimap-inhibit-logging
  1763. "(inhibited)\n"
  1764. command))))
  1765. command)
  1766. (defun nnimap-command (&rest args)
  1767. (erase-buffer)
  1768. (let* ((sequence (apply #'nnimap-send-command args))
  1769. (response (nnimap-get-response sequence)))
  1770. (if (equal (caar response) "OK")
  1771. (cons t response)
  1772. (nnheader-report 'nnimap "%s"
  1773. (mapconcat (lambda (a)
  1774. (format "%s" a))
  1775. (car response) " "))
  1776. nil)))
  1777. (defun nnimap-get-response (sequence)
  1778. (nnimap-wait-for-response sequence)
  1779. (nnimap-parse-response))
  1780. (defun nnimap-wait-for-connection (&optional regexp)
  1781. (nnimap-wait-for-line (or regexp "^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
  1782. (defun nnimap-wait-for-line (regexp &optional response-regexp)
  1783. (let ((process (get-buffer-process (current-buffer))))
  1784. (goto-char (point-min))
  1785. (while (and (memq (process-status process)
  1786. '(open run))
  1787. (not (re-search-forward regexp nil t)))
  1788. (nnheader-accept-process-output process)
  1789. (goto-char (point-min)))
  1790. (forward-line -1)
  1791. (and (looking-at (or response-regexp regexp))
  1792. (match-string 1))))
  1793. (defun nnimap-wait-for-response (sequence &optional messagep)
  1794. (let ((process (get-buffer-process (current-buffer)))
  1795. openp)
  1796. (condition-case nil
  1797. (progn
  1798. (goto-char (point-max))
  1799. (while (and (setq openp (memq (process-status process)
  1800. '(open run)))
  1801. (progn
  1802. ;; Skip past any "*" lines that the server has
  1803. ;; output.
  1804. (while (and (not (bobp))
  1805. (progn
  1806. (forward-line -1)
  1807. (looking-at "\\*\\|[0-9]+ OK NOOP"))))
  1808. (not (looking-at (format "%d .*\n" sequence)))))
  1809. (when messagep
  1810. (nnheader-message-maybe
  1811. 7 "nnimap read %dk from %s%s" (/ (buffer-size) 1000)
  1812. nnimap-address
  1813. (if (not (zerop (nnimap-initial-resync nnimap-object)))
  1814. (format " (initial sync of %d group%s; please wait)"
  1815. (nnimap-initial-resync nnimap-object)
  1816. (if (= (nnimap-initial-resync nnimap-object) 1)
  1817. ""
  1818. "s"))
  1819. "")))
  1820. (nnheader-accept-process-output process)
  1821. (goto-char (point-max)))
  1822. (setf (nnimap-initial-resync nnimap-object) 0)
  1823. openp)
  1824. (quit
  1825. (when debug-on-quit
  1826. (debug "Quit"))
  1827. ;; The user hit C-g while we were waiting: kill the process, in case
  1828. ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
  1829. ;; NAT routers).
  1830. (delete-process process)
  1831. nil))))
  1832. (defun nnimap-parse-response ()
  1833. (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
  1834. result)
  1835. (dolist (line lines)
  1836. (push (cdr (nnimap-parse-line line)) result))
  1837. ;; Return the OK/error code first, and then all the "continuation
  1838. ;; lines" afterwards.
  1839. (cons (pop result)
  1840. (nreverse result))))
  1841. ;; Parse an IMAP response line lightly. They look like
  1842. ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
  1843. ;; the lines into a list of strings and lists of string.
  1844. (defun nnimap-parse-line (line)
  1845. (let (char result)
  1846. (with-temp-buffer
  1847. (mm-disable-multibyte)
  1848. (insert line)
  1849. (goto-char (point-min))
  1850. (while (not (eobp))
  1851. (if (eql (setq char (following-char)) ? )
  1852. (forward-char 1)
  1853. (push
  1854. (cond
  1855. ((eql char ?\[)
  1856. (split-string
  1857. (buffer-substring
  1858. (1+ (point))
  1859. (if (search-forward "]" (line-end-position) 'move)
  1860. (1- (point))
  1861. (point)))))
  1862. ((eql char ?\()
  1863. (split-string
  1864. (buffer-substring
  1865. (1+ (point))
  1866. (if (search-forward ")" (line-end-position) 'move)
  1867. (1- (point))
  1868. (point)))))
  1869. ((eql char ?\")
  1870. (forward-char 1)
  1871. (buffer-substring
  1872. (point)
  1873. (1- (or (search-forward "\"" (line-end-position) 'move)
  1874. (point)))))
  1875. (t
  1876. (buffer-substring (point) (if (search-forward " " nil t)
  1877. (1- (point))
  1878. (goto-char (point-max))))))
  1879. result)))
  1880. (nreverse result))))
  1881. (defun nnimap-last-response-string ()
  1882. (save-excursion
  1883. (forward-line 1)
  1884. (let ((end (point)))
  1885. (forward-line -1)
  1886. (when (not (bobp))
  1887. (forward-line -1)
  1888. (while (and (not (bobp))
  1889. (eql (following-char) ?*))
  1890. (forward-line -1))
  1891. (unless (eql (following-char) ?*)
  1892. (forward-line 1)))
  1893. (buffer-substring (point) end))))
  1894. (defvar nnimap-incoming-split-list nil)
  1895. (defun nnimap-fetch-inbox (articles)
  1896. (erase-buffer)
  1897. (nnimap-wait-for-response
  1898. (nnimap-send-command
  1899. "UID FETCH %s %s"
  1900. (nnimap-article-ranges articles)
  1901. (format "(UID %s%s)"
  1902. (format
  1903. (if (nnimap-ver4-p)
  1904. "BODY.PEEK"
  1905. "RFC822.PEEK"))
  1906. (cond
  1907. (nnimap-split-download-body-default
  1908. "[]")
  1909. ((nnimap-ver4-p)
  1910. "[HEADER]")
  1911. (t
  1912. "[1]"))))
  1913. t))
  1914. (defun nnimap-split-incoming-mail ()
  1915. (with-current-buffer (nnimap-buffer)
  1916. (let ((nnimap-incoming-split-list nil)
  1917. (nnmail-split-methods
  1918. (cond
  1919. ((eq nnimap-split-methods 'default)
  1920. nnmail-split-methods)
  1921. (nnimap-split-methods
  1922. nnimap-split-methods)
  1923. (nnimap-split-fancy
  1924. 'nnmail-split-fancy)))
  1925. (nnmail-split-fancy (or nnimap-split-fancy
  1926. nnmail-split-fancy))
  1927. (nnmail-inhibit-default-split-group t)
  1928. (groups (nnimap-get-groups))
  1929. (can-move (nnimap-capability "MOVE"))
  1930. new-articles)
  1931. (erase-buffer)
  1932. (nnimap-command "SELECT %S" nnimap-inbox)
  1933. (setf (nnimap-group nnimap-object) nnimap-inbox)
  1934. (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
  1935. (when new-articles
  1936. (nnimap-fetch-inbox new-articles)
  1937. (nnimap-transform-split-mail)
  1938. (nnheader-ms-strip-cr)
  1939. (nnmail-cache-open)
  1940. (nnmail-split-incoming (current-buffer)
  1941. #'nnimap-save-mail-spec
  1942. nil nil
  1943. #'nnimap-dummy-active-number
  1944. #'nnimap-save-mail-spec)
  1945. (when nnimap-incoming-split-list
  1946. (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
  1947. sequences junk-articles)
  1948. ;; Create any groups that doesn't already exist on the
  1949. ;; server first.
  1950. (dolist (spec specs)
  1951. (when (and (not (member (car spec) groups))
  1952. (not (eq (car spec) 'junk)))
  1953. (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
  1954. ;; Then copy over all the messages.
  1955. (erase-buffer)
  1956. (dolist (spec specs)
  1957. (let ((group (car spec))
  1958. (ranges (cdr spec)))
  1959. (if (eq group 'junk)
  1960. (setq junk-articles ranges)
  1961. ;; Don't copy if the message is already in its
  1962. ;; target group.
  1963. (unless (string= group nnimap-inbox)
  1964. (push (list (nnimap-send-command
  1965. (if can-move
  1966. "UID MOVE %s %S"
  1967. "UID COPY %s %S")
  1968. (nnimap-article-ranges ranges)
  1969. (utf7-encode group t))
  1970. ranges)
  1971. sequences)))))
  1972. ;; Wait for the last COPY response...
  1973. (when (and (not can-move) sequences)
  1974. (nnimap-wait-for-response (caar sequences))
  1975. ;; And then mark the successful copy actions as deleted,
  1976. ;; and possibly expunge them.
  1977. (nnimap-mark-and-expunge-incoming
  1978. (nnimap-parse-copied-articles sequences)))
  1979. (nnimap-mark-and-expunge-incoming junk-articles)))))))
  1980. (defun nnimap-mark-and-expunge-incoming (range)
  1981. (when range
  1982. (setq range (nnimap-article-ranges range))
  1983. (erase-buffer)
  1984. (let ((sequence
  1985. (nnimap-send-command
  1986. "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
  1987. (cond
  1988. ;; If the server supports it, we now delete the message we have
  1989. ;; just copied over.
  1990. ((nnimap-capability "UIDPLUS")
  1991. (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
  1992. ;; If it doesn't support UID EXPUNGE, then we only expunge if the
  1993. ;; user has configured it.
  1994. (nnimap-expunge
  1995. (setq sequence (nnimap-send-command "EXPUNGE"))))
  1996. (nnimap-wait-for-response sequence))))
  1997. (defun nnimap-parse-copied-articles (sequences)
  1998. (let (sequence copied range)
  1999. (goto-char (point-min))
  2000. (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t)
  2001. (setq sequence (string-to-number (match-string 1)))
  2002. (when (setq range (cadr (assq sequence sequences)))
  2003. (push (gnus-uncompress-range range) copied)))
  2004. (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
  2005. (defun nnimap-new-articles (flags)
  2006. (let (new)
  2007. (dolist (elem flags)
  2008. (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
  2009. (cdr elem))
  2010. (push (car elem) new)))
  2011. (gnus-compress-sequence (nreverse new))))
  2012. (defun nnimap-make-split-specs (list)
  2013. (let ((specs nil)
  2014. entry)
  2015. (dolist (elem list)
  2016. (destructuring-bind (article spec) elem
  2017. (dolist (group (delete nil (mapcar #'car spec)))
  2018. (unless (setq entry (assoc group specs))
  2019. (push (setq entry (list group)) specs))
  2020. (setcdr entry (cons article (cdr entry))))))
  2021. (dolist (entry specs)
  2022. (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
  2023. specs))
  2024. (defun nnimap-transform-split-mail ()
  2025. (goto-char (point-min))
  2026. (let (article bytes)
  2027. (block nil
  2028. (while (not (eobp))
  2029. (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
  2030. (delete-region (point) (progn (forward-line 1) (point)))
  2031. (when (eobp)
  2032. (return)))
  2033. (setq article (match-string 1)
  2034. bytes (nnimap-get-length))
  2035. (delete-region (line-beginning-position) (line-end-position))
  2036. ;; Insert MMDF separator, and a way to remember what this
  2037. ;; article UID is.
  2038. (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
  2039. (forward-char (1+ bytes))
  2040. (setq bytes (nnimap-get-length))
  2041. (delete-region (line-beginning-position) (line-end-position))
  2042. ;; There's a body; skip past that.
  2043. (when bytes
  2044. (forward-char (1+ bytes))
  2045. (delete-region (line-beginning-position) (line-end-position)))))))
  2046. (defun nnimap-dummy-active-number (_group &optional _server)
  2047. 1)
  2048. (defun nnimap-save-mail-spec (group-art &optional _server _full-nov)
  2049. (let (article)
  2050. (goto-char (point-min))
  2051. (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
  2052. (error "Invalid nnimap mail")
  2053. (setq article (string-to-number (match-string 1))))
  2054. (push (list article
  2055. (if (eq group-art 'junk)
  2056. (list (cons 'junk 1))
  2057. group-art))
  2058. nnimap-incoming-split-list)))
  2059. (defun nnimap-make-thread-query (header)
  2060. (let* ((id (mail-header-id header))
  2061. (refs (split-string
  2062. (or (mail-header-references header)
  2063. "")))
  2064. (value
  2065. (format
  2066. "(OR HEADER REFERENCES %S HEADER Message-Id %S)"
  2067. id id)))
  2068. (dolist (refid refs value)
  2069. (setq value (format
  2070. "(OR (OR HEADER Message-Id %S HEADER REFERENCES %S) %s)"
  2071. refid refid value)))))
  2072. (provide 'nnimap)
  2073. ;;; nnimap.el ends here