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-debug)
33 (defun riece-handle-numeric-reply (prefix number name string)
34 (let ((base-number (* (/ number 100) 100))
36 (setq function (intern-soft (format "riece-handle-%03d-message" number)))
38 (symbol-function function))
41 (format "riece-handle-default-%03d-message" base-number))))
43 (symbol-function function))
44 (riece-funcall-ignore-errors (symbol-name function)
45 function prefix number name
46 (riece-decode-coding-string string)))))
48 (defun riece-handle-message (prefix message string)
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))
62 (riece-funcall-ignore-errors (symbol-name function)
63 function prefix string))
64 (riece-ignore-errors (symbol-name after-hook)
65 (run-hook-with-args-until-success after-hook prefix string)))))
67 (defsubst riece-chomp-string (string)
68 (if (string-match "\r\\'" string)
69 (substring string 0 (match-beginning 0))
72 (defun riece-filter (process input)
74 (set-buffer (process-buffer process))
75 (goto-char (point-max))
77 (goto-char riece-read-point)
79 (while (looking-at ".*\n") ;the input line is finished
82 ":\\([^ ]+\\) +\\([0-5][0-9][0-9]\\) +\\([^ ]+\\) +\\(.*\\)")
83 (riece-handle-numeric-reply
84 (match-string 1) ;prefix
85 (string-to-number (match-string 2)) ;number
86 (match-string 3) ;name
87 (riece-chomp-string (match-string 4))) ;reply string
88 (if (looking-at "\\(:\\([^ ]+\\) +\\)?\\([^ ]+\\) +\\(.*\\)")
90 (match-string 2) ;optional prefix
91 (match-string 3) ;command
92 (riece-chomp-string (match-string 4))) ;params & trailing
94 (message "Weird message from server: %s"
95 (buffer-substring (point) (progn
100 (delete-region (point-min) (point)))
101 (setq riece-read-point (point))))
104 (autoload 'riece-exit "riece"))
105 (defun riece-sentinel (process status)
106 (if riece-reconnect-with-password
108 (with-current-buffer (process-buffer process)
110 (riece-close-server-process process)
112 (if (equal server-name "")
114 (riece-server-name-to-server server-name))
116 (let ((server-name (with-current-buffer (process-buffer process)
119 (if (equal server-name "")
120 (message "Connection closed: %s"
121 (substring status 0 (1- (length status))))
122 (message "Connection to \"%s\" closed: %s"
123 server-name (substring status 0 (1- (length status)))))
124 (if (equal server-name "")
125 (message "Connection closed")
126 (message "Connection to \"%s\" closed" server-name)))
127 (let ((channels riece-current-channels))
129 (if (and (car channels)
130 (equal (riece-identity-server (car channels))
132 (riece-part-channel (car channels)))
133 (setq channels (cdr channels))))
134 (riece-redisplay-buffers)
135 (riece-close-server-process process)
136 ;; If no server process is available, exit.
137 (unless riece-server-process-alist
140 (provide 'riece-filter)
142 ;;; riece-filter.el ends here