Debian Codegraph @

mew-local.el

raw
1 ;;; mew-local.el
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: Dec 12, 2001
5
6 (eval-when-compile
7 (require 'mew-summary))
8
9 (defvar mew-local-folder-alist-file ".mew-folder-alist")
10 (defvar mew-local-folder-alist nil)
11 (defvar mew-local-folder-alist-time nil)
12
13 (defvar mew-local-friend-folder-list-file ".mew-friend-folder-list")
14 (defvar mew-local-friend-folder-list nil)
15
16 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17 ;;;
18 ;;; Listing directories
19 ;;;
20
21 (defun mew-dir-list (dir)
22 (if (file-directory-p (expand-file-name dir))
23 (funcall mew-dir-list-function dir)
24 ()))
25
26 (defun mew-dir-list-with-link-count (dir)
27 "Collecting a directory list under DIR.
28 Subdirectories are expressed by a list.
29 This function uses two techniques for speed up.
30
31 One is to gather candidates of directory by matching
32 'mew-regex-folder-candidate'. The default is
33 \"^[^.0-9]\\|^[0-9].*[^0-9]\". So, typical messages whose name is
34 numeric are not gathered. This makes it faster to check whether or not
35 each candidate is a directory in 'while' loop.
36
37 The other is to see if the link count of a directory is 2. If so, the
38 directory does not have subdirectories. So, it is not necessary to
39 trace down. This technique can be used on UNIX variants."
40 (let* ((default-directory (expand-file-name dir default-directory))
41 (dirent (directory-files "." nil mew-regex-folder-candidate)) ;; MUST sort
42 dirs ent subdirs)
43 (dolist (file dirent)
44 (setq ent (mew-file-chase-links file))
45 (when (file-directory-p ent)
46 (setq dirs (cons file dirs))
47 (when (and (mew-file-get-links ent) ;; necessary
48 (/= (mew-file-get-links ent) 2))
49 (setq subdirs (mew-dir-list-with-link-count file))
50 (if subdirs (setq dirs (cons subdirs dirs))))))
51 (nreverse dirs)))
52
53 (defun mew-dir-list-without-link-count (dir)
54 "Collecting a directory list under DIR.
55 Subdirectories are expressed by a list.
56 This function uses one technique for speed up.
57
58 It is to gather candidates of directory by matching
59 'mew-regex-folder-candidate'. The default is
60 \"^[^.0-9]\\|^[0-9].*[^0-9]\". So, typical messages whose name is
61 numeric are not gathered. This makes it faster to check whether or not
62 each candidate is a directory in 'while' loop."
63 (let* ((default-directory (expand-file-name dir default-directory))
64 (dirent (directory-files "." nil mew-regex-folder-candidate)) ;; MUST sort
65 dirs ent subdirs)
66 (dolist (file dirent)
67 (setq ent (mew-file-chase-links file))
68 (when (file-directory-p ent)
69 (setq dirs (cons file dirs))
70 (setq subdirs (mew-dir-list-without-link-count file))
71 (if subdirs (setq dirs (cons subdirs dirs)))))
72 (nreverse dirs)))
73
74 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
75 ;;;
76 ;;; Folder list
77 ;;;
78
79 (defun mew-local-folder-entry (ent1 ent2 make-list)
80 (if make-list
81 ent1
82 (mew-folder-func ent1 ent2)))
83
84 (defun mew-local-folder-make-alist (dirs prefix &optional make-list)
85 (let (fldpfx dir ret ent)
86 (if (= (length prefix) 1)
87 (setq fldpfx prefix)
88 (setq fldpfx (file-name-as-directory prefix)))
89 (while dirs
90 (setq dir (car dirs))
91 (setq dirs (cdr dirs))
92 (setq ent (concat fldpfx dir))
93 (cond
94 ((consp (car dirs)) ;; not listp because nil is a list.
95 (cond
96 ((or (equal mew-attach-folder ent) (equal mew-draft-folder ent))
97 (setq ret (cons (mew-local-folder-entry ent nil make-list) ret))
98 (setq dirs (cdr dirs))) ;; skip subfolder
99 (t
100 (setq ret (cons (mew-local-folder-entry (file-name-as-directory ent) dir make-list) ret))
101 (setq ret (nconc (mew-local-folder-make-alist (car dirs) ent make-list) ret))
102 (setq dirs (cdr dirs)))))
103 (t
104 (if (string-match mew-regex-ignore-folders ent)
105 (setq ret (cons (mew-local-folder-entry ent nil make-list) ret))
106 (setq ret (cons (mew-local-folder-entry ent dir make-list) ret))))))
107 ret))
108
109 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
110 ;;;
111 ;;; Folders
112 ;;;
113
114 (defun mew-local-folder-clean-up ()
115 (setq mew-local-folder-alist nil)
116 (setq mew-local-friend-folder-list nil))
117
118 (defun mew-local-folder-alist ()
119 (let* ((file (expand-file-name mew-local-folder-alist-file mew-conf-path))
120 (t1 (mew-file-get-time file))
121 (t2 mew-local-folder-alist-time))
122 (if (mew-compare-times t1 t2)
123 (mew-local-folder-load))
124 mew-local-folder-alist))
125
126 (defun mew-local-friend-folder-list ()
127 mew-local-friend-folder-list)
128
129 (defun mew-local-folder-load ()
130 (let ((file (expand-file-name mew-local-folder-alist-file mew-conf-path)))
131 (setq mew-local-folder-alist
132 (mew-lisp-load mew-local-folder-alist-file))
133 (setq mew-local-folder-alist-time (mew-file-get-time file))
134 (setq mew-local-friend-folder-list
135 (mew-lisp-load mew-local-friend-folder-list-file))))
136
137 (defun mew-local-folder-save ()
138 (let ((file (expand-file-name mew-local-folder-alist-file mew-conf-path)))
139 (mew-lisp-save mew-local-folder-alist-file
140 mew-local-folder-alist)
141 (setq mew-local-folder-alist-time (mew-file-get-time file))
142 (mew-lisp-save mew-local-friend-folder-list-file
143 mew-local-friend-folder-list)))
144
145 (defun mew-local-folder-set (folders friends)
146 (setq mew-local-folder-alist folders)
147 (setq mew-local-friend-folder-list friends)
148 (mew-local-folder-save))
149
150 (defun mew-local-update (&optional interactivep)
151 "Update the list of local folders."
152 (interactive)
153 (unless interactivep
154 (add-hook 'kill-emacs-hook 'mew-folder-clean-up)
155 (mew-local-folder-load))
156 ;;
157 (when (or interactivep (null mew-local-folder-alist))
158 (message "Collecting local folders...")
159 (let* ((mail-dirs (mew-dir-list mew-mail-path))
160 (from-dirs (mew-dir-list (mew-expand-folder mew-friend-folder)))
161 (folders (mew-local-folder-make-alist mail-dirs mew-folder-local))
162 (friends (mew-local-folder-make-alist from-dirs mew-friend-folder t)))
163 (setq folders (nreverse folders))
164 (setq friends (nreverse friends))
165 (mew-local-folder-set folders friends))
166 (message "Collecting local folders...done")))
167
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169 ;;;
170 ;;; Folder sub function
171 ;;;
172
173 (defun mew-local-folder-insert (folder)
174 "Insert FOLDER to 'mew-local-folder-alist'.
175 Binary search is used for speed reasons."
176 (let (dir subdir)
177 (unless (string-match mew-regex-ignore-folders folder)
178 (setq dir (directory-file-name (mew-folder-string folder)))
179 ;; foo/bar -> foo/bar
180 ;; foo/bar/ -> foo/bar
181 (setq subdir (file-name-nondirectory dir))
182 ;; foo/bar -> bar
183 ;; foo -> foo
184 )
185 (mew-folder-insert folder mew-local-folder-alist subdir)
186 (if (and (string-match (concat "^" (regexp-quote mew-friend-folder)) folder)
187 (not (member folder mew-local-friend-folder-list)))
188 (setq mew-local-friend-folder-list
189 (cons folder mew-local-friend-folder-list)))
190 (mew-local-folder-save)))
191
192 (defun mew-local-folder-delete (folder)
193 (mew-folder-delete folder mew-local-folder-alist)
194 (setq mew-local-friend-folder-list
195 (delete folder mew-local-friend-folder-list))
196 (mew-local-folder-save))
197
198 (defun mew-local-folder-check (folder &optional ask)
199 (let ((absdir (mew-expand-folder folder)))
200 (if (file-directory-p absdir)
201 t
202 (if (or (not ask)
203 (y-or-n-p (format "%s does not exist. Create it? " folder)))
204 (progn
205 (mew-make-directory absdir)
206 ;; may be called by IMAP...
207 (if (mew-folder-localp folder)
208 (mew-local-folder-insert folder))
209 folder)
210 nil))))
211
212 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
213 ;;;
214 ;;; Process name
215 ;;;
216
217 (defvar mew-local-info-list
218 '("directive" "buf" "bnm" "mdb" "flush" "rcnt" "rttl" "first" "case"))
219
220 (mew-info-defun "mew-local-" mew-local-info-list)
221
222 (defconst mew-local-info-prefix "mew-local-info-")
223
224 (defun mew-local-info-name (bnm)
225 (format "%s<%s>" mew-local-info-prefix bnm))
226
227 (defun mew-local-buffer-name (folder)
228 (concat mew-buffer-prefix folder))
229
230 (defun mew-local-debug (label string)
231 (when (mew-debug 'ls)
232 (with-current-buffer (get-buffer-create mew-buffer-debug)
233 (goto-char (point-max))
234 (insert (format "\n<%s>\n%s\n" label string)))))
235
236 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
237 ;;;
238 ;;; Scan
239 ;;;
240
241 (defun mew-scan-mewl-folder (folder)
242 (mew-replace-character folder mew-sp ?\007))
243
244 (defun mew-scan-mewl-src (folder &optional range)
245 (setq folder (mew-scan-mewl-folder (mew-expand-folder2 folder)))
246 (if range
247 (list "-s" (format "%s %s" folder range))
248 (list "-s" folder)))
249
250 (defun mew-local-retrieve (directive &rest args)
251 ;; 'inc folder opts
252 ;; 'scan folder range
253 ;; 'vir opts func lra erase
254 (mew-summary-with-mewl
255 (let* ((process-connection-type mew-connection-type1)
256 (bnm (mew-summary-folder-name 'ext))
257 (pnm (mew-local-info-name bnm))
258 (buf (get-buffer-create (mew-local-buffer-name bnm)))
259 range pro opts)
260 (message "Scanning %s..." bnm)
261 (mew-sinfo-set-summary-form (mew-get-summary-form bnm))
262 (mew-sinfo-set-summary-column (mew-get-summary-column bnm))
263 (mew-sinfo-set-unread-mark nil)
264 (mew-sinfo-set-scan-id nil)
265 (mew-sinfo-set-scan-md5 nil)
266 (mew-info-clean-up pnm)
267 (mew-local-set-directive pnm directive)
268 (mew-local-set-bnm pnm bnm)
269 (mew-local-set-buf pnm buf)
270 (mew-local-set-case pnm mew-case) ;; xxx specfy to mew-local-retrieve?
271 (cond
272 ((eq directive 'inc)
273 (mew-sinfo-set-unread-mark (mew-get-unread-mark bnm))
274 (mew-local-set-flush pnm (nth 1 args))
275 (setq opts (append (nth 2 args) (mew-scan-mewl-src (nth 0 args)))))
276 ((eq directive 'scan)
277 (setq range (nth 1 args))
278 (if (nth 2 args)
279 (progn
280 (mew-local-set-mdb pnm (mew-summary-mark-collect4))
281 (mew-erase-buffer)
282 (setq mew-summary-buffer-raw nil)
283 (mew-summary-folder-cache-save))
284 (mew-sinfo-set-unread-mark (mew-get-unread-mark bnm)))
285 (setq opts (mew-scan-mewl-src (nth 0 args) range)))
286 ((eq directive 'vir)
287 (mew-sinfo-set-unread-mark (mew-get-unread-mark bnm))
288 (setq opts (nth 0 args))
289 (mew-vinfo-set-func (nth 1 args))
290 (mew-vinfo-set-lra (nth 2 args))
291 (unless (nth 3 args) (mew-erase-buffer))
292 (mew-local-set-rttl pnm (nth 4 args))))
293 (mew-sinfo-set-start-point (point)) ;; after erase-buffer
294 (mew-local-set-rcnt pnm 1)
295 (with-current-buffer buf
296 (mew-erase-buffer))
297 (setq opts (append (list "-b" mew-mail-path
298 "-l" (number-to-string mew-scan-max-field-length)
299 "-c" (number-to-string mew-scan-max-body-length)
300 "-w"
301 "-x" mew-suffix
302 "-f" (mapconcat 'identity
303 (nthcdr 2 mew-scan-fields)
304 ","))
305 opts))
306 (setq pro (apply 'start-process pnm buf mew-prog-mewl opts))
307 (mew-summary-lock pro "Scanning")
308 (mew-set-process-cs pro mew-cs-text-for-net mew-cs-text-for-write)
309 ;; text may be broken, so undecided is very dangerous!
310 (set-process-filter pro 'mew-local-filter)
311 (set-process-sentinel pro 'mew-local-sentinel)
312 (mew-process-silent-exit pro))))
313
314 (defun mew-local-filter (process string)
315 (let* ((width (1- (mew-scan-width)))
316 (pnm (process-name process))
317 (bnm (mew-local-get-bnm pnm))
318 (first (mew-local-get-first pnm))
319 (draftp (mew-folder-draftp bnm))
320 (directive (mew-local-get-directive pnm))
321 (case (mew-local-get-case pnm))
322 vec rttl mark)
323 (mew-local-debug "FILTER" string)
324 (mew-filter
325 (mew-set-buffer-multibyte nil)
326 (goto-char (point-max))
327 (mew-elet (insert string))
328 (goto-char (point-max))
329 (forward-line -1)
330 (when (looking-at "^\\.$")
331 (mew-dot-delete)
332 (goto-char (point-min))
333 (unless first
334 (when (looking-at "NumOfMsg: \\([0-9]+\\)")
335 (setq rttl (string-to-number (match-string 1)))
336 (mew-local-set-rttl pnm rttl)
337 (forward-line)
338 (delete-region (point-min) (point)))
339 (mew-local-set-first pnm t))
340 (mew-net-status3 bnm (mew-local-get-rttl pnm) (mew-local-get-rcnt pnm))
341 (mew-local-set-rcnt pnm (1+ (mew-local-get-rcnt pnm)))
342 (mew-set-buffer-multibyte t)
343 (setq vec (mew-scan-header draftp))
344 (if (eq directive 'inc) (setq mark (mew-scan-inbox-action vec case)))
345 (mew-scan-body vec draftp)
346 (mew-scan-insert-line bnm vec width nil mark)
347 (delete-region (point-min) (point-max))
348 (process-send-string process "\n")))))
349
350 (defun mew-local-sentinel (process event)
351 (let* ((pnm (process-name process))
352 (bnm (mew-local-get-bnm pnm))
353 (buf (mew-local-get-buf pnm))
354 (virtualp (mew-folder-virtualp bnm))
355 (mdb (mew-local-get-mdb pnm))
356 (flush (mew-local-get-flush pnm)))
357 (save-excursion
358 (mew-local-debug "SENTINEL" event)
359 (mew-filter
360 (mew-set-buffer-multibyte t)
361 (set-buffer bnm)
362 (mew-summary-mark-recover mdb)
363 (if (and virtualp (mew-vinfo-get-func))
364 (funcall (mew-vinfo-get-func)))
365 (mew-vinfo-set-func nil)
366 (mew-info-clean-up pnm)
367 (if virtualp
368 (progn
369 (mew-summary-set-count-line)
370 (mew-virtual-set-cache-time))
371 (mew-summary-folder-cache-save))
372 (set-buffer-modified-p nil)
373 (mew-summary-unlock)
374 (if (and (mew-folder-imapp bnm)
375 (not (mew-folder-imap-queuep)))
376 (if (mew-local-get-rttl pnm)
377 (message "Type '\\[mew-summary-ls]' to override invalid messages")
378 ;; This folder has been just created.
379 ;; Ideally scan should not be called. But there is no way
380 ;; to avoid the call.
381 (message ""))
382 (message "Scanning %s...done" bnm))
383 (run-hooks 'mew-scan-sentinel-hook)
384 (when (and mew-auto-flush-queue flush)
385 (mew-smtp-flush-queue mew-case))
386 (mew-remove-buffer buf)))))
387
388 (defun mew-virtual-set-cache-time ()
389 (let* ((ctime (current-time))
390 (cache-time (list (nth 0 ctime) (nth 1 ctime))))
391 (mew-sinfo-set-cache-time cache-time)))
392
393 ;;; Code:
394
395 (require 'mew)
396
397 (provide 'mew-local)
398
399 ;;; Copyright Notice:
400
401 ;; Copyright (C) 2001-2015 Mew developing team.
402 ;; All rights reserved.
403
404 ;; Redistribution and use in source and binary forms, with or without
405 ;; modification, are permitted provided that the following conditions
406 ;; are met:
407 ;;
408 ;; 1. Redistributions of source code must retain the above copyright
409 ;; notice, this list of conditions and the following disclaimer.
410 ;; 2. Redistributions in binary form must reproduce the above copyright
411 ;; notice, this list of conditions and the following disclaimer in the
412 ;; documentation and/or other materials provided with the distribution.
413 ;; 3. Neither the name of the team nor the names of its contributors
414 ;; may be used to endorse or promote products derived from this software
415 ;; without specific prior written permission.
416 ;;
417 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
418 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
419 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
421 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
422 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
423 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
424 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
425 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
426 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
427 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
428
429 ;;; mew-local.el ends here