| 1 |
;;; mew-imap2.el for appending |
| 2 |
|
| 3 |
;; Author: Kazu Yamamoto <[email protected]> |
| 4 |
;; Created: May 22, 2003 |
| 5 |
|
| 6 |
;;; Code: |
| 7 |
|
| 8 |
(require 'mew) |
| 9 |
|
| 10 |
(defvar mew-imap2-literal+ t) |
| 11 |
|
| 12 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 13 |
;;; |
| 14 |
;;; IMAP2 info |
| 15 |
;;; |
| 16 |
|
| 17 |
(defvar mew-imap2-info-list |
| 18 |
'("case" "directive" "mailbox" |
| 19 |
;; |
| 20 |
"src" "messages" "src-list" "src-list-orig" |
| 21 |
"server" "port" "ssh-server" |
| 22 |
"user" "auth" "auth-list" "account" |
| 23 |
"status" "process" "ssh-process" "ssl-process" |
| 24 |
"cnt" "ttl" "literal+" |
| 25 |
;; parameters used internally and should be initialized |
| 26 |
"tag" "string" "error" "auth-selected" "authl" "aux" "done")) |
| 27 |
|
| 28 |
(defvar mew-imap2-info-list-clean-length 20) |
| 29 |
|
| 30 |
(mew-info-defun "mew-imap2-" mew-imap2-info-list) |
| 31 |
|
| 32 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 33 |
;;; |
| 34 |
;;; FSM |
| 35 |
;;; |
| 36 |
|
| 37 |
(defvar mew-imap2-fsm |
| 38 |
'(("greeting" ("OK" . "capability")) |
| 39 |
("capability" ("OK" . "post-capability")) |
| •40 |
("auth-xoauth2" ("OK" . "next") ("NO" . "xoauth2-wpwd")) |
| 41 |
("auth-cram-md5" ("OK" . "pwd-cram-md5") ("NO" . "wpwd")) |
| 42 |
("pwd-cram-md5" ("OK" . "next") ("NO" . "wpwd")) |
| 43 |
("auth-login" ("OK" . "user-login") ("NO" . "wpwd")) |
| 44 |
("user-login" ("OK" . "pwd-login") ("NO" . "wpwd")) |
| 45 |
("pwd-login" ("OK" . "next") ("NO" . "wpwd")) |
| 46 |
("login" ("OK" . "next") ("NO" . "wpwd")) |
| 47 |
("append" ("OK" . "post-append") ("\\[TRYCREATE\\]" . "create")) |
| 48 |
("create" ("OK" . "append") ("NO" . "wmbx")) |
| 49 |
("post-append" ("OK" . "done") ("\\[TRYCREATE\\]" . "create")) |
| 50 |
("logout" ("OK" . "noop")))) |
| 51 |
|
| 52 |
(defun mew-imap2-fsm-by-status (status) |
| 53 |
(assoc status mew-imap2-fsm)) |
| 54 |
|
| 55 |
(defun mew-imap2-fsm-next (status code) |
| 56 |
(cdr (mew-assoc-match2 code (nthcdr 1 (mew-imap2-fsm-by-status status)) 0))) |
| 57 |
|
| 58 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 59 |
;;; |
| 60 |
;;; Filters 2 |
| 61 |
;;; |
| 62 |
|
| 63 |
(defun mew-imap2-command-capability (pro pnm) |
| 64 |
(mew-imap2-process-send-string pro pnm "CAPABILITY")) |
| 65 |
|
| 66 |
(defun mew-imap2-command-post-capability (pro pnm) |
| 67 |
(let ((case-fold-search t) |
| 68 |
(aux (mew-imap2-get-aux pnm)) |
| 69 |
(start 0) |
| 70 |
authl literal+) |
| 71 |
(while (string-match "AUTH=\\([^ \r\n]+\\)" aux start) |
| 72 |
(setq authl (cons (mew-match-string 1 aux) authl)) |
| 73 |
(setq start (match-end 0))) |
| 74 |
(if (and mew-imap2-literal+ |
| 75 |
(string-match "\\bliteral\\+\\b" aux)) |
| 76 |
(setq literal+ t)) |
| 77 |
(mew-imap2-set-authl pnm authl) |
| 78 |
(mew-imap2-set-literal+ pnm literal+) |
| 79 |
(mew-imap2-set-status pnm "authentication") |
| 80 |
(mew-imap2-command-authentication pro pnm))) |
| 81 |
|
| 82 |
(defun mew-imap2-command-authentication (pro pnm) |
| 83 |
(cond |
| 84 |
((eq (mew-imap2-get-auth pnm) t) ;; t means SASL |
| 85 |
(let ((auth-list (mew-imap2-get-auth-list pnm)) |
| 86 |
(authl (mew-imap2-get-authl pnm)) |
| 87 |
auth func) |
| 88 |
(if (and authl |
| 89 |
(setq auth (mew-auth-select2 authl auth-list)) |
| 90 |
(setq func (mew-imap2-auth-get-func auth)) |
| 91 |
(fboundp func)) |
| 92 |
(progn |
| 93 |
(mew-imap2-set-auth-selected pnm auth) |
| 94 |
(funcall func pro pnm)) |
| 95 |
(mew-imap2-debug "<AUTH>" "No preferred IMAP AUTH.\n") |
| 96 |
(mew-imap2-set-status pnm "login") |
| 97 |
(mew-imap2-command-login pro pnm)))) |
| 98 |
(t |
| 99 |
(mew-imap2-set-status pnm "login") |
| 100 |
(mew-imap2-command-login pro pnm)))) |
| 101 |
|
| 102 |
(defun mew-imap2-command-login (pro pnm) |
| 103 |
(let* ((user (mew-imap2-get-user pnm)) |
| 104 |
(prompt (format "IMAP password (%s): " |
| 105 |
(mew-imap2-get-account pnm))) |
| 106 |
(pass (mew-input-passwd prompt (mew-imap2-passtag pnm)))) |
| 107 |
(setq user (mew-quote-string user ?\\ '(?\\ ?\"))) |
| 108 |
(setq pass (mew-quote-string pass ?\\ '(?\\ ?\"))) |
| 109 |
(mew-imap2-process-send-string pro pnm "LOGIN \"%s\" \"%s\"" user pass))) |
| 110 |
|
| 111 |
(defun mew-imap2-command-wpwd (pro pnm) |
| 112 |
(let ((auth (mew-imap2-get-auth-selected pnm))) |
| 113 |
(mew-passwd-set-passwd (mew-imap2-passtag pnm) nil) |
| 114 |
(mew-imap2-set-error pnm (format "IMAP %s password is wrong!" auth)) |
| 115 |
(mew-imap2-command-logout2 pro pnm))) |
| 116 |
|
| 117 |
(defun mew-imap2-command-next (pro pnm) |
| 118 |
(let ((directive (mew-imap2-get-directive pnm)) |
| 119 |
(case (mew-imap2-get-case pnm)) |
| 120 |
(src-list (mew-imap2-get-src-list pnm)) |
| 121 |
(msgs (mew-imap2-get-messages pnm)) |
| 122 |
src dst mailbox msg src-file) |
| 123 |
(if msgs |
| 124 |
(setq src (mew-imap2-get-src pnm)) |
| 125 |
(if (or (eq directive 'copy) (eq directive 'move)) |
| 126 |
(progn |
| 127 |
(setq msgs src-list) |
| 128 |
(mew-imap2-set-src-list pnm nil) |
| 129 |
(mew-imap2-set-ttl pnm (length msgs)) |
| 130 |
(mew-imap2-set-cnt pnm 0) |
| 131 |
(setq dst (mew-imap2-get-mailbox pnm)) |
| 132 |
(setq mailbox (mew-imap-expand-mailbox case dst)) |
| 133 |
(setq mailbox (mew-imap-utf-7-encode-string mailbox)) |
| 134 |
(mew-imap2-set-mailbox pnm mailbox)) |
| 135 |
(catch 'break |
| 136 |
(while src-list ;; cannot use dolist |
| 137 |
(setq src (car src-list)) |
| 138 |
(setq src-list (cdr src-list)) |
| 139 |
(mew-imap2-set-src pnm src) |
| 140 |
(mew-imap2-set-src-list pnm src-list) |
| 141 |
(setq msgs (mew-folder-messages (mew-case-folder case src))) |
| 142 |
(when msgs |
| 143 |
(mew-imap2-set-ttl pnm (length msgs)) |
| 144 |
(mew-imap2-set-cnt pnm 0) |
| 145 |
(cond |
| 146 |
((eq directive 'cp-r) |
| 147 |
(setq dst (mew-imap-local-to-imap case src)) |
| 148 |
(unless (assoc dst (mew-imap-folder-alist case)) |
| 149 |
(mew-imap-folder-insert case dst)) |
| 150 |
(setq mailbox (mew-imap-expand-mailbox case dst))) |
| 151 |
((eq directive 'fcc) |
| 152 |
(setq mailbox (mew-imap-fcc-to-mailbox case src)))) |
| 153 |
(setq mailbox (mew-imap-utf-7-encode-string mailbox)) |
| 154 |
(mew-imap2-set-mailbox pnm mailbox) |
| 155 |
(throw 'break nil)))))) |
| 156 |
(if msgs |
| 157 |
(progn |
| 158 |
(setq msg (car msgs)) |
| 159 |
(setq msgs (cdr msgs)) |
| 160 |
(cond |
| 161 |
((or (eq directive 'copy) (eq directive 'move)) |
| 162 |
(setq src-file msg)) |
| 163 |
(t |
| 164 |
(setq src-file (mew-expand-msg (mew-case-folder case src) msg)))) |
| 165 |
(mew-frwlet mew-cs-text-for-read mew-cs-dummy |
| 166 |
(set-buffer (mew-find-file-noselect src-file))) |
| 167 |
(mew-set-buffer-multibyte nil) |
| 168 |
(mew-info-clean-up pnm mew-imap2-info-list-clean-length) |
| 169 |
(mew-imap2-set-messages pnm msgs) |
| 170 |
(set-process-buffer pro (current-buffer)) |
| 171 |
(mew-imap2-set-status pnm "append") |
| 172 |
(mew-imap2-command-append pro pnm)) |
| 173 |
(mew-imap2-set-status pnm "logout") |
| 174 |
(mew-imap2-command-logout pro pnm)))) |
| 175 |
|
| 176 |
(defun mew-imap2-command-append (pro pnm) |
| 177 |
(let ((mailbox (mew-imap2-get-mailbox pnm)) |
| 178 |
(cnt (mew-imap2-get-cnt pnm)) |
| 179 |
(ttl (mew-imap2-get-ttl pnm)) |
| 180 |
(mbx (mew-imap2-get-mailbox pnm)) |
| 181 |
(literal+ (mew-imap2-get-literal+ pnm)) |
| 182 |
bytes) |
| 183 |
(setq cnt (1+ cnt)) |
| 184 |
(mew-imap2-set-cnt pnm cnt) |
| 185 |
(message "Copying to %s %d/%d (%02d%%)" mbx cnt ttl (/ (* cnt 100) ttl)) |
| 186 |
(goto-char (point-max)) |
| 187 |
(unless (bolp) (insert "\n")) |
| 188 |
(if (mew-eol-fix-for-write) |
| 189 |
(setq bytes (- (point-max) (point-min))) |
| 190 |
(setq bytes (- (point-max) (point-min))) |
| 191 |
(setq bytes (+ bytes (count-lines (point-min) (point-max))))) |
| 192 |
(set-buffer-modified-p nil) |
| 193 |
(if (not literal+) |
| 194 |
(mew-imap2-process-send-string |
| 195 |
pro pnm "APPEND \"%s\" {%d}" mailbox bytes) |
| 196 |
(mew-imap2-process-send-string |
| 197 |
pro pnm "APPEND \"%s\" {%d+}" mailbox bytes) |
| 198 |
(mew-imap2-set-status pnm "post-append") |
| 199 |
(mew-imap2-command-post-append pro pnm)))) |
| 200 |
|
| 201 |
(defun mew-imap2-command-post-append (pro _pnm) |
| 202 |
(process-send-region pro (point-min) (point-max)) |
| 203 |
(process-send-string pro mew-cs-eol)) ;; APPEND command ends |
| 204 |
|
| 205 |
(defun mew-imap2-command-create (pro pnm) |
| 206 |
(let ((mailbox (mew-imap2-get-mailbox pnm)) |
| 207 |
(cnt (mew-imap2-get-cnt pnm))) |
| 208 |
(setq cnt (1- cnt)) |
| 209 |
(mew-imap2-set-cnt pnm cnt) |
| 210 |
(mew-imap2-process-send-string pro pnm "CREATE \"%s\"" mailbox))) |
| 211 |
|
| 212 |
(defun mew-imap2-command-wmbx (pro pnm) |
| 213 |
(mew-imap2-set-error pnm "mailbox is wrong!") |
| 214 |
(mew-imap2-set-status pnm "logout") |
| 215 |
(mew-imap2-command-logout pro pnm)) |
| 216 |
|
| 217 |
(defun mew-imap2-command-done (pro pnm) |
| 218 |
(let ((buf (process-buffer pro))) |
| 219 |
(set-process-buffer pro nil) |
| 220 |
(mew-remove-buffer buf) |
| 221 |
(mew-imap2-set-status pro "next") |
| 222 |
(mew-imap2-command-next pro pnm))) |
| 223 |
|
| 224 |
(defun mew-imap2-command-logout (pro pnm) |
| 225 |
(mew-imap2-set-done pnm t) |
| 226 |
(mew-imap2-process-send-string pro pnm "LOGOUT")) |
| 227 |
|
| 228 |
(defun mew-imap2-command-logout2 (pro pnm) |
| 229 |
;; error is set |
| 230 |
(mew-imap2-set-done pnm t) |
| 231 |
(when (and (processp pro) (eq (process-status pro) 'open)) |
| 232 |
(mew-imap2-set-status pnm "logout") |
| 233 |
(mew-imap2-process-send-string pro pnm "LOGOUT"))) |
| 234 |
|
| 235 |
(defun mew-imap2-command-noop (_pro _pnm) |
| 236 |
()) |
| 237 |
|
| 238 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 239 |
;;; |
| 240 |
;;; AUTH |
| 241 |
;;; |
| 242 |
|
| 243 |
(defvar mew-imap2-auth-alist |
| 244 |
'(("CRAM-MD5" mew-imap2-command-auth-cram-md5) |
| •245 |
("LOGIN" mew-imap2-command-auth-login) |
| 246 |
("XOAUTH2" mew-imap2-command-auth-xoauth2))) |
| 247 |
|
| 248 |
(defun mew-imap2-auth-get-func (auth) |
| 249 |
(nth 1 (mew-assoc-case-equal auth mew-imap2-auth-alist 0))) |
| 250 |
|
| 251 |
(defun mew-imap2-command-auth-cram-md5 (pro pnm) |
| 252 |
(mew-imap2-process-send-string pro pnm "AUTHENTICATE CRAM-MD5") |
| 253 |
(mew-imap2-set-status pnm "auth-cram-md5")) |
| 254 |
|
| 255 |
(defun mew-imap2-command-pwd-cram-md5 (pro pnm) |
| 256 |
(let ((user (mew-imap2-get-user pnm)) |
| 257 |
(prompt (format "IMAP CRAM-MD5 password (%s): " |
| 258 |
(mew-imap2-get-account pnm))) |
| 259 |
(aux (mew-imap2-get-aux pnm)) |
| 260 |
challenge passwd cram-md5) |
| 261 |
(if (string-match " \\([a-zA-Z0-9+/]+=*\\)" aux) |
| 262 |
(setq challenge (mew-match-string 1 aux))) |
| 263 |
(setq passwd (mew-imap2-input-passwd prompt pnm)) |
| 264 |
(setq cram-md5 (mew-cram-md5 user passwd challenge)) |
| 265 |
(mew-imap2-process-send-string2 pro cram-md5))) |
| 266 |
|
| 267 |
(defun mew-imap2-command-auth-login (pro pnm) |
| 268 |
(mew-imap2-process-send-string pro pnm "AUTHENTICATE LOGIN") |
| 269 |
(mew-imap2-set-status pnm "auth-login")) |
| 270 |
|
| 271 |
(defun mew-imap2-command-user-login (pro pnm) |
| 272 |
(let* ((user (mew-imap2-get-user pnm)) |
| 273 |
(euser (mew-base64-encode-string user))) |
| 274 |
(mew-imap2-process-send-string2 pro euser))) |
| 275 |
|
| 276 |
(defun mew-imap2-command-pwd-login (pro pnm) |
| 277 |
(let* ((prompt (format "IMAP LOGIN password (%s): " |
| 278 |
(mew-imap2-get-account pnm))) |
| 279 |
(passwd (mew-imap2-input-passwd prompt pnm)) |
| 280 |
(epasswd (mew-base64-encode-string passwd))) |
| 281 |
(mew-imap2-process-send-string2 pro epasswd))) |
| 282 |
|
| 283 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 284 |
;;; |
| 285 |
;;; Sub functions |
| 286 |
;;; |
| 287 |
|
| 288 |
(defconst mew-imap2-info-prefix "mew-imap2-info-") |
| 289 |
|
| 290 |
(defun mew-imap2-info-name (case) |
| 291 |
(let ((server (mew-imap-server case)) |
| 292 |
(port (mew-*-to-string (mew-imap-port case))) |
| 293 |
(sshsrv (mew-imap-ssh-server case)) |
| 294 |
(name mew-imap2-info-prefix)) |
| 295 |
(setq name (concat name server)) |
| 296 |
(unless (mew-port-equal port mew-imap-port) |
| 297 |
(setq name (concat name ":" port))) |
| 298 |
(if sshsrv |
| 299 |
(concat name "%" sshsrv) |
| 300 |
name))) |
| 301 |
|
| 302 |
(defun mew-imap2-buffer-name (pnm) |
| 303 |
(concat mew-buffer-prefix pnm)) |
| 304 |
|
| 305 |
(defun mew-imap2-process-send-string (pro pnm &rest args) |
| 306 |
(let ((str (apply 'format args)) |
| 307 |
(tag (mew-imap2-tag))) |
| 308 |
(mew-imap2-debug "=SEND=" (concat tag " " str)) |
| 309 |
(mew-imap2-set-tag pnm tag) |
| 310 |
(if (and (processp pro) (eq (process-status pro) 'open)) |
| 311 |
(process-send-string pro (concat tag " " str mew-cs-eol)) |
| 312 |
(message "IMAP time out")))) |
| 313 |
|
| 314 |
(defun mew-imap2-process-send-string2 (pro &rest args) |
| 315 |
(let ((str (apply 'format args))) |
| 316 |
(if (and (processp pro) (eq (process-status pro) 'open)) |
| 317 |
(process-send-string pro (concat str mew-cs-eol)) |
| 318 |
(message "IMAP time out")))) |
| 319 |
|
| 320 |
(defun mew-imap2-tag () |
| 321 |
(format "%s%04d" (mew-random-string 4 nil) (% (mew-random) 10000))) |
| 322 |
|
| 323 |
(defun mew-imap2-passtag (pnm) |
| 324 |
(let ((server (mew-imap2-get-server pnm)) |
| 325 |
(port (mew-imap2-get-port pnm)) |
| 326 |
(user (mew-imap2-get-user pnm))) |
| 327 |
(concat user "@" server ":" port))) |
| 328 |
|
| 329 |
(defun mew-imap2-passtag2 (case) |
| 330 |
(let ((server (mew-imap-server case)) |
| 331 |
(port (mew-imap-port case)) |
| 332 |
(user (mew-imap-user case))) |
| 333 |
(concat user "@" server ":" port))) |
| 334 |
|
| 335 |
(defun mew-imap2-input-passwd (prompt pnm) |
| 336 |
(let* ((tag (mew-imap2-passtag pnm)) |
| 337 |
(pro (mew-imap2-get-process pnm)) |
| 338 |
(pass (mew-input-passwd prompt tag))) |
| 339 |
(unless (and (processp pro) (eq (process-status pro) 'open)) |
| 340 |
(mew-passwd-set-passwd tag nil)) |
| 341 |
pass)) |
| 342 |
|
| 343 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 344 |
;;; |
| 345 |
;;; Namespace |
| 346 |
;;; |
| 347 |
|
| 348 |
|
| 349 |
(defvar mew-folder-imap-fcc-prefix "%.fcc") |
| 350 |
|
| 351 |
(defun mew-folder-imap-to-fcc (case folder) |
| 352 |
(concat |
| 353 |
(file-name-as-directory |
| 354 |
(mew-case-folder case mew-folder-imap-fcc-prefix)) |
| 355 |
(mew-folder-string folder))) |
| 356 |
|
| 357 |
(defun mew-folder-imap-fcc-dir (case) |
| 358 |
(mew-expand-folder (mew-case-folder case mew-folder-imap-fcc-prefix))) |
| 359 |
|
| 360 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 361 |
;;; |
| 362 |
;;; Opening IMAP |
| 363 |
;;; |
| 364 |
|
| •365 |
(defun mew-imap2-open (pnm case server port starttlsp) |
| 366 |
(let ((sprt (mew-*-to-port port)) |
| •367 |
(sslnp (mew-ssl-native-p (mew-imap-ssl case))) |
| 368 |
pro tm) |
| 369 |
(condition-case emsg |
| 370 |
(progn |
| 371 |
(setq tm (run-at-time mew-imap-timeout-time nil 'mew-imap2-timeout)) |
| 372 |
(message "Connecting to the IMAP server...") |
| •373 |
(setq pro (mew-open-network-stream pnm nil server sprt |
| 374 |
'imap sslnp starttlsp case)) |
| 375 |
(setq pro (car pro)) |
| 376 |
(when (not (processp pro)) (signal 'quit nil)) |
| 377 |
(mew-process-silent-exit pro) |
| 378 |
(mew-set-process-cs pro mew-cs-text-for-net mew-cs-text-for-net) |
| 379 |
(message "Connecting to the IMAP server...done")) |
| 380 |
(quit |
| 381 |
(setq pro nil) |
| 382 |
(message "Cannot connect to the IMAP server")) |
| 383 |
(error |
| 384 |
(setq pro nil) |
| 385 |
(message "%s, %s" (nth 1 emsg) (nth 2 emsg)))) |
| 386 |
(if tm (cancel-timer tm)) |
| 387 |
pro)) |
| 388 |
|
| 389 |
(defun mew-imap2-timeout () |
| •390 |
;; Do not timeout if the NSM query pane is active. |
| 391 |
(unless (get-buffer "*Network Security Manager*") |
| 392 |
(message "IMAP connection timed out (%d seconds)" |
| 393 |
mew-imap-timeout-time) |
| 394 |
(signal 'quit nil))) |
| 395 |
|
| 396 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 397 |
;;; |
| 398 |
;;; Launcher |
| 399 |
;;; |
| 400 |
|
| 401 |
(defun mew-imap2-copy-message (case directive src-list &optional dst) |
| 402 |
(let* ((server (mew-imap-server case)) |
| 403 |
(user (mew-imap-user case)) |
| 404 |
(port (mew-*-to-string (mew-imap-port case))) |
| 405 |
(pnm (mew-imap2-info-name case)) |
| 406 |
(sshsrv (mew-imap-ssh-server case)) |
| 407 |
(sslp (mew-imap-ssl case)) |
| 408 |
(sslport (mew-imap-ssl-port case)) |
| •409 |
(sslnp (mew-ssl-native-p (mew-imap-ssl case))) |
| 410 |
(starttlsp |
| 411 |
(mew-ssl-starttls-p (mew-imap-ssl case) |
| 412 |
(mew-*-to-string (mew-imap-port case)) |
| 413 |
(mew-imap-ssl-port case))) |
| 414 |
(proxysrv (mew-imap-proxy-server case)) |
| 415 |
(proxyport (mew-imap-proxy-port case)) |
| 416 |
process sshname sshpro sslname sslpro lport tls) |
| 417 |
(cond |
| •418 |
(sslnp |
| 419 |
(let ((serv (if starttlsp port sslport))) |
| 420 |
(setq process (mew-imap2-open pnm case server serv starttlsp)))) |
| 421 |
(sshsrv |
| 422 |
(setq sshpro (mew-open-ssh-stream case server port sshsrv)) |
| 423 |
(when sshpro |
| 424 |
(setq sshname (process-name sshpro)) |
| 425 |
(setq lport (mew-ssh-pnm-to-lport sshname)) |
| 426 |
(when lport |
| •427 |
(setq process (mew-imap2-open pnm case "localhost" lport nil))))) |
| 428 |
(sslp |
| •429 |
(when starttlsp (setq tls mew-tls-imap)) |
| 430 |
(setq sslpro (mew-open-ssl-stream case server sslport tls)) |
| 431 |
(when sslpro |
| 432 |
(setq sslname (process-name sslpro)) |
| 433 |
(setq lport (mew-ssl-pnm-to-lport sslname)) |
| 434 |
(when lport |
| •435 |
(setq process (mew-imap2-open pnm case mew-ssl-localhost lport nil))))) |
| 436 |
(proxysrv |
| •437 |
(setq process (mew-imap2-open pnm case proxysrv proxyport nil))) |
| 438 |
(t |
| •439 |
(setq process (mew-imap2-open pnm case server port nil)))) |
| 440 |
(if (null process) |
| 441 |
(cond |
| 442 |
((and sshsrv (null sshpro)) |
| 443 |
(message "Cannot create to the SSH connection")) |
| 444 |
((and sslp (null sslpro)) |
| 445 |
(message "Cannot create to the SSL/TLS connection")) |
| 446 |
(t |
| 447 |
(message "Cannot connect to the IMAP server"))) |
| 448 |
(mew-info-clean-up pnm) |
| 449 |
(mew-imap2-set-case pnm case) |
| 450 |
(mew-imap2-set-directive pnm directive) |
| 451 |
(mew-imap2-set-server pnm server) |
| 452 |
(mew-imap2-set-port pnm port) |
| 453 |
(mew-imap2-set-process pnm process) |
| 454 |
(mew-imap2-set-ssh-server pnm sshsrv) |
| 455 |
(mew-imap2-set-ssh-process pnm sshpro) |
| 456 |
(mew-imap2-set-ssl-process pnm sslpro) |
| 457 |
(mew-imap2-set-user pnm user) |
| 458 |
(mew-imap2-set-account pnm (format "%s@%s" user server)) |
| 459 |
(mew-imap2-set-auth pnm (mew-imap-auth case)) |
| 460 |
(mew-imap2-set-auth-list pnm (mew-imap-auth-list case)) |
| 461 |
(mew-imap2-set-status pnm "greeting") |
| 462 |
(mew-imap2-set-src-list pnm src-list) |
| 463 |
(mew-imap2-set-src-list-orig pnm src-list) |
| 464 |
(mew-imap2-set-mailbox pnm dst) |
| 465 |
;; |
| 466 |
(set-process-buffer process nil) |
| 467 |
(set-process-sentinel process 'mew-imap2-sentinel) |
| 468 |
(set-process-filter process 'mew-imap2-filter) |
| •469 |
(message "Copying in background...") |
| 470 |
(when sslnp |
| 471 |
;; GnuTLS requires a client-initiated command after the |
| 472 |
;; session is established or upgraded to use TLS because |
| 473 |
;; no additional greeting from the server. |
| 474 |
(mew-imap2-set-status pnm "capability") |
| 475 |
(mew-imap2-command-capability process pnm)) |
| 476 |
))) |
| 477 |
|
| 478 |
(defun mew-summary-from-local-to-imap () |
| 479 |
"Copy messages in local folders under specified folder prefix |
| 480 |
to a IMAP server decided by specified case. This may take very long |
| 481 |
time (even several days) if there are many local messages." |
| 482 |
(interactive) |
| 483 |
(let* ((prefix (mew-input-local-folder mew-folder-local)) |
| 484 |
(case (mew-input-case mew-case-default "IMAP")) |
| 485 |
(alist (mew-local-folder-alist)) |
| 486 |
regex src-list fld) |
| 487 |
(unless prefix (setq prefix mew-folder-local)) |
| 488 |
(setq regex (concat "^" (regexp-quote prefix))) |
| 489 |
(dolist (ent alist) |
| 490 |
(setq fld (car ent)) |
| 491 |
(if (string-match regex fld) |
| 492 |
(setq src-list (cons fld src-list)))) |
| 493 |
(setq src-list (nreverse src-list)) |
| 494 |
(mew-imap2-copy-message case 'cp-r src-list))) |
| 495 |
|
| 496 |
(defun mew-imap2-fcc (case) |
| 497 |
(let* ((prefix mew-folder-imap-fcc-prefix) |
| 498 |
(fcc-dir (mew-folder-imap-fcc-dir case)) |
| 499 |
(fcc-dirs (mew-dir-list fcc-dir)) |
| 500 |
(src-list (mew-local-folder-make-alist fcc-dirs prefix 'make-list))) |
| 501 |
;; src-list |
| 502 |
(mew-imap2-copy-message case 'fcc src-list))) |
| 503 |
|
| 504 |
(defun mew-summary-imap-copy () |
| 505 |
"Copy a message to the server side of IMAP." |
| 506 |
(interactive) |
| 507 |
(mew-summary-not-in-draft |
| 508 |
(let* ((fld (mew-summary-folder-name)) |
| 509 |
(msg (mew-summary-message-number)) |
| 510 |
(fld/msg (mew-expand-msg fld msg)) |
| 511 |
(src-list (list fld/msg)) |
| 512 |
(case:dst (mew-input-folder mew-case mew-imap-inbox-folder)) |
| 513 |
(case (mew-case:folder-case case:dst)) |
| 514 |
(dst (mew-case:folder-folder case:dst))) |
| 515 |
(when (mew-folder-imapp dst) |
| 516 |
(mew-imap2-copy-message case 'copy src-list dst))))) |
| 517 |
|
| 518 |
(defun mew-summary-mark-imap-copy (&optional arg) |
| 519 |
"Copy messages marked with '*' to the server side of IMAP. |
| 520 |
If called with '\\[universal-argument]', only messages marked |
| 521 |
with '*' in the region are handled." |
| 522 |
(interactive "P") |
| 523 |
(mew-summary-not-in-draft |
| 524 |
(let* ((fld (mew-summary-folder-name)) |
| 525 |
(case:dst (mew-input-folder mew-case mew-imap-inbox-folder)) |
| 526 |
(case (mew-case:folder-case case:dst)) |
| 527 |
(dst (mew-case:folder-folder case:dst)) |
| 528 |
msgs beg end region src-list) |
| 529 |
(if (mew-mark-active-p) (setq arg t)) |
| 530 |
(cond |
| 531 |
(arg |
| 532 |
(setq region (mew-summary-get-region)) |
| 533 |
(setq beg (car region)) |
| 534 |
(setq end (cdr region))) |
| 535 |
(t |
| 536 |
(setq beg (point-min)) |
| 537 |
(setq end (point-max)))) |
| 538 |
(setq msgs (mew-summary-mark-collect mew-mark-review beg end)) |
| 539 |
(if (null msgs) |
| 540 |
(message "No mark") |
| 541 |
(when (mew-folder-imapp dst) |
| •542 |
(setq src-list (mapcar (lambda (x) (mew-expand-msg fld x)) msgs)) |
| 543 |
(mew-imap2-copy-message case 'copy src-list dst)))))) |
| 544 |
|
| 545 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 546 |
;;; |
| 547 |
;;; Filter and sentinel |
| 548 |
;;; |
| 549 |
|
| 550 |
(defun mew-imap2-debug (label string) |
| 551 |
(when (mew-debug 'net) |
| 552 |
(with-current-buffer (get-buffer-create mew-buffer-debug) |
| 553 |
(goto-char (point-max)) |
| 554 |
(insert (format "\n<%s>\n%s\n" label string))))) |
| 555 |
|
| •556 |
(defun mew-imap2-filter (process string) |
| 557 |
(let* ((pnm (process-name process)) |
| •558 |
(status (mew-imap2-get-status pnm)) |
| 559 |
(tag (mew-imap2-get-tag pnm)) |
| 560 |
(eos (format "^%s \\(OK\\|NO\\|BAD\\)\\(.*\\)" tag)) |
| 561 |
(str (concat (mew-imap2-get-string pnm) string)) |
| 562 |
(buf (process-buffer process)) |
| 563 |
aux stay next func code resp) |
| 564 |
(save-excursion |
| 565 |
(mew-imap2-debug (upcase (format "%s" status)) string) |
| 566 |
(if (and buf (get-buffer buf)) (set-buffer buf)) |
| 567 |
(while (string-match "^\\*[^\n]*\n" str) |
| 568 |
(setq aux (substring str 0 (match-end 0))) |
| 569 |
(setq str (substring str (match-end 0))) |
| 570 |
(cond |
| 571 |
((string= status "greeting") |
| 572 |
(setq next (mew-imap2-fsm-next "greeting" "OK"))) |
| 573 |
((string= status "capability") |
| 574 |
(mew-imap2-set-aux pnm (concat (mew-imap2-get-aux pnm) aux))))) |
| 575 |
(mew-imap2-set-string pnm str) |
| 576 |
(cond |
| 577 |
(next |
| 578 |
nil) |
| 579 |
((string-match "^\\+ *\\(.*\\)" str) |
| 580 |
(mew-imap2-set-aux pnm str) |
| 581 |
(setq next (mew-imap2-fsm-next |
| 582 |
status |
| 583 |
(if (string= status "auth-xoauth2") |
| 584 |
(mew-auth-xoauth2-json-status (mew-match-string 1)) |
| 585 |
"OK")))) |
| 586 |
((string-match eos str) |
| 587 |
(mew-imap2-set-tag pnm nil) |
| 588 |
(setq code (mew-match-string 1 str)) |
| 589 |
(setq resp (mew-match-string 2 str)) |
| 590 |
(setq next (mew-imap2-fsm-next status code)) |
| 591 |
(unless next (setq next (mew-imap2-fsm-next status resp)))) |
| 592 |
(t |
| 593 |
(setq stay t))) |
| 594 |
(unless stay |
| 595 |
(unless next (setq next "logout")) |
| 596 |
(mew-imap2-set-status pnm next) |
| 597 |
(setq func (intern-soft (concat "mew-imap2-command-" next))) |
| 598 |
(if (fboundp func) |
| 599 |
(funcall func process pnm) |
| 600 |
(error "No function called %s" (symbol-name func))) |
| 601 |
(mew-imap2-set-string pnm nil))))) |
| 602 |
|
| 603 |
(defun mew-imap2-sentinel (process event) |
| 604 |
(let* ((pnm (process-name process)) |
| 605 |
(directive (mew-imap2-get-directive pnm)) |
| 606 |
(case (mew-imap2-get-case pnm)) |
| 607 |
(done (mew-imap2-get-done pnm)) |
| 608 |
(error (mew-imap2-get-error pnm)) |
| 609 |
(sshpro (mew-imap2-get-ssh-process pnm)) |
| 610 |
(sslpro (mew-imap2-get-ssl-process pnm)) |
| 611 |
(srcflds (mew-imap2-get-src-list-orig pnm))) |
| 612 |
(save-excursion |
| 613 |
(mew-imap2-debug "IMAP SENTINEL" event) |
| 614 |
(cond |
| 615 |
(error |
| 616 |
(message "%s" error)) |
| 617 |
(done |
| 618 |
(cond |
| 619 |
((eq directive 'fcc) |
| 620 |
(dolist (srcfld srcflds) |
| 621 |
(mew-imap2-copy-fcc-cache case srcfld)) |
| 622 |
(mew-delete-directory-recursively |
| 623 |
(mew-folder-imap-fcc-dir case))) |
| 624 |
((eq directive 'move) |
| 625 |
(let ((file (car (mew-imap2-get-src-list-orig pnm)))) |
| 626 |
(if (file-exists-p file) (delete-file file))))) |
| 627 |
(message "Copying in background...done")) |
| 628 |
(t |
| 629 |
(message "IMAP connection is lost"))) |
| 630 |
(mew-info-clean-up pnm) |
| 631 |
(if (and (processp sshpro) (not mew-ssh-keep-connection)) |
| 632 |
(process-send-string sshpro "exit\n")) |
| 633 |
(if (and (processp sslpro) (not mew-ssl-keep-connection)) |
| 634 |
(delete-process sslpro)) |
| 635 |
(run-hooks 'mew-imap2-sentinel-hook)))) |
| 636 |
|
| 637 |
(defun mew-imap2-copy-fcc-cache (case srcfld) |
| 638 |
(let* ((case:srcfld (mew-case-folder case srcfld)) |
| 639 |
(srcs (mew-folder-messages case:srcfld)) |
| 640 |
(prefix (file-name-as-directory mew-folder-imap-fcc-prefix)) |
| 641 |
(regex (concat "^" (regexp-quote prefix))) |
| 642 |
dst dstfld case:dstfld i) |
| 643 |
(string-match regex srcfld) |
| 644 |
(setq dstfld (concat mew-folder-imap (substring srcfld (match-end 0)))) |
| 645 |
(setq case:dstfld (mew-case-folder case dstfld)) |
| 646 |
(mew-local-folder-check case:dstfld) |
| 647 |
(setq i (string-to-number (mew-folder-new-message case:dstfld 'num 'cache))) |
| 648 |
;; to make code simple, we just copy the file. |
| 649 |
(dolist (src srcs) |
| 650 |
(setq dst (mew-i2s i 'cache)) |
| 651 |
(copy-file (mew-expand-msg case:srcfld src) |
| 652 |
(mew-expand-new-msg case:dstfld dst)) |
| 653 |
(setq i (1+ i))) |
| 654 |
(mew-touch-folder case:dstfld))) |
| 655 |
|
| 656 |
(provide 'mew-imap2) |
| 657 |
|
| 658 |
;;; Copyright Notice: |
| 659 |
|
| •660 |
;; Copyright (C) 2003-2020 Mew developing team. |
| 661 |
;; All rights reserved. |
| 662 |
|
| 663 |
;; Redistribution and use in source and binary forms, with or without |
| 664 |
;; modification, are permitted provided that the following conditions |
| 665 |
;; are met: |
| 666 |
;; |
| 667 |
;; 1. Redistributions of source code must retain the above copyright |
| 668 |
;; notice, this list of conditions and the following disclaimer. |
| 669 |
;; 2. Redistributions in binary form must reproduce the above copyright |
| 670 |
;; notice, this list of conditions and the following disclaimer in the |
| 671 |
;; documentation and/or other materials provided with the distribution. |
| 672 |
;; 3. Neither the name of the team nor the names of its contributors |
| 673 |
;; may be used to endorse or promote products derived from this software |
| 674 |
;; without specific prior written permission. |
| 675 |
;; |
| 676 |
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND |
| 677 |
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 678 |
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 679 |
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE |
| 680 |
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 681 |
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 682 |
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 683 |
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 684 |
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 685 |
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 686 |
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 687 |
|
| 688 |
;;; mew-imap2.el ends here |