nnimap.el 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  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. (info (when info (list info)))
  731. active)
  732. (with-current-buffer nntp-server-buffer
  733. (when result
  734. (when (or (not dont-check)
  735. (not (setq active
  736. (nth 2 (assoc group nnimap-current-infos)))))
  737. (let ((sequences (nnimap-retrieve-group-data-early
  738. server info)))
  739. (nnimap-finish-retrieve-group-infos server info sequences
  740. t)
  741. (setq active (nth 2 (assoc group nnimap-current-infos)))))
  742. (setq active (or active '(0 . 1)))
  743. (erase-buffer)
  744. (insert (format "211 %d %d %d %S\n"
  745. (- (cdr active) (car active))
  746. (car active)
  747. (cdr active)
  748. group))
  749. t))))
  750. (deffoo nnimap-request-group-scan (group &optional server info)
  751. (setq group (nnimap-decode-gnus-group group))
  752. (when (nnimap-change-group nil server)
  753. (let (marks high low)
  754. (with-current-buffer (nnimap-buffer)
  755. (erase-buffer)
  756. (let ((group-sequence
  757. (nnimap-send-command "SELECT %S" (utf7-encode group t)))
  758. (flag-sequence
  759. (nnimap-send-command "UID FETCH 1:* FLAGS")))
  760. (setf (nnimap-group nnimap-object) group)
  761. (nnimap-wait-for-response flag-sequence)
  762. (setq marks
  763. (nnimap-flags-to-marks
  764. (nnimap-parse-flags
  765. (list (list group-sequence flag-sequence
  766. 1 group "SELECT")))))
  767. (when (and info
  768. marks)
  769. (nnimap-update-infos marks (list info))
  770. (nnimap-store-info info (gnus-active (gnus-info-group info))))
  771. (goto-char (point-max))
  772. (let ((uidnext (nth 5 (car marks))))
  773. (setq high (or (if uidnext
  774. (1- uidnext)
  775. (nth 3 (car marks)))
  776. 0)
  777. low (or (nth 4 (car marks)) uidnext 1)))))
  778. (with-current-buffer nntp-server-buffer
  779. (erase-buffer)
  780. (insert
  781. (format
  782. "211 %d %d %d %S\n" (1+ (- high low)) low high group))
  783. t))))
  784. (deffoo nnimap-request-create-group (group &optional server _args)
  785. (setq group (nnimap-decode-gnus-group group))
  786. (when (nnimap-change-group nil server)
  787. (with-current-buffer (nnimap-buffer)
  788. (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
  789. (deffoo nnimap-request-delete-group (group &optional _force server)
  790. (setq group (nnimap-decode-gnus-group group))
  791. (when (nnimap-change-group nil server)
  792. (with-current-buffer (nnimap-buffer)
  793. (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
  794. (deffoo nnimap-request-rename-group (group new-name &optional server)
  795. (setq group (nnimap-decode-gnus-group group))
  796. (when (nnimap-change-group nil server)
  797. (with-current-buffer (nnimap-buffer)
  798. (nnimap-unselect-group)
  799. (car (nnimap-command "RENAME %S %S"
  800. (utf7-encode group t) (utf7-encode new-name t))))))
  801. (defun nnimap-unselect-group ()
  802. ;; Make sure we don't have this group open read/write by asking
  803. ;; to examine a mailbox that doesn't exist. This seems to be
  804. ;; the only way that allows us to reliably go back to unselected
  805. ;; state on Courier.
  806. (nnimap-command "EXAMINE DOES.NOT.EXIST"))
  807. (deffoo nnimap-request-expunge-group (group &optional server)
  808. (setq group (nnimap-decode-gnus-group group))
  809. (when (nnimap-change-group group server)
  810. (with-current-buffer (nnimap-buffer)
  811. (car (nnimap-command "EXPUNGE")))))
  812. (defun nnimap-get-flags (spec)
  813. (let ((articles nil)
  814. elems end)
  815. (with-current-buffer (nnimap-buffer)
  816. (erase-buffer)
  817. (nnimap-wait-for-response (nnimap-send-command
  818. "UID FETCH %s FLAGS" spec))
  819. (setq end (point))
  820. (subst-char-in-region (point-min) (point-max)
  821. ?\\ ?% t)
  822. (goto-char (point-min))
  823. (while (search-forward " FETCH " end t)
  824. (setq elems (read (current-buffer)))
  825. (push (cons (cadr (memq 'UID elems))
  826. (cadr (memq 'FLAGS elems)))
  827. articles)))
  828. (nreverse articles)))
  829. (deffoo nnimap-close-group (_group &optional _server)
  830. t)
  831. (deffoo nnimap-request-move-article (article group server accept-form
  832. &optional _last internal-move-group)
  833. (setq group (nnimap-decode-gnus-group group))
  834. (when internal-move-group
  835. (setq internal-move-group (nnimap-decode-gnus-group internal-move-group)))
  836. (with-temp-buffer
  837. (mm-disable-multibyte)
  838. (when (funcall (if internal-move-group
  839. 'nnimap-request-head
  840. 'nnimap-request-article)
  841. article group server (current-buffer))
  842. ;; If the move is internal (on the same server), just do it the easy
  843. ;; way.
  844. (let ((message-id (message-field-value "message-id")))
  845. (if internal-move-group
  846. (with-current-buffer (nnimap-buffer)
  847. (let* ((can-move (nnimap-capability "MOVE"))
  848. (command (if can-move
  849. "UID MOVE %d %S"
  850. "UID COPY %d %S"))
  851. (result (nnimap-command command article
  852. (utf7-encode internal-move-group t))))
  853. (when (and (car result) (not can-move))
  854. (nnimap-delete-article article))
  855. (cons internal-move-group
  856. (or (nnimap-find-uid-response "COPYUID" (caddr result))
  857. (nnimap-find-article-by-message-id
  858. internal-move-group server message-id
  859. nnimap-request-articles-find-limit)))))
  860. ;; Move the article to a different method.
  861. (let ((result (eval accept-form)))
  862. (when result
  863. (nnimap-change-group group server)
  864. (nnimap-delete-article article)
  865. result)))))))
  866. (deffoo nnimap-request-expire-articles (articles group &optional server force)
  867. (setq group (nnimap-decode-gnus-group group))
  868. (cond
  869. ((null articles)
  870. nil)
  871. ((not (nnimap-change-group group server))
  872. articles)
  873. ((and force
  874. (eq nnmail-expiry-target 'delete))
  875. (unless (nnimap-delete-article (gnus-compress-sequence articles))
  876. (nnheader-message 7 "Article marked for deletion, but not expunged."))
  877. nil)
  878. (t
  879. (let ((deletable-articles
  880. (if (or force
  881. (eq nnmail-expiry-wait 'immediate))
  882. articles
  883. (gnus-sorted-intersection
  884. articles
  885. (nnimap-find-expired-articles group)))))
  886. (if (null deletable-articles)
  887. articles
  888. (if (eq nnmail-expiry-target 'delete)
  889. (nnimap-delete-article (gnus-compress-sequence deletable-articles))
  890. (setq deletable-articles
  891. (nnimap-process-expiry-targets
  892. deletable-articles group server)))
  893. ;; Return the articles we didn't delete.
  894. (gnus-sorted-complement articles deletable-articles))))))
  895. (defun nnimap-process-expiry-targets (articles group server)
  896. (let ((deleted-articles nil)
  897. (articles-to-delete nil))
  898. (cond
  899. ;; shortcut further processing if we're going to delete the articles
  900. ((eq nnmail-expiry-target 'delete)
  901. (setq articles-to-delete articles)
  902. t)
  903. ;; or just move them to another folder on the same IMAP server
  904. ((and (not (functionp nnmail-expiry-target))
  905. (gnus-server-equal (gnus-group-method nnmail-expiry-target)
  906. (gnus-server-to-method
  907. (format "nnimap:%s" server))))
  908. (and (nnimap-change-group group server)
  909. (with-current-buffer (nnimap-buffer)
  910. (nnheader-message 7 "Expiring articles from %s: %s" group articles)
  911. (let ((can-move (nnimap-capability "MOVE")))
  912. (nnimap-command
  913. (if can-move
  914. "UID MOVE %s %S"
  915. "UID COPY %s %S")
  916. (nnimap-article-ranges (gnus-compress-sequence articles))
  917. (utf7-encode (gnus-group-real-name nnmail-expiry-target) t))
  918. (set (if can-move 'deleted-articles 'articles-to-delete) articles))))
  919. t)
  920. (t
  921. (dolist (article articles)
  922. (let ((target nnmail-expiry-target))
  923. (with-temp-buffer
  924. (mm-disable-multibyte)
  925. (when (nnimap-request-article article group server (current-buffer))
  926. (when (functionp target)
  927. (setq target (funcall target group)))
  928. (if (and target
  929. (not (eq target 'delete)))
  930. (if (or (gnus-request-group target t)
  931. (gnus-request-create-group target))
  932. (progn
  933. (nnmail-expiry-target-group target group)
  934. (nnheader-message 7 "Expiring article %s:%d to %s"
  935. group article target))
  936. (setq target nil))
  937. (nnheader-message 7 "Expiring article %s:%d" group article))
  938. (when target
  939. (push article articles-to-delete))))))
  940. (setq articles-to-delete (nreverse articles-to-delete))))
  941. ;; Change back to the current group again.
  942. (nnimap-change-group group server)
  943. (when articles-to-delete
  944. (nnimap-delete-article (gnus-compress-sequence articles-to-delete))
  945. (setq deleted-articles articles-to-delete))
  946. deleted-articles))
  947. (defun nnimap-find-expired-articles (group)
  948. (let ((cutoff (nnmail-expired-article-p group nil nil)))
  949. (when cutoff
  950. (with-current-buffer (nnimap-buffer)
  951. (let ((result
  952. (nnimap-command
  953. "UID SEARCH SENTBEFORE %s"
  954. (format-time-string
  955. (format "%%d-%s-%%Y"
  956. (upcase
  957. (car (rassoc (nth 4 (decode-time cutoff))
  958. parse-time-months))))
  959. cutoff))))
  960. (and (car result)
  961. (delete 0 (mapcar #'string-to-number
  962. (cdr (assoc "SEARCH" (cdr result)))))))))))
  963. (defun nnimap-find-article-by-message-id (group server message-id
  964. &optional limit)
  965. "Search for message with MESSAGE-ID in GROUP from SERVER.
  966. If LIMIT, first try to limit the search to the N last articles."
  967. (with-current-buffer (nnimap-buffer)
  968. (erase-buffer)
  969. (let* ((change-group-result (nnimap-change-group group server nil t))
  970. (number-of-article
  971. (and (listp change-group-result)
  972. (catch 'found
  973. (dolist (result (cdr change-group-result))
  974. (when (equal "EXISTS" (cadr result))
  975. (throw 'found (car result)))))))
  976. (sequence
  977. (nnimap-send-command
  978. "UID SEARCH%s HEADER Message-Id %S"
  979. (if (and limit number-of-article)
  980. ;; The -1 is because IMAP message
  981. ;; numbers are one-based rather than
  982. ;; zero-based.
  983. (format " %s:*" (- (string-to-number number-of-article)
  984. limit -1))
  985. "")
  986. message-id)))
  987. (when (nnimap-wait-for-response sequence)
  988. (let ((article (car (last (cdr (assoc "SEARCH"
  989. (nnimap-parse-response)))))))
  990. (if article
  991. (string-to-number article)
  992. (when (and limit number-of-article)
  993. (nnimap-find-article-by-message-id group server message-id))))))))
  994. (defun nnimap-delete-article (articles)
  995. (with-current-buffer (nnimap-buffer)
  996. (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
  997. (nnimap-article-ranges articles))
  998. (cond
  999. ((nnimap-capability "UIDPLUS")
  1000. (nnimap-command "UID EXPUNGE %s"
  1001. (nnimap-article-ranges articles))
  1002. t)
  1003. (nnimap-expunge
  1004. (nnimap-command "EXPUNGE")
  1005. t)
  1006. (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
  1007. "server doesn't support UIDPLUS, so we won't "
  1008. "delete this article now"))))))
  1009. (deffoo nnimap-request-scan (&optional group server)
  1010. (when group
  1011. (setq group (nnimap-decode-gnus-group group)))
  1012. (when (and (nnimap-change-group nil server)
  1013. nnimap-inbox
  1014. nnimap-split-methods)
  1015. (nnheader-message 7 "nnimap %s splitting mail..." server)
  1016. (if (listp nnimap-inbox)
  1017. (dolist (nnimap-inbox nnimap-inbox)
  1018. (nnimap-split-incoming-mail))
  1019. (nnimap-split-incoming-mail))
  1020. (nnheader-message 7 "nnimap %s splitting mail...done" server)))
  1021. (defun nnimap-marks-to-flags (marks)
  1022. (let (flags flag)
  1023. (dolist (mark marks)
  1024. (when (setq flag (cadr (assq mark nnimap-mark-alist)))
  1025. (push flag flags)))
  1026. flags))
  1027. (deffoo nnimap-request-update-group-status (group status &optional server)
  1028. (setq group (nnimap-decode-gnus-group group))
  1029. (when (nnimap-change-group nil server)
  1030. (let ((command (assoc
  1031. status
  1032. '((subscribe "SUBSCRIBE")
  1033. (unsubscribe "UNSUBSCRIBE")))))
  1034. (when command
  1035. (with-current-buffer (nnimap-buffer)
  1036. (nnimap-command "%s %S" (cadr command) (utf7-encode group t)))))))
  1037. (deffoo nnimap-request-set-mark (group actions &optional server)
  1038. (setq group (nnimap-decode-gnus-group group))
  1039. (when (nnimap-change-group group server)
  1040. (let (sequence)
  1041. (with-current-buffer (nnimap-buffer)
  1042. (erase-buffer)
  1043. ;; Just send all the STORE commands without waiting for
  1044. ;; response. If they're successful, they're successful.
  1045. (dolist (action actions)
  1046. (destructuring-bind (range action marks) action
  1047. (let ((flags (nnimap-marks-to-flags marks)))
  1048. (when flags
  1049. (setq sequence (nnimap-send-command
  1050. "UID STORE %s %sFLAGS.SILENT (%s)"
  1051. (nnimap-article-ranges range)
  1052. (cond
  1053. ((eq action 'del) "-")
  1054. ((eq action 'add) "+")
  1055. ((eq action 'set) ""))
  1056. (mapconcat #'identity flags " ")))))))
  1057. ;; Wait for the last command to complete to avoid later
  1058. ;; synchronization problems with the stream.
  1059. (when sequence
  1060. (nnimap-wait-for-response sequence))))))
  1061. (deffoo nnimap-request-accept-article (group &optional server _last)
  1062. (unless group
  1063. ;; We're respooling. Find out where mail splitting would place
  1064. ;; this article.
  1065. (setq group
  1066. (caar
  1067. (nnmail-article-group
  1068. ;; We don't really care about the article number, because
  1069. ;; that's determined by the IMAP server later. So just
  1070. ;; return the group name.
  1071. `(lambda (group)
  1072. (list (list group)))))))
  1073. (setq group (nnimap-decode-gnus-group group))
  1074. (when (nnimap-change-group nil server)
  1075. (nnmail-check-syntax)
  1076. (let ((message-id (message-field-value "message-id"))
  1077. sequence message)
  1078. (nnimap-add-cr)
  1079. (setq message (buffer-substring-no-properties (point-min) (point-max)))
  1080. (with-current-buffer (nnimap-buffer)
  1081. (when (setq message (or (nnimap-process-quirk "OK Gimap " 'append message)
  1082. message))
  1083. ;; If we have this group open read-only, then unselect it
  1084. ;; before appending to it.
  1085. (when (equal (nnimap-examined nnimap-object) group)
  1086. (nnimap-unselect-group))
  1087. (erase-buffer)
  1088. (setq sequence (nnimap-send-command
  1089. "APPEND %S {%d}" (utf7-encode group t)
  1090. (length message)))
  1091. (unless nnimap-streaming
  1092. (nnimap-wait-for-connection "^[+]"))
  1093. (process-send-string (get-buffer-process (current-buffer)) message)
  1094. (process-send-string (get-buffer-process (current-buffer))
  1095. (if (nnimap-newlinep nnimap-object)
  1096. "\n"
  1097. "\r\n"))
  1098. (let ((result (nnimap-get-response sequence)))
  1099. (if (not (nnimap-ok-p result))
  1100. (progn
  1101. (nnheader-report 'nnimap "%s" result)
  1102. nil)
  1103. (cons group
  1104. (or (nnimap-find-uid-response "APPENDUID" (car result))
  1105. (nnimap-find-article-by-message-id
  1106. group server message-id
  1107. nnimap-request-articles-find-limit))))))))))
  1108. (defun nnimap-process-quirk (greeting-match type data)
  1109. (when (and (nnimap-greeting nnimap-object)
  1110. (string-match greeting-match (nnimap-greeting nnimap-object))
  1111. (eq type 'append)
  1112. (string-match "\000" data))
  1113. (let ((choice (gnus-multiple-choice
  1114. "Message contains NUL characters. Delete, continue, abort? "
  1115. '((?d "Delete NUL characters")
  1116. (?c "Try to APPEND the message as is")
  1117. (?a "Abort")))))
  1118. (cond
  1119. ((eq choice ?a)
  1120. (nnheader-report 'nnimap "Aborted APPEND due to NUL characters"))
  1121. ((eq choice ?c)
  1122. data)
  1123. (t
  1124. (with-temp-buffer
  1125. (insert data)
  1126. (goto-char (point-min))
  1127. (while (search-forward "\000" nil t)
  1128. (replace-match "" t t))
  1129. (buffer-string)))))))
  1130. (defun nnimap-ok-p (value)
  1131. (and (consp value)
  1132. (consp (car value))
  1133. (equal (caar value) "OK")))
  1134. (defun nnimap-find-uid-response (name list)
  1135. (let ((result (car (last (nnimap-find-response-element name list)))))
  1136. (and result
  1137. (string-to-number result))))
  1138. (defun nnimap-find-response-element (name list)
  1139. (let (result)
  1140. (dolist (elem list)
  1141. (when (and (consp elem)
  1142. (equal name (car elem)))
  1143. (setq result elem)))
  1144. result))
  1145. (deffoo nnimap-request-replace-article (article group buffer)
  1146. (setq group (nnimap-decode-gnus-group group))
  1147. (let (group-art)
  1148. (when (and (nnimap-change-group group)
  1149. ;; Put the article into the group.
  1150. (with-current-buffer buffer
  1151. (setq group-art
  1152. (nnimap-request-accept-article group nil t))))
  1153. (nnimap-delete-article (list article))
  1154. ;; Return the new article number.
  1155. (cdr group-art))))
  1156. (defun nnimap-add-cr ()
  1157. (goto-char (point-min))
  1158. (while (re-search-forward "\r?\n" nil t)
  1159. (replace-match "\r\n" t t)))
  1160. (defun nnimap-get-groups ()
  1161. (erase-buffer)
  1162. (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
  1163. groups)
  1164. (nnimap-wait-for-response sequence)
  1165. (subst-char-in-region (point-min) (point-max)
  1166. ?\\ ?% t)
  1167. (goto-char (point-min))
  1168. (nnimap-unfold-quoted-lines)
  1169. (goto-char (point-min))
  1170. (while (search-forward "* LIST " nil t)
  1171. (let ((flags (read (current-buffer)))
  1172. (_separator (read (current-buffer)))
  1173. (group (buffer-substring-no-properties
  1174. (progn (skip-chars-forward " \"")
  1175. (point))
  1176. (progn (end-of-line)
  1177. (skip-chars-backward " \r\"")
  1178. (point)))))
  1179. (unless (member '%NoSelect flags)
  1180. (push (utf7-decode (if (stringp group)
  1181. group
  1182. (format "%s" group))
  1183. t)
  1184. groups))))
  1185. (nreverse groups)))
  1186. (defun nnimap-get-responses (sequences)
  1187. (let (responses)
  1188. (dolist (sequence sequences)
  1189. (goto-char (point-min))
  1190. (when (re-search-forward (format "^%d " sequence) nil t)
  1191. (push (list sequence (nnimap-parse-response))
  1192. responses)))
  1193. responses))
  1194. (deffoo nnimap-request-list (&optional server)
  1195. (when (nnimap-change-group nil server)
  1196. (with-current-buffer nntp-server-buffer
  1197. (erase-buffer)
  1198. (let ((groups
  1199. (with-current-buffer (nnimap-buffer)
  1200. (nnimap-get-groups)))
  1201. sequences responses)
  1202. (when groups
  1203. (with-current-buffer (nnimap-buffer)
  1204. (setf (nnimap-group nnimap-object) nil)
  1205. (dolist (group groups)
  1206. (setf (nnimap-examined nnimap-object) group)
  1207. (push (list (nnimap-send-command "EXAMINE %S"
  1208. (utf7-encode group t))
  1209. group)
  1210. sequences))
  1211. (nnimap-wait-for-response (caar sequences))
  1212. (setq responses
  1213. (nnimap-get-responses (mapcar #'car sequences))))
  1214. (dolist (response responses)
  1215. (let* ((sequence (car response))
  1216. (response (cadr response))
  1217. (group (cadr (assoc sequence sequences)))
  1218. (egroup (encode-coding-string group 'utf-8)))
  1219. (when (and group
  1220. (equal (caar response) "OK"))
  1221. (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
  1222. highest exists)
  1223. (dolist (elem response)
  1224. (when (equal (cadr elem) "EXISTS")
  1225. (setq exists (string-to-number (car elem)))))
  1226. (when uidnext
  1227. (setq highest (1- (string-to-number (car uidnext)))))
  1228. (cond
  1229. ((null highest)
  1230. (insert (format "%S 0 1 y\n" egroup)))
  1231. ((zerop exists)
  1232. ;; Empty group.
  1233. (insert (format "%S %d %d y\n" egroup
  1234. highest (1+ highest))))
  1235. (t
  1236. ;; Return the widest possible range.
  1237. (insert (format "%S %d 1 y\n" egroup
  1238. (or highest exists)))))))))
  1239. t)))))
  1240. (deffoo nnimap-request-newgroups (_date &optional server)
  1241. (when (nnimap-change-group nil server)
  1242. (with-current-buffer nntp-server-buffer
  1243. (erase-buffer)
  1244. (dolist (group (with-current-buffer (nnimap-buffer)
  1245. (nnimap-get-groups)))
  1246. (unless (assoc group nnimap-current-infos)
  1247. ;; Insert dummy numbers here -- they don't matter.
  1248. (insert (format "%S 0 1 y\n" (encode-coding-string group 'utf-8)))))
  1249. t)))
  1250. (deffoo nnimap-retrieve-group-data-early (server infos)
  1251. (when (and (nnimap-change-group nil server)
  1252. infos)
  1253. (with-current-buffer (nnimap-buffer)
  1254. (erase-buffer)
  1255. (setf (nnimap-group nnimap-object) nil)
  1256. (setf (nnimap-initial-resync nnimap-object) 0)
  1257. (let ((qresyncp (nnimap-capability "QRESYNC"))
  1258. params sequences active uidvalidity modseq group
  1259. unexist)
  1260. ;; Go through the infos and gather the data needed to know
  1261. ;; what and how to request the data.
  1262. (dolist (info infos)
  1263. (setq params (gnus-info-params info)
  1264. group (nnimap-decode-gnus-group
  1265. (gnus-group-real-name (gnus-info-group info)))
  1266. active (cdr (assq 'active params))
  1267. unexist (assq 'unexist (gnus-info-marks info))
  1268. uidvalidity (cdr (assq 'uidvalidity params))
  1269. modseq (cdr (assq 'modseq params)))
  1270. (setf (nnimap-examined nnimap-object) group)
  1271. (if (and qresyncp
  1272. uidvalidity
  1273. active
  1274. modseq
  1275. unexist)
  1276. (push
  1277. (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
  1278. (utf7-encode group t)
  1279. (nnimap-quirk "QRESYNC")
  1280. uidvalidity modseq)
  1281. 'qresync
  1282. nil group 'qresync)
  1283. sequences)
  1284. (let ((command
  1285. (if uidvalidity
  1286. "EXAMINE"
  1287. ;; If we don't have a UIDVALIDITY, then this is
  1288. ;; the first time we've seen the group, so we
  1289. ;; have to do a SELECT (which is slower than an
  1290. ;; examine), but will tell us whether the group
  1291. ;; is read-only or not.
  1292. "SELECT"))
  1293. start)
  1294. (if (and active uidvalidity unexist)
  1295. ;; Fetch the last 100 flags.
  1296. (setq start (max 1 (- (cdr active) 100)))
  1297. (incf (nnimap-initial-resync nnimap-object))
  1298. (setq start 1))
  1299. (push (list (nnimap-send-command "%s %S" command
  1300. (utf7-encode group t))
  1301. (nnimap-send-command "UID FETCH %d:* FLAGS" start)
  1302. start group command)
  1303. sequences))))
  1304. sequences))))
  1305. (defun nnimap-quirk (command)
  1306. (let ((quirk (assoc command nnimap-quirks)))
  1307. ;; If this server is of a type that matches a quirk, then return
  1308. ;; the "quirked" command instead of the proper one.
  1309. (if (or (null quirk)
  1310. (not (string-match (nth 1 quirk) (nnimap-greeting nnimap-object))))
  1311. command
  1312. (nth 2 quirk))))
  1313. (deffoo nnimap-finish-retrieve-group-infos (server infos sequences
  1314. &optional dont-insert)
  1315. (when (and sequences
  1316. (nnimap-change-group nil server t)
  1317. ;; Check that the process is still alive.
  1318. (get-buffer-process (nnimap-buffer))
  1319. (memq (process-status (get-buffer-process (nnimap-buffer)))
  1320. '(open run)))
  1321. (with-current-buffer (nnimap-buffer)
  1322. ;; Wait for the final data to trickle in.
  1323. (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
  1324. (caar sequences)
  1325. (cadar sequences))
  1326. t)
  1327. ;; Now we should have most of the data we need, no matter
  1328. ;; whether we're QRESYNCING, fetching all the flags from
  1329. ;; scratch, or just fetching the last 100 flags per group.
  1330. (nnimap-update-infos (nnimap-flags-to-marks
  1331. (nnimap-parse-flags
  1332. (nreverse sequences)))
  1333. infos)
  1334. (unless dont-insert
  1335. ;; Finally, just return something resembling an active file in
  1336. ;; the nntp buffer, so that the agent can save the info, too.
  1337. (with-current-buffer nntp-server-buffer
  1338. (erase-buffer)
  1339. (dolist (info infos)
  1340. (let* ((group (gnus-info-group info))
  1341. (active (gnus-active group)))
  1342. (when active
  1343. (insert (format "%S %d %d y\n"
  1344. (decode-coding-string
  1345. (gnus-group-real-name group) 'utf-8)
  1346. (cdr active)
  1347. (car active))))))))))))
  1348. (defun nnimap-update-infos (flags infos)
  1349. (dolist (info infos)
  1350. (let* ((group (nnimap-decode-gnus-group
  1351. (gnus-group-real-name (gnus-info-group info))))
  1352. (marks (cdr (assoc group flags))))
  1353. (when marks
  1354. (nnimap-update-info info marks)))))
  1355. (defun nnimap-update-info (info marks)
  1356. (destructuring-bind (existing flags high low uidnext start-article
  1357. permanent-flags uidvalidity
  1358. vanished highestmodseq) marks
  1359. (cond
  1360. ;; Ignore groups with no UIDNEXT/marks. This happens for
  1361. ;; completely empty groups.
  1362. ((and (not existing)
  1363. (not uidnext))
  1364. (let ((active (cdr (assq 'active (gnus-info-params info)))))
  1365. (when active
  1366. (gnus-set-active (gnus-info-group info) active))))
  1367. ;; We have a mismatch between the old and new UIDVALIDITY
  1368. ;; identifiers, so we have to re-request the group info (the next
  1369. ;; time). This virtually never happens.
  1370. ((let ((old-uidvalidity
  1371. (cdr (assq 'uidvalidity (gnus-info-params info)))))
  1372. (and old-uidvalidity
  1373. (not (equal old-uidvalidity uidvalidity))
  1374. (or (not start-article)
  1375. (> start-article 1))))
  1376. (gnus-group-remove-parameter info 'uidvalidity)
  1377. (gnus-group-remove-parameter info 'modseq))
  1378. ;; We have the data needed to update.
  1379. (t
  1380. (let* ((group (gnus-info-group info))
  1381. (completep (and start-article
  1382. (= start-article 1)))
  1383. (active (or (gnus-active group)
  1384. (cdr (assq 'active (gnus-info-params info))))))
  1385. (when uidnext
  1386. (setq high (1- uidnext)))
  1387. ;; First set the active ranges based on high/low.
  1388. (if (or completep
  1389. (not (gnus-active group)))
  1390. (gnus-set-active group
  1391. (cond
  1392. (active
  1393. (cons (min (or low (car active))
  1394. (car active))
  1395. (max (or high (cdr active))
  1396. (cdr active))))
  1397. ((and low high)
  1398. (cons low high))
  1399. (uidnext
  1400. ;; No articles in this group.
  1401. (cons uidnext (1- uidnext)))
  1402. (start-article
  1403. (cons start-article (1- start-article)))
  1404. (t
  1405. ;; No articles and no uidnext.
  1406. nil)))
  1407. (gnus-set-active group
  1408. (cons (car active)
  1409. (or high (1- uidnext)))))
  1410. ;; See whether this is a read-only group.
  1411. (unless (eq permanent-flags 'not-scanned)
  1412. (gnus-group-set-parameter
  1413. info 'permanent-flags
  1414. (and (or (memq '%* permanent-flags)
  1415. (memq '%Seen permanent-flags))
  1416. permanent-flags)))
  1417. ;; Update marks and read articles if this isn't a
  1418. ;; read-only IMAP group.
  1419. (when (setq permanent-flags
  1420. (cdr (assq 'permanent-flags (gnus-info-params info))))
  1421. (if (and highestmodseq
  1422. (not start-article))
  1423. ;; We've gotten the data by QRESYNCing.
  1424. (nnimap-update-qresync-info
  1425. info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
  1426. ;; Do normal non-QRESYNC flag updates.
  1427. ;; Update the list of read articles.
  1428. (let* ((unread
  1429. (gnus-compress-sequence
  1430. (gnus-set-difference
  1431. (gnus-set-difference
  1432. existing
  1433. (gnus-sorted-union
  1434. (cdr (assoc '%Seen flags))
  1435. (cdr (assoc '%Deleted flags))))
  1436. (cdr (assoc '%Flagged flags)))))
  1437. (read (gnus-range-difference
  1438. (cons start-article high) unread)))
  1439. (when (> start-article 1)
  1440. (setq read
  1441. (gnus-range-nconcat
  1442. (if (> start-article 1)
  1443. (gnus-sorted-range-intersection
  1444. (cons 1 (1- start-article))
  1445. (gnus-info-read info))
  1446. (gnus-info-read info))
  1447. read)))
  1448. (when (or (not (listp permanent-flags))
  1449. (memq '%Seen permanent-flags))
  1450. (gnus-info-set-read info read))
  1451. ;; Update the marks.
  1452. (setq marks (gnus-info-marks info))
  1453. (dolist (type (cdr nnimap-mark-alist))
  1454. (when (or (not (listp permanent-flags))
  1455. (memq (car (assoc (caddr type) flags))
  1456. permanent-flags)
  1457. (memq '%* permanent-flags))
  1458. (let ((old-marks (assoc (car type) marks))
  1459. (new-marks
  1460. (gnus-compress-sequence
  1461. (cdr (or (assoc (caddr type) flags) ; %Flagged
  1462. (assoc (intern (cadr type) obarray) flags)
  1463. (assoc (cadr type) flags)))))) ; "\Flagged"
  1464. (setq marks (delq old-marks marks))
  1465. (pop old-marks)
  1466. (when (and old-marks
  1467. (> start-article 1))
  1468. (setq old-marks (gnus-range-difference
  1469. old-marks
  1470. (cons start-article high)))
  1471. (setq new-marks (gnus-range-nconcat old-marks new-marks)))
  1472. (when new-marks
  1473. (push (cons (car type) new-marks) marks)))))
  1474. ;; Keep track of non-existing articles.
  1475. (let* ((old-unexists (assq 'unexist marks))
  1476. (active (gnus-active group))
  1477. (unexists
  1478. (if completep
  1479. (gnus-range-difference
  1480. active
  1481. (gnus-compress-sequence existing))
  1482. (gnus-add-to-range
  1483. (cdr old-unexists)
  1484. (gnus-list-range-difference
  1485. existing (gnus-active group))))))
  1486. (when (> (car active) 1)
  1487. (setq unexists (gnus-range-add
  1488. (cons 1 (1- (car active)))
  1489. unexists)))
  1490. (if old-unexists
  1491. (setcdr old-unexists unexists)
  1492. (push (cons 'unexist unexists) marks)))
  1493. (gnus-info-set-marks info marks t))))
  1494. ;; Tell Gnus whether there are any \Recent messages in any of
  1495. ;; the groups.
  1496. (let ((recent (cdr (assoc '%Recent flags))))
  1497. (when (and active
  1498. recent
  1499. (> (car (last recent)) (cdr active)))
  1500. (push (list (cons (gnus-group-real-name group) 0))
  1501. nnmail-split-history)))
  1502. ;; Note the active level for the next run-through.
  1503. (gnus-group-set-parameter info 'active (gnus-active group))
  1504. (gnus-group-set-parameter info 'uidvalidity uidvalidity)
  1505. (gnus-group-set-parameter info 'modseq highestmodseq)
  1506. (nnimap-store-info info (gnus-active group)))))))
  1507. (defun nnimap-update-qresync-info (info existing vanished flags)
  1508. ;; Add all the vanished articles to the list of read articles.
  1509. (gnus-info-set-read
  1510. info
  1511. (gnus-add-to-range
  1512. (gnus-add-to-range
  1513. (gnus-range-add (gnus-info-read info)
  1514. vanished)
  1515. (cdr (assq '%Flagged flags)))
  1516. (cdr (assq '%Seen flags))))
  1517. (let ((marks (gnus-info-marks info)))
  1518. (dolist (type (cdr nnimap-mark-alist))
  1519. (let ((ticks (assoc (car type) marks))
  1520. (new-marks
  1521. (cdr (or (assoc (caddr type) flags) ; %Flagged
  1522. (assoc (intern (cadr type) obarray) flags)
  1523. (assoc (cadr type) flags))))) ; "\Flagged"
  1524. (setq marks (delq ticks marks))
  1525. (pop ticks)
  1526. ;; Add the new marks we got.
  1527. (setq ticks (gnus-add-to-range ticks new-marks))
  1528. ;; Remove the marks from messages that don't have them.
  1529. (setq ticks (gnus-remove-from-range
  1530. ticks
  1531. (gnus-compress-sequence
  1532. (gnus-sorted-complement existing new-marks))))
  1533. (when ticks
  1534. (push (cons (car type) ticks) marks)))
  1535. (gnus-info-set-marks info marks t))
  1536. ;; Add vanished to the list of unexisting articles.
  1537. (when vanished
  1538. (let* ((old-unexists (assq 'unexist marks))
  1539. (unexists (gnus-range-add (cdr old-unexists) vanished)))
  1540. (if old-unexists
  1541. (setcdr old-unexists unexists)
  1542. (push (cons 'unexist unexists) marks)))
  1543. (gnus-info-set-marks info marks t))))
  1544. (defun nnimap-imap-ranges-to-gnus-ranges (irange)
  1545. (if (zerop (length irange))
  1546. nil
  1547. (let ((result nil))
  1548. (dolist (elem (split-string irange ","))
  1549. (push
  1550. (if (string-match ":" elem)
  1551. (let ((numbers (split-string elem ":")))
  1552. (cons (string-to-number (car numbers))
  1553. (string-to-number (cadr numbers))))
  1554. (string-to-number elem))
  1555. result))
  1556. (nreverse result))))
  1557. (defun nnimap-store-info (info active)
  1558. (let* ((group (gnus-group-real-name (gnus-info-group info)))
  1559. (entry (assoc group nnimap-current-infos)))
  1560. (if entry
  1561. (setcdr entry (list info active))
  1562. (push (list group info active) nnimap-current-infos))))
  1563. (defun nnimap-flags-to-marks (groups)
  1564. (let (data group uidnext articles start-article mark permanent-flags
  1565. uidvalidity vanished highestmodseq)
  1566. (dolist (elem groups)
  1567. (setq group (car elem)
  1568. uidnext (nth 1 elem)
  1569. start-article (nth 2 elem)
  1570. permanent-flags (nth 3 elem)
  1571. uidvalidity (nth 4 elem)
  1572. vanished (nth 5 elem)
  1573. highestmodseq (nth 6 elem)
  1574. articles (nthcdr 7 elem))
  1575. (let ((high (caar articles))
  1576. marks low existing)
  1577. (dolist (article articles)
  1578. (setq low (car article))
  1579. (push (car article) existing)
  1580. (dolist (flag (cdr article))
  1581. (setq mark (assoc flag marks))
  1582. (if (not mark)
  1583. (push (list flag (car article)) marks)
  1584. (setcdr mark (cons (car article) (cdr mark))))))
  1585. (push (list group existing marks high low uidnext start-article
  1586. permanent-flags uidvalidity vanished highestmodseq)
  1587. data)))
  1588. data))
  1589. (defun nnimap-parse-flags (sequences)
  1590. (goto-char (point-min))
  1591. ;; Change \Delete etc to %Delete, so that the Emacs Lisp reader can
  1592. ;; read it.
  1593. (subst-char-in-region (point-min) (point-max)
  1594. ?\\ ?% t)
  1595. ;; Remove any MODSEQ entries in the buffer, because they may contain
  1596. ;; numbers that are too large for 32-bit Emacsen.
  1597. (while (re-search-forward " MODSEQ ([0-9]+)" nil t)
  1598. (replace-match "" t t))
  1599. (goto-char (point-min))
  1600. (let (start end articles groups uidnext elems permanent-flags
  1601. uidvalidity vanished highestmodseq)
  1602. (dolist (elem sequences)
  1603. (destructuring-bind (group-sequence flag-sequence totalp group command)
  1604. elem
  1605. (setq start (point))
  1606. (when (and
  1607. ;; The EXAMINE was successful.
  1608. (search-forward (format "\n%d OK " group-sequence) nil t)
  1609. (progn
  1610. (forward-line 1)
  1611. (setq end (point))
  1612. (goto-char start)
  1613. (setq permanent-flags
  1614. (if (equal command "SELECT")
  1615. (and (search-forward "PERMANENTFLAGS "
  1616. (or end (point-min)) t)
  1617. (read (current-buffer)))
  1618. 'not-scanned))
  1619. (goto-char start)
  1620. (setq uidnext
  1621. (and (search-forward "UIDNEXT "
  1622. (or end (point-min)) t)
  1623. (read (current-buffer))))
  1624. (goto-char start)
  1625. (setq uidvalidity
  1626. (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
  1627. (or end (point-min)) t)
  1628. ;; Store UIDVALIDITY as a string, as it's
  1629. ;; too big for 32-bit Emacsen, usually.
  1630. (match-string 1)))
  1631. (goto-char start)
  1632. (setq vanished
  1633. (and (eq flag-sequence 'qresync)
  1634. (re-search-forward "^\\* VANISHED .*? \\([0-9:,]+\\)"
  1635. (or end (point-min)) t)
  1636. (match-string 1)))
  1637. (goto-char start)
  1638. (setq highestmodseq
  1639. (and (re-search-forward "HIGHESTMODSEQ \\([0-9]+\\)"
  1640. (or end (point-min)) t)
  1641. (match-string 1)))
  1642. (goto-char end)
  1643. (forward-line -1))
  1644. ;; The UID FETCH FLAGS was successful.
  1645. (or (eq flag-sequence 'qresync)
  1646. (search-forward (format "\n%d OK " flag-sequence) nil t)))
  1647. (if (eq flag-sequence 'qresync)
  1648. (progn
  1649. (goto-char start)
  1650. (setq start end))
  1651. (setq start (point))
  1652. (goto-char end))
  1653. (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
  1654. (progn
  1655. (setq elems (read (current-buffer)))
  1656. (push (cons (cadr (memq 'UID elems))
  1657. (cadr (memq 'FLAGS elems)))
  1658. articles)))
  1659. (push (nconc (list group uidnext totalp permanent-flags uidvalidity
  1660. vanished highestmodseq)
  1661. articles)
  1662. groups)
  1663. (if (eq flag-sequence 'qresync)
  1664. (goto-char end)
  1665. (setq end (point)))
  1666. (setq articles nil))))
  1667. groups))
  1668. (defun nnimap-find-process-buffer (buffer)
  1669. (cadr (assoc buffer nnimap-connection-alist)))
  1670. (deffoo nnimap-request-post (&optional _server)
  1671. (setq nnimap-status-string "Read-only server")
  1672. nil)
  1673. (defvar gnus-refer-thread-use-nnir) ;; gnus-sum.el
  1674. (declare-function gnus-fetch-headers "gnus-sum"
  1675. (articles &optional limit force-new dependencies))
  1676. (autoload 'nnir-search-thread "nnir")
  1677. (deffoo nnimap-request-thread (header &optional group server)
  1678. (when group
  1679. (setq group (nnimap-decode-gnus-group group)))
  1680. (if gnus-refer-thread-use-nnir
  1681. (nnir-search-thread header)
  1682. (when (nnimap-change-group group server)
  1683. (let* ((cmd (nnimap-make-thread-query header))
  1684. (result (with-current-buffer (nnimap-buffer)
  1685. (nnimap-command "UID SEARCH %s" cmd))))
  1686. (when result
  1687. (gnus-fetch-headers
  1688. (and (car result)
  1689. (delete 0 (mapcar #'string-to-number
  1690. (cdr (assoc "SEARCH" (cdr result))))))
  1691. nil t))))))
  1692. (defun nnimap-change-group (group &optional server no-reconnect read-only)
  1693. "Change group to GROUP if non-nil.
  1694. If SERVER is set, check that server is connected, otherwise retry
  1695. to reconnect, unless NO-RECONNECT is set to t. Return nil if
  1696. unsuccessful in connecting.
  1697. If GROUP is nil, return t.
  1698. If READ-ONLY is set, send EXAMINE rather than SELECT to the server.
  1699. Return the server's response to the SELECT or EXAMINE command."
  1700. (let ((open-result t))
  1701. (when (and server
  1702. (not (nnimap-server-opened server)))
  1703. (setq open-result (nnimap-open-server server nil no-reconnect)))
  1704. (cond
  1705. ((not open-result)
  1706. nil)
  1707. ((not group)
  1708. t)
  1709. (t
  1710. (with-current-buffer (nnimap-buffer)
  1711. (let ((result (nnimap-command "%s %S"
  1712. (if read-only
  1713. "EXAMINE"
  1714. "SELECT")
  1715. (utf7-encode group t))))
  1716. (when (car result)
  1717. (setf (nnimap-group nnimap-object) group
  1718. (nnimap-select-result nnimap-object) result)
  1719. result)))))))
  1720. (defun nnimap-find-connection (buffer)
  1721. "Find the connection delivering to BUFFER."
  1722. (let ((entry (assoc buffer nnimap-connection-alist)))
  1723. (when entry
  1724. (if (and (buffer-name (cadr entry))
  1725. (get-buffer-process (cadr entry))
  1726. (memq (process-status (get-buffer-process (cadr entry)))
  1727. '(open run)))
  1728. (get-buffer-process (cadr entry))
  1729. (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
  1730. nil))))
  1731. (defvar nnimap-sequence 0)
  1732. (defun nnimap-send-command (&rest args)
  1733. (setf (nnimap-last-command-time nnimap-object) (current-time))
  1734. (process-send-string
  1735. (get-buffer-process (current-buffer))
  1736. (nnimap-log-command
  1737. (format "%d %s%s\n"
  1738. (incf nnimap-sequence)
  1739. (apply #'format args)
  1740. (if (nnimap-newlinep nnimap-object)
  1741. ""
  1742. "\r"))))
  1743. ;; Some servers apparently can't have many outstanding
  1744. ;; commands, so throttle them.
  1745. (unless nnimap-streaming
  1746. (nnimap-wait-for-response nnimap-sequence))
  1747. nnimap-sequence)
  1748. (defvar nnimap-record-commands nil
  1749. "If non-nil, log commands to the \"*imap log*\" buffer.")
  1750. (defun nnimap-log-buffer ()
  1751. (let ((name "*imap log*"))
  1752. (or (get-buffer name)
  1753. (with-current-buffer (get-buffer-create name)
  1754. (when (boundp 'window-point-insertion-type)
  1755. (make-local-variable 'window-point-insertion-type)
  1756. (setq window-point-insertion-type t))
  1757. (current-buffer)))))
  1758. (defun nnimap-log-command (command)
  1759. (when nnimap-record-commands
  1760. (with-current-buffer (nnimap-log-buffer)
  1761. (goto-char (point-max))
  1762. (insert (format-time-string "%H:%M:%S")
  1763. " [" nnimap-address "] "
  1764. (if nnimap-inhibit-logging
  1765. "(inhibited)\n"
  1766. command))))
  1767. command)
  1768. (defun nnimap-command (&rest args)
  1769. (erase-buffer)
  1770. (let* ((sequence (apply #'nnimap-send-command args))
  1771. (response (nnimap-get-response sequence)))
  1772. (if (equal (caar response) "OK")
  1773. (cons t response)
  1774. (nnheader-report 'nnimap "%s"
  1775. (mapconcat (lambda (a)
  1776. (format "%s" a))
  1777. (car response) " "))
  1778. nil)))
  1779. (defun nnimap-get-response (sequence)
  1780. (nnimap-wait-for-response sequence)
  1781. (nnimap-parse-response))
  1782. (defun nnimap-wait-for-connection (&optional regexp)
  1783. (nnimap-wait-for-line (or regexp "^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
  1784. (defun nnimap-wait-for-line (regexp &optional response-regexp)
  1785. (let ((process (get-buffer-process (current-buffer))))
  1786. (goto-char (point-min))
  1787. (while (and (memq (process-status process)
  1788. '(open run))
  1789. (not (re-search-forward regexp nil t)))
  1790. (nnheader-accept-process-output process)
  1791. (goto-char (point-min)))
  1792. (forward-line -1)
  1793. (and (looking-at (or response-regexp regexp))
  1794. (match-string 1))))
  1795. (defun nnimap-wait-for-response (sequence &optional messagep)
  1796. (let ((process (get-buffer-process (current-buffer)))
  1797. openp)
  1798. (condition-case nil
  1799. (progn
  1800. (goto-char (point-max))
  1801. (while (and (setq openp (memq (process-status process)
  1802. '(open run)))
  1803. (progn
  1804. ;; Skip past any "*" lines that the server has
  1805. ;; output.
  1806. (while (and (not (bobp))
  1807. (progn
  1808. (forward-line -1)
  1809. (looking-at "\\*\\|[0-9]+ OK NOOP"))))
  1810. (not (looking-at (format "%d .*\n" sequence)))))
  1811. (when messagep
  1812. (nnheader-message-maybe
  1813. 7 "nnimap read %dk from %s%s" (/ (buffer-size) 1000)
  1814. nnimap-address
  1815. (if (not (zerop (nnimap-initial-resync nnimap-object)))
  1816. (format " (initial sync of %d group%s; please wait)"
  1817. (nnimap-initial-resync nnimap-object)
  1818. (if (= (nnimap-initial-resync nnimap-object) 1)
  1819. ""
  1820. "s"))
  1821. "")))
  1822. (nnheader-accept-process-output process)
  1823. (goto-char (point-max)))
  1824. (setf (nnimap-initial-resync nnimap-object) 0)
  1825. openp)
  1826. (quit
  1827. (when debug-on-quit
  1828. (debug "Quit"))
  1829. ;; The user hit C-g while we were waiting: kill the process, in case
  1830. ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
  1831. ;; NAT routers).
  1832. (delete-process process)
  1833. nil))))
  1834. (defun nnimap-parse-response ()
  1835. (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
  1836. result)
  1837. (dolist (line lines)
  1838. (push (cdr (nnimap-parse-line line)) result))
  1839. ;; Return the OK/error code first, and then all the "continuation
  1840. ;; lines" afterwards.
  1841. (cons (pop result)
  1842. (nreverse result))))
  1843. ;; Parse an IMAP response line lightly. They look like
  1844. ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
  1845. ;; the lines into a list of strings and lists of string.
  1846. (defun nnimap-parse-line (line)
  1847. (let (char result)
  1848. (with-temp-buffer
  1849. (mm-disable-multibyte)
  1850. (insert line)
  1851. (goto-char (point-min))
  1852. (while (not (eobp))
  1853. (if (eql (setq char (following-char)) ? )
  1854. (forward-char 1)
  1855. (push
  1856. (cond
  1857. ((eql char ?\[)
  1858. (split-string
  1859. (buffer-substring
  1860. (1+ (point))
  1861. (if (search-forward "]" (line-end-position) 'move)
  1862. (1- (point))
  1863. (point)))))
  1864. ((eql char ?\()
  1865. (split-string
  1866. (buffer-substring
  1867. (1+ (point))
  1868. (if (search-forward ")" (line-end-position) 'move)
  1869. (1- (point))
  1870. (point)))))
  1871. ((eql char ?\")
  1872. (forward-char 1)
  1873. (buffer-substring
  1874. (point)
  1875. (1- (or (search-forward "\"" (line-end-position) 'move)
  1876. (point)))))
  1877. (t
  1878. (buffer-substring (point) (if (search-forward " " nil t)
  1879. (1- (point))
  1880. (goto-char (point-max))))))
  1881. result)))
  1882. (nreverse result))))
  1883. (defun nnimap-last-response-string ()
  1884. (save-excursion
  1885. (forward-line 1)
  1886. (let ((end (point)))
  1887. (forward-line -1)
  1888. (when (not (bobp))
  1889. (forward-line -1)
  1890. (while (and (not (bobp))
  1891. (eql (following-char) ?*))
  1892. (forward-line -1))
  1893. (unless (eql (following-char) ?*)
  1894. (forward-line 1)))
  1895. (buffer-substring (point) end))))
  1896. (defvar nnimap-incoming-split-list nil)
  1897. (defun nnimap-fetch-inbox (articles)
  1898. (erase-buffer)
  1899. (nnimap-wait-for-response
  1900. (nnimap-send-command
  1901. "UID FETCH %s %s"
  1902. (nnimap-article-ranges articles)
  1903. (format "(UID %s%s)"
  1904. (format
  1905. (if (nnimap-ver4-p)
  1906. "BODY.PEEK"
  1907. "RFC822.PEEK"))
  1908. (cond
  1909. (nnimap-split-download-body-default
  1910. "[]")
  1911. ((nnimap-ver4-p)
  1912. "[HEADER]")
  1913. (t
  1914. "[1]"))))
  1915. t))
  1916. (defun nnimap-split-incoming-mail ()
  1917. (with-current-buffer (nnimap-buffer)
  1918. (let ((nnimap-incoming-split-list nil)
  1919. (nnmail-split-methods
  1920. (cond
  1921. ((eq nnimap-split-methods 'default)
  1922. nnmail-split-methods)
  1923. (nnimap-split-methods
  1924. nnimap-split-methods)
  1925. (nnimap-split-fancy
  1926. 'nnmail-split-fancy)))
  1927. (nnmail-split-fancy (or nnimap-split-fancy
  1928. nnmail-split-fancy))
  1929. (nnmail-inhibit-default-split-group t)
  1930. (groups (nnimap-get-groups))
  1931. (can-move (nnimap-capability "MOVE"))
  1932. new-articles)
  1933. (erase-buffer)
  1934. (nnimap-command "SELECT %S" nnimap-inbox)
  1935. (setf (nnimap-group nnimap-object) nnimap-inbox)
  1936. (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
  1937. (when new-articles
  1938. (nnimap-fetch-inbox new-articles)
  1939. (nnimap-transform-split-mail)
  1940. (nnheader-ms-strip-cr)
  1941. (nnmail-cache-open)
  1942. (nnmail-split-incoming (current-buffer)
  1943. #'nnimap-save-mail-spec
  1944. nil nil
  1945. #'nnimap-dummy-active-number
  1946. #'nnimap-save-mail-spec)
  1947. (when nnimap-incoming-split-list
  1948. (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
  1949. sequences junk-articles)
  1950. ;; Create any groups that doesn't already exist on the
  1951. ;; server first.
  1952. (dolist (spec specs)
  1953. (when (and (not (member (car spec) groups))
  1954. (not (eq (car spec) 'junk)))
  1955. (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
  1956. ;; Then copy over all the messages.
  1957. (erase-buffer)
  1958. (dolist (spec specs)
  1959. (let ((group (car spec))
  1960. (ranges (cdr spec)))
  1961. (if (eq group 'junk)
  1962. (setq junk-articles ranges)
  1963. ;; Don't copy if the message is already in its
  1964. ;; target group.
  1965. (unless (string= group nnimap-inbox)
  1966. (push (list (nnimap-send-command
  1967. (if can-move
  1968. "UID MOVE %s %S"
  1969. "UID COPY %s %S")
  1970. (nnimap-article-ranges ranges)
  1971. (utf7-encode group t))
  1972. ranges)
  1973. sequences)))))
  1974. ;; Wait for the last COPY response...
  1975. (when (and (not can-move) sequences)
  1976. (nnimap-wait-for-response (caar sequences))
  1977. ;; And then mark the successful copy actions as deleted,
  1978. ;; and possibly expunge them.
  1979. (nnimap-mark-and-expunge-incoming
  1980. (nnimap-parse-copied-articles sequences)))
  1981. (nnimap-mark-and-expunge-incoming junk-articles)))))))
  1982. (defun nnimap-mark-and-expunge-incoming (range)
  1983. (when range
  1984. (setq range (nnimap-article-ranges range))
  1985. (erase-buffer)
  1986. (let ((sequence
  1987. (nnimap-send-command
  1988. "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
  1989. (cond
  1990. ;; If the server supports it, we now delete the message we have
  1991. ;; just copied over.
  1992. ((nnimap-capability "UIDPLUS")
  1993. (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
  1994. ;; If it doesn't support UID EXPUNGE, then we only expunge if the
  1995. ;; user has configured it.
  1996. (nnimap-expunge
  1997. (setq sequence (nnimap-send-command "EXPUNGE"))))
  1998. (nnimap-wait-for-response sequence))))
  1999. (defun nnimap-parse-copied-articles (sequences)
  2000. (let (sequence copied range)
  2001. (goto-char (point-min))
  2002. (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t)
  2003. (setq sequence (string-to-number (match-string 1)))
  2004. (when (setq range (cadr (assq sequence sequences)))
  2005. (push (gnus-uncompress-range range) copied)))
  2006. (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
  2007. (defun nnimap-new-articles (flags)
  2008. (let (new)
  2009. (dolist (elem flags)
  2010. (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
  2011. (cdr elem))
  2012. (push (car elem) new)))
  2013. (gnus-compress-sequence (nreverse new))))
  2014. (defun nnimap-make-split-specs (list)
  2015. (let ((specs nil)
  2016. entry)
  2017. (dolist (elem list)
  2018. (destructuring-bind (article spec) elem
  2019. (dolist (group (delete nil (mapcar #'car spec)))
  2020. (unless (setq entry (assoc group specs))
  2021. (push (setq entry (list group)) specs))
  2022. (setcdr entry (cons article (cdr entry))))))
  2023. (dolist (entry specs)
  2024. (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
  2025. specs))
  2026. (defun nnimap-transform-split-mail ()
  2027. (goto-char (point-min))
  2028. (let (article bytes)
  2029. (block nil
  2030. (while (not (eobp))
  2031. (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
  2032. (delete-region (point) (progn (forward-line 1) (point)))
  2033. (when (eobp)
  2034. (return)))
  2035. (setq article (match-string 1)
  2036. bytes (nnimap-get-length))
  2037. (delete-region (line-beginning-position) (line-end-position))
  2038. ;; Insert MMDF separator, and a way to remember what this
  2039. ;; article UID is.
  2040. (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
  2041. (forward-char (1+ bytes))
  2042. (setq bytes (nnimap-get-length))
  2043. (delete-region (line-beginning-position) (line-end-position))
  2044. ;; There's a body; skip past that.
  2045. (when bytes
  2046. (forward-char (1+ bytes))
  2047. (delete-region (line-beginning-position) (line-end-position)))))))
  2048. (defun nnimap-dummy-active-number (_group &optional _server)
  2049. 1)
  2050. (defun nnimap-save-mail-spec (group-art &optional _server _full-nov)
  2051. (let (article)
  2052. (goto-char (point-min))
  2053. (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
  2054. (error "Invalid nnimap mail")
  2055. (setq article (string-to-number (match-string 1))))
  2056. (push (list article
  2057. (if (eq group-art 'junk)
  2058. (list (cons 'junk 1))
  2059. group-art))
  2060. nnimap-incoming-split-list)))
  2061. (defun nnimap-make-thread-query (header)
  2062. (let* ((id (mail-header-id header))
  2063. (refs (split-string
  2064. (or (mail-header-references header)
  2065. "")))
  2066. (value
  2067. (format
  2068. "(OR HEADER REFERENCES %S HEADER Message-Id %S)"
  2069. id id)))
  2070. (dolist (refid refs value)
  2071. (setq value (format
  2072. "(OR (OR HEADER Message-Id %S HEADER REFERENCES %S) %s)"
  2073. refid refid value)))))
  2074. (provide 'nnimap)
  2075. ;;; nnimap.el ends here