* riece-handle.el (riece-300): Require 'riece-000, 'riece-200,
[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     (setq function (intern-soft (format "riece-handle-%03d-message" number)))
36     (unless (and function
37                  (symbol-function function))
38       (setq function
39             (intern-soft
40              (format "riece-handle-default-%03d-message" base-number))))
41     (if (and function
42              (symbol-function function))
43         (condition-case error
44             (funcall function prefix number name
45                      (riece-decode-coding-string string))
46           (error
47            (if riece-debug
48                (message "Error in `%S': %S" function error)))))))
49
50 (defun riece-handle-message (prefix message string)
51   (if (and prefix
52            (string-match "![^\x00\x0d\x0a @]+@" prefix))
53       (riece-user-set-user-at-host
54        (riece-get-user (substring prefix 0 (match-beginning 0)))
55        (riece-parse-user-at-host (substring prefix (1+ (match-beginning 0))))))
56   (setq message (downcase message)
57         string (riece-decode-coding-string string))
58   (let ((function (intern-soft (concat "riece-handle-" message "-message")))
59         (hook (intern (concat "riece-" message "-hook")))
60         (after-hook (intern (concat "riece-after-" message "-hook"))))
61     (unless (condition-case error
62                 (run-hook-with-args-until-success hook prefix string)
63               (error
64                (if riece-debug
65                    (message "Error in `%S': %S" hook error))
66                nil))
67       (if function
68           (condition-case error
69               (funcall function prefix string)
70             (error
71              (if riece-debug
72                  (message "Error in `%S': %S" function error)))))
73       (condition-case error
74           (run-hook-with-args-until-success after-hook prefix string)
75         (error
76          (if riece-debug
77              (message "Error in `%S': %S" after-hook error)))))))
78
79 (defun riece-filter (process input)
80   (save-excursion
81     (set-buffer (process-buffer process))
82     (goto-char riece-read-point)
83     (unless riece-debug
84       (delete-region (riece-line-beginning-position) (point-min))
85       (setq riece-read-point (point)))
86     (insert input)
87     (goto-char (prog1 riece-read-point
88                  (setq riece-read-point (point))))
89     (beginning-of-line)
90     (catch 'contiguous
91       (while (not (eobp))
92         (save-excursion
93           (if (looking-at
94                ":\\([^ ]+\\) +\\([0-5][0-9][0-9]\\) +\\([^ ]+\\) +\\(.*\\)\r\n")
95               (riece-handle-numeric-reply
96                (match-string 1)         ;prefix
97                (string-to-number (match-string 2)) ;number
98                (match-string 3)         ;name
99                (match-string 4))                ;reply string
100             (if (looking-at "\\(:\\([^ ]+\\) +\\)?\\([^ ]+\\) +\\(.*\\)\r\n")
101                 (riece-handle-message
102                  (match-string 2)       ;optional prefix
103                  (match-string 3)       ;command
104                  (match-string 4))      ;params & trailing
105               (if (looking-at ".*\r\n")
106                   (if riece-debug
107                       (message "Weird message from server: %s"
108                                (buffer-substring (point) (progn
109                                                            (end-of-line)
110                                                            (point)))))
111                 (throw 'contiguous nil)))))
112         (forward-line)))))
113
114 (eval-when-compile
115   (autoload 'riece-exit "riece"))
116 (defun riece-sentinel (process status)
117   (if riece-reconnect-with-password
118       (let ((server-name
119              (with-current-buffer (process-buffer process)
120                riece-server-name)))
121         (riece-close-server-process process)
122         (riece-open-server
123          (if (equal server-name "")
124              riece-server
125            (riece-server-name-to-server server-name))
126          server-name))
127     (let ((server-name (with-current-buffer (process-buffer process)
128                          riece-server-name)))
129       (if riece-debug
130           (if (equal server-name "")
131               (message "Connection closed: %s"
132                        (substring status 0 (1- (length status))))
133             (message "Connection to \"%s\" closed: %s"
134                      server-name (substring status 0 (1- (length status)))))
135         (if (equal server-name "")
136             (message "Connection closed")
137           (message "Connection to \"%s\" closed" server-name)))
138       (let ((channels riece-current-channels))
139         (while channels
140           (if (and (car channels)
141                    (equal (riece-identity-server (car channels))
142                           server-name))
143               (riece-part-channel (car channels)))
144           (setq channels (cdr channels))))
145       (riece-redisplay-buffers)
146       (riece-close-server-process process)
147       ;; If no server process is available, exit.
148       (unless riece-server-process-alist
149         (riece-exit)))))
150
151 (provide 'riece-filter)
152
153 ;;; riece-filter.el ends here