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