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