lisp/gnus.el: Change version number
[elisp/gnus.git-] / lisp / smtp.el
1 ;;; smtp.el --- basic functions to send mail with SMTP server
2
3 ;; Copyright (C) 1995, 1996, 1998 Free Software Foundation, Inc.
4
5 ;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp>
6 ;; ESMTP support: Simon Leinen <simon@switch.ch>
7 ;; Keywords: SMTP, mail
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'mail-utils) ; pick up mail-strip-quoted-names
29
30 (defgroup smtp nil
31   "SMTP protocol for sending mail."
32   :group 'mail)
33
34 (defcustom smtp-default-server nil
35   "*Specify default SMTP server."
36   :type '(choice (const nil) string)
37   :group 'smtp)
38
39 (defcustom smtp-server (or (getenv "SMTPSERVER") smtp-default-server)
40   "*The name of the host running SMTP server."
41   :type '(choice (const nil) string)
42   :group 'smtp)
43
44 (defcustom smtp-service "smtp"
45   "*SMTP service port number. \"smtp\" or 25."
46   :type '(choice (integer :tag "25" 25)
47                  (string :tag "smtp" "smtp"))
48   :group 'smtp)
49
50 (defcustom smtp-use-8bitmime t
51   "*If non-nil, use ESMTP 8BITMIME if available."
52   :type 'boolean
53   :group 'smtp)
54
55 (defcustom smtp-local-domain nil
56   "*Local domain name without a host name.
57 If the function (system-name) returns the full internet address,
58 don't define this value."
59   :type '(choice (const nil) string)
60   :group 'smtp)
61
62 (defcustom smtp-coding-system 'binary
63   "*Coding-system for SMTP output."
64   :type 'coding-system
65   :group 'smtp)
66
67 (defvar smtp-debug-info nil)
68 (defvar smtp-read-point nil)
69
70 (defun smtp-make-fqdn ()
71   "Return user's fully qualified domain name."
72   (let ((system-name (system-name)))
73     (cond
74      (smtp-local-domain
75       (concat system-name "." smtp-local-domain))
76      ((string-match "[^.]\\.[^.]" system-name)
77       system-name)
78      (t
79       (error "Cannot generate valid FQDN. Set `smtp-local-domain' correctly.")))))
80
81 (defun smtp-via-smtp (sender recipients smtp-text-buffer)
82   (let ((coding-system-for-read smtp-coding-system)
83         (coding-system-for-write smtp-coding-system)
84         process response extensions)
85     (save-excursion
86       (set-buffer
87        (get-buffer-create
88         (format "*trace of SMTP session to %s*" smtp-server)))
89       (erase-buffer)
90       (make-local-variable 'smtp-read-point)
91       (setq smtp-read-point (point-min))
92
93       (unwind-protect
94           (catch 'done
95             (setq process (open-network-stream "SMTP"
96                                                (current-buffer)
97                                                smtp-server smtp-service))
98             (or process (throw 'done nil))
99
100             (set-process-filter process 'smtp-process-filter)
101
102             ;; Greeting
103             (setq response (smtp-read-response process))
104             (if (or (null (car response))
105                     (not (integerp (car response)))
106                     (>= (car response) 400))
107                 (throw 'done (car (cdr response))))
108
109             ;; EHLO
110             (smtp-send-command process
111                                (format "EHLO %s" (smtp-make-fqdn)))
112             (setq response (smtp-read-response process))
113             (if (or (null (car response))
114                     (not (integerp (car response)))
115                     (>= (car response) 400))
116                 (progn
117                   ;; HELO
118                   (smtp-send-command process
119                                      (format "HELO %s" (smtp-make-fqdn)))
120                   (setq response (smtp-read-response process))
121                   (if (or (null (car response))
122                           (not (integerp (car response)))
123                           (>= (car response) 400))
124                       (throw 'done (car (cdr response)))))
125               (let ((extension-lines (cdr (cdr response))))
126                 (while extension-lines
127                   (push (intern (downcase (substring (car extension-lines) 4)))
128                         extensions)
129                   (setq extension-lines (cdr extension-lines)))))
130
131             ;; ONEX --- One message transaction only (sendmail extension?)
132             (if (or (memq 'onex extensions)
133                     (memq 'xone extensions))
134                 (progn
135                   (smtp-send-command process "ONEX")
136                   (setq response (smtp-read-response process))
137                   (if (or (null (car response))
138                           (not (integerp (car response)))
139                           (>= (car response) 400))
140                       (throw 'done (car (cdr response))))))
141
142             ;; VERB --- Verbose (sendmail extension?)
143             (if (and smtp-debug-info
144                      (or (memq 'verb extensions)
145                          (memq 'xvrb extensions)))
146                 (progn
147                   (smtp-send-command process "VERB")
148                   (setq response (smtp-read-response process))
149                   (if (or (null (car response))
150                           (not (integerp (car response)))
151                           (>= (car response) 400))
152                       (throw 'done (car (cdr response))))))
153
154             ;; XUSR --- Initial (user) submission (sendmail extension?)
155             (if (memq 'xusr extensions)
156                 (progn
157                   (smtp-send-command process "XUSR")
158                   (setq response (smtp-read-response process))
159                   (if (or (null (car response))
160                           (not (integerp (car response)))
161                           (>= (car response) 400))
162                       (throw 'done (car (cdr response))))))
163
164             ;; MAIL FROM:<sender>
165             (smtp-send-command
166              process
167              (format "MAIL FROM:<%s>%s%s"
168                      sender
169                      ;; SIZE --- Message Size Declaration (RFC1870)
170                      (if (memq 'size extensions)
171                          (format " SIZE=%d"
172                                  (save-excursion
173                                    (set-buffer smtp-text-buffer)
174                                    (+ (- (point-max) (point-min))
175                                       ;; Add one byte for each change-of-line
176                                       ;; because or CR-LF representation:
177                                       (count-lines (point-min) (point-max))
178                                       ;; For some reason, an empty line is
179                                       ;; added to the message.  Maybe this
180                                       ;; is a bug, but it can't hurt to add
181                                       ;; those two bytes anyway:
182                                       2)))
183                        "")
184                      ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
185                      (if (and (memq '8bitmime extensions)
186                               smtp-use-8bitmime)
187                          " BODY=8BITMIME"
188                        "")))
189             (setq response (smtp-read-response process))
190             (if (or (null (car response))
191                     (not (integerp (car response)))
192                     (>= (car response) 400))
193                 (throw 'done (car (cdr response))))
194         
195             ;; RCPT TO:<recipient>
196             (while recipients
197               (smtp-send-command process
198                                  (format "RCPT TO:<%s>" (car recipients)))
199               (setq recipients (cdr recipients))
200               (setq response (smtp-read-response process))
201               (if (or (null (car response))
202                       (not (integerp (car response)))
203                       (>= (car response) 400))
204                   (throw 'done (car (cdr response)))))
205         
206             ;; DATA
207             (smtp-send-command process "DATA")
208             (setq response (smtp-read-response process))
209             (if (or (null (car response))
210                     (not (integerp (car response)))
211                     (>= (car response) 400))
212                 (throw 'done (car (cdr response))))
213
214             ;; Mail contents
215             (smtp-send-data process smtp-text-buffer)
216
217             ;; DATA end "."
218             (smtp-send-command process ".")
219             (setq response (smtp-read-response process))
220             (if (or (null (car response))
221                     (not (integerp (car response)))
222                     (>= (car response) 400))
223                 (throw 'done (car (cdr response))))
224
225             t)
226
227         (if (and process
228                  (eq (process-status process) 'open))
229             (progn
230               ;; QUIT
231               (smtp-send-command process "QUIT")
232               (smtp-read-response process)
233               (delete-process process)))))))
234
235 (defun smtp-process-filter (process output)
236   (save-excursion
237     (set-buffer (process-buffer process))
238     (goto-char (point-max))
239     (insert output)))
240
241 (defun smtp-read-response (process)
242   (let ((case-fold-search nil)
243         (response-strings nil)
244         (response-continue t)
245         (return-value '(nil ()))
246         match-end)
247
248     (while response-continue
249       (goto-char smtp-read-point)
250       (while (not (search-forward "\r\n" nil t))
251         (accept-process-output process)
252         (goto-char smtp-read-point))
253
254       (setq match-end (point))
255       (setq response-strings
256             (cons (buffer-substring smtp-read-point (- match-end 2))
257                   response-strings))
258         
259       (goto-char smtp-read-point)
260       (if (looking-at "[0-9]+ ")
261           (let ((begin (match-beginning 0))
262                 (end (match-end 0)))
263             (if smtp-debug-info
264                 (message "%s" (car response-strings)))
265
266             (setq smtp-read-point match-end)
267
268             ;; ignore lines that start with "0"
269             (if (looking-at "0[0-9]+ ")
270                 nil
271               (setq response-continue nil)
272               (setq return-value
273                     (cons (string-to-int
274                            (buffer-substring begin end))
275                           (nreverse response-strings)))))
276         
277         (if (looking-at "[0-9]+-")
278             (progn (if smtp-debug-info
279                      (message "%s" (car response-strings)))
280                    (setq smtp-read-point match-end)
281                    (setq response-continue t))
282           (progn
283             (setq smtp-read-point match-end)
284             (setq response-continue nil)
285             (setq return-value
286                   (cons nil (nreverse response-strings)))))))
287     (setq smtp-read-point match-end)
288     return-value))
289
290 (defun smtp-send-command (process command)
291   (goto-char (point-max))
292   (insert command "\r\n")
293   (setq smtp-read-point (point))
294   (process-send-string process command)
295   (process-send-string process "\r\n"))
296
297 (defun smtp-send-data-1 (process data)
298   (goto-char (point-max))
299   (if smtp-debug-info
300       (insert data "\r\n"))
301   (setq smtp-read-point (point))
302   ;; Escape "." at start of a line.
303   (if (eq (string-to-char data) ?.)
304       (process-send-string process "."))
305   (process-send-string process data)
306   (process-send-string process "\r\n"))
307
308 (defun smtp-send-data (process buffer)
309   (let ((data-continue t)
310         (sending-data nil)
311         this-line
312         this-line-end)
313
314     (save-excursion
315       (set-buffer buffer)
316       (goto-char (point-min)))
317
318     (while data-continue
319       (save-excursion
320         (set-buffer buffer)
321         (beginning-of-line)
322         (setq this-line (point))
323         (end-of-line)
324         (setq this-line-end (point))
325         (setq sending-data nil)
326         (setq sending-data (buffer-substring this-line this-line-end))
327         (if (or (/= (forward-line 1) 0) (eobp))
328             (setq data-continue nil)))
329
330       (smtp-send-data-1 process sending-data))))
331
332 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
333   "Get address list suitable for smtp RCPT TO:<address>."
334   (let ((case-fold-search t)
335         (simple-address-list "")
336         this-line
337         this-line-end
338         addr-regexp
339         (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
340     (unwind-protect
341         (save-excursion
342           ;;
343           (set-buffer smtp-address-buffer)
344           (erase-buffer)
345           (insert (save-excursion
346                     (set-buffer smtp-text-buffer)
347                     (buffer-substring-no-properties header-start header-end)))
348           (goto-char (point-min))
349           ;; RESENT-* fields should stop processing of regular fields.
350           (save-excursion
351             (if (re-search-forward "^RESENT-TO:" header-end t)
352                 (setq addr-regexp
353                       "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
354               (setq addr-regexp  "^\\(TO:\\|CC:\\|BCC:\\)")))
355
356           (while (re-search-forward addr-regexp header-end t)
357             (replace-match "")
358             (setq this-line (match-beginning 0))
359             (forward-line 1)
360             ;; get any continuation lines.
361             (while (and (looking-at "^[ \t]+") (< (point) header-end))
362               (forward-line 1))
363             (setq this-line-end (point-marker))
364             (setq simple-address-list
365                   (concat simple-address-list " "
366                           (mail-strip-quoted-names
367                            (buffer-substring this-line this-line-end)))))
368           (erase-buffer)
369           (insert-string " ")
370           (insert-string simple-address-list)
371           (insert-string "\n")
372           ;; newline --> blank
373           (subst-char-in-region (point-min) (point-max) 10 ?  t)
374           ;; comma   --> blank
375           (subst-char-in-region (point-min) (point-max) ?, ?  t)
376           ;; tab     --> blank
377           (subst-char-in-region (point-min) (point-max)  9 ?  t)
378
379           (goto-char (point-min))
380           ;; tidyness in case hook is not robust when it looks at this
381           (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
382
383           (goto-char (point-min))
384           (let (recipient-address-list)
385             (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
386               (backward-char 1)
387               (setq recipient-address-list
388                     (cons (buffer-substring (match-beginning 1) (match-end 1))
389                           recipient-address-list)))
390             recipient-address-list))
391       (kill-buffer smtp-address-buffer))))
392
393 (provide 'smtp)
394
395 ;;; smtp.el ends here