rbot-remote: allow override of function
[rbot] / 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 function = 'say'
21 uri = 'http://localhost:7268/dispatch'
22
23 opts = OptionParser.new
24 opts.on('-u', '--user <user>', "remote user (mandatory)") { |v| user = v }
25 opts.on('-p', '--password <pw>', "remote user password (mandatory)") { |v| pw = v }
26 opts.on('-d', '--destination <user/#channel>', "destination of the action (mandatory)") { |v| dst = v }
27 opts.on('-f', '--function <func>', "function to trigger (e.g. say, notify), default: #{function}") { |v| function = v }
28 opts.on('-r', '--uri <drb uri>', "rbot url (#{uri})") { |v| uri = v }
29 opts.on('-h', '--help', "this message") { |v| pw = nil } # sorry!
30 opts.on('-a', '--about', "what it's all about.") { |v|
31     puts <<EOF
32 This is just a proof-of-concept example for the rbot webserver dispatch feature.
33 This program reads lines of text from the standard input and sends them to a specified irc
34 channel or user via rbot. Make sure you enable the webservice dispatch feature
35 before use.
36
37 The necessary setup is:
38     1) # create a new rbot user ("rmuser", in this example) with a password
39        # ("rmpw", in this example). in an open query to rbot:
40
41        <you> user create rmuser rmpw
42        <rbot> created botuser remote
43
44     2) # add a permission to say for your newly created remote user:
45
46        <you> allow rmuser to do say #channel message
47        <rbot> okies!
48
49     3) # run the #{$0} and type something. the message should
50        # show up on your channel / arrive as an irc private message.
51        
52        [you@yourhost ~]$ ./bin/rbot-remote -u rmuser -p rmpw -d '#your-channel'
53        hello, world!
54        <Ctrl-D>
55        [you@yourhost ~]$
56 EOF
57     exit 0
58 }
59 opts.parse!
60
61 if !pw || !user || !dst
62     puts opts.to_s
63     exit 0
64 end
65
66 uri = URI(uri)
67 uri.user = user
68 uri.password = pw
69
70 loop {
71     s = gets or break
72     s.chomp!
73     resp = Net::HTTP.post_form(uri, 'command' => [function, dst, s].join(' '))
74     puts [resp.code, resp.message, resp.body].join("\t")
75 }
76