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 (defvar riece-filter-semaphore '(nil))
35 (defun riece-handle-numeric-reply (prefix number name string)
36 (let ((base-number (* (/ number 100) 100))
38 (setq function (intern-soft (format "riece-handle-%03d-message" number)))
40 (symbol-function function))
43 (format "riece-handle-default-%03d-message" base-number))))
45 (symbol-function function))
46 (riece-ignore-errors (symbol-name function)
47 (funcall function prefix number name
48 (riece-decode-coding-string string))))))
50 (defun riece-handle-message (prefix message string)
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 (symbol-name hook)
62 (run-hook-with-args-until-success hook prefix string))
64 (riece-ignore-errors (symbol-name function)
65 (funcall function prefix string)))
66 (riece-ignore-errors (symbol-name after-hook)
67 (run-hook-with-args-until-success after-hook prefix string)))))
69 ;;; stolen (and renamed) from gnus-async.el.
70 (defun riece-get-semaphore (semaphore)
71 "Wait until SEMAPHORE is released."
72 (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
75 (defun riece-release-semaphore (semaphore)
77 (setcdr (symbol-value semaphore) nil))
79 (defmacro riece-filter-with-semaphore (&rest forms)
82 (riece-get-semaphore 'riece-filter-semaphore)
84 (riece-release-semaphore 'riece-filter-semaphore)))
86 (put 'riece-filter-with-semaphore 'lisp-indent-function 0)
87 (put 'riece-filter-with-semaphore 'edebug-form-spec '(body))
89 (defsubst riece-chomp-string (string)
90 (if (string-match "\r\\'" string)
91 (substring string 0 (match-beginning 0))
94 (defun riece-filter (process input)
95 (riece-filter-with-semaphore
97 (set-buffer (process-buffer process))
98 (goto-char riece-read-point)
100 (delete-region (riece-line-beginning-position) (point-min))
101 (setq riece-read-point (point)))
103 (goto-char (prog1 riece-read-point
104 (setq riece-read-point (point))))
106 (while (and (not (eobp))
107 (looking-at ".*\n")) ;the input line is not finished
110 ":\\([^ ]+\\) +\\([0-5][0-9][0-9]\\) +\\([^ ]+\\) +\\(.*\\)\n")
111 (riece-handle-numeric-reply
112 (match-string 1) ;prefix
113 (string-to-number (match-string 2)) ;number
114 (match-string 3) ;name
115 (riece-chomp-string (match-string 4))) ;reply string
116 (if (looking-at "\\(:\\([^ ]+\\) +\\)?\\([^ ]+\\) +\\(.*\\)\n")
117 (riece-handle-message
118 (match-string 2) ;optional prefix
119 (match-string 3) ;command
120 (riece-chomp-string (match-string 4))) ;params & trailing
122 (message "Weird message from server: %s"
123 (buffer-substring (point) (progn
129 (autoload 'riece-exit "riece"))
130 (defun riece-sentinel (process status)
131 (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 (let ((server-name (with-current-buffer (process-buffer process)
144 (if (equal server-name "")
145 (message "Connection closed: %s"
146 (substring status 0 (1- (length status))))
147 (message "Connection to \"%s\" closed: %s"
148 server-name (substring status 0 (1- (length status)))))
149 (if (equal server-name "")
150 (message "Connection closed")
151 (message "Connection to \"%s\" closed" server-name)))
152 (let ((channels riece-current-channels))
154 (if (and (car channels)
155 (equal (riece-identity-server (car channels))
157 (riece-part-channel (car channels)))
158 (setq channels (cdr channels))))
159 (riece-redisplay-buffers)
160 (riece-close-server-process process)
161 ;; If no server process is available, exit.
162 (unless riece-server-process-alist
165 (provide 'riece-filter)
167 ;;; riece-filter.el ends here