Don't require `poem'.
[elisp/flim.git] / qmtp.el
1 ;;; qmtp.el --- basic functions to send mail with QMTP server
2
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
4
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: QMTP, qmail
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25
26 ;;; Commentary:
27
28 ;; Installation:
29
30 ;; To send mail using QMTP instead of SMTP, do
31
32 ;; (fset 'smtp-send-buffer 'qmtp-send-buffer)
33
34 ;;; Code:
35
36 (require 'custom)
37
38 (defgroup qmtp nil
39   "QMTP protocol for sending mail."
40   :group 'mail)
41
42 (defcustom qmtp-default-server nil
43   "Specify default QMTP server."
44   :type '(choice (const nil) string)
45   :group 'qmtp)
46
47 (defvar qmtp-server qmtp-default-server
48   "The name of the host running QMTP server.
49 It can also be a function
50 called from `qmtp-via-qmtp' with arguments SENDER and RECIPIENTS.")
51
52 (defcustom qmtp-service "qmtp"
53   "QMTP service port number.  \"qmtp\" or 209."
54   :type '(choice (integer :tag "209" 209)
55                  (string :tag "qmtp" "qmtp"))
56   :group 'qmtp)
57
58 (defcustom qmtp-timeout 30
59   "Timeout for each QMTP session."
60   :type 'integer
61   :group 'qmtp)
62
63 (defvar qmtp-open-connection-function (function open-network-stream))
64
65 (defvar qmtp-error-response-alist
66   '((?Z "Temporary failure")
67     (?D "Permanent failure")))
68
69 (defvar qmtp-read-point nil)
70
71 (defun qmtp-encode-netstring-string (string)
72   (format "%d:%s," (length string) string))
73
74 (defun qmtp-send-package (process sender recipients buffer)
75   (with-temp-buffer
76     (buffer-disable-undo)
77     (erase-buffer)
78     (set-buffer-multibyte nil)
79     (insert
80      (format "%d:\n"
81              (with-current-buffer buffer
82                (1+ (point-max));; for the "\n"
83                )))
84     (insert-buffer-substring buffer)
85     (insert
86      "\n,"
87      (qmtp-encode-netstring-string sender)
88      (qmtp-encode-netstring-string
89       (mapconcat #'qmtp-encode-netstring-string
90                  recipients "")))
91     (process-send-region process (point-min)(point-max)))
92   (goto-char qmtp-read-point)
93   (while (and (memq (process-status process) '(open run))
94               (not (re-search-forward "^[0-9]+:" nil 'noerror)))
95     (unless (accept-process-output process qmtp-timeout)
96       (error "timeout expired: %d" qmtp-timeout))
97     (goto-char qmtp-read-point))
98   (let ((response (char-after (match-end 0))))
99     (unless (eq response ?K)
100       (error (nth 1 (assq response qmtp-error-response-alist))))
101     (setq recipients (cdr recipients))
102     (beginning-of-line 2)
103     (setq qmtp-read-point (point))))
104
105 ;;;###autoload
106 (defun qmtp-via-qmtp (sender recipients buffer)
107   (condition-case nil
108       (progn
109         (qmtp-send-buffer sender recipients buffer)
110         t)
111     (error)))
112
113 (make-obsolete 'qmtp-via-qmtp "It's old API.")
114
115 ;;;###autoload
116 (defun qmtp-send-buffer (sender recipients buffer)
117   (save-excursion
118     (set-buffer
119      (get-buffer-create
120       (format "*trace of QMTP session to %s*" qmtp-server)))
121     (buffer-disable-undo)
122     (erase-buffer)
123     (make-local-variable 'qmtp-read-point)
124     (setq qmtp-read-point (point-min))
125     (let (process)
126       (unwind-protect
127           (let ((coding-system-for-read  'binary)
128                 (coding-system-for-write 'binary))
129             (setq process
130                   (funcall qmtp-open-connection-function
131                            "QMTP" (current-buffer) qmtp-server qmtp-service))
132             (qmtp-send-package process sender recipients buffer))
133         (when (and process
134                    (memq (process-status process) '(open run)))
135           ;; QUIT
136           (process-send-eof process)
137           (delete-process process))))))
138
139 (provide 'qmtp)
140
141 ;;; qmtp.el ends here