Debian Codegraph @

mew-nntp2.el

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 ;;; mew-nntp2.el for posting
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: Nov 19, 1999
5
6 ;;; Code:
7
8 (require 'mew)
9
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 ;;;
12 ;;; NNTP2 info
13 ;;;
14
15 (defvar mew-nntp2-info-list
16 '(;; parameters to be saved
17 "raw-header" "newsgroups" "fcc" "msgid" "logtime"
18 "case" ;; save for re-edit, not for sending
19 ;; parameters used internally
20 "server" "port" "ssh-server" "user" "account"
21 "status" "process" "ssh-process" "ssl-process"
22 "qfld" "messages"
23 ;; parameters used internally and should be initialized
24 "string" "error" "done" "imapp"))
25
26 (mew-info-defun "mew-nntp2-" mew-nntp2-info-list)
27
28 (defvar mew-nntp2-info-list-save-length 6)
29 (defvar mew-nntp2-info-list-clean-length 16)
30
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 ;;;
33 ;;; FSM
34 ;;;
35
36 (defvar mew-nntp2-fsm
37 '(("greeting" ("20[01]" . "mode-reader")) ;; a broken server returns 200 even if post is allowed
38 ("mode-reader" (t . "authinfo"))
39 ("authinfo" ("381" . "authpass"))
40 ("authpass" ("281" . "next") (t . "wpwd"))
41 ("post" ("340" . "post-post"))
42 ("post-post" ("240" . "done"))
43 ("quit" (t . "noop"))))
44
45 (defun mew-nntp2-fsm-by-status (status)
46 (assoc status mew-nntp2-fsm))
47
48 (defun mew-nntp2-fsm-next (status code)
49 (cdr (mew-assoc-match2 code (nthcdr 1 (mew-nntp2-fsm-by-status status)) 0)))
50
51 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52 ;;;
53 ;;; Filters 2
54 ;;;
55
56 (defun mew-nntp2-command-mode-reader (pro _pnm)
57 (mew-nntp2-process-send-string pro "MODE READER"))
58
59 (defun mew-nntp2-command-authinfo (pro pnm)
60 (let ((user (mew-nntp2-get-user pnm)))
61 (if user
62 (mew-nntp2-process-send-string pro "AUTHINFO USER %s" user)
63 (mew-nntp2-set-status pnm "next")
64 (mew-nntp2-command-next pro pnm))))
65
66 (defun mew-nntp2-command-authpass (pro pnm)
67 (let* ((prompt (format "NNTP password (%s): " (mew-nntp2-get-account pnm)))
68 (pass (mew-nntp2-input-passwd prompt pnm)))
69 (mew-nntp2-process-send-string pro "AUTHINFO PASS %s" pass)))
70
71 (defun mew-nntp2-command-wpwd (pro pnm)
72 (mew-passwd-set-passwd (mew-nntp2-passtag pnm) nil)
73 (mew-nntp2-set-error pnm "NNTP password is wrong!")
74 (mew-nntp2-command-quit2 pro pnm))
75
76 (defun mew-nntp2-command-next (pro pnm)
77 (let ((msgs (mew-nntp2-get-messages pnm))
78 (qfld (mew-nntp2-get-qfld pnm))
79 (case (mew-nntp2-get-case pnm))
80 msg)
81 (if msgs
82 (progn
83 (setq msg (car msgs))
84 (setq msgs (cdr msgs))
85 (mew-queue-insert-file pnm mew-nntp2-info-list-save-length qfld msg)
86 (mew-set-buffer-multibyte nil)
87 (mew-info-clean-up pnm mew-nntp2-info-list-clean-length)
88 (mew-nntp2-set-case pnm case) ;; override
89 (mew-nntp2-set-messages pnm msgs)
90 (set-process-buffer pro (current-buffer))
91 (mew-nntp2-set-status pnm "post")
92 (mew-nntp2-command-post pro pnm))
93 (mew-nntp2-set-status pnm "quit")
94 (mew-nntp2-command-quit pro pnm))))
95
96 (defun mew-nntp2-command-post (pro _pnm)
97 (mew-nntp2-process-send-string pro "POST"))
98
99 (defun mew-nntp2-command-post-post (pro _pnm)
100 (goto-char (point-max))
101 (unless (bolp) (insert "\n"))
102 (mew-dot-insert)
103 (mew-eol-fix-for-write)
104 (set-buffer-modified-p nil)
105 (process-send-region pro (point-min) (point-max))
106 (mew-nntp2-process-send-string pro "."))
107
108 (defun mew-nntp2-command-done (pro pnm)
109 (let ((fcc (mew-nntp2-get-fcc pnm))
110 (case (mew-nntp2-get-case pnm))
111 (back (mew-queue-backup (buffer-file-name) mew-queue-info-suffix))
112 imapp)
113 ;; mew-folder-new-message may be slow if the folder contains
114 ;; a lot of messages. So, let's Fcc in background.
115 (setq imapp (mew-net-fcc-message case fcc back))
116 (mew-nntp2-set-imapp pnm imapp)
117 (mew-nntp2-log pnm)
118 (mew-nntp2-set-status pro "next")
119 (mew-nntp2-command-next pro pnm)))
120
121 (defun mew-nntp2-command-quit (pro pnm)
122 (mew-nntp2-set-done pnm t)
123 (mew-nntp2-process-send-string pro "QUIT"))
124
125 (defun mew-nntp2-command-quit2 (pro pnm)
126 ;; error is set
127 (mew-nntp2-set-done pnm t)
128 (when (and (processp pro) (eq (process-status pro) 'open))
129 (mew-nntp2-set-status pnm "quit")
130 (mew-nntp2-process-send-string pro "QUIT")))
131
132 (defun mew-nntp2-command-noop (_pro _pnm)
133 ())
134
135 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
136 ;;;
137 ;;; Sub functions
138 ;;;
139
140 (defconst mew-nntp2-info-prefix "mew-nntp2-info-")
141
142 (defun mew-nntp2-info-name (case)
143 (let ((server (mew-nntp-server case))
144 (port (mew-*-to-string (mew-nntp-port case)))
145 (sshsrv (mew-nntp-ssh-server case))
146 (name mew-nntp2-info-prefix))
147 (setq name (concat name server))
148 (unless (mew-port-equal port mew-nntp-port)
149 (setq name (concat name ":" port)))
150 (if sshsrv
151 (concat name "%" sshsrv)
152 name)))
153
154 (defun mew-nntp2-process-send-string (pro &rest args)
155 (let ((str (apply 'format args)))
156 (mew-nntp2-debug "=SEND=" str)
157 (if (and (processp pro) (eq (process-status pro) 'open))
158 (process-send-string pro (concat str mew-cs-eol))
159 (message "NNTP time out"))))
160
161 (defun mew-nntp2-passtag (pnm)
162 (let ((server (mew-nntp2-get-server pnm))
163 (port (mew-nntp2-get-port pnm))
164 (user (mew-nntp2-get-user pnm)))
165 (concat user "@" server ":" port)))
166
167 (defun mew-nntp2-input-passwd (prompt pnm)
168 (let* ((tag (mew-nntp2-passtag pnm))
169 (pro (mew-nntp2-get-process pnm))
170 (pass (mew-input-passwd prompt tag)))
171 (unless (and (processp pro) (eq (process-status pro) 'open))
172 (mew-passwd-set-passwd tag nil))
173 pass))
174
175 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 ;;;
177 ;;; Opening NNTP
178 ;;;
179
180 (defun mew-nntp2-open (pnm server port)
181 (let ((sprt (mew-*-to-port port))
182 pro tm)
183 (condition-case emsg
184 (progn
185 (setq tm (run-at-time mew-nntp-timeout-time nil 'mew-nntp2-timeout))
186 (message "Connecting to the NNTP server...")
187 (setq pro (open-network-stream pnm nil server sprt))
188 (mew-process-silent-exit pro)
189 (mew-set-process-cs pro mew-cs-text-for-net mew-cs-text-for-net)
190 (message "Connecting to the NNTP server...done"))
191 (quit
192 (setq pro nil)
193 (message "Cannot connect to the NNTP server"))
194 (error
195 (setq pro nil)
196 (message "%s, %s" (nth 1 emsg) (nth 2 emsg))))
197 (if tm (cancel-timer tm))
198 pro))
199
200 (defun mew-nntp2-timeout ()
201 (signal 'quit nil))
202
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204 ;;;
205 ;;; Launcher
206 ;;;
207
208 (defun mew-nntp2-send-message (case qfld msgs)
209 (let ((server (mew-nntp-server case))
210 (user (mew-nntp-user case))
211 (port (mew-*-to-string (mew-nntp-port case)))
212 (pnm (mew-nntp2-info-name case))
213 (sshsrv (mew-nntp-ssh-server case))
214 (sslp (mew-nntp-ssl case))
215 (sslport (mew-nntp-ssl-port case))
216 process sshname sshpro sslname sslpro lport tls)
217 (cond
218 (sshsrv
219 (setq sshpro (mew-open-ssh-stream case server port sshsrv))
220 (when sshpro
221 (setq sshname (process-name sshpro))
222 (setq lport (mew-ssh-pnm-to-lport sshname))
223 (when lport
224 (setq process (mew-nntp2-open pnm "localhost" lport)))))
225 (sslp
226 (if (mew-port-equal port sslport) (setq tls mew-tls-nntp))
227 (setq sslpro (mew-open-ssl-stream case server sslport tls))
228 (when sslpro
229 (setq sslname (process-name sslpro))
230 (setq lport (mew-ssl-pnm-to-lport sslname))
231 (when lport
232 (setq process (mew-nntp2-open pnm mew-ssl-localhost lport)))))
233 (t
234 (setq process (mew-nntp2-open pnm server port))))
235 (if (null process)
236 (cond
237 ((and sshsrv (null sshpro))
238 (message "Cannot create to the SSH connection"))
239 ((and sslp (null sslpro))
240 (message "Cannot create to the SSL/TLS connection"))
241 (t
242 (message "Cannot connect to the NNTP server")))
243 (mew-info-clean-up pnm mew-nntp2-info-list-clean-length)
244 (mew-nntp2-set-case pnm case)
245 (mew-nntp2-set-qfld pnm qfld)
246 (mew-nntp2-set-messages pnm msgs)
247 (mew-nntp2-set-server pnm server)
248 (mew-nntp2-set-port pnm port)
249 (mew-nntp2-set-ssh-server pnm sshsrv)
250 (mew-nntp2-set-ssh-process pnm sshpro)
251 (mew-nntp2-set-ssl-process pnm sslpro)
252 (mew-nntp2-set-user pnm user)
253 (mew-nntp2-set-account pnm (format "%s@%s" user server))
254 (mew-nntp2-set-status pnm "greeting")
255 ;;
256 (set-process-buffer process nil)
257 (set-process-sentinel process 'mew-nntp2-sentinel)
258 (set-process-filter process 'mew-nntp2-filter)
259 (message "Posting in background..."))))
260
261 (defun mew-nntp2-flush-queue (case &optional qfld)
262 (let (msgs)
263 (unless qfld (setq qfld (mew-postq-folder case)))
264 (if (mew-nntp2-get-server (mew-nntp2-info-name case)) ;; lock
265 (message "%s is locked" qfld)
266 (setq msgs (mew-folder-messages qfld))
267 (when msgs
268 (mew-summary-folder-cache-clean qfld)
269 (run-hooks 'mew-nntp2-flush-hook)
270 (message "Flushing %s..." qfld)
271 (mew-nntp2-send-message case qfld msgs)))))
272
273 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
274 ;;;
275 ;;; Filter and sentinel 2
276 ;;;
277
278 (defun mew-nntp2-debug (label string)
279 (when (mew-debug 'net)
280 (with-current-buffer (get-buffer-create mew-buffer-debug)
281 (goto-char (point-max))
282 (insert (format "\n<%s>\n%s\n" label string)))))
283
284 (defun mew-nntp2-filter (process string)
285 (let* ((pnm (process-name process))
286 (status (mew-nntp2-get-status pnm))
287 (str (concat (mew-nntp2-get-string pnm) string))
288 (buf (process-buffer process))
289 next func code)
290 (save-excursion
291 (mew-nntp2-debug (upcase status) string)
292 (if (and buf (get-buffer buf)) (set-buffer buf))
293 ;; NNTP server's strings should be short enough.
294 (mew-nntp2-set-string pnm str)
295 (cond
296 ((and (string-match "\n$" str)
297 (string-match "^\\([0-9][0-9][0-9]\\) " str))
298 (setq code (match-string 1 str))
299 (setq next (mew-nntp2-fsm-next status code))
300 (cond
301 (next
302 (mew-nntp2-set-status pnm next)
303 (setq func (intern-soft (concat "mew-nntp2-command-" next)))
304 (and func (funcall func process pnm))
305 (mew-nntp2-set-string pnm nil))
306 (t
307 (if (string-match "^pwd-" status)
308 (mew-nntp2-set-error pnm "NNTP password is wrong!")
309 (if (string-match "\n$" str)
310 (setq str (substring str 0 -1)))
311 (unless (string-match "[.!]$" str)
312 (setq str (concat str "."))))
313 (mew-nntp2-set-error pnm str)
314 (mew-nntp2-command-quit2 process pnm))))
315 (t ()))))) ;; stay
316
317 (defun mew-nntp2-sentinel (process event)
318 (let* ((pnm (process-name process))
319 (buf (process-buffer process))
320 (qfld (mew-nntp2-get-qfld pnm))
321 (case (mew-nntp2-get-case pnm))
322 (done (mew-nntp2-get-done pnm))
323 (imapp (mew-nntp2-get-imapp pnm))
324 (error (mew-nntp2-get-error pnm))
325 (sshpro (mew-nntp2-get-ssh-process pnm))
326 (sslpro (mew-nntp2-get-ssl-process pnm)))
327 (save-excursion
328 (mew-nntp2-debug "NNTP SENTINEL" event)
329 (cond
330 (error
331 (when buf
332 ;; A message file is not inserted at the beginning of the NNTP
333 ;; session.
334 (set-buffer buf)
335 (mew-nntp2-queue case error))
336 (mew-nntp2-log pnm error)
337 (if (memq system-type '(windows-nt ms-dos cygwin))
338 (message (format "%s This mail has been queued to %s" error qfld))
339 (message-box (format "%s This mail has been queued to %s" error qfld))))
340 (done
341 (message "Posting in background...done"))
342 (t
343 (if (null buf)
344 (message "NNTP connection is lost")
345 (set-buffer buf)
346 (mew-nntp2-queue case "NNTP connection is lost"))))
347 (mew-info-clean-up pnm)
348 (if (and (processp sshpro) (not mew-ssh-keep-connection))
349 (process-send-string sshpro "exit\n"))
350 (if (and (processp sslpro) (not mew-ssl-keep-connection))
351 (delete-process sslpro))
352 (run-hooks 'mew-nntp2-sentinel-hook)
353 (if imapp (mew-imap2-fcc case))
354 (when buf (mew-remove-buffer buf)))))
355
356 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
357 ;;;
358 ;;; Queuing
359 ;;;
360
361 (defun mew-nntp2-queue (case err)
362 ;; Must be in a buffer where a message is contained.
363 (let* ((pnm (mew-nntp2-info-name case))
364 (qfld (mew-postq-folder case))
365 (oname (buffer-name))
366 (work (buffer-file-name))
367 file-info file info nname)
368 (mew-local-folder-check qfld)
369 (setq file-info (mew-queue-enqueue work qfld))
370 (mew-set '(file info) file-info)
371 (setq file (file-name-nondirectory file))
372 (setq nname (mew-concat-folder qfld file))
373 (if (mew-draft-p)
374 (mew-nntp2-set-case pnm (mew-tinfo-get-case)))
375 ;;
376 (let* ((n mew-nntp2-info-list-save-length)
377 (data (make-vector n nil)))
378 (dotimes (i n)
379 (aset data i (aref (mew-info pnm) i)))
380 (mew-lisp-save info data))
381 ;;
382 (mew-remove-buffer (current-buffer))
383 (message "%s has been queued to %s (%s)" oname nname err)
384 (mew-touch-folder qfld)
385 (file-name-sans-extension file)))
386
387 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
388 ;;;
389 ;;; Logging
390 ;;;
391
392 (defun mew-nntp2-log (pnm &optional err)
393 (let ((logtime (mew-nntp2-get-logtime pnm))
394 (msgid (mew-nntp2-get-msgid pnm))
395 (newsgroups (mew-nntp2-get-newsgroups pnm))
396 (server (mew-nntp2-get-server pnm))
397 (sshsrv (mew-nntp2-get-ssh-server pnm))
398 (sslp (mew-nntp2-get-ssl-process pnm)))
399 (with-temp-buffer
400 (and logtime (insert logtime))
401 (and msgid (insert " id=" msgid))
402 (and server (insert " server=" server))
403 (and sshsrv (insert " sshsrv=" sshsrv))
404 (and sslp (insert " SSL/TLS"))
405 (and newsgroups (insert " newsgroups=" newsgroups))
406 (if err
407 (insert " status=" "("
408 (substring err 0 (string-match "\n+$" err))
409 ")")
410 (insert " status=sent"))
411 (insert "\n")
412 (write-region (point-min) (point-max)
413 (expand-file-name mew-nntp-log-file mew-conf-path)
414 'append 'no-msg))))
415
416 (provide 'mew-nntp2)
417
418 ;;; Copyright Notice:
419
420 ;; Copyright (C) 1999-2015 Mew developing team.
421 ;; All rights reserved.
422
423 ;; Redistribution and use in source and binary forms, with or without
424 ;; modification, are permitted provided that the following conditions
425 ;; are met:
426 ;;
427 ;; 1. Redistributions of source code must retain the above copyright
428 ;; notice, this list of conditions and the following disclaimer.
429 ;; 2. Redistributions in binary form must reproduce the above copyright
430 ;; notice, this list of conditions and the following disclaimer in the
431 ;; documentation and/or other materials provided with the distribution.
432 ;; 3. Neither the name of the team nor the names of its contributors
433 ;; may be used to endorse or promote products derived from this software
434 ;; without specific prior written permission.
435 ;;
436 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
437 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
438 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
439 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
440 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
441 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
442 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
443 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
444 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
445 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
446 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
447
448 ;;; mew-nntp2.el ends here