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