-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpjb-mail.el
919 lines (824 loc) · 31.6 KB
/
pjb-mail.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
;;;; -*- mode:emacs-lisp;coding:utf-8 -*-
;;;;*****************************************************************************
;;;;FILE: pjb-mail.el
;;;;LANGUAGE: emacs lisp
;;;;SYSTEM: emacs
;;;;USER-INTERFACE: emacs
;;;;DESCRIPTION
;;;;
;;;; This module exports various functions related to mail handling.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal J. Bourguignon
;;;;MODIFICATIONS
;;;; 2002-10-01 <PJB> Created.
;;;;
;;;;BUGS
;;;;LEGAL
;;;; LGPL
;;;;
;;;; Copyright Pascal J. Bourguignon 2002 - 2011
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 2 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
;;;;
;;;;******************************************************************************
(require 'pjb-cl)
(require 'pjb-list)
(require 'pjb-strings)
(require 'pjb-emacs)
(require 'pjb-pgp)
(require 'mailheader)
(require 'mm nil t)
(require 'sendmail)
(defun slist-get (slist prop &rest options)
"
DO: Extract a value from a property list.
PLIST: is a property list, which is a list of the form
(PROP1 VALUE1 PROP2 VALUE2...).
RETURN: the value corresponding to the given PROP, or nil if PROP is not
one of the properties on the list.
NOTE: The difference with plist-get is that this function works with
strings properties.
OPTIONS can contain :ignore-case in which case the case string and prefix
are matched case insensitively.
"
(let ((ignore-case (member :ignore-case options))
)
(when ignore-case
(setq prop (upcase prop)))
(loop for clist = slist then (cddr clist)
while clist
for sprop = (if ignore-case (upcase (car clist)) (car clist))
while (string/= sprop prop)
finally return (cadr clist))
)) ;;slist-get
(defun pjb-mail-narrow-to-headers ()
"Narrow the buffer to the head of the message."
;; Taken from message-narrow-to-headers
(interactive)
(widen)
(narrow-to-region
(goto-char (point-min))
(if (re-search-forward
(concat "^" (regexp-quote mail-header-separator) "\n") nil t)
(match-beginning 0)
(point-max)))
(goto-char (point-min))) ;;pjb-mail-narrow-to-headers
(defun pjb-mail-remove-header (header &optional is-regexp first reverse)
"
PRE: (pjb-mail-narrow-to-header)
DO: Remove HEADER in the narrowed buffer.
If IS-REGEXP, HEADER is a regular expression.
If FIRST, only remove the first instance of the header.
If REVERSE, remove the header not matching HEADER (regexp).
RETURN: the number of headers removed.
NOTE: See original code: message-remove-header
"
(goto-char (point-min))
(let ((regexp (if is-regexp header (concat "^" (regexp-quote header) ":")))
(number 0)
(case-fold-search t)
last)
(while (and (not (eobp))
(not last))
(if (let ((case-fold-search t))
;; case is not significant in headers whatever what.
(if reverse
(not (looking-at regexp))
(looking-at regexp)))
(progn
(incf number)
(when first
(setq last t))
(delete-region
(point)
;; There might be a continuation header, so we have to search
;; until we find a new non-continuation line.
(progn
(forward-line 1)
(if (re-search-forward "^[^ \t]" nil t)
(goto-char (match-beginning 0))
(point-max)))))
(forward-line 1)
(if (re-search-forward "^[^ \t]" nil t)
(goto-char (match-beginning 0))
(point-max))))
number))
(defun pjb-mail-make-boundary ()
"
RETURN: A unique string that can be used as a boundary.
"
(let ((ut (decode-time)))
(format "%s.%08d.%04d-%02d-%02d-%02d-%02d-%02d/%02d"
(system-name) (random 100000000)
(elt ut 5) (elt ut 4) (elt ut 3)
(elt ut 2) (elt ut 1) (elt ut 0)
(or (elt ut 8) 0))))
(defun pjb-mail$$insert-content-headers (&rest headers-k-v)
"
DO: Inserts at point 'Content-' MIME headers as directed by headers-k-v
which should be of the form:
:key1 value1 :key2 value2
-->
Content-Key1: value1
Content-Key2: value2
"
;;(message "headers-k-v=%S\n" headers-k-v)
(loop for current = headers-k-v then (cddr current)
while current
for key = (car current)
for value = (cadr current)
do (insert (format "Content-%s: %s\n"
(capitalize (subseq (symbol-name key) 1))
value))
) ;;loop
) ;;pjb-mail$$insert-content-headers
(defun pjb-mail$$select (indicator list)
"
RETURN: The sublist of the elements from LIST for which the indicator function
does not return nil.
"
(let ( (result nil) )
(dolist (element list)
(when (funcall indicator element)
(setq result (cons element result))))
result)
) ;;pjb-mail$$select
(defun pjb-mail-make-mime ()
"
PRE: the current buffer contains a RFC822 message.
DO: If the mail is not already a MIME message,
then makes it a MIME message.
POST: the current buffer contains a MIME multipart/mixed message,
either the original one, or a new one with the old message contents
as the first MIME section.
"
(let ((headers
(progn (widen) (goto-char (point-min)) (mail-header-extract)))
boundary)
(unless (mail-header 'mime-version headers)
;; not already a MIME message.
;; let's encapusulate the existing body into the first section before
;; adding a new section for the attached file.
(setq boundary (pjb-mail-make-boundary))
(pjb-mail-narrow-to-headers)
(goto-char (point-max))
(insert "MIME-Version: 1\n")
(pjb-mail$$insert-content-headers :transfer-encoding "8bit"
:type (format "multipart/mixed; boundary=\"%s\""
boundary)
:disposition "inline")
(widen)
(mail-text)
(insert "\n")
(insert "This is a MIME formated message.\n\n")
(insert (format "--%s\n" boundary))
(pjb-mail$$insert-content-headers :encoding "8bit"
:type "text/plain; charset=iso-8859-1"
:language "fr,en"
:disposition "inline"
:description "Cover letter.")
(goto-char (point-max))
(insert "\n\n")
(insert (format "--%s--\n" boundary))
(insert "\n")
(widen)
) ;;unless not already a MIME message.
)) ;;pjb-mail-make-mime
(defun pjb-mail-make-multipart/mixed ()
"
PRE: the current buffer contains a MIME message.
DO: if it's not already a multipart/mixed message,
then makes it multipart/mixed.
POST: the current buffer contains a MIME multipart/mixed message,
either the original one, or a new one with the old message contents
as the first MIME section.
"
(let ((headers
(progn (widen) (goto-char (point-min)) (mail-header-extract)))
boundary)
(unless (pjb-string-prefix-p (mail-header 'content-type headers)
"multipart/mixed" :ignore-case)
;; already a MIME, but not a mixed
;; let's move the content- headers to the existing body and
;; encapusulate it into the first section before
;; adding a new section for the attached file.
(setq boundary (pjb-mail-make-boundary))
;; since it's not a multipart/mixed, there's no boundary yet.
;; first, let's report the current "Content-" headers into the body.
(widen)
(mail-text)
(insert "\n")
(insert "This is a MIME formated message.\n\n")
(insert (format "--%s\n" boundary))
(apply 'pjb-mail$$insert-content-headers
(flatten
(mapcar
(lambda (item)
(list (intern (concat ":"
(chop-prefix (symbol-name (car item))
"content-" :ignore-case)))
(cdr item)))
(pjb-mail$$select
(lambda (item)
(pjb-string-prefix-p (symbol-name (car item))
"content-" :ignore-case))
headers)
)))
(insert "\n")
;; (sleep 0.001) ;; to display the changes...
(goto-char (point-max)) ;; end of message text
(insert "\n\n")
(insert (format "--%s--\n" boundary))
(insert "\n")
;; next, let's remove the current "Content-" headers
(pjb-mail-narrow-to-headers)
(pjb-mail-remove-header "^Content-.*" t nil nil)
;; finally, let's add the new "Content-" headers.
(goto-char (point-max)) ;; end of headers
;; no need to insert MIME-Version, it's aready a MIME message.
(pjb-mail$$insert-content-headers
:transfer-encoding "8bit"
:type (format "multipart/mixed; boundary=\"%s\""
boundary)
:disposition "inline")
(widen)
)))
(defun pjb-mail-get-multipart/mixed-boundary (headers)
"
RETURN: the boundary attribute of the multipart/mixed content-type
header in headers.
"
(replace-regexp-in-string
"^\"\\(.*\\)\"$" "\\1"
(slist-get (delete "" (flatten
(mapcar
(lambda (s) (split-string s "="))
(split-string (chop-prefix
(mail-header 'content-type headers)
"multipart/mixed" :ignore-case)
"[\n ]*;[\n ]*"))))
"boundary" :ignore-case) nil nil))
(defun pjb-mail-attach-file (file-name &optional compress)
"
DO: Ask for the path of a file to attach to the message being edited
in the current buffer.
"
(interactive "*fFile to attach:
P")
;; (unless (eq major-mode 'mail-mode)
;; (error "I can attach a file only to a mail buffer."))
(pjb-mail-make-mime)
(pjb-mail-make-multipart/mixed)
(let ((headers (progn (widen) (goto-char (point-min)) (mail-header-extract))))
(unless (mail-header 'content-type headers)
(error "Can't get Content-Type: header!"))
;; already a MIME and already a mixed
;; let's just recover the boundary and add a section.
(let ((boundary (pjb-mail-get-multipart/mixed-boundary headers)))
(when (or (null boundary) (= 0 (length boundary)))
(error "Invalid multipart/mixed boundary. Please fix."))
(widen)
(goto-char (point-max))
(if (re-search-backward (format "^--%s\\(--\\)?\n"
(regexp-quote boundary))
(point-min) t)
(progn ;; found
(goto-char (match-beginning 0))
(insert (concat "--" boundary "\n"))
;; TODO: handle errors in case insert-attachment-here fails.
(pjb-mail-insert-attachment-here file-name compress))
(error "No boundary in a multipart/mixed. Please fix.")))))
(defun parse-address-list-string (adlist-string)
"
DO: parse the adlist-string and extract from it the email addresses.
RETURN: a list of strings containing each one address.
"
(let ((i 0)
(len (length adlist-string))
(result nil)
(curaddr nil)
(state :unknown)
;; ( :unknown :in-address
;; :in-comment-paren :in-escape-quote :in-comment-angle )
(prevstate nil)
(escape nil) ;; ?\
(start 0)
curchar
(dbl-quote (character "\"" ))
(left-paren (character "(" ))
(right-paren (character ")" ))
(left-angle (character "<" ))
(right-angle (character ">" ))
(anti-slash (character "\\" ))
(comma (character "," ))
)
(while (<= i len)
(if (< i len)
(setq curchar (aref adlist-string i))
(setq curchar nil))
(cond
((eq state :unknown)
(if (and escape curchar)
(progn
(push curchar curaddr)
(setq escape nil))
(cond
((or (null curchar) (= curchar comma))
(let ((address (list-to-string (nreverse curaddr))))
(when (< 0 (length address))
(push (chop-spaces address) result))
(setq curaddr nil)))
((= curchar dbl-quote)
(push curchar curaddr)
(setq prevstate state
state :in-escape-quote))
((= curchar left-paren) (setq state :in-comment-paren))
((= curchar left-angle) (setq state :in-address
curaddr nil))
((= curchar anti-slash)
(push curchar curaddr)
(setq escape t))
(t (push curchar curaddr))
))) ;;state :unknown
((eq state :in-address)
(if (and escape curchar)
(progn
(push curchar curaddr)
(setq escape nil))
(cond
((or (null curchar) (= curchar right-angle))
(let ((address (list-to-string (nreverse curaddr))))
(when (< 0 (length address))
(push (chop-spaces address) result))
(setq curaddr nil))
(setq state :in-comment-angle))
((= curchar anti-slash)
(push curchar curaddr)
(setq escape t))
(t (push curchar curaddr))
))) ;;state :in-address
((eq state :in-comment-paren)
(if escape
(setq escape nil)
(cond
((null curchar))
((= curchar right-paren) (setq state :unknown))
((= curchar anti-slash) (setq escape t))
))) ;;state :in-comment-paren
((eq state :in-escape-quote)
(if escape
(progn
(push curchar curaddr)
(setq escape nil))
(cond
((null curchar))
((= curchar dbl-quote)
(setq state prevstate))
((= curchar anti-slash)
(push curchar curaddr)
(setq escape t))
(t
(push curchar curaddr))
))) ;;state :in-escape-quote
((eq state :in-comment-angle)
(if escape
(setq escape nil)
(cond
((null curchar))
((= curchar anti-slash) (setq escape t))
((= curchar comma) (setq state :unknown))
))) ;;state :in-comment-angle
)
(setq i (1+ i))
) ;;while
result)) ;;parse-address-list-string
;;; (mapc (lambda (adls)
;;; (printf "%S\n %S\n\n" adls (parse-address-list-string adls)))
;;; ;; --------------------------------------------------
;;; '("Bernard <[email protected]>,Agathe <[email protected]>"
;;; "ber\\<nard <ber(n)@dot.com>"
;;; "[email protected]"
;;; "(comment) [email protected]"
;;; "[email protected] (comment)"
;;; "(comment) [email protected] (comment)"
;;; "pjb@(comment)informatimago.com"
;;; "<[email protected]>"
;;; "<(comment) [email protected]>"
;;; "<[email protected] (comment)>"
;;; "<(comment) [email protected] (comment)>"
;;; "<pjb@(comment)informatimago.com>"
;;; "a,b,c"
;;; ","
;;; ""
;;; "a,,b"
;;; "(abc),(def)"
;;; "(abc)"
;;; "a\\,b,c"
;;; "\\\\,"
;;; "\\,"
;;; "\""
;;; "a\\,,b"
;;; "\\a,,b"
;;; "\\(abc)\\,(def)"
;;; "\\(abc)"
;;; "\"Toto Duchnock\" <[email protected]>"
;;; "<\"toto duchnock\"@dot.com> haha ,([email protected]) toto"
;;; ))
(defun pjb-mail-sign (pass-phrase)
"
Content-Type: multipart/signed; micalg=pgp-sha1;
protocol=\"application/pgp-signature\"; boundary=\"5mCyUwZo2JvN/JJP\"
--5mCyUwZo2JvN/JJP
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
--5mCyUwZo2JvN/JJP
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: w00t!
iD8DBQE9xuYI76uNUzjDrRQRAmiHAJ9rPkgxJBK6TdIaiaalc7U5JxLX+wCfU/IG
4KOwAyGYpBr18qA4av8qMjA=
=WfP3
-----END PGP SIGNATURE-----
--5mCyUwZo2JvN/JJP--
"
) ;;pjb-mail-sign
(defun pjb-mail-encrypt (pass-phrase recipients &optional conv-utf-8)
"
DO: Encrypt the current mail and put it into a multipart/encrypted MIME.
Sign and encrypt the buffer, with pgp-signer key,
unlocked by the PASS-PHRASE, for the given RECIPIENTS.
"
(interactive
(list
;; pass-phrase
(progn
(unless (eq major-mode 'mail-mode)
(error "I can encrypt a message only in a mail buffer."))
(let ((pass-phrase (read-string "Pass phrase: ")))
(pgp-remove-key-from-history pass-phrase)
pass-phrase))
;; recipients
(let ((recipients
(unsplit-string
(remove-duplicates
(flatten
(mapcar ;; extract only the email addresses
(lambda (item) (parse-address-list-string (cdr item)))
(pjb-mail$$select ;; select only To: Cc: and Bcc: headers.
(lambda (item) (member (car item) '(to cc bcc)))
(progn ;; get the headers.
(widen)
(expand-mail-aliases (point-min) (progn (mail-text) (point)))
(goto-char (point-min))
(mail-header-extract)))))))))
(if recipients
recipients
(read-string "Recipients: ")))
;; conv-utf-8
current-prefix-arg
))
(unless (eq major-mode 'mail-mode)
(error "I can encrypt a message only in a mail buffer."))
(pjb-mail-make-mime)
(let ((headers
(progn (widen) (goto-char (point-min)) (mail-header-extract)))
(boundary (pjb-mail-make-boundary))
(text-beg (make-marker))
(text-end (make-marker))
)
;; copy the Content-* headers to the message body, ready to be encrypted.
(mail-text)
(apply 'pjb-mail$$insert-content-headers
(flatten
(mapcar
(lambda (item)
(list (intern (concat ":"
(chop-prefix (symbol-name (car item))
"content-" :ignore-case)))
(cdr item)))
(pjb-mail$$select
(lambda (item)
(pjb-string-prefix-p (symbol-name (car item))
"content-" :ignore-case))
headers)
)))
(insert "\n")
;; next, let's remove the current "Content-" headers
(pjb-mail-narrow-to-headers)
(pjb-mail-remove-header "^Content-.*" t nil nil)
;; let's add the new "Content-" headers.
(goto-char (point-max)) ;; end of headers
;; no need to insert MIME-Version, it's aready a MIME message.
(pjb-mail$$insert-content-headers
:transfer-encoding "7bit" ;; encrypted is pure ASCII
:type (format (concat "multipart/encrypted; "
"protocol=\"application/pgp-encrypted\"; "
"boundary=\"%s\"") boundary)
:disposition "inline")
(widen)
(mail-text)
(set-marker text-beg (point))
(set-marker text-end (point-max))
(insert-before-markers
(format
(concat "Beginning of MIME encrypted message.\n"
"\n"
"--%s\n"
"Content-Type: application/pgp-encrypted\n"
"Content-Disposition: inline; filename=\"msg.asc\"\n"
"\n"
"Version: 1\n"
"\n"
"--%s\n"
"Content-Type: application/octet-stream\n"
"Content-Disposition: inline\n"
"\n") boundary boundary))
(goto-char text-end)
(insert
(format
(concat "\n"
"--%s--\n"
"\n"
"End of MIME encrypted message.\n"
"\n") boundary))
(condition-case exception
(progn
(when conv-utf-8
(pgp-convert-to-utf-8 text-beg text-end) )
(pgp-encrypt-pk-range pass-phrase recipients text-beg text-end)
)
(error
(message "PGP encryption raised an exception: %S" exception)))
(set-marker text-beg nil)
(set-marker text-end nil)
(if (< 0 (buffer-size (buffer-named "*PGP*")))
(save-selected-window
(switch-to-buffer-other-window "*PGP*")
(fit-window-to-buffer)
))
))
(defun old-pjb-mail-attach-file (file-name &optional compress)
"
"
(interactive "*fFile to attach:
P")
(unless (eq major-mode 'mail-mode)
(error "I can attach a file only to a mail buffer."))
(let (headers boundary
)
(setq headers
(progn (widen) (goto-char (point-min)) (mail-header-extract)))
;;(pjb-mail-narrow-to-headers)
(unless (mail-header 'mime-version headers)
;; not already a MIME message.
;; let's encapusulate the existing body into the first section before
;; adding a new section for the attached file.
(setq boundary (pjb-mail-make-boundary))
(pjb-mail-narrow-to-headers)
(goto-char (point-max))
(insert "MIME-Version: 1\n")
(pjb-mail$$insert-content-headers :transfer-encoding "8bit"
:type (format "multipart/mixed; boundary=\"%s\""
boundary)
:disposition "inline")
(widen)
(mail-text)
(insert "\n")
(insert "This is a MIME formated message.\n\n")
(insert (format "--%s\n" boundary))
(pjb-mail$$insert-content-headers :encoding "8bit"
:type "text/plain; charset=iso-8859-1"
:language "fr,en"
:disposition "inline"
:description "Cover letter.")
(goto-char (point-max))
(insert "\n\n")
(insert (format "--%s--\n" boundary))
(insert "\n")
(setq headers
(progn (widen) (goto-char (point-min)) (mail-header-extract)))
) ;;unless not already a MIME message.
(unless (pjb-string-prefix-p (mail-header 'content-type headers)
"multipart/mixed" :ignore-case)
;; already a MIME, but not a mixed
;; let's move the content- headers to the existing body and
;; encapusulate it into the first section before
;; adding a new section for the attached file.
(setq boundary (pjb-mail-make-boundary))
;; since it's not a multipart/mixed, there's no boundary yet.
;; first, let's report the current "Content-" headers into the body.
(widen)
(mail-text)
(insert "\n")
(insert "This is a MIME formated message.\n\n")
(insert (format "--%s\n" boundary))
(apply 'pjb-mail$$insert-content-headers
(flatten
(mapcar
(lambda (item)
(list (intern (concat ":"
(chop-prefix (symbol-name (car item))
"content-" :ignore-case)))
(cdr item)))
(pjb-mail$$select
(lambda (item)
(pjb-string-prefix-p (symbol-name (car item))
"content-" :ignore-case))
headers)
)))
(sleep 0.001)
(goto-char (point-max)) ;; end of message text
(insert "\n\n")
(insert (format "--%s--\n" boundary))
(insert "\n")
;; next, let's remove the current "Content-" headers
(pjb-mail-narrow-to-headers)
(pjb-mail-remove-header "^Content-.*" t nil nil)
;; finally, let's add the new "Content-" headers.
(goto-char (point-max)) ;; end of headers
;; no need to insert MIME-Version, it's aready a MIME message.
(pjb-mail$$insert-content-headers :transfer-encoding "8bit"
:type (format "multipart/mixed; boundary=\"%s\""
boundary)
:disposition "inline")
(setq headers
(progn (widen) (goto-char (point-min)) (mail-header-extract)))
) ;;unless not already a multipart/mixed
(unless (mail-header 'content-type headers)
(error "Can't get Content-Type: header!"))
;; already a MIME and already a mixed
;; let's just recover the boundary and add a section.
(setq boundary
(replace-regexp-in-string "^\"\\(.*\\)\"$" "\\1" (slist-get (flatten (mapcar (lambda (s) (split-string s "=")) (split-string (chop-prefix (mail-header (quote content-type) headers) "multipart/mixed" :ignore-case) "[\n ]*;[\n ]*"))) "boundary" :ignore-case) nil nil))
(when (or (null boundary) (= 0 (length boundary)))
(error "Invalid multipart/mixed boundary. Please fix."))
(widen)
(goto-char (point-max))
(if (re-search-backward (format "^--%s\\(--\\)?\n"
(regexp-quote boundary))
(point-min) t)
(progn ;; found
(goto-char (match-beginning 0))
(insert (concat "--" boundary "\n"))
;; TODO: handle errors in case insert-attachment-here fails.
(pjb-mail-insert-attachment-here file-name compress)
)
(error "No boundary in a multipart/mixed. Please fix."))
) ;;progn
) ;;old-pjb-mail-attach-file
(defvar *pjb-mail-inline-types*
'("image/.*"))
(defvar *pjb-mail-mime-type-as-8bit*
'(
"application/ghostview"
"application/mac-binhex40"
"application/pgp"
"application/pgp-signature"
"application/postscript"
"application/rtf"
"application/x-csh"
"application/x-httpd-php"
"application/x-perl"
"application/x-sh"
"application/x-shar"
"application/x-tcl"
"application/x-tex"
"application/x-texinfo"
"application/x-troff"
"application/x-troff-man"
"application/x-troff-me"
"application/x-troff-ms"
"application/octetstream"
"message/external-body"
"message/news"
"message/partial"
"message/rfc822"
"multipart/alternative"
"multipart/appledouble"
"multipart/digest"
"multipart/mixed"
"multipart/parallel"
"text/css"
"text/html"
"text/plain"
"text/richtext"
"text/tab-separated-values"
"text/x-setext"
"text/x-sgml"
"text/x-vCalendar"
"text/x-vCard"
"text/xml"
"text/vnd.rn-realtext"
)
"List of mime type that can be sent with transport-encoding: 8bit.
Others will be sent as base64.")
(defun pjb-mail-insert-attachment-here (file-name &optional compress)
"
DO: Insert a file attachment at the point, prefixed with Content- headers
and possibly encoded. May ask the user for further attributes.
"
(setq file-name (expand-file-name file-name))
(unless (file-exists-p file-name)
(error "File '%s' does not exist." file-name))
(when (file-directory-p file-name)
(error "Can't attach a directory!"))
(let ((out-fname (basename file-name))
(type nil)
transfer-encoding
description
(disposition "attachment")) ; may be "inline"
(if compress
(setq type "application/x-gzip"
transfer-encoding "base64")
(progn
;; (mm-parse-mimetypes)
;; (when (string-match "\\(\\.[^\\.]+\\)$" file-name)
;; (setq type (mm-extension-to-mime (match-string 0 file-name))))
(unless type
(setq type
(replace-regexp-in-string
", English" ""
(car (split-string
;; TODO: handle errors
(shell-command-to-string
(format "file -L -b -i %s 2>/dev/null"
(shell-quote-argument file-name))) "\n")) t t)))
(when (or (null type) (= 0 (length type)) (string= "data" type))
(setq type
(read-string "Can't determine mime-type. Please tell me: "
"application/octetstream")))
(when (= 0 (length type))
(setq type "application/octetstream"))
(if (member* (car (split-string type " *; *"))
*pjb-mail-mime-type-as-8bit* :test (function cl:string-equal))
(setq transfer-encoding "8bit")
(setq transfer-encoding "base64"))
(when (member* (car (split-string type " *; *"))
*pjb-mail-inline-types*
:test (lambda (s r)
;; (message "(%S %S) -> %S" s r (string-match r s))
(string-match r s)))
(setf disposition "inline"))))
(setq description (read-string "Please enter attachment description: "))
(when (= 0 (length description))
(setq description
(format "Attachment %s" (shell-quote-argument out-fname))))
(pjb-mail$$insert-content-headers
:transfer-encoding transfer-encoding
:type type
:disposition (format "%s; filename=\"%s\""
disposition
(shell-quote-argument out-fname))
:description description)
(insert "\n\n")
(previous-line 1)
(if (string= "8bit" transfer-encoding)
(insert-file-contents file-name)
(if compress
(shell-command (format "gzip<%s|base64-encode"
(shell-quote-argument file-name)) t nil)
(shell-command (format "base64<%s"
(shell-quote-argument file-name)) t nil)))))
(when (require 'vm nil t)
(require 'vm-vars)
;;----------------------------------------------------------------------
;; Create auto-folders
;;----------------------------------------------------------------------
(defstruct (afr (:type list)) label slot regexp)
(defvar *auto-folder-regexps*
'(("Subject" vm-subject-of "^Subject:")
("To" vm-to-of "^To:")
("From" vm-from-of "^From:")
("FromToCc" vm-from-of "^\\(From:\\|To:\\|Cc:\\)")))
(defun vm-current-message () (car vm-message-pointer))
;; (defun pjb-vm-create-auto-folder-and-save-message (selector)
;; (interactive
;; (list
;; (completing-read
;; "Selector: " (mapcar (afr-label) *auto-folder-regexps*) nil t "From")))
;; (let ((
;;
;; (vm-subject-of message)
;; (vm-from-of message)
;; (vm-to-of message)
;; (vm-cc-of message)
;;
;; (vm-auto-select-folder vm-message-pointer vm-auto-folder-alist)
);; when require
(provide 'pjb-mail)
;;;; THE END ;;;;