cl-smtp.lisp 21.3 KB
Newer Older
Jan Idzikowski's avatar
Jan Idzikowski committed
1
2
3
4
;;; -*- mode: Lisp -*-
	
;;; This file is part of CL-SMTP, the Lisp SMTP Client

5
;;; Copyright (C) 2004/2005/2006/2007/2008/2009/2010 Jan Idzikowski
Jan Idzikowski's avatar
Jan Idzikowski committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the Lisp Lesser General Public License
;;; (http://opensource.franz.com/preamble.html), known as the LLGPL.

;;; 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
;;; Lisp Lesser GNU General Public License for more details.

;;; File: cl-smtp.lisp
;;; Description: main smtp client logic

(in-package :cl-smtp)

21
(defparameter *x-mailer* (format nil "(~A ~A)" 
Jan Idzikowski's avatar
Jan Idzikowski committed
22
23
24
25
26
				 (lisp-implementation-type)
				 (lisp-implementation-version)))

(defun check-arg (arg name)
  (cond
27
28
   ((or (stringp arg)
        (pathnamep arg))
Jan Idzikowski's avatar
Jan Idzikowski committed
29
30
31
32
33
34
    (list arg))
   ((listp arg)
    arg)
   (t
    (error "the \"~A\" argument is not a string or cons" name))))

Jan Idzikowski's avatar
Jan Idzikowski committed
35
36
37
38
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defvar *line-with-one-dot* #.(format nil "~C~C.~C~C" #\Return #\NewLine
                                        #\Return #\NewLine))
  (defvar *line-with-two-dots* #.(format nil "~C~C..~C~C" #\Return #\NewLine
39
40
                                         #\Return #\NewLine))
  (defvar *return-newline* #.(format nil "~C~C" #\Return #\NewLine)))
Jan Idzikowski's avatar
Jan Idzikowski committed
41

Jan Idzikowski's avatar
Jan Idzikowski committed
42
(defun mask-dot (str)
Jan Idzikowski's avatar
Jan Idzikowski committed
43
44
  "Replace all occurences of \r\n.\r\n in STR with \r\n..\r\n"
  (let ((resultstr ""))
Jan Idzikowski's avatar
Jan Idzikowski committed
45
    (labels ((mask (tempstr)
Jan Idzikowski's avatar
Jan Idzikowski committed
46
	       (let ((n (search *line-with-one-dot* tempstr)))
Jan Idzikowski's avatar
Jan Idzikowski committed
47
48
49
50
		 (cond
		  (n
		   (setf resultstr (concatenate 'string resultstr 
						(subseq tempstr 0 n)
Jan Idzikowski's avatar
Jan Idzikowski committed
51
52
						*line-with-two-dots*))
		   (mask (subseq tempstr (+ n #.(length *line-with-one-dot*)))))
Jan Idzikowski's avatar
Jan Idzikowski committed
53
54
55
56
57
58
		  (t
		   (setf resultstr (concatenate 'string resultstr 
						tempstr)))))))
      (mask str))
    resultstr))

59
(defun string-to-base64-string (str)
60
  (declare (ignorable str))
61
  #+allegro (excl:string-to-base64-string str :wrap-at-column nil)
62
  #-allegro (cl-base64:string-to-base64-string str))
63

64
65
66
(defun rfc2045-q-encode-string (str &key (external-format :utf-8))
  (let ((line-has-non-ascii nil)
        (exformat (flex:make-external-format external-format)))
67
68
69
70
71
    (with-output-to-string (s)
      (loop for c across str do
           (cond
             ((< 127 (char-code c))
              (unless line-has-non-ascii
72
73
                (format s "=?~A?Q?" 
                        (string-upcase (symbol-name external-format)))
74
75
76
                (setf line-has-non-ascii t))
              (loop for byte across (flex:string-to-octets 
                                     (make-string 1 :initial-element c)
77
                                     :external-format exformat)
78
79
80
81
82
83
84
85
86
                 do (format s "=~2,'0X" byte)))
             (t 
              (when line-has-non-ascii
                (format s "?=")
                (setf line-has-non-ascii nil))
              (format s "~C" c))))
      (when line-has-non-ascii
        (format s "?=")))))

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
(defun substitute-return-newline (str)
  "Replace all occurences of \r\n in STR with spaces"
  (let ((resultstr ""))
    (labels ((mask (tempstr)
	       (let ((n (search *return-newline* tempstr)))
		 (cond
                   (n
                    (setf resultstr (concatenate 'string resultstr 
                                                 (subseq tempstr 0 n)
                                                 " "))
                    (mask (subseq tempstr (+ n 2))))
                   (t
                    (setf resultstr (concatenate 'string resultstr 
                                                 tempstr)))))))
      (mask str))
    resultstr))

Jan Idzikowski's avatar
Jan Idzikowski committed
104
105
106
107
108
109
110
111
112
113
(define-condition smtp-error (error)
  ())

(define-condition smtp-protocol-error (smtp-error)
  ((command :initarg :command :reader command)
   (expected-response-code :initarg :expected-response-code :reader expected-response-code)
   (response-code :initarg :response-code :reader response-code)
   (response-message :initarg :response-message :reader response-message))
  (:report (lambda (condition stream)
             (print-unreadable-object (condition stream :type t)
114
               (format stream "a command failed:~%command: ~S expected: ~A response-code: ~A response-message: ~A"
Jan Idzikowski's avatar
Jan Idzikowski committed
115
116
                       (command condition)
                       (expected-response-code condition)
117
                       (response-code condition)
Jan Idzikowski's avatar
Jan Idzikowski committed
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
                       (response-message condition))))))

(define-condition rcpt-failed (smtp-protocol-error)
  ((recipient :initarg :recipient
              :reader recipient))
  (:report (lambda (condition stream)
             (print-unreadable-object (condition stream :type t)
               (format stream "while trying to send email through SMTP, the server rejected the recipient ~A: ~A"
                       (recipient condition)
                       (response-message condition))))))

(defun smtp-command (stream command expected-response-code
                     &key (condition-class 'smtp-protocol-error) 
                     condition-arguments)
  (when command
    (write-to-smtp stream command))
  (multiple-value-bind (code msgstr lines)
      (read-from-smtp stream)
    (when (/= code expected-response-code)
      (apply #'error
             condition-class
             (append condition-arguments
                     (list :command command
                           :expected-response-code expected-response-code
                           :response-code code
                           :response-message msgstr))))
    lines))

146
(defun do-with-smtp-mail (host envelope-sender to thunk &key port authentication ssl local-hostname (external-format :utf-8))
147
148
149
  (usocket:with-client-socket (socket stream host port 
                                      :element-type '(unsigned-byte 8))
    (setf stream (flexi-streams:make-flexi-stream 
150
151
152
153
                  stream
                  :external-format 
                  (flexi-streams:make-external-format 
                   external-format :eol-style :lf)))
Jan Idzikowski's avatar
Jan Idzikowski committed
154
155
156
157
    (let ((stream (smtp-handshake stream
                                  :authentication authentication 
                                  :ssl ssl
                                  :local-hostname local-hostname)))
158
      (initiate-smtp-mail stream envelope-sender to)
159
      (funcall thunk stream)
Jan Idzikowski's avatar
Jan Idzikowski committed
160
161
      (finish-smtp-mail stream))))

162
(defmacro with-smtp-mail ((stream-var host envelope-sender to &key ssl (port (if (eq :tls ssl) 465 25)) authentication local-hostname (external-format :utf-8))
Jan Idzikowski's avatar
Jan Idzikowski committed
163
164
165
166
167
168
169
                          &body body)
  "Encapsulate a SMTP MAIl conversation.  A connection to the SMTP
   server on HOST and PORT is established and a MAIL command is
   initiated with FROM being the mail sender and TO being the list of
   recipients.  BODY is evaluated with STREAM-VAR being the stream
   connected to the remote SMTP server.  BODY is expected to write the
   RFC2821 message (headers and body) to STREAM-VAR."
170
  `(do-with-smtp-mail ,host ,envelope-sender ,to
Jan Idzikowski's avatar
Jan Idzikowski committed
171
172
173
174
                      (lambda (,stream-var) ,@body)
                      :port ,port
                      :authentication ,authentication 
                      :ssl ,ssl
175
176
                      :local-hostname ,local-hostname
                      :external-format ,external-format))
Jan Idzikowski's avatar
Jan Idzikowski committed
177
178

(defun send-email (host from to subject message 
Jan Idzikowski's avatar
Jan Idzikowski committed
179
		   &key ssl (port (if (eq :tls ssl) 465 25)) cc bcc reply-to extra-headers
180
		   html-message display-name authentication
181
		   attachments (buffer-size 256) envelope-sender (external-format :utf-8))
Jan Idzikowski's avatar
Jan Idzikowski committed
182
183
184
185
  (send-smtp host from (check-arg to "to") subject (mask-dot message)
	     :port port :cc (check-arg cc "cc") :bcc (check-arg bcc "bcc")
	     :reply-to reply-to 
	     :extra-headers extra-headers
186
	     :html-message html-message
187
	     :display-name display-name
188
189
190
191
	     :authentication authentication
	     :attachments (check-arg attachments "attachments")
	     :buffer-size (if (numberp buffer-size) 
			      buffer-size
192
			      256)
193
	     :envelope-sender (or envelope-sender from)
194
             :external-format external-format
195
	     :ssl ssl))
Jan Idzikowski's avatar
Jan Idzikowski committed
196

Jan Idzikowski's avatar
Jan Idzikowski committed
197
198
199
200
(defun send-smtp (host from to subject message
                  &key ssl (port (if (eq :tls ssl) 465 25)) cc bcc
		  reply-to extra-headers html-message display-name
		  authentication attachments buffer-size
201
                  (local-hostname (usocket::get-host-name))
202
203
204
		  (envelope-sender from)
		  (external-format :utf-8))
  (with-smtp-mail (stream host envelope-sender (append to cc bcc)
Jan Idzikowski's avatar
Jan Idzikowski committed
205
206
207
                          :port port
                          :authentication authentication 
                          :ssl ssl
208
209
                          :local-hostname local-hostname
                          :external-format external-format)
Jan Idzikowski's avatar
Jan Idzikowski committed
210
211
212
    (let* ((boundary (make-random-boundary))
           (html-boundary (if (and attachments html-message)
                              (make-random-boundary)
213
214
215
216
                              boundary))
           (content-type 
            (format nil "text/plain; charset=~S" 
                    (string-upcase (symbol-name external-format)))))
Jan Idzikowski's avatar
Jan Idzikowski committed
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
      (send-mail-headers stream
                         :from from
                         :to to
                         :cc cc
                         :reply-to reply-to
                         :display-name display-name 
                         :extra-headers extra-headers :subject subject)
      (when (or attachments html-message)
        (send-multipart-headers stream
                                :attachment-boundary (when attachments boundary) 
                                :html-boundary html-boundary))
      ;;----------- Send  the body Message ---------------------------
      ;;--- Send the proper headers depending on plain-text, 
      ;;--- multi-part or html email 
      (cond ((and attachments html-message)
             ;; if both present, start attachment section, 
             ;; then define alternative section, 
             ;; then write alternative header
             (progn 
               (generate-message-header 
                stream :boundary boundary :include-blank-line? nil)
               (generate-multipart-header stream html-boundary 
                                          :multipart-type "alternative")
               (write-blank-line stream)
               (generate-message-header 
242
                stream :boundary html-boundary :content-type content-type 
Jan Idzikowski's avatar
Jan Idzikowski committed
243
244
245
246
                :content-disposition "inline" :include-blank-line? nil)))
            (attachments 
             (generate-message-header 
              stream :boundary boundary 
247
              :content-type content-type :content-disposition "inline"
Jan Idzikowski's avatar
Jan Idzikowski committed
248
249
250
              :include-blank-line? nil))
            (html-message
             (generate-message-header 
251
              stream :boundary html-boundary :content-type content-type 
Jan Idzikowski's avatar
Jan Idzikowski committed
252
253
              :content-disposition "inline"))
            (t 
254
             (generate-message-header stream :content-type content-type
Jan Idzikowski's avatar
Jan Idzikowski committed
255
256
257
258
259
260
261
262
                                      :include-blank-line? nil)))
      (write-blank-line stream)
      (write-to-smtp stream message)
      (write-blank-line stream)
      ;;---------- Send  Html text if needed -------------------------
      (when html-message
        (generate-message-header 
         stream :boundary html-boundary 
263
264
         :content-type (format nil "text/html; charset=~S" 
                               (string-upcase (symbol-name external-format)))
Jan Idzikowski's avatar
Jan Idzikowski committed
265
266
267
268
269
270
         :content-disposition "inline")
        (write-to-smtp stream html-message)
        (send-end-marker stream html-boundary))
      ;;---------- Send Attachments -----------------------------------
      (when attachments
        (dolist (attachment attachments)
271
272
          (send-attachment stream attachment boundary buffer-size 
                           external-format))
Jan Idzikowski's avatar
Jan Idzikowski committed
273
        (send-end-marker stream boundary)))))
Jan Idzikowski's avatar
Jan Idzikowski committed
274

Jan Idzikowski's avatar
Jan Idzikowski committed
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
(define-condition no-supported-authentication-method (smtp-error)
  ((features :initarg :features :reader features))
  (:report (lambda (condition stream)
             (print-unreadable-object (condition stream :type t)
               (format stream "SMTP authentication has been requested, but the SMTP server did not advertise any ~
                               supported authentication scheme.  Features announced: ~{~S~^, ~}"
                       (features condition))))))

(defun smtp-authenticate (stream authentication features)
  "Authenticate to the SMTP server connected on STREAM.
   AUTHENTICATION is a list of two or three elements.  If the first
   element is a keyword, it specifies the desired authentication
   method (:PLAIN or :LOGIN), which is currently ignored.  The actual
   method used is determined by looking at the advertised features of
   the SMTP server.  The (other) two elements of the AUTHENTICATION
   list are the login username and password.  FEATURES is the list of
   features announced by the SMTP server.

   If the server does not announce any compatible authentication scheme,
   the NO-SUPPORTED-AUTHENTICATION-METHOD error is signalled."
  (when (keywordp (car authentication))
    (pop authentication))
  (let ((server-authentication (loop for i in features
                                  for e = (search "AUTH " i :test #'equal)
                                  when (and e (= e 0))
                                  return i)))
    (destructuring-bind (username password) authentication
      (cond
        ((search " PLAIN" server-authentication :test #'equal)
         (smtp-command stream (format nil "AUTH PLAIN ~A" 
                                      (string-to-base64-string
                                       (format nil "~A~C~A~C~A" 
                                               username
                                               #\null username
                                               #\null password)))
                       235))
        ((search " LOGIN" server-authentication :test #'equal)
         (smtp-command stream "AUTH LOGIN"
                       334)
         (smtp-command stream (string-to-base64-string username)
                       334)
         (smtp-command stream (string-to-base64-string password)
                       235))
        (t
         (error 'no-supported-authentication-method :features features))))))

(defun smtp-handshake (stream &key authentication ssl local-hostname)
  "Perform the initial SMTP handshake on STREAM.  Returns the stream
   to use further down in the conversation, which may be different from
   the original stream if we switched to SSL."

  (unless (or ssl authentication)
    ;; Unless we want ESMTP features, perform classic SMTP handshake and return
328
329
    ;; Read the initial greeting from the SMTP server
    (smtp-command stream nil 220)
Jan Idzikowski's avatar
Jan Idzikowski committed
330
    (smtp-command stream (format nil "HELO ~A" 
331
                                 (usocket::get-host-name))
Jan Idzikowski's avatar
Jan Idzikowski committed
332
333
334
335
336
337
                  250)
    (return-from smtp-handshake stream))

  ;; When SSL or authentication requested, perform ESMTP EHLO
  (let (features)
    (labels
338
339
340
341
        ((read-greetings ()
	   ;; Read the initial greeting from the SMTP server
	   (smtp-command stream nil 220))
	 (do-ehlo ()
Jan Idzikowski's avatar
Jan Idzikowski committed
342
343
344
345
346
347
           (setf features (rest (smtp-command stream (format nil "EHLO ~A" local-hostname)
                                              250))))
         (convert-connection-to-ssl ()
           (setf stream 
                 #+allegro (socket:make-ssl-client-stream stream)
                 #-allegro
348
                 (let ((s (flexi-streams:flexi-stream-stream stream)))
Jan Idzikowski's avatar
Jan Idzikowski committed
349
                   (cl+ssl:make-ssl-client-stream 
350
                    (cl+ssl:stream-fd s)
Jan Idzikowski's avatar
Jan Idzikowski committed
351
352
353
354
355
356
357
358
359
                    :close-callback (lambda () (close s)))))
           #-allegro
           (setf stream (flexi-streams:make-flexi-stream 
                         stream
                         :external-format 
                         (flexi-streams:make-external-format 
                          :latin-1 :eol-style :lf)))))
      (ecase ssl
        ((or t :starttls)
360
	 (read-greetings)
Jan Idzikowski's avatar
Jan Idzikowski committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
         (do-ehlo)
         (unless (find "STARTTLS" features :test #'equal)
           (error "this server does not supports TLS"))
         (print-debug "this server supports TLS")
         (smtp-command stream "STARTTLS"
                       220)
         (convert-connection-to-ssl)
         ;; After STARTTLS, the connection is "like new".  Re-do the
         ;; EHLO command to switch the server to ESMTP mode and read
         ;; the list of announced features again.
         (do-ehlo))
        (:tls
         ;; Plain SSL connection
         (convert-connection-to-ssl)
375
	 (read-greetings)
Jan Idzikowski's avatar
Jan Idzikowski committed
376
377
         (do-ehlo))
        ((nil)
378
	 (read-greetings)
Jan Idzikowski's avatar
Jan Idzikowski committed
379
380
381
         (do-ehlo))))
    (when authentication
      (smtp-authenticate stream authentication features)))
382
  stream)
383
  
384
(defun initiate-smtp-mail (stream envelope-sender to)
Jan Idzikowski's avatar
Jan Idzikowski committed
385
386
387
388
389
390
391
392
  "Initiate an SMTP MAIL command, sending a MAIL FROM command for the
   email address in FROM and RCPT commands for all receipients in TO,
   which is expected to be a list.

   If any of the TO addresses is not accepted, a RCPT-FAILED condition
   is signalled.  This condition may be handled by the caller in order
   to send the email anyway."
  (smtp-command stream 
393
                (format nil "MAIL FROM:<~A>" (substitute-return-newline envelope-sender))
Jan Idzikowski's avatar
Jan Idzikowski committed
394
395
396
                250)
  (dolist (address to)
    (restart-case 
397
398
        (smtp-command stream (format nil "RCPT TO:<~A>" 
                                     (substitute-return-newline address))
Jan Idzikowski's avatar
Jan Idzikowski committed
399
400
401
402
403
404
405
406
407
408
409
                      250
                      :condition-class 'rcpt-failed
                      :condition-arguments (list :recipient address))
      (ignore-recipient ())))
  (smtp-command stream "DATA"
                354))

(defun finish-smtp-mail (stream)
  "Finish sending an email to the SMTP server connected to on STREAM.
   The server is expected to be inside of the DATA SMTP command.  The
   connection is then terminated by sending a QUIT command."
410
  ;;(fresh-line stream)
411
  (write-to-smtp stream "")
Jan Idzikowski's avatar
Jan Idzikowski committed
412
413
414
415
416
  (smtp-command stream "." 250)
  (smtp-command stream "QUIT" 221))

(defun send-mail-headers (stream 
			  &key from to cc reply-to 
417
418
			  extra-headers display-name subject 
                          (external-format :utf-8))
Jan Idzikowski's avatar
Jan Idzikowski committed
419
420
421
  "Send email headers according to the given arguments to the SMTP
   server connected to on STREAM.  The server is expected to have
   previously accepted the DATA SMTP command."
422
  (write-to-smtp stream (format nil "Date: ~A" (get-email-date-string)))
423
424
  (if display-name
      (write-to-smtp stream (format nil "From: ~A <~A>" 
425
426
427
                                    (rfc2045-q-encode-string 
                                     display-name :external-format external-format)
                                    from))
428
      (write-to-smtp stream (format nil "From: ~A" from)))
429
  (write-to-smtp stream (format nil "To: ~{ ~a~^,~}" to))
430
  (when cc
431
    (write-to-smtp stream (format nil "Cc: ~{ ~a~^,~}" cc)))
432
433
434
  (write-to-smtp stream (format nil "Subject: ~A" 
                                (rfc2045-q-encode-string 
                                 subject :external-format external-format)))
435
  (write-to-smtp stream (format nil "X-Mailer: cl-smtp~A" 
436
437
				(rfc2045-q-encode-string 
                                 *x-mailer* :external-format external-format)))
438
  (when reply-to
439
440
441
    (write-to-smtp stream (format nil "Reply-To: ~A" 
                                  (rfc2045-q-encode-string 
                                   reply-to :external-format external-format))))
442
443
444
  (when (and extra-headers
	     (listp extra-headers))
    (dolist (l extra-headers)
445
      (write-to-smtp stream 
446
		     (format nil "~A: ~{~a~^,~}" (car l) (rest l)))))
447
  (write-to-smtp stream "Mime-Version: 1.0"))
448

449
(defun send-multipart-headers (stream &key attachment-boundary html-boundary)
450
  (cond (attachment-boundary
451
	 (generate-multipart-header stream attachment-boundary 
452
453
				    :multipart-type "mixed"))
	(html-boundary (generate-multipart-header 
454
			stream html-boundary 
455
456
			:multipart-type "alternative"))
	(t nil)))
Jan Idzikowski's avatar
Jan Idzikowski committed
457

458
(defun write-to-smtp (stream command)
Jan Idzikowski's avatar
Jan Idzikowski committed
459
  (print-debug (format nil "to server: ~A" command)) 
460
  (write-sequence command stream)
461
462
463
  (write-char #\Return stream)
  (write-char #\NewLine stream)
  (force-output stream))
Jan Idzikowski's avatar
Jan Idzikowski committed
464

465
466
467
468
(defun write-blank-line (stream)
  (write-char #\Return stream)
  (write-char #\NewLine stream)
  (force-output stream))
469

470
471
472
(defun read-from-smtp (stream &optional lines)
  (let* ((line (read-line stream))
	 (response (string-trim '(#\Return #\NewLine) (subseq line 4)))
Jan Idzikowski's avatar
Jan Idzikowski committed
473
474
475
	 (response-code (parse-integer line :start 0 :junk-allowed t)))
    (print-debug (format nil "from server: ~A" line))
    (if (= (char-code (elt line 3)) (char-code #\-))
476
	(read-from-smtp stream (append lines (list response)))
Jan Idzikowski's avatar
Jan Idzikowski committed
477
	(values response-code response lines))))
478

Jan Idzikowski's avatar
Jan Idzikowski committed
479
480
481
482
483
484
485
486
487
(defun get-email-date-string ()
  (multiple-value-bind (sec min h d m y wd) (get-decoded-time)
    (let* ((month (elt '("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec") (- m 1)))
	   (weekday (elt '("Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun") wd))
	   (timezone (get-timezone-from-integer
		      (- (encode-universal-time sec min h d m y 0)
			 (get-universal-time)))))
      (format nil "~A, ~2,'0d ~A ~d ~2,'0d:~2,'0d:~2,'0d ~D" 
	      weekday d month y h min sec timezone))))
488

Jan Idzikowski's avatar
Jan Idzikowski committed
489
490
491
492
(defun get-timezone-from-integer (x)
  (let ((min (/ x 60))
	(hour (/ x 3600)))
    (if (integerp hour)
Jan Idzikowski's avatar
Jan Idzikowski committed
493
        (cond
Jan Idzikowski's avatar
Jan Idzikowski committed
494
495
496
	  ((>= hour 0)
	   (format nil "+~2,'0d00" hour))
	  ((< hour 0)
Jan Idzikowski's avatar
Jan Idzikowski committed
497
498
499
          (format nil "-~2,'0d00" (* -1 hour))))
        (multiple-value-bind (h m) (truncate min 60)
          (cond
Jan Idzikowski's avatar
Jan Idzikowski committed
500
501
502
503
	  ((>= hour 0)
	   (format nil "+~2,'0d~2,'0d" h (truncate m)))
	  ((< hour 0)
	   (format nil "-~2,'0d~2,'0d" (* -1 h) (* -1 (truncate m)))))))))