*** empty log message ***
[gnus] / lisp / nntp.el
1 ;;; nntp.el --- NNTP (RFC977) Interface for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,92,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'rnews)
29 (require 'nnheader)
30
31 (eval-and-compile
32   (autoload 'news-setup "rnewspost")
33   (autoload 'news-reply-mode "rnewspost"))
34
35 (defvar nntp-server-hook nil
36   "Hooks for the NNTP server.
37 If the kanji code of the NNTP server is different from the local kanji
38 code, the correct kanji code of the buffer associated with the NNTP
39 server must be specified as follows:
40
41 \(setq nntp-server-hook
42       (function
43        (lambda ()
44          ;; Server's Kanji code is EUC (NEmacs hack).
45          (make-local-variable 'kanji-fileio-code)
46          (setq kanji-fileio-code 0))))
47
48 If you'd like to change something depending on the server in this
49 hook, use the variable `nntp-server-name'.")
50
51 (defvar nntp-server-opened-hook 
52   (list
53    (lambda ()
54      (nntp-send-command "MODE" "READER")))
55   "Hook used for sending commands to the server at startup.
56 It is used by default to send the \"MODE READER\" command to the
57 server. This makes innd servers spawn an nnrpd server.
58 Other useful commands might be \"AUTHINFO\".")
59
60 (defvar nntp-large-newsgroup 50
61   "The number of the articles which indicates a large newsgroup.
62 If the number of the articles is greater than the value, verbose
63 messages will be shown to indicate the current status.")
64
65 (defvar nntp-buggy-select (memq system-type '(usg-unix-v fujitsu-uts))
66   "t if your select routine is buggy.
67 If the select routine signals error or fall into infinite loop while
68 waiting for the server response, the variable must be set to t.  In
69 case of Fujitsu UTS, it is set to T since `accept-process-output'
70 doesn't work properly.")
71
72 (defvar nntp-maximum-request 400
73   "The maximum number of the requests sent to the NNTP server at one time.
74 If Emacs hangs up while retrieving headers, set the variable to a
75 lower value.")
76
77 (defvar nntp-debug-read 10000
78   "Display '...' every 10Kbytes of a message being received if it is non-nil.
79 If it is a number, dots are displayed per the number.")
80
81 (defvar nntp-nov-is-evil nil
82   "If non-nil, nntp will never attempt to use XOVER when talking to the server.")
83
84 (defvar nntp-xover-commands '("XOVER" "XOVERVIEW")
85   "List of strings that are used as commands to fetch NOV lines from a server.
86 The strings are tried in turn until a positive response is gotten. If
87 none of the commands are successful, nntp will just grab headers one
88 by one.")
89
90 \f
91 (defconst nntp-version "nntp 4.0"
92   "Version numbers of this version of NNTP.")
93
94 (defvar nntp-server-name nil
95   "The name of the host running NNTP server.")
96
97 (defvar nntp-server-buffer nil
98   "Buffer associated with NNTP server process.")
99
100 (defvar nntp-server-process nil
101   "The NNTP server process.
102 You'd better not use this variable in NNTP front-end program but
103 instead use `nntp-server-buffer'.")
104
105 (defvar nntp-status-string nil
106   "Save the server response message.
107 You'd better not use this variable in NNTP front-end program but
108 instead call function `nntp-status-message' to get status message.")
109
110 (defvar nntp-current-server "")
111
112 (defvar nntp-server-alist nil)
113
114 (defvar nntp-server-xover t)
115
116 (defvar nntp-current-group "")
117
118 ;;; Interface funtions.
119
120 (defun nntp-retrieve-headers (sequence &optional newsgroup server)
121   "Retrieve the headers to the articles in SEQUENCE."
122   (nntp-possibly-change-server newsgroup server)
123   (save-excursion
124     (set-buffer nntp-server-buffer)
125     (erase-buffer)
126     (if (and (not gnus-nov-is-evil) 
127              (not nntp-nov-is-evil)
128              (nntp-retrieve-headers-with-xover sequence))
129         'nov
130       (let ((number (length sequence))
131             (count 0)
132             (received 0)
133             (last-point (point-min)))
134         ;; Send HEAD command.
135         (while sequence
136           (nntp-send-strings-to-server "HEAD" (car sequence))
137           (setq sequence (cdr sequence))
138           (setq count (1+ count))
139           ;; Every 400 header requests we have to read stream in order
140           ;;  to avoid deadlock.
141           (if (or (null sequence)       ;All requests have been sent.
142                   (zerop (% count nntp-maximum-request)))
143               (progn
144                 (accept-process-output)
145                 (while (progn
146                          (goto-char last-point)
147                          ;; Count replies.
148                          (while (re-search-forward "^[0-9]" nil t)
149                            (setq received (1+ received)))
150                          (setq last-point (point))
151                          (< received count))
152                   ;; If number of headers is greater than 100, give
153                   ;;  informative messages.
154                   (and (numberp nntp-large-newsgroup)
155                        (> number nntp-large-newsgroup)
156                        (zerop (% received 20))
157                        (message "NNTP: Receiving headers... %d%%"
158                                 (/ (* received 100) number)))
159                   (nntp-accept-response))
160                 ))
161           )
162         ;; Wait for text of last command.
163         (goto-char (point-max))
164         (re-search-backward "^[0-9]" nil t)
165         (if (looking-at "^[23]")
166             (while (progn
167                      (goto-char (- (point-max) 3))
168                      (not (looking-at "^\\.\r$")))
169               (nntp-accept-response)))
170         (and (numberp nntp-large-newsgroup)
171              (> number nntp-large-newsgroup)
172              (message "NNTP: Receiving headers... done"))
173
174         ;; Now all of replies are received.
175         (setq received number)
176         ;; First, fold continuation lines.
177         (goto-char (point-min))
178         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
179           (replace-match " " t t))
180         ;; Remove all "\r"'s
181         (goto-char (point-min))
182         (while (re-search-forward "\r" nil t)
183           (replace-match "" t t))
184         'headers))))
185
186 (defun nntp-open-server (server &optional service)
187   "Open news server on SERVER.
188 If SERVER is nil, use value of environment variable `NNTPSERVER'.
189 If optional argument SERVICE is non-nil, open by the service name."
190   (let ((server (or server (getenv "NNTPSERVER")))
191         (status nil))
192     (setq nntp-status-string "")
193     (message "nntp: Connecting to server on %s..." server)
194     (cond ((and server (nntp-open-server-internal server service))
195            (setq nntp-current-server server)
196            (setq status (nntp-wait-for-response "^[23].*\r$"))
197            ;; Do check unexpected close of connection.
198            ;; Suggested by feldmark@hanako.stars.flab.fujitsu.junet.
199            (if status
200                (progn
201                  (set-process-sentinel nntp-server-process
202                                        'nntp-default-sentinel)
203                  ;; You can send commands at startup like AUTHINFO here.
204                  ;; Added by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>
205                  (run-hooks 'nntp-server-opened-hook))
206              ;; We have to close connection here, since function
207              ;;  `nntp-server-opened' may return incorrect status.
208              (nntp-close-server-internal server)
209              ))
210           ((null server)
211            (setq nntp-status-string "NNTP server is not specified."))
212           )
213     (message "")
214     status
215     ))
216
217 (defun nntp-close-server (&optional server)
218   "Close news server."
219   (nntp-possibly-change-server nil server)
220   (unwind-protect
221       (progn
222         ;; Un-set default sentinel function before closing connection.
223         (and nntp-server-process
224              (eq 'nntp-default-sentinel
225                  (process-sentinel nntp-server-process))
226              (set-process-sentinel nntp-server-process nil))
227         ;; We cannot send QUIT command unless the process is running.
228         (if (nntp-server-opened)
229             (nntp-send-command nil "QUIT"))
230         )
231     (nntp-close-server-internal server)
232     ))
233
234 (fset 'nntp-request-quit (symbol-function 'nntp-close-server))
235
236 (defun nntp-server-opened (&optional server)
237   "Return server process status.
238 If the stream is opened, return non-nil, otherwise return nil."
239   (if (or server nntp-current-server)
240       (let ((process (nth 1 (assoc (or server nntp-current-server)
241                                    nntp-server-alist))))
242         (and process 
243              (memq (process-status process) '(open run))))))
244
245 (defun nntp-status-message (&optional server)
246   "Return server status response as string."
247   (if (and nntp-status-string
248            ;; NNN MESSAGE
249            (string-match "[0-9][0-9][0-9][ \t]+\\([^\r]*\\).*$"
250                          nntp-status-string))
251       (substring nntp-status-string (match-beginning 1) (match-end 1))
252     ;; Empty message if nothing.
253     ""
254     ))
255
256 (defun nntp-request-article (id &optional newsgroup server buffer)
257   "Select article by message ID (or number)."
258   (nntp-possibly-change-server newsgroup server)
259   (unwind-protect
260       (progn
261         (if buffer (set-process-buffer nntp-server-process buffer))
262         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
263           ;; If NEmacs, end of message may look like: "\256\215" (".^M")
264           (prog1
265               (nntp-send-command "^\\.\r$" "ARTICLE" id)
266             (nntp-decode-text))))
267     (if buffer (set-process-buffer nntp-server-process nntp-server-buffer))))
268
269 (defun nntp-request-body (id &optional newsgroup server)
270   "Select article body by message ID (or number)."
271   (nntp-possibly-change-server newsgroup server)
272   (prog1
273       ;; If NEmacs, end of message may look like: "\256\215" (".^M")
274       (nntp-send-command "^\\.\r$" "BODY" id)
275     (nntp-decode-text)
276     ))
277
278 (defun nntp-request-head (id &optional newsgroup server)
279   "Select article head by message ID (or number)."
280   (nntp-possibly-change-server newsgroup server)
281   (prog1
282       (nntp-send-command "^\\.\r$" "HEAD" id)
283     (nntp-decode-text)
284     ))
285
286 (defun nntp-request-stat (id &optional newsgroup server)
287   "Select article by message ID (or number)."
288   (nntp-possibly-change-server newsgroup server)
289   (nntp-send-command "^[23].*\r$" "STAT" id))
290
291 (defun nntp-request-group (group &optional server dont-check)
292   "Select news GROUP."
293   (if (nntp-possibly-change-server nil server)
294       (progn
295         (nntp-send-command "^.*\r$" "GROUP" group)
296         )))
297
298 (defun nntp-close-group (group &optional server)
299   t)
300
301 (defun nntp-request-list (&optional server)
302   "List active newsgroups."
303   (nntp-possibly-change-server nil server)
304   (prog1
305       (nntp-send-command "^\\.\r$" "LIST")
306     (nntp-decode-text)
307     ))
308
309 (defun nntp-request-list-newsgroups (&optional server)
310   "List newsgroups (defined in NNTP2)."
311   (nntp-possibly-change-server nil server)
312   (prog1
313       (nntp-send-command "^\\.\r$" "LIST NEWSGROUPS")
314     (nntp-decode-text)
315     ))
316
317 (defun nntp-request-newgroups (date &optional server)
318   "List new groups (defined in NNTP2)."
319   (nntp-possibly-change-server nil server)
320   (prog1
321       (nntp-send-command "^\\.\r$" "NEWGROUPS" date)
322     (nntp-decode-text)))
323
324 (defun nntp-request-list-distributions (&optional server)
325   "List distributions (defined in NNTP2)."
326   (nntp-possibly-change-server nil server)
327   (prog1
328       (nntp-send-command "^\\.\r$" "LIST DISTRIBUTIONS")
329     (nntp-decode-text)
330     ))
331
332 (defun nntp-request-last (&optional newsgroup server)
333   "Set current article pointer to the previous article
334 in the current news group."
335   (nntp-possibly-change-server newsgroup server)
336   (nntp-send-command "^[23].*\r$" "LAST"))
337
338 (defun nntp-request-next (&optional newsgroup server)
339   "Advance current article pointer."
340   (nntp-possibly-change-server newsgroup server)
341   (nntp-send-command "^[23].*\r$" "NEXT"))
342
343 (defun nntp-request-post (&optional server)
344   "Post a new news in current buffer."
345   (nntp-possibly-change-server nil server)
346   (if (nntp-send-command "^[23].*\r$" "POST")
347       (progn
348         (nntp-encode-text)
349         (nntp-send-region-to-server (point-min) (point-max))
350         ;; 1.2a NNTP's post command is buggy. "^M" (\r) is not
351         ;;  appended to end of the status message.
352         (nntp-wait-for-response "^[23].*$")
353         )))
354
355 (defun nntp-request-post-buffer (method header article-buffer group info)
356   (let (from subject date to followup-to newsgroups message-of
357              references distribution message-id follow-to)
358     (save-excursion
359       (set-buffer (get-buffer-create "*post-news*"))
360       (news-reply-mode)
361       (if (and (buffer-modified-p)
362                (> (buffer-size) 0)
363                (not (y-or-n-p "Unsent article being composed; erase it? ")))
364           ()
365         (erase-buffer)
366         (if (eq method 'post)
367             (news-setup nil nil nil header article-buffer)
368           (save-excursion
369             (set-buffer article-buffer)
370             (goto-char (point-min))
371             (narrow-to-region (point-min)
372                               (progn (search-forward "\n\n") (point)))
373             (if (and (boundp 'gnus-followup-to-function)
374                      gnus-followup-to-function)
375                 (setq follow-to (funcall gnus-followup-to-function group)))
376             (setq from (header-from header))
377             (setq date (header-date header))
378             (and from
379                  (let ((stop-pos 
380                         (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
381                    (setq message-of
382                          (concat (if stop-pos (substring from 0 stop-pos) from)
383                                  "'s message of " date))))
384             (setq subject (header-subject header))
385             (or (string-match "^[Rr][Ee]:" subject)
386                 (setq subject (concat "Re: " subject)))
387             (setq followup-to (mail-fetch-field "followup-to"))
388             (if (or (null gnus-use-followup-to) ;Ignore followup-to: field.
389                     (string-equal "" followup-to) ;Bogus header.
390                     (string-equal "poster" followup-to)) ;Poster
391                 (setq followup-to nil))
392             (setq newsgroups (or followup-to (mail-fetch-field "newsgroups")))
393             (setq references (header-references header))
394             (setq distribution (mail-fetch-field "distribution"))
395             ;; Remove bogus distribution.
396             (and (string= distribution "world")
397                  (setq distribution nil))
398             (setq message-id (header-id header))
399             (widen))
400           (setq news-reply-yank-from from)
401           (setq news-reply-yank-message-id message-id)
402           ;; Prevent getting BCC or FCC fields inserted for both mail
403           ;; and news.  
404           (let ((mail-self-blind
405                  (and (not gnus-mail-self-blind) mail-self-blind))
406                 (mail-archive-file-name
407                  (and (not gnus-author-copy) mail-archive-file-name)))
408             (news-setup 
409              ;; Suggested by Daniel Quinlan <quinlan@best.com>.
410              (if (eq gnus-auto-mail-to-author 'ask)
411                  (and (y-or-n-p "Also send mail to author? ") from)
412                (and gnus-auto-mail-to-author from))
413              subject message-of newsgroups article-buffer))
414           ;; Fold long references line to follow RFC1036.
415           (mail-position-on-field "References")
416           (let ((begin (- (point) (length "References: ")))
417                 (fill-column 79)
418                 (fill-prefix "\t"))
419             (if references (insert references))
420             (if (and references message-id) (insert " "))
421             (if message-id (insert message-id))
422             ;; The region must end with a newline to fill the region
423             ;; without inserting extra newline.
424             (fill-region-as-paragraph begin (1+ (point))))
425           (if distribution
426               (progn
427                 (mail-position-on-field "Distribution")
428                 (insert distribution)))))
429       (current-buffer))))
430
431 ;;; Internal functions.
432
433 (defun nntp-default-sentinel (proc status)
434   "Default sentinel function for NNTP server process."
435   (let ((servers nntp-server-alist))
436     (while (and servers 
437                 (not (equal proc (nth 1 (car servers)))))
438       (setq servers (cdr servers)))
439     (message "nntp: Connection closed to server %s." 
440              (or (car (car servers)) "(none)"))
441     (ding)))
442
443 ;; Encoding and decoding of NNTP text.
444
445 (defun nntp-decode-text ()
446   "Decode text transmitted by NNTP.
447 0. Delete status line.
448 1. Delete `^M' at end of line.
449 2. Delete `.' at end of buffer (end of text mark).
450 3. Delete `.' at beginning of line."
451   (save-excursion
452     (set-buffer nntp-server-buffer)
453     ;; Insert newline at end of buffer.
454     (goto-char (point-max))
455     (if (not (bolp))
456         (insert "\n"))
457     ;; Delete status line.
458     (goto-char (point-min))
459     (delete-region (point) (progn (forward-line 1) (point)))
460     ;; Delete `^M' at end of line.
461     ;; (replace-regexp "\r$" "")
462     (while (not (eobp))
463       (end-of-line)
464       (if (= (preceding-char) ?\r)
465           (delete-char -1))
466       (forward-line 1)
467       )
468     ;; Delete `.' at end of buffer (end of text mark).
469     (goto-char (point-max))
470     (forward-line -1)                   ;(beginning-of-line)
471     (if (looking-at "^\\.$")
472         (delete-region (point) (progn (forward-line 1) (point))))
473     ;; Replace `..' at beginning of line with `.'.
474     (goto-char (point-min))
475     ;; (replace-regexp "^\\.\\." ".")
476     (while (search-forward "\n.." nil t)
477       (delete-char -1))
478     ))
479
480 (defun nntp-encode-text ()
481   "Encode text in current buffer for NNTP transmission.
482 1. Insert `.' at beginning of line.
483 2. Insert `.' at end of buffer (end of text mark)."
484   (save-excursion
485     ;; Insert newline at end of buffer.
486     (goto-char (point-max))
487     (if (not (bolp))
488         (insert "\n"))
489     ;; Replace `.' at beginning of line with `..'.
490     (goto-char (point-min))
491     ;; (replace-regexp "^\\." "..")
492     (while (search-forward "\n." nil t)
493       (insert "."))
494     ;; Insert `.' at end of buffer (end of text mark).
495     (goto-char (point-max))
496     (insert ".\r\n")
497     ))
498
499 \f
500 ;;;
501 ;;; Synchronous Communication with NNTP Server.
502 ;;;
503
504 (defun nntp-send-command (response cmd &rest args)
505   "Wait for server RESPONSE after sending CMD and optional ARGS to server."
506   (save-excursion
507     ;; Clear communication buffer.
508     (set-buffer nntp-server-buffer)
509     (erase-buffer)
510     (apply 'nntp-send-strings-to-server cmd args)
511     (if response
512         (nntp-wait-for-response response)
513       t)
514     ))
515
516 (defun nntp-wait-for-response (regexp)
517   "Wait for server response which matches REGEXP."
518   (save-excursion
519     (let ((status t)
520           (wait t)
521           (dotnum 0)                    ;Number of "." being displayed.
522           (dotsize                      ;How often "." displayed.
523            (if (numberp nntp-debug-read) nntp-debug-read 10000)))
524       (set-buffer nntp-server-buffer)
525       ;; Wait for status response (RFC977).
526       ;; 1xx - Informative message.
527       ;; 2xx - Command ok.
528       ;; 3xx - Command ok so far, send the rest of it.
529       ;; 4xx - Command was correct, but couldn't be performed for some
530       ;;       reason.
531       ;; 5xx - Command unimplemented, or incorrect, or a serious
532       ;;       program error occurred.
533       (nntp-accept-response)
534       (while wait
535         (goto-char (point-min))
536         (cond ((looking-at "[23]")
537                (setq wait nil))
538               ((looking-at "[45]")
539                (setq status nil)
540                (setq wait nil))
541               (t (nntp-accept-response))
542               ))
543       ;; Save status message.
544       (end-of-line)
545       (setq nntp-status-string
546             (buffer-substring (point-min) (point)))
547       (if status
548           (progn
549             (setq wait t)
550             (while wait
551               (goto-char (point-max))
552               (forward-line -1)         ;(beginning-of-line)
553               ;;(message (buffer-substring
554               ;;         (point)
555               ;;         (save-excursion (end-of-line) (point))))
556               (if (looking-at regexp)
557                   (setq wait nil)
558                 (if nntp-debug-read
559                     (let ((newnum (/ (buffer-size) dotsize)))
560                       (if (not (= dotnum newnum))
561                           (progn
562                             (setq dotnum newnum)
563                             (message "NNTP: Reading %s"
564                                      (make-string dotnum ?.))))))
565                 (nntp-accept-response)
566                 ;;(if nntp-debug-read (message ""))
567                 ))
568             ;; Remove "...".
569             (if (and nntp-debug-read (> dotnum 0))
570                 (message ""))
571             ;; Successfully received server response.
572             t
573             ))
574       )))
575
576 \f
577 ;;;
578 ;;; Low-Level Interface to NNTP Server.
579 ;;; 
580
581 (defun nntp-retrieve-headers-with-xover (sequence)
582   (if (not nntp-server-xover)
583       ()
584     (let ((range (format "%d-%d" (car sequence)
585                          (nntp-last-element sequence))))
586       (prog1
587           (if (stringp nntp-server-xover)
588               (nntp-send-command "^\\.\r$" nntp-server-xover range)
589             (let ((commands nntp-xover-commands))
590               (while (and commands
591                           (eq t nntp-server-xover))
592                 (nntp-send-command "^\\.\r$" (car commands) range)
593                 (save-excursion
594                   (set-buffer nntp-server-buffer)
595                   (goto-char 1)
596                   (if (looking-at "[23]") 
597                       (setq nntp-server-xover (car commands))))
598                 (setq commands (cdr commands)))
599               (if (eq t nntp-server-xover)
600                   (setq nntp-server-xover nil))
601               (setcar (nthcdr 2 (assoc nntp-current-server nntp-server-alist))
602                       nntp-server-xover)
603               nntp-server-xover))
604         (if nntp-server-xover (nntp-decode-text) (erase-buffer))))))
605
606 (defun nntp-send-strings-to-server (&rest strings)
607   "Send list of STRINGS to news server as command and its arguments."
608   (let ((cmd (car strings))
609         (strings (cdr strings)))
610     ;; Command and each argument must be separated by one or more spaces.
611     (while strings
612       (setq cmd (concat cmd " " (car strings)))
613       (setq strings (cdr strings)))
614     ;; Command line must be terminated by a CR-LF.
615     (if (not (nntp-server-opened nntp-current-server))
616         (progn
617           (nntp-close-server nntp-current-server)
618           (if (not (nntp-open-server nntp-current-server))
619               (error (nntp-status-message)))
620           (save-excursion
621             (set-buffer nntp-server-buffer)
622             (erase-buffer))))
623     (process-send-string nntp-server-process (concat cmd "\r\n"))
624     ))
625
626 (defun nntp-send-region-to-server (begin end)
627   "Send current buffer region (from BEGIN to END) to news server."
628   (save-excursion
629     ;; We have to work in the buffer associated with NNTP server
630     ;;  process because of NEmacs hack.
631     (copy-to-buffer nntp-server-buffer begin end)
632     (set-buffer nntp-server-buffer)
633     (setq begin (point-min))
634     (setq end (point-max))
635     ;; `process-send-region' does not work if text to be sent is very
636     ;;  large. I don't know maximum size of text sent correctly.
637     (let ((last nil)
638           (size 100))                   ;Size of text sent at once.
639       (save-restriction
640         (narrow-to-region begin end)
641         (goto-char begin)
642         (while (not (eobp))
643           ;;(setq last (min end (+ (point) size)))
644           ;; NEmacs gets confused if character at `last' is Kanji.
645           (setq last (save-excursion
646                        (goto-char (min end (+ (point) size)))
647                        (or (eobp) (forward-char 1)) ;Adjust point
648                        (point)))
649           (process-send-region nntp-server-process (point) last)
650           ;; I don't know whether the next codes solve the known
651           ;;  problem of communication error of GNU Emacs.
652           (accept-process-output)
653           ;;(sit-for 0)
654           (goto-char last)
655           )))
656     ;; We cannot erase buffer, because reply may be received.
657     (delete-region begin end)
658     ))
659
660 (defun nntp-open-server-internal (server &optional service)
661   "Open connection to news server on SERVER by SERVICE (default is nntp)."
662   (let (proc)
663     (save-excursion
664       ;; Use TCP/IP stream emulation package if needed.
665       (or (fboundp 'open-network-stream)
666           (require 'tcp))
667       ;; Initialize communication buffer.
668       (nnheader-init-server-buffer)
669       (set-buffer nntp-server-buffer)
670       (if (setq proc
671                 (condition-case nil
672                     (open-network-stream "nntpd" (current-buffer)
673                                          server (or service "nntp"))
674                   (error nil)))
675           (progn
676             (setq nntp-server-process proc)
677             ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
678             (process-kill-without-query proc)
679             (setq nntp-server-xover t)
680             (setq nntp-server-name server)
681             (setq nntp-server-alist (cons (list server nntp-server-process t)
682                                           nntp-server-alist))
683             ;; It is possible to change kanji-fileio-code in this hook.
684             (run-hooks 'nntp-server-hook)
685             nntp-server-process)))))
686
687 (defun nntp-close-server-internal (&optional server)
688   "Close connection to news server."
689   (nntp-possibly-change-server nil server)
690   (if nntp-server-process
691       (delete-process nntp-server-process))
692   (setq nntp-server-process nil)
693   (let* ((servers nntp-server-alist)
694          (prev servers))
695     (if (and servers (string= (car (car servers)) server))
696         (setq nntp-server-alist (cdr nntp-server-alist))
697       (setq servers (cdr servers))
698       (while servers
699         (if (string= (car (car servers)) server)
700             (setcdr prev (cdr servers)))
701         (setq prev servers)
702         (setq servers (cdr servers))))))
703
704 (defun nntp-request-close ()
705   "Close all server connections."
706   (while nntp-server-alist
707     (delete-process (car (cdr (car nntp-server-alist))))
708     (setq nntp-server-alist (cdr nntp-server-alist)))
709   (setq nntp-current-server "")
710   (setq nntp-server-process nil))
711
712 (defun nntp-accept-response ()
713   "Read response of server.
714 It is well-known that the communication speed will be much improved by
715 defining this function as macro."
716   ;; To deal with server process exiting before
717   ;;  accept-process-output is called.
718   ;; Suggested by Jason Venner <jason@violet.berkeley.edu>.
719   ;; This is a copy of `nntp-default-sentinel'.
720   (or (memq (process-status nntp-server-process) '(open run))
721       (error "NNTP: Connection closed."))
722   (if nntp-buggy-select
723       (progn
724         ;; We cannot use `accept-process-output'.
725         ;; Fujitsu UTS requires messages during sleep-for. I don't know why.
726         (message "NNTP: Reading...")
727         (sleep-for 1)
728         (message ""))
729     (condition-case errorcode
730         (accept-process-output nntp-server-process)
731       (error
732        (cond ((string-equal "select error: Invalid argument" (nth 1 errorcode))
733               ;; Ignore select error.
734               nil
735               )
736              (t
737               (signal (car errorcode) (cdr errorcode))
738        ))
739     ))))
740
741 (defun nntp-last-element (list)
742   "Return last element of LIST."
743   (while (cdr list)
744     (setq list (cdr list)))
745   (car list))
746
747 (defun nntp-possibly-change-server (newsgroup server)
748   (let (result changed-server)
749     ;; First see if we need to change the server - or even open a new 
750     ;; server.  
751     (if (and server (not (string= server nntp-current-server)))
752         (progn  
753           ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
754           (if (or (assoc server nntp-server-alist)
755                   (nntp-open-server server))
756               ;; `nntp-open-server' may change `nntp-server-alist', so
757               ;; we assoc again.
758               (let ((info (assoc server nntp-server-alist)))
759                 (setq nntp-current-server server)
760                 ;; Variable for backwards compatability.
761                 (setq nntp-server-name server)
762                 (setq nntp-server-process (nth 1 info))
763                 (setq nntp-server-xover (nth 2 info))
764                 (setq changed-server t)
765                 (setq result t))))
766       (setq result t))
767     ;; The we see whether it is necessary to change newsgroup.
768     (if (and newsgroup result (or (not (string= newsgroup nntp-current-group))
769                                   changed-server))
770         (progn
771           (setq result (nntp-request-group newsgroup server))
772           (setq nntp-current-group newsgroup)))
773     result))
774
775 (provide 'nntp)
776
777 ;;; nntp.el ends here