home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / gnus / nntp.el < prev    next >
Encoding:
Text File  |  1993-01-11  |  22.1 KB  |  687 lines

  1. ;;; NNTP (RFC977) Interface for GNU Emacs
  2. ;; Copyright (C) 1987, 1988, 1989 Fujitsu Laboratories LTD.
  3. ;; Copyright (C) 1987, 1988, 1989, 1990 Masanobu UMEDA
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  9. ;; accepts responsibility to anyone for the consequences of using it
  10. ;; or for whether it serves any particular purpose or works at all,
  11. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  12. ;; License for full details.
  13.  
  14. ;; Everyone is granted permission to copy, modify and redistribute
  15. ;; GNU Emacs, but only under the conditions described in the
  16. ;; GNU Emacs General Public License.   A copy of this license is
  17. ;; supposed to have been given to you along with GNU Emacs so you
  18. ;; can know your rights and responsibilities.  It should be in a
  19. ;; file named COPYING.  Among other things, the copyright notice
  20. ;; and this notice must be preserved on all copies.
  21.  
  22. ;; This implementation is tested on both 1.2a and 1.5 version of the
  23. ;; NNTP package.
  24.  
  25. ;; Troubleshooting of NNTP
  26. ;;
  27. ;; (1) Select routine may signal an error or fall into infinite loop
  28. ;;  while waiting for the server response. In this case, you'd better
  29. ;;  not use byte-compiled codes but original source. If you still have
  30. ;;  a problems with it, set the variable `nntp-buggy-select' to T.
  31. ;;
  32. ;; (2) Emacs may hang up while retrieving headers since too many
  33. ;;  requests have been sent to the NNTP server without reading their
  34. ;;  replies. In this case, reduce the number of the requests sent to
  35. ;;  the server at one time by setting the variable
  36. ;;  `nntp-maximum-request' to a lower value.
  37. ;;
  38. ;; (3) If the TCP/IP stream (open-network-stream) is not supported by
  39. ;;  emacs, compile and install `tcp.el' and `tcp.c' which is an
  40. ;;  emulation program of the stream. If you modified `tcp.c' for your
  41. ;;  system, please send me the diffs. I'll include some of them in the
  42. ;;  future releases.
  43.  
  44. (provide 'nntp)
  45.  
  46. (defvar nntp-server-hook nil
  47.   "*Hooks for the NNTP server.
  48. If the kanji code of the NNTP server is different from the local kanji
  49. code, the correct kanji code of the buffer associated with the NNTP
  50. server must be specified as follows:
  51.  
  52. (setq nntp-server-hook
  53.       (function
  54.        (lambda ()
  55.      ;; Server's Kanji code is EUC (NEmacs hack).
  56.      (make-local-variable 'kanji-fileio-code)
  57.      (setq kanji-fileio-code 0))))
  58.  
  59. If you'd like to change something depending on the server in this
  60. hook, use the variable `nntp-server-name'.")
  61.  
  62. (defvar nntp-large-newsgroup 50
  63.   "*The number of the articles which indicates a large newsgroup.
  64. If the number of the articles is greater than the value, verbose
  65. messages will be shown to indicate the current status.")
  66.  
  67. (defvar nntp-buggy-select (memq system-type '(usg-unix-v fujitsu-uts))
  68.   "*T if your select routine is buggy.
  69. If the select routine signals error or fall into infinite loop while
  70. waiting for the server response, the variable must be set to t.  In
  71. case of Fujitsu UTS, it is set to T since `accept-process-output'
  72. doesn't work properly.")
  73.  
  74. (defvar nntp-maximum-request 400
  75.   "*The maximum number of the requests sent to the NNTP server at one time.
  76. If Emacs hangs up while retrieving headers, set the variable to a
  77. lower value.")
  78.  
  79. (defvar nntp-debug-read t
  80.   "*If non-nil, show the current status about reading the NNTP server output.")
  81.  
  82.  
  83. (defconst nntp-version "NNTP 3.10"
  84.   "Version numbers of this version of NNTP.")
  85.  
  86. (defvar nntp-server-name nil
  87.   "The name of the host running NNTP server.")
  88.  
  89. (defvar nntp-server-buffer nil
  90.   "Buffer associated with NNTP server process.")
  91.  
  92. (defvar nntp-server-process nil
  93.   "The NNTP server process.
  94. You'd better not use this variable in NNTP front-end program but
  95. instead use `nntp-server-buffer'.")
  96.  
  97. (defvar nntp-status-string nil
  98.   "Save the server response message.
  99. You'd better not use this variable in NNTP front-end program but
  100. instead call function `nntp-status-message' to get status message.")
  101.  
  102. ;;;
  103. ;;; Extended Command for retrieving many headers.
  104. ;;;
  105. ;; Retrieving lots of headers by sending command asynchronously.
  106. ;; Access functions to headers are defined as macro.
  107.  
  108. (defmacro nntp-header-number (header)
  109.   "Return article number in HEADER."
  110.   (` (aref (, header) 0)))
  111.  
  112. (defmacro nntp-set-header-number (header number)
  113.   "Set article number of HEADER to NUMBER."
  114.   (` (aset (, header) 0 (, number))))
  115.  
  116. (defmacro nntp-header-subject (header)
  117.   "Return subject string in HEADER."
  118.   (` (aref (, header) 1)))
  119.  
  120. (defmacro nntp-set-header-subject (header subject)
  121.   "Set article subject of HEADER to SUBJECT."
  122.   (` (aset (, header) 1 (, subject))))
  123.  
  124. (defmacro nntp-header-from (header)
  125.   "Return author string in HEADER."
  126.   (` (aref (, header) 2)))
  127.  
  128. (defmacro nntp-set-header-from (header from)
  129.   "Set article author of HEADER to FROM."
  130.   (` (aset (, header) 2 (, from))))
  131.  
  132. (defmacro nntp-header-xref (header)
  133.   "Return xref string in HEADER."
  134.   (` (aref (, header) 3)))
  135.  
  136. (defmacro nntp-set-header-xref (header xref)
  137.   "Set article xref of HEADER to xref."
  138.   (` (aset (, header) 3 (, xref))))
  139.  
  140. (defmacro nntp-header-lines (header)
  141.   "Return lines in HEADER."
  142.   (` (aref (, header) 4)))
  143.  
  144. (defmacro nntp-set-header-lines (header lines)
  145.   "Set article lines of HEADER to LINES."
  146.   (` (aset (, header) 4 (, lines))))
  147.  
  148. (defmacro nntp-header-date (header)
  149.   "Return date in HEADER."
  150.   (` (aref (, header) 5)))
  151.  
  152. (defmacro nntp-set-header-date (header date)
  153.   "Set article date of HEADER to DATE."
  154.   (` (aset (, header) 5 (, date))))
  155.  
  156. (defmacro nntp-header-id (header)
  157.   "Return Id in HEADER."
  158.   (` (aref (, header) 6)))
  159.  
  160. (defmacro nntp-set-header-id (header id)
  161.   "Set article Id of HEADER to ID."
  162.   (` (aset (, header) 6 (, id))))
  163.  
  164. (defmacro nntp-header-references (header)
  165.   "Return references in HEADER."
  166.   (` (aref (, header) 7)))
  167.  
  168. (defmacro nntp-set-header-references (header ref)
  169.   "Set article references of HEADER to REF."
  170.   (` (aset (, header) 7 (, ref))))
  171.  
  172. (defun nntp-retrieve-headers (sequence)
  173.   "Return list of article headers specified by SEQUENCE of article id.
  174. The format of list is
  175.  `([NUMBER SUBJECT FROM XREF LINES DATE MESSAGE-ID REFERENCES] ...)'.
  176. Reader macros for the vector are defined as `nntp-header-FIELD'.
  177. Writer macros for the vector are defined as `nntp-set-header-FIELD'.
  178. News group must be selected before calling me."
  179.   (save-excursion
  180.     (set-buffer nntp-server-buffer)
  181.     (erase-buffer)
  182.     (let ((number (length sequence))
  183.       (last-point (point-min))
  184.       (received 0)
  185.       (count 0)
  186.       (headers nil)            ;Result list.
  187.       (article 0)
  188.       (subject nil)
  189.       (message-id)
  190.       (from nil)
  191.       (xref nil)
  192.       (lines 0)
  193.       (date nil)
  194.       (references nil))
  195.       ;; Send HEAD command.
  196.       (while sequence
  197.     (nntp-send-strings-to-server "HEAD" (car sequence))
  198.     (setq sequence (cdr sequence))
  199.     (setq count (1+ count))
  200.     ;; Every 400 header requests we have to read stream in order
  201.     ;;  to avoid deadlock.
  202.     (if (or (null sequence)        ;All requests have been sent.
  203.         (zerop (% count nntp-maximum-request)))
  204.         (progn
  205.           (accept-process-output)
  206.           (while (progn
  207.                (goto-char last-point)
  208.                ;; Count replies.
  209.                (while (re-search-forward "^[0-9]" nil t)
  210.              (setq received (1+ received)))
  211.                (setq last-point (point))
  212.                (< received count))
  213.         ;; If number of headers is greater than 100, give
  214.         ;;  informative messages.
  215.         (and (numberp nntp-large-newsgroup)
  216.              (> number nntp-large-newsgroup)
  217.              (zerop (% received 20))
  218.              (message "NNTP: %d%% of headers received."
  219.                   (/ (* received 100) number)))
  220.         (nntp-accept-response))
  221.           ))
  222.     )
  223.       ;; Wait for text of last command.
  224.       (goto-char (point-max))
  225.       (re-search-backward "^[0-9]" nil t)
  226.       (if (looking-at "^[23]")
  227.       (while (progn
  228.            (goto-char (- (point-max) 3))
  229.            (not (looking-at "^\\.\r$")))
  230.         (nntp-accept-response)))
  231.       (and (numberp nntp-large-newsgroup)
  232.        (> number nntp-large-newsgroup)
  233.        (message "NNTP: 100%% of headers received."))
  234.       ;; Now all of replies are received.
  235.       (setq received number)
  236.       ;; First, fold continuation lines.
  237.       (goto-char (point-min))
  238.       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
  239.         (replace-match " " t t))
  240.       ;;(delete-non-matching-lines
  241.       ;; "^Subject:\\|^Xref:\\|^From:\\|^Lines:\\|^Date:\\|^References:\\|^[23]")
  242.       (and (numberp nntp-large-newsgroup)
  243.        (> number nntp-large-newsgroup)
  244.        (message "NNTP: Parsing headers..."))
  245.       ;; Then examines replies.
  246.       (goto-char (point-min))
  247.       (while (not (eobp))
  248.     (cond ((looking-at "^[23][0-9][0-9][ \t]+\\([0-9]+\\)[ \t]+\\(<[^>]+>\\)")
  249.            (setq article
  250.              (string-to-int
  251.               (buffer-substring (match-beginning 1) (match-end 1))))
  252.            (setq message-id
  253.              (buffer-substring (match-beginning 2) (match-end 2)))
  254.            (forward-line 1)
  255.            ;; Set default value.
  256.            (setq subject nil)
  257.            (setq xref nil)
  258.            (setq from nil)
  259.            (setq lines 0)
  260.            (setq date nil)
  261.            (setq references nil)
  262.            ;; Thanks go to mly@AI.MIT.EDU (Richard Mlynarik)
  263.            (while (and (not (eobp))
  264.                (not (memq (following-char) '(?2 ?3))))
  265.          (if (looking-at "\\(From\\|Subject\\|Date\\|Lines\\|Xref\\|References\\):[ \t]+\\([^ \t\n]+.*\\)\r$")
  266.              (let ((s (buffer-substring
  267.                    (match-beginning 2) (match-end 2)))
  268.                (c (char-after (match-beginning 0))))
  269.                ;; We don't have to worry about letter case.
  270.                (cond ((char-equal c ?F)    ;From:
  271.                   (setq from s))
  272.                  ((char-equal c ?S)    ;Subject:
  273.                   (setq subject s))
  274.                  ((char-equal c ?D)    ;Date:
  275.                   (setq date s))
  276.                  ((char-equal c ?L)    ;Lines:
  277.                   (setq lines (string-to-int s)))
  278.                  ((char-equal c ?X)    ;Xref:
  279.                   (setq xref s))
  280.                  ((char-equal c ?R)    ;References:
  281.                   (setq references s))
  282.                  )))
  283.          (forward-line 1))
  284.            ;; Finished to parse one header.
  285.            (if (null subject)
  286.            (setq subject "(None)"))
  287.            (if (null from)
  288.            (setq from "(Unknown User)"))
  289.            (setq headers
  290.              (cons (vector article subject from
  291.                    xref lines date
  292.                    message-id references) headers))
  293.            )
  294.           (t (forward-line 1))
  295.           )
  296.     (setq received (1- received))
  297.     (and (numberp nntp-large-newsgroup)
  298.          (> number nntp-large-newsgroup)
  299.          (zerop (% received 20))
  300.          (message "NNTP: Parsing headers... %d%%"
  301.               (/ (* received 100) number)))
  302.     )
  303.       (and (numberp nntp-large-newsgroup)
  304.        (> number nntp-large-newsgroup)
  305.        (message "NNTP: Parsing headers... done"))
  306.       (nreverse headers)
  307.       )))
  308.  
  309.  
  310. ;;;
  311. ;;; Raw Interface to Network News Transfer Protocol (RFC977).
  312. ;;;
  313.  
  314. (defun nntp-open-server (host &optional service)
  315.   "Open news server on HOST.
  316. If HOST is nil, use value of environment variable `NNTPSERVER'.
  317. If optional argument SERVICE is non-nil, open by the service name."
  318.   (let ((host (or host (getenv "NNTPSERVER")))
  319.     (status nil))
  320.     (setq nntp-status-string "")
  321.     (cond ((and host (nntp-open-server-internal host service))
  322.        (setq status (nntp-wait-for-response "^[23].*\r$"))
  323.        ;; Do check unexpected close of connection.
  324.        ;; Suggested by feldmark@hanako.stars.flab.fujitsu.junet.
  325.        (if status
  326.            (set-process-sentinel nntp-server-process
  327.                      'nntp-default-sentinel)
  328.          ;; We have to close connection here, since function
  329.          ;;  `nntp-server-opened' may return incorrect status.
  330.          (nntp-close-server-internal)
  331.          ))
  332.       ((null host)
  333.        (setq nntp-status-string "NNTP server is not specified."))
  334.       )
  335.     status
  336.     ))
  337.  
  338. (defun nntp-close-server ()
  339.   "Close news server."
  340.   (unwind-protect
  341.       (progn
  342.     ;; Un-set default sentinel function before closing connection.
  343.     (and nntp-server-process
  344.          (eq 'nntp-default-sentinel
  345.          (process-sentinel nntp-server-process))
  346.          (set-process-sentinel nntp-server-process nil))
  347.     ;; We cannot send QUIT command unless the process is running.
  348.     (if (nntp-server-opened)
  349.         (nntp-send-command nil "QUIT"))
  350.     )
  351.     (nntp-close-server-internal)
  352.     ))
  353.  
  354. (fset 'nntp-request-quit (symbol-function 'nntp-close-server))
  355.  
  356. (defun nntp-server-opened ()
  357.   "Return server process status, T or NIL.
  358. If the stream is opened, return T, otherwise return NIL."
  359.   (and nntp-server-process
  360.        (memq (process-status nntp-server-process) '(open run))))
  361.  
  362. (defun nntp-status-message ()
  363.   "Return server status response as string."
  364.   (if (and nntp-status-string
  365.        ;; NNN MESSAGE
  366.        (string-match "[0-9][0-9][0-9][ \t]+\\([^\r]*\\).*$"
  367.              nntp-status-string))
  368.       (substring nntp-status-string (match-beginning 1) (match-end 1))
  369.     ;; Empty message if nothing.
  370.     ""
  371.     ))
  372.  
  373. (defun nntp-request-article (id)
  374.   "Select article by message ID (or number)."
  375.   (prog1
  376.       ;; If NEmacs, end of message may look like: "\256\215" (".^M")
  377.       (nntp-send-command "^\\.\r$" "ARTICLE" id)
  378.     (nntp-decode-text)
  379.     ))
  380.  
  381. (defun nntp-request-body (id)
  382.   "Select article body by message ID (or number)."
  383.   (prog1
  384.       ;; If NEmacs, end of message may look like: "\256\215" (".^M")
  385.       (nntp-send-command "^\\.\r$" "BODY" id)
  386.     (nntp-decode-text)
  387.     ))
  388.  
  389. (defun nntp-request-head (id)
  390.   "Select article head by message ID (or number)."
  391.   (prog1
  392.       (nntp-send-command "^\\.\r$" "HEAD" id)
  393.     (nntp-decode-text)
  394.     ))
  395.  
  396. (defun nntp-request-stat (id)
  397.   "Select article by message ID (or number)."
  398.   (nntp-send-command "^[23].*\r$" "STAT" id))
  399.  
  400. (defun nntp-request-group (group)
  401.   "Select news GROUP."
  402.   ;; 1.2a NNTP's group command is buggy. "^M" (\r) is not appended to
  403.   ;;  end of the status message.
  404.   (nntp-send-command "^[23].*$" "GROUP" group))
  405.  
  406. (defun nntp-request-list ()
  407.   "List valid newsgoups."
  408.   (prog1
  409.       (nntp-send-command "^\\.\r$" "LIST")
  410.     (nntp-decode-text)
  411.     ))
  412.  
  413. (defun nntp-request-last ()
  414.   "Set current article pointer to the previous article
  415. in the current news group."
  416.   (nntp-send-command "^[23].*\r$" "LAST"))
  417.  
  418. (defun nntp-request-next ()
  419.   "Advance current article pointer."
  420.   (nntp-send-command "^[23].*\r$" "NEXT"))
  421.  
  422. (defun nntp-request-post ()
  423.   "Post a new news in current buffer."
  424.   ;; Some hosts take fuckin' forever to post, so I added more status msgs.
  425.   (message "NNTP: awaiting POST connection...")
  426.   (if (nntp-send-command "^[23].*\r$" "POST")
  427.       (progn
  428.     (message "NNTP: got POST connection; encoding text...")
  429.     (nntp-encode-text)
  430.     (message "NNTP: got POST connection; sending text...")
  431.     (nntp-send-region-to-server (point-min) (point-max))
  432.     ;; 1.2a NNTP's post command is buggy. "^M" (\r) is not
  433.     ;;  appended to end of the status message.
  434.     (message "NNTP: text sent; awaiting response...")
  435.     (prog1
  436.         (nntp-wait-for-response "^[23].*$")
  437.       (message "NNTP: post complete.")))))
  438.  
  439. (defun nntp-default-sentinel (proc status)
  440.   "Default sentinel function for NNTP server process."
  441.   (if (and nntp-server-process
  442.        (not (nntp-server-opened)))
  443.       (error "NNTP: Connection closed.")
  444.     ))
  445.  
  446. ;; Encoding and decoding of NNTP text.
  447.  
  448. (defun nntp-decode-text ()
  449.   "Decode text transmitted by NNTP.
  450. 0. Delete status line.
  451. 1. Delete `^M' at end of line.
  452. 2. Delete `.' at end of buffer (end of text mark).
  453. 3. Delete `.' at beginning of line."
  454.   (save-excursion
  455.     (set-buffer nntp-server-buffer)
  456.     ;; Insert newline at end of buffer.
  457.     (goto-char (point-max))
  458.     (if (not (bolp))
  459.     (insert "\n"))
  460.     ;; Delete status line.
  461.     (goto-char (point-min))
  462.     (delete-region (point) (progn (forward-line 1) (point)))
  463.     ;; Delete `^M' at end of line.
  464.     ;; (replace-regexp "\r$" "")
  465.     (while (not (eobp))
  466.       (end-of-line)
  467.       (if (= (preceding-char) ?\r)
  468.       (delete-char -1))
  469.       (forward-line 1)
  470.       )
  471.     ;; Delete `.' at end of buffer (end of text mark).
  472.     (goto-char (point-max))
  473.     (forward-line -1)            ;(beginning-of-line)
  474.     (if (looking-at "^\\.$")
  475.     (delete-region (point) (progn (forward-line 1) (point))))
  476.     ;; Replace `..' at beginning of line with `.'.
  477.     (goto-char (point-min))
  478.     ;; (replace-regexp "^\\.\\." ".")
  479.     (while (search-forward "\n.." nil t)
  480.       (delete-char -1))
  481.     ))
  482.  
  483. (defun nntp-encode-text ()
  484.   "Encode text in current buffer for NNTP transmission.
  485. 1. Insert `.' at beginning of line.
  486. 2. Insert `.' at end of buffer (end of text mark)."
  487.   (save-excursion
  488.     ;; Insert newline at end of buffer.
  489.     (goto-char (point-max))
  490.     (if (not (bolp))
  491.     (insert "\n"))
  492.     ;; Replace `.' at beginning of line with `..'.
  493.     (goto-char (point-min))
  494.     ;; (replace-regexp "^\\." "..")
  495.     (while (search-forward "\n." nil t)
  496.       (insert "."))
  497.     ;; Insert `.' at end of buffer (end of text mark).
  498.     (goto-char (point-max))
  499.     (insert ".\n")
  500.     ))
  501.  
  502.  
  503. ;;;
  504. ;;; Synchronous Communication with NNTP Server.
  505. ;;;
  506.  
  507. (defun nntp-send-command (response cmd &rest args)
  508.   "Wait for server RESPONSE after sending CMD and optional ARGS to server."
  509.   (save-excursion
  510.     ;; Clear communication buffer.
  511.     (set-buffer nntp-server-buffer)
  512.     (erase-buffer)
  513.     (apply 'nntp-send-strings-to-server cmd args)
  514.     (if response
  515.     (nntp-wait-for-response response)
  516.       t)
  517.     ))
  518.  
  519. (defun nntp-wait-for-response (regexp)
  520.   "Wait for server response which matches REGEXP."
  521.   (save-excursion
  522.     (let ((status t)
  523.       (wait t))
  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.             (message "NNTP: Reading..."))
  560.         (nntp-accept-response)
  561.         (if nntp-debug-read
  562.             (message ""))
  563.         ))
  564.         ;; Successfully received server response.
  565.         t
  566.         ))
  567.       )))
  568.  
  569.  
  570. ;;;
  571. ;;; Low-Level Interface to NNTP Server.
  572. ;;; 
  573.  
  574. (defun nntp-send-strings-to-server (&rest strings)
  575.   "Send list of STRINGS to news server as command and its arguments."
  576.   (let ((cmd (car strings))
  577.     (strings (cdr strings)))
  578.     ;; Command and each argument must be separeted by one or more spaces.
  579.     (while strings
  580.       (setq cmd (concat cmd " " (car strings)))
  581.       (setq strings (cdr strings)))
  582.     ;; Command line must be terminated by a CR-LF.
  583.     (process-send-string nntp-server-process (concat cmd "\n"))
  584.     ))
  585.  
  586. (defun nntp-send-region-to-server (begin end)
  587.   "Send current buffer region (from BEGIN to END) to news server."
  588.   (save-excursion
  589.     ;; We have to work in the buffer associated with NNTP server
  590.     ;;  process because of NEmacs hack.
  591.     (copy-to-buffer nntp-server-buffer begin end)
  592.     (set-buffer nntp-server-buffer)
  593.     (setq begin (point-min))
  594.     (setq end (point-max))
  595.     ;; `process-send-region' does not work if text to be sent is very
  596.     ;;  large. I don't know maximum size of text sent correctly.
  597.     (let* ((last nil)
  598.        (size 100)            ;Size of text sent at once.
  599.        (total (/ (- end begin) size))
  600.        (sent 0))
  601.       (save-restriction
  602.     (narrow-to-region begin end)
  603.     (goto-char begin)
  604.     (while (not (eobp))
  605.       (message "NNTP: got POST connection; sending text...(%d%%)"
  606.            (/ (* 100 sent) total))
  607.       (setq sent (+ sent 1))
  608.       ;;(setq last (min end (+ (point) size)))
  609.       ;; NEmacs gets confused if character at `last' is Kanji.
  610.       (setq last (save-excursion
  611.                (goto-char (min end (+ (point) size)))
  612.                (or (eobp) (forward-char 1)) ;Adjust point
  613.                (point)))
  614.       (process-send-region nntp-server-process (point) last)
  615.       ;; I don't know whether the next codes solve the known
  616.       ;;  problem of communication error of GNU Emacs.
  617.       (accept-process-output)
  618.       ;;(sit-for 0)
  619.       (goto-char last)
  620.       )))
  621.     ;; We cannot erase buffer, because reply may be received.
  622.     (delete-region begin end)
  623.     ))
  624.  
  625. (defun nntp-open-server-internal (host &optional service)
  626.   "Open connection to news server on HOST by SERVICE (default is nntp)."
  627.   (save-excursion
  628.     ;; Use TCP/IP stream emulation package if needed.
  629.     (or (fboundp 'open-network-stream)
  630.     (require 'tcp))
  631.     ;; Initialize communication buffer.
  632.     (setq nntp-server-buffer (get-buffer-create " *nntpd*"))
  633.     (set-buffer nntp-server-buffer)
  634.     (buffer-disable-undo (current-buffer))
  635.     (erase-buffer)
  636.     (kill-all-local-variables)
  637.     (setq case-fold-search t)        ;Should ignore case.
  638.     (setq nntp-server-process
  639.       (open-network-stream "nntpd" (current-buffer)
  640.                    host (or service "nntp")))
  641.     (setq nntp-server-name host)
  642.     ;; It is possible to change kanji-fileio-code in this hook.
  643.     (run-hooks 'nntp-server-hook)
  644.     ;; Kill this process without complaint when exiting Emacs.
  645.     (process-kill-without-query nntp-server-process)
  646.     ;; Return the server process.
  647.     nntp-server-process
  648.     ))
  649.  
  650. (defun nntp-close-server-internal ()
  651.   "Close connection to news server."
  652.   (if nntp-server-process
  653.       (delete-process nntp-server-process))
  654.   (if nntp-server-buffer
  655.       (kill-buffer nntp-server-buffer))
  656.   (setq nntp-server-buffer nil)
  657.   (setq nntp-server-process nil))
  658.  
  659. (defun nntp-accept-response ()
  660.   "Read response of server.
  661. It is well-known that the communication speed will be much improved by
  662. defining this function as macro."
  663.   ;; To deal with server process exiting before
  664.   ;;  accept-process-output is called.
  665.   ;; Suggested by Jason Venner <jason@violet.berkeley.edu>.
  666.   ;; This is a copy of `nntp-default-sentinel'.
  667.   (or (memq (process-status nntp-server-process) '(open run))
  668.       (error "NNTP: Connection closed."))
  669.   (if nntp-buggy-select
  670.       (progn
  671.     ;; We cannot use `accept-process-output'.
  672.     ;; Fujitsu UTS requires messages during sleep-for. I don't know why.
  673.     (message "NNTP: Reading...")
  674.     (sleep-for 1)
  675.     (message ""))
  676.     (condition-case errorcode
  677.     (accept-process-output nntp-server-process)
  678.       (error
  679.        (cond ((string-equal "select error: Invalid argument" (nth 1 errorcode))
  680.           ;; Ignore select error.
  681.           nil
  682.           )
  683.          (t
  684.           (signal (car errorcode) (cdr errorcode))))
  685.        ))
  686.     ))
  687.