Debian Codegraph @

mew-nntp2.el

raw ◀ ▶
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 case server port starttlsp)
181 (let ((sprt (mew-*-to-port port))
•182 (sslnp (mew-ssl-native-p (mew-nntp-ssl case)))
183 pro tm)
184 (condition-case emsg
185 (progn
186 (setq tm (run-at-time mew-nntp-timeout-time nil 'mew-nntp2-timeout))
187 (message "Connecting to the NNTP server...")
•188 (setq pro (mew-open-network-stream pnm nil server sprt
189 'nntp sslnp starttlsp case))
190 (setq pro (car pro))
191 (when (not (processp pro)) (signal 'quit nil))
192 (mew-process-silent-exit pro)
193 (mew-set-process-cs pro mew-cs-text-for-net mew-cs-text-for-net)
194 (message "Connecting to the NNTP server...done"))
195 (quit
196 (setq pro nil)
197 (message "Cannot connect to the NNTP server"))
198 (error
199 (setq pro nil)
200 (message "%s, %s" (nth 1 emsg) (nth 2 emsg))))
201 (if tm (cancel-timer tm))
202 pro))
203
204 (defun mew-nntp2-timeout ()
•205 ;; Do not timeout if the NSM query pane is active.
206 (unless (get-buffer "*Network Security Manager*")
207 (message "NNTP connection timed out (%d seconds)"
208 mew-nntp-timeout-time)
209 (signal 'quit nil)))
210
211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
212 ;;;
213 ;;; Launcher
214 ;;;
215
216 (defun mew-nntp2-send-message (case qfld msgs)
217 (let ((server (mew-nntp-server case))
218 (user (mew-nntp-user case))
219 (port (mew-*-to-string (mew-nntp-port case)))
220 (pnm (mew-nntp2-info-name case))
221 (sshsrv (mew-nntp-ssh-server case))
222 (sslp (mew-nntp-ssl case))
223 (sslport (mew-nntp-ssl-port case))
•224 (sslnp (mew-ssl-native-p (mew-nntp-ssl case)))
225 (starttlsp
226 (mew-ssl-starttls-p (mew-nntp-ssl case)
227 (mew-*-to-string (mew-nntp-port case))
228 (mew-nntp-ssl-port case)))
229 process sshname sshpro sslname sslpro lport tls)
230 (cond
•231 (sslnp
232 (let ((serv (if starttlsp port sslport)))
233 (setq process (mew-nntp2-open pnm case server serv starttlsp))))
234 (sshsrv
235 (setq sshpro (mew-open-ssh-stream case server port sshsrv))
236 (when sshpro
237 (setq sshname (process-name sshpro))
238 (setq lport (mew-ssh-pnm-to-lport sshname))
239 (when lport
•240 (setq process (mew-nntp2-open pnm case "localhost" lport nil)))))
241 (sslp
•242 (when starttlsp (setq tls mew-tls-nntp))
243 (setq sslpro (mew-open-ssl-stream case server sslport tls))
244 (when sslpro
245 (setq sslname (process-name sslpro))
246 (setq lport (mew-ssl-pnm-to-lport sslname))
247 (when lport
•248 (setq process (mew-nntp2-open pnm case mew-ssl-localhost lport nil)))))
249 (t
•250 (setq process (mew-nntp2-open pnm case server port nil))))
251 (if (null process)
252 (cond
253 ((and sshsrv (null sshpro))
254 (message "Cannot create to the SSH connection"))
255 ((and sslp (null sslpro))
256 (message "Cannot create to the SSL/TLS connection"))
257 (t
258 (message "Cannot connect to the NNTP server")))
259 (mew-info-clean-up pnm mew-nntp2-info-list-clean-length)
260 (mew-nntp2-set-case pnm case)
261 (mew-nntp2-set-qfld pnm qfld)
262 (mew-nntp2-set-messages pnm msgs)
263 (mew-nntp2-set-server pnm server)
264 (mew-nntp2-set-port pnm port)
265 (mew-nntp2-set-ssh-server pnm sshsrv)
266 (mew-nntp2-set-ssh-process pnm sshpro)
267 (mew-nntp2-set-ssl-process pnm sslpro)
268 (mew-nntp2-set-user pnm user)
269 (mew-nntp2-set-account pnm (format "%s@%s" user server))
270 (mew-nntp2-set-status pnm "greeting")
271 ;;
272 (set-process-buffer process nil)
273 (set-process-sentinel process 'mew-nntp2-sentinel)
274 (set-process-filter process 'mew-nntp2-filter)
•275 (message "Posting in background...")
276 (when sslnp
277 ;; GnuTLS requires a client-initiated command after the
278 ;; session is established or upgraded to use TLS because
279 ;; no additional greeting from the server.
280 (mew-nntp-command-mode-reader process pnm))
281 )))
282 (defun mew-nntp2-flush-queue (case &optional qfld)
283 (let (msgs)
284 (unless qfld (setq qfld (mew-postq-folder case)))
285 (if (mew-nntp2-get-server (mew-nntp2-info-name case)) ;; lock
286 (message "%s is locked" qfld)
287 (setq msgs (mew-folder-messages qfld))
288 (when msgs
289 (mew-summary-folder-cache-clean qfld)
290 (run-hooks 'mew-nntp2-flush-hook)
291 (message "Flushing %s..." qfld)
292 (mew-nntp2-send-message case qfld msgs)))))
293
294 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
295 ;;;
296 ;;; Filter and sentinel 2
297 ;;;
298
299 (defun mew-nntp2-debug (label string)
300 (when (mew-debug 'net)
301 (with-current-buffer (get-buffer-create mew-buffer-debug)
302 (goto-char (point-max))
303 (insert (format "\n<%s>\n%s\n" label string)))))
304
305 (defun mew-nntp2-filter (process string)
306 (let* ((pnm (process-name process))
307 (status (mew-nntp2-get-status pnm))
308 (str (concat (mew-nntp2-get-string pnm) string))
309 (buf (process-buffer process))
310 next func code)
311 (save-excursion
312 (mew-nntp2-debug (upcase status) string)
313 (if (and buf (get-buffer buf)) (set-buffer buf))
314 ;; NNTP server's strings should be short enough.
315 (mew-nntp2-set-string pnm str)
316 (cond
317 ((and (string-match "\n$" str)
318 (string-match "^\\([0-9][0-9][0-9]\\) " str))
319 (setq code (match-string 1 str))
320 (setq next (mew-nntp2-fsm-next status code))
321 (cond
322 (next
323 (mew-nntp2-set-status pnm next)
324 (setq func (intern-soft (concat "mew-nntp2-command-" next)))
325 (and func (funcall func process pnm))
326 (mew-nntp2-set-string pnm nil))
327 (t
328 (if (string-match "^pwd-" status)
329 (mew-nntp2-set-error pnm "NNTP password is wrong!")
330 (if (string-match "\n$" str)
331 (setq str (substring str 0 -1)))
332 (unless (string-match "[.!]$" str)
333 (setq str (concat str "."))))
334 (mew-nntp2-set-error pnm str)
335 (mew-nntp2-command-quit2 process pnm))))
336 (t ()))))) ;; stay
337
338 (defun mew-nntp2-sentinel (process event)
339 (let* ((pnm (process-name process))
340 (buf (process-buffer process))
341 (qfld (mew-nntp2-get-qfld pnm))
342 (case (mew-nntp2-get-case pnm))
343 (done (mew-nntp2-get-done pnm))
344 (imapp (mew-nntp2-get-imapp pnm))
345 (error (mew-nntp2-get-error pnm))
346 (sshpro (mew-nntp2-get-ssh-process pnm))
347 (sslpro (mew-nntp2-get-ssl-process pnm)))
348 (save-excursion
349 (mew-nntp2-debug "NNTP SENTINEL" event)
350 (cond
351 (error
352 (when buf
353 ;; A message file is not inserted at the beginning of the NNTP
354 ;; session.
355 (set-buffer buf)
356 (mew-nntp2-queue case error))
357 (mew-nntp2-log pnm error)
358 (if (memq system-type '(windows-nt ms-dos cygwin))
359 (message (format "%s This mail has been queued to %s" error qfld))
360 (message-box (format "%s This mail has been queued to %s" error qfld))))
361 (done
362 (message "Posting in background...done"))
363 (t
364 (if (null buf)
365 (message "NNTP connection is lost")
366 (set-buffer buf)
367 (mew-nntp2-queue case "NNTP connection is lost"))))
368 (mew-info-clean-up pnm)
369 (if (and (processp sshpro) (not mew-ssh-keep-connection))
370 (process-send-string sshpro "exit\n"))
371 (if (and (processp sslpro) (not mew-ssl-keep-connection))
372 (delete-process sslpro))
373 (run-hooks 'mew-nntp2-sentinel-hook)
374 (if imapp (mew-imap2-fcc case))
375 (when buf (mew-remove-buffer buf)))))
376
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378 ;;;
379 ;;; Queuing
380 ;;;
381
382 (defun mew-nntp2-queue (case err)
383 ;; Must be in a buffer where a message is contained.
384 (let* ((pnm (mew-nntp2-info-name case))
385 (qfld (mew-postq-folder case))
386 (oname (buffer-name))
387 (work (buffer-file-name))
388 file-info file info nname)
389 (mew-local-folder-check qfld)
390 (setq file-info (mew-queue-enqueue work qfld))
391 (mew-set '(file info) file-info)
392 (setq file (file-name-nondirectory file))
393 (setq nname (mew-concat-folder qfld file))
394 (if (mew-draft-p)
395 (mew-nntp2-set-case pnm (mew-tinfo-get-case)))
396 ;;
397 (let* ((n mew-nntp2-info-list-save-length)
398 (data (make-vector n nil)))
399 (dotimes (i n)
400 (aset data i (aref (mew-info pnm) i)))
401 (mew-lisp-save info data))
402 ;;
403 (mew-remove-buffer (current-buffer))
404 (message "%s has been queued to %s (%s)" oname nname err)
405 (mew-touch-folder qfld)
406 (file-name-sans-extension file)))
407
408 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
409 ;;;
410 ;;; Logging
411 ;;;
412
413 (defun mew-nntp2-log (pnm &optional err)
414 (let ((logtime (mew-nntp2-get-logtime pnm))
415 (msgid (mew-nntp2-get-msgid pnm))
416 (newsgroups (mew-nntp2-get-newsgroups pnm))
417 (server (mew-nntp2-get-server pnm))
418 (sshsrv (mew-nntp2-get-ssh-server pnm))
419 (sslp (mew-nntp2-get-ssl-process pnm)))
420 (with-temp-buffer
421 (and logtime (insert logtime))
422 (and msgid (insert " id=" msgid))
423 (and server (insert " server=" server))
424 (and sshsrv (insert " sshsrv=" sshsrv))
425 (and sslp (insert " SSL/TLS"))
426 (and newsgroups (insert " newsgroups=" newsgroups))
427 (if err
428 (insert " status=" "("
429 (substring err 0 (string-match "\n+$" err))
430 ")")
431 (insert " status=sent"))
432 (insert "\n")
433 (write-region (point-min) (point-max)
434 (expand-file-name mew-nntp-log-file mew-conf-path)
435 'append 'no-msg))))
436
437 (provide 'mew-nntp2)
438
439 ;;; Copyright Notice:
440
•441 ;; Copyright (C) 1999-2020 Mew developing team.
442 ;; All rights reserved.
443
444 ;; Redistribution and use in source and binary forms, with or without
445 ;; modification, are permitted provided that the following conditions
446 ;; are met:
447 ;;
448 ;; 1. Redistributions of source code must retain the above copyright
449 ;; notice, this list of conditions and the following disclaimer.
450 ;; 2. Redistributions in binary form must reproduce the above copyright
451 ;; notice, this list of conditions and the following disclaimer in the
452 ;; documentation and/or other materials provided with the distribution.
453 ;; 3. Neither the name of the team nor the names of its contributors
454 ;; may be used to endorse or promote products derived from this software
455 ;; without specific prior written permission.
456 ;;
457 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
458 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
459 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
460 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
461 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
462 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
463 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
464 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
465 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
466 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
467 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
468
469 ;;; mew-nntp2.el ends here