* elmo.el (elmo-folder-list-flagged): New generic function.
[elisp/wanderlust.git] / elmo / elmo-net.el
1 ;;; elmo-net.el --- Network module for ELMO.
2
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
7
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14 ;;
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19 ;;
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24 ;;
25
26 ;;; Commentary:
27 ;;
28
29 (eval-when-compile (require 'cl))
30
31 (require 'elmo-util)
32 (require 'elmo-dop)
33 (require 'elmo-vars)
34 (require 'elmo-cache)
35 (require 'elmo)
36
37 ;;; Code:
38 ;;
39
40 ;;; ELMO net folder
41 (eval-and-compile
42   (luna-define-class elmo-net-folder
43                      (elmo-folder)
44                      (user auth server port stream-type))
45   (luna-define-internal-accessors 'elmo-net-folder))
46
47 ;;; Session
48 (eval-and-compile
49   (autoload 'starttls-negotiate "starttls")
50   (autoload 'sasl-find-mechanism "sasl")
51   (autoload 'sasl-make-client "sasl")
52   (autoload 'sasl-mechanism-name "sasl")
53   (autoload 'sasl-next-step "sasl")
54   (autoload 'sasl-step-data "sasl")
55   (autoload 'sasl-step-set-data "sasl"))
56
57 (defvar sasl-mechanisms)
58
59 ;;; Code:
60 ;;
61 (eval-and-compile
62   (luna-define-class elmo-network-session () (name
63                                               server
64                                               port
65                                               user
66                                               auth
67                                               stream-type
68                                               process
69                                               greeting))
70   (luna-define-internal-accessors 'elmo-network-session))
71
72 (luna-define-generic elmo-network-initialize-session (session)
73   "Initialize SESSION (Called before authentication).")
74
75 (luna-define-generic elmo-network-initialize-session-buffer (session buffer)
76   "Initialize SESSION's BUFFER.")
77
78 (luna-define-generic elmo-network-authenticate-session (session)
79   "Authenticate SESSION.")
80
81 (luna-define-generic elmo-network-setup-session (session)
82   "Setup SESSION. (Called after authentication).")
83
84 (luna-define-generic elmo-network-close-session (session)
85   "Close SESSION.")
86
87 (luna-define-method
88   elmo-network-initialize-session-buffer ((session
89                                            elmo-network-session) buffer)
90   (with-current-buffer buffer
91     (elmo-set-buffer-multibyte nil)
92     (buffer-disable-undo (current-buffer))))
93
94 (luna-define-method elmo-network-close-session ((session elmo-network-session))
95   (when (elmo-network-session-process-internal session)
96 ;;; (memq (process-status (elmo-network-session-process-internal session))
97 ;;;       '(open run))
98     (kill-buffer (process-buffer
99                   (elmo-network-session-process-internal session)))
100     (delete-process (elmo-network-session-process-internal session))))
101
102 (defmacro elmo-network-stream-type-spec-string (stream-type)
103   (` (nth 0 (, stream-type))))
104
105 (defmacro elmo-network-stream-type-symbol (stream-type)
106   (` (nth 1 (, stream-type))))
107
108 (defmacro elmo-network-stream-type-feature (stream-type)
109   (` (nth 2 (, stream-type))))
110
111 (defmacro elmo-network-stream-type-function (stream-type)
112   (` (nth 3 (, stream-type))))
113
114 (defsubst elmo-network-session-password-key (session)
115   (format "%s:%s/%s@%s:%d"
116           (upcase
117            (nth 1 (split-string (symbol-name
118                                  (luna-class-name session)) "[4-]")))
119           (elmo-network-session-user-internal session)
120           (elmo-network-session-auth-internal session)
121           (elmo-network-session-server-internal session)
122           (elmo-network-session-port-internal session)))
123
124 (defvar elmo-network-session-cache nil)
125
126 (defsubst elmo-network-session-cache-key (name folder)
127   "Returns session cache key for NAME and FOLDER."
128   (format "%s:%s/%s@%s:%d%s"
129           name
130           (elmo-net-folder-user-internal folder)
131           (elmo-net-folder-auth-internal folder)
132           (elmo-net-folder-server-internal folder)
133           (elmo-net-folder-port-internal folder)
134           (or
135            (elmo-network-stream-type-spec-string
136             (elmo-net-folder-stream-type-internal folder)) "")))
137
138 (defun elmo-network-clear-session-cache ()
139   "Clear session cache."
140   (interactive)
141   (dolist (pair elmo-network-session-cache)
142     (elmo-network-close-session (cdr pair)))
143   (setq elmo-network-session-cache nil))
144
145 (defmacro elmo-network-session-buffer (session)
146   "Get buffer for SESSION."
147   (` (process-buffer (elmo-network-session-process-internal
148                       (, session)))))
149
150 (defun elmo-network-get-session (class name folder &optional if-exists)
151   "Get network session from session cache or a new network session.
152 CLASS is the class name of the session.
153 NAME is the name of the process.
154 FOLDER is the ELMO folder structure.
155 Returns a `elmo-network-session' instance.
156 If optional argument IF-EXISTS is non-nil, it does not return session
157 if there is no session cache.
158 if making session failed, returns nil."
159   (let (pair session key)
160     (if (not (elmo-plugged-p
161               (elmo-net-folder-server-internal folder)
162               (elmo-net-folder-port-internal folder)
163               (elmo-network-stream-type-symbol
164                (elmo-net-folder-stream-type-internal folder))))
165         (error "Unplugged"))
166     (setq pair (assoc (setq key (elmo-network-session-cache-key name folder))
167                       elmo-network-session-cache))
168     (when (and pair
169                (not (memq (process-status
170                            (elmo-network-session-process-internal
171                             (cdr pair)))
172                           '(open run))))
173       (setq elmo-network-session-cache
174             (delq pair elmo-network-session-cache))
175       (elmo-network-close-session (cdr pair))
176       (setq pair nil))
177     (if pair
178         (cdr pair)                      ; connection cache exists.
179       (unless if-exists
180         (setq session
181               (elmo-network-open-session
182                class
183                name
184                (elmo-net-folder-server-internal folder)
185                (elmo-net-folder-port-internal folder)
186                (elmo-net-folder-user-internal folder)
187                (elmo-net-folder-auth-internal folder)
188                (elmo-net-folder-stream-type-internal folder)))
189         (setq elmo-network-session-cache
190               (cons (cons key session)
191                     elmo-network-session-cache))
192         session))))
193
194 (defun elmo-network-open-session (class name server port user auth
195                                         stream-type)
196   "Open an authenticated network session.
197 CLASS is the class name of the session.
198 NAME is the name of the process.
199 SERVER is the name of the server server.
200 PORT is the port number of the service.
201 USER is the user-id for the authenticate.
202 AUTH is the authenticate method name (symbol).
203 STREAM-TYPE is the stream type (See also `elmo-network-stream-type-alist').
204 Returns a process object.  if making session failed, returns nil."
205   (let ((session
206          (luna-make-entity class
207                            :name name
208                            :server server
209                            :port port
210                            :user user
211                            :auth auth
212                            :stream-type stream-type
213                            :process nil
214                            :greeting nil))
215         (buffer (format " *%s session for %s@%s:%d%s"
216                         name
217                         user
218                         server
219                         port
220                         (or (elmo-network-stream-type-spec-string stream-type)
221                             "")))
222         process)
223     (condition-case error
224         (progn
225           (if (get-buffer buffer) (kill-buffer buffer))
226           (setq buffer (get-buffer-create buffer))
227           (elmo-network-initialize-session-buffer session buffer)
228           (elmo-network-session-set-process-internal
229            session
230            (setq process (elmo-open-network-stream
231                           (elmo-network-session-name-internal session)
232                           buffer server port stream-type)))
233           (when process
234             (elmo-network-initialize-session session)
235             (elmo-network-authenticate-session session)
236             (elmo-network-setup-session session)))
237       (error
238        (when (eq (car error) 'elmo-open-error)
239          (elmo-set-plugged nil server port
240                            (elmo-network-stream-type-symbol stream-type)
241                            (current-time))
242          (message "Auto plugged off at %s:%d :%s" server port (cadr error))
243          (sit-for 1))
244        (when (eq (car error) 'elmo-authenticate-error)
245          (elmo-remove-passwd (elmo-network-session-password-key session)))
246        (elmo-network-close-session session)
247        (signal (car error)(cdr error))))
248     session))
249
250 (defun elmo-open-network-stream (name buffer server service stream-type)
251   (let ((auto-plugged (and elmo-auto-change-plugged
252                            (> elmo-auto-change-plugged 0)))
253         process)
254     (if (and stream-type
255              (elmo-network-stream-type-feature stream-type))
256         (require (elmo-network-stream-type-feature stream-type)))
257     (condition-case err
258         (let (process-connection-type)
259           (as-binary-process
260            (setq process
261                  (if stream-type
262                      (funcall (elmo-network-stream-type-function stream-type)
263                               name buffer server service)
264                    (open-network-stream name buffer server service)))))
265       (error
266        (when auto-plugged
267          (elmo-set-plugged nil server service
268                            (elmo-network-stream-type-symbol stream-type)
269                            (current-time))
270          (message "Auto plugged off at %s:%d" server service)
271          (sit-for 1))
272        (signal (car err) (cdr err))))
273     (when process
274       (process-kill-without-query process)
275       (when auto-plugged
276         (elmo-set-plugged t server service
277                           (elmo-network-stream-type-symbol stream-type)))
278       process)))
279
280 (defun elmo-get-network-stream-type (symbol)
281   "Return network stream type corresponding to SYMBOL.
282 Returned value is searched from `elmo-network-stream-type-alist'."
283   (let ((alist elmo-network-stream-type-alist)
284         spec)
285     (while alist
286       (when (eq (nth 1 (car alist)) symbol)
287         (setq spec (car alist))
288         (setq alist nil))
289       (setq alist (cdr alist)))
290     spec))
291
292 (luna-define-method elmo-folder-initialize ((folder
293                                              elmo-net-folder)
294                                             name)
295   ;; user and auth should be set in subclass.
296   (when (string-match "\\(@[^@:/!]+\\)?\\(:[0-9]+\\)?\\(!.*\\)?$" name)
297     (if (match-beginning 1)
298         (elmo-net-folder-set-server-internal
299          folder
300          (elmo-match-substring 1 name 1)))
301     (if (match-beginning 2)
302         (elmo-net-folder-set-port-internal
303          folder
304          (string-to-int (elmo-match-substring 2 name 1))))
305     (if (match-beginning 3)
306         (elmo-net-folder-set-stream-type-internal
307          folder
308          (assoc (elmo-match-string 3 name)
309                 elmo-network-stream-type-alist)))
310     (substring name 0 (match-beginning 0))))
311
312 (luna-define-method elmo-net-port-info ((folder elmo-net-folder))
313   (list (elmo-net-folder-server-internal folder)
314         (elmo-net-folder-port-internal folder)
315         (elmo-network-stream-type-symbol
316          (elmo-net-folder-stream-type-internal folder))))
317
318 (defun elmo-net-port-label (folder)
319   (concat
320    (symbol-name (elmo-folder-type-internal folder))
321    (if (elmo-net-folder-stream-type-internal folder)
322        (concat "!" (symbol-name
323                     (elmo-network-stream-type-symbol
324                      (elmo-net-folder-stream-type-internal
325                       folder)))))))
326
327 (luna-define-method elmo-folder-plugged-p ((folder elmo-net-folder))
328   (apply 'elmo-plugged-p
329          (append (elmo-net-port-info folder)
330                  (list nil (quote (elmo-net-port-label folder))))))
331
332 (luna-define-method elmo-folder-set-plugged ((folder elmo-net-folder)
333                                              plugged &optional add)
334   (apply 'elmo-set-plugged plugged
335          (append (elmo-net-port-info folder)
336                  (list nil nil (quote (elmo-net-port-label folder)) add))))
337
338 (luna-define-method elmo-folder-create ((folder elmo-net-folder))
339   (if (elmo-folder-plugged-p folder)
340       (elmo-folder-send folder 'elmo-folder-create-plugged)
341     (elmo-folder-send folder 'elmo-folder-create-unplugged)))
342
343 (luna-define-method elmo-folder-create-unplugged ((folder elmo-net-folder))
344   (if elmo-enable-disconnected-operation
345       (elmo-folder-create-dop folder)
346     (error "Unplugged")))
347
348 (luna-define-method elmo-folder-exists-p ((folder elmo-net-folder))
349   (if (elmo-folder-plugged-p folder)
350       (elmo-folder-send folder 'elmo-folder-exists-p-plugged)
351     ;; If unplugged, guess by msgdb.
352     (file-directory-p (elmo-folder-msgdb-path folder))))
353
354 (luna-define-method elmo-folder-status ((folder elmo-net-folder))
355   (if (elmo-folder-plugged-p folder)
356       (elmo-folder-send folder 'elmo-folder-status-plugged)
357     (elmo-folder-send folder 'elmo-folder-status-unplugged)))
358
359 (luna-define-method elmo-folder-status-unplugged
360   ((folder elmo-net-folder))
361   (if elmo-enable-disconnected-operation
362       (elmo-folder-status-dop folder)
363     (error "Unplugged")))
364
365 (luna-define-method elmo-folder-list-messages-internal
366   ((folder elmo-net-folder) &optional nohide)
367   (elmo-net-folder-list-messages-internal folder nohide))
368
369 (defun elmo-net-folder-list-messages-internal (folder nohide)
370   (if (elmo-folder-plugged-p folder)
371       (elmo-folder-send folder 'elmo-folder-list-messages-plugged nohide)
372     (elmo-folder-send folder 'elmo-folder-list-messages-unplugged)))
373
374 (luna-define-method elmo-folder-list-messages-plugged
375   ((folder elmo-net-folder))
376   nil)
377
378 ;; Should consider offline append and removal.
379 (luna-define-method elmo-folder-list-messages-unplugged ((folder
380                                                           elmo-net-folder))
381   (if elmo-enable-disconnected-operation
382       (let ((deleting (elmo-dop-list-deleting-messages folder)))
383         (nconc
384          ;; delete deleting messages
385          (elmo-delete-if
386           (lambda (number) (memq number deleting))
387           ;; current number-list.
388           (mapcar
389            'car
390            (elmo-msgdb-get-number-alist (elmo-folder-msgdb folder))))
391          ;; append appending messages
392          (mapcar (lambda (x) (* -1 x))
393                  (elmo-dop-spool-folder-list-messages folder))))
394     t))
395
396 (luna-define-method elmo-folder-list-unreads :around ((folder
397                                                        elmo-net-folder))
398   (if (and (elmo-folder-plugged-p folder)
399            (elmo-folder-use-flag-p folder))
400       (elmo-folder-send folder 'elmo-folder-list-unreads-plugged)
401     (luna-call-next-method)))
402
403 (luna-define-method elmo-folder-list-importants :around ((folder
404                                                           elmo-net-folder))
405   (if (and (elmo-folder-plugged-p folder)
406            (elmo-folder-use-flag-p folder))
407       (elmo-folder-send folder 'elmo-folder-list-importants-plugged)
408     (luna-call-next-method)))
409
410 (luna-define-method elmo-folder-list-answereds :around ((folder
411                                                          elmo-net-folder))
412   (if (and (elmo-folder-plugged-p folder)
413            (elmo-folder-use-flag-p folder))
414       (elmo-folder-send folder 'elmo-folder-list-answereds-plugged)
415     (luna-call-next-method)))
416
417 (luna-define-method elmo-folder-list-unreads-plugged
418   ((folder elmo-net-folder))
419   t)
420
421 (luna-define-method elmo-folder-list-importants-plugged
422   ((folder elmo-net-folder))
423   t)
424
425 (luna-define-method elmo-folder-list-answereds-plugged
426   ((folder elmo-net-folder))
427   t)
428
429 (luna-define-method elmo-folder-delete-messages ((folder elmo-net-folder)
430                                                  numbers)
431   (if (elmo-folder-plugged-p folder)
432       (elmo-folder-send folder 'elmo-folder-delete-messages-plugged numbers)
433     (elmo-folder-send folder 'elmo-folder-delete-messages-unplugged numbers)))
434
435 (luna-define-method elmo-folder-delete-messages-unplugged ((folder
436                                                             elmo-net-folder)
437                                                            numbers)
438   (elmo-folder-delete-messages-dop folder numbers))
439
440 (luna-define-method elmo-folder-msgdb-create ((folder elmo-net-folder)
441                                               numbers seen-list)
442   (if (elmo-folder-plugged-p folder)
443       (elmo-folder-send folder 'elmo-folder-msgdb-create-plugged
444                         numbers seen-list)
445     (elmo-folder-send folder 'elmo-folder-msgdb-create-unplugged
446                       numbers seen-list)))
447
448 (luna-define-method elmo-folder-msgdb-create-unplugged ((folder
449                                                          elmo-net-folder)
450                                                         numbers
451                                                         seen-list)
452   ;; XXXX should be appended to already existing msgdb.
453   (elmo-dop-msgdb
454    (elmo-folder-msgdb-create (elmo-dop-spool-folder folder)
455                              (mapcar 'abs numbers)
456                              seen-list)))
457
458 (luna-define-method elmo-folder-unmark-important :before ((folder
459                                                            elmo-net-folder)
460                                                           numbers)
461   (when (elmo-folder-use-flag-p folder)
462     (if (elmo-folder-plugged-p folder)
463         (elmo-folder-send folder 'elmo-folder-unmark-important-plugged
464                           numbers)
465       (elmo-folder-send folder
466                         'elmo-folder-unmark-important-unplugged numbers))))
467
468 (luna-define-method elmo-folder-mark-as-important :before ((folder
469                                                             elmo-net-folder)
470                                                            numbers)
471   (when (elmo-folder-use-flag-p folder)
472     (if (elmo-folder-plugged-p folder)
473         (elmo-folder-send folder 'elmo-folder-mark-as-important-plugged
474                           numbers)
475       (elmo-folder-send folder 'elmo-folder-mark-as-important-unplugged
476                         numbers))))
477
478 (luna-define-method elmo-folder-unmark-read :before ((folder elmo-net-folder)
479                                                      numbers
480                                                      &optional ignore-flag)
481   (when (and (elmo-folder-use-flag-p folder)
482              (not ignore-flag))
483     (if (elmo-folder-plugged-p folder)
484         (elmo-folder-send folder 'elmo-folder-unmark-read-plugged numbers)
485       (elmo-folder-send folder 'elmo-folder-unmark-read-unplugged numbers))))
486
487 (luna-define-method elmo-folder-mark-as-read :before ((folder elmo-net-folder)
488                                                       numbers
489                                                       &optional ignore-flag)
490   (when (and (elmo-folder-use-flag-p folder)
491              (not ignore-flag))
492     (if (elmo-folder-plugged-p folder)
493         (elmo-folder-send folder 'elmo-folder-mark-as-read-plugged numbers)
494       (elmo-folder-send
495        folder 'elmo-folder-mark-as-read-unplugged numbers))))
496
497 (luna-define-method elmo-folder-unmark-answered :before ((folder
498                                                           elmo-net-folder)
499                                                          numbers)
500   (when (elmo-folder-use-flag-p folder)
501     (if (elmo-folder-plugged-p folder)
502         (elmo-folder-send folder 'elmo-folder-unmark-answered-plugged
503                           numbers)
504       (elmo-folder-send folder
505                         'elmo-folder-unmark-answered-unplugged numbers))))
506
507 (luna-define-method elmo-folder-mark-as-answered :before ((folder
508                                                            elmo-net-folder)
509                                                           numbers)
510   (when (elmo-folder-use-flag-p folder)
511     (if (elmo-folder-plugged-p folder)
512         (elmo-folder-send folder 'elmo-folder-mark-as-answered-plugged
513                           numbers)
514       (elmo-folder-send folder 'elmo-folder-mark-as-answered-unplugged
515                         numbers))))
516
517 (luna-define-method elmo-folder-mark-as-read-unplugged ((folder
518                                                          elmo-net-folder)
519                                                         numbers)
520   (elmo-folder-mark-as-read-dop folder numbers))
521
522 (luna-define-method elmo-folder-unmark-read-unplugged ((folder elmo-net-folder)
523                                                      numbers)
524   (elmo-folder-unmark-read-dop folder numbers))
525
526 (luna-define-method elmo-folder-mark-as-important-unplugged ((folder
527                                                               elmo-net-folder)
528                                                              numbers)
529   (elmo-folder-mark-as-important-dop folder numbers))
530
531 (luna-define-method elmo-folder-unmark-important-unplugged ((folder
532                                                              elmo-net-folder)
533                                                             numbers)
534   (elmo-folder-unmark-important-dop folder numbers))
535
536 (luna-define-method elmo-folder-mark-as-answered-unplugged ((folder
537                                                              elmo-net-folder)
538                                                             numbers)
539   (elmo-folder-mark-as-answered-dop folder numbers))
540
541 (luna-define-method elmo-folder-unmark-answered-unplugged
542   ((folder elmo-net-folder) numbers)
543   (elmo-folder-unmark-answered-dop folder numbers))
544
545 (luna-define-method elmo-message-encache :around ((folder elmo-net-folder)
546                                                   number &optional read)
547   (if (elmo-folder-plugged-p folder)
548       (luna-call-next-method)
549     (if elmo-enable-disconnected-operation
550         (elmo-message-encache-dop folder number read)
551       (error "Unplugged"))))
552
553 (luna-define-generic elmo-message-fetch-plugged (folder number strategy
554                                                         &optional
555                                                         section
556                                                         outbuf
557                                                         unseen)
558   "")
559
560 (luna-define-generic elmo-message-fetch-unplugged (folder number strategy
561                                                           &optional
562                                                           section
563                                                           outbuf
564                                                           unseen)
565   "")
566
567 (luna-define-method elmo-message-fetch-internal ((folder elmo-net-folder)
568                                                  number strategy
569                                                  &optional section unseen)
570   (if (elmo-folder-plugged-p folder)
571       (elmo-message-fetch-plugged folder number
572                                   strategy section
573                                   (current-buffer) unseen)
574     (elmo-message-fetch-unplugged folder number
575                                   strategy section
576                                   (current-buffer) unseen)))
577
578 (luna-define-method elmo-message-fetch-unplugged
579   ((folder elmo-net-folder) number strategy  &optional section outbuf unseen)
580   (if (and elmo-enable-disconnected-operation
581            (< number 0))
582       (elmo-message-fetch-internal
583        (elmo-dop-spool-folder folder) (abs number) strategy
584        section unseen)
585     (error "Unplugged")))
586
587 (luna-define-method elmo-folder-check ((folder elmo-net-folder))
588   (if (elmo-folder-plugged-p folder)
589       (elmo-folder-send folder 'elmo-folder-check-plugged)))
590
591 (luna-define-method elmo-folder-close :after ((folder elmo-net-folder))
592   (if (elmo-folder-plugged-p folder)
593       (elmo-folder-send folder 'elmo-folder-check-plugged)))
594
595 (luna-define-method elmo-folder-diff :around ((folder elmo-net-folder)
596                                               &optional numbers)
597   (if (and (elmo-folder-use-flag-p folder)
598            (elmo-folder-plugged-p folder))
599       (elmo-folder-send folder 'elmo-folder-diff-plugged)
600     (luna-call-next-method)))
601
602 (luna-define-method elmo-folder-local-p ((folder elmo-net-folder))
603   nil)
604
605 (luna-define-method elmo-quit ((folder elmo-net-folder))
606   (elmo-network-clear-session-cache))
607
608 (require 'product)
609 (product-provide (provide 'elmo-net) (require 'elmo-version))
610
611 ;;; elmo-net.el ends here