Debian Codegraph @

mew-scan.el

raw
1 ;;; mew-scan.el --- Scanning messages for Mew
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: Oct 2, 1996
5
6 ;;; Code:
7
8 (require 'mew)
9 (eval-when-compile
10 (require 'mew-env0))
11
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 ;;;
14 ;;; Scan info
15 ;;;
16
17 (defvar mew-scan-info-list '("folder" "message"))
18 ;; See mew-scan-fields. 0th is fld, 1st is msg (ie num).
19
20 (mew-info-defun "mew-scan-" mew-scan-info-list)
21
22 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23 ;;;
24 ;;; Variables
25 ;;;
26
27 (defvar mew-summary-form-size-unit '("" "k" "M" "G" "T"))
28 (defvar mew-vec [0 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 17 18 19 20]
29 "Just for test of (MEW-FOO).")
30
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 ;;;
33 ;;; Setup
34 ;;;
35
36 (defun mew-scan-setup ()
37 "Define functions (MEW-FOO) according 'mew-scan-fields-alias'."
38 (dotimes (i (length mew-scan-fields-alias))
39 (fset (intern (concat "MEW-" (nth i mew-scan-fields-alias)))
40 `(lambda () (aref mew-vec ,i)))))
41
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 ;;;
44 ;;; Pre-defined functions for mew-summary-form
45 ;;;
46
47 (defun mew-summary-form-mark ()
48 "A function to return a mark.
49 'mew-summary-form-mark-delete' and 'mew-summary-form-mark-review'
50 effect to this function."
51 (let ((mark-delete mew-mark-delete)
52 (mark-review mew-mark-review)
53 (mark-spam mew-mark-delete)
54 (unread (and mew-use-unread-mark (mew-sinfo-get-unread-mark)))
55 duplicated spam review id body md5)
56 (when mew-summary-form-mark-delete
57 (if (mew-characterp mew-summary-form-mark-delete)
58 (setq mark-delete mew-summary-form-mark-delete))
59 (when (setq id (mew-idstr-get-first-id (MEW-ID)))
60 (if (member id (mew-sinfo-get-scan-id))
61 (unless (mew-scan-message-invalidp)
62 (setq duplicated t))
63 (mew-sinfo-set-scan-id (cons id (mew-sinfo-get-scan-id))))))
64 (when (and mew-summary-form-mark-spam
65 (mew-sinfo-get-inboxp))
66 (if (mew-characterp mew-summary-form-mark-spam)
67 (setq mark-spam mew-summary-form-mark-spam))
68 (setq body (MEW-BODY))
69 (unless (string= body "")
70 (setq md5 (mew-md5 body))
71 (if (member md5 (mew-sinfo-get-scan-md5))
72 (unless (mew-scan-message-invalidp)
73 (setq spam t))
74 (mew-sinfo-set-scan-md5 (cons md5 (mew-sinfo-get-scan-md5))))))
75 (when mew-summary-form-mark-review
76 (if (mew-characterp mew-summary-form-mark-review)
77 (setq mark-review mew-summary-form-mark-review))
78 (let* ((mew-header-max-depth nil)
79 (to (mew-addrstr-parse-address-list (MEW-TO)))
80 (cc (mew-addrstr-parse-address-list (MEW-CC))))
81 (setq to (nconc to cc))
82 (catch 'loop
83 (dolist (cto to)
84 (if (mew-is-my-address mew-regex-my-address-list cto)
85 (throw 'loop (setq review t)))))))
86 (cond
87 (duplicated (char-to-string mark-delete))
88 (spam (char-to-string mark-spam))
89 (review (char-to-string mark-review))
90 (unread (char-to-string mew-mark-unread))
91 (t (char-to-string mew-mark-read)))))
92
93 (defvar mew-type-mark-invalid "#")
94 (defvar mew-type-mark-truncated "T")
95 (defvar mew-type-mark-signed "S")
96 (defvar mew-type-mark-encrypted "E")
97 (defvar mew-type-mark-multipart "M")
98 (defvar mew-type-mark-alternative "-")
99 (defvar mew-type-mark-partial "P")
100 (defvar mew-type-mark-nothing " ")
101
102 (defun mew-summary-form-type ()
103 "A function to return a mark of content type."
104 (let ((ct (MEW-CT))
105 (case-fold-search t))
106 (cond
107 ((mew-scan-message-invalidp) mew-type-mark-invalid)
108 ((mew-scan-message-truncatedp) mew-type-mark-truncated)
109 ((string-match mew-ct-mls ct) mew-type-mark-signed)
110 ((string-match mew-ct-mle ct) mew-type-mark-encrypted)
111 ((string-match mew-ct-sms ct) mew-type-mark-encrypted)
112 ((string-match mew-ct-xsms ct) mew-type-mark-encrypted)
113 ((or (string-match mew-ct-smm ct)
114 (string-match mew-ct-xsmm ct))
115 ;; checking smime-type
116 (cond
117 ((string-match mew-ct-smm-sig ct) mew-type-mark-signed)
118 ((string-match mew-ct-smm-enc ct) mew-type-mark-encrypted)
119 ;; smime-type is optional, sigh
120 (t mew-type-mark-encrypted)))
121 ((mew-ct-alternativep ct) mew-type-mark-alternative)
122 ((mew-ct-multipartp ct) mew-type-mark-multipart)
123 ((mew-ct-partialp ct) mew-type-mark-partial)
124 (t mew-type-mark-nothing))))
125
126 (defun mew-summary-form-time ()
127 "A function to return a message time, HH:MM"
128 (let ((s (MEW-DATE)))
129 (if (or (string= s "")
130 (not (string-match mew-time-rfc-regex s)))
131 (setq s (mew-time-ctz-to-rfc
132 (mew-file-get-time (mew-expand-msg (MEW-FLD) (MEW-NUM))))))
133 (if (string-match mew-time-rfc-regex s)
134 (format "%02d:%02d"
135 (or (mew-time-rfc-hour) 0)
136 (or (mew-time-rfc-min) 0))
137 "00:00")))
138
139 (defun mew-summary-form-date ()
140 "A function to return a date, MM/DD."
141 (let ((s (MEW-DATE)))
142 (when (or (string= s "")
143 (not (string-match mew-time-rfc-regex s)))
144 (setq s (mew-time-ctz-to-rfc
145 (mew-file-get-time (mew-expand-msg (MEW-FLD) (MEW-NUM))))))
146 (if (string-match mew-time-rfc-regex s)
147 (format "%02d/%02d"
148 (mew-time-mon-str-to-int (mew-time-rfc-mon))
149 (mew-time-rfc-day))
150 "")))
151
152 (defun mew-summary-form-year ()
153 "A function to return a message year, YYYY"
154 (let ((s (MEW-DATE)) year)
155 (when (or (string= s "")
156 (not (string-match mew-time-rfc-regex s)))
157 (setq s (mew-time-ctz-to-rfc
158 (mew-file-get-time (mew-expand-msg (MEW-FLD) (MEW-NUM))))))
159 (if (not (string-match mew-time-rfc-regex s))
160 "0000"
161 (setq year (mew-time-rfc-year))
162 (cond
163 ((< year 50)
164 (setq year (+ year 2000)))
165 ((< year 100)
166 (setq year (+ year 1900))))
167 (number-to-string year))))
168
169 (defun mew-summary-form-size ()
170 "A function to return the size of the message. Should be used
171 with -4. See also 'mew-summary-form-size-0k' and 'mew-summary-form-size-huge'."
172 (let ((len-1 (1- (length mew-summary-form-size-unit)))
173 (SIZE (mew-scan-uid-size (MEW-UID)))
174 (i 0) size unit)
175 (if (and SIZE (string-match "^[0-9]+$" SIZE))
176 (setq size (string-to-number SIZE))
177 (setq size (mew-file-get-size (mew-expand-msg (MEW-FLD) (MEW-NUM)))))
178 (while (and (< i len-1) (>= size 1000))
179 (setq size (/ size 1000))
180 (setq i (1+ i)))
181 (if (and mew-summary-form-size-huge (>= size 1000))
182 "HUGE"
183 (setq unit (nth i mew-summary-form-size-unit))
184 (if (and mew-summary-form-size-0k (string= unit ""))
185 "0k"
186 (concat
187 (if (integerp size)
188 (number-to-string size)
189 (format "%.0f" size))
190 unit)))))
191
192 (defun mew-summary-form-extract-addr (addr)
193 "Extract addr according to 'mew-summary-form-extract-rule'."
194 (condition-case nil
195 (let* ((func (if mew-addrbook-for-summary
196 (mew-addrbook-func mew-addrbook-for-summary)))
197 (raw (or (mew-addrstr-parse-address addr) ""))
198 (rules mew-summary-form-extract-rule)
199 ret nickname)
200 (catch 'matched
201 (dolist (rule rules)
202 (cond
203 ((and (eq rule 'name)
204 (or (string-match "^\"\\([^\"]+\\)\"[ \t]*<[^>]+>" addr)
205 (string-match "^\\([^<]+\\)<[^>]+>" addr)))
206 (throw 'matched (setq ret (mew-chop (match-string 1 addr)))))
207 ((and (eq rule 'comment)
208 (string-match "^[^(]+(\\(.+\\))" addr))
209 (throw 'matched (setq ret (mew-chop (match-string 1 addr)))))
210 ((eq rule 'address)
211 (throw 'matched (setq ret raw)))
212 ((and (eq rule 'nickname)
213 ;; set nickname here for efficiency
214 (or nickname
215 (setq nickname (if func (funcall func raw)))))
216 (throw 'matched (setq ret nickname)))
217 ((and (stringp rule)
218 (string-match rule addr))
219 (throw 'matched (setq ret (mew-chop (match-string 1 addr))))))))
220 (or ret addr))
221 ;; a version of downcase causes error if argument is non-ascii.
222 (error mew-error-broken-address)))
223
224 (defun mew-summary-form-from ()
225 "A function to return an address.
226 If the message is destined to me AND 'mew-summary-form-from-me-prefix'
227 is a string, an address on To:, is returned. In this
228 case, 'mew-summary-form-from-me-prefix' is prepended to the address.
229
230 Otherwise, an address on From: is returned.
231
232 Address is converted by 'mew-summary-form-extract-addr'. See also
233 'mew-summary-form-extract-rule'."
234 (let* ((FROM (MEW-FROM)) (TO (MEW-TO))
235 (from (or (mew-addrstr-parse-address FROM) "")))
236 (cond
237 ((string= FROM "")
238 "")
239 ((and (stringp mew-summary-form-from-me-prefix)
240 (not (string= TO ""))
241 (mew-is-my-address mew-regex-my-address-list from))
242 (mew-replace-white-space
243 (concat mew-summary-form-from-me-prefix (mew-summary-form-extract-addr TO))))
244 (t
245 (mew-replace-white-space (mew-summary-form-extract-addr FROM))))))
246
247 (defun mew-summary-form-subj ()
248 "A function to return Subject:. Unnecessary white spaces are removed."
249 ;; The beginning white spaces have been removed in mew-scan-header
250 ;; (mew-keyval).
251 (let ((subj (MEW-SUBJ)))
252 (if (string= subj "") (setq subj mew-error-no-subject))
253 (if mew-decode-broken
254 subj
255 ;; already well-formatted
256 (mew-replace-white-space subj))))
257
258 (defun mew-summary-form-body ()
259 (mew-header-sanity-check-string (MEW-BODY)))
260
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
262 ;;;
263 ;;; Scan from
264 ;;;
265
266 (defun mew-decide-summary-form (folder)
267 (mew-folder-spec folder
268 mew-summary-form-list
269 mew-summary-form-list-string-type
270 mew-summary-form-list-list-type))
271
272 (defun mew-get-summary-form (folder)
273 "Get summary-form from 'mew-summary-form-list',
274 'mew-summary-form-list-string-type, and 'mew-summary-form-list-list-type'.
275 'mew-summary-form-header' is prepended. "
276 (let* ((form-col (mew-decide-summary-form folder))
277 (form (or (nth 0 form-col) mew-summary-form)))
278 (append mew-summary-form-header form)))
279
280 (defun mew-get-summary-column (folder)
281 (let ((form-col (mew-decide-summary-form folder)))
282 (or (nth 1 form-col)
283 (mew-thread-column (mew-get-summary-form folder))
284 mew-thread-column)))
285
286 (defun mew-thread-column (form)
287 (let ((col 0))
288 (catch 'loop
289 (dolist (ent form)
290 (cond
291 ((consp ent)
292 (setq col (+ col (abs (car ent)))))
293 ((stringp ent)
294 (setq col (+ col (string-width ent))))
295 ((eq ent t)
296 (throw 'loop col))
297 (t
298 (setq col (1+ col))))))))
299
300 (defun mew-get-unread-mark (folder)
301 (car (mew-folder-spec folder
302 mew-unread-mark-list
303 mew-unread-mark-list-string-type
304 mew-unread-mark-list-list-type)))
305
306 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
307 ;;;
308 ;;; The engine function to call mew-summary-form-*
309 ;;;
310
311 (defvar mew-inherit-total nil)
312 (defvar mew-inherit-width nil)
313
314 (defun mew-scan-get-piece (spec)
315 (let (func width str rightp nopad)
316 (if (stringp spec)
317 (progn
318 (setq mew-inherit-total (+ mew-inherit-total (string-width spec)))
319 spec)
320 (if (symbolp spec)
321 (setq width 1 func spec)
322 (mew-set '(width func) spec))
323 (when (and (integerp width) (symbolp func))
324 (when (= width 0)
325 (setq width (- mew-inherit-width mew-inherit-total 1))
326 (unless mew-use-spc-padding
327 (setq nopad t)))
328 (if (< width 0) (setq width (abs width) rightp t))
329 (setq mew-inherit-total (+ mew-inherit-total width))
330 (setq func (intern-soft
331 (concat mew-summary-form-func-prefix (symbol-name func))))
332 (when (fboundp func)
333 (setq str (funcall func))
334 (if rightp
335 (if (<= (string-width str) width)
336 (format (format "%%%ds" width) str)
337 (setq mew-inherit-total (+ (- mew-inherit-total width) (string-width str)))
338 str) ;; width may exceed.
339 (mew-substring str width nil nopad)))))))
340
341 (defun mew-sumsym-encode-folder (fld)
342 (mew-replace-character fld ? ?\t))
343
344 (defun mew-sumsym-decode-folder (fld)
345 (mew-replace-character fld ?\t ? ))
346
347 (defun mew-scan-get-line (mew-vec mew-inherit-width)
348 (let* ((mew-inherit-total 0) (fld "")
349 (line (mapconcat 'mew-scan-get-piece (mew-sinfo-get-summary-form) ""))
350 par-id my-id msg ld uid siz irt-list)
351 (setq my-id (or (mew-idstr-get-first-id (MEW-ID)) ""))
352 ;; RFC 2822 says: the "In-Reply-To:" field may be used to identify
353 ;; the message (or messages) to which the new message is a reply,
354 ;; while the "References:" field may be used to identify a
355 ;; "thread" of conversation.
356 ;;
357 ;; However, even if the References field exists, it may not contain
358 ;; a parent's ID. So, if the In-Reply-To field contain one ID,
359 ;; we use it for thread.
360 ;;
361 ;; (1) The In-Reply-To contains one ID, use it.
362 ;; (2) The References contains one or more IDs, use the last one.
363 ;; (3) The In-Reply-To contains two or more IDs, use the first one.
364 (setq par-id (or (mew-idstr-get-first-id (MEW-XREF)) ""))
365 (when (string= par-id "")
366 (setq irt-list (mew-idstr-to-id-list (MEW-IRT)))
367 (if (= (length irt-list) 1)
368 (setq par-id (car irt-list))
369 (setq par-id (or (mew-idstr-get-last-id (MEW-REF))
370 (car irt-list)
371 ""))))
372 (when (mew-virtual-p)
373 (setq fld (mew-sumsym-encode-folder
374 (or (cdr (assoc (mew-scan-get-folder mew-vec)
375 (mew-vinfo-get-lra)))
376 ;; Spotlight
377 (MEW-FLD))))) ;; xxx
378 (setq msg (mew-scan-get-message mew-vec))
379 (setq uid (or (mew-scan-uid-uid (MEW-UID)) ""))
380 (setq siz (or (mew-scan-uid-size (MEW-UID)) ""))
381 (setq ld (format "\r %s %s %s %s %s %s\n" fld msg my-id par-id uid siz))
382 (cons line ld)))
383
384 ;; See also mew-summary-cook-region
385 (defun mew-scan-insert-line (folder vec width lmsg &optional mark-or-dst)
386 (when (get-buffer folder)
387 (with-current-buffer folder
388 (let* ((line (mew-scan-get-line vec width))
389 (opos (point))
390 (omax (point-max))
391 beg med face olen nlen mark msg)
392 (mew-elet
393 (if (null lmsg)
394 (goto-char (point-max))
395 ;; a message marked with 'T'.
396 (when (mew-summary-search-msg lmsg)
397 (setq mark (mew-summary-get-mark))
398 (setq beg (point))
399 (forward-line)
400 ;; To avoid inserting a line AFTER the cursor underline,
401 ;; keep this line and make it invisible.
402 (put-text-property beg (point) 'invisible t)
403 (forward-line -1)))
404 (setq beg (point))
405 ;; To "insert" just after mew-marker-decode-syntax-end.
406 (insert (car line))
407 (setq med (point))
408 (insert (cdr line))
409 (goto-char beg)
410 (cond
411 ((stringp mark-or-dst) ;; xxx
412 (setq msg (mew-scan-get-message vec))
413 (mew-refile-reset msg)
414 (mew-refile-set msg (mew-split mark-or-dst ?,))
415 (mew-summary-refile-log folder mark-or-dst)
416 (setq med (+ med (mew-summary-refile-override-body mark-or-dst 'force)))
417 (mew-mark-put mew-mark-refile))
418 ((mew-characterp mark-or-dst) ;; mew-inbox-action-alist
419 (mew-mark-put mark-or-dst))
420 (mark
421 (mew-summary-mark-as mark))
422 ((and mew-use-highlight-mark ;; mew-summary-form-mark
423 (setq mark (mew-summary-get-mark)) ;; duplicated, etc
424 (setq face (mew-highlight-mark-get-face mark)))
425 (put-text-property beg med 'face face)))
426 (if mew-use-highlight-mouse-line
427 (put-text-property
428 beg med 'mouse-face mew-highlight-mouse-line-face))
429 (forward-line)
430 (put-text-property med (1- (point)) 'invisible t)
431 ;; Removing the invisible line.
432 (when lmsg
433 ;; UID information will be removed. So, we need to adjust
434 ;; the position.
435 (setq nlen (- (point) beg))
436 (setq beg (point))
437 (forward-line)
438 (when (> opos beg)
439 (setq olen (- (point) beg))
440 (setq opos (- opos (- olen nlen))))
441 (delete-region beg (point))))
442 (if (or (eq opos (mew-sinfo-get-start-point))
443 (/= opos omax))
444 ;; move the cursor to the original position.
445 (goto-char opos))))))
446
447 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
448 ;;;
449 ;;; Sub-functions for Scan
450 ;;;
451
452 (defun mew-scan-header (&optional draftp)
453 (let ((vec (make-vector (length mew-scan-fields) ""))
454 (lim (1- mew-scan-max-field-length))
455 i key med str n)
456 (goto-char (point-min))
457 (unless (re-search-forward mew-eoh nil t)
458 (goto-char (point-max)))
459 (save-restriction
460 (narrow-to-region (point-min) (point))
461 (goto-char (point-min))
462 (while (not (eobp))
463 (if (not (looking-at mew-keyval))
464 (forward-line)
465 (setq key (mew-capitalize (mew-match-string 1)))
466 (setq med (match-end 0))
467 ;; Three lines should be enough for Summary mode.
468 (forward-line)
469 (setq i 0)
470 (while (and (< i lim) (looking-at mew-lwsp))
471 (forward-line)
472 (setq i (1+ i)))
473 (when (and (setq n (mew-member-case-equal key mew-scan-fields))
474 (string= (aref vec n) "")) ;; avoid multiple times
475 (when (member key mew-scan-decode-fields)
476 (mew-header-decode-region key med (point) draftp))
477 ;; We need to keep composite properties of charset.
478 ;; This must be "buffer-substring".
479 (setq str (buffer-substring med (1- (point))))
480 (aset vec n str))
481 (mew-header-goto-next))))
482 vec))
483
484 (defun mew-scan-field-to-func (key)
485 (let ((n (mew-member-case-equal key mew-scan-fields))
486 (len (length mew-scan-fields-alias))
487 ali)
488 (if (and n (< n len)) (setq ali (nth n mew-scan-fields-alias)))
489 (if (stringp ali) (symbol-function (intern-soft (concat "MEW-" ali))))))
490
491 (defun mew-scan-inbox-action (mew-vec case)
492 (let ((alist (mew-inbox-action-alist case))
493 key val val-func ret mark-or-dst regex-list)
494 (catch 'loop
495 (dolist (ent alist)
496 (setq key (car ent))
497 (setq val-func (mew-scan-field-to-func key))
498 (if (and val-func (functionp val-func))
499 (setq val (funcall val-func))
500 (setq val nil))
501 (when val
502 (setq ent (cdr ent))
503 (dolist (act ent)
504 (if (symbolp act)
505 (when (fboundp act)
506 (setq ret (funcall act val))
507 (if ret (throw 'loop nil)))
508 (when (listp act)
509 (setq mark-or-dst (car act))
510 (setq regex-list (cdr act))
511 (dolist (rl regex-list)
512 (if (string-match rl val)
513 (throw 'loop (setq ret mark-or-dst))))))))))
514 ret))
515
516 (defvar mew-regex-ignore-scan-body-list
517 '("^[ \t]*$"
518 "^[ \t]*[-a-zA-Z0-9]+: "
519 "^[ \t]*[[>:|#;/_}]"
520 "^[ \t]*\\w+\\(['._-]+\\w+\\)*>"
521 "^[ \t]*[[</(.-]+ *\\(snip\\|\\.\\.\\)"
522 "^ "
523 "^--"
524 "^- --"
525 "^=2D"
526 "^.\\{1,100\\}\\(:\\|;\\|/\\)[ \t]*$"
527 "^.\\{1,100\\}\\(wrote\\|writes?\\|said\\|says?\\)[^.!\n]?[ \t]*$"
528 "^[ \t]*\\(On\\|At\\) .*[^.! \t\n][ \t]*$"
529 "^[ \t]*In \\(message\\|article\\|mail\\|news\\|<\\|\"\\|\\[\\|(\\)"))
530
531 (defun mew-scan-body (mew-vec &optional draftp)
532 (forward-line)
533 (let* ((i 0) (I mew-scan-max-body-length)
534 (j 0) (J mew-scan-body-length)
535 (ctr (MEW-CT))
536 (cte (MEW-CTE))
537 (body "")
538 (case-fold-search t)
539 textp charset cs beg end skip boundary found regex)
540 (catch 'break
541 (cond
542 (draftp
543 (setq textp t)
544 (setq cs mew-cs-m17n))
545 ((string= ctr "")
546 (if (and (not mew-scan-decode-bq-body) cte (mew-case-equal cte mew-b64))
547 (throw 'break nil))
548 (setq textp t)
549 (setq cs mew-cs-autoconv))
550 (t
551 ;; The following code is generic but too slow.
552 ;; (setq ctl (mew-param-decode ctr))
553 ;; (setq ct (mew-syntax-get-value ctl 'cap))
554 ;; So, this hard coding is used.
555 (while (and (string-match "^Multipart/" ctr)
556 (string-match "boundary=\"?\\([^\"\n\t;]+\\)\"?" ctr))
557 (setq boundary (mew-match-string 1 ctr))
558 (setq boundary (concat "^--" (regexp-quote boundary)))
559 (setq found nil)
560 (catch 'loop
561 (setq i 0)
562 (while (< i I)
563 (if (looking-at boundary) (throw 'loop (setq found t)))
564 (forward-line)
565 (setq i (1+ i))))
566 (if (not found)
567 (throw 'break nil)
568 (forward-line)
569 (save-restriction
570 (narrow-to-region (point) (point-max))
571 (setq ctr (mew-header-get-value mew-ct:))
572 (setq cte (mew-addrstr-parse-value
573 (mew-header-get-value mew-cte:)))
574 (mew-header-goto-end)) ;; should be in the narrowed region
575 (unless ctr ;; not ""
576 (setq textp t)
577 (setq cs mew-cs-autoconv)
578 (throw 'break nil))))
579 (if (and (not mew-scan-decode-bq-body) cte (mew-case-equal cte mew-b64))
580 (throw 'break nil))
581 (when (string-match "^Text/Plain" ctr)
582 (when (string-match "charset=\"?\\([^\"\n\t;]+\\)\"?" ctr)
583 (setq charset (mew-match-string 1 ctr)))
584 ;; xxx quoted-printable. not enough DB in mew-mule3.el.
585 (setq textp t)
586 (setq cs (mew-charset-to-cs charset))
587 (if (null cs) (setq cs mew-cs-autoconv)))))) ;; end of 'break
588 (when (and mew-scan-decode-bq-body cte
589 (or (mew-case-equal cte mew-b64) (mew-case-equal cte mew-qp)))
590 (setq beg (point))
591 (if (not found)
592 (setq end (point-max))
593 (while (and (not (eobp)) (not (looking-at boundary)))
594 (forward-line))
595 (setq end (point)))
596 (goto-char beg)
597 (condition-case nil
598 (cond
599 ((mew-case-equal cte mew-b64)
600 (save-restriction
601 (narrow-to-region beg end)
602 (base64-decode-region beg end)
603 (while (re-search-forward "\r+$" nil t)
604 (replace-match ""))
605 (if (re-search-forward "[^\n]\\'" nil t)
606 (insert "\n")))
607 (goto-char beg))
608 ((mew-case-equal cte mew-qp)
609 (quoted-printable-decode-region beg end mew-cs-binary)))
610 (error nil)))
611 (set-buffer-multibyte nil)
612 (when (and textp (mew-coding-system-p cs))
613 (setq i 0)
614 (while (and (not (eobp)) (< i I) (< j J))
615 (setq regex mew-regex-ignore-scan-body-list)
616 (setq skip nil)
617 (catch 'matched
618 (dolist (re regex)
619 (if (looking-at re)
620 (throw 'matched (setq skip t)))))
621 (if skip
622 (forward-line)
623 (when (looking-at "^[ \t]+")
624 (goto-char (match-end 0)))
625 (setq beg (point))
626 (forward-line)
627 (setq body (concat body (mew-buffer-substring beg (1- (point))) " "))
628 (setq j (1+ j)))
629 (setq i (1+ i)))
630 (set-buffer-multibyte t)
631 (setq body (mew-replace-white-space body))
632 (setq body (condition-case nil
633 (mew-cs-decode-string body cs)
634 (error body)))
635 (aset mew-vec (1- (length mew-vec)) body))))
636
637 (defun mew-scan-width ()
638 (if (and (integerp mew-summary-scan-width)
639 (> mew-summary-scan-width 40)) ;; xxx
640 mew-summary-scan-width
641 (max mew-window-magic (window-width))))
642
643 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
644 ;;;
645 ;;; X-Mew-Uidl:
646 ;;;
647
648 (defun mew-scan-uid-uid (uid)
649 (nth 0 (mew-split uid mew-sp)))
650
651 (defun mew-scan-uid-size (uid)
652 (nth 1 (mew-split uid mew-sp)))
653
654 (defun mew-scan-uid-case (uid)
655 (nth 2 (mew-split uid mew-sp)))
656
657 (defun mew-header-insert-xmu (uid siz truncated &optional case)
658 (when (and (stringp uid) (stringp siz))
659 (setq siz (number-to-string (string-to-number siz))) ;; removing 0
660 (let (fields)
661 (if (not truncated)
662 (setq fields (concat uid " " siz))
663 (setq fields (concat uid " 0" siz)) ;; e.g. 0500 == truncated
664 (if case (setq fields (concat fields " " case))))
665 (save-excursion
666 (mew-header-delete-lines (list mew-x-mew-uidl:)))
667 (mew-header-insert mew-x-mew-uidl: fields 'no-fold))))
668
669 (defun mew-header-insert-x-gm-labels (labels)
670 (save-excursion
671 (mew-header-delete-lines (list "X-GM-LABELS:"))
672 (mew-header-insert "X-GM-LABELS:" labels 'no-fold)))
673
674 (defun mew-header-insert-x-gm-msgid (msgid)
675 (save-excursion
676 (mew-header-delete-lines (list "X-GM-MSGID:"))
677 (mew-header-insert
678 "X-GM-MSGID:"
679 (format "%x" (string-to-number msgid))
680 'no-fold)))
681
682 (defun mew-header-insert-x-gm-thrid (thrid)
683 (save-excursion
684 (mew-header-delete-lines (list "X-GM-THRID:"))
685 (mew-header-insert
686 "X-GM-THRID:"
687 (format "%x" (string-to-number thrid))
688 'no-fold)))
689
690 (defun mew-scan-message-truncatedp ()
691 (mew-msg-truncatedp (mew-scan-uid-size (MEW-UID))))
692
693 (defun mew-scan-message-invalidp ()
694 (mew-msg-invalidp (MEW-NUM)))
695
696 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
697 ;;;
698 ;;; Scanning a folder
699 ;;;
700
701 (defun mew-summary-ls (&optional header-only goend update)
702 "List this folder asynchronously.
703
704 In a LOCAL folder: messages in the local folder are scanned according
705 to the range which you specify.
706
707 In a REMOTE folder: messages in the server's folder are cached
708 according to the range which you specify. If
709 'mew-pop-header-only'/'mew-imap-header-only'/'mew-nntp-header-only' is
710 non-nil, only headers of messages are cached. If executed with
711 '\\[universal-argument]', these variables are considered reversed."
712 (interactive "P")
713 (mew-summary-only
714 (when (mew-summary-exclusive-p)
715 (let* ((bnm (mew-summary-folder-name 'ext))
716 (case (mew-sinfo-get-case))
717 (fld (mew-sinfo-get-folder))
718 (askp mew-ask-range)
719 (directive 'scan)
720 (get-body (not header-only))
721 scanp range dir-newp)
722 (mew-summary-folder-cache-load)
723 (cond
724 (update
725 (setq askp nil)
726 (setq range nil) ;; update
727 (setq scanp t))
728 ((mew-called-interactively-p) ;; "s"
729 (setq askp t)
730 (setq scanp t))
731 ((mew-summary-folder-dir-newp) ;; "g"
732 (setq askp nil)
733 (setq scanp t)))
734 ;; for mew-summary-exchange-point.
735 (mew-sinfo-set-ret-pos (point))
736 (if (mew-summary-folder-dir-newp) (setq dir-newp t))
737 (if (or (mew-called-interactively-p) goend) (goto-char (point-max)))
738 (set-buffer-modified-p nil)
739 (if (not scanp)
740 (progn
741 (run-hooks 'mew-summary-ls-no-scan-hook)
742 t) ;; return value (not scanned)
743 (mew-summary-reset)
744 ;;
745 (mew-sinfo-set-direction 'down)
746 (cond
747 ((and (mew-folder-remotep fld)
748 (not (mew-folder-imap-queuep)))
749 (if (and dir-newp (mew-folder-imapp fld))
750 (mew-local-retrieve 'scan bnm (mew-range-update bnm))
751 (if askp (setq range (mew-input-range-remote bnm)))
752 (when (eq range 'sync)
753 (setq range nil)
754 (setq directive 'sync))
755 (cond
756 ((mew-folder-popp fld)
757 (if (mew-pop-header-only case)
758 (setq get-body (not get-body)))
759 (mew-pop-retrieve case directive bnm range get-body))
760 ((mew-folder-imapp fld)
761 (if (mew-imap-header-only case)
762 (setq get-body (not get-body)))
763 (mew-imap-retrieve case directive bnm range get-body))
764 ((mew-folder-nntpp fld)
765 (if (mew-nntp-header-only case)
766 (setq get-body (not get-body)))
767 (mew-nntp-retrieve case directive bnm range get-body)))))
768 (t ;; local
769 (setq range (mew-input-range bnm askp))
770 (if range
771 (mew-local-retrieve 'scan bnm (nth 0 range) (nth 1 range))
772 (message "range is wrong"))))
773 nil))))) ;; return value (scanned)
774
775 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
776 ;;;
777 ;;; Summary file cache
778 ;;;
779
780 (defun mew-compare-times (t1 t2)
781 ;; Is t1 newer than t2?
782 (cond
783 ((null t1) nil)
784 ((null t2) t) ;; do update
785 ((> (nth 0 t1) (nth 0 t2)) t)
786 ((= (nth 0 t1) (nth 0 t2))
787 (if (> (nth 1 t1) (nth 1 t2)) t nil)) ;; nil if equal
788 (t nil)))
789
790 (defun mew-summary-folder-dir-newp ()
791 (let* ((folder (mew-summary-folder-name 'ext))
792 (dir (file-chase-links (mew-expand-folder folder)))
793 (mfile (expand-file-name mew-summary-touch-file dir))
794 (t1 (mew-file-get-time mfile))
795 (cache (expand-file-name mew-summary-cache-file dir))
796 (t2 (mew-file-get-time cache)))
797 (if (and (null t1)
798 (file-directory-p dir)
799 (mew-dir-messages dir))
800 t
801 (mew-compare-times t1 t2))))
802
803 (defun mew-summary-folder-cache-newp ()
804 (let* ((folder (mew-summary-folder-name 'ext))
805 (cache (mew-expand-file folder mew-summary-cache-file))
806 (t1 (mew-file-get-time cache))
807 (t2 (mew-sinfo-get-cache-time)))
808 (mew-compare-times t1 t2)))
809
810 (defun mew-summary-set-count-line ()
811 (let* ((ttl-line (mew-count-lines (point-min) (point-max)))
812 (mid-point (/ (buffer-size) 2))
813 (mid-marker (mew-sinfo-get-mid-marker))
814 mid-line)
815 (save-excursion
816 (goto-char mid-point)
817 (beginning-of-line)
818 (if (and (mew-thread-p) mew-use-thread-separator
819 (looking-at mew-regex-thread-separator))
820 (forward-line))
821 (setq mid-point (point))
822 (setq mid-line (mew-count-lines (point-min) (point))))
823 (mew-sinfo-set-ttl-line ttl-line)
824 (mew-sinfo-set-mid-line mid-line)
825 (unless (markerp mid-marker)
826 (setq mid-marker (make-marker))
827 (mew-sinfo-set-mid-marker mid-marker))
828 (set-marker mid-marker mid-point)))
829
830 (defun mew-summary-folder-cache-load ()
831 (let* ((folder (mew-summary-folder-name 'ext))
832 (cache (mew-expand-file folder mew-summary-cache-file))
833 refile refs)
834 (when (and (file-readable-p cache)
835 (mew-summary-folder-cache-newp))
836 (mew-elet
837 (mew-erase-buffer)
838 (mew-frwlet mew-cs-m17n mew-cs-dummy
839 (mew-insert-file-contents cache))
840 (mew-sinfo-set-cache-time (mew-file-get-time cache))
841 (if (= (point-max) 1)
842 (setq mew-summary-buffer-raw nil)
843 (setq mew-summary-buffer-raw t))
844 (mew-sinfo-load)
845 (setq refs (mew-summary-mark-collect mew-mark-refile))
846 (setq refile (mew-summary-mark-recover
847 (mew-sinfo-get-mark-hist) (mew-sinfo-get-refile) refs))
848 (mew-sinfo-set-refile refile)
849 (mew-summary-set-count-line)
850 (set-buffer-modified-p nil)))))
851
852 (defun mew-summary-folder-cache-save ()
853 (let* ((folder (mew-summary-folder-name 'ext))
854 (cache (mew-expand-file folder mew-summary-cache-file))
855 (cbuf (current-buffer)))
856 (when (file-writable-p cache)
857 (mew-touch-folder folder)
858 (save-restriction
859 (widen)
860 (if (and (mew-decode-syntax-p)
861 (equal (mew-decode-syntax-buffer) cbuf))
862 (let ((min (point-min))
863 (max (point-max))
864 (beg (mew-decode-syntax-begin))
865 (end (mew-decode-syntax-end)))
866 (with-temp-buffer
867 (mew-insert-buffer-substring cbuf min beg)
868 (mew-insert-buffer-substring cbuf end max)
869 (mew-frwlet mew-cs-dummy mew-cs-m17n
870 (write-region (point-min) (point-max) cache nil 'no-msg))))
871 ;; (write-region 1 1 ...) does not update the file timestamp
872 ;; but does the directory timestamp. So, we need to delete
873 ;; the file to update the file timestamp.
874 (if (= (point-min) (point-max)) (mew-delete-file cache))
875 (mew-frwlet mew-cs-dummy mew-cs-m17n
876 (write-region (point-min) (point-max) cache nil 'no-msg))
877 (mew-set-file-modes cache))
878 (mew-summary-set-count-line)
879 (mew-sinfo-set-cache-time (mew-file-get-time cache))
880 (mew-sinfo-save)
881 (mew-sinfo-set-mark-hist nil)))))
882
883 ;; See also mew-net-folder-clean.
884 (defun mew-summary-folder-cache-clean (folder)
885 "Erase Summary mode then remove and touch the cache file."
886 (if (get-buffer folder)
887 (with-current-buffer folder
888 (mew-erase-buffer)
889 (set-buffer-modified-p nil)))
890 (let ((cfile (mew-expand-file folder mew-summary-cache-file)))
891 (if (file-exists-p cfile)
892 (write-region "" nil cfile nil 'no-msg))))
893
894 (provide 'mew-scan)
895
896 (defvar mew-compiling nil)
897 (eval-when-compile
898 (when mew-compiling
899 (require 'mew-varsx)
900 (mew-scan-setup)))
901
902 ;;; Copyright Notice:
903
904 ;; Copyright (C) 1996-2015 Mew developing team.
905 ;; All rights reserved.
906
907 ;; Redistribution and use in source and binary forms, with or without
908 ;; modification, are permitted provided that the following conditions
909 ;; are met:
910 ;;
911 ;; 1. Redistributions of source code must retain the above copyright
912 ;; notice, this list of conditions and the following disclaimer.
913 ;; 2. Redistributions in binary form must reproduce the above copyright
914 ;; notice, this list of conditions and the following disclaimer in the
915 ;; documentation and/or other materials provided with the distribution.
916 ;; 3. Neither the name of the team nor the names of its contributors
917 ;; may be used to endorse or promote products derived from this software
918 ;; without specific prior written permission.
919 ;;
920 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
921 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
922 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
923 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
924 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
925 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
926 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
927 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
928 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
929 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
930 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
931
932 ;;; mew-scan.el ends here