Fixed.
[riece] / lisp / riece-async.el
1 ;;; riece-async.el --- connect to IRC server via asynchronous proxy
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: IRC, riece
6
7 ;; This file is part of Riece.
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; This program allows to connect to an IRC server via local proxy
27 ;; which responds to PING requests from server.
28
29 ;; To use, add the following line to your ~/.riece/init.el:
30 ;; (add-to-list 'riece-addons 'riece-async)
31
32 ;; If you want to enable this feature per server, write the server
33 ;; spec like this:
34 ;; (add-to-list 'riece-server-alist
35 ;;              '("async" :host "irc.tokyo.wide.ad.jp"
36 ;;                :function riece-async-open-network-stream))
37
38 ;;; Code:
39
40 (require 'riece-options)
41 (require 'riece-ruby)                   ;riece-ruby-command,
42                                         ;riece-ruby-substitute-variables
43
44 (defgroup riece-async nil
45   "Connect to IRC server via asynchronous proxy"
46   :prefix "riece-"
47   :group 'riece)
48
49 (defcustom riece-async-server-program
50   '("\
51 orequire 'io/nonblock'
52 socket = TCPSocket.new(" host ", " service ")
53 $stdout.write(\"NOTICE CONNECTED #{$$}\\r\\n\")
54 $stdout.flush
55 $stdout.nonblock = true
56 trap('STOP', 'IGNORE')
57 trap('TSTP', 'IGNORE')
58 wfds_in = []
59 buf = ''
60 loop do
61   rfds, wfds, = select([socket, $stdin], wfds_in)
62   unless wfds.empty?
63     until buf.length <= " max-buffer-size "
64       i = buf.index(\"\\r\\n\")
65       break unless i
66       buf.slice!(0 .. i + 1)
67     end
68     begin
69       until buf.empty?
70         len = $stdout.syswrite(buf)
71         buf.slice!(0 .. len)
72       end
73       wfds_in = []
74     rescue Errno::EAGAIN
75     end
76   end
77   if rfds.include?(socket)
78     line = socket.gets(\"\\r\\n\")
79     break unless line
80     if line =~ /^(?::[^ ]+ +)?PING +(.+)\\r\\n/i
81       socket.write(\"PONG #{$1}\\r\\n\")
82       socket.flush
83     else
84       wfds_in = [$stdout]
85       buf << line
86     end
87   end
88   if rfds.include?($stdin)
89     line = $stdin.gets(\"\\r\\n\")
90     break unless line
91     socket.write(line)
92     socket.flush
93   end
94 end
95 socket.close
96 ")
97   "Ruby program of asynchronous proxy."
98   :type 'list
99   :group 'riece-async)
100
101 (defcustom riece-async-max-buffer-size 65535
102   "Maximum size of the write buffer."
103   :type 'integer
104   :group 'riece-async)
105
106 (defconst riece-async-description
107   "Keep IRC connection with external process")
108
109 ;;;###autoload
110 (defun riece-async-open-network-stream (name buffer host service)
111   (let* ((process-connection-type nil)
112          (process (start-process name buffer riece-ruby-command "-rsocket")))
113     (process-kill-without-query process)
114     (process-send-string process
115                          (riece-ruby-substitute-variables
116                           (list (cons 'host
117                                       (concat "'" host "'"))
118                                 (cons 'service
119                                       (if (numberp service)
120                                           (number-to-string service)
121                                         (concat "'" service "'")))
122                                 (cons 'max-buffer-size
123                                       (number-to-string
124                                        riece-async-max-buffer-size)))))
125     (process-send-string process "\0\n") ;input to process is needed
126     (if buffer
127         (save-excursion
128           (set-buffer (process-buffer process))
129           (while (and (eq (process-status process) 'run)
130                       (progn
131                         (goto-char (point-min))
132                         (not (looking-at (format "NOTICE CONNECTED %d"
133                                                  (process-id process))))))
134             (accept-process-output process))))
135     process))
136
137 (defun riece-async-insinuate ()
138   (setq riece-default-open-connection-function
139         #'riece-async-open-network-stream))
140
141 (provide 'riece-async)
142
143 ;;; riece-rdcc.el ends here