pop3.el 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. ;;; pop3.el --- Post Office Protocol (RFC 1460) interface
  2. ;; Copyright (C) 1996-2017 Free Software Foundation, Inc.
  3. ;; Author: Richard L. Pieri <ratinox@peorth.gweep.net>
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: mail
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Most of the standard Post Office Protocol version 3 (RFC 1460) commands
  19. ;; are implemented. The LIST command has not been implemented due to lack
  20. ;; of actual usefulness.
  21. ;; The optional POP3 command TOP has not been implemented.
  22. ;; This program was inspired by Kyle E. Jones's vm-pop program.
  23. ;;; Code:
  24. (eval-when-compile (require 'cl))
  25. (require 'mail-utils)
  26. (defvar parse-time-months)
  27. (defgroup pop3 nil
  28. "Post Office Protocol."
  29. :group 'mail
  30. :group 'mail-source)
  31. (defcustom pop3-maildrop (or (user-login-name)
  32. (getenv "LOGNAME")
  33. (getenv "USER"))
  34. "POP3 maildrop."
  35. :version "22.1" ;; Oort Gnus
  36. :type 'string
  37. :group 'pop3)
  38. (defcustom pop3-mailhost (or (getenv "MAILHOST") ;; nil -> mismatch
  39. "pop3")
  40. "POP3 mailhost."
  41. :version "22.1" ;; Oort Gnus
  42. :type 'string
  43. :group 'pop3)
  44. (defcustom pop3-port 110
  45. "POP3 port."
  46. :version "22.1" ;; Oort Gnus
  47. :type 'number
  48. :group 'pop3)
  49. (defcustom pop3-password-required t
  50. "Non-nil if a password is required when connecting to POP server."
  51. :version "22.1" ;; Oort Gnus
  52. :type 'boolean
  53. :group 'pop3)
  54. ;; Should this be customizable?
  55. (defcustom pop3-password nil
  56. "Password to use when connecting to POP server."
  57. :type '(choice (const nil) string)
  58. :group 'pop3)
  59. (defcustom pop3-authentication-scheme 'pass
  60. "POP3 authentication scheme.
  61. Defaults to `pass', for the standard USER/PASS authentication. The other
  62. valid value is `apop'."
  63. :type '(choice (const :tag "Normal user/password" pass)
  64. (const :tag "APOP" apop))
  65. :version "22.1" ;; Oort Gnus
  66. :group 'pop3)
  67. (defcustom pop3-stream-length 100
  68. "How many messages should be requested at one time.
  69. The lower the number, the more latency-sensitive the fetching
  70. will be. If your pop3 server doesn't support streaming at all,
  71. set this to 1."
  72. :type 'number
  73. :version "24.1"
  74. :group 'pop3)
  75. (defcustom pop3-leave-mail-on-server nil
  76. "Non-nil if the mail is to be left on the POP server after fetching.
  77. Mails once fetched will never be fetched again by the UIDL control.
  78. If this is neither nil nor a number, all mails will be left on the
  79. server. If this is a number, leave mails on the server for this many
  80. days since you first checked new mails. If this is nil, mails will be
  81. deleted on the server right after fetching.
  82. Gnus users should use the `:leave' keyword in a mail source to direct
  83. the behavior per server, rather than directly modifying this value.
  84. Note that POP servers maintain no state information between sessions,
  85. so what the client believes is there and what is actually there may
  86. not match up. If they do not, then you may get duplicate mails or
  87. the whole thing can fall apart and leave you with a corrupt mailbox."
  88. :version "24.4"
  89. :type '(choice (const :tag "Don't leave mails" nil)
  90. (const :tag "Leave all mails" t)
  91. (number :tag "Leave mails for this many days" :value 14))
  92. :group 'pop3)
  93. (defcustom pop3-uidl-file "~/.pop3-uidl"
  94. "File used to save UIDL."
  95. :version "24.4"
  96. :type 'file
  97. :group 'pop3)
  98. (defcustom pop3-uidl-file-backup '(0 9)
  99. "How to backup the UIDL file `pop3-uidl-file' when updating.
  100. If it is a list of numbers, the first one binds `kept-old-versions' and
  101. the other binds `kept-new-versions' to keep number of oldest and newest
  102. versions. Otherwise, the value binds `version-control' (which see).
  103. Note: Backup will take place whenever you check new mails on a server.
  104. So, you may lose the backup files having been saved before a trouble
  105. if you set it so as to make too few backups whereas you have access to
  106. many servers."
  107. :version "24.4"
  108. :type '(choice (group :tag "Keep versions" :format "\n%v" :indent 3
  109. (number :tag "oldest")
  110. (number :tag "newest"))
  111. (sexp :format "%v"
  112. :match (lambda (widget value)
  113. (condition-case nil
  114. (not (and (numberp (car value))
  115. (numberp (car (cdr value)))))
  116. (error t)))))
  117. :group 'pop3)
  118. (defvar pop3-timestamp nil
  119. "Timestamp returned when initially connected to the POP server.
  120. Used for APOP authentication.")
  121. (defvar pop3-read-point nil)
  122. (defvar pop3-debug nil)
  123. ;; Borrowed from nnheader-accept-process-output in nnheader.el. See the
  124. ;; comments there for explanations about the values.
  125. (eval-and-compile
  126. (if (and (fboundp 'nnheader-accept-process-output)
  127. (boundp 'nnheader-read-timeout))
  128. (defalias 'pop3-accept-process-output 'nnheader-accept-process-output)
  129. ;; Borrowed from `nnheader.el':
  130. (defvar pop3-read-timeout
  131. (if (memq system-type '(windows-nt cygwin))
  132. 1.0
  133. 0.01)
  134. "How long pop3 should wait between checking for the end of output.
  135. Shorter values mean quicker response, but are more CPU intensive.")
  136. (defun pop3-accept-process-output (process)
  137. (accept-process-output
  138. process
  139. (truncate pop3-read-timeout)
  140. (truncate (* (- pop3-read-timeout
  141. (truncate pop3-read-timeout))
  142. 1000))))))
  143. (defvar pop3-uidl)
  144. ;; List of UIDLs of existing messages at present in the server:
  145. ;; ("UIDL1" "UIDL2" "UIDL3"...)
  146. (defvar pop3-uidl-saved)
  147. ;; Locally saved UIDL data; an alist of the server, the user, and the UIDL
  148. ;; and timestamp pairs:
  149. ;; (("SERVER_A" ("USER_A1" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
  150. ;; ("USER_A2" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
  151. ;; ...)
  152. ;; ("SERVER_B" ("USER_B1" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
  153. ;; ("USER_B2" "UIDL1" TIMESTAMP1 "UIDL2" TIMESTAMP2...)
  154. ;; ...))
  155. ;; Where TIMESTAMP is the most significant two digits of an Emacs time,
  156. ;; i.e. the return value of `current-time'.
  157. ;;;###autoload
  158. (defun pop3-movemail (file)
  159. "Transfer contents of a maildrop to the specified FILE.
  160. Use streaming commands."
  161. (let ((process (pop3-open-server pop3-mailhost pop3-port))
  162. messages total-size
  163. pop3-uidl
  164. pop3-uidl-saved)
  165. (pop3-logon process)
  166. (if pop3-leave-mail-on-server
  167. (setq messages (pop3-uidl-stat process)
  168. total-size (cadr messages)
  169. messages (car messages))
  170. (let ((size (pop3-stat process)))
  171. (dotimes (i (car size)) (push (1+ i) messages))
  172. (setq messages (nreverse messages)
  173. total-size (cadr size))))
  174. (when messages
  175. (with-current-buffer (process-buffer process)
  176. (pop3-send-streaming-command process "RETR" messages total-size)
  177. (pop3-write-to-file file messages)
  178. (unless pop3-leave-mail-on-server
  179. (pop3-send-streaming-command process "DELE" messages nil))))
  180. (if pop3-leave-mail-on-server
  181. (when (prog1 (pop3-uidl-dele process) (pop3-quit process))
  182. (pop3-uidl-save))
  183. (pop3-quit process)
  184. ;; Remove UIDL data for the account that got not to leave mails.
  185. (setq pop3-uidl-saved (pop3-uidl-load))
  186. (let ((elt (assoc pop3-maildrop
  187. (cdr (assoc pop3-mailhost pop3-uidl-saved)))))
  188. (when elt
  189. (setcdr elt nil)
  190. (pop3-uidl-save))))
  191. t))
  192. (defun pop3-send-streaming-command (process command messages total-size)
  193. (erase-buffer)
  194. (let ((count (length messages))
  195. (i 1)
  196. (start-point (point-min))
  197. (waited-for 0))
  198. (while messages
  199. (process-send-string process (format "%s %d\r\n" command (pop messages)))
  200. ;; Only do 100 messages at a time to avoid pipe stalls.
  201. (when (zerop (% i pop3-stream-length))
  202. (setq start-point
  203. (pop3-wait-for-messages process pop3-stream-length
  204. total-size start-point))
  205. (incf waited-for pop3-stream-length))
  206. (incf i))
  207. (pop3-wait-for-messages process (- count waited-for)
  208. total-size start-point)))
  209. (defun pop3-wait-for-messages (process count total-size start-point)
  210. (while (> count 0)
  211. (goto-char start-point)
  212. (while (or (and (re-search-forward "^\\+OK" nil t)
  213. (or (not total-size)
  214. (re-search-forward "^\\.\r?\n" nil t)))
  215. (re-search-forward "^-ERR " nil t))
  216. (decf count)
  217. (setq start-point (point)))
  218. (unless (memq (process-status process) '(open run))
  219. (error "pop3 process died"))
  220. (when total-size
  221. (let ((size 0))
  222. (goto-char (point-min))
  223. (while (re-search-forward "^\\+OK.*\n" nil t)
  224. (setq size (+ size (- (point))
  225. (if (re-search-forward "^\\.\r?\n" nil 'move)
  226. (match-beginning 0)
  227. (point)))))
  228. (message "pop3 retrieved %dKB (%d%%)"
  229. (truncate (/ size 1000))
  230. (truncate (* (/ (* size 1.0) total-size) 100)))))
  231. (pop3-accept-process-output process))
  232. start-point)
  233. (defun pop3-write-to-file (file messages)
  234. (let ((pop-buffer (current-buffer))
  235. (start (point-min))
  236. beg end
  237. temp-buffer)
  238. (with-temp-buffer
  239. (setq temp-buffer (current-buffer))
  240. (with-current-buffer pop-buffer
  241. (goto-char (point-min))
  242. (while (re-search-forward "^\\+OK" nil t)
  243. (forward-line 1)
  244. (setq beg (point))
  245. (when (re-search-forward "^\\.\r?\n" nil t)
  246. (setq start (point))
  247. (forward-line -1)
  248. (setq end (point)))
  249. (with-current-buffer temp-buffer
  250. (goto-char (point-max))
  251. (let ((hstart (point)))
  252. (insert-buffer-substring pop-buffer beg end)
  253. (pop3-clean-region hstart (point))
  254. (goto-char (point-max))
  255. (pop3-munge-message-separator hstart (point))
  256. (when pop3-leave-mail-on-server
  257. (pop3-uidl-add-xheader hstart (pop messages)))
  258. (goto-char (point-max))))))
  259. (let ((coding-system-for-write 'binary))
  260. (goto-char (point-min))
  261. ;; Check whether something inserted a newline at the start and
  262. ;; delete it.
  263. (when (eolp)
  264. (delete-char 1))
  265. (write-region (point-min) (point-max) file nil 'nomesg)))))
  266. (defun pop3-logon (process)
  267. (let ((pop3-password pop3-password))
  268. ;; for debugging only
  269. (if pop3-debug (switch-to-buffer (process-buffer process)))
  270. ;; query for password
  271. (if (and pop3-password-required (not pop3-password))
  272. (setq pop3-password
  273. (read-passwd (format "Password for %s: " pop3-maildrop))))
  274. (cond ((equal 'apop pop3-authentication-scheme)
  275. (pop3-apop process pop3-maildrop))
  276. ((equal 'pass pop3-authentication-scheme)
  277. (pop3-user process pop3-maildrop)
  278. (pop3-pass process))
  279. (t (error "Invalid POP3 authentication scheme")))))
  280. (defun pop3-get-message-count ()
  281. "Return the number of messages in the maildrop."
  282. (let* ((process (pop3-open-server pop3-mailhost pop3-port))
  283. message-count
  284. (pop3-password pop3-password))
  285. ;; for debugging only
  286. (if pop3-debug (switch-to-buffer (process-buffer process)))
  287. ;; query for password
  288. (if (and pop3-password-required (not pop3-password))
  289. (setq pop3-password
  290. (read-passwd (format "Password for %s: " pop3-maildrop))))
  291. (cond ((equal 'apop pop3-authentication-scheme)
  292. (pop3-apop process pop3-maildrop))
  293. ((equal 'pass pop3-authentication-scheme)
  294. (pop3-user process pop3-maildrop)
  295. (pop3-pass process))
  296. (t (error "Invalid POP3 authentication scheme")))
  297. (setq message-count (car (pop3-stat process)))
  298. (pop3-quit process)
  299. message-count))
  300. (defun pop3-uidl-stat (process)
  301. "Return a list of unread message numbers and total size."
  302. (pop3-send-command process "UIDL")
  303. (let (err messages size)
  304. (if (condition-case code
  305. (progn
  306. (pop3-read-response process)
  307. t)
  308. (error (setq err (error-message-string code))
  309. nil))
  310. (let ((start pop3-read-point)
  311. saved list)
  312. (with-current-buffer (process-buffer process)
  313. (while (not (re-search-forward "^\\.\r\n" nil t))
  314. (unless (memq (process-status process) '(open run))
  315. (error "pop3 server closed the connection"))
  316. (pop3-accept-process-output process)
  317. (goto-char start))
  318. (setq pop3-read-point (point-marker)
  319. pop3-uidl nil)
  320. (while (progn (forward-line -1) (>= (point) start))
  321. (when (looking-at "[0-9]+ \\([^\n\r ]+\\)")
  322. (push (match-string 1) pop3-uidl)))
  323. (when pop3-uidl
  324. (setq pop3-uidl-saved (pop3-uidl-load)
  325. saved (cdr (assoc pop3-maildrop
  326. (cdr (assoc pop3-mailhost
  327. pop3-uidl-saved)))))
  328. (let ((i (length pop3-uidl)))
  329. (while (> i 0)
  330. (unless (member (nth (1- i) pop3-uidl) saved)
  331. (push i messages))
  332. (decf i)))
  333. (when messages
  334. (setq list (pop3-list process)
  335. size 0)
  336. (dolist (msg messages)
  337. (setq size (+ size (cdr (assq msg list)))))
  338. (list messages size)))))
  339. (message "%s doesn't support UIDL (%s), so we try a regressive way..."
  340. pop3-mailhost err)
  341. (sit-for 1)
  342. (setq size (pop3-stat process))
  343. (dotimes (i (car size)) (push (1+ i) messages))
  344. (setcar size (nreverse messages))
  345. size)))
  346. (defun pop3-uidl-dele (process)
  347. "Delete messages according to `pop3-leave-mail-on-server'.
  348. Return non-nil if it is necessary to update the local UIDL file."
  349. (let* ((ctime (current-time))
  350. (srvr (assoc pop3-mailhost pop3-uidl-saved))
  351. (saved (assoc pop3-maildrop (cdr srvr)))
  352. i uidl mod new tstamp dele)
  353. (setcdr (cdr ctime) nil)
  354. ;; Add new messages to the data to be saved.
  355. (cond ((and pop3-uidl saved)
  356. (setq i (1- (length pop3-uidl)))
  357. (while (>= i 0)
  358. (unless (member (setq uidl (nth i pop3-uidl)) (cdr saved))
  359. (push ctime new)
  360. (push uidl new))
  361. (decf i)))
  362. (pop3-uidl
  363. (setq new (mapcan (lambda (elt) (list elt ctime)) pop3-uidl))))
  364. (when new (setq mod t))
  365. ;; List expirable messages and delete them from the data to be saved.
  366. (setq ctime (when (numberp pop3-leave-mail-on-server)
  367. (/ (+ (* (car ctime) 65536.0) (cadr ctime)) 86400))
  368. i (1- (length saved)))
  369. (while (> i 0)
  370. (if (member (setq uidl (nth (1- i) saved)) pop3-uidl)
  371. (progn
  372. (setq tstamp (nth i saved))
  373. (if (and ctime
  374. (> (- ctime (/ (+ (* (car tstamp) 65536.0) (cadr tstamp))
  375. 86400))
  376. pop3-leave-mail-on-server))
  377. ;; Mails to delete.
  378. (progn
  379. (setq mod t)
  380. (push uidl dele))
  381. ;; Mails to keep.
  382. (push tstamp new)
  383. (push uidl new)))
  384. ;; Mails having been deleted in the server.
  385. (setq mod t))
  386. (decf i 2))
  387. (cond (saved
  388. (setcdr saved new))
  389. (srvr
  390. (setcdr (last srvr) (list (cons pop3-maildrop new))))
  391. (t
  392. (add-to-list 'pop3-uidl-saved
  393. (list pop3-mailhost (cons pop3-maildrop new))
  394. t)))
  395. ;; Actually delete the messages in the server.
  396. (when dele
  397. (setq uidl nil
  398. i (length pop3-uidl))
  399. (while (> i 0)
  400. (when (member (nth (1- i) pop3-uidl) dele)
  401. (push i uidl))
  402. (decf i))
  403. (when uidl
  404. (pop3-send-streaming-command process "DELE" uidl nil)))
  405. mod))
  406. (defun pop3-uidl-load ()
  407. "Load saved UIDL."
  408. (when (file-exists-p pop3-uidl-file)
  409. (with-temp-buffer
  410. (condition-case code
  411. (progn
  412. (insert-file-contents pop3-uidl-file)
  413. (goto-char (point-min))
  414. (read (current-buffer)))
  415. (error
  416. (message "Error while loading %s (%s)"
  417. pop3-uidl-file (error-message-string code))
  418. (sit-for 1)
  419. nil)))))
  420. (defun pop3-uidl-save ()
  421. "Save UIDL."
  422. (with-temp-buffer
  423. (if pop3-uidl-saved
  424. (progn
  425. (insert "(")
  426. (dolist (srvr pop3-uidl-saved)
  427. (when (cdr srvr)
  428. (insert "(\"" (pop srvr) "\"\n ")
  429. (dolist (elt srvr)
  430. (when (cdr elt)
  431. (insert "(\"" (pop elt) "\"\n ")
  432. (while elt
  433. (insert (format "\"%s\" %s\n " (pop elt) (pop elt))))
  434. (delete-char -4)
  435. (insert ")\n ")))
  436. (delete-char -3)
  437. (if (eq (char-before) ?\))
  438. (insert ")\n ")
  439. (goto-char (1+ (point-at-bol)))
  440. (delete-region (point) (point-max)))))
  441. (when (eq (char-before) ? )
  442. (delete-char -2))
  443. (insert ")\n"))
  444. (insert "()\n"))
  445. (let ((buffer-file-name pop3-uidl-file)
  446. (delete-old-versions t)
  447. (kept-new-versions kept-new-versions)
  448. (kept-old-versions kept-old-versions)
  449. (version-control version-control))
  450. (if (consp pop3-uidl-file-backup)
  451. (setq kept-new-versions (cadr pop3-uidl-file-backup)
  452. kept-old-versions (car pop3-uidl-file-backup)
  453. version-control t)
  454. (setq version-control pop3-uidl-file-backup))
  455. (save-buffer))))
  456. (defun pop3-uidl-add-xheader (start msgno)
  457. "Add X-UIDL header."
  458. (let ((case-fold-search t))
  459. (save-restriction
  460. (narrow-to-region start (progn
  461. (goto-char start)
  462. (search-forward "\n\n" nil 'move)
  463. (1- (point))))
  464. (goto-char start)
  465. (while (re-search-forward "^x-uidl:" nil t)
  466. (while (progn
  467. (forward-line 1)
  468. (memq (char-after) '(?\t ? ))))
  469. (delete-region (match-beginning 0) (point)))
  470. (goto-char (point-max))
  471. (insert "X-UIDL: " (nth (1- msgno) pop3-uidl) "\n"))))
  472. (defcustom pop3-stream-type nil
  473. "Transport security type for POP3 connections.
  474. This may be either nil (plain connection), `ssl' (use an
  475. SSL/TSL-secured stream) or `starttls' (use the starttls mechanism
  476. to turn on TLS security after opening the stream). However, if
  477. this is nil, `ssl' is assumed for connections to port
  478. 995 (pop3s)."
  479. :version "23.1" ;; No Gnus
  480. :group 'pop3
  481. :type '(choice (const :tag "Plain" nil)
  482. (const :tag "SSL/TLS" ssl)
  483. (const starttls)))
  484. (defun pop3-open-server (mailhost port)
  485. "Open TCP connection to MAILHOST on PORT.
  486. Returns the process associated with the connection."
  487. (let ((coding-system-for-read 'binary)
  488. (coding-system-for-write 'binary)
  489. result)
  490. (with-current-buffer
  491. (get-buffer-create (concat " trace of POP session to "
  492. mailhost))
  493. (erase-buffer)
  494. (setq pop3-read-point (point-min))
  495. (setq result
  496. (open-network-stream
  497. "POP" (current-buffer) mailhost port
  498. :type (cond
  499. ((or (eq pop3-stream-type 'ssl)
  500. (and (not pop3-stream-type)
  501. (member port '(995 "pop3s"))))
  502. 'tls)
  503. (t
  504. (or pop3-stream-type 'network)))
  505. :warn-unless-encrypted t
  506. :capability-command "CAPA\r\n"
  507. :end-of-command "^\\(-ERR\\|+OK\\).*\n"
  508. :end-of-capability "^\\.\r?\n\\|^-ERR"
  509. :success "^\\+OK.*\n"
  510. :return-list t
  511. :starttls-function
  512. (lambda (capabilities)
  513. (and (string-match "\\bSTLS\\b" capabilities)
  514. "STLS\r\n"))))
  515. (when result
  516. (let ((response (plist-get (cdr result) :greeting)))
  517. (setq pop3-timestamp
  518. (substring response (or (string-match "<" response) 0)
  519. (+ 1 (or (string-match ">" response) -1)))))
  520. (set-process-query-on-exit-flag (car result) nil)
  521. (erase-buffer)
  522. (car result)))))
  523. ;; Support functions
  524. (defun pop3-send-command (process command)
  525. (set-buffer (process-buffer process))
  526. (goto-char (point-max))
  527. ;; (if (= (aref command 0) ?P)
  528. ;; (insert "PASS <omitted>\r\n")
  529. ;; (insert command "\r\n"))
  530. (setq pop3-read-point (point))
  531. (goto-char (point-max))
  532. (process-send-string process (concat command "\r\n")))
  533. (defun pop3-read-response (process &optional return)
  534. "Read the response from the server.
  535. Return the response string if optional second argument is non-nil."
  536. (let ((case-fold-search nil)
  537. match-end)
  538. (with-current-buffer (process-buffer process)
  539. (goto-char pop3-read-point)
  540. (while (and (memq (process-status process) '(open run))
  541. (not (search-forward "\r\n" nil t)))
  542. (pop3-accept-process-output process)
  543. (goto-char pop3-read-point))
  544. (setq match-end (point))
  545. (goto-char pop3-read-point)
  546. (if (looking-at "-ERR")
  547. (error "%s" (buffer-substring (point) (- match-end 2)))
  548. (if (not (looking-at "+OK"))
  549. (progn (setq pop3-read-point match-end) nil)
  550. (setq pop3-read-point match-end)
  551. (if return
  552. (buffer-substring (point) match-end)
  553. t)
  554. )))))
  555. (defun pop3-clean-region (start end)
  556. (setq end (set-marker (make-marker) end))
  557. (save-excursion
  558. (goto-char start)
  559. (while (and (< (point) end) (search-forward "\r\n" end t))
  560. (replace-match "\n" t t))
  561. (goto-char start)
  562. (while (and (< (point) end) (re-search-forward "^\\." end t))
  563. (replace-match "" t t)
  564. (forward-char)))
  565. (set-marker end nil))
  566. ;; Copied from message-make-date.
  567. (defun pop3-make-date (&optional now)
  568. "Make a valid date header.
  569. If NOW, use that time instead."
  570. (require 'parse-time)
  571. (let* ((now (or now (current-time)))
  572. (zone (nth 8 (decode-time now)))
  573. (sign "+"))
  574. (when (< zone 0)
  575. (setq sign "-")
  576. (setq zone (- zone)))
  577. (concat
  578. (format-time-string "%d" now)
  579. ;; The month name of the %b spec is locale-specific. Pfff.
  580. (format " %s "
  581. (capitalize (car (rassoc (nth 4 (decode-time now))
  582. parse-time-months))))
  583. (format-time-string "%Y %H:%M:%S %z" now))))
  584. (defun pop3-munge-message-separator (start end)
  585. "Check to see if a message separator exists. If not, generate one."
  586. (save-excursion
  587. (save-restriction
  588. (narrow-to-region start end)
  589. (goto-char (point-min))
  590. (if (not (or (looking-at "From .?") ; Unix mail
  591. (looking-at "\001\001\001\001\n") ; MMDF
  592. (looking-at "BABYL OPTIONS:") ; Babyl
  593. ))
  594. (let* ((from (mail-strip-quoted-names (mail-fetch-field "From")))
  595. (tdate (mail-fetch-field "Date"))
  596. (date (split-string (or (and tdate
  597. (not (string= "" tdate))
  598. tdate)
  599. (pop3-make-date))
  600. " "))
  601. (From_))
  602. ;; sample date formats I have seen
  603. ;; Date: Tue, 9 Jul 1996 09:04:21 -0400 (EDT)
  604. ;; Date: 08 Jul 1996 23:22:24 -0400
  605. ;; should be
  606. ;; Tue Jul 9 09:04:21 1996
  607. ;; Fixme: This should use timezone on the date field contents.
  608. (setq date
  609. (cond ((not date)
  610. "Tue Jan 1 00:00:0 1900")
  611. ((string-match "[A-Z]" (nth 0 date))
  612. (format "%s %s %s %s %s"
  613. (nth 0 date) (nth 2 date) (nth 1 date)
  614. (nth 4 date) (nth 3 date)))
  615. (t
  616. ;; this really needs to be better but I don't feel
  617. ;; like writing a date to day converter.
  618. (format "Sun %s %s %s %s"
  619. (nth 1 date) (nth 0 date)
  620. (nth 3 date) (nth 2 date)))
  621. ))
  622. (setq From_ (format "\nFrom %s %s\n" from date))
  623. (while (string-match "," From_)
  624. (setq From_ (concat (substring From_ 0 (match-beginning 0))
  625. (substring From_ (match-end 0)))))
  626. (goto-char (point-min))
  627. (insert From_)
  628. (if (search-forward "\n\n" nil t)
  629. nil
  630. (goto-char (point-max))
  631. (insert "\n"))
  632. (let ((size (- (point-max) (point))))
  633. (forward-line -1)
  634. (insert (format "Content-Length: %s\n" size)))
  635. )))))
  636. ;; The Command Set
  637. ;; AUTHORIZATION STATE
  638. (defun pop3-user (process user)
  639. "Send USER information to POP3 server."
  640. (pop3-send-command process (format "USER %s" user))
  641. (let ((response (pop3-read-response process t)))
  642. (if (not (and response (string-match "+OK" response)))
  643. (error "USER %s not valid" user))))
  644. (defun pop3-pass (process)
  645. "Send authentication information to the server."
  646. (pop3-send-command process (format "PASS %s" pop3-password))
  647. (let ((response (pop3-read-response process t)))
  648. (if (not (and response (string-match "+OK" response)))
  649. (pop3-quit process))))
  650. (defun pop3-apop (process user)
  651. "Send alternate authentication information to the server."
  652. (let ((pass pop3-password))
  653. (if (and pop3-password-required (not pass))
  654. (setq pass
  655. (read-passwd (format "Password for %s: " pop3-maildrop))))
  656. (if pass
  657. (let ((hash (md5 (concat pop3-timestamp pass) nil nil 'binary)))
  658. (pop3-send-command process (format "APOP %s %s" user hash))
  659. (let ((response (pop3-read-response process t)))
  660. (if (not (and response (string-match "+OK" response)))
  661. (pop3-quit process)))))
  662. ))
  663. ;; TRANSACTION STATE
  664. (defun pop3-stat (process)
  665. "Return the number of messages in the maildrop and the maildrop's size."
  666. (pop3-send-command process "STAT")
  667. (let ((response (pop3-read-response process t)))
  668. (list (string-to-number (nth 1 (split-string response " ")))
  669. (string-to-number (nth 2 (split-string response " "))))
  670. ))
  671. (defun pop3-list (process &optional msg)
  672. "If MSG is nil, return an alist of (MESSAGE-ID . SIZE) pairs.
  673. Otherwise, return the size of the message-id MSG"
  674. (pop3-send-command process (if msg
  675. (format "LIST %d" msg)
  676. "LIST"))
  677. (let ((response (pop3-read-response process t)))
  678. (if msg
  679. (string-to-number (nth 2 (split-string response " ")))
  680. (let ((start pop3-read-point) end)
  681. (with-current-buffer (process-buffer process)
  682. (while (not (re-search-forward "^\\.\r\n" nil t))
  683. (pop3-accept-process-output process)
  684. (goto-char start))
  685. (setq pop3-read-point (point-marker))
  686. (goto-char (match-beginning 0))
  687. (setq end (point-marker))
  688. (mapcar #'(lambda (s) (let ((split (split-string s " ")))
  689. (cons (string-to-number (nth 0 split))
  690. (string-to-number (nth 1 split)))))
  691. (split-string (buffer-substring start end) "\r\n" t)))))))
  692. (defun pop3-retr (process msg crashbuf)
  693. "Retrieve message-id MSG to buffer CRASHBUF."
  694. (pop3-send-command process (format "RETR %s" msg))
  695. (pop3-read-response process)
  696. (let ((start pop3-read-point) end)
  697. (with-current-buffer (process-buffer process)
  698. (while (not (re-search-forward "^\\.\r\n" nil t))
  699. (unless (memq (process-status process) '(open run))
  700. (error "pop3 server closed the connection"))
  701. (pop3-accept-process-output process)
  702. (goto-char start))
  703. (setq pop3-read-point (point-marker))
  704. ;; this code does not seem to work for some POP servers...
  705. ;; and I cannot figure out why not.
  706. ;; (goto-char (match-beginning 0))
  707. ;; (backward-char 2)
  708. ;; (if (not (looking-at "\r\n"))
  709. ;; (insert "\r\n"))
  710. ;; (re-search-forward "\\.\r\n")
  711. (goto-char (match-beginning 0))
  712. (setq end (point-marker))
  713. (pop3-clean-region start end)
  714. (pop3-munge-message-separator start end)
  715. (with-current-buffer crashbuf
  716. (erase-buffer))
  717. (copy-to-buffer crashbuf start end)
  718. (delete-region start end)
  719. )))
  720. (defun pop3-dele (process msg)
  721. "Mark message-id MSG as deleted."
  722. (pop3-send-command process (format "DELE %s" msg))
  723. (pop3-read-response process))
  724. (defun pop3-noop (process msg)
  725. "No-operation."
  726. (pop3-send-command process "NOOP")
  727. (pop3-read-response process))
  728. (defun pop3-last (process)
  729. "Return highest accessed message-id number for the session."
  730. (pop3-send-command process "LAST")
  731. (let ((response (pop3-read-response process t)))
  732. (string-to-number (nth 1 (split-string response " ")))
  733. ))
  734. (defun pop3-rset (process)
  735. "Remove all delete marks from current maildrop."
  736. (pop3-send-command process "RSET")
  737. (pop3-read-response process))
  738. ;; UPDATE
  739. (defun pop3-quit (process)
  740. "Close connection to POP3 server.
  741. Tell server to remove all messages marked as deleted, unlock the maildrop,
  742. and close the connection."
  743. (pop3-send-command process "QUIT")
  744. (pop3-read-response process t)
  745. (if process
  746. (with-current-buffer (process-buffer process)
  747. (goto-char (point-max))
  748. (delete-process process))))
  749. ;; Summary of POP3 (Post Office Protocol version 3) commands and responses
  750. ;;; AUTHORIZATION STATE
  751. ;; Initial TCP connection
  752. ;; Arguments: none
  753. ;; Restrictions: none
  754. ;; Possible responses:
  755. ;; +OK [POP3 server ready]
  756. ;; USER name
  757. ;; Arguments: a server specific user-id (required)
  758. ;; Restrictions: authorization state [after unsuccessful USER or PASS
  759. ;; Possible responses:
  760. ;; +OK [valid user-id]
  761. ;; -ERR [invalid user-id]
  762. ;; PASS string
  763. ;; Arguments: a server/user-id specific password (required)
  764. ;; Restrictions: authorization state, after successful USER
  765. ;; Possible responses:
  766. ;; +OK [maildrop locked and ready]
  767. ;; -ERR [invalid password]
  768. ;; -ERR [unable to lock maildrop]
  769. ;; STLS (RFC 2595)
  770. ;; Arguments: none
  771. ;; Restrictions: Only permitted in AUTHORIZATION state.
  772. ;; Possible responses:
  773. ;; +OK
  774. ;; -ERR
  775. ;;; TRANSACTION STATE
  776. ;; STAT
  777. ;; Arguments: none
  778. ;; Restrictions: transaction state
  779. ;; Possible responses:
  780. ;; +OK nn mm [# of messages, size of maildrop]
  781. ;; LIST [msg]
  782. ;; Arguments: a message-id (optional)
  783. ;; Restrictions: transaction state; msg must not be deleted
  784. ;; Possible responses:
  785. ;; +OK [scan listing follows]
  786. ;; -ERR [no such message]
  787. ;; RETR msg
  788. ;; Arguments: a message-id (required)
  789. ;; Restrictions: transaction state; msg must not be deleted
  790. ;; Possible responses:
  791. ;; +OK [message contents follow]
  792. ;; -ERR [no such message]
  793. ;; DELE msg
  794. ;; Arguments: a message-id (required)
  795. ;; Restrictions: transaction state; msg must not be deleted
  796. ;; Possible responses:
  797. ;; +OK [message deleted]
  798. ;; -ERR [no such message]
  799. ;; NOOP
  800. ;; Arguments: none
  801. ;; Restrictions: transaction state
  802. ;; Possible responses:
  803. ;; +OK
  804. ;; LAST
  805. ;; Arguments: none
  806. ;; Restrictions: transaction state
  807. ;; Possible responses:
  808. ;; +OK nn [highest numbered message accessed]
  809. ;; RSET
  810. ;; Arguments: none
  811. ;; Restrictions: transaction state
  812. ;; Possible responses:
  813. ;; +OK [all delete marks removed]
  814. ;; UIDL [msg]
  815. ;; Arguments: a message-id (optional)
  816. ;; Restrictions: transaction state; msg must not be deleted
  817. ;; Possible responses:
  818. ;; +OK [uidl listing follows]
  819. ;; -ERR [no such message]
  820. ;;; UPDATE STATE
  821. ;; QUIT
  822. ;; Arguments: none
  823. ;; Restrictions: none
  824. ;; Possible responses:
  825. ;; +OK [TCP connection closed]
  826. (provide 'pop3)
  827. ;;; pop3.el ends here