-
Notifications
You must be signed in to change notification settings - Fork 11
/
imap.cl
2168 lines (1791 loc) · 59.9 KB
/
imap.cl
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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; -*- mode: common-lisp; package: net.post-office -*-
;;
;; imap.cl
;; imap and pop interface
;;
;; See the file LICENSE for the full license governing this code.
#+(version= 7 0)
(sys:defpatch "imap" 1
"v1: fetch-letter-sequence support."
:type :system
:post-loadable t)
#+(version= 8 0)
(sys:defpatch "imap" 1
"v1: fetch-letter-sequence support."
:type :system
:post-loadable t)
#+(version= 8 1)
(sys:defpatch "imap" 1
"v1: Add ssl/tls support for both imap/pop connections."
:type :system
:post-loadable t)
;; Description:
;;- This code in this file obeys the Lisp Coding Standard found in
;;- https://www.franz.com/~jkf/coding_standards.html
;;-
(defpackage :net.post-office
(:use :lisp :excl)
(:export
#:address-name
#:address-additional
#:address-mailbox
#:address-host
#:alter-flags
#:close-connection
#:close-mailbox
#:copy-to-mailbox
#:create-mailbox
#:delete-letter
#:delete-mailbox
#:envelope-date
#:envelope-subject
#:envelope-from
#:envelope-sender
#:envelope-reply-to
#:envelope-to
#:envelope-cc
#:envelope-bcc
#:envelope-in-reply-to
#:envelope-message-id
#:expunge-mailbox
#:fetch-field
#:fetch-letter
#:fetch-letter-sequence
#:end-of-letter-p
#:with-fetch-letter-sequence
#:fetch-parts
#:*imap-version-number*
#:make-envelope-from-text
#:mailbox-flags ; accessor
#:mailbox-permanent-flags ; acc
#:mailbox-list
#:mailbox-list-flags
#:mailbox-list-separator
#:mailbox-list-name
#:mailbox-message-count ; accessor
#:mailbox-recent-messages ; ac
#:mailbox-separator ; accessor
#:mailbox-uidvalidity
#:mailbox-uidnext
#:make-imap-connection
#:make-pop-connection
#:with-imap-connection
#:with-pop-connection
#:noop
#:parse-mail-header
#:top-lines ; pop only
#:unique-id ; pop only
#:po-condition
#:po-condition-identifier
#:po-condition-server-string
#:po-error
#:rename-mailbox
#:reset-mailbox
#:search-mailbox
#:select-mailbox
)
)
(in-package :net.post-office)
(provide :imap)
(defparameter *imap-version-number* '(:major 1 :minor 14)) ; major.minor
;; todo
;; have the list of tags selected done on a per connection basis to
;; eliminate any possible multithreading problems
;;
;;
(defvar *debug-imap* nil)
(defclass post-office ()
((socket :initarg :socket
:accessor post-office-socket)
(host :initarg :host
:accessor post-office-host
:initform nil)
(user :initarg :user
:accessor post-office-user
:initform nil)
(state :accessor post-office-state
:initarg :state
:initform :unconnected)
(timeout
;; time to wait for network activity for actions that should
;; happen very quickly when things are operating normally
:initarg :timeout
:initform 60
:accessor timeout)
))
(defclass imap-mailbox (post-office)
((mailbox-name ; currently selected mailbox
:accessor mailbox-name
:initform nil)
(separator
;; string that separates mailbox names in the hierarchy
:accessor mailbox-separator
:initform "")
;;; these slots hold information about the currently selected mailbox:
(message-count ; how many in the mailbox
:accessor mailbox-message-count
:initform 0)
(recent-messages ; how many messages since we last checked
:accessor mailbox-recent-messages
:initform 0)
(uidvalidity ; used to denote messages uniquely
:accessor mailbox-uidvalidity
:initform 0)
(uidnext
:accessor mailbox-uidnext ;; predicted next uid
:initform 0)
(flags ; list of flags that can be stored in a message
:accessor mailbox-flags
:initform nil)
(permanent-flags ; list of flags that be stored permanently
:accessor mailbox-permanent-flags
:initform nil)
(first-unseen ; number of the first unseen message
:accessor first-unseen
:initform 0)
;;; end list of values for the currently selected mailbox
;;; state information for fetch-letter-sequence
(fetch-letter-offset
:accessor fetch-letter-offset)
(fetch-letter-number
:accessor fetch-letter-number)
(fetch-letter-uid
:accessor fetch-letter-uid)
(fetch-letter-finished
:accessor fetch-letter-finished)
)
)
(defclass pop-mailbox (post-office)
((message-count ; how many in the mailbox
:accessor mailbox-message-count
:initform 0)
(fetch-letter-state
:accessor state
:initform :invalid)))
(defstruct (mailbox-list (:type list))
;; a list of these are returned by mailbox-list
flags
separator
name)
(defstruct (envelope (:type list))
;; returned by fetch-letter as the value of the envelope property
date
subject
from
sender
reply-to
to
cc
bcc
in-reply-to
message-id)
(defstruct (address (:type list))
name ;; often the person's full name
additional
mailbox ;; the login name
host ;; the name of the machine
)
;--------------------------------
; conditions
;
; We define a set of conditions that are signalled due to events
; in the imap interface.
; Each condition has an indentifier which is a keyword. That can
; be used in the handling code to identify the class of error.
; All our conditions are po-condition or po-error (which is a subclass of
; po-condition).
;
; A condition will have a server-string value if it as initiated by
; something returned by the server.
; A condition will have a format-control value if we want to display
; something we generated in response to
;
;
;
;; identifiers used in conditions/errors
; :problem condition
; the server responded with 'no' followed by an explanation.
; this mean that something unusual happend and doesn't necessarily
; mean that the command has completely failed (but it might).
;
; :unknown-ok condition
; the server responded with an 'ok' followed by something
; we don't recognize. It's probably safe to ignore this.
;
; :unknown-untagged condition
; the server responded with some untagged command we don't
; recognize. it's probaby ok to ignore this.
;
; :error-response error
; the command failed.
;
; :syntax-error error
; the data passed to a function in this interface was malformed
;
; :unexpected error
; the server responded an unexpected way.
;
; :server-shutdown-connection error
; the server has shut down the connection, don't attempt to
; send any more commands to this connection, or even close it.
;
; :timeout error
; server failed to respond within the timeout period
;
; :response-too-large error
; contents of a response is too large to store in a Lisp array.
;; conditions
(define-condition po-condition ()
;; used to notify user of things that shouldn't necessarily stop
;; program flow
((identifier
;; keyword identifying the error (or :unknown)
:reader po-condition-identifier
:initform :unknown
:initarg :identifier
)
(server-string
;; message from the imap server
:reader po-condition-server-string
:initform ""
:initarg :server-string
))
(:report
(lambda (con stream)
(with-slots (identifier server-string) con
;; a condition either has a server-string or it has a
;; format-control string
(format stream "Post Office condition: ~s~%" identifier)
(if* (and (slot-boundp con 'excl::format-control)
(excl::simple-condition-format-control con))
then (apply #'format stream
(excl::simple-condition-format-control con)
(excl::simple-condition-format-arguments con)))
(if* server-string
then (format stream
"~&Message from server: ~s"
(string-left-trim " " server-string)))))))
(define-condition po-error (po-condition error)
;; used to denote things that should stop program flow
())
;; aignalling the conditions
(defun po-condition (identifier &key server-string format-control
format-arguments)
(signal (make-instance 'po-condition
:identifier identifier
:server-string server-string
:format-control format-control
:format-arguments format-arguments
)))
(defun po-error (identifier &key server-string
format-control format-arguments)
(error (make-instance 'po-error
:identifier identifier
:server-string server-string
:format-control format-control
:format-arguments format-arguments)))
;----------------------------------------------
(defparameter *imap-tags* '("t01" "t02" "t03" "t04" "t05" "t06" "t07"))
(defvar *cur-imap-tags* nil)
(defvar *crlf*
(let ((str (make-string 2)))
(setf (aref str 0) #\return)
(setf (aref str 1) #\linefeed)
str))
;; returns values: socket starttls
;; server is a cons of the form:
;; (server-name &key (port 25) (ssl nil) (starttls nil) ...ssl-client-keywords...)
(defun connect-to-imap/pop-server (server-info server-type)
(macrolet ((pop-keyword (k l) `(prog1 (getf ,l ,k) (remf ,l ,k)))
(server-port (ssl type) `(cond ((eq ,type :imap) (if ,ssl 993 143))
((eq ,type :pop) (if ,ssl 995 110)))))
(let* ((server (car server-info))
(ssl-args (cdr server-info))
ssl port starttls sock)
(setq ssl (pop-keyword :ssl ssl-args))
(setq port (or (pop-keyword :port ssl-args) (server-port ssl server-type)))
(setq starttls (pop-keyword :starttls ssl-args))
(setq sock (socket:make-socket :remote-host server
:remote-port port))
(when ssl
(setq sock (apply #'socket:make-ssl-client-stream sock ssl-args)))
(values sock starttls))) )
(defun make-imap-connection (host &key (port 143)
user
password
(timeout 30))
(multiple-value-bind (sock starttls)
(if (consp host)
(connect-to-imap/pop-server host :imap)
(socket:make-socket :remote-host host :remote-port port))
(let ((imap (make-instance 'imap-mailbox
:socket sock
:host host
:timeout timeout
:state :unauthorized)))
(multiple-value-bind (tag cmd count extra comment)
(get-and-parse-from-imap-server imap)
(declare (ignorable cmd count extra))
(if* (not (eq :untagged tag))
then (po-error :error-response
:server-string comment)))
; check for starttls negotiation
(when starttls
(let (capabilities)
(send-command-get-results
imap "CAPABILITY"
#'(lambda (mb cmd count extra comment)
(declare (ignorable mb cmd count extra))
(setq capabilities comment))
#'(lambda (mb cmd count extra comment)
(check-for-success mb cmd count extra comment
"CAPABILITY")))
(when (and capabilities (match-re "STARTTLS" capabilities :case-fold t
:return nil))
;; negotiate starttls
(send-command-get-results imap "STARTTLS"
#'handle-untagged-response
#'(lambda (mb cmd count extra comment)
(check-for-success mb cmd count extra comment
"STARTTLS")
(setf (post-office-socket mb)
(socket:make-ssl-client-stream
(post-office-socket mb) :method :tlsv1)))))))
; now login
(send-command-get-results imap
(format nil "login ~a ~a" user password)
#'handle-untagged-response
#'(lambda (mb command count extra comment)
(check-for-success mb command count extra
comment
"login")))
; find the separator character
(let ((res (mailbox-list imap)))
;;
(let ((sep (cadr (car res))))
(if* sep
then (setf (mailbox-separator imap) sep))))
imap)))
(defmethod close-connection ((mb imap-mailbox))
(let ((sock (post-office-socket mb)))
(if* sock
then (ignore-errors
(send-command-get-results
mb
"logout"
; don't want to get confused by untagged
; bye command, which is expected here
#'(lambda (mb command count extra)
(declare (ignore mb command count extra))
nil)
#'(lambda (mb command count extra comment)
(check-for-success mb command count extra
comment
"logout")))))
(setf (post-office-socket mb) nil)
(if* sock then (ignore-errors (close sock)))
t))
(defmethod close-connection ((pb pop-mailbox))
(let ((sock (post-office-socket pb)))
(if* sock
then (ignore-errors
(send-pop-command-get-results
pb
"QUIT")))
(setf (post-office-socket pb) nil)
(if* sock then (ignore-errors (close sock)))
t))
(defun make-pop-connection (host &key (port 110)
user
password
(timeout 30))
(multiple-value-bind (sock starttls)
(if (consp host)
(connect-to-imap/pop-server host :pop)
(socket:make-socket :remote-host host :remote-port port))
(let ((pop (make-instance 'pop-mailbox
:socket sock
:host host
:timeout timeout
:state :unauthorized)))
(multiple-value-bind (result)
(get-and-parse-from-pop-server pop)
(if* (not (eq :ok result))
then (po-error :error-response
:format-control
"unexpected line from server after connect")))
; check for starttls negotiation
(when starttls
(let ((capabilities (send-pop-command-get-results pop "capa" t)))
(when (and capabilities (match-re "STLS" capabilities :case-fold t
:return nil))
(send-pop-command-get-results pop "STLS")
(setf (post-office-socket pop) (socket:make-ssl-client-stream
(post-office-socket pop) :method :tlsv1)))))
; now login
(send-pop-command-get-results pop (format nil "user ~a" user))
(send-pop-command-get-results pop (format nil "pass ~a" password))
(let ((res (send-pop-command-get-results pop "stat")))
(setf (mailbox-message-count pop) (car res)))
pop)))
(defmethod send-command-get-results ((mb imap-mailbox)
command untagged-handler tagged-handler)
;; send a command and retrieve results until we get the tagged
;; response for the command we sent
;;
(let ((tag (get-next-tag)))
(format (post-office-socket mb)
"~a ~a~a" tag command *crlf*)
(force-output (post-office-socket mb))
(if* *debug-imap*
then (format t
"~a ~a~a" tag command *crlf*)
(force-output))
(loop
(multiple-value-bind (got-tag cmd count extra comment)
(get-and-parse-from-imap-server mb)
(if* (eq got-tag :untagged)
then (funcall untagged-handler mb cmd count extra comment)
elseif (equal tag got-tag)
then (funcall tagged-handler mb cmd count extra comment)
(return)
else (po-error :error-response
:format-control "received tag ~s out of order"
:format-arguments (list got-tag)
:server-string comment))))))
(defun get-next-tag ()
(let ((tag (pop *cur-imap-tags*)))
(if* tag
thenret
else (setq *cur-imap-tags* *imap-tags*)
(pop *cur-imap-tags*))))
(defun handle-untagged-response (mb command count extra comment)
;; default function to handle untagged responses, which are
;; really just returning general state information about
;; the mailbox
(case command
(:exists (setf (mailbox-message-count mb) count))
(:recent (setf (mailbox-recent-messages mb) count))
(:flags (setf (mailbox-flags mb) (kwd-intern-possible-list extra)))
(:bye ; occurs when connection times out or mailbox lock is stolen
(ignore-errors (close (post-office-socket mb)))
(po-error :server-shutdown-connection
:server-string "server shut down the connection"))
(:no ; used when grabbing a lock from another process
(po-condition :problem :server-string comment))
(:ok ; a whole variety of things
(if* extra
then (if* (equalp (car extra) "unseen")
then (setf (first-unseen mb) (cadr extra))
elseif (equalp (car extra) "uidvalidity")
then (setf (mailbox-uidvalidity mb) (cadr extra))
elseif (equalp (car extra) "uidnext")
then (setf (mailbox-uidnext mb) (cadr extra))
elseif (equalp (car extra) "permanentflags")
then (setf (mailbox-permanent-flags mb)
(kwd-intern-possible-list (cadr extra)))
else (po-condition :unknown-ok :server-string comment))))
(t (po-condition :unknown-untagged :server-string comment)))
)
(defmethod begin-extended-results-sequence ((mb pop-mailbox))
(setf (state mb) 1))
(defmethod get-extended-results-sequence ((mb pop-mailbox) buffer &key (start 0) (end (length buffer)))
(declare (optimize (speed 3) (safety 1)))
(let ((inpos start)
(outpos start)
(sock (post-office-socket mb))
ch
stop)
(macrolet ((add-to-buffer ()
`(progn
(setf (schar buffer outpos) ch)
(incf outpos))))
(while (and (< inpos end) (/= (state mb) 4))
(setf stop (read-sequence buffer sock :start inpos :end end :partial-fill t))
(while (< inpos stop)
(setf ch (schar buffer inpos))
(if* (eq ch #\return)
thenret ; ignore crs
else (ecase (state mb)
(1 (if* (eq ch #\.) ; at beginning of line
then (setf (state mb) 2)
elseif (eq ch #\linefeed)
then
(add-to-buffer) ; state stays at 1
else
(setf (state mb) 3)
(add-to-buffer)))
(2 ; seen first dot
(if* (eq ch #\linefeed)
then ; end of results
(setf (state mb) 4)
(return)
else
(setf (state mb) 3)
(add-to-buffer))) ; normal reading
(3 ; middle of line
(if* (eq ch #\linefeed)
then (setf (state mb) 1))
(add-to-buffer))))
(incf inpos))
(setf inpos outpos))
outpos)))
(defmacro end-of-extended-results-p (mb)
`(= (state ,mb) 4))
(defmethod end-extended-results-sequence ((mb pop-mailbox))
(declare (optimize (speed 3) (safety 1)))
(let ((buffer (make-string 4096)))
(until (end-of-extended-results-p mb)
(get-extended-results-sequence mb buffer)))
(setf (state mb) :invalid-state)
t)
(defmacro with-extended-results-sequence ((mailbox) &body body)
(let ((mb (gensym)))
`(let ((,mb ,mailbox))
(begin-extended-results-sequence ,mb)
(unwind-protect
(progn
,@body)
;; cleanup
(end-extended-results-sequence ,mb)))))
(defun send-pop-command-get-results (pop command &optional extrap)
(declare (optimize (speed 3) (safety 1)))
;; send the given command to the pop server
;; if extrap is true and if the response is +ok, then data
;; will follow the command (up to and excluding the first line consisting
;; of just a period)
;;
;; if the pop server returns an error code we signal a lisp error.
;; otherwise
;; return
;; extrap is nil -- return the list of tokens on the line after +ok
;; extrap is true -- return the extra object (a big string)
;;
(format (post-office-socket pop) "~a~a" command *crlf*)
(force-output (post-office-socket pop))
(if* *debug-imap*
then (format t "~a~a" command *crlf*)
(force-output t))
(multiple-value-bind (result parsed line)
(get-and-parse-from-pop-server pop)
(if* (not (eq result :ok))
then (po-error :error-response
:server-string line))
(if* extrap
then ;; get the rest of the data
;; many but not all pop servers return the size of the data
;; after the +ok, so we use that to initially size the
;; retreival buffer.
(let* ((buf (get-line-buffer (+ (if* (fixnump (car parsed))
then (car parsed)
else 2048 ; reasonable size
)
50)))
(buflen (length buf))
(pos 0))
(with-extended-results-sequence (pop)
(until (end-of-extended-results-p pop)
(if* (>= pos buflen)
then ;; grow buffer
(if* (>= buflen (1- array-total-size-limit))
then ; can't grow it any further
(po-error
:response-too-large
:format-control
"response from mail server is too large to hold in a lisp array"))
(let ((new-buf (get-line-buffer (* buflen 2))))
(init-line-buffer new-buf buf)
(free-line-buffer buf)
(setq buf new-buf)
(setq buflen (length buf))))
(setf pos (get-extended-results-sequence pop buf :start pos :end buflen))))
(prog1 (subseq buf 0 pos)
(free-line-buffer buf)))
else parsed)))
(defun convert-flags-plist (plist)
;; scan the plist looking for "flags" indicators and
;; turn value into a list of symbols rather than strings
(do ((xx plist (cddr xx)))
((null xx) plist)
(if* (equalp "flags" (car xx))
then (setf (cadr xx) (mapcar #'kwd-intern (cadr xx))))))
(defmethod select-mailbox ((mb imap-mailbox) name)
;; select the given mailbox
(send-command-get-results mb
(format nil "select ~a" name)
#'handle-untagged-response
#'(lambda (mb command count extra comment)
(declare (ignore mb count extra))
(if* (not (eq command :ok))
then (po-error
:problem
:format-control
"imap mailbox select failed"
:server-string comment))))
(setf (mailbox-name mb) name)
t
)
(defmethod fetch-letter ((mb imap-mailbox) number &key uid)
;; return the whole letter
(fetch-field number "body[]"
(fetch-parts mb number "body[]" :uid uid)
:uid uid))
(defmethod fetch-letter ((pb pop-mailbox) number &key uid)
(declare (ignore uid))
(send-pop-command-get-results pb
(format nil "RETR ~d" number)
t ; extra stuff
))
(defmethod begin-fetch-letter-sequence ((mb imap-mailbox) number &key uid)
(setf (fetch-letter-offset mb) 0)
(setf (fetch-letter-number mb) number)
(setf (fetch-letter-uid mb) uid)
(setf (fetch-letter-finished mb) nil))
(defmethod begin-fetch-letter-sequence ((mb pop-mailbox) number &key uid)
(declare (ignore uid))
(send-pop-command-get-results mb (format nil "RETR ~d" number))
(begin-extended-results-sequence mb))
(defmethod fetch-letter-sequence ((mb imap-mailbox) buffer
&key (start 0) (end (length buffer)))
(let* ((num (fetch-letter-number mb))
(offset (fetch-letter-offset mb))
(uid (fetch-letter-uid mb))
(buflen (- end start))
(data (fetch-field num (format nil "body[]<~d>" offset)
(fetch-parts mb num
(format nil "body[]<~d.~d>" offset buflen)
:uid uid)
:uid uid))
(datalen (length data)))
(setf (subseq buffer start end) data)
(if* (and (> buflen 0) (= datalen 0))
then (setf (fetch-letter-finished mb) t))
(setf (fetch-letter-offset mb) (+ offset buflen))
(+ start datalen)))
(defmethod fetch-letter-sequence ((mb pop-mailbox) buffer &key (start 0) (end (length buffer)))
(get-extended-results-sequence mb buffer :start start :end end))
(defmethod end-fetch-letter-sequence ((mb imap-mailbox))
)
(defmethod end-fetch-letter-sequence ((mb pop-mailbox))
(end-extended-results-sequence mb))
(defmethod end-of-letter-p ((mb imap-mailbox))
(fetch-letter-finished mb))
(defmethod end-of-letter-p ((mb pop-mailbox))
(end-of-extended-results-p mb))
(defmacro with-fetch-letter-sequence ((mailbox &rest args) &body body)
(let ((mb (gensym)))
`(let ((,mb ,mailbox))
(begin-fetch-letter-sequence ,mb ,@args)
(unwind-protect
(progn
,@body)
;; cleanup
(end-fetch-letter-sequence ,mb)))))
(defmethod fetch-parts ((mb imap-mailbox) number parts &key uid)
(let (res)
(send-command-get-results
mb
(format nil "~afetch ~a ~a"
(if* uid then "uid " else "")
(message-set-string number)
(or parts "body[]")
)
#'(lambda (mb command count extra comment)
(if* (eq command :fetch)
then (push (list count (internalize-flags extra)) res)
else (handle-untagged-response
mb command count extra comment)))
#'(lambda (mb command count extra comment)
(declare (ignore mb count extra))
(if* (not (eq command :ok))
then (po-error :problem
:format-control "imap mailbox fetch failed"
:server-string comment))))
res))
(defun fetch-field (letter-number field-name info &key uid)
;; given the information from a fetch-letter, return the
;; particular field for the particular letter
;;
;; info is as returned by fetch
;; field-name is a string, case doesn't matter.
;;
(dolist (item info)
;; item is (messagenumber plist-info)
;; the same messagenumber may appear in multiple items
(let (use-this)
(if* uid
then ; uid appears as a property in the value, not
; as the top level message sequence number
(do ((xx (cadr item) (cddr xx)))
((null xx))
(if* (equalp "uid" (car xx))
then (if* (eql letter-number (cadr xx))
then (return (setq use-this t))
else (return))))
else ; just a message sequence number
(setq use-this (eql letter-number (car item))))
(if* use-this
then (do ((xx (cadr item) (cddr xx)))
((null xx))
(if* (equalp field-name (car xx))
then (return-from fetch-field (cadr xx))))))))
(defun internalize-flags (stuff)
;; given a plist like object, look for items labelled "flags" and
;; convert the contents to internal flags objects
(do ((xx stuff (cddr xx)))
((null xx))
(if* (equalp (car xx) "flags")
then ; we can end up with sublists of forms if we
; do add-flags with a list of flags. this seems like
; a bug in the imap server.. but we have to deal with it
(setf (cadr xx) (kwd-intern-possible-list (cadr xx)))
(return)))
stuff)
(defmethod delete-letter ((mb imap-mailbox) messages &key (expunge t) uid)
;; delete all the mesasges and do the expunge to make
;; it permanent if expunge is true
(alter-flags mb messages :add-flags :\\deleted :uid uid)
(if* expunge then (expunge-mailbox mb)))
(defmethod delete-letter ((pb pop-mailbox) messages &key (expunge nil) uid)
;; delete all the messages. We can't expunge without quitting so
;; we don't expunge
(declare (ignore expunge uid))
(if* (or (numberp messages)
(and (consp messages) (eq :seq (car messages))))
then (setq messages (list messages)))
(if* (not (consp messages))
then (po-error :syntax-error
:format-control "expect a mesage number or list of messages, not ~s"
:format-arguments (list messages)))
(dolist (message messages)
(if* (numberp message)
then (send-pop-command-get-results pb
(format nil "DELE ~d" message))
elseif (and (consp message) (eq :seq (car message)))
then (do ((start (cadr message) (1+ start))
(end (caddr message)))
((> start end))
(send-pop-command-get-results pb
(format nil "DELE ~d" start)))
else (po-error :syntax-error
:format-control "bad message number ~s"
:format-arguments (list message)))))
(defmethod noop ((mb imap-mailbox))
;; just poke the server... keeping it awake and checking for
;; new letters
(send-command-get-results mb
"noop"
#'handle-untagged-response
#'(lambda (mb command count extra comment)
(check-for-success
mb command count extra
comment
"noop"))))
(defmethod noop ((pb pop-mailbox))
;; send the stat command instead so we can update the message count
(let ((res (send-pop-command-get-results pb "stat")))
(setf (mailbox-message-count pb) (car res)))
)
(defmethod unique-id ((pb pop-mailbox) &optional message)
;; if message is given, return the unique id of that
;; message,
;; if message is not given then return a list of lists:
;; (message unique-id)
;; for all messages not marked as deleted
;;
(if* message
then (let ((res (send-pop-command-get-results pb
(format nil
"UIDL ~d"
message))))
(cadr res))
else ; get all of them
(let* ((res (send-pop-command-get-results pb "UIDL" t))
(end (length res))
kind
mnum
mid
(next 0))
(let ((coll))
(loop
(multiple-value-setq (kind mnum next)
(get-next-token res next end))
(if* (eq :eof kind) then (return))
(if* (not (eq :number kind))
then ; hmm. bogus
(po-error :unexpected
:format-control "uidl returned illegal message number in ~s"
:format-arguments (list res)))
; now get message id
(multiple-value-setq (kind mid next)
(get-next-token res next end))
(if* (eq :number kind)
then ; looked like a number to the tokenizer,
; make it a string to be consistent
(setq mid (format nil "~d" mid))
elseif (not (eq :string kind))