| 1 |
;;; mew-smtp.el |
| 2 |
|
| 3 |
;; Author: Kazu Yamamoto <[email protected]> |
| 4 |
;; Created: Dec 3, 1999 |
| 5 |
|
| 6 |
;;; Code: |
| 7 |
|
| 8 |
(require 'mew) |
| •9 |
(autoload 'puny-encode-domain "puny") |
| 10 |
(when (and (fboundp 'gnutls-available-p) |
| 11 |
(gnutls-available-p)) |
| 12 |
(require 'gnutls) |
| 13 |
(require 'nsm)) |
| 14 |
|
| 15 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 16 |
;;; |
| 17 |
;;; SMTP info |
| 18 |
;;; |
| 19 |
|
| 20 |
(defvar mew-smtp-info-list |
| 21 |
'(;; parameters to be saved |
| 22 |
"raw-header" "recipients" "orig-recipients" |
| 23 |
"bcc" "fcc" "msgid" "logtime" |
| 24 |
"case" ;; save for re-edit |
| 25 |
;; parameters used internally |
| 26 |
"server" "port" "ssh-server" |
| 27 |
"user" "auth-user" "auth-list" |
| 28 |
"helo-domain" |
| 29 |
"status" "process" "ssh-process" "ssl-process" "ssl-p" |
| 30 |
"qfld" "messages" |
| 31 |
;; parameters used internally and should be initialized |
| 32 |
"string" "error" "auth-selected" "timer" "cont" "from" "sender" |
| 33 |
"done" "imapp" "capa" "fallback")) |
| 34 |
|
| 35 |
(mew-info-defun "mew-smtp-" mew-smtp-info-list) |
| 36 |
|
| 37 |
(defvar mew-smtp-info-list-save-length 8) |
| 38 |
(defvar mew-smtp-info-list-clean-length 21) |
| 39 |
|
| 40 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 41 |
;;; |
| 42 |
;;; FSM |
| 43 |
;;; |
| 44 |
|
| 45 |
(defvar mew-smtp-fsm |
| 46 |
'(("greeting" ("220" . "ehlo")) |
| 47 |
("ehlo" ("250" . "post-ehlo") (t . "helo")) |
| 48 |
;; |
| 49 |
("auth" ("250" . "next")) |
| 50 |
("auth-cram-md5" ("334" . "pwd-cram-md5") (t . "wpwd")) |
| •51 |
("pwd-cram-md5" ("235" . "next") (t . "wpwd")) |
| 52 |
("auth-login" ("334" . "user-login") (t . "wpwd")) |
| 53 |
("user-login" ("334" . "pwd-login") (t . "wpwd")) |
| 54 |
("pwd-login" ("235" . "next") (t . "wpwd")) |
| 55 |
("auth-plain" ("235" . "next") (t . "wpwd")) |
| 56 |
;; This is checked only for Gmail https://developers.google.com/gmail/imap/xoauth2-protocol |
| 57 |
;; XXX: MS Exchange Returns 334 like CRAM-MD5? |
| 58 |
;; https://docs.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth |
| 59 |
("auth-xoauth2" ("235" . "next") (t . "wpwd")) |
| 60 |
;; See blow |
| 61 |
;; ("auth-plain" ("334" . "pwd-plain") (t . "wpwd")) |
| 62 |
;; ("pwd-plain" ("235" . "next") (t . "wpwd")) |
| 63 |
;; |
| 64 |
("helo" ("250" . "next")) |
| 65 |
("mail-from" ("250" . "rcpt-to")) |
| 66 |
("rcpt-to" ("250" . "data")) |
| 67 |
("data" ("354" . "content")) |
| 68 |
("content" ("250" . "done")) |
| 69 |
("quit" (t . "noop")))) |
| 70 |
|
| 71 |
(defun mew-smtp-fsm-by-status (status) |
| 72 |
(assoc status mew-smtp-fsm)) |
| 73 |
|
| 74 |
(defun mew-smtp-fsm-next (status code) |
| 75 |
(cdr (mew-assoc-match2 code (cdr (mew-smtp-fsm-by-status status)) 0))) |
| 76 |
|
| 77 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 78 |
;;; |
| 79 |
;;; Filters |
| 80 |
;;; |
| 81 |
|
| 82 |
(defun mew-smtp-command-helo (pro pnm) |
| 83 |
(let ((helo-domain (mew-smtp-get-helo-domain pnm))) |
| 84 |
(mew-smtp-process-send-string pro "HELO %s" helo-domain))) |
| 85 |
|
| 86 |
(defun mew-smtp-command-ehlo (pro pnm) |
| 87 |
(let ((helo-domain (mew-smtp-get-helo-domain pnm))) |
| 88 |
(mew-smtp-process-send-string pro "EHLO %s" helo-domain))) |
| 89 |
|
| 90 |
(defun mew-smtp-command-post-ehlo (pro pnm) |
| 91 |
(let ((str (mew-smtp-get-string pnm)) (start 0) capa) |
| 92 |
(catch 'loop |
| 93 |
(while (string-match "^250\\([- ]?\\)\\(.*\\)$" str start) |
| 94 |
(setq capa (cons (mew-split (match-string 2 str) 32) capa)) |
| 95 |
(if (string= (match-string 1 str) " ") |
| 96 |
(throw 'loop nil)) |
| 97 |
(setq start (match-end 0)))) |
| 98 |
(mew-smtp-set-capa pnm (nreverse capa)) |
| 99 |
(mew-smtp-set-status pnm "auth") |
| 100 |
(mew-smtp-command-auth pro pnm))) |
| 101 |
|
| 102 |
(defun mew-smtp-command-auth (pro pnm) |
| 103 |
(let* ((case (mew-smtp-get-case pnm)) |
| 104 |
(use-smtp-auth (mew-use-smtp-auth case)) |
| 105 |
(capa (mew-smtp-get-capa pnm)) |
| 106 |
(auths (assoc "AUTH" capa)) |
| 107 |
(auth-list (mew-smtp-get-auth-list pnm)) |
| 108 |
auth func) |
| 109 |
(cond |
| 110 |
((and use-smtp-auth auths) |
| 111 |
(if (and (setq auth (mew-auth-select2 auths auth-list)) |
| 112 |
(setq func (mew-smtp-auth-get-func auth)) |
| 113 |
(fboundp func)) |
| 114 |
(progn |
| 115 |
(mew-smtp-set-auth-selected pnm auth) |
| 116 |
(funcall func pro pnm)) |
| 117 |
(mew-smtp-debug "<AUTH>" "No preferred SMTP AUTH.\n") |
| 118 |
(mew-smtp-command-wpwd pro pnm))) |
| 119 |
(t |
| 120 |
(mew-smtp-set-status pnm "next") |
| 121 |
(mew-smtp-command-next pro pnm))))) |
| 122 |
|
| 123 |
(defun mew-smtp-command-wpwd (pro pnm) |
| 124 |
(let ((auth (mew-smtp-get-auth-selected pnm))) |
| 125 |
(mew-passwd-set-passwd (mew-smtp-passtag pnm) nil) |
| 126 |
(if auth |
| 127 |
(mew-smtp-set-error pnm (format "SMTP %s password is wrong!" auth)) |
| 128 |
(mew-smtp-set-error pnm "No SMTP AUTH available!")) |
| 129 |
(mew-smtp-command-quit2 pro pnm))) |
| 130 |
|
| 131 |
(defun mew-smtp-command-next (pro pnm) |
| 132 |
(let ((msgs (mew-smtp-get-messages pnm)) |
| 133 |
(qfld (mew-smtp-get-qfld pnm)) |
| 134 |
(case (mew-smtp-get-case pnm)) |
| 135 |
(buf (process-buffer pro)) |
| 136 |
msg) |
| 137 |
(if msgs |
| 138 |
(progn |
| 139 |
(setq msg (car msgs)) |
| 140 |
(setq msgs (cdr msgs)) |
| 141 |
(mew-queue-insert-file pnm mew-smtp-info-list-save-length qfld msg) |
| 142 |
(mew-set-buffer-multibyte nil) |
| 143 |
(mew-info-clean-up pnm mew-smtp-info-list-clean-length) |
| 144 |
(mew-smtp-set-case pnm case) ;; override |
| 145 |
(mew-smtp-set-messages pnm msgs) |
| 146 |
(set-process-buffer pro (current-buffer)) |
| 147 |
(mew-remove-buffer buf) |
| 148 |
(mew-smtp-set-status pnm "mail-from") |
| 149 |
(mew-smtp-command-mail-from pro pnm)) |
| 150 |
(mew-smtp-set-status pnm "quit") |
| 151 |
(mew-smtp-command-quit pro pnm)))) |
| 152 |
|
| 153 |
(defun mew-smtp-command-mail-from (pro pnm) |
| 154 |
(widen) |
| 155 |
(clear-visited-file-modtime) |
| 156 |
;; |
| 157 |
(let* ((case (mew-smtp-get-case pnm)) |
| 158 |
(resentp (mew-header-existp mew-resent-from:)) |
| 159 |
(sender: (if resentp mew-resent-sender: mew-sender:)) |
| 160 |
(from: (if resentp mew-resent-from: mew-from:)) |
| 161 |
(from (mew-header-get-value from:)) |
| 162 |
(froms (mew-addrstr-parse-address-list from)) |
| 163 |
(nfrom (length froms)) |
| 164 |
(mail-from (mew-smtp-mail-from case))) |
| 165 |
(mew-smtp-set-from pnm from) ;; for Bcc: |
| 166 |
(unless mail-from |
| 167 |
;; Which address is suitable for MAIL FROM if multiple? |
| 168 |
(setq mail-from (car froms))) |
| 169 |
(unless (mew-header-existp sender:) |
| 170 |
(if (= nfrom 1) |
| 171 |
(if (and mew-use-sender (not (string= mail-from (car froms)))) |
| 172 |
(mew-smtp-set-sender pnm (cons sender: mail-from))) |
| 173 |
(mew-smtp-set-sender pnm (cons sender: mail-from)))) |
| 174 |
;; |
| 175 |
(mew-smtp-process-send-string pro "MAIL FROM:<%s>" mail-from))) |
| 176 |
|
| 177 |
(defun mew-smtp-command-rcpt-to (pro pnm) |
| 178 |
(let* ((recipients (mew-smtp-get-recipients pnm)) |
| 179 |
(recipient (car recipients))) |
| 180 |
(setq recipients (cdr recipients)) |
| 181 |
(mew-smtp-set-recipients pnm recipients) |
| 182 |
(if recipients (mew-smtp-set-status pnm "mail-from")) |
| 183 |
(mew-smtp-process-send-string pro "RCPT TO:<%s>" recipient))) |
| 184 |
|
| 185 |
(defun mew-smtp-command-data (pro pnm) |
| 186 |
(goto-char (point-max)) |
| 187 |
(unless (bolp) (insert "\n")) |
| 188 |
(mew-dot-insert) |
| 189 |
(mew-eol-fix-for-write) |
| 190 |
(set-buffer-modified-p nil) |
| 191 |
(mew-smtp-set-cont pnm (point-min)) |
| 192 |
(mew-smtp-set-timer pnm nil) |
| 193 |
(mew-smtp-process-send-string pro "DATA")) |
| 194 |
|
| 195 |
(defun mew-smtp-command-content (pro pnm) |
| 196 |
(save-excursion |
| 197 |
(let ((cont (mew-smtp-get-cont pnm)) |
| 198 |
(sender (mew-smtp-get-sender pnm)) |
| 199 |
(inc 1000) (i 0) (N 10)) |
| 200 |
(set-buffer (process-buffer pro)) |
| 201 |
;; Sender: |
| 202 |
(when sender |
| 203 |
(mew-smtp-process-send-string pro "%s %s" (car sender) (cdr sender)) |
| 204 |
(mew-smtp-set-sender pnm nil)) |
| 205 |
;; |
| 206 |
(while (and (< cont (point-max)) (not (input-pending-p)) (< i N)) |
| 207 |
(let ((next (min (point-max) (+ cont inc)))) |
| 208 |
(if (and (processp pro) (eq (process-status pro) 'open)) |
| 209 |
(process-send-region pro cont next)) |
| 210 |
(setq cont next) |
| 211 |
(setq i (1+ i)))) |
| 212 |
(mew-smtp-set-cont pnm cont) |
| 213 |
(if (< cont (point-max)) |
| 214 |
(let ((timer |
| 215 |
(if (input-pending-p) |
| 216 |
(run-with-idle-timer |
| 217 |
0.01 nil 'mew-smtp-command-content pro pnm) |
| 218 |
(run-at-time 0.05 nil 'mew-smtp-command-content pro pnm)))) |
| 219 |
(mew-smtp-set-timer pnm timer)) |
| 220 |
(mew-smtp-set-cont pnm nil) |
| 221 |
(mew-smtp-set-timer pnm nil) |
| 222 |
(mew-smtp-process-send-string pro "."))))) |
| 223 |
|
| 224 |
(defun mew-smtp-command-done (pro pnm) |
| 225 |
(let ((fcc (mew-smtp-get-fcc pnm)) |
| 226 |
(case (mew-smtp-get-case pnm)) |
| 227 |
(back (mew-queue-backup (buffer-file-name) mew-queue-info-suffix)) |
| 228 |
imapp) |
| 229 |
;; mew-folder-new-message may be slow if the folder contains |
| 230 |
;; a lot of messages. So, let's Fcc in background. |
| 231 |
(setq imapp (mew-net-fcc-message case fcc back)) |
| 232 |
(mew-smtp-set-imapp pnm imapp) |
| 233 |
(mew-smtp-log pnm) |
| 234 |
(if (mew-smtp-get-bcc pnm) |
| 235 |
(mew-smtp-bcc pro pnm back) |
| 236 |
(mew-smtp-set-status pro "next") |
| 237 |
(mew-smtp-command-next pro pnm)))) |
| 238 |
|
| 239 |
(defun mew-smtp-command-quit (pro pnm) |
| 240 |
(mew-smtp-set-done pnm t) |
| 241 |
(mew-smtp-process-send-string pro "QUIT")) |
| 242 |
|
| 243 |
(defun mew-smtp-command-quit2 (pro pnm) |
| 244 |
;; error is set |
| 245 |
(mew-smtp-set-done pnm t) |
| 246 |
(when (and (processp pro) (eq (process-status pro) 'open)) |
| 247 |
(mew-smtp-set-status pnm "quit") |
| 248 |
(mew-smtp-process-send-string pro "QUIT"))) |
| 249 |
|
| 250 |
(defun mew-smtp-command-noop (_pro _pnm) |
| 251 |
()) |
| 252 |
|
| 253 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 254 |
;;; |
| 255 |
;;; SMTP AUTH (RFC 2554) |
| •256 |
;;; |
| 257 |
|
| 258 |
(defvar mew-smtp-auth-alist |
| 259 |
'(("CRAM-MD5" mew-smtp-command-auth-cram-md5) ;; RFC 2195 |
| 260 |
("PLAIN" mew-smtp-command-auth-plain) ;; RFC 2595 |
| 261 |
("LOGIN" mew-smtp-command-auth-login) ;; No spec |
| 262 |
("XOAUTH2" mew-smtp-command-auth-xoauth2))) |
| 263 |
|
| 264 |
(defun mew-smtp-auth-get-func (auth) |
| 265 |
(nth 1 (mew-assoc-case-equal auth mew-smtp-auth-alist 0))) |
| 266 |
|
| 267 |
;; CRAM MD5 |
| 268 |
|
| 269 |
(defun mew-smtp-command-auth-cram-md5 (pro pnm) |
| 270 |
(mew-smtp-process-send-string pro "AUTH CRAM-MD5") |
| 271 |
(mew-smtp-set-status pnm "auth-cram-md5")) |
| 272 |
|
| 273 |
(defun mew-smtp-command-pwd-cram-md5 (pro pnm) |
| 274 |
(let* ((str (mew-smtp-get-string pnm)) |
| 275 |
(user (mew-smtp-get-auth-user pnm)) |
| 276 |
(prompt (format "SMTP CRAM-MD5 password (%s): " user)) |
| 277 |
challenge passwd cram-md5) |
| 278 |
(if (string-match " \\([a-zA-Z0-9+/]+=*\\)" str) ;; xxx |
| 279 |
(setq challenge (match-string 1 str))) |
| 280 |
(setq passwd (mew-smtp-input-passwd prompt pnm)) |
| 281 |
(setq cram-md5 (mew-cram-md5 user passwd challenge)) |
| 282 |
(mew-smtp-process-send-string pro cram-md5))) |
| 283 |
|
| 284 |
;; LOGIN |
| 285 |
|
| 286 |
(defun mew-smtp-command-auth-login (pro pnm) |
| 287 |
(mew-smtp-process-send-string pro "AUTH LOGIN") |
| 288 |
(mew-smtp-set-status pnm "auth-login")) |
| 289 |
|
| 290 |
(defun mew-smtp-command-user-login (pro pnm) |
| 291 |
(let* ((user (mew-smtp-get-auth-user pnm)) |
| 292 |
(euser (mew-base64-encode-string user))) |
| 293 |
(mew-smtp-process-send-string pro "%s" euser))) |
| 294 |
|
| 295 |
(defun mew-smtp-command-pwd-login (pro pnm) |
| 296 |
(let* ((user (mew-smtp-get-auth-user pnm)) |
| 297 |
(prompt (format "SMTP LOGIN password (%s): " user)) |
| 298 |
(passwd (mew-smtp-input-passwd prompt pnm)) |
| 299 |
(epasswd (mew-base64-encode-string passwd))) |
| 300 |
(mew-smtp-process-send-string pro epasswd))) |
| 301 |
|
| 302 |
;; There are two variations for SMTP AUTH PLAIN. |
| 303 |
;; 1) AUTH PLAIN then id+password |
| 304 |
;; 2) AUTH PLAIN id+password |
| 305 |
;; |
| 306 |
;; 1) is popular however a server of an ISP supports 2) only, sigh. |
| 307 |
|
| 308 |
(defun mew-smtp-command-auth-plain (pro pnm) |
| 309 |
(let* ((case (mew-smtp-get-case pnm)) |
| 310 |
(user (mew-smtp-get-auth-user pnm)) |
| 311 |
(authorize-id (mew-smtp-auth-plain-authorize-id case)) |
| 312 |
(prompt (format "SMTP PLAIN password (%s): " user)) |
| 313 |
(passwd (mew-smtp-input-passwd prompt pnm)) |
| 314 |
(plain (mew-base64-encode-string |
| 315 |
(if authorize-id |
| •316 |
(format "%s\0%s\0%s" user user passwd) |
| 317 |
(format "\0%s\0%s" user passwd))))) |
| 318 |
(mew-smtp-process-send-string pro "AUTH PLAIN %s" plain) |
| 319 |
(mew-smtp-set-status pnm "auth-plain"))) |
| 320 |
|
| 321 |
(defun mew-smtp-command-auth-xoauth2 (pro pnm) |
| 322 |
(let* ((user (mew-smtp-get-auth-user pnm)) |
| 323 |
(token (mew-auth-oauth2-token-access-token)) |
| 324 |
(auth-string (mew-auth-xoauth2-auth-string user token))) |
| 325 |
(mew-smtp-process-send-string pro "AUTH XOAUTH2 %s" auth-string) |
| 326 |
(mew-smtp-set-status pnm "auth-xoauth2"))) |
| 327 |
|
| 328 |
;; (defun mew-smtp-command-auth-plain (pro pnm) |
| 329 |
;; (mew-smtp-process-send-string pro "AUTH PLAIN") |
| 330 |
;; (mew-smtp-set-status pnm "auth-plain")) |
| 331 |
|
| 332 |
;; (defun mew-smtp-command-pwd-plain (pro pnm) |
| 333 |
;; (let* ((case (mew-smtp-get-case pnm)) |
| 334 |
;; (user (mew-smtp-get-auth-user pnm)) |
| 335 |
;; (authorize-id (mew-smtp-auth-plain-authorize-id case)) |
| 336 |
;; (prompt (format "SMTP PLAIN password (%s): " user)) |
| 337 |
;; (passwd (mew-smtp-input-passwd prompt pnm)) |
| 338 |
;; (plain (mew-base64-encode-string |
| 339 |
;; (if authorize-id |
| 340 |
;; (format "%s\0%s\0%s" user user passwd) |
| 341 |
;; (format "\0%s\0%s" user passwd))))) |
| 342 |
;; (mew-smtp-process-send-string pro "%s" plain))) |
| 343 |
|
| 344 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 345 |
;;; |
| 346 |
;;; Sub functions |
| 347 |
;;; |
| 348 |
|
| 349 |
(defconst mew-smtp-info-prefix "mew-smtp-info-") |
| 350 |
|
| 351 |
(defun mew-smtp-info-name (case &optional fallbacked) |
| 352 |
(let ((server (mew-smtp-server case)) |
| 353 |
(port (mew-*-to-string (mew-smtp-port case))) |
| 354 |
(user (mew-smtp-user case)) |
| 355 |
(sshsrv (mew-smtp-ssh-server case)) |
| 356 |
(name mew-smtp-info-prefix)) |
| 357 |
(if user |
| 358 |
(setq name (concat name user "@" server)) |
| 359 |
(setq name (concat name server))) |
| 360 |
(when (and (not fallbacked) |
| 361 |
mew-use-submission ;; xxx to be deleted |
| 362 |
(fboundp 'make-network-process) |
| 363 |
(string= port "smtp")) |
| 364 |
(setq port "submission")) |
| 365 |
(unless (mew-port-equal port mew-smtp-port) |
| 366 |
(setq name (concat name ":" port))) |
| 367 |
(if sshsrv |
| 368 |
(concat name "%" sshsrv) |
| 369 |
name))) |
| 370 |
|
| 371 |
(defun mew-smtp-process-send-string (pro &rest args) |
| 372 |
(let ((str (apply 'format args))) |
| 373 |
(mew-smtp-debug "=SEND=" str) |
| 374 |
(if (and (processp pro) (eq (process-status pro) 'open)) |
| 375 |
(process-send-string pro (concat str mew-cs-eol)) |
| 376 |
(message "SMTP time out")))) |
| 377 |
|
| 378 |
(defun mew-smtp-passtag (pnm) |
| 379 |
(concat (mew-smtp-get-auth-user pnm) |
| 380 |
"@" (mew-smtp-get-server pnm) |
| 381 |
":" (mew-smtp-get-port pnm))) |
| 382 |
|
| 383 |
(defun mew-smtp-input-passwd (prompt pnm) |
| 384 |
(let ((tag (mew-smtp-passtag pnm)) |
| 385 |
(pro (mew-smtp-get-process pnm)) |
| 386 |
pass) |
| 387 |
(setq pass (mew-input-passwd prompt tag)) |
| 388 |
(unless (and (processp pro) (eq (process-status pro) 'open)) |
| 389 |
(mew-passwd-set-passwd tag nil)) |
| 390 |
pass)) |
| 391 |
|
| 392 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 393 |
;;; |
| 394 |
;;; Opening SMTP |
| 395 |
;;; |
| 396 |
|
| 397 |
(defvar mew-use-submission nil) |
| 398 |
|
| 399 |
(defvar mew-smtp-submission-timeout 10) |
| 400 |
(defun mew-smtp-submission-timeout (pro) |
| 401 |
(when (and (processp pro) (eq (process-status pro) 'connect)) |
| 402 |
(mew-smtp-sentinel pro "time out - failed\n"))) |
| 403 |
|
| 404 |
(defvar mew-smtp-submission-family 'ipv4) |
| 405 |
|
| •406 |
;;; XXX: (mew-open-network-stream) always returns a list |
| 407 |
;; and is also used for non-SMTP protocols. |
| 408 |
;;; XXX: port must be resolved by using mew-serv-to-port |
| 409 |
;; because some service names are not in /etc/services. |
| 410 |
;; mew-serv-to-port uses mew-port-db. |
| 411 |
(setq mew--advice-tls-parameters-plist nil) |
| 412 |
(defun mew--advice-filter-args-gnutls-negotiate (&rest args) |
| 413 |
(nconc (car args) mew--advice-tls-parameters-plist)) |
| 414 |
;;; |
| 415 |
;;; Functions to handle tunneling by an external program. The |
| 416 |
;;; external program must be capable of bidirectional communications |
| 417 |
;;; via stdin and stdout and forward the data stream from/to the final |
| 418 |
;;; destination. TLS and STARTTLS will be handled by GnuTLS. |
| 419 |
;;; |
| 420 |
(defun mew--tun-type (tun-plist) |
| 421 |
(plist-get tun-plist :type)) |
| 422 |
(setq mew--advice-tun-command nil) |
| 423 |
(defun mew--advice-tun-command (case host port tun-plist) |
| 424 |
(cond |
| 425 |
((eq (mew--tun-type tun-plist) 'ssh) |
| 426 |
(format "%s -W %s:%s -oPort=%s %s" |
| 427 |
(mew-ssh-prog case) |
| 428 |
(plist-get tun-plist :host) |
| 429 |
(plist-get tun-plist :port) |
| 430 |
port host)) |
| 431 |
(t nil))) |
| 432 |
;; Override function for TLS connection over tunnel. |
| 433 |
(defun mew--advice-override-open-gnutls-stream |
| 434 |
(name buffer host service &optional nowait) |
| 435 |
;; Must be equivalent to |
| 436 |
;; (open-gnutls-stream name buffer host service &optional nowait) |
| 437 |
(make-process args |
| 438 |
:command mew--advice-tun-command |
| 439 |
:coding 'utf-8-unix |
| 440 |
:connection-type 'pipe |
| 441 |
:noquery t |
| 442 |
:stderr nil) |
| 443 |
;; Do gnutls-negotiate here |
| 444 |
) |
| 445 |
;; Override function for PLAIN or STARTTLS connection over tunnel. |
| 446 |
(defun mew--advice-override-make-network-process (&rest args) |
| 447 |
;; PLAIN: make-network-process |
| 448 |
;; STARTTLS: make-network-process -> gnutls-negotiate |
| 449 |
(make-process args |
| 450 |
:command mew--advice-tun-command |
| 451 |
:coding 'utf-8-unix |
| 452 |
:connection-type 'pipe |
| 453 |
:noquery t |
| 454 |
:stderr nil)) |
| 455 |
;;; |
| 456 |
;;; XXX: This conditional can be removed safely. |
| 457 |
(if (fboundp 'make-network-process) |
| •458 |
(defun mew-open-network-stream (name buf server port proto sslnp |
| 459 |
starttlsp case &optional tun-plist) |
| 460 |
(let* ((tun-type (mew--tun-type tun-plist)) |
| 461 |
(mew--advice-tun-command (mew--advice-tun-command |
| 462 |
case server port tun-plist)) |
| 463 |
(status-msg (format "Opening a %s connection %s%s%s..." |
| 464 |
(if sslnp "TLS" "TCP") |
| 465 |
(if sslnp "(GnuTLS" "") |
| 466 |
(if sslnp |
| 467 |
(if starttlsp ", STARTTLS)" ")") |
| 468 |
"") |
| 469 |
(if (eq tun-type 'ssh) " over SSH" |
| 470 |
""))) |
| 471 |
family nowait pro tlsparams) |
| 472 |
;; SMTP-specific |
| 473 |
(when (and (eq proto 'smtp) mew-inherit-submission) |
| 474 |
(setq family mew-smtp-submission-family) |
| 475 |
(setq nowait t)) |
| •476 |
;; TLS does not work for Unix-domain socket for now. |
| 477 |
(when (and (not sslnp) |
| 478 |
(stringp port) (string-match "^/" port)) |
| 479 |
(setq family 'local) |
| 480 |
(setq server 'local)) |
| •481 |
(cond |
| 482 |
;; Both GnuTLS and NSM are mandatory for 'native. |
| 483 |
((and sslnp (or (not (fboundp 'gnutls-available-p)) |
| 484 |
(not (gnutls-available-p)) |
| 485 |
(not (fboundp 'gnutls-boot-parameters)) |
| 486 |
(not (fboundp 'nsm-level)))) |
| 487 |
(setq pro |
| 488 |
(list nil |
| 489 |
:error t |
| 490 |
:status-msg |
| 491 |
(concat status-msg |
| 492 |
"FAILED (GnuTLS or NSM not available)")))) |
| 493 |
((and sslnp) |
| 494 |
(let ((hostname (puny-encode-domain server)) |
| 495 |
;; Note: on Emacs 26.3 and prior GnuTLS always uses |
| 496 |
;; the system-wide default path first even if |
| 497 |
;; trustfiles is specified. |
| 498 |
(trustfiles (mew-ssl-trustfiles case)) |
| 499 |
(nsm-noninteractive nil) |
| 500 |
(network-security-level network-security-level)) |
| 501 |
(when (eq (mew-ssl-verify-level case) 0) |
| 502 |
;; Forcibly disable verification. |
| 503 |
(setq network-security-level 'low)) |
| 504 |
(setq tlsparams |
| 505 |
(cons 'gnutls-x509pki |
| 506 |
;; XXX: (gnutls-boot-parameters) returns |
| 507 |
;; :priority key instead of :priority-string |
| 508 |
;; while (gnutls-negotiate) accepts |
| 509 |
;; :priority-string. To handle this odd |
| 510 |
;; mismatch, create :priority-string in the |
| 511 |
;; result of (gnutls-boot-parameters) here. |
| 512 |
(let ((boot-params |
| 513 |
(gnutls-boot-parameters |
| 514 |
:type 'gnutls-x509pki |
| 515 |
:keylist (mew-ssl-client-keycert-list case) |
| 516 |
:trustfiles (mew-ssl-trustfiles case) |
| 517 |
:priority-string (mew-ssl-algorithm-priority case) |
| 518 |
:min-prime-bits mew-ssl-min-prime-bits |
| 519 |
;; |
| 520 |
;; mew-ssl-verify-error should be nil |
| 521 |
;; to defer verification to NSM. Note |
| 522 |
;; that gnutls-verify-error overrides |
| 523 |
;; verify-error when it is nil. |
| 524 |
;; Setting gnutls-verify-error to t is |
| 525 |
;; also discouraged. |
| 526 |
;; |
| 527 |
:verify-error mew-ssl-verify-error |
| 528 |
:hostname hostname))) |
| 529 |
(plist-put boot-params |
| 530 |
:priority-string |
| 531 |
(plist-get boot-params :priority))))) |
| 532 |
;; debug output: TLS params |
| 533 |
(funcall (intern (concat "mew-" (symbol-name proto) "-debug")) |
| 534 |
(format "TLS proto=%s, server=%s:%s, starttlsp=%s" |
| 535 |
proto hostname port starttlsp) |
| 536 |
(format "verify-level=%s, network-security-level=%s, nowait=%s, tlsparams=%s" |
| 537 |
(mew-ssl-verify-level case) network-security-level |
| 538 |
nowait |
| 539 |
(apply #'concat (mapcar (lambda (a) (format "%s " a)) tlsparams)))) |
| 540 |
(let ((type (if starttlsp 'starttls 'tls))) |
| 541 |
(with-temp-message status-msg |
| 542 |
;; XXX: (open-network-stream) does not pass tlsparams |
| 543 |
;; to (gnutls-negotiate) to start STARTTLS. As a |
| 544 |
;; workaround, add an advice to forcibly append the |
| 545 |
;; parameters. This should be fixed in |
| 546 |
;; (open-network-stream). |
| 547 |
(setq mew--advice-tls-parameters-plist (cdr tlsparams)) |
| 548 |
(advice-add 'gnutls-negotiate |
| 549 |
:filter-args #'mew--advice-filter-args-gnutls-negotiate) |
| 550 |
;; |
| 551 |
;; Override key functions when using a tunnel. |
| 552 |
(cond |
| 553 |
((and mew--advice-tun-command (eq type 'tls)) |
| 554 |
(advice-add 'open-gnutls-stream |
| 555 |
:override #'mew--advice-override-open-gnutls-stream)) |
| 556 |
(mew--advice-tun-command |
| 557 |
(advice-add 'make-network-process |
| 558 |
:override #'mew--advice-override-make-network-process))) |
| 559 |
(setq pro (open-network-stream |
| 560 |
name buf server port |
| 561 |
:type type |
| 562 |
:return-list t |
| 563 |
:nowait nowait |
| 564 |
:always-query-capabilities |
| 565 |
(mew-starttls-get-param proto :always-query-capabilities nil) |
| 566 |
:capability-command |
| 567 |
(mew-starttls-get-param proto :capability-command t) |
| 568 |
:end-of-capability |
| 569 |
(mew-starttls-get-param proto :end-of-capability t) |
| 570 |
:end-of-command |
| 571 |
(mew-starttls-get-param proto :end-of-command t) |
| 572 |
:success |
| 573 |
(mew-starttls-get-param proto :success t) |
| 574 |
:starttls-function |
| 575 |
(mew-starttls-get-param proto :starttls-function nil))) |
| 576 |
(cond |
| 577 |
((and mew--advice-tun-command (eq type 'tls)) |
| 578 |
(advice-remove 'open-gnutls-stream |
| 579 |
#'mew--advice-override-open-gnutls-stream)) |
| 580 |
(mew--advice-tun-command |
| 581 |
(advice-remove 'make-network-process |
| 582 |
#'mew--advice-override-make-network-process))) |
| 583 |
(advice-remove 'gnutls-negotiate |
| 584 |
#'mew--advice-filter-args-gnutls-negotiate) |
| 585 |
;; |
| 586 |
;; When a validation error occurs, (car pro) will be nil. |
| 587 |
;; |
| 588 |
(let ((plainp (eq 'plain (plist-get (cdr pro) :type))) |
| 589 |
(openp (and (car pro) |
| 590 |
(eq 'open (process-status (car pro))))) |
| 591 |
;; Falling back to a plain connection is allowed |
| 592 |
;; only when verify-level < 2. |
| 593 |
(needtlsp (and starttlsp |
| 594 |
(> (mew-ssl-verify-level case) 1)))) |
| 595 |
(cond |
| 596 |
((not openp) |
| 597 |
(let ((msg (plist-get (cdr pro) :error))) |
| 598 |
(setq pro (list nil |
| 599 |
:error t |
| 600 |
:status-msg |
| 601 |
(concat status-msg "FAILED: " msg))))) |
| 602 |
((and plainp needtlsp) |
| 603 |
(delete-process (car pro)) |
| 604 |
(setq pro (list nil |
| 605 |
:error t |
| 606 |
:status-msg |
| 607 |
(concat status-msg "FAILED")))) |
| 608 |
(t |
| 609 |
(setq pro (list |
| 610 |
(car pro) |
| 611 |
:error nil))))))))) |
| 612 |
(t |
| 613 |
(with-temp-message status-msg |
| 614 |
(let ((params (list :name name :buffer buf |
| 615 |
:service port :family family |
| 616 |
:nowait nowait)) |
| 617 |
;; :host will be ignored when family is 'local. |
| 618 |
(host (if (not (eq family 'local)) |
| 619 |
(list :host server)))) |
| 620 |
(setq pro (list |
| 621 |
(apply #'make-network-process (nconc params host)) |
| 622 |
:greeting nil |
| 623 |
:capabilities nil |
| 624 |
:type 'plain |
| 625 |
:error nil)))))) |
| 626 |
(if (and (eq proto 'smtp) nowait) |
| 627 |
(run-at-time mew-smtp-submission-timeout nil 'mew-smtp-submission-timeout pro)) |
| •628 |
(when (plist-get (cdr pro) :error) |
| 629 |
(message (plist-get (cdr pro) :status-msg))) |
| 630 |
pro)) |
| •631 |
(defun mew-open-network-stream (name buf server port proto sslnp starttlsp case) |
| 632 |
(open-network-stream name buf server port :return-list t))) |
| 633 |
|
| •634 |
(defun mew-smtp-open (pnm case server port starttlsp) |
| 635 |
(let ((sprt (mew-*-to-port port)) |
| •636 |
(sslnp (mew-ssl-native-p (mew-smtp-ssl case))) |
| 637 |
pro tm) |
| 638 |
;; xxx some OSes do not define "submission", sigh. |
| 639 |
(when (and (stringp sprt) (string= sprt "submission")) |
| 640 |
(setq sprt (mew-serv-to-port sprt))) |
| 641 |
(condition-case emsg |
| 642 |
(progn |
| 643 |
(setq tm (run-at-time mew-smtp-timeout-time nil 'mew-smtp-timeout)) |
| 644 |
(message "Connecting to the SMTP server...") |
| •645 |
(setq pro (mew-open-network-stream pnm nil server sprt |
| 646 |
'smtp sslnp starttlsp case)) |
| 647 |
(when (and sslnp starttlsp) |
| 648 |
(mew-smtp-debug "*GREETING*" |
| 649 |
(plist-get (cdr pro) :greeting)) |
| 650 |
(mew-smtp-debug "*CAPABILITIES*" |
| 651 |
(plist-get (cdr pro) :capabilities))) |
| 652 |
(setq pro (car pro)) |
| 653 |
(when (not (processp pro)) (signal 'quit nil)) |
| 654 |
(mew-process-silent-exit pro) |
| 655 |
(mew-set-process-cs pro mew-cs-text-for-net mew-cs-text-for-net) |
| 656 |
(message "Connecting to the SMTP server...done")) |
| 657 |
(quit |
| 658 |
(setq pro nil) |
| 659 |
(message "Cannot connect to the SMTP server")) |
| 660 |
(error |
| 661 |
(setq pro nil) |
| 662 |
(message "%s, %s" (nth 1 emsg) (nth 2 emsg)))) |
| 663 |
(if tm (cancel-timer tm)) |
| 664 |
pro)) |
| 665 |
|
| 666 |
(defun mew-smtp-timeout () |
| •667 |
;; Do not timeout if the NSM query pane is active. |
| 668 |
(unless (get-buffer "*Network Security Manager*") |
| 669 |
(message "SMTP connection timed out (%d seconds)" |
| 670 |
mew-smtp-timeout-time) |
| 671 |
(signal 'quit nil))) |
| 672 |
|
| 673 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 674 |
;;; |
| 675 |
;;; Launcher |
| 676 |
;;; |
| 677 |
|
| 678 |
(defun mew-smtp-send-message (case qfld msgs &optional fallbacked) |
| 679 |
(let ((server (mew-smtp-server case)) |
| 680 |
(user (mew-smtp-user-only case)) |
| 681 |
(port (mew-*-to-string (mew-smtp-port case))) |
| 682 |
(pnm (mew-smtp-info-name case fallbacked)) |
| 683 |
(sshsrv (mew-smtp-ssh-server case)) |
| 684 |
(sslp (mew-smtp-ssl case)) |
| 685 |
(sslport (mew-*-to-string (mew-smtp-ssl-port case))) |
| •686 |
(sslnp (mew-ssl-native-p (mew-smtp-ssl case))) |
| 687 |
(starttlsp |
| 688 |
(mew-ssl-starttls-p (mew-smtp-ssl case) |
| 689 |
(mew-*-to-string (mew-smtp-port case)) |
| 690 |
(mew-smtp-ssl-port case))) |
| 691 |
mew-inherit-submission |
| 692 |
process sshname sshpro sslname sslpro lport tlsp tls fallback) |
| •693 |
(cond |
| 694 |
((and (not sslnp) sslp starttlsp) |
| 695 |
(setq tlsp t) |
| 696 |
;; let stunnel know that a wrapper protocol is SMTP |
| •697 |
(setq tls mew-tls-smtp))) |
| 698 |
;; a fallback: "submission" -> "smtp" |
| 699 |
;; mew-smtp-port is "smtp" and mew-use-submission is t on Emacs 22 |
| •700 |
(when (and (or (not sslp) starttlsp tlsp) |
| 701 |
(not fallbacked) |
| 702 |
mew-use-submission |
| 703 |
(fboundp 'make-network-process) ;; Emacs 22 or later |
| 704 |
(mew-port-equal port "smtp")) |
| 705 |
(setq port "submission") |
| 706 |
(setq fallback t) |
| •707 |
(when (and sslp (not sslnp) (not tlsp)) |
| 708 |
;; TLS uses stunnel. So, we should not use non-blocking connect. |
| 709 |
;; Timeout should be carried out by stunnel. |
| 710 |
(setq mew-inherit-submission t)) |
| •711 |
(if (mew-port-equal sslport "smtp") |
| 712 |
(setq sslport "submission"))) |
| 713 |
(cond |
| •714 |
(sslnp |
| 715 |
(let ((serv (if starttlsp port sslport))) |
| 716 |
(setq process (mew-smtp-open pnm case server serv starttlsp)))) |
| 717 |
(sshsrv |
| 718 |
(setq sshpro (mew-open-ssh-stream case server port sshsrv)) |
| 719 |
(when sshpro |
| 720 |
(setq sshname (process-name sshpro)) |
| 721 |
(setq lport (mew-ssh-pnm-to-lport sshname)) |
| 722 |
(when lport |
| •723 |
(setq process (mew-smtp-open pnm case "localhost" lport nil))))) |
| 724 |
(sslp |
| 725 |
(setq sslpro (mew-open-ssl-stream case server sslport tls)) |
| 726 |
(when sslpro |
| 727 |
(setq sslname (process-name sslpro)) |
| 728 |
(setq lport (mew-ssl-pnm-to-lport sslname)) |
| 729 |
(when lport |
| •730 |
(setq process (mew-smtp-open pnm case mew-ssl-localhost lport nil))))) |
| 731 |
(t |
| •732 |
(setq process (mew-smtp-open pnm case server port nil)))) |
| 733 |
(if (null process) |
| 734 |
(cond |
| 735 |
((and sshsrv (null sshpro)) |
| 736 |
(message "Cannot create to the SSH connection")) |
| •737 |
(sslnp |
| 738 |
(message "Cannot open an SSL/TLS (GnuTLS) connection")) |
| 739 |
((and sslp (null sslpro)) |
| 740 |
(message "Cannot create to the SSL/TLS connection")) |
| 741 |
(t |
| •742 |
(if (and (not (eq server 'local)) |
| 743 |
(or (not sslp) tlsp) |
| 744 |
(not fallbacked) |
| 745 |
mew-use-submission |
| 746 |
(fboundp 'make-network-process) |
| 747 |
(mew-port-equal port "submission")) |
| 748 |
(progn |
| 749 |
;; make-network-process with :nowait t sometime |
| 750 |
;; returns nil, why? |
| 751 |
(mew-smtp-send-message case qfld msgs t) |
| 752 |
(mew-info-clean-up pnm)) |
| 753 |
(message "Cannot connect to the SMTP server")))) |
| 754 |
(mew-info-clean-up pnm mew-smtp-info-list-clean-length) |
| 755 |
(mew-smtp-set-case pnm case) |
| 756 |
(mew-smtp-set-qfld pnm qfld) |
| 757 |
(mew-smtp-set-messages pnm msgs) |
| 758 |
(mew-smtp-set-server pnm server) |
| 759 |
(if sslp |
| 760 |
(mew-smtp-set-port pnm sslport) |
| 761 |
(mew-smtp-set-port pnm port)) |
| 762 |
(mew-smtp-set-process pnm process) |
| 763 |
(mew-smtp-set-ssh-server pnm sshsrv) |
| 764 |
(mew-smtp-set-ssh-process pnm sshpro) |
| 765 |
(mew-smtp-set-ssl-process pnm sslpro) |
| 766 |
(mew-smtp-set-ssl-p pnm sslp) |
| 767 |
(mew-smtp-set-helo-domain pnm (mew-smtp-helo-domain case)) |
| 768 |
(mew-smtp-set-user pnm user) |
| 769 |
(mew-smtp-set-auth-user pnm (mew-smtp-user case)) |
| 770 |
(mew-smtp-set-auth-list pnm (mew-smtp-auth-list case)) |
| 771 |
(mew-smtp-set-status pnm "greeting") |
| 772 |
(mew-smtp-set-fallback pnm fallback) |
| 773 |
;; |
| 774 |
(set-process-buffer process nil) |
| 775 |
(set-process-sentinel process 'mew-smtp-sentinel) |
| 776 |
(set-process-filter process 'mew-smtp-filter) |
| •777 |
(message "Sending in background...") |
| 778 |
;; |
| 779 |
(when sslnp |
| 780 |
;; GnuTLS requires a client-initiated command after the |
| 781 |
;; session is established or upgraded to use TLS because |
| 782 |
;; no additional greeting from the server. |
| 783 |
(mew-smtp-set-status pnm "ehlo") |
| 784 |
(mew-smtp-command-ehlo process pnm)) |
| 785 |
))) |
| 786 |
|
| 787 |
(defun mew-smtp-flush-queue (case &optional qfld) |
| 788 |
(let (msgs) |
| 789 |
(unless qfld (setq qfld (mew-queue-folder case))) |
| 790 |
(if (mew-smtp-get-server (mew-smtp-info-name case)) ;; lock |
| 791 |
(message "%s is locked" qfld) |
| 792 |
(setq msgs (mew-folder-messages qfld)) |
| 793 |
(when msgs |
| 794 |
(mew-summary-folder-cache-clean qfld) |
| 795 |
(run-hooks 'mew-smtp-flush-hook) |
| 796 |
(message "Flushing %s..." qfld) |
| 797 |
(mew-smtp-send-message case qfld msgs))))) |
| 798 |
|
| 799 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 800 |
;;; |
| 801 |
;;; Filter and sentinel |
| 802 |
;;; |
| 803 |
|
| 804 |
(defun mew-smtp-debug (label string) |
| 805 |
(when (mew-debug 'net) |
| 806 |
(with-current-buffer (get-buffer-create mew-buffer-debug) |
| 807 |
(goto-char (point-max)) |
| 808 |
(insert (format "\n<%s>\n%s\n" label string))))) |
| 809 |
|
| 810 |
(defun mew-smtp-filter (process string) |
| 811 |
(let* ((pnm (process-name process)) |
| 812 |
(status (mew-smtp-get-status pnm)) |
| 813 |
(str (concat (mew-smtp-get-string pnm) string)) |
| 814 |
(buf (process-buffer process)) |
| 815 |
next func code) |
| 816 |
(save-excursion |
| 817 |
(mew-smtp-debug (upcase status) string) |
| 818 |
(if (and buf (get-buffer buf)) (set-buffer buf)) |
| 819 |
;; SMTP server's strings should be short enough. |
| 820 |
(mew-smtp-set-string pnm str) |
| 821 |
(cond |
| 822 |
((and (string-match "\n$" str) |
| 823 |
(string-match "^\\([1-5][0-7][0-9]\\) " str)) |
| 824 |
(setq code (match-string 1 str)) |
| 825 |
(setq next (mew-smtp-fsm-next status code)) |
| 826 |
(cond |
| 827 |
(next |
| 828 |
(mew-smtp-set-status pnm next) |
| 829 |
(setq func (intern-soft (concat "mew-smtp-command-" next))) |
| 830 |
(and func (funcall func process pnm)) |
| 831 |
(mew-smtp-set-string pnm nil)) |
| 832 |
(t |
| 833 |
(if (string-match "^pwd-" status) |
| 834 |
(mew-smtp-set-error pnm "SMTP password is wrong!") |
| 835 |
(if (string-match "\n$" str) |
| 836 |
(setq str (substring str 0 -1))) |
| 837 |
(unless (string-match "[.!]$" str) |
| 838 |
(setq str (concat str ".")))) |
| 839 |
(mew-smtp-set-error pnm str) |
| 840 |
(mew-smtp-command-quit2 process pnm)))) |
| 841 |
(t ()))))) ;; stay |
| 842 |
|
| 843 |
(defun mew-smtp-sentinel (process event) |
| 844 |
(let* ((pnm (process-name process)) |
| 845 |
(pro (mew-smtp-get-process pnm)) |
| 846 |
(case (mew-smtp-get-case pnm)) |
| 847 |
(qfld (mew-smtp-get-qfld pnm)) |
| 848 |
(msgs (mew-smtp-get-messages pnm)) |
| 849 |
(fallback (mew-smtp-get-fallback pnm)) |
| 850 |
(status (mew-smtp-get-status pnm))) |
| 851 |
(save-excursion |
| 852 |
(mew-smtp-debug "SMTP SENTINEL" event) |
| 853 |
(cond |
| 854 |
((or (string-match "failed" event) |
| 855 |
(and (string= status "greeting") |
| 856 |
(string-match "connection broken by remote peer" event))) |
| 857 |
(if fallback |
| 858 |
(progn |
| 859 |
(mew-smtp-send-message case qfld msgs fallback) |
| 860 |
;; A failed process stays |
| 861 |
(when (memq (process-status pro) '(failed connect)) |
| 862 |
(delete-process pro) |
| 863 |
(mew-info-clean-up pnm))) |
| 864 |
(mew-smtp-set-error pnm (substring event 0 -1)) |
| 865 |
(mew-smtp-sentinel2 process event))) |
| 866 |
((string-match "open" event) |
| 867 |
;; OK connected |
| 868 |
) |
| 869 |
(t |
| 870 |
(mew-smtp-sentinel2 process event)))))) |
| 871 |
|
| 872 |
(defun mew-smtp-sentinel2 (process _event) |
| 873 |
(let* ((pnm (process-name process)) |
| 874 |
(buf (process-buffer process)) |
| 875 |
(qfld (mew-smtp-get-qfld pnm)) |
| 876 |
(case (mew-smtp-get-case pnm)) |
| 877 |
(done (mew-smtp-get-done pnm)) |
| 878 |
(imapp (mew-smtp-get-imapp pnm)) |
| 879 |
(error (mew-smtp-get-error pnm)) |
| 880 |
(sshpro (mew-smtp-get-ssh-process pnm)) |
| 881 |
(sslpro (mew-smtp-get-ssl-process pnm))) |
| 882 |
(save-excursion |
| 883 |
(cond |
| 884 |
(error |
| 885 |
(when buf |
| 886 |
;; A message file is not inserted at the beginning of the SMTP |
| 887 |
;; session. |
| 888 |
(set-buffer buf) |
| 889 |
(mew-smtp-queue case error pnm)) |
| 890 |
(mew-smtp-log pnm error) |
| 891 |
|
| 892 |
(if (memq system-type '(windows-nt ms-dos cygwin)) |
| 893 |
(message (format "%s This mail has been queued to %s" error qfld)) |
| 894 |
(message-box (format "%s This mail has been queued to %s" error qfld)))) |
| 895 |
(done |
| 896 |
(message "Sending in background...done")) |
| 897 |
(t |
| 898 |
(if (null buf) |
| 899 |
(message "SMTP connection is lost") |
| 900 |
(set-buffer buf) |
| 901 |
(mew-smtp-queue case "SMTP connection is lost" pnm)))) |
| 902 |
(mew-info-clean-up pnm) |
| 903 |
(if (and (processp sshpro) (not mew-ssh-keep-connection)) |
| 904 |
(process-send-string sshpro "exit\n")) |
| 905 |
(if (and (processp sslpro) (not mew-ssl-keep-connection)) |
| 906 |
(delete-process sslpro)) |
| 907 |
(run-hooks 'mew-smtp-sentinel-hook) |
| 908 |
(if (and done imapp) (mew-imap2-fcc case)) |
| 909 |
(when buf (mew-remove-buffer buf))))) |
| 910 |
|
| 911 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 912 |
;;; |
| 913 |
;;; Queuing |
| 914 |
;;; |
| 915 |
|
| 916 |
(defun mew-smtp-queue (case err &optional apnm) |
| 917 |
;; Must be in a buffer where a message is contained. |
| 918 |
(let* ((pnm (or apnm (mew-smtp-info-name case))) |
| 919 |
(qfld (mew-queue-folder case)) |
| 920 |
(oname (buffer-name)) |
| 921 |
(work (buffer-file-name)) |
| 922 |
file-info file info nname) |
| 923 |
(mew-local-folder-check qfld) |
| 924 |
(setq file-info (mew-queue-enqueue work qfld)) |
| 925 |
(mew-set '(file info) file-info) |
| 926 |
(setq file (file-name-nondirectory file)) |
| 927 |
(setq nname (mew-concat-folder qfld file)) |
| 928 |
(if (mew-draft-p) |
| 929 |
(mew-smtp-set-case pnm (mew-tinfo-get-case))) |
| 930 |
;; |
| 931 |
(mew-smtp-set-recipients pnm (mew-smtp-get-orig-recipients pnm)) |
| 932 |
(let* ((n mew-smtp-info-list-save-length) |
| 933 |
(data (make-vector n nil))) |
| 934 |
(dotimes (i n) |
| 935 |
(aset data i (aref (mew-info pnm) i))) |
| 936 |
(mew-lisp-save info data)) |
| 937 |
;; |
| 938 |
(message "%s has been queued to %s (%s)" oname nname err) |
| 939 |
(mew-touch-folder qfld) |
| 940 |
(file-name-sans-extension file))) |
| 941 |
|
| 942 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 943 |
;;; |
| 944 |
;;; Bcc: |
| 945 |
;;; |
| 946 |
|
| 947 |
(defun mew-smtp-bcc (pro pnm back) |
| 948 |
(let* ((dir (file-name-directory back)) |
| 949 |
(msg (file-name-nondirectory back)) |
| 950 |
(case (mew-smtp-get-case pnm)) |
| 951 |
msgid logtime) |
| 952 |
(mew-elet |
| 953 |
(mew-erase-buffer) |
| 954 |
(mew-set-buffer-multibyte t) |
| 955 |
(mew-smtp-set-recipients pnm (mew-smtp-get-bcc pnm)) |
| 956 |
(mew-smtp-set-orig-recipients pnm (mew-smtp-get-bcc pnm)) |
| 957 |
(mew-smtp-set-bcc pnm nil) |
| 958 |
;; |
| 959 |
(mew-set '(msgid logtime) (mew-encode-id-date pnm (mew-smtp-message-id case))) ;; save-excursion |
| 960 |
(mew-smtp-set-msgid pnm msgid) |
| 961 |
(mew-smtp-set-logtime pnm logtime) |
| 962 |
(goto-char (point-max)) |
| 963 |
(mew-draft-header-insert mew-to: "Bcc-Receiver:;") |
| 964 |
(mew-draft-header-insert mew-subj: mew-bcc-subject) |
| 965 |
(mew-draft-header-insert mew-from: (mew-smtp-get-from pnm)) |
| 966 |
(mew-draft-header-insert mew-organization: (mew-organization case)) |
| 967 |
(mew-draft-header-insert-alist (mew-header-alist case)) |
| 968 |
;; X-Mailer: must be the last |
| 969 |
(if (mew-use-x-mailer case) |
| 970 |
(mew-draft-header-insert mew-x-mailer: mew-x-mailer)) |
| 971 |
(mew-header-set "\n") |
| 972 |
(insert mew-bcc-body) |
| 973 |
(goto-char (mew-header-end)) |
| 974 |
(forward-line) |
| 975 |
(setq mew-encode-syntax (mew-encode-syntax-initial dir)) |
| 976 |
(setq mew-encode-syntax |
| 977 |
(mew-syntax-insert-entry |
| 978 |
mew-encode-syntax |
| 979 |
'(2) |
| 980 |
(mew-encode-syntax-single msg mew-type-msg nil nil nil))) |
| 981 |
(mew-encode-multipart mew-encode-syntax dir 0 'buffered) |
| 982 |
(mew-encode-make-header) |
| 983 |
(mew-encode-save-draft) |
| 984 |
(mew-overlay-delete-buffer) |
| 985 |
(mew-set-buffer-multibyte nil) |
| 986 |
(mew-info-clean-up pnm mew-smtp-info-list-clean-length) |
| 987 |
(set-process-buffer pro (current-buffer)) |
| 988 |
(mew-smtp-set-status pnm "mail-from") |
| 989 |
(mew-smtp-command-mail-from pro pnm)))) |
| 990 |
|
| 991 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 992 |
;;; |
| 993 |
;;; Logging |
| 994 |
;;; |
| 995 |
|
| 996 |
(defun mew-smtp-log (pnm &optional err) |
| 997 |
(let ((logtime (mew-smtp-get-logtime pnm)) |
| 998 |
(msgid (mew-smtp-get-msgid pnm)) |
| 999 |
(recipients (mew-smtp-get-orig-recipients pnm)) |
| 1000 |
(server (mew-smtp-get-server pnm)) |
| 1001 |
(port (mew-smtp-get-port pnm)) |
| 1002 |
(sshsrv (mew-smtp-get-ssh-server pnm)) |
| 1003 |
(sslp (mew-smtp-get-ssl-p pnm))) |
| 1004 |
(with-temp-buffer |
| 1005 |
(and logtime (insert logtime)) |
| 1006 |
(and msgid (insert " id=" msgid)) |
| 1007 |
(and server (insert " server=" server ":" port)) |
| 1008 |
(and sshsrv (insert " sshsrv=" sshsrv)) |
| 1009 |
(and sslp (insert " SSL/TLS")) |
| 1010 |
(and recipients |
| 1011 |
(setq recipients (mapconcat 'identity recipients ","))) |
| 1012 |
(and recipients (insert " recipients=" recipients)) |
| 1013 |
(if err |
| 1014 |
(insert " status=" "(" |
| 1015 |
(substring err 0 (string-match "\n+$" err)) |
| 1016 |
")") |
| 1017 |
(insert " status=sent")) |
| 1018 |
(insert "\n") |
| 1019 |
(write-region (point-min) (point-max) |
| 1020 |
(expand-file-name mew-smtp-log-file mew-conf-path) |
| 1021 |
'append 'no-msg)))) |
| 1022 |
|
| 1023 |
(provide 'mew-smtp) |
| 1024 |
|
| 1025 |
;;; Copyright Notice: |
| 1026 |
|
| •1027 |
;; Copyright (C) 1999-2020 Mew developing team. |
| 1028 |
;; All rights reserved. |
| 1029 |
|
| 1030 |
;; Redistribution and use in source and binary forms, with or without |
| 1031 |
;; modification, are permitted provided that the following conditions |
| 1032 |
;; are met: |
| 1033 |
;; |
| 1034 |
;; 1. Redistributions of source code must retain the above copyright |
| 1035 |
;; notice, this list of conditions and the following disclaimer. |
| 1036 |
;; 2. Redistributions in binary form must reproduce the above copyright |
| 1037 |
;; notice, this list of conditions and the following disclaimer in the |
| 1038 |
;; documentation and/or other materials provided with the distribution. |
| 1039 |
;; 3. Neither the name of the team nor the names of its contributors |
| 1040 |
;; may be used to endorse or promote products derived from this software |
| 1041 |
;; without specific prior written permission. |
| 1042 |
;; |
| 1043 |
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND |
| 1044 |
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 1045 |
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 1046 |
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE |
| 1047 |
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 1048 |
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 1049 |
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 1050 |
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 1051 |
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 1052 |
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 1053 |
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 1054 |
|
| 1055 |
;;; mew-smtp.el ends here |