]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - bin/rbot-remote
Partial revert: reintroduce rbot-remote
[user/henk/code/ruby/rbot.git] / bin / rbot-remote
1 #! /usr/bin/ruby
2
3 require 'uri'
4 require 'net/http'
5 require 'optparse'
6
7 #++
8 #
9 # :title: webserver dispatch example script
10 #
11 # Author:: jsn (dmitry kim) <dmitry dot kim at gmail dot org>
12 # Copyright:: (C) 2007 dmitry kim
13 # License:: in public domain
14 # Modified by:: Giuseppe "Oblomov" Bilotta <giuseppe dot bilotta at gmail dot com>
15 # Copyright:: (C) 2020 Giuseppe Bilotta
16
17 user = nil
18 pw = nil
19 dst = nil
20 uri = 'http://localhost:7268/dispatch'
21
22 opts = OptionParser.new
23 opts.on('-u', '--user <user>', "remote user (mandatory)") { |v| user = v }
24 opts.on('-p', '--password <pw>', "remote user password (mandatory)") { |v| pw = v }
25 opts.on('-d', '--destination <user or #channel>') { |v| dst = v }
26 opts.on('-r', '--uri <drb uri>', "rbot url (#{uri})") { |v| uri = v }
27 opts.on('-h', '--help', "this message") { |v| pw = nil } # sorry!
28 opts.on('-a', '--about', "what it's all about.") { |v|
29     puts <<EOF
30 This is just a proof-of-concept example for the rbot webserver dispatch feature.
31 This program reads lines of text from the standard input and sends them to a specified irc
32 channel or user via rbot. Make sure you enable the webservice dispatch feature
33 before use.
34
35 The necessary setup is:
36     1) # create a new rbot user ("rmuser", in this example) with a password
37        # ("rmpw", in this example). in an open query to rbot:
38
39        <you> user create rmuser rmpw
40        <rbot> created botuser remote
41
42     2) # add a permission to say for your newly created remote user:
43
44        <you> allow rmuser to do say #channel message
45        <rbot> okies!
46
47     3) # run the #{$0} and type something. the message should
48        # show up on your channel / arrive as an irc private message.
49        
50        [you@yourhost ~]$ ./bin/rbot-remote -u rmuser -p rmpw -d '#your-channel'
51        hello, world!
52        <Ctrl-D>
53        [you@yourhost ~]$
54 EOF
55     exit 0
56 }
57 opts.parse!
58
59 if !pw || !user || !dst
60     puts opts.to_s
61     exit 0
62 end
63
64 uri = URI(uri)
65 uri.user = user
66 uri.password = pw
67
68 loop {
69     s = gets or break
70     s.chomp!
71     resp = Net::HTTP.post_form(uri, 'command' => ['say', dst, s].join(' '))
72     puts [resp.code, resp.message, resp.body].join("\t")
73 }
74