Debian Codegraph @

mew-header.el

raw
1 ;; mew-header.el --- Mail header stuff for Mew
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: Oct 2, 1996
5
6 ;;; Code:
7
8 (require 'mew)
9
10 (defvar mew-anonymous-recipients ":;")
11
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 ;;;
14 ;;; Header functions
15 ;;;
16
17 (defun mew-header-goto-next ()
18 (while (looking-at mew-lwsp) (forward-line)))
19
20 ;; see also mew-header-end
21 (defun mew-header-goto-end ()
22 (goto-char (point-min))
23 (if (re-search-forward mew-eoh nil t)
24 (beginning-of-line)
25 (goto-char (point-max))
26 (if (eolp) (insert "\n"))))
27
28 (defun mew-header-goto-body ()
29 (mew-header-goto-end)
30 (forward-line))
31
32 (defun mew-header-get-value (field &optional as-list)
33 "currently, when no match, it returns nil."
34 ;; maybe called in narrowed region.
35 ;; we cannot widen for citation.
36 (let ((case-fold-search t)
37 (regex (format "^%s[ \t]*" field))
38 start match ret)
39 (save-excursion
40 (mew-header-goto-end)
41 (save-restriction
42 (narrow-to-region (point-min) (point))
43 (goto-char (point-min))
44 (catch 'only-one
45 (while (re-search-forward regex nil t)
46 (setq start (match-end 0))
47 (forward-line)
48 (mew-header-goto-next)
49 (setq match (mew-buffer-substring start (1- (point))))
50 (unless (string= "" match)
51 (if ret
52 (if as-list
53 (setq ret (cons match ret))
54 (setq ret (concat ret "," match)))
55 (if as-list
56 (setq ret (list match))
57 (setq ret match))
58 (if (string= field mew-from:) (throw 'only-one nil))))))))
59 (if as-list
60 (nreverse ret)
61 ret)))
62
63 (defalias 'mew-header-existp 'mew-header-get-value)
64
65 (defun mew-header-replace-value (field value)
66 (let ((case-fold-search t)
67 (regex (format "^%s[ \t]*" field))
68 start)
69 (save-excursion
70 (mew-header-goto-end)
71 (save-restriction
72 (narrow-to-region (point-min) (point))
73 (goto-char (point-min))
74 ;; make value with evaluating
75 (if (and (listp value)
76 (stringp (car value)))
77 (setq value (cons 'format value)))
78 (setq value
79 (condition-case e
80 (eval value)
81 (error
82 (format "*** ERROR: %s: %s ***" (car e) (car (cdr e))))))
83 (unless (or (stringp value)
84 (null value))
85 (setq value (format "%s" value)))
86 ;; at this time, value is string or nil
87 (cond
88 ((re-search-forward regex nil t)
89 (setq start (match-beginning 0))
90 (forward-line)
91 (mew-header-goto-next)
92 (delete-region start (point))
93 (if value
94 (mew-draft-header-insert field value)))
95 (value ;; insert when value is exist
96 (mew-header-goto-end)
97 (mew-draft-header-insert field value)))))))
98
99 (defun mew-make-field-regex (fields)
100 (concat "^\\(" (mapconcat 'identity fields "\\|") "\\)"))
101
102 (defun mew-header-delete-lines (fields)
103 (when fields
104 (let ((case-fold-search t)
105 (regex (mew-make-field-regex fields))
106 start)
107 (mew-header-goto-end)
108 (save-restriction
109 (narrow-to-region (point-min) (point))
110 (goto-char (point-min))
111 (while (re-search-forward regex nil t)
112 (setq start (match-beginning 0))
113 (forward-line)
114 (mew-header-goto-next)
115 (delete-region start (point)))))))
116
117 (defun mew-header-delete-other-lines (fields)
118 (when fields
119 (let ((case-fold-search t)
120 (regex (mew-make-field-regex fields))
121 start)
122 (mew-header-goto-end)
123 (save-restriction
124 (narrow-to-region (point-min) (point))
125 (goto-char (point-min))
126 (while (not (eobp))
127 (if start (delete-region start (point)))
128 (if (looking-at regex)
129 (setq start nil)
130 (setq start (point)))
131 (forward-line)
132 (mew-header-goto-next))
133 (if start (delete-region start (point)))))))
134
135 (defun mew-header-replace-lines (fields prefix)
136 (when fields
137 (let ((case-fold-search t)
138 (regex (mew-make-field-regex fields)))
139 (mew-header-goto-end)
140 (save-restriction
141 (narrow-to-region (point-min) (point))
142 (goto-char (point-min))
143 (while (re-search-forward regex nil t)
144 (beginning-of-line)
145 (insert prefix)
146 (forward-line))))))
147
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 ;;;
150 ;;; Header sort
151 ;;;
152
153 (defun mew-header-sort (order)
154 (when order
155 (let* ((case-fold-search t)
156 (len (length order))
157 (vec (make-vector len nil))
158 key beg n line other)
159 (mew-header-goto-end)
160 (save-restriction
161 (narrow-to-region (point-min) (point))
162 (goto-char (point-min))
163 (while (not (eobp))
164 (if (not (looking-at mew-keyval))
165 (forward-line)
166 (setq key (mew-match-string 1))
167 (setq beg (match-beginning 0))
168 (forward-line)
169 (mew-header-goto-next)
170 (setq line (mew-buffer-substring beg (point)))
171 (delete-region beg (point))
172 (setq n (mew-member-case-equal key order))
173 (if n
174 (aset vec n (concat (aref vec n) line))
175 (setq other (concat other line)))))
176 (dotimes (i len)
177 (and (aref vec i) (insert (aref vec i))))
178 (and other (insert other))))))
179
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 ;;;
182 ;;; Header insertion
183 ;;;
184
185 (defun mew-draft-header-insert (field value)
186 "No encoding. Just insert."
187 (if (and value (stringp field)) (insert field " " value "\n")))
188
189 (defun mew-draft-header-fill (field value)
190 (unless (mew-header-existp field) (mew-draft-header-insert field value)))
191
192 (defun mew-header-insert (key value &optional no-fold)
193 (if (and value (stringp key))
194 (let ((beg (point)) params med parname parval)
195 (when (listp value)
196 (setq params (cdr value))
197 (setq value (car value)))
198 (insert key)
199 (insert " ")
200 (setq med (point))
201 (if (string-match "^[\t -~]*$" value)
202 (insert value)
203 (mew-header-encode-text value nil (length key)))
204 (dolist (par params)
205 (mew-set '(parname parval) par)
206 (insert ";")
207 (cond
208 ((string-match "^[-a-zA-Z0-9]+$" parval)
209 ) ;; do nothing
210 ((and (string= (mew-charset-guess-string parval) mew-us-ascii)
211 (not (string-match "\"" parval)))
212 (setq parval (concat "\"" parval "\"")))
213 (t
214 (setq parval (mew-param-encode parval))
215 (setq parname (concat parname "*"))))
216 (insert " " parname "=" parval))
217 (insert "\n")
218 (unless no-fold
219 (mew-header-fold-region beg (point) med)))))
220
221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
222 ;;;
223 ;;; Low-level functions to parse fields
224 ;;;
225
226 ;;
227 ;; The purpose of this function is to purse and extract value
228 ;; of address or content header. text is not target!
229 ;;
230 ;; ADDRP is t if STR are addresses.
231 ;; ADDRP is nil if STR is a value of content-*.
232 ;;
233 ;; 'Winnie (The) Pooh <"W. Pooh" (loves "honey") @ (somewhere in) England>'
234 ;; ->
235 ;; '"W. Pooh"@England'
236 ;;
237 ;; For 'destination', SEP is ",".
238 ;; For Content-*, SEP is ";".
239 ;;
240 ;; The special (i.e. SEP) is one of delimiters.
241 ;; 'linear-white-space' is another delimiter.
242 ;; 'quoted-string', 'comment', and 'domain-literal' is self-delimiting.
243 ;; But 'domain-literal' (e.g. [1.2.3.4]) is meaningless.
244 ;; So, the delimiters consists of SEP, SP, TAB, CRLF, "(", ")", and <">.
245 ;;
246 ;; 'qtext' remains. (It will be removed by mew-param-decode.)
247 ;; ## how about 'quoted-pair'?
248 ;;
249 ;; 'comment' must be symmetric. That is, it must have the exact number of
250 ;; ")" against that of ")". The entire 'comment', even if nested, is
251 ;; ignored.
252 ;;
253 ;; 'encoded-word' is meaningless.
254 ;; (1) Remove 'comment' so 'encoded-word' in 'comment' is meaningless.
255 ;; (2) 'phrase' of 'mailbox' will be ignored because 'addr-spec' in
256 ;; 'route-addr' will be extracted. So, 'encoded-word' in 'phrase' is
257 ;; meaningless.
258
259 (defmacro mew-addrstr-parse-syntax-list-check-depth (&optional depth)
260 `(progn
261 (setq sep-cnt (1+ sep-cnt))
262 (if (and ,depth (>= sep-cnt ,depth))
263 (throw 'max nil))
264 (when (and (integerp mew-header-max-depth)
265 (>= sep-cnt mew-header-max-depth))
266 (mew-warn "Too many values. Truncate values over mew-header-max-depth")
267 (throw 'max nil))))
268
269 (defun mew-addrstr-parse-syntax-list (str sep addrp &optional depth allow-spc)
270 (if str (mew-addrstr-parse-syntax-list1 str sep addrp depth allow-spc)))
271
272 (defun mew-addrstr-parse-syntax-list1 (str sep addrp depth allow-spc)
273 (let* ((i 0) (len (length str))
274 (par-cnt 0) (tmp-cnt 0) (sep-cnt 0)
275 ;; Emacs 23 has non-safe aset.
276 (tmp (mew-set-string-multibyte (mew-make-string len)))
277 c ret prevc
278 (do-clear
279 (lambda ()
280 (setq tmp-cnt 0)
281 (mew-addrstr-parse-syntax-list-check-depth depth)))
282 (do-cons-ret
283 (lambda ()
284 (setq ret (cons (substring tmp 0 tmp-cnt) ret))))
285 (do-copy
286 (lambda ()
287 (aset tmp tmp-cnt c)
288 (setq tmp-cnt (1+ tmp-cnt))))
289 (in-fold-quote
290 (lambda ()
291 (setq i (1+ i))
292 (catch 'fold-quote
293 (while (< i len)
294 (setq c (aref str i))
295 (cond
296 ;; ((or (char-equal c ?\t) (char-equal c mew-sp))
297 ;; (setq i (1+ i)))
298 ((char-equal c ?\")
299 (funcall do-copy)
300 (throw 'quote nil))
301 (t
302 (funcall do-copy)
303 (throw 'fold-quote nil)))))))
304 (in-quote
305 (lambda ()
306 (funcall do-copy)
307 (setq i (1+ i))
308 (catch 'quote
309 (while (< i len)
310 (setq prevc c)
311 (setq c (aref str i))
312 (cond
313 ((and (char-equal c ?\") (not (char-equal prevc ?\\)))
314 (funcall do-copy)
315 (throw 'quote nil))
316 ((char-equal c ?\n)
317 (funcall in-fold-quote))
318 (t
319 (funcall do-copy)))
320 (setq i (1+ i))))))
321 (in-comment
322 (lambda ()
323 (setq par-cnt 1)
324 (setq i (1+ i))
325 (catch 'comment
326 (while (< i len)
327 (setq c (aref str i))
328 (cond
329 ((char-equal c ?\()
330 (setq par-cnt (1+ par-cnt)))
331 ((char-equal c ?\))
332 (setq par-cnt (1- par-cnt))
333 (if (eq par-cnt 0) (throw 'comment nil))))
334 (setq i (1+ i))))))
335 (do-skip-until
336 (lambda (target)
337 (while (and (< i len) (not (char-equal (aref str i) target)))
338 (setq i (1+ i)))))
339 (in-addr
340 (lambda ()
341 (cond
342 (addrp
343 (let (rbeg rend)
344 (setq i (1+ i))
345 (setq rbeg i)
346 (funcall do-skip-until ?>)
347 (when (> i rbeg)
348 (setq rend i)
349 ;; note: to be used for substring, so not 1-.
350 ;; should not be nested but easy to implement...
351 (setq ret (cons (car (mew-addrstr-parse-syntax-list (substring str rbeg rend) sep t)) ret))))
352 (funcall do-skip-until sep)
353 (funcall do-clear))
354 (t
355 ;; just ignore
356 (funcall do-skip-until ?>)))))
357 (do-sep
358 (lambda ()
359 ;; broken quoted string cannot be rescued because
360 ;; the separator cannot be distinguished
361 (if (> tmp-cnt 0) (funcall do-cons-ret))
362 (funcall do-clear)))
363 (do-rescue
364 (lambda (var)
365 (save-match-data
366 (if (and addrp (string-match "<\\([^>]+\\)>" var))
367 (setq ret (cons (mew-match-string 1 var) ret))
368 (funcall do-cons-ret))))))
369 ;; main
370 (catch 'max
371 (while (< i len)
372 (setq c (aref str i))
373 (cond
374 ((char-equal c ?\")
375 (funcall in-quote))
376 ((char-equal c ?\()
377 (funcall in-comment))
378 ((char-equal c ?<)
379 (funcall in-addr))
380 ((or (char-equal c ?\n) (char-equal c ?\t)
381 (and (not allow-spc) (char-equal c mew-sp)))
382 ) ;; do nothing
383 ((char-equal c sep)
384 (funcall do-sep))
385 (t
386 (funcall do-copy)))
387 (setq i (1+ i)))
388 ;; broken quoted string can be rescued if it appears solely
389 (cond
390 ((> par-cnt 0)
391 (funcall do-rescue str))
392 ((> tmp-cnt 0)
393 (funcall do-rescue tmp))))
394 (setq ret (delete nil ret))
395 (when allow-spc
396 (setq ret (mapcar (lambda (str)
397 (if (string-match "^ +\\(.*\\)" str)
398 (mew-match-string 1 str)
399 str))
400 ret)))
401 (nreverse ret)))
402
403 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
404 ;;;
405 ;;; High-level functions to parse fields
406 ;;;
407
408 (defun mew-addrstr-parse-value (value)
409 (car (mew-addrstr-parse-syntax-list value ?\; nil 1)))
410
411 (defun mew-addrstr-parse-value-list (value)
412 (mew-addrstr-parse-syntax-list value ?\; nil))
413
414 (defun mew-addrstr-parse-value2 (value)
415 (car (mew-addrstr-parse-syntax-list value ?, nil 1)))
416
417 (defun mew-addrstr-parse-value-list2 (value)
418 (mew-addrstr-parse-syntax-list value ?, nil nil t))
419
420 ;;
421
422 (defun mew-addrstr-parse-address (address)
423 (car (mew-addrstr-parse-syntax-list address ?, t 1)))
424
425 (defun mew-addrstr-parse-address-list (address)
426 (mew-addrstr-parse-syntax-list address ?, t))
427
428 (defun mew-header-parse-address (field)
429 (car
430 (mew-addrstr-parse-syntax-list
431 (mapconcat 'mew-header-get-value (list field) ",")
432 ?, t 1)))
433
434 (defun mew-header-parse-address-list (field-list)
435 (mew-addrstr-parse-syntax-list
436 (mapconcat 'mew-header-get-value field-list ",")
437 ?, t))
438
439 (defun mew-header-parse-address-list2 (field-list)
440 "Collect addresses from FIELD-LIST.
441 Remove anonymous addresses."
442 (let ((vals (mew-addrstr-parse-syntax-list
443 (mapconcat 'mew-header-get-value field-list ",")
444 ?, t))
445 ret)
446 (dolist (val vals)
447 (unless (string-match mew-anonymous-recipients val)
448 (setq ret (cons val ret))))
449 (nreverse ret)))
450
451 ;;
452
453 (defun mew-addrstr-extract-user-list (addr-list)
454 (mapcar 'mew-addrstr-extract-user addr-list))
455
456 (defun mew-addrstr-extract-user (addr)
457 "Extracts username from ADDR"
458 (if (string-match "@.*:" addr) ;; xxx what's this?
459 (setq addr (substring addr (match-end 0)))
460 (setq addr (mew-replace-character addr mew-sp ?_))
461 (setq addr (substring addr 0 (string-match "%" addr)))
462 (setq addr (substring addr 0 (string-match "@" addr)))
463 ;; just for refile: "To: recipients:;" -> recipients
464 (setq addr (substring addr 0 (string-match mew-anonymous-recipients addr)))
465 ;; removing Notes domain
466 (setq addr (substring addr 0 (string-match "/" addr)))))
467
468 ;;
469
470 (defun mew-addrstr-append-domain (addr)
471 (if mew-addrbook-append-domain-p
472 (if (string-match "@" addr)
473 addr
474 (concat addr "@" (mew-mail-domain))) ;; xxx
475 addr))
476
477 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
478 ;;;
479 ;;; Functions for ID
480 ;;;
481
482 (defun mew-idstr-get-first-id (idstr)
483 (when (stringp idstr)
484 (if (string-match mew-regex-id idstr)
485 (mew-replace-white-space2 (match-string 0 idstr)))))
486
487 (defun mew-idstr-get-last-id (idstr)
488 (when (stringp idstr)
489 (let (beg end start)
490 (while (string-match mew-regex-id idstr start)
491 (setq beg (match-beginning 0))
492 (setq end (match-end 0))
493 (setq start (match-end 0)))
494 (if (and beg end)
495 (mew-replace-white-space2 (substring idstr beg end))))))
496
497 (defun mew-idstr-to-id-list (idstr &optional rev)
498 (when (stringp idstr)
499 (let (ret start)
500 (while (string-match mew-regex-id idstr start)
501 (setq start (match-end 0))
502 (setq ret (cons (mew-replace-white-space2 (match-string 0 idstr)) ret)))
503 (if rev
504 ret
505 (nreverse ret)))))
506
507 (defun mew-id-list-to-idstr (id-list)
508 (let (skip)
509 (if (integerp mew-references-max-count)
510 (setq skip (- (length id-list) mew-references-max-count)))
511 (if (and (integerp skip) (> skip 0))
512 (setq id-list (nthcdr skip id-list)))
513 (mew-join "\n\t" id-list)))
514
515 (provide 'mew-header)
516
517 ;;; Copyright Notice:
518
519 ;; Copyright (C) 1996-2015 Mew developing team.
520 ;; All rights reserved.
521
522 ;; Redistribution and use in source and binary forms, with or without
523 ;; modification, are permitted provided that the following conditions
524 ;; are met:
525 ;;
526 ;; 1. Redistributions of source code must retain the above copyright
527 ;; notice, this list of conditions and the following disclaimer.
528 ;; 2. Redistributions in binary form must reproduce the above copyright
529 ;; notice, this list of conditions and the following disclaimer in the
530 ;; documentation and/or other materials provided with the distribution.
531 ;; 3. Neither the name of the team nor the names of its contributors
532 ;; may be used to endorse or promote products derived from this software
533 ;; without specific prior written permission.
534 ;;
535 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
536 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
537 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
538 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
539 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
540 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
541 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
542 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
543 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
544 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
545 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
546
547 ;;; mew-header.el ends here