File: pop3.lsp

package info (click to toggle)
newlisp 10.7.5-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,292 kB
  • sloc: ansic: 33,280; lisp: 4,181; sh: 609; makefile: 215
file content (323 lines) | stat: -rw-r--r-- 11,048 bytes parent folder | download | duplicates (4)
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
;; @module pop3.lsp
;; @description POP3 mail retrieval routines
;; @version 2.0 - eliminated old net-send syntax
;; @version 2.1 - changes for 10.0
;; @version 2.3 - three fixes by winger 2012-08-29 (search: winger's fix)
;; @version 2.4 - changes references to /usr/ to /usr/local/
; Do not fully understand winger's 'net-receive-blank' , couldn't 'net-flush'
; be used instead? The problem seems to be trailing spaces after "+OK".
; Can somebody with access to a pop3 server verify?
;; @author Lutz Mueller et al., 2001, 2002, 2008, 2010, 2012
;;
;;
;; <h2>POP3 mail retrieval routines</h2>
;; Only the module 'pop3.lsp' is required, not other libraries need to be
;; present. Not all mailservers support all functions.
;;
;; To use the module put a 'load' statement at the top of your file:
;; <pre>
;; (load "/usr/local/share/newlisp/modules/pop3.lsp")
;; ; or shorter
;; (module "pop3")
;; </pre>
;;
;; <h2>Function overview</h2>
;; Load down all messages and put them in a directory 'messages/':
;; <pre>
;; (POP3:get-all-mail "user" "password" "pop.my-isp.com" "messages/")
;; </pre>
;; Load down only new messages:
;; <pre>
;; (POP3:get-new-mail "user" "password" "pop.my-isp.com" "messages/")
;; </pre>
;; Delete messages, which have not been read:
;; <pre>
;; (POP3:delete-old-mail "user" "password" "pop.my-isp.com")
;; </pre>
;; Delete all messages:
;; <pre>
;; (POP3:delete-all-mail "user" "password" "pop.my-isp.com")
;; </pre>
;; Get a list of status numbers '(<totalMessages>, <totalBytes>, <lastRead>)':
;; <pre>
;; (POP3:get-mail-status "user" "password" "pop.my-isp.com")
;; </pre>
;; Get error message for failed all/new/status function:
;; <pre>
;; (POP3:get-error-text)
;; </pre>
;; All functions return 'nil' on error and 'POP3:get-error-text' can be used to
;; retrieve the error text.
;;
;; The variable 'POP3:debug-flag' can be set to 'true' to display all of the
;; dialog with the pop2 mail server.

(context 'POP3)

(set 'debug-flag nil)

;; @syntax (POP3:get-all-mail <str-user> <str-password> <str-server> <str-dir>)
;; @param <str-user> The user ID.
;; @param <str-password> The password for the user ID.
;; @param <str-dir> The local directory for the retrieved mail.
;; @return On success 'true' else 'nil'.

(define (get-all-mail userName password pop3server mail-dir)
    (and
        (connect pop3server)
        (logon userName password)
        (set 'status (get-status))
        (set 'no-msgs (nth 2 status))
        (if (> no-msgs 0)
          (get-messages 1 no-msgs mail-dir)
          true)
        (log-off)))

;; @syntax (POP3:get-new-mail <str-user> <str-password> <str-server> <str-dir>)
;; @param <str-user> The user ID.
;; @param <str-password> The password for the user ID.
;; @param <str-dir> The local directory for the retrieved mail.
;; @return On success returns 'true' else 'nil'.
;; On failure use 'POP3:get-error-text' to retrieve the text of
;; the last error which occured.

(define (get-new-mail userName password pop3server mail-dir)
    (and
        (connect pop3server)
        (logon userName password)
        (set 'status (get-status true))
        (if (<= (first status) (nth 2 status))
            ; winger's fix1 "messages are counted from 1"
            (get-messages (++ (first status)) (nth 2 status) mail-dir)
            ; (get-messages (first status) (nth 2 status) mail-dir);
            true)
        (log-off)
    ) )

;; @syntax (POP3:get-mail-status <str-user> <str-password> <str-server>)
;; @param <str-user> The user ID.
;; @param <str-password> The password for the user ID.
;; @return A list of status information.
;; The list of status information returned contains the following items:
;; (<totalMessages>, <totalBytes>, <lastRead>)

(define (get-mail-status userName password pop3server)
    (and
        (connect pop3server)
        (logon userName password)
        (set 'status (get-status true))
        (log-off)
        status))

;; @syntax (POP3:delete-old-mail <str-user> <str-password> <str-server>)
;; @param <str-user> The user ID.
;; @param <str-password> The password for the user ID.
;; @return The number of messages left on the server.

(define (delete-old-mail userName password pop3server)
    (and
        (connect pop3server)
        (logon userName password)
        (set 'status (get-status true))
        (if (> (first status) 1)
            (for (msg 1 (- (first status) 1) ) (delete-message msg))
            true)
        (log-off)
        (first status)))

;; @syntax (POP3:delete-all-mail <str-user> <str-password> <str-server>)
;; @param <str-user> The user ID.
;; @param <str-password> The password for the user ID.
;; @return The number of the message last read.
(define (delete-all-mail userName password pop3server)
    (and
        (connect pop3server)
        (logon userName password)
        (set 'status (get-status))
        (if (> (last status) 0)
            (for (msg 1 (last status) ) (delete-message msg))
            true)
        (log-off)
        (last status)))

; receive request answer and verify
;
(define (net-confirm-request)
    (if (net-receive socket rcvbuff 512 "+OK")
        (begin
        (if debug-flag (println rcvbuff))
            (if (find "-ERR" rcvbuff)
                (finish rcvbuff)
                true))
        nil))

; winger's fix2 bypass " " of "+OK "
(define-macro (net-receive-blank int_socket sym-buffer max-bytes wait-string)
    (letex (int_socket (eval int_socket)
            sym-buffer sym-buffer
            max-bytes max-bytes)
        (if (and (net-receive int_socket sym-buffer max-bytes) (= " " sym-buffer))
            (net-receive int_socket sym-buffer max-bytes) )
    )
)

(define (net-flush)
    (if socket
        (while (> (net-peek socket) 0)
            (net-receive socket junk 256)
            (if debug-flag (println junk) )))
    true)

; connect to server
;
(define (connect server)
    (set 'socket (net-connect pop3server 110))
    (if (and debug-flag socket) (println "connected on: " socket) )
    (if (and socket (net-confirm-request))
        (net-flush)
        (finish "could not connect")))

;
(define (logon userName password)
    (and
        (set 'sndbuff (append "USER " userName "\r\n"))
        (net-send socket sndbuff)
        (if debug-flag (println "sent: " sndbuff) true)
        (net-confirm-request)
        (net-flush)
        (set 'sndbuff (append "PASS " password "\r\n"))
        (net-send socket sndbuff)
        (if debug-flag (println "sent: " sndbuff) true)
        (net-confirm-request)
        (net-flush)
        (if debug-flag (println "logon successful") true)))


; get status and last read
;
(define (get-status last-flag)
    (and
        (set 'sndbuff "STAT\r\n")
        (net-send socket sndbuff)
        (if debug-flag (println "sent: " sndbuff) true)
        (net-confirm-request)
        ; (net-receive socket status 256) ; old in 2.1 (10.4.3)
        (net-receive-blank socket status 256) ; new in 2.3 (10.4.4)
        (if debug-flag (println "status: " status) true)
        (net-flush)
        (if last-flag
            (begin
                (set 'sndbuff "LAST\r\n")
                (net-send socket sndbuff)
                (if debug-flag (println "sent: " sndbuff) true)
                (net-confirm-request)
                ; (net-receive socket last-read 256) ; old
                (net-receive-blank socket last-read 256) ; new
                (if debug-flag (println "last read: " last-read) true)
                (net-flush))
            (set 'last-read "0"))
        (set 'result (list (int (first (parse status)))))
        (if debug-flag (println "parsed status: " result) true)
        (push (int (nth 1 (parse status))) result)
        (push (int (first (parse last-read))) result)
        result)) ; not necessary starting 9.9.5 because push returns the list


; get a message
;
(define (retrieve-message , message)
    (set 'finished nil)
    (set 'message "")
    (while (not finished)
        (net-receive socket rcvbuff 16384)
        (set 'message (append message rcvbuff))
        (if (find "\r\n.\r\n" message) (set 'finished true)))
    (if debug-flag (println "received message") true)
    message)


; get all messages
;
; v 1.4: modified file name generation to improve uniqueness. (CaveGuy)
;        file name now created using last SMTP or ESMTP ID from header.
; v 1.5: changed file type to ".pop3" to reflect the context that created it.
;        (get-messages now forces the directory, if it does not exsist.
; v 1.6: make sure directory? doesn't have trailing slash in arg
;
(define (get-messages from to mail-dir)
   (if (ends-with mail-dir "/") (set 'mail-dir (chop mail-dir)))
   (if (if (not (directory? mail-dir)) (make-dir mail-dir) true)
       (begin
          (set 'mail-dir (append mail-dir "/")) 
          (for (msg from to)
               (if debug-flag (println "getting message " msg) true)
               (set 'sndbuff (append "RETR " (string msg) "\r\n"))
               (net-send socket sndbuff)
               (if debug-flag (println "sent: " sndbuff) true)
               (set 'message (retrieve-message))
               (if debug-flag (println (slice message 1 200)) true)
               (set 'istr (get-message-id message))
               (set 'istr (append mail-dir "ME-" istr))
               (if debug-flag (println "saving " istr) true)
               (write-file istr message)
               (if (not (rename-file istr (append istr ".pop3")))
               (delete-file istr)))))
    true) ; other parts of pop3 rely on 'true' return

; delete messages
;
(define (delete-message msg)
    (and
        (set 'sndbuff (append "DELE " (string msg) "\r\n"))
        (net-send socket sndbuff)
        (if debug-flag (println "sent: " sndbuff) true)
        (net-confirm-request)))

; get-message-date was
; changed to get-message-id
; v 1.4: CaveGuy

(define (get-message-id message)
    (set 'ipos (+ (find "id <| id |\tid " message 1) 5)
         ; winger's fix3 delete char '>'
         'iend (-- (find "@|;|\n|\r| |\t" (slice message ipos) 1)))
         ; 'iend (find "@|;|\n|\r| |\t" (slice message ipos) 1));
    (if debug-flag
    (print "Message ID: " (slice message ipos iend) "\n"))
    (set 'istr (slice message ipos iend)) )


; log off
;
(define (log-off)
    (set 'sndbuff "QUIT\r\n")
    (net-send socket sndbuff)
    (if debug-flag (println "sent: " sndbuff) true)
    (net-receive socket rcvbuff 256)
    (if debug-flag (println rcvbuff) true)
    true)

; report error and finish
;
(define (finish message)
    (if (ends-with message "+OK")
      (set 'message (chop message 3)))
    ;(print "<h3>" message "</h3>")
    (set 'mail-error-text message)
    (if debug-flag (println "ERROR: " message) true)
    (if socket (net-flush))
    (if socket (log-off))
    nil)

;; @syntax (POP3:get-error-text)
;; @return The text of the last error occurred.

(define (get-error-text) mail-error-text)

(context 'MAIN)


; test
;(if (not(POP3:get-all-mail "user" "password" "my-isp.com" "mail"))
;    (print (POP3:get-error-text)) true)
;(exit)