Rearrange header.
[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-via-smtp 'qmtp-via-qmtp)
33
34 ;;; Code:
35
36 (require 'poem)
37 (require 'pcustom)
38
39 (defgroup qmtp nil
40   "QMTP protocol for sending mail."
41   :group 'mail)
42
43 (defcustom qmtp-default-server nil
44   "Specify default QMTP server."
45   :type '(choice (const nil) string)
46   :group 'qmtp)
47
48 (defvar qmtp-server qmtp-default-server
49   "The name of the host running QMTP server.
50 It can also be a function
51 called from `qmtp-via-qmtp' with arguments SENDER and RECIPIENTS.")
52
53 (defcustom qmtp-service "qmtp"
54   "QMTP service port number.  \"qmtp\" or 209."
55   :type '(choice (integer :tag "209" 209)
56                  (string :tag "qmtp" "qmtp"))
57   :group 'qmtp)
58
59 (defcustom qmtp-timeout 30
60   "Timeout for each QMTP session."
61   :type 'integer
62   :group 'qmtp)
63
64 (defvar qmtp-open-connection-function (function open-network-stream))
65
66 (defvar qmtp-error-response-alist
67   '((?Z "Temporary failure")
68     (?D "Permanent failure")))
69
70 (defvar qmtp-read-point nil)
71
72 (defun qmtp-encode-netstring-string (string)
73   (format "%d:%s," (length string) string))
74
75 (defun qmtp-send-package (process sender recipients buffer)
76   (with-temp-buffer
77     (buffer-disable-undo)
78     (erase-buffer)
79     (set-buffer-multibyte nil)
80     (insert
81      (format "%d:\n"
82              (with-current-buffer buffer
83                (1+ (point-max));; for the "\n"
84                )))
85     (insert-buffer-substring buffer)
86     (insert
87      "\n,"
88      (qmtp-encode-netstring-string sender)
89      (qmtp-encode-netstring-string
90       (mapconcat #'qmtp-encode-netstring-string
91                  recipients "")))
92     (process-send-region process (point-min)(point-max)))
93   (goto-char qmtp-read-point)
94   (while (and (memq (process-status process) '(open run))
95               (not (re-search-forward "^[0-9]+:" nil 'noerror)))
96     (unless (accept-process-output process qmtp-timeout)
97       (error "timeout expired: %d" qmtp-timeout))
98     (goto-char qmtp-read-point))
99   (let ((response (char-after (match-end 0))))
100     (unless (eq response ?K)
101       (error (nth 1 (assq response qmtp-error-response-alist))))
102     (setq recipients (cdr recipients))
103     (beginning-of-line 2)
104     (setq qmtp-read-point (point))))
105
106 ;;;###autoload
107 (defun qmtp-via-qmtp (sender recipients buffer)
108   (condition-case nil
109       (progn
110         (qmtp-send-buffer sender recipients buffer)
111         t)
112     (error)))
113
114 (make-obsolete 'qmtp-via-qmtp "It's old API.")
115
116 ;;;###autoload
117 (defun qmtp-send-buffer (sender recipients buffer)
118   (save-excursion
119     (set-buffer
120      (get-buffer-create
121       (format "*trace of QMTP session to %s*" qmtp-server)))
122     (buffer-disable-undo)
123     (erase-buffer)
124     (make-local-variable 'qmtp-read-point)
125     (setq qmtp-read-point (point-min))
126     (let (process)
127       (unwind-protect
128           (progn
129             (as-binary-process
130              (setq process
131                    (funcall qmtp-open-connection-function
132                             "QMTP" (current-buffer) qmtp-server qmtp-service)))
133             (qmtp-send-package process sender recipients buffer))
134         (when (and process
135                    (memq (process-status process) '(open run)))
136           ;; QUIT
137           (process-send-eof process)
138           (delete-process process))))))
139
140 (provide 'qmtp)
141
142 ;;; qmtp.el ends here