
(provide (quote nntp))

(defvar nntp-server-hook nil "\
*Hooks for the NNTP server.
If the kanji code of the NNTP server is different from the local kanji
code, the correct kanji code of the buffer associated with the NNTP
server must be specified as follows:

(setq nntp-server-hook
      (function
       (lambda ()
	 ;; Server's Kanji code is EUC (NEmacs hack).
	 (make-local-variable 'kanji-fileio-code)
	 (setq kanji-fileio-code 0))))

If you'd like to change something depending on the server in this
hook, use the variable `nntp-server-name'.")

(defvar nntp-large-newsgroup 50 "\
*The number of the articles which indicates a large newsgroup.
If the number of the articles is greater than the value, verbose
messages will be shown to indicate the current status.")

(defvar nntp-buggy-select (memq system-type (quote (usg-unix-v fujitsu-uts))) "\
*T if your select routine is buggy.
If the select routine signals error or fall into infinite loop while
waiting for the server response, the variable must be set to t.  In
case of Fujitsu UTS, it is set to T since `accept-process-output'
doesn't work properly.")

(defvar nntp-maximum-request 400 "\
*The maximum number of the requests sent to the NNTP server at one time.
If Emacs hangs up while retrieving headers, set the variable to a
lower value.")

(defvar nntp-debug-read t "\
*If non-nil, show the current status about reading the NNTP server output.")

(defconst nntp-version "NNTP 3.10" "\
Version numbers of this version of NNTP.")

(defvar nntp-server-name nil "\
The name of the host running NNTP server.")

(defvar nntp-server-buffer nil "\
Buffer associated with NNTP server process.")

(defvar nntp-server-process nil "\
The NNTP server process.
You'd better not use this variable in NNTP front-end program but
instead use `nntp-server-buffer'.")

(defvar nntp-status-string nil "\
Save the server response message.
You'd better not use this variable in NNTP front-end program but
instead call function `nntp-status-message' to get status message.")

(defmacro nntp-header-number (header) "Return article number in HEADER." (byte-code "E" [header aref 0] 3))

(defmacro nntp-set-header-number (header number) "Set article number of HEADER to NUMBER." (byte-code "	F" [header number aset 0] 4))

(defmacro nntp-header-subject (header) "Return subject string in HEADER." (byte-code "E" [header aref 1] 3))

(defmacro nntp-set-header-subject (header subject) "Set article subject of HEADER to SUBJECT." (byte-code "	F" [header subject aset 1] 4))

(defmacro nntp-header-from (header) "Return author string in HEADER." (byte-code "E" [header aref 2] 3))

(defmacro nntp-set-header-from (header from) "Set article author of HEADER to FROM." (byte-code "	F" [header from aset 2] 4))

(defmacro nntp-header-xref (header) "Return xref string in HEADER." (byte-code "E" [header aref 3] 3))

(defmacro nntp-set-header-xref (header xref) "Set article xref of HEADER to xref." (byte-code "	F" [header xref aset 3] 4))

(defmacro nntp-header-lines (header) "Return lines in HEADER." (byte-code "E" [header aref 4] 3))

(defmacro nntp-set-header-lines (header lines) "Set article lines of HEADER to LINES." (byte-code "	F" [header lines aset 4] 4))

(defmacro nntp-header-date (header) "Return date in HEADER." (byte-code "E" [header aref 5] 3))

(defmacro nntp-set-header-date (header date) "Set article date of HEADER to DATE." (byte-code "	F" [header date aset 5] 4))

(defmacro nntp-header-id (header) "Return Id in HEADER." (byte-code "E" [header aref 6] 3))

(defmacro nntp-set-header-id (header id) "Set article Id of HEADER to ID." (byte-code "	F" [header id aset 6] 4))

(defmacro nntp-header-references (header) "Return references in HEADER." (byte-code "E" [header aref 7] 3))

(defmacro nntp-set-header-references (header ref) "Set article references of HEADER to REF." (byte-code "	F" [header ref aset 7] 4))

(defun nntp-retrieve-headers (sequence) "\
Return list of article headers specified by SEQUENCE of article id.
The format of list is
 `([NUMBER SUBJECT FROM XREF LINES DATE MESSAGE-ID REFERENCES] ...)'.
Reader macros for the vector are defined as `nntp-header-FIELD'.
Writer macros for the vector are defined as `nntp-set-header-FIELD'.
News group must be selected before calling me." (byte-code "q 
Ge
	
 
@\"
AT
?K \"!  b#d TT `W ! 	V \"! \"	\"\" Q + db#! dZb!?   ! 	V !	eb# # ! 	V !ebm?:!
!!\"!!!\"
!ǉ	ǉǉ։ǉǉm?Ng>?!!!\"!f\"w\"	\"\"!\"@ \"*!E	?A 	?B C 	
&B!S!6	V6\"!6D \"	\"\" !N	VNE !F !.)" [nntp-server-buffer number sequence last-point received count headers nil article subject message-id from xref lines date references nntp-maximum-request t nntp-large-newsgroup s c erase-buffer 0 nntp-send-strings-to-server "HEAD" zerop % accept-process-output re-search-forward "^[0-9]" numberp 20 message "NNTP: %d%% of headers received." / * 100 nntp-accept-response re-search-backward looking-at "^[23]" 3 "^\\.$" "NNTP: 100%% of headers received." "\\(?
[ 	]+\\)+" replace-match " " "NNTP: Parsing headers..." "^[23][0-9][0-9][ 	]+\\([0-9]+\\)[ 	]+\\(<[^>]+>\\)" string-to-int buffer-substring match-beginning 1 match-end 2 forward-line (50 51) "\\(From\\|Subject\\|Date\\|Lines\\|Xref\\|References\\):[ 	]+\\([^ 	
]+.*\\)$" char-equal 70 83 68 76 88 82 "(None)" "(Unknown User)" vector "NNTP: Parsing headers... %d%%" "NNTP: Parsing headers... done" nreverse] 57))

(defun nntp-open-server (host &optional service) "\
Open news server on HOST.
If HOST is nil, use value of environment variable `NNTPSERVER'.
If optional argument SERVICE is non-nil, open by the service name." (byte-code " !ȉ \"/ !	* \",  7 ?7 ω	*" [host status nil nntp-status-string service nntp-server-process getenv "NNTPSERVER" "" nntp-open-server-internal nntp-wait-for-response "^[23].*$" set-process-sentinel nntp-default-sentinel nntp-close-server-internal "NNTP server is not specified."] 7))

(defun nntp-close-server nil "\
Close news server." (byte-code " != \"  \")" [nntp-server-process nil ((byte-code " " [nntp-close-server-internal] 2)) nntp-default-sentinel process-sentinel set-process-sentinel nntp-server-opened nntp-send-command "QUIT"] 6))

(fset (quote nntp-request-quit) (symbol-function (quote nntp-close-server)))

(defun nntp-server-opened nil "\
Return server process status, T or NIL.
If the stream is opened, return T, otherwise return NIL." (byte-code "	 !>" [nntp-server-process process-status (open run)] 3))

(defun nntp-status-message nil "\
Return server status response as string." (byte-code " \" !!O Ƈ" [nntp-status-string string-match "[0-9][0-9][0-9][ 	]+\\([^]*\\).*$" match-beginning 1 match-end ""] 6))

(defun nntp-request-article (id) "\
Select article by message ID (or number)." (byte-code "# " [id nntp-send-command "^\\.$" "ARTICLE" nntp-decode-text] 4))

(defun nntp-request-body (id) "\
Select article body by message ID (or number)." (byte-code "# " [id nntp-send-command "^\\.$" "BODY" nntp-decode-text] 4))

(defun nntp-request-head (id) "\
Select article head by message ID (or number)." (byte-code "# " [id nntp-send-command "^\\.$" "HEAD" nntp-decode-text] 4))

(defun nntp-request-stat (id) "\
Select article by message ID (or number)." (byte-code "#" [id nntp-send-command "^[23].*$" "STAT"] 4))

(defun nntp-request-group (group) "\
Select news GROUP." (byte-code "#" [group nntp-send-command "^[23].*$" "GROUP"] 4))

(defun nntp-request-list nil "\
List valid newsgoups." (byte-code "\" " [nntp-send-command "^\\.$" "LIST" nntp-decode-text] 4))

(defun nntp-request-last nil "\
Set current article pointer to the previous article
in the current news group." (byte-code "\"" [nntp-send-command "^[23].*$" "LAST"] 3))

(defun nntp-request-next nil "\
Advance current article pointer." (byte-code "\"" [nntp-send-command "^[23].*$" "NEXT"] 3))

(defun nntp-request-post nil "\
Post a new news in current buffer." (byte-code "\"  ed\"!" [nntp-send-command "^[23].*$" "POST" nntp-encode-text nntp-send-region-to-server nntp-wait-for-response "^[23].*$"] 5))

(defun nntp-default-sentinel (proc status) "\
Default sentinel function for NNTP server process." (byte-code "  ? !" [nntp-server-process nntp-server-opened error "NNTP: Connection closed."] 3))

(defun nntp-decode-text nil "\
Decode text transmitted by NNTP.
0. Delete status line.
1. Delete `^M' at end of line.
2. Delete `.' at end of buffer (end of text mark).
3. Delete `.' at beginning of line." (byte-code "qdbn? ceb`!`\"m?4  hU, !! db!!J `!`\"eb#] !N )" [nntp-server-buffer nil t "
" delete-region forward-line 1 end-of-line 13 delete-char -1 looking-at "^\\.$" search-forward "
.."] 13))

(defun nntp-encode-text nil "\
Encode text in current buffer for NNTP transmission.
1. Insert `.' at beginning of line.
2. Insert `.' at end of buffer (end of text mark)." (byte-code "dbn? ceb# c dbc)" [nil t "
" search-forward "
." "." ".
"] 4))

(defun nntp-send-command (response cmd &rest args) "\
Wait for server RESPONSE after sending CMD and optional ARGS to server." (byte-code "q 	
# ! )" [nntp-server-buffer cmd args response t erase-buffer apply nntp-send-strings-to-server nntp-wait-for-response] 5))

(defun nntp-wait-for-response (regexp) "\
Wait for server response which matches REGEXP." (byte-code "q 
4 eb! ĉ0 !. ĉĉ0    e`\"y 
w db!!_ ĉs g ! s !G *)" [status t wait nntp-server-buffer nil nntp-status-string regexp nntp-debug-read nntp-accept-response looking-at "[23]" "[45]" end-of-line buffer-substring forward-line -1 message "NNTP: Reading..." ""] 13))

(defun nntp-send-strings-to-server (&rest strings) "\
Send list of STRINGS to news server as command and its arguments." (byte-code "	@	A	 	@Q	A 
P\"*" [cmd strings nntp-server-process " " process-send-string "
"] 4))

(defun nntp-send-region-to-server (begin end) "\
Send current buffer region (from BEGIN to END) to news server." (byte-code "	
#qed	
\"	bm?I 
`\\^bm3 !`)`# b )*	
\")" [nntp-server-buffer begin end last nil size nntp-server-process copy-to-buffer 100 narrow-to-region forward-char 1 process-send-region accept-process-output delete-region] 8))

(defun nntp-open-server-internal (host &optional service) "\
Open connection to news server on HOST by SERVICE (default is nntp)." (byte-code "!
 !!qp!  p+ $!)" [nntp-server-buffer case-fold-search t nntp-server-process host service nntp-server-name fboundp open-network-stream require tcp get-buffer-create " *nntpd*" buffer-flush-undo erase-buffer kill-all-local-variables "nntpd" "nntp" run-hooks nntp-server-hook] 11))

(defun nntp-close-server-internal nil "\
Close connection to news server." (byte-code " !	 	!" [nntp-server-process nntp-server-buffer nil delete-process kill-buffer] 4))

(defun nntp-accept-response nil "\
Read response of server.
It is well-known that the communication speed will be much improved by
defining this function as macro." (byte-code "!> !	 !!!\" ͏" [nntp-server-process nntp-buggy-select process-status (open run) error "NNTP: Connection closed." message "NNTP: Reading..." sleep-for 1 "" errorcode (byte-code "!" [nntp-server-process accept-process-output] 2) ((error (byte-code "8\"  @A\"" [errorcode nil t string-equal "select error: Invalid argument" 1 signal] 4)))] 8))
