search: be more rbot-ish in ddg
[rbot] / bin / rbot-remote
1 #! /usr/bin/ruby
2 require 'drb'
3 require 'optparse'
4
5 #++
6 #
7 # :title: RemoteCtl example script
8 #
9 # Author:: jsn (dmitry kim) <dmitry dot kim at gmail dot org>
10 # Copyright:: (C) 2007 dmitry kim
11 # License:: in public domain
12
13 user = nil
14 pw = nil
15 dst = nil
16 uri = 'druby://localhost:7268'
17
18 opts = OptionParser.new
19 opts.on('-u', '--user <user>', "remote user (mandatory)") { |v| user = v }
20 opts.on('-p', '--password <pw>', "remote user password (mandatory)") { |v| pw = v }
21 opts.on('-d', '--destination <user or #channel>') { |v| dst = v }
22 opts.on('-r', '--uri <drb uri>', "rbot url (#{uri})") { |v| uri = v }
23 opts.on('-h', '--help', "this message") { |v| pw = nil } # sorry!
24 opts.on('-a', '--about', "what it's all about.") { |v|
25     puts <<EOF
26 This is just a proof-of-concept example for rbot druby-based api. This program
27 reads lines of text from the standard input and sends them to a specified irc
28 channel or user via rbot. Make sure you have remotectl.rb plugin loaded before
29 use.
30
31 The necessary setup is:
32     1) # create a new rbot user ("rmuser", in this example) with a password
33        # ("rmpw", in this example). in an open query to rbot:
34
35        <you> user create rmuser rmpw
36        <rbot> created botuser remote
37
38     2) # add a remotectl permission to your newly created remote user:
39
40        <you> permissions set +remotectl for rmuser
41        <rbot> okies!
42
43     3) # add specific permissions for the commands you want to allow via
44        # remote interface. for example, in this script we want 'say',
45        # 'action' and other basic commands to work:
46
47        <you> permissions set +basics::talk::do for rmuser
48        <rbot> alright
49
50     4) # run the #{$0} and type something. the message should
51        # show up on your channel / arrive as an irc private message.
52        
53        [you@yourhost ~]$ ./bin/rbot-remote -u rmuser -p rmpw -d '#your-channel'
54        hello, world!
55        <Ctrl-D>
56        [you@yourhost ~]$
57 EOF
58     exit 0
59 }
60 opts.parse!
61
62 if !pw || !user || !dst
63     puts opts.to_s
64     exit 0
65 end
66
67 rbot = DRbObject.new_with_uri(uri)
68 id = rbot.delegate(nil, "remote login #{user} #{pw}")[:return]
69 puts "id is #{id.inspect}"
70 loop {
71     s = gets or break
72     s.chomp!
73     rv = rbot.delegate(id, "dispatch say #{dst} #{s}") or break
74     puts "rv is #{rv.inspect}"
75 }
76