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