4 # :title: Web service for bot
6 # Author:: Matthias Hecker (apoc@geekosphere.org)
8 # HTTP(S)/json based web service for remote controlling the bot,
9 # similar to remote but much more portable.
11 # For more info/documentation:
12 # https://github.com/4poc/rbot/wiki/Web-Service
16 require 'webrick/https'
21 class ::WebServiceUser < Irc::User
22 def initialize(str, botuser, opts={})
28 attr_accessor :response
31 class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet
32 def initialize(server, bot)
37 def dispatch_command(command, botuser, ip)
38 netmask = '%s!%s@%s' % [botuser.username, botuser.username, ip]
40 user = WebServiceUser.new(netmask, botuser)
41 message = Irc::PrivMessage.new(@bot, nil, user, @bot.myself, command)
43 @bot.plugins.irc_delegate('privmsg', message)
45 { :reply => user.response }
48 # Handle a dispatch request.
50 post = CGI::parse(req.body)
53 username = post['username'].first
54 password = post['password'].first
55 command = post['command'].first
57 botuser = @bot.auth.get_botuser(username)
58 raise 'Permission Denied' if not botuser or botuser.password != password
60 ret = dispatch_command(command, botuser, ip)
63 if req['Accept'] == 'application/json'
64 res['Content-Type'] = 'application/json'
65 res.body = JSON.dump ret
67 res['Content-Type'] = 'text/plain'
68 res.body = ret[:reply].join("\n") + "\n"
73 class WebServiceModule < CoreBotModule
75 Config.register Config::BooleanValue.new('webservice.autostart',
77 :requires_rescan => true,
78 :desc => 'Whether the web service should be started automatically')
80 Config.register Config::IntegerValue.new('webservice.port',
81 :default => 7260, # that's 'rbot'
82 :requires_rescan => true,
83 :desc => 'Port on which the web service will listen')
85 Config.register Config::StringValue.new('webservice.host',
86 :default => '127.0.0.1',
87 :requires_rescan => true,
88 :desc => 'Host the web service will bind on')
90 Config.register Config::BooleanValue.new('webservice.ssl',
92 :requires_rescan => true,
93 :desc => 'Whether the web server should use SSL (recommended!)')
95 Config.register Config::StringValue.new('webservice.ssl_key',
96 :default => '~/.rbot/wskey.pem',
97 :requires_rescan => true,
98 :desc => 'Private key file to use for SSL')
100 Config.register Config::StringValue.new('webservice.ssl_cert',
101 :default => '~/.rbot/wscert.pem',
102 :requires_rescan => true,
103 :desc => 'Certificate file to use for SSL')
107 @port = @bot.config['webservice.port']
108 @host = @bot.config['webservice.host']
111 start_service if @bot.config['webservice.autostart']
113 error "couldn't start web service provider: #{e.inspect}"
118 raise "Remote service provider already running" if @server
119 opts = {:BindAddress => @host, :Port => @port}
120 if @bot.config['webservice.ssl']
121 opts.merge! :SSLEnable => true
122 cert = File.expand_path @bot.config['webservice.ssl_cert']
123 key = File.expand_path @bot.config['webservice.ssl_key']
124 if File.exists? cert and File.exists? key
125 debug 'using ssl certificate files'
127 :SSLCertificate => OpenSSL::X509::Certificate.new(File.read(cert)),
128 :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read(key))
131 debug 'using on-the-fly generated ssl certs'
132 opts.merge! :SSLCertName => [ %w[CN localhost] ]
133 # the problem with this is that it will always use the same
134 # serial number which makes this feature pretty much useless.
137 # Logging to file in ~/.rbot
138 logfile = File.open(@bot.path('webservice.log'), 'a+')
140 :Logger => WEBrick::Log.new(logfile),
141 :AccessLog => [[logfile, WEBrick::AccessLog::COMBINED_LOG_FORMAT]]
143 @server = WEBrick::HTTPServer.new(opts)
144 debug 'webservice started: ' + opts.inspect
145 @server.mount('/dispatch', DispatchServlet, @bot)
146 Thread.new { @server.start }
150 @server.shutdown if @server
159 def handle_start(m, params)
162 s << 'web service already running'
166 s << 'web service started'
168 s << 'unable to start web service, error: ' + $!.to_s
176 webservice = WebServiceModule.new
178 webservice.map 'webservice start',
179 :action => 'handle_start',
180 :auth_path => ':manage:'
182 webservice.map 'webservice stop',
183 :action => 'handle_stop',
184 :auth_path => ':manage:'
186 webservice.default_auth('*', false)