New file.
[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-funcall-ignore-errors (if (symbolp function)
45                                          (symbol-name function)
46                                        (format "numeric-reply-%d" number))
47                                      function prefix number name
48                                      (riece-decode-coding-string string)))))
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 (riece-ignore-errors (if (symbolp hook)
62                                      (symbol-name hook)
63                                    (format "%s-hook" message))
64               (run-hook-with-args-until-success hook prefix string))
65       (if function
66           (riece-funcall-ignore-errors (if (symbolp function)
67                                            (symbol-name function)
68                                          (format "message-%s" message))
69                                        function prefix string))
70       (riece-ignore-errors (if (symbolp after-hook)
71                                (symbol-name after-hook)
72                              (format "%s-after-hook" message))
73         (run-hook-with-args-until-success after-hook prefix string)))))
74
75 (defsubst riece-chomp-string (string)
76   (if (string-match "\r\\'" string)
77       (substring string 0 (match-beginning 0))
78     string))
79
80 (defun riece-filter (process input)
81   (save-excursion
82     (set-buffer (process-buffer process))
83     (goto-char (point-max))
84     (insert input)
85     (goto-char riece-read-point)
86     (beginning-of-line)
87     (while (looking-at ".*\n")  ;the input line is finished
88       (save-excursion
89         (if (looking-at
90              ":\\([^ ]+\\) +\\([0-5][0-9][0-9]\\) +\\([^ ]+\\) +\\(.*\\)")
91             (riece-handle-numeric-reply
92              (match-string 1)           ;prefix
93              (string-to-number (match-string 2)) ;number
94              (match-string 3)           ;name
95              (riece-chomp-string (match-string 4))) ;reply string
96           (if (looking-at "\\(:\\([^ ]+\\) +\\)?\\([^ ]+\\) +\\(.*\\)")
97               (riece-handle-message
98                (match-string 2)         ;optional prefix
99                (match-string 3)         ;command
100                (riece-chomp-string (match-string 4))) ;params & trailing
101             (if riece-debug
102                 (message "Weird message from server: %s"
103                          (buffer-substring (point) (progn
104                                                      (end-of-line)
105                                                      (point))))))))
106       (forward-line))
107     (unless riece-debug
108       (delete-region (point-min) (point)))
109     (setq riece-read-point (point))))
110
111 (eval-when-compile
112   (autoload 'riece-exit "riece"))
113 (defun riece-sentinel (process status)
114   (if riece-reconnect-with-password
115       (let ((server-name
116              (with-current-buffer (process-buffer process)
117                riece-server-name)))
118         (riece-close-server-process process)
119         (riece-open-server
120          (if (equal server-name "")
121              riece-server
122            (riece-server-name-to-server server-name))
123          server-name))
124     (let ((server-name (with-current-buffer (process-buffer process)
125                          riece-server-name)))
126       (if riece-debug
127           (if (equal server-name "")
128               (riece-debug (format "Connection closed: %s"
129                                    (substring status 0 (1- (length status)))))
130             (riece-debug (format "Connection to \"%s\" closed: %s"
131                                  server-name
132                                  (substring status 0 (1- (length status))))))
133         (if (equal server-name "")
134             (message "Connection closed")
135           (message "Connection to \"%s\" closed" server-name)))
136       (let ((channels riece-current-channels))
137         (while channels
138           (if (and (car channels)
139                    (equal (riece-identity-server (car channels))
140                           server-name))
141               (riece-part-channel (car channels)))
142           (setq channels (cdr channels))))
143       (riece-redisplay-buffers)
144       (riece-close-server-process process)
145       ;; If no server process is available, exit.
146       (unless riece-server-process-alist
147         (riece-exit)))))
148
149 (provide 'riece-filter)
150
151 ;;; riece-filter.el ends here