* riece-display.el (riece-display-connect-signals): Update
[riece] / lisp / riece-filter.el
1 ;;; riece-filter.el --- process filter and sentinel
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
10 ;; This program 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 ;; This program 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'riece-handle)
28 (require 'riece-misc)
29 (require 'riece-server)                 ;riece-close-server
30 (require 'riece-identity)
31
32 (defun riece-handle-numeric-reply (prefix number name string)
33   (let ((base-number (* (/ number 100) 100))
34         function)
35     (condition-case nil
36         (require (intern (format "riece-%03d" base-number)))
37       (error))
38     (setq function (intern-soft (format "riece-handle-%03d-message" number)))
39     (unless (and function
40                  (symbol-function function))
41       (setq function
42             (intern-soft
43              (format "riece-handle-default-%03d-message" base-number))))
44     (if (and function
45              (symbol-function function))
46         (condition-case error
47             (funcall function prefix number name
48                      (riece-decode-coding-string string))
49           (error
50            (if riece-debug
51                (message "Error in `%S': %S" function error)))))))
52
53 (defun riece-default-handle-numeric-reply
54   (client-prefix prefix number name string)
55   (riece-insert
56    (list riece-dialogue-buffer riece-others-buffer)
57    (concat client-prefix
58            (riece-concat-server-name
59             (mapconcat #'identity (riece-split-parameters string) " "))
60            "\n")))
61
62 (defun riece-handle-message (prefix message string)
63   (if (and prefix
64            (string-match "![^\x00\x0d\x0a @]+@" prefix))
65       (riece-user-set-user-at-host
66        (riece-get-user (substring prefix 0 (match-beginning 0)))
67        (riece-parse-user-at-host (substring prefix (1+ (match-beginning 0))))))
68   (setq message (downcase message)
69         string (riece-decode-coding-string string))
70   (let ((function (intern-soft (concat "riece-handle-" message "-message")))
71         (hook (intern (concat "riece-" message "-hook")))
72         (after-hook (intern (concat "riece-after-" message "-hook"))))
73     (unless (condition-case error
74                 (run-hook-with-args-until-success hook prefix string)
75               (error
76                (if riece-debug
77                    (message "Error in `%S': %S" hook error))
78                nil))
79       (if function
80           (condition-case error
81               (funcall function prefix string)
82             (error
83              (if riece-debug
84                  (message "Error in `%S': %S" function error)))))
85       (condition-case error
86           (run-hook-with-args-until-success after-hook prefix string)
87         (error
88          (if riece-debug
89              (message "Error in `%S': %S" after-hook error)))))))
90
91 (defun riece-filter (process input)
92   (save-excursion
93     (set-buffer (process-buffer process))
94     (goto-char riece-read-point)
95     (unless riece-debug
96       (delete-region (riece-line-beginning-position) (point-min))
97       (setq riece-read-point (point)))
98     (insert input)
99     (goto-char (prog1 riece-read-point
100                  (setq riece-read-point (point))))
101     (beginning-of-line)
102     (catch 'contiguous
103       (while (not (eobp))
104         (save-excursion
105           (if (looking-at
106                ":\\([^ ]+\\) +\\([0-5][0-9][0-9]\\) +\\([^ ]+\\) +\\(.*\\)\r\n")
107               (riece-handle-numeric-reply
108                (match-string 1)         ;prefix
109                (string-to-number (match-string 2)) ;number
110                (match-string 3)         ;name
111                (match-string 4))                ;reply string
112             (if (looking-at "\\(:\\([^ ]+\\) +\\)?\\([^ ]+\\) +\\(.*\\)\r\n")
113                 (riece-handle-message
114                  (match-string 2)       ;optional prefix
115                  (match-string 3)       ;command
116                  (match-string 4))      ;params & trailing
117               (if (looking-at ".*\r\n")
118                   (if riece-debug
119                       (message "Weird message from server: %s"
120                                (buffer-substring (point) (progn
121                                                            (end-of-line)
122                                                            (point)))))
123                 (throw 'contiguous nil)))))
124         (forward-line)))))
125
126 (eval-when-compile
127   (autoload 'riece-exit "riece"))
128 (defun riece-sentinel (process status)
129   (if riece-reconnect-with-password
130       (let ((server-name
131              (with-current-buffer (process-buffer process)
132                riece-server-name)))
133         (riece-close-server-process process)
134         (riece-open-server
135          (if (equal server-name "")
136              riece-server
137            (riece-server-name-to-server server-name))
138          server-name))
139     (let ((server-name (with-current-buffer (process-buffer process)
140                          riece-server-name)))
141       (if riece-debug
142           (if (equal server-name "")
143               (message "Connection closed: %s"
144                        (substring status 0 (1- (length status))))
145             (message "Connection to \"%s\" closed: %s"
146                      server-name (substring status 0 (1- (length status)))))
147         (if (equal server-name "")
148             (message "Connection closed")
149           (message "Connection to \"%s\" closed" server-name)))
150       (let ((channels riece-current-channels))
151         (while channels
152           (if (and (car channels)
153                    (equal (riece-identity-server (car channels))
154                           server-name))
155               (riece-part-channel (car channels)))
156           (setq channels (cdr channels))))
157       (riece-redisplay-buffers)
158       (riece-close-server-process process)
159       ;; If no server process is available, exit.
160       (unless riece-server-process-alist
161         (riece-exit)))))
162
163 (provide 'riece-filter)
164
165 ;;; riece-filter.el ends here