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 PingServlet < WEBrick::HTTPServlet::AbstractServlet
32 def initialize(server, bot)
38 res['Content-Type'] = 'text/plain'
43 class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet
44 def initialize(server, bot)
49 def dispatch_command(command, botuser, ip)
50 netmask = '%s!%s@%s' % [botuser.username, botuser.username, ip]
52 user = WebServiceUser.new(netmask, botuser)
53 message = Irc::PrivMessage.new(@bot, nil, user, @bot.myself, command)
55 @bot.plugins.irc_delegate('privmsg', message)
57 { :reply => user.response }
60 # Handle a dispatch request.
62 post = CGI::parse(req.body)
65 username = post['username'].first
66 password = post['password'].first
67 command = post['command'].first
69 botuser = @bot.auth.get_botuser(username)
70 raise 'Permission Denied' if not botuser or botuser.password != password
72 ret = dispatch_command(command, botuser, ip)
75 if req['Accept'] == 'application/json'
76 res['Content-Type'] = 'application/json'
77 res.body = JSON.dump ret
79 res['Content-Type'] = 'text/plain'
80 res.body = ret[:reply].join("\n") + "\n"
85 class WebServiceModule < CoreBotModule
87 Config.register Config::BooleanValue.new('webservice.autostart',
89 :requires_rescan => true,
90 :desc => 'Whether the web service should be started automatically')
92 Config.register Config::IntegerValue.new('webservice.port',
93 :default => 7260, # that's 'rbot'
94 :requires_rescan => true,
95 :desc => 'Port on which the web service will listen')
97 Config.register Config::StringValue.new('webservice.host',
98 :default => '127.0.0.1',
99 :requires_rescan => true,
100 :desc => 'Host the web service will bind on')
102 Config.register Config::BooleanValue.new('webservice.ssl',
104 :requires_rescan => true,
105 :desc => 'Whether the web server should use SSL (recommended!)')
107 Config.register Config::StringValue.new('webservice.ssl_key',
108 :default => '~/.rbot/wskey.pem',
109 :requires_rescan => true,
110 :desc => 'Private key file to use for SSL')
112 Config.register Config::StringValue.new('webservice.ssl_cert',
113 :default => '~/.rbot/wscert.pem',
114 :requires_rescan => true,
115 :desc => 'Certificate file to use for SSL')
119 @port = @bot.config['webservice.port']
120 @host = @bot.config['webservice.host']
123 start_service if @bot.config['webservice.autostart']
125 error "couldn't start web service provider: #{e.inspect}"
130 raise "Remote service provider already running" if @server
131 opts = {:BindAddress => @host, :Port => @port}
132 if @bot.config['webservice.ssl']
133 opts.merge! :SSLEnable => true
134 cert = File.expand_path @bot.config['webservice.ssl_cert']
135 key = File.expand_path @bot.config['webservice.ssl_key']
136 if File.exists? cert and File.exists? key
137 debug 'using ssl certificate files'
139 :SSLCertificate => OpenSSL::X509::Certificate.new(File.read(cert)),
140 :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read(key))
143 debug 'using on-the-fly generated ssl certs'
144 opts.merge! :SSLCertName => [ %w[CN localhost] ]
145 # the problem with this is that it will always use the same
146 # serial number which makes this feature pretty much useless.
149 # Logging to file in ~/.rbot
150 logfile = File.open(@bot.path('webservice.log'), 'a+')
152 :Logger => WEBrick::Log.new(logfile),
153 :AccessLog => [[logfile, WEBrick::AccessLog::COMBINED_LOG_FORMAT]]
155 @server = WEBrick::HTTPServer.new(opts)
156 debug 'webservice started: ' + opts.inspect
157 @server.mount('/dispatch', DispatchServlet, @bot)
158 @server.mount('/ping', PingServlet, @bot)
159 Thread.new { @server.start }
163 @server.shutdown if @server
172 def handle_start(m, params)
175 s << 'web service already running'
179 s << 'web service started'
181 s << 'unable to start web service, error: ' + $!.to_s
189 webservice = WebServiceModule.new
191 webservice.map 'webservice start',
192 :action => 'handle_start',
193 :auth_path => ':manage:'
195 webservice.map 'webservice stop',
196 :action => 'handle_stop',
197 :auth_path => ':manage:'
199 webservice.default_auth('*', false)