| 1 |
;;; mew-bq.el --- Base64 and Quoted-Printable encoding for Mew |
| 2 |
|
| 3 |
;; Author: Kazu Yamamoto <[email protected]> |
| 4 |
;; Created: Aug 20, 1997 |
| 5 |
|
| 6 |
;;; Code: |
| 7 |
|
| 8 |
(require 'mew) |
| 9 |
|
| 10 |
(defvar mew-header-encode-switch |
| 11 |
'(("B" . mew-base64-encode-string) |
| 12 |
("Q" . mew-q-encode-string))) |
| 13 |
|
| 14 |
(defun mew-header-encode-get-func (b-or-q) |
| 15 |
(cdr (mew-assoc-case-equal b-or-q mew-header-encode-switch 0))) |
| 16 |
|
| 17 |
|
| 18 |
(defvar mew-header-decode-switch |
| 19 |
`(("B" |
| 20 |
mew-base64-decode-string |
| 21 |
mew-base64-decode-error-string |
| 22 |
,mew-error-invalid-b-encoding) |
| 23 |
("Q" |
| 24 |
mew-q-decode-string |
| 25 |
nil |
| 26 |
,mew-error-invalid-q-encoding))) |
| 27 |
|
| 28 |
(defun mew-header-decode-get-func (b-or-q) |
| 29 |
(nth 1 (mew-assoc-case-equal b-or-q mew-header-decode-switch 0))) |
| 30 |
|
| 31 |
(defun mew-header-decode-get-error-func (b-or-q) |
| 32 |
(nth 2 (mew-assoc-case-equal b-or-q mew-header-decode-switch 0))) |
| 33 |
|
| 34 |
(defun mew-header-decode-get-error-str (b-or-q) |
| 35 |
(nth 3 (mew-assoc-case-equal b-or-q mew-header-decode-switch 0))) |
| 36 |
|
| 37 |
(defconst mew-header-decode-regex |
| 38 |
"=\\?\\([^? \t]+\\)\\?\\(.\\)\\?\\([^? \t]+\\)\\?=") |
| 39 |
|
| 40 |
(defconst mew-header-decode-regex2 |
| 41 |
"[ \t]*=\\?\\([^? \t]+\\)\\?\\(.\\)\\?\\([^? \t]+\\)\\?=") |
| 42 |
|
| 43 |
(defconst mew-header-decode-regex3 |
| 44 |
"^\\([ \t]+\\)=\\?[^? \t]+\\?.\\?[^? \t]+\\?=") |
| 45 |
|
| 46 |
(defconst mew-header-param-regex |
| 47 |
"^\\([^=*]+\\)\\(\\|\\*[0-9]+\\)\\(\\*?\\)=\\(.*\\)$") |
| 48 |
|
| 49 |
;;; |
| 50 |
;;; |
| 51 |
;;; |
| 52 |
|
| 53 |
(defun mew-header-sanity-check-string (str) |
| 54 |
(if (null str) |
| 55 |
str |
| 56 |
(let ((regex mew-regex-ctls)) |
| 57 |
(while (string-match regex str) |
| 58 |
(setq str (replace-match "" nil t str))) |
| 59 |
str))) |
| 60 |
|
| 61 |
(defun mew-header-sanity-check-string2 (str key) |
| 62 |
(if (null str) |
| 63 |
str |
| 64 |
(let ((regex mew-regex-ctls-wo-tab)) |
| 65 |
(if (not (string-match regex str)) |
| 66 |
str |
| 67 |
(mew-decode-warning-fields key 'ctl) |
| 68 |
(setq str (replace-match "" nil t str)) |
| 69 |
(while (string-match regex str) |
| 70 |
(setq str (replace-match "" nil t str))) |
| 71 |
str)))) |
| 72 |
|
| 73 |
(defun mew-header-sanity-check-region (beg end &optional key) |
| 74 |
(let ((regex mew-regex-ctls-wo-tab-lf)) |
| 75 |
;; meaningless tabs are removed by mew-header-decode-region |
| 76 |
(save-restriction |
| 77 |
(narrow-to-region beg end) |
| 78 |
(goto-char (point-min)) |
| 79 |
(when (re-search-forward regex nil t) |
| 80 |
(if key (mew-decode-warning-fields key 'ctl)) |
| 81 |
(replace-match "" nil t) |
| 82 |
(while (re-search-forward regex nil t) |
| 83 |
(replace-match "" nil t)))))) |
| 84 |
|
| 85 |
(defun mew-header-encode (charset b-or-q fun hcs str) |
| 86 |
;; sanity check should be done |
| 87 |
(let* ((estr (mew-cs-encode-string str hcs))) |
| 88 |
(concat "=?" charset "?" b-or-q "?" (funcall fun estr) "?="))) |
| 89 |
|
| 90 |
(defun mew-header-decode (charset b-or-q estr &optional key) |
| 91 |
(let* ((fun (mew-header-decode-get-func b-or-q)) |
| 92 |
(cs (mew-charset-to-cs charset)) |
| 93 |
str) |
| 94 |
(when (and mew-use-autoconv-when-unknown |
| 95 |
(not (mew-coding-system-p cs))) |
| 96 |
(setq cs mew-cs-autoconv)) |
| 97 |
(cond |
| 98 |
((not (mew-coding-system-p cs)) |
| 99 |
mew-error-unknown-charset) |
| 100 |
(fun ;; if cs is nil, mew-cs-decode-string does not cs-decode. |
| 101 |
(setq str (funcall fun estr)) |
| 102 |
(unless str (setq str (mew-header-decode-error key b-or-q estr))) |
| 103 |
(if (null str) |
| 104 |
(mew-header-decode-get-error-str b-or-q) |
| 105 |
(when (and mew-decode-broken |
| 106 |
(not (eq cs mew-cs-autoconv)) |
| 107 |
(or (null charset) |
| 108 |
(mew-case-equal charset mew-us-ascii) |
| 109 |
(mew-case-equal charset mew-utf-8)) |
| 110 |
(string-match mew-regex-singlebyte-nonascii str)) |
| 111 |
(setq cs mew-cs-autoconv)) |
| 112 |
(if (null cs) |
| 113 |
str |
| 114 |
(mew-cs-decode-string str cs)))) |
| 115 |
(t estr)))) |
| 116 |
|
| 117 |
(defun mew-header-decode-error (key b-or-q estr) |
| 118 |
(let ((func (mew-header-decode-get-error-func b-or-q))) |
| 119 |
(if func (funcall func key b-or-q estr)))) |
| 120 |
|
| 121 |
(defun mew-base64-decode-error-string (key _b-or-q estr) |
| 122 |
(let ((str estr) strlen r ret) |
| 123 |
(when (string-match "^\\([^=]*\\)=*$" str) |
| 124 |
(setq str (mew-match-string 1 str))) |
| 125 |
(setq strlen (length str)) |
| 126 |
(setq r (% strlen 4)) |
| 127 |
(cond |
| 128 |
((= r 0) |
| 129 |
(setq ret (mew-base64-decode-string str))) |
| 130 |
((= r 1) |
| 131 |
) |
| 132 |
((= r 2) |
| 133 |
(setq ret (mew-base64-decode-string (concat str "==")))) |
| 134 |
((= r 3) |
| 135 |
(setq ret (mew-base64-decode-string (concat str "="))))) |
| 136 |
(if ret (mew-decode-warning-fields key 'padding)) |
| 137 |
ret)) |
| 138 |
|
| 139 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 140 |
;;; |
| 141 |
;;; Quoted-printable encoding |
| 142 |
;;; |
| 143 |
|
| 144 |
(defun mew-q-encode-string (str &optional prefix) |
| 145 |
(let* ((len (length str)) |
| 146 |
(ret (mew-make-string (* len 3))) |
| 147 |
(j 0) char) |
| 148 |
(dotimes (i len) |
| 149 |
(setq char (aref str i)) |
| 150 |
(cond |
| 151 |
((char-equal char mew-sp) |
| 152 |
(aset ret j ?_)) |
| 153 |
((and (> char mew-sp) |
| 154 |
(< char 126) |
| 155 |
(not (char-equal char ?=)) |
| 156 |
(not (char-equal char ??)) |
| 157 |
(not (char-equal char ?_)) ;; space |
| 158 |
(or (not prefix) (not (memq char '(?# ?@))))) |
| 159 |
(aset ret j char)) |
| 160 |
(t |
| 161 |
(aset ret j (or prefix ?=)) |
| 162 |
(setq j (1+ j)) |
| 163 |
(aset ret j (aref "0123456789ABCDEF" (lsh char -4))) |
| 164 |
(setq j (1+ j)) |
| 165 |
(aset ret j (aref "0123456789ABCDEF" (logand char 15))))) |
| 166 |
(setq j (1+ j))) |
| 167 |
(substring ret 0 j))) |
| 168 |
|
| 169 |
(defun mew-samba-encoding (str) |
| 170 |
(setq str (mew-cs-encode-string str mew-cs-samba)) |
| 171 |
(let* ((len (length str)) |
| 172 |
(ret (mew-make-string (* len 3))) |
| 173 |
(j 0) char type) |
| 174 |
(cond |
| 175 |
((eq mew-use-samba-encoding-type 'cap) |
| 176 |
(setq type 'cap)) |
| 177 |
(t |
| 178 |
(setq type 'hex))) |
| 179 |
(dotimes (i len) |
| 180 |
(setq char (aref str i)) |
| 181 |
(if (and (eq type 'cap) (< char 128)) |
| 182 |
(aset ret j char) |
| 183 |
(aset ret j ?:) |
| 184 |
(setq j (1+ j)) |
| 185 |
(aset ret j (aref "0123456789abcdef" (lsh char -4))) |
| 186 |
(setq j (1+ j)) |
| 187 |
(aset ret j (aref "0123456789abcdef" (logand char 15)))) |
| 188 |
(setq j (1+ j))) |
| 189 |
(substring ret 0 j))) |
| 190 |
|
| 191 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 192 |
;;; |
| 193 |
;;; Quoted-printable decoding |
| 194 |
;;; |
| 195 |
|
| 196 |
(defun mew-hexchar-to-int (hex) |
| 197 |
;; would return nil if error |
| 198 |
(cond |
| 199 |
((and (<= ?0 hex) (<= hex ?9)) (- hex ?0)) |
| 200 |
((and (<= ?A hex) (<= hex ?F)) (+ (- hex ?A) 10)) |
| 201 |
((and (<= ?a hex) (<= hex ?f)) (+ (- hex ?a) 10)))) |
| 202 |
|
| 203 |
(defun mew-q-decode-string (qpstr &optional okey) |
| 204 |
(condition-case nil |
| 205 |
(let* ((len (length qpstr)) |
| 206 |
(ret (mew-make-string len)) |
| 207 |
(i 0) (j 0) char key) |
| 208 |
(setq key (or okey ?=)) |
| 209 |
(while (< i len) ;; cannot use non-lexbind dotimes since Emacs 29 does not support it |
| 210 |
(setq char (aref qpstr i)) |
| 211 |
(cond |
| 212 |
((and (not okey) (char-equal char ?_)) |
| 213 |
(aset ret j mew-sp)) |
| 214 |
((char-equal char key) |
| 215 |
(aset ret j (+ (* (mew-hexchar-to-int (aref qpstr (1+ i))) 16) |
| 216 |
(mew-hexchar-to-int (aref qpstr (+ i 2))))) |
| 217 |
(setq i (+ i 2))) |
| 218 |
(t |
| 219 |
(aset ret j char))) |
| 220 |
(setq j (1+ j)) |
| 221 |
(setq i (1+ i))) |
| 222 |
(substring ret 0 j)) |
| 223 |
(error nil))) |
| 224 |
|
| 225 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 226 |
;; |
| 227 |
;; RFC 2047 encoding |
| 228 |
;; |
| 229 |
|
| 230 |
;; RFC2822 says that each line SHOULD be no more than 78 characters, |
| 231 |
;; excluding the CRLF. |
| 232 |
;; RFC 2047 says that encoded-word must be less than or equal to 75. |
| 233 |
;; RFC 2047 says that each line which includes one or more encoded-words |
| 234 |
;; must be less than or equal to 76. |
| 235 |
|
| 236 |
(defvar mew-encode-word-max-length 75) |
| 237 |
(defvar mew-field-max-length 76) |
| 238 |
|
| 239 |
;; If possible, mew-header-encode-string should expect the length of |
| 240 |
;; results and split 'str' before encoding so that every 'encoded-word' |
| 241 |
;; fits in 75 length. However, it is very difficult first because |
| 242 |
;; it is very difficult to know actual length of 'str' after convention |
| 243 |
;; from the internal representation to charset encoding. Second because |
| 244 |
;; ISO-2022-JP cannot be simply split. If split, extra escape sequences |
| 245 |
;; appear. Moreover, it is not effective to expect the length of results |
| 246 |
;; because 'str' is short enough in most cases. So, we measure the length |
| 247 |
;; of results. If it is longer than 75, 'str' is split and 'substr1' and |
| 248 |
;; 'substr2' are encoded.... Repeat this recursively but not so deeply. |
| 249 |
|
| 250 |
(defun mew-header-encode-string (str &optional key-len) |
| 251 |
(let* ((ecsdb (or (mew-ecsdb-guess-string str) |
| 252 |
(mew-charset-to-ecsdb (mew-charset-m17n)))) |
| 253 |
(hcs (mew-ecsdb-hcs ecsdb)) |
| 254 |
charset b-or-q fun) |
| 255 |
(if (not (mew-coding-system-p hcs)) |
| 256 |
(mew-encode-error |
| 257 |
(format "Unknown coding system %s in the header" (symbol-name hcs))) |
| 258 |
(setq charset (mew-cs-to-charset hcs)) |
| 259 |
(setq b-or-q (mew-ecsdb-get-b-or-q ecsdb)) |
| 260 |
(setq fun (mew-header-encode-get-func b-or-q)) |
| 261 |
(if (null fun) |
| 262 |
(mew-encode-error |
| 263 |
(format "Unknown encoding function for %s" b-or-q)) |
| 264 |
(mew-header-encode-string1 charset b-or-q fun hcs str key-len))))) |
| 265 |
|
| 266 |
(defun mew-header-encode-string1 (charset b-or-q fun hcs str &optional key-len) |
| 267 |
(let* ((max mew-encode-word-max-length) |
| 268 |
(encoded-word (mew-header-encode charset b-or-q fun hcs str))) |
| 269 |
(if key-len |
| 270 |
(setq max (- max key-len))) |
| 271 |
(if (> (length encoded-word) max) |
| 272 |
(let ((med (/ (length str) 2))) |
| 273 |
(append |
| 274 |
(mew-header-encode-string1 |
| 275 |
charset b-or-q fun hcs (substring str 0 med) key-len) |
| 276 |
(mew-header-encode-string1 |
| 277 |
charset b-or-q fun hcs (substring str med)))) |
| 278 |
(list encoded-word)))) |
| 279 |
|
| 280 |
(defun mew-header-encode-split-string (str) |
| 281 |
"Split STR to need-to-encode string and non-encode-string." |
| 282 |
(let ((start 0) beg end ret) |
| 283 |
(while (string-match "\\(^\\|[ \t]+\\)[\t -~]+\\($\\|[ \t]+\\)" str start) |
| 284 |
(setq beg (match-beginning 0)) |
| 285 |
(setq end (match-end 0)) |
| 286 |
(if (= start beg) |
| 287 |
(setq ret (cons (substring str beg end) ret)) |
| 288 |
(setq ret (cons (substring str beg end) |
| 289 |
(cons (substring str start beg) ret)))) |
| 290 |
(setq start end)) |
| 291 |
(if (/= start (length str)) |
| 292 |
(setq ret (cons (substring str start) ret))) |
| 293 |
(nreverse ret))) |
| 294 |
|
| 295 |
(defun mew-header-encode-comma-text (str) |
| 296 |
(let ((str-list (mapcar 'mew-chop (mew-split str ?,)))) |
| 297 |
(mew-header-encode-text (car str-list)) |
| 298 |
(setq str-list (cdr str-list)) |
| 299 |
(dolist (sl str-list) |
| 300 |
(insert ", ") ;; must be fold here |
| 301 |
(mew-header-encode-text sl)))) |
| 302 |
|
| 303 |
(defmacro mew-header-encode-cond (c) |
| 304 |
`(cond |
| 305 |
((> ,c 127) ;; non-ascii |
| 306 |
(when (eq status 'space) |
| 307 |
(insert (substring str bound i)) |
| 308 |
(setq bound i)) |
| 309 |
(setq status 'non-ascii)) |
| 310 |
;; end of non-ascii |
| 311 |
(t ;; ascii |
| 312 |
(cond |
| 313 |
((eq status 'space) |
| 314 |
(insert (substring str bound i)) ;; spaces |
| 315 |
(setq bound i) |
| 316 |
(setq status 'ascii)) |
| 317 |
((eq status 'ascii) |
| 318 |
(setq status 'ascii)) |
| 319 |
((eq status 'non-ascii) |
| 320 |
(setq status 'non-ascii)) |
| 321 |
((eq status 'non-ascii-space) |
| 322 |
(mew-header-encode-text (substring str bound SBOUND)) |
| 323 |
;; non-ascii |
| 324 |
(insert (substring str SBOUND i)) ;; spaces |
| 325 |
(setq bound i) |
| 326 |
(setq status 'ascii)))) |
| 327 |
;; end of ascii |
| 328 |
)) |
| 329 |
|
| 330 |
(defmacro mew-header-encode-cond2 (opt) |
| 331 |
`(cond |
| 332 |
((eq status 'ascii) |
| 333 |
(insert (substring str bound i))) |
| 334 |
((eq status 'space) |
| 335 |
(insert (substring str bound i))) |
| 336 |
((eq status 'non-ascii) |
| 337 |
(mew-header-encode-text (substring str bound i)) ,opt) |
| 338 |
((eq status 'non-ascii-space) |
| 339 |
(mew-header-encode-text (substring str bound SBOUND) ,opt) |
| 340 |
(insert (substring str SBOUND i))))) |
| 341 |
|
| 342 |
(defun mew-header-encode-addr (str) |
| 343 |
(let* ((len (length str)) |
| 344 |
(i 0) (bound 0) (status 'space) |
| 345 |
SBOUND open c I) |
| 346 |
;; status space, ascii, non-ascii, non-ascii-space |
| 347 |
;; assumptions: |
| 348 |
;; <> does not contain non-ascii characters. |
| 349 |
;; () does not recurse. |
| 350 |
;; if " " contains non-ascii, cause an error. |
| 351 |
(while (< i len) |
| 352 |
(setq c (aref str i)) |
| 353 |
(cond |
| 354 |
;; quote |
| 355 |
((char-equal c ?\") |
| 356 |
(setq I (1+ i)) |
| 357 |
(setq open t) |
| 358 |
(catch 'quote |
| 359 |
(while (< I len) |
| 360 |
(setq c (aref str I)) |
| 361 |
(cond |
| 362 |
((char-equal c ?\") |
| 363 |
(setq open nil) |
| 364 |
(throw 'quote nil)) |
| 365 |
((> c 127) |
| 366 |
(mew-encode-error |
| 367 |
"Only ASCII is allowed in quoted-string in the header"))) |
| 368 |
(setq I (1+ I)))) |
| 369 |
(if open |
| 370 |
(mew-encode-error "Quote string must be closed in the header")) |
| 371 |
(mew-header-encode-cond ?a) |
| 372 |
(setq i I)) |
| 373 |
;; end of quote |
| 374 |
;; comment |
| 375 |
((char-equal c ?\() |
| 376 |
(mew-header-encode-cond2 nil) |
| 377 |
(insert "(") |
| 378 |
(setq i (1+ i)) |
| 379 |
(setq bound i) |
| 380 |
(setq status 'ascii) |
| 381 |
(setq open t) |
| 382 |
(let (qp) |
| 383 |
(catch 'comment |
| 384 |
(while (< i len) |
| 385 |
(setq c (aref str i)) |
| 386 |
(cond |
| 387 |
((char-equal c ?\)) |
| 388 |
(setq open nil) |
| 389 |
(throw 'comment nil)) |
| 390 |
((char-equal c ?\") |
| 391 |
(setq qp t)) |
| 392 |
((> c 127) |
| 393 |
(setq status 'non-ascii))) |
| 394 |
(setq i (1+ i)))) |
| 395 |
(if (and qp (eq status 'non-ascii)) |
| 396 |
(mew-encode-error |
| 397 |
"Only ASCII is allowed in quoted-string in the header"))) |
| 398 |
(if open |
| 399 |
(mew-encode-error "Comment must be closed in the header")) |
| 400 |
(mew-header-encode-cond2 'comment) |
| 401 |
(if (= i len) |
| 402 |
() |
| 403 |
(insert ")") |
| 404 |
(setq bound (1+ i))) |
| 405 |
(setq status 'space)) |
| 406 |
;; end of () |
| 407 |
;; route |
| 408 |
((char-equal c ?<) |
| 409 |
(mew-header-encode-cond2 nil) |
| 410 |
(if (or (char-equal (char-before (point)) mew-sp) |
| 411 |
(char-equal (char-before (point)) ?\t)) |
| 412 |
(insert "<") |
| 413 |
(insert " <")) |
| 414 |
(setq i (1+ i)) |
| 415 |
(setq bound i) |
| 416 |
(setq status 'ascii) |
| 417 |
(setq open t) |
| 418 |
(catch 'route |
| 419 |
(while (< i len) |
| 420 |
(setq c (aref str i)) |
| 421 |
(cond |
| 422 |
((char-equal c ?>) |
| 423 |
(setq open nil) |
| 424 |
(throw 'route nil)) |
| 425 |
((> c 127) |
| 426 |
(mew-encode-error "<> must contain ASCII only")) |
| 427 |
(t |
| 428 |
(insert c))) |
| 429 |
(setq i (1+ i)))) |
| 430 |
(if open |
| 431 |
(mew-encode-error "<> must be closed in the header")) |
| 432 |
(if (= i len) |
| 433 |
() |
| 434 |
(insert ">") |
| 435 |
(setq bound (1+ i))) |
| 436 |
(setq status 'space)) |
| 437 |
((char-equal c ?>) |
| 438 |
(mew-encode-error "Unbalanced <> in the header")) |
| 439 |
;; end of <> |
| 440 |
|
| 441 |
;; group |
| 442 |
;; 'mailbox' between ":" and ";" should be 'addr-spec', |
| 443 |
;; not be 'phrase route-addr' in Mew because they are to be |
| 444 |
;; removed. |
| 445 |
((char-equal c ?:) |
| 446 |
(mew-header-encode-cond2 nil) |
| 447 |
;; RFC 2047 says: |
| 448 |
;; An 'encoded-word' that appears within a |
| 449 |
;; 'phrase' MUST be separated from any adjacent 'word', 'text' or |
| 450 |
;; 'special' by 'linear-white-space'. |
| 451 |
;; However, we do not care if an 'encoded-word' is followed by |
| 452 |
;; ":". The spec is believed too strict. 'encoded-word' can be |
| 453 |
;; recognized because of the token rule. |
| 454 |
(insert ":") |
| 455 |
(setq i (1+ i)) |
| 456 |
(unless (char-equal (aref str i) ?\;) |
| 457 |
(mew-encode-error ": must be followed by ; in the header")) |
| 458 |
(insert ";") |
| 459 |
(setq bound (1+ i)) |
| 460 |
(setq status 'space)) |
| 461 |
;; end of group |
| 462 |
|
| 463 |
;; space |
| 464 |
((or (char-equal c mew-sp) (char-equal c ?\t)) |
| 465 |
(cond |
| 466 |
((or (eq status 'ascii) (eq status 'space)) |
| 467 |
(insert (substring str bound i)) ;; 'ascii |
| 468 |
(setq bound i) |
| 469 |
(setq status 'space)) |
| 470 |
((eq status 'non-ascii) |
| 471 |
(setq status 'non-ascii-space) |
| 472 |
(setq SBOUND i)))) |
| 473 |
;; end of white space |
| 474 |
;; comma |
| 475 |
((char-equal c ?,) |
| 476 |
(mew-header-encode-cond2 nil) |
| 477 |
(insert ", ") |
| 478 |
(setq i (1+ i)) |
| 479 |
(catch 'comma |
| 480 |
(while (< i len) |
| 481 |
(setq c (aref str i)) |
| 482 |
(if (or (char-equal c mew-sp) (char-equal c ?\t) (char-equal c ?\n)) |
| 483 |
() ;; loop |
| 484 |
(throw 'comma nil)) |
| 485 |
(setq i (1+ i)))) |
| 486 |
;; get back to the end of white spaces |
| 487 |
(setq bound i) |
| 488 |
(setq c mew-sp) |
| 489 |
(setq i (1- i)) |
| 490 |
(setq status 'space)) |
| 491 |
;; end of comma |
| 492 |
;; the others |
| 493 |
(t (mew-header-encode-cond c))) |
| 494 |
;; end of outside cond |
| 495 |
(setq i (1+ i))) |
| 496 |
;; end of while |
| 497 |
(mew-header-encode-cond2 nil))) |
| 498 |
|
| 499 |
(defun mew-header-encode-text (str &optional _comment key-len) |
| 500 |
;; 'comment' means that we are in RFC822 comment "(...)". |
| 501 |
(let ((str-list (mew-header-encode-split-string str)) |
| 502 |
head-is-e e-list) |
| 503 |
(if (string-match "^[\t -~]+$" (car str-list)) |
| 504 |
(progn |
| 505 |
;; ascii |
| 506 |
(insert (car str-list)) |
| 507 |
(setq str-list (cdr str-list))) |
| 508 |
(setq head-is-e t)) |
| 509 |
(while str-list |
| 510 |
;; encoded-words |
| 511 |
(if (and key-len head-is-e) |
| 512 |
(progn |
| 513 |
(setq e-list (mew-header-encode-string (car str-list) key-len)) |
| 514 |
(setq head-is-e nil)) |
| 515 |
(setq e-list (mew-header-encode-string (car str-list)))) |
| 516 |
(insert (mapconcat 'identity e-list " ")) |
| 517 |
;; ascii |
| 518 |
(setq str-list (cdr str-list)) |
| 519 |
(when (car str-list) |
| 520 |
(insert (car str-list)) |
| 521 |
(setq str-list (cdr str-list)))))) |
| 522 |
|
| 523 |
(defun mew-header-fold-region (beg end med &optional use-tab) |
| 524 |
(let ((limit1 med) limit2) |
| 525 |
(save-restriction |
| 526 |
(narrow-to-region beg end) |
| 527 |
(goto-char beg) |
| 528 |
(while (not (eobp)) |
| 529 |
(while (> (- (setq limit2 (save-excursion (end-of-line) (point))) |
| 530 |
(point)) |
| 531 |
mew-field-max-length) |
| 532 |
(forward-char mew-field-max-length) |
| 533 |
(if (re-search-backward "[ \t]" limit1 t) |
| 534 |
(progn |
| 535 |
(insert "\n") |
| 536 |
(if use-tab |
| 537 |
(progn |
| 538 |
(delete-char 1) |
| 539 |
(insert "\t")))) |
| 540 |
;; Ugh! |
| 541 |
(if (re-search-forward "[ \t]" limit2 t) ;; hold on anyway |
| 542 |
(progn |
| 543 |
(backward-char) |
| 544 |
(insert "\n") |
| 545 |
(if use-tab |
| 546 |
(progn |
| 547 |
(delete-char 1) |
| 548 |
(insert "\t")))) |
| 549 |
(forward-line))) ;; give up this line |
| 550 |
(setq limit1 (1+ (point)))) |
| 551 |
(forward-line) |
| 552 |
(setq limit1 (1+ (point))))))) |
| 553 |
|
| 554 |
(defun mew-header-resent-p (field) |
| 555 |
(let ((case-fold-search t)) |
| 556 |
(string-match mew-resent-regex field))) |
| 557 |
|
| 558 |
(defun mew-header-encode-region (beg end &optional resentp) |
| 559 |
(let (key med type str start last fast-path) |
| 560 |
(save-restriction |
| 561 |
(narrow-to-region beg end) |
| 562 |
(mew-header-sanity-check-region (point-min) (point-max)) |
| 563 |
(mew-charset-sanity-check (point-min) (point-max)) |
| 564 |
(goto-char (point-min)) |
| 565 |
(while (not (eobp)) |
| 566 |
(setq start (point)) |
| 567 |
(if (not (looking-at mew-keyval)) |
| 568 |
(forward-line) |
| 569 |
(setq key (mew-match-string 1)) |
| 570 |
(setq med (match-end 0)) |
| 571 |
(setq type (mew-field-type-for-encoding key)) |
| 572 |
(setq fast-path t) |
| 573 |
(if (eq type 'mailbox) |
| 574 |
(if resentp |
| 575 |
(if (mew-header-resent-p key) |
| 576 |
(setq fast-path nil)) |
| 577 |
(if (not (mew-header-resent-p key)) |
| 578 |
(setq fast-path nil)))) |
| 579 |
(forward-line) |
| 580 |
(mew-header-goto-next) |
| 581 |
(setq last (1- (point))) |
| 582 |
(unless (= last med) |
| 583 |
(if (and (string= (mew-charset-guess-region med last) mew-us-ascii) |
| 584 |
fast-path) |
| 585 |
() ;; fast path |
| 586 |
;; if us-ascii AND mailbox, need to check syntax |
| 587 |
(setq str (mew-buffer-substring med (1- (point)))) |
| 588 |
;; excluding \n |
| 589 |
(delete-region med (point)) |
| 590 |
(cond |
| 591 |
((eq type 'mailbox) |
| 592 |
(mew-header-encode-addr str)) |
| 593 |
((eq type 'mime) |
| 594 |
(mew-header-encode-addr str)) |
| 595 |
((eq type 'comma-text) |
| 596 |
(mew-header-encode-comma-text str)) |
| 597 |
((eq type 'text) |
| 598 |
(mew-header-encode-text str nil (length key))) |
| 599 |
((eq type 'unstruct) |
| 600 |
(mew-header-encode-text str nil (length key)))) |
| 601 |
(insert "\n")) ;; previously deleted, so insert here |
| 602 |
(mew-header-fold-region start (point) med))))))) |
| 603 |
|
| 604 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 605 |
;; |
| 606 |
;; RFC 2047 decoding |
| 607 |
;; |
| 608 |
|
| 609 |
(defun mew-header-unfold-region (type) |
| 610 |
(goto-char (point-min)) |
| 611 |
(cond |
| 612 |
((eq type 'struct) |
| 613 |
;; If each line does not end with ",", unfold it. |
| 614 |
;; In Page 5 of RFC822 says, "Unfolding is accomplished by |
| 615 |
;; regarding CRLF immediately followed by a LWSP-char as |
| 616 |
;; equivalent to the LWSP-char". However, it also says, |
| 617 |
;; "In structured field bodies, multiple linear space ASCII |
| 618 |
;; characters (namely HTABs and SPACEs) are treated as single |
| 619 |
;; spaces and may freely surround any symbol." So, remove |
| 620 |
;; continuous white spaces. |
| 621 |
(when (and mew-decode-broken (looking-at "\n[ \t]+")) |
| 622 |
(replace-match "" nil t)) |
| 623 |
(goto-char (point-min)) |
| 624 |
(while (re-search-forward ",[ \t]+\n" nil t) |
| 625 |
(replace-match ",\n" nil t)) |
| 626 |
(goto-char (point-min)) |
| 627 |
(while (re-search-forward "\\([^,]\\)[ \t]*\n[ \t]+" nil t) |
| 628 |
(replace-match "\\1 "))) |
| 629 |
((eq type 'struct2) ;; for Authentication-Results: |
| 630 |
(while (re-search-forward "[ \t]*\n[ \t]+" nil t) (replace-match " ")) |
| 631 |
(goto-char (point-min)) |
| 632 |
(while (search-forward ";" nil t) |
| 633 |
(insert "\n") |
| 634 |
(if (looking-at "[\t ]+") |
| 635 |
(replace-match "\t") |
| 636 |
(insert "\t")))) |
| 637 |
((eq type 'text) |
| 638 |
;; In Page 5 of RFC822 says, "Unfolding is accomplished by |
| 639 |
;; regarding CRLF immediately followed by a LWSP-char as |
| 640 |
;; equivalent to the LWSP-char". |
| 641 |
(while (re-search-forward "\n\\([ \t]\\)" nil t) |
| 642 |
(replace-match "\\1"))) |
| 643 |
(t ;; unstruct |
| 644 |
))) |
| 645 |
|
| 646 |
(defun mew-header-decode-string (key) |
| 647 |
(let ((beg (match-beginning 0)) |
| 648 |
(end (match-end 0)) |
| 649 |
(css (mew-header-decode (mew-match-string 1) |
| 650 |
(mew-match-string 2) |
| 651 |
(mew-match-string 3) |
| 652 |
key))) |
| 653 |
(setq css (mew-header-sanity-check-string2 css key)) |
| 654 |
(delete-region beg end) |
| 655 |
(insert css))) |
| 656 |
|
| 657 |
(defun mew-header-decode-in-quotedp () |
| 658 |
(save-excursion |
| 659 |
(beginning-of-line) |
| 660 |
(char-equal (following-char) ?\"))) |
| 661 |
|
| 662 |
(defun mew-header-decode-in-addr-spec () |
| 663 |
(eq (get-text-property (match-beginning 0) 'mew-addr-spec) t)) |
| 664 |
|
| 665 |
(defun mew-decode-warning-body (err msg) |
| 666 |
(mew-xinfo-set-warning |
| 667 |
(cons |
| 668 |
(cond |
| 669 |
((eq err 'unknown-charset) |
| 670 |
(format "Charset (%s) for body is not supported.\n" msg)) |
| 671 |
((eq err 'no-charset) |
| 672 |
(if msg |
| 673 |
(format "Charset (%s) for body is mis-matched.\n" msg) |
| 674 |
"Charset for body is not specified.\n")) |
| 675 |
((eq err 'multi) |
| 676 |
(format "Invalid encoding (%s) for multipart.\n" msg))) |
| 677 |
(mew-xinfo-get-warning)))) |
| 678 |
|
| 679 |
(defun mew-decode-warning-fields (key err) |
| 680 |
(unless (member key mew-no-warning-fields) |
| 681 |
(let (wmsg level) |
| 682 |
(cond |
| 683 |
((eq err 'spc) |
| 684 |
(setq level 1) |
| 685 |
(setq wmsg (concat "tab/spc characters on " key " are simplified.\n"))) |
| 686 |
((eq err 'ctl) |
| 687 |
(setq level 1) |
| 688 |
(setq wmsg (concat "some control code on " key " are removed.\n"))) |
| 689 |
((eq err 'raw) |
| 690 |
(setq level 2) |
| 691 |
(setq wmsg (concat key " has raw text strings.\n"))) |
| 692 |
((eq err 'padding) |
| 693 |
(setq level 2) |
| 694 |
(setq wmsg (concat key " has illegal padding in encoded text.\n"))) |
| 695 |
((eq err 'quoted) |
| 696 |
(setq level 2) |
| 697 |
(setq wmsg (concat key " has encoded-words in quoted text.\n")))) |
| 698 |
(if (>= level mew-warning-field-level) |
| 699 |
(mew-xinfo-set-warning (cons wmsg (mew-xinfo-get-warning))))))) |
| 700 |
|
| 701 |
(defun mew-decode-warning-params (param err) |
| 702 |
(unless (and param (member (downcase param) mew-no-warning-params)) |
| 703 |
(mew-xinfo-set-warning |
| 704 |
(cons |
| 705 |
(cond |
| 706 |
((eq err 'raw) |
| 707 |
(concat "The '" param "' parameter has raw text.\n")) |
| 708 |
((eq err 'ctl) |
| 709 |
(concat "The '" param "' has invalid control codes.\n")) |
| 710 |
((eq err 'enc) |
| 711 |
(concat "The '" param "' parameter has encoded-word.\n")) |
| 712 |
((eq err 'char) |
| 713 |
(concat "The '" param "' has invalid characters.\n"))) |
| 714 |
(mew-xinfo-get-warning))))) |
| 715 |
|
| 716 |
(defun mew-header-decode-region (key rbeg rend &optional draftp) |
| 717 |
"RFC 2047 decoding. This is liberal on the one point from RFC 2047. |
| 718 |
That is, each line may be more than 75." |
| 719 |
(setq key (capitalize key)) |
| 720 |
(let* ((type (mew-field-type-for-decoding key)) (nl "\0")) |
| 721 |
(save-restriction |
| 722 |
(narrow-to-region rbeg rend) |
| 723 |
;; Handling invalid raw text. |
| 724 |
(goto-char (point-min)) |
| 725 |
(if draftp |
| 726 |
(mew-cs-decode-region (point-min) (point-max) mew-cs-m17n) |
| 727 |
(when (and mew-decode-broken |
| 728 |
(re-search-forward mew-regex-esc-or-nonascii nil t)) |
| 729 |
(mew-decode-warning-fields key 'raw) |
| 730 |
(mew-cs-decode-region (point-min) (point-max) mew-cs-autoconv))) |
| 731 |
;; |
| 732 |
(mew-header-unfold-region type) |
| 733 |
;; |
| 734 |
(goto-char (point-min)) |
| 735 |
(when (re-search-forward mew-header-decode-regex nil t) |
| 736 |
;; In Page 10 of RFC 2047 says, "When displaying a particular |
| 737 |
;; header field that contains multiple 'encoded-word's, any |
| 738 |
;; 'linear-white-space' that separates a pair of adjacent |
| 739 |
;; 'encoded-word's is ignored". So, use looking-at. |
| 740 |
(cond |
| 741 |
((memq type '(text comma-text)) |
| 742 |
;; This is text field. We do not care quote! |
| 743 |
;; make use of the search above |
| 744 |
(mew-header-decode-string key) |
| 745 |
(while (looking-at mew-header-decode-regex2) |
| 746 |
(mew-header-decode-string key)) |
| 747 |
;; |
| 748 |
(while (re-search-forward mew-header-decode-regex nil t) |
| 749 |
(mew-header-decode-string key) |
| 750 |
(while (looking-at mew-header-decode-regex2) |
| 751 |
(mew-header-decode-string key)))) |
| 752 |
(t |
| 753 |
;; This is structured field. |
| 754 |
;; encoded-word in quoted-string should not be decoded |
| 755 |
;; according to RFC 2047. However, if users wish |
| 756 |
;; (ie mew-decode-broken is *non-nil*), decode it. |
| 757 |
;; Regular expression cannot express a quoted string in |
| 758 |
;; general case. So, canonicalize as follows: |
| 759 |
;; Not quoted line (may be null line) |
| 760 |
;; "quoted line" |
| 761 |
;; Not quoted line |
| 762 |
;; "quoted line" |
| 763 |
;; ... |
| 764 |
;; For this purpose, "\n" is converted to ^\@(\0). |
| 765 |
(goto-char (point-min)) |
| 766 |
(while (re-search-forward "\n" nil t) |
| 767 |
(replace-match nl nil t)) |
| 768 |
;; |
| 769 |
(goto-char (point-min)) |
| 770 |
(while (re-search-forward mew-regex-id nil t) |
| 771 |
(put-text-property (match-beginning 0) (match-end 0) |
| 772 |
'mew-addr-spec t)) |
| 773 |
;; |
| 774 |
(goto-char (point-min)) |
| 775 |
;; Perfect regular expression for quoted strings. |
| 776 |
;; See "Mastering Regular Expressions", O'REILLY |
| 777 |
(while (re-search-forward "\"\\([^\"\\\\]\\|\\\\.\\)*\"" nil t) |
| 778 |
(goto-char (match-beginning 0)) |
| 779 |
(insert "\n") |
| 780 |
(goto-char (1+ (match-end 0))) |
| 781 |
(insert "\n")) |
| 782 |
;; |
| 783 |
(goto-char (point-min)) ;; anyway |
| 784 |
;; |
| 785 |
(while (re-search-forward mew-header-decode-regex nil t) |
| 786 |
(cond |
| 787 |
((mew-header-decode-in-addr-spec) |
| 788 |
) ;; do nothing |
| 789 |
((mew-header-decode-in-quotedp) |
| 790 |
(when mew-decode-broken |
| 791 |
(mew-decode-warning-fields key 'quoted) |
| 792 |
(mew-header-decode-string key) |
| 793 |
(while (looking-at mew-header-decode-regex2) |
| 794 |
(mew-header-decode-string key)))) |
| 795 |
(t |
| 796 |
(mew-header-decode-string key) |
| 797 |
(while (looking-at mew-header-decode-regex2) |
| 798 |
(mew-header-decode-string key))))) |
| 799 |
;; Getting back to the original |
| 800 |
(goto-char (point-min)) |
| 801 |
(while (re-search-forward "\n" nil t) |
| 802 |
(replace-match "" nil t)) |
| 803 |
(goto-char (point-min)) |
| 804 |
(while (re-search-forward nl nil t) |
| 805 |
(replace-match "\n" nil t)) |
| 806 |
(remove-text-properties (point-min) (point-max) '(mew-addr-spec t))))) |
| 807 |
;; |
| 808 |
(if (and mew-decode-broken (member key mew-decode-ws-fields)) |
| 809 |
(let (ws) |
| 810 |
;; We want to warn only in the case of ^\t. |
| 811 |
(goto-char (point-min)) |
| 812 |
;; Because lines are already unfolded, we cannot tell |
| 813 |
;; whether or not this TABs locate on the beginning |
| 814 |
;; of a line. But this should be acceptable. |
| 815 |
(while (re-search-forward "\t+" nil t) |
| 816 |
(or ws (setq ws t)) |
| 817 |
(replace-match " " nil t)) |
| 818 |
(goto-char (point-min)) |
| 819 |
;; Continuous SPCs are not invalid, just for appearance. |
| 820 |
(while (re-search-forward " +" nil t) |
| 821 |
(or ws (setq ws t)) |
| 822 |
(replace-match " " nil t)) |
| 823 |
(if ws (mew-decode-warning-fields key 'spc)))) |
| 824 |
;; |
| 825 |
(mew-header-sanity-check-region (point-min) (point-max) key) |
| 826 |
(goto-char (point-max))))) |
| 827 |
|
| 828 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 829 |
;; |
| 830 |
;; RFC 2231 |
| 831 |
;; |
| 832 |
|
| 833 |
(defun mew-param-encode (str) |
| 834 |
(let* ((ecsdb (or (mew-ecsdb-guess-string str) |
| 835 |
(mew-charset-to-ecsdb (mew-charset-m17n)))) |
| 836 |
(hcs (mew-ecsdb-hcs ecsdb)) |
| 837 |
(charset (mew-cs-to-charset hcs)) |
| 838 |
(estr (mew-cs-encode-string str hcs)) |
| 839 |
(len (length estr)) |
| 840 |
(ret (mew-make-string (* len 3))) |
| 841 |
(j 0) char) |
| 842 |
(dotimes (i len) |
| 843 |
(setq char (aref estr i)) |
| 844 |
(if (or (and (<= ?0 char) (<= char ?9)) |
| 845 |
(and (<= ?a char) (<= char ?z)) |
| 846 |
(and (<= ?A char) (<= char ?Z))) |
| 847 |
(aset ret j char) |
| 848 |
(aset ret j ?%) |
| 849 |
(setq j (1+ j)) |
| 850 |
(aset ret j (aref "0123456789ABCDEF" (lsh char -4))) |
| 851 |
(setq j (1+ j)) |
| 852 |
(aset ret j (aref "0123456789ABCDEF" (logand char 15)))) |
| 853 |
(setq j (1+ j))) |
| 854 |
(concat charset "''" (substring ret 0 j)))) |
| 855 |
|
| 856 |
(defun mew-param-decode (whole-value) |
| 857 |
(let* ((value-params (mew-addrstr-parse-value-list whole-value)) |
| 858 |
(value (car value-params)) |
| 859 |
(params (cdr value-params)) |
| 860 |
(max 0) num |
| 861 |
ret ext ext-sort entry charset cs |
| 862 |
paramname-value paramname paramvalue) |
| 863 |
(dolist (param params) |
| 864 |
(setq paramname-value (mew-param-analyze param)) |
| 865 |
(when paramname-value |
| 866 |
(setcar paramname-value (downcase (nth 0 paramname-value))) |
| 867 |
(if (= (length paramname-value) 2) |
| 868 |
(setq ret (cons paramname-value ret)) |
| 869 |
(setq ext (cons paramname-value ext)) |
| 870 |
(setq num (nth 2 paramname-value)) |
| 871 |
(if (> num max) (setq max num))))) |
| 872 |
(if (null ext) |
| 873 |
(cons value (nreverse ret)) ;; fast return |
| 874 |
(setq max (1+ max)) |
| 875 |
(dolist (ea ext) |
| 876 |
(mew-set '(paramname paramvalue num) ea) |
| 877 |
(if (setq entry (assoc paramname ext-sort)) |
| 878 |
(progn |
| 879 |
(aset (nth 1 entry) num paramvalue) |
| 880 |
(when (= num 0) |
| 881 |
(setcar (nthcdr 2 entry) (nth 3 ea)) ;; charset |
| 882 |
(setcar (nthcdr 3 entry) (nth 4 ea)))) ;; lang |
| 883 |
(setq entry (make-vector max nil)) |
| 884 |
(aset entry num paramvalue) |
| 885 |
(setq ext-sort |
| 886 |
(cons (list paramname entry (nth 3 ea) (nth 4 ea)) |
| 887 |
ext-sort)))) |
| 888 |
(dolist (ea ext-sort) |
| 889 |
(setq paramvalue nil) |
| 890 |
(mew-set '(paramname entry charset) ea) |
| 891 |
(setq num 0) |
| 892 |
(catch 'concat-loop |
| 893 |
(while (< num max) |
| 894 |
(if (aref entry num) |
| 895 |
(setq paramvalue (concat paramvalue (aref entry num))) ;; xxx |
| 896 |
(throw 'concat-loop nil)) |
| 897 |
(setq num (1+ num)))) |
| 898 |
(when charset |
| 899 |
(setq cs (mew-charset-to-cs charset)) |
| 900 |
(when (and mew-use-autoconv-when-unknown |
| 901 |
(not (mew-coding-system-p cs))) |
| 902 |
(setq cs mew-cs-autoconv)) |
| 903 |
(when (and mew-decode-broken |
| 904 |
(not (eq cs mew-cs-autoconv)) |
| 905 |
(or (null charset) |
| 906 |
(mew-case-equal charset mew-us-ascii) |
| 907 |
(mew-case-equal charset mew-utf-8)) |
| 908 |
(string-match mew-regex-singlebyte-nonascii paramvalue)) |
| 909 |
(setq cs mew-cs-autoconv)) |
| 910 |
(if (mew-coding-system-p cs) |
| 911 |
(setq paramvalue (mew-cs-decode-string paramvalue cs)) |
| 912 |
(setq paramvalue mew-error-unknown-charset))) |
| 913 |
(setq ret (cons (list paramname paramvalue) ret))) |
| 914 |
(setq ret (mapcar 'mew-param-sanity-check ret)) |
| 915 |
(cons value (nreverse ret))))) ;; late return |
| 916 |
|
| 917 |
(defun mew-param-analyze (param) |
| 918 |
"Return (paramname paramvalue) or |
| 919 |
(paramname paramvalue section charset lang)." |
| 920 |
(let (name section asterisk value charset lang) |
| 921 |
(when (string-match mew-header-param-regex param) |
| 922 |
(setq name (match-string 1 param)) |
| 923 |
(setq section (match-string 2 param)) ;; *21 |
| 924 |
(setq asterisk (string= (match-string 3 param) "*")) |
| 925 |
(setq value (match-string 4 param)) |
| 926 |
(if (string= section "") |
| 927 |
(if asterisk |
| 928 |
(setq section 0) |
| 929 |
(setq section nil)) |
| 930 |
(setq section (string-to-number (substring section 1)))) |
| 931 |
(if (null asterisk) |
| 932 |
;; delete quote |
| 933 |
(progn |
| 934 |
(if (and (< 1 (length value)) |
| 935 |
(char-equal (aref value 0) 34) |
| 936 |
(char-equal (aref value (1- (length value))) 34)) |
| 937 |
(setq value (substring value 1 -1))) |
| 938 |
(if mew-decode-broken |
| 939 |
(setq value (mew-param-analyze-broken value name)))) |
| 940 |
(when (and (= section 0) |
| 941 |
;; broken UAs quote extended-value, sigh |
| 942 |
(string-match "^\"?\\([^']*\\)'\\([^']*\\)'\\([^\"]*\\)\"?$" value)) |
| 943 |
(setq charset (match-string 1 value)) |
| 944 |
(setq lang (match-string 2 value)) |
| 945 |
(if (string= lang "") (setq lang nil)) |
| 946 |
(setq value (match-string 3 value)) |
| 947 |
(if (string= value "") (setq value nil))) |
| 948 |
;; broken UAs quote extended-value, sigh |
| 949 |
(if (string-match "^\"\\([^\"]*\\)\"$" value) |
| 950 |
(setq value (mew-match-string 1 value))) |
| 951 |
(setq value (mew-q-decode-string value ?%))) |
| 952 |
(if section |
| 953 |
(list name value section charset lang) |
| 954 |
(list name value))))) |
| 955 |
|
| 956 |
(defun mew-param-analyze-broken (value name) |
| 957 |
;; broken message handling |
| 958 |
(when (and (mew-member-case-equal name mew-broken-parameter-list) |
| 959 |
(string-match mew-regex-esc-or-nonascii value)) |
| 960 |
(mew-decode-warning-params name 'raw) |
| 961 |
(setq value (mew-cs-decode-string value mew-cs-autoconv))) |
| 962 |
(when (string-match mew-header-decode-regex value) |
| 963 |
(let (pre med pst) |
| 964 |
(mew-decode-warning-params name 'enc) |
| 965 |
(while (string-match mew-header-decode-regex value) |
| 966 |
;; We cannot ensure that sub-function does not use match functions. |
| 967 |
;; So, pre/pst prevents infinite loop. |
| 968 |
(setq pre (substring value 0 (match-beginning 0))) |
| 969 |
(setq pst (substring value (match-end 0))) |
| 970 |
(setq med (mew-header-decode (match-string 1 value) |
| 971 |
(match-string 2 value) |
| 972 |
(match-string 3 value))) |
| 973 |
(if (string-match mew-header-decode-regex3 pst) |
| 974 |
(setq pst (substring pst (match-end 1)))) |
| 975 |
(setq value (concat pre med pst))))) |
| 976 |
value) |
| 977 |
|
| 978 |
(defun mew-param-sanity-check (ent) |
| 979 |
(let (param value new) |
| 980 |
(mew-set '(param value) ent) |
| 981 |
(setq new (mew-header-sanity-check-string value)) |
| 982 |
(unless (string= value new) (mew-decode-warning-params param 'ctl)) |
| 983 |
(list param new))) |
| 984 |
|
| 985 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 986 |
;;; |
| 987 |
;;; Punycode:: RFC 3492 |
| 988 |
;;; |
| 989 |
|
| 990 |
(defconst mew-puny-base 36) |
| 991 |
(defconst mew-puny-tmin 1) |
| 992 |
(defconst mew-puny-tmax 26) |
| 993 |
(defconst mew-puny-damp 700) |
| 994 |
(defconst mew-puny-skew 38) |
| 995 |
(defconst mew-puny-initial-bias 72) |
| 996 |
(defconst mew-puny-initial-n 128) |
| 997 |
(defconst mew-puny-delimiter ?-) |
| 998 |
|
| 999 |
(defun mew-puny-adapt (delta numpoints firsttime) |
| 1000 |
(let ((k 0)) |
| 1001 |
(if firsttime |
| 1002 |
(setq delta (/ delta mew-puny-damp)) |
| 1003 |
(setq delta (/ delta 2))) |
| 1004 |
(setq delta (+ delta (/ delta numpoints))) |
| 1005 |
(while (> delta (/ (* (- mew-puny-base mew-puny-tmin) mew-puny-tmax) 2)) |
| 1006 |
(setq delta (/ delta (- mew-puny-base mew-puny-tmin))) |
| 1007 |
(setq k (+ k mew-puny-base))) |
| 1008 |
(+ k (/ (* (1+ (- mew-puny-base mew-puny-tmin)) delta) (+ delta mew-puny-skew))))) |
| 1009 |
|
| 1010 |
(defun mew-puny-decode-digit (cp) |
| 1011 |
(if (< (- cp 48) 10) |
| 1012 |
(- cp 22) |
| 1013 |
(if (< (- cp 65) 26) |
| 1014 |
(- cp 65) |
| 1015 |
(if (< (- cp 97) 26) |
| 1016 |
(- cp 97) |
| 1017 |
mew-puny-base)))) |
| 1018 |
|
| 1019 |
(defun mew-puny-encode-digit (d) |
| 1020 |
(if (< d 26) |
| 1021 |
(+ d 22 75) ;; a-z |
| 1022 |
(+ d 22))) ;; 0-9 |
| 1023 |
|
| 1024 |
(defun mew-puny-decode (input) |
| 1025 |
(when mew-cs-utf-16be |
| 1026 |
(condition-case nil |
| 1027 |
(mew-cs-decode-string |
| 1028 |
(mew-puny-decode1 (substring input 4)) ;; xn-- |
| 1029 |
mew-cs-utf-16be) |
| 1030 |
(error nil)))) |
| 1031 |
|
| 1032 |
(defun mew-puny-decode1 (input) |
| 1033 |
(let* ((n mew-puny-initial-n) |
| 1034 |
(bias mew-puny-initial-bias) |
| 1035 |
(len (length input)) |
| 1036 |
(in 0) (out 0) |
| 1037 |
(i 0) (b 0) |
| 1038 |
digit thr oldi w k output ret) |
| 1039 |
(dotimes (j len) |
| 1040 |
(if (= (aref input j) mew-puny-delimiter) (setq b j))) |
| 1041 |
(dotimes (j b) |
| 1042 |
(setq output (cons (aref input j) output)) |
| 1043 |
(setq out (1+ out))) |
| 1044 |
(setq output (nreverse output)) |
| 1045 |
(if (> b 0) (setq in (1+ b)) (setq in 0)) |
| 1046 |
(while (< in len) |
| 1047 |
(setq oldi i) |
| 1048 |
(setq w 1) |
| 1049 |
(setq k mew-puny-base) |
| 1050 |
(catch 'loop |
| 1051 |
(while t |
| 1052 |
(if (>= in len) (error "punycode bad input")) |
| 1053 |
(setq digit (mew-puny-decode-digit (aref input in))) |
| 1054 |
(if (>= digit mew-puny-base) (error "punycode bad input")) |
| 1055 |
(setq in (1+ in)) |
| 1056 |
(setq i (+ i (* digit w))) |
| 1057 |
(if (<= k bias) |
| 1058 |
(setq thr mew-puny-tmin) |
| 1059 |
(if (>= k (+ bias mew-puny-tmax)) |
| 1060 |
(setq thr mew-puny-tmax) |
| 1061 |
(setq thr (- k bias)))) |
| 1062 |
(if (< digit thr) (throw 'loop nil)) |
| 1063 |
(setq w (* w (- mew-puny-base thr))) |
| 1064 |
(setq k (+ k mew-puny-base)))) |
| 1065 |
(setq out (1+ out)) |
| 1066 |
(setq bias (mew-puny-adapt (- i oldi) out (= oldi 0))) |
| 1067 |
(setq n (+ n (/ i out))) |
| 1068 |
(setq i (% i out)) |
| 1069 |
(if (= i 0) |
| 1070 |
(setq output (cons n (nthcdr i output))) |
| 1071 |
(setcdr (nthcdr (1- i) output) (cons n (nthcdr i output)))) |
| 1072 |
(setq i (1+ i))) |
| 1073 |
(setq ret (mew-make-string (* out 2))) |
| 1074 |
(let ((j 0)) |
| 1075 |
(dolist (op output) |
| 1076 |
(aset ret j (/ op 256)) |
| 1077 |
(setq j (1+ j)) |
| 1078 |
(aset ret j (% op 256)) |
| 1079 |
(setq j (1+ j)))) |
| 1080 |
ret)) |
| 1081 |
|
| 1082 |
(defun mew-puny-encode (input) |
| 1083 |
(when mew-cs-utf-16be |
| 1084 |
(condition-case nil |
| 1085 |
(concat "xn--" (mew-puny-encode1 (mew-cs-encode-string input mew-cs-utf-16be))) |
| 1086 |
(error nil)))) |
| 1087 |
|
| 1088 |
(defun mew-puny-encode1 (input) |
| 1089 |
(let* ((len (length input)) |
| 1090 |
(h-len (/ len 2)) |
| 1091 |
(n mew-puny-initial-n) |
| 1092 |
(bias mew-puny-initial-bias) |
| 1093 |
(delta 0) (out 0) |
| 1094 |
(output (mew-make-string (* len 4))) |
| 1095 |
j h b m q k thr uni) |
| 1096 |
(setq j 0) |
| 1097 |
(while (< j len) ;; cannot use non-lexbind dotimes since Emacs 29 does not support it |
| 1098 |
(setq uni (aref input j)) |
| 1099 |
(setq j (1+ j)) |
| 1100 |
(setq uni (+ (* uni 256) (aref input j))) |
| 1101 |
(when (< uni 128) ;; basic |
| 1102 |
(aset output out uni) |
| 1103 |
(setq out (1+ out))) |
| 1104 |
(setq j (1+ j))) |
| 1105 |
(setq h out) |
| 1106 |
(setq b out) |
| 1107 |
(when (> b 0) |
| 1108 |
(aset output out mew-puny-delimiter) |
| 1109 |
(setq out (1+ out))) |
| 1110 |
(while (< h h-len) |
| 1111 |
(setq m 65536) ;; 17bits |
| 1112 |
(setq j 0) |
| 1113 |
(while (< j len) ;; cannot use non-lexbind dotimes since Emacs 29 does not support it |
| 1114 |
(setq uni (aref input j)) |
| 1115 |
(setq j (1+ j)) |
| 1116 |
(setq uni (+ (* uni 256) (aref input j))) |
| 1117 |
(if (and (>= uni n) (< uni m)) (setq m uni)) |
| 1118 |
(setq j (1+ j))) |
| 1119 |
(setq delta (+ delta (* (- m n) (1+ h)))) |
| 1120 |
(setq n m) |
| 1121 |
(setq j 0) |
| 1122 |
(while (< j len) ;; cannot use non-lexbind dotimes since Emacs 29 does not support it |
| 1123 |
(setq uni (aref input j)) |
| 1124 |
(setq j (1+ j)) |
| 1125 |
(setq uni (+ (* uni 256) (aref input j))) |
| 1126 |
(when (< uni n) |
| 1127 |
(setq delta (1+ delta)) |
| 1128 |
(if (= delta 0) (error "punycode overflow"))) |
| 1129 |
(when (= uni n) |
| 1130 |
(setq q delta) |
| 1131 |
(setq k mew-puny-base) |
| 1132 |
(catch 'loop |
| 1133 |
(while t |
| 1134 |
(if (<= k bias) |
| 1135 |
(setq thr mew-puny-tmin) |
| 1136 |
(if (>= k (+ bias mew-puny-tmax)) |
| 1137 |
(setq thr mew-puny-tmax) |
| 1138 |
(setq thr (- k bias)))) |
| 1139 |
(if (< q thr) (throw 'loop nil)) |
| 1140 |
(aset output out (mew-puny-encode-digit (+ thr (% (- q thr) (- mew-puny-base thr))))) |
| 1141 |
(setq out (1+ out)) |
| 1142 |
(setq q (/ (- q thr) (- mew-puny-base thr))) |
| 1143 |
(setq k (+ k mew-puny-base)))) |
| 1144 |
(aset output out (mew-puny-encode-digit q)) |
| 1145 |
(setq out (1+ out)) |
| 1146 |
(setq bias (mew-puny-adapt delta (1+ h) (= h b))) |
| 1147 |
(setq delta 0) |
| 1148 |
(setq h (1+ h))) |
| 1149 |
(setq j (1+ j))) |
| 1150 |
(setq delta (1+ delta)) |
| 1151 |
(setq n (1+ n))) |
| 1152 |
(substring output 0 out))) |
| 1153 |
|
| 1154 |
(defun mew-puny-encode-url (url) |
| 1155 |
(let (beg end idn) |
| 1156 |
(with-temp-buffer |
| 1157 |
(insert url) |
| 1158 |
(goto-char (point-min)) |
| 1159 |
(if (search-forward "://" nil t) |
| 1160 |
(setq beg (point)) |
| 1161 |
(setq beg (point-min))) |
| 1162 |
(if (search-forward "/" nil t) |
| 1163 |
(setq end (1- (point))) |
| 1164 |
(setq end (point-max))) |
| 1165 |
(save-restriction |
| 1166 |
(narrow-to-region beg end) |
| 1167 |
(goto-char (point-min)) |
| 1168 |
(while (re-search-forward "[^.]?[^.\000-\177][^.]*" nil t) |
| 1169 |
(setq idn (mew-match-string 0)) |
| 1170 |
(delete-region (match-beginning 0) (match-end 0)) |
| 1171 |
(insert (mew-puny-encode idn)))) |
| 1172 |
(mew-buffer-substring (point-min) (point-max))))) |
| 1173 |
|
| 1174 |
(provide 'mew-bq) |
| 1175 |
|
| 1176 |
;;; Copyright Notice: |
| 1177 |
|
| 1178 |
;; Copyright (C) 1997-2015 Mew developing team. |
| 1179 |
;; All rights reserved. |
| 1180 |
|
| 1181 |
;; Redistribution and use in source and binary forms, with or without |
| 1182 |
;; modification, are permitted provided that the following conditions |
| 1183 |
;; are met: |
| 1184 |
;; |
| 1185 |
;; 1. Redistributions of source code must retain the above copyright |
| 1186 |
;; notice, this list of conditions and the following disclaimer. |
| 1187 |
;; 2. Redistributions in binary form must reproduce the above copyright |
| 1188 |
;; notice, this list of conditions and the following disclaimer in the |
| 1189 |
;; documentation and/or other materials provided with the distribution. |
| 1190 |
;; 3. Neither the name of the team nor the names of its contributors |
| 1191 |
;; may be used to endorse or promote products derived from this software |
| 1192 |
;; without specific prior written permission. |
| 1193 |
;; |
| 1194 |
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND |
| 1195 |
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 1196 |
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 1197 |
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE |
| 1198 |
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 1199 |
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 1200 |
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 1201 |
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 1202 |
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 1203 |
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 1204 |
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 1205 |
|
| 1206 |
;;; mew-bq.el ends here |