30eb6583410e343867edcabba2b00e57e837aede
[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 (defgroup riece-async nil
41   "Connect to IRC server via asynchronous proxy"
42   :prefix "riece-"
43   :group 'riece)
44
45 (defcustom riece-async-ruby-command "ruby"
46   "Command name for Ruby interpreter."
47   :type 'string
48   :group 'riece-async)
49
50 (defcustom riece-async-server-program
51   '("\
52 require 'io/nonblock'
53 socket = TCPSocket.new(" host ", " service ")
54 $stdout.write(\"NOTICE CONNECTED #{$$}\r\n\")
55 $stdout.flush
56 $stdout.nonblock = true
57 trap('STOP', 'IGNORE')
58 trap('TSTP', 'IGNORE')
59 wfds_in = []
60 buf = ''
61 loop do
62   rfds, wfds, = select([socket, $stdin], wfds_in)
63   unless wfds.empty?
64     begin
65       until buf.empty?
66         len = $stdout.syswrite(buf)
67         buf.slice!(0 .. len)
68       end
69       wfds_in = []
70     rescue Errno::EAGAIN
71     end
72   end
73   if rfds.include?(socket)
74     line = socket.gets(\"\r\n\")
75     break unless line
76     if line =~ /^(?::[^ ]+ +)?PING +(.+)\r\n/i
77       socket.write(\"PONG #{$1}\r\n\")
78       socket.flush
79     else
80       wfds_in = [$stdout]
81       buf << line
82       until buf.length <= " max-buffer-size "
83         buf.slice!(0 .. buf.index(\"\r\n\"))
84       end
85     end
86   end
87   if rfds.include?($stdin)
88     line = $stdin.gets(\"\r\n\")
89     break unless line
90     socket.write(line)
91     socket.flush
92   end
93 end
94 socket.close
95 exit
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 (defun riece-async-substitute-variables (program variable value)
107   (setq program (copy-sequence program))
108   (let ((pointer program))
109     (while pointer
110       (setq pointer (memq variable program))
111       (if pointer
112           (setcar pointer value)))
113     program))
114
115 ;;;###autoload
116 (defun riece-async-open-network-stream (name buffer host service)
117   (let* ((process-connection-type nil)
118          (process (start-process name buffer "ruby" "-rsocket")))
119     (process-kill-without-query process)
120     (process-send-string process
121                          (apply #'concat
122                                 (riece-async-substitute-variables
123                                  (riece-async-substitute-variables
124                                   (riece-async-substitute-variables
125                                    riece-async-server-program
126                                    'host
127                                    (concat "'" host "'"))
128                                   'service
129                                   (if (numberp service)
130                                       (number-to-string service)
131                                     (concat "'" service "'")))
132                                  'max-buffer-size
133                                  (number-to-string
134                                   riece-async-max-buffer-size))))
135     (process-send-string process "\0\n") ;input to process is needed
136     (if buffer
137         (save-excursion
138           (set-buffer (process-buffer process))
139           (while (and (eq (process-status process) 'run)
140                       (progn
141                         (goto-char (point-min))
142                         (not (looking-at (format "NOTICE CONNECTED %d"
143                                                  (process-id process))))))
144             (accept-process-output process))))
145     process))
146
147 (defun riece-async-insinuate ()
148   (setq riece-default-open-connection-function
149         #'riece-async-open-network-stream))
150
151 (provide 'riece-async)
152
153 ;;; riece-rdcc.el ends here