1 ;;; riece-filter.el --- process filter and sentinel
2 ;; Copyright (C) 1998-2003 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: IRC, riece
8 ;; This file is part of Riece.
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)
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.
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.
27 (require 'riece-handle)
29 (require 'riece-server) ;riece-close-server
30 (require 'riece-identity)
31 (require 'riece-display)
33 (defun riece-handle-numeric-reply (prefix number name string)
34 (let ((base-number (* (/ number 100) 100))
37 (require (intern (format "riece-%03d" base-number)))
39 (setq function (intern-soft (format "riece-handle-%03d-message" number)))
41 (symbol-function function))
44 (format "riece-handle-default-%03d-message" base-number))))
46 (symbol-function function))
48 (funcall function prefix number name
49 (riece-decode-coding-string string))
52 (message "Error occurred in `%S': %S" function error)))))))
54 (defun riece-default-handle-numeric-reply
55 (client-prefix prefix number name string)
57 (list riece-dialogue-buffer riece-others-buffer)
59 (riece-concat-server-name
60 (mapconcat #'identity (riece-split-parameters string) " "))
63 (defun riece-handle-message (prefix message string)
65 (string-match "![^\x00\x0d\x0a @]+@" prefix))
66 (riece-user-set-user-at-host
67 (riece-get-user (substring prefix 0 (match-beginning 0)))
68 (riece-parse-user-at-host (substring prefix (1+ (match-beginning 0))))))
69 (setq message (downcase message)
70 string (riece-decode-coding-string string))
71 (let ((function (intern-soft (concat "riece-handle-" message "-message")))
72 (hook (intern (concat "riece-" message "-hook")))
73 (after-hook (intern (concat "riece-after-" message "-hook"))))
74 (unless (condition-case error
75 (run-hook-with-args-until-success hook prefix string)
78 (message "Error occurred in `%S': %S" hook error))
82 (funcall function prefix string)
85 (message "Error occurred in `%S': %S" function error)))))
87 (run-hook-with-args-until-success after-hook prefix string)
90 (message "Error occurred in `%S': %S" after-hook error)))))))
92 (defun riece-filter (process input)
94 (set-buffer (process-buffer process))
95 (goto-char riece-read-point)
97 (delete-region (riece-line-beginning-position) (point-min))
98 (setq riece-read-point (point)))
100 (goto-char (prog1 riece-read-point
101 (setq riece-read-point (point))))
107 ":\\([^ ]+\\) +\\([0-5][0-9][0-9]\\) +\\([^ ]+\\) +\\(.*\\)\r\n")
108 (riece-handle-numeric-reply
109 (match-string 1) ;prefix
110 (string-to-number (match-string 2)) ;number
111 (match-string 3) ;name
112 (match-string 4)) ;reply string
113 (if (looking-at "\\(:\\([^ ]+\\) +\\)?\\([^ ]+\\) +\\(.*\\)\r\n")
114 (riece-handle-message
115 (match-string 2) ;optional prefix
116 (match-string 3) ;command
117 (match-string 4)) ;params & trailing
118 (if (looking-at ".*\r\n")
120 (message "Weird message from server: %s"
121 (buffer-substring (point) (progn
124 (throw 'contiguous nil)))))
128 (autoload 'riece-exit "riece"))
129 (defun riece-sentinel (process status)
130 (if riece-reconnect-with-password
133 (with-current-buffer (process-buffer process)
135 (riece-close-server-process process)
137 (if (equal server-name "")
139 (riece-server-name-to-server server-name))
141 (setq riece-reconnect-with-password nil))
142 (let ((server-name (with-current-buffer (process-buffer process)
144 (if (and (process-id process) ;not a network connection
145 (string-match "^exited abnormally with code \\([0-9]+\\)"
147 (if (equal server-name "")
148 (message "Connection closed: %s" (match-string 1 status))
149 (message "Connection to \"%s\" closed: %s"
150 server-name (match-string 1 status)))
151 (if (equal server-name "")
152 (message "Connection closed: %s"
153 (substring status 0 (1- (length status))))
154 (message "Connection to \"%s\" closed: %s"
155 server-name (substring status 0 (1- (length status))))))
156 (let ((channels riece-current-channels))
158 (if (and (car channels)
159 (equal (riece-identity-server (car channels))
161 (riece-part-channel (car channels)))
162 (setq channels (cdr channels))))
163 (riece-redisplay-buffers)
164 (riece-close-server-process process)
165 ;; If no server process is available, exit.
166 (unless riece-process-list
169 (provide 'riece-filter)
171 ;;; riece-filter.el ends here