8ac4e894b369ff126b796344707700bbb1152e01
[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 IRC server via asynchronous proxy
27 ;; which responds to PING request from server in background.
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 to 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 socket = TCPSocket.new(" host ", " service ")
53 $stdout.write(\"NOTICE CONNECTED\r\n\")
54 $stdout.flush
55 loop do
56   rfds, = select([socket, $stdin])
57   if rfds.delete(socket)
58     line = socket.gets(\"\r\n\")
59     break unless line
60     if line =~ /^(?::[^ ]+ +)?PING +(.+)\r\n/i
61       socket.write(\"PONG #{$1}\r\n\")
62       socket.flush
63     else
64       $stdout.write(line)
65       $stdout.flush
66     end
67   end
68   if rfds.delete($stdin)
69     line = $stdin.gets(\"\r\n\")
70     break unless line
71     socket.write(line)
72     socket.flush
73   end
74 end
75 socket.close
76 exit
77 ")
78   "Ruby program of asynchronous proxy"
79   :type 'list
80   :group 'riece-async)
81
82 (defun riece-async-substitute-variables (program variable value)
83   (setq program (copy-sequence program))
84   (let ((pointer program))
85     (while pointer
86       (setq pointer (memq variable program))
87       (if pointer
88           (setcar pointer value)))
89     program))
90
91 ;;;###autoload
92 (defun riece-async-open-network-stream (name buffer host service)
93   (let* ((process-connection-type nil)
94          (process (start-process name buffer "ruby" "-rsocket")))
95     (process-kill-without-query process)
96     (process-send-string process
97                          (apply #'concat
98                                 (riece-async-substitute-variables
99                                  (riece-async-substitute-variables
100                                   riece-async-server-program
101                                   'host
102                                   (concat "'" host "'"))
103                                  'service
104                                  (if (numberp service)
105                                      (number-to-string service)
106                                    (concat "'" service "'")))))
107     (process-send-string process "\0\n") ;input to process is needed
108     (if buffer
109         (save-excursion
110           (set-buffer (process-buffer process))
111           (while (and (eq (process-status process) 'run)
112                       (progn
113                         (goto-char (point-min))
114                         (not (looking-at "NOTICE CONNECTED"))))
115             (accept-process-output process))))
116     process))
117
118 (defun riece-async-insinuate ()
119   (setq riece-default-open-connection-function
120         #'riece-async-open-network-stream))
121
122 (provide 'riece-async)
123
124 ;;; riece-rdcc.el ends here