nnimap.el 68 KB

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