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'
20 class ::WebServiceUser < Irc::User
21 def initialize(str, botuser, opts={})
27 attr_accessor :response
30 class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet
31 def initialize(server, bot)
39 post = CGI::parse(req.body)
42 command = uri.gsub('/', ' ').strip
44 username = post['username'].first
45 password = post['password'].first
47 botuser = @bot.auth.get_botuser(username)
48 netmask = '%s!%s@%s' % [botuser.username, botuser.username, ip]
50 if not botuser or botuser.password != password
51 raise 'Permission Denied'
54 ws_user = WebServiceUser.new(netmask, botuser)
55 message = Irc::PrivMessage.new(@bot, nil, ws_user, @bot.myself, command)
57 @bot.plugins.irc_delegate('privmsg', message)
60 res['Content-Type'] = 'text/plain'
61 res.body = ws_user.response.join("\n") + "\n"
65 class WebServiceModule < CoreBotModule
67 Config.register Config::BooleanValue.new('webservice.autostart',
69 :requires_rescan => true,
70 :desc => 'Whether the web service should be started automatically')
72 Config.register Config::IntegerValue.new('webservice.port',
73 :default => 7260, # that's 'rbot'
74 :requires_rescan => true,
75 :desc => 'Port on which the web service will listen')
77 Config.register Config::StringValue.new('webservice.host',
78 :default => '127.0.0.1',
79 :requires_rescan => true,
80 :desc => 'Host the web service will bind on')
82 Config.register Config::BooleanValue.new('webservice.ssl',
84 :requires_rescan => true,
85 :desc => 'Whether the web server should use SSL (recommended!)')
87 Config.register Config::StringValue.new('webservice.ssl_key',
88 :default => '~/.rbot/wskey.pem',
89 :requires_rescan => true,
90 :desc => 'Private key file to use for SSL')
92 Config.register Config::StringValue.new('webservice.ssl_cert',
93 :default => '~/.rbot/wscert.pem',
94 :requires_rescan => true,
95 :desc => 'Certificate file to use for SSL')
99 @port = @bot.config['webservice.port']
100 @host = @bot.config['webservice.host']
103 start_service if @bot.config['webservice.autostart']
105 error "couldn't start web service provider: #{e.inspect}"
110 raise "Remote service provider already running" if @server
111 opts = {:BindAddress => @host, :Port => @port}
112 if @bot.config['webservice.ssl']
113 opts.merge! :SSLEnable => true
114 cert = File.expand_path @bot.config['webservice.ssl_cert']
115 key = File.expand_path @bot.config['webservice.ssl_key']
116 if File.exists? cert and File.exists? key
117 debug 'using ssl certificate files'
119 :SSLCertificate => OpenSSL::X509::Certificate.new(File.read(cert)),
120 :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read(key))
123 debug 'using on-the-fly generated ssl certs'
124 opts.merge! :SSLCertName => [ %w[CN localhost] ]
125 # the problem with this is that it will always use the same
126 # serial number which makes this feature pretty much useless.
129 @server = WEBrick::HTTPServer.new(opts)
130 debug 'webservice started: ' + opts.inspect
131 @server.mount('/dispatch', DispatchServlet, @bot)
132 Thread.new { @server.start }
136 @server.shutdown if @server
145 def handle_start(m, params)
148 s << 'web service already running'
152 s << 'web service started'
154 s << 'unable to start web service, error: ' + $!.to_s
162 webservice = WebServiceModule.new
164 webservice.map 'webservice start',
165 :action => 'handle_start',
166 :auth_path => ':manage:'
168 webservice.map 'webservice stop',
169 :action => 'handle_stop',
170 :auth_path => ':manage:'
172 webservice.default_auth('*', false)