-
Notifications
You must be signed in to change notification settings - Fork 2
/
tykebot.rb
478 lines (427 loc) · 15.5 KB
/
tykebot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#--
# Copyright (c) 2011 Andrew Grmiberg <tykeal@bardicgrove.org>
# TykeBot created with the help & and blatant ripping off of the following
#
# jabber-bot:
# Copyright (c) 2011 Brett Stimmerman <brettstimmerman@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of this project nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# mucbot:
# "THE BEER-WARE LICENSE" (Revision 42):
# <vivien@didelot.org> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return. Vivien Didelot
#++
require 'rubygems'
require 'xmpp4r'
require 'xmpp4r/framework/bot'
require 'xmpp4r/muc'
require 'xmpp4r/vcard'
require 'base64'
require 'sanitize'
require 'shellwords'
require 'lib/utils'
require 'lib/dynamic_loader'
require 'lib/hacks'
require 'lib/tykemuc'
require 'lib/tyke_message'
require 'lib/crontimer'
require 'lib/pubsub'
require 'lib/plugin'
require 'lib/command'
require "lib/naive_bayes"
class TykeBot
# Direct access to the Jabber::Framework::Bot
attr_reader :jabber
# Direct access to the Jabber::MUC::SimpleMUCClient
attr_reader :room
# Access to our config object
attr_reader :config
# plugins
attr_reader :plugins
# CronTimer
attr_reader :timer
# Creates a new Jabber::Framework::Bot object with the specified +config+
# Hash, which must contain +jabber_id+, +password+, and +master+ at a
# minimum.
#
# You may optionally give your bot a custom +name+. If +name+ is omitted,
# the username portion of +jabber_id+ is used instead.
#
# You may choose to restrict a TykeBot to listen only to its master(s),
# or make it +public+.
#
# You may optionally specify a Jabber +presence+, +status+, and +priority+.
# If omitted, they each default to +nil+.
#
# By default, a TykeBot has only a single command, 'help [<command>]',
# which displays a help message for the specified command, or all commands
# if <command> is omitted.
#
# If you choose to make a public bot, only the commands you specify as
# public, as well as the default 'help' command, will be public.
#
# # A minimally configured private bot with a single master.
# # Bot only good for direct one on one chat
# bot = TykeBot.new(
# :jabber_id => 'bot@example.com',
# :password => 'secret',
# :master => 'master@example.com'
# )
#
# # A highly configured public bot with a custom name, mutliple masters,
# # Jabber presence, status, and priority.
# # Bot also capable of doing groupchat
# masters = ['master1@example.com', 'master2@example.com']
#
# bot = Jabber::Bot.new(
# :name => 'PublicBot',
# :jabber_id => 'bot@example.com',
# :server => 'example.com',
# :password => 'secret',
# :master => masters,
# :is_public => true,
# :presence => :chat,
# :priority => 5,
# :status => 'Hello, I am PublicBot.'
# :room => 'testroom',
# :chat_prefix => /^!(.+)$/
# )
#
def initialize(config)
@config = config
@config[:is_public] ||= false
@config[:chat_prefix] ||= /\A!(.+)\Z/m
@config[:avatar] ||= 'data/TykeBot.png'
if @config[:name].nil? || @config[:name].length == 0
@config[:name] = @config[:jabber_id].sub(/@.+$/, '')
end
Jabber.debug = @config[:jabber_debug] || false
logger.level = @config[:debug] ? Logger::DEBUG : Logger::INFO
# Default to asking about unknown commands.
@config[:misunderstood_message] = @config[:misunderstood_message].nil? ? true : @config[:misunderstood_message]
@plugins=[]
@commands = []
@inits=[]
@pubsub = PubSub.new
@timer=CronTimer.new
on(:command) {|message| dispatch_command(message)}
end
# used for room nick
def name
config[:name]
end
# all-in-one helper for default behaviour
def discover_and_load_plugins
load_plugins(discover_plugins)
end
# Look in the file system in the dirs specified in the config or default to 'plugins/'
# supports black listing of plugins by name in config using config[:blacklist_plugins]
def discover_plugins
plugins={}
blacklist=(config[:blacklist_plugins]||[]).map(&:to_s)
plugin_dirs=config[:plugin_dirs]||['plugins']
logger.debug("auto discovering plugins: dirs: #{plugin_dirs.inspect} blacklist: #{blacklist.inspect}")
(config[:plugin_dirs]||['plugins']).map{|dir| Dir.glob(File.join(dir,'*.rb')) + Dir.glob(File.join(dir,'*','init.rb'))}.flatten.compact.uniq.map do |f|
p=Plugin.new(self,f)
unless blacklist.include?(p.name)
plugins[p.name] ||= p
end
end
plugins.values
end
# Load the array of plugins passed. This can be populated by calling discover_plugins.
def load_plugins(plugins)
plugins.sort{|a,b| a.name<=>b.name}.each do |plugin|
begin
logger.debug("Loading plugin: %s from: %s",plugin.name,plugin.file)
DynamicLoader.new{plugin}.load(plugin.file)
@plugins << plugin
rescue
logger.error("failed to load plugin:",$!)
exit if config[:exit_on_load_error]
end
end
end
# a one time callback after plugins are loaded and bot is connected/ready
def add_plugin_init(plugin,&block)
logger.debug("adding plugin %s to init list",plugin.name)
@inits << [plugin,block]
end
# call after loading plugins
def init_plugins
@inits.each do |plugin,callback|
begin
logger.debug("initializing plugin #{plugin.name}")
callback.call(plugin)
rescue
logger.error("failed initialing plugin: %s",plugin.name,$!)
end
end
@inits.clear # only do once!
end
# connect the bot, join the room, and join the pubsub thread
# try to reconnect periodically if we become disconnected
def run(check_connection_every_n_seconds=20)
init_plugins
@pubsub.start_publisher_thread
loop do
begin
unless connected?
connect
join if config[:room]
# Beacon our presence on every successful connection
presence(@config[:presence], @config[:status], @config[:priority])
end
logger.debug("joining pubsub thread...")
@pubsub.join(check_connection_every_n_seconds)
rescue
logger.error
end
end
end
# Connect the bot, making it available to accept commands.
# You can specify a custom startup message with the ':startup_message'
# configuration setting.
def connect
jid = Jabber::JID.new(@config[:jabber_id])
logger.info("Connecting as %s...",jid)
begin
@jabber = Jabber::Framework::Bot.new(jid, @config[:password])
if connected?
logger.info("Connected.")
# connect our avatar
set_avatar
jabber.stream.add_message_callback do |message|
message = TykeMessage.new(self,message)
# push private chats into the firehose as well (this may well break things)
publish(:firehose, message) unless message.delay?
publish_command(message) if valid_command?(message)
end
end
rescue Exception => e
logger.error(e)
end
end
def set_avatar
if not @config[:avatar].nil? && connected?
logger.info("Setting avatar to %s",@config[:avatar])
Thread.new do
vcard = Jabber::Vcard::IqVcard.new
vcard["FN"] = @config[:name]
vcard["NICKNAME"] = @config[:name]
# buddy icon
vcard["PHOTO/TYPE"] = "image/png"
# open buddy icon/avatar image file
image_file = File.new(@config[:avatar], 'r')
# Base64 encode the file contents
image_b64 = Base64.encode64(image_file.read())
# process sha1 hash of photo conents
# this is used by the presence setting
image_file.rewind
@avatar_sha1 = Digest::SHA1.hexdigest(image_file.read())
vcard["PHOTO/BINVAL"] = image_b64
begin
vcard_helper = Jabber::Vcard::Helper.new(@jabber.stream).set(vcard)
rescue
logger.info("#{Time.now} vcard operation timed out.")
end
end
end
end
def connected?
jabber && jabber.stream.is_connected?
end
# Join the bot to the room and enable callbacks.
def join
nick = @config[:name]
serv = @config[:server]
room = @config[:room]
jid = Jabber::JID.new("#{room}@conference.#{serv.split(':').first}/#{nick}")
logger.info("Joining room %s",jid)
# We need a connection or else we'll blow up
if connected?
@room = TykeMuc.new(@jabber.stream)
@room.join(jid)
@room.add_message_callback do |message|
message = TykeMessage.new(self,message)
# don't include past messages in firehose for now
publish(:firehose, message) unless message.delay?
if valid_command?(message)
begin
message.body = strip_prefix(message.body)
publish_command(message)
rescue Exception =>e
puts e.message
end
end
end
@room.add_join_callback do |message|
publish(:welcome, TykeMessage.new(self,message))
end
@room.add_leave_callback do |message|
publish(:leave, TykeMessage.new(self,message))
end
publish(:join)
end
end
# Disconnect the bot. Once the bot has been disconnected, there is no way
# to restart it by issuing a command.
def disconnect
if connected?
send(:to=>@config[:master], :text=>"#{@config[:name]} disconnecting...")
@jabber.stream.close
end
end
def master?(jid)
Array(@config[:master]).include?(jid)
end
# Sets the bot presence, status message and priority.
def presence(presence=nil, status=nil, priority=nil)
@config[:presence] = presence
@config[:status] = status
@config[:priority] = priority
status_message = Jabber::Presence.new(presence, status, priority)
# Attach our avatar info
if not @avatar_sha1.nil?
x = REXML::Element::new("x")
x.add_namespace('vcard-temp:x:update')
photo = REXML::Element::new('photo')
# avatar sha1 that was computed by the add_avatar call
avatar_hash = REXML::Text.new(@avatar_sha1)
# add text to the photo
photo.add(avatar_hash)
# add the photo to x
x.add(photo)
# attach this to our presence
status_message.add_element(x)
end
@jabber.stream.send(status_message) if @jabber.stream.is_connected?
end
# Sets the bot presence. If you need to set more than just the presence,
# use presence() instead.
#
# Available values for presence are:
#
# * nil : online
# * :chat : free for chat
# * :away : away from the computer
# * :dnd : do not disturb
# * :xa : extended away
#
def presence=(presence)
presence(presence, @config[:status], @config[:priority])
end
# Set the bot priority. Priority is an integer from -127 to 127. If you need
# to set more than just the priority, use presence() instead.
def priority=(priority)
presence(@config[:presence], @config[:status], priority)
end
# Set the status message. A status message is just a String, e.g. 'I am
# here.' or 'Out to lunch.' If you need to set more than just the status
# message, use presence() instead.
def status=(status)
presence(@config[:presence], status, @config[:priority])
end
# send text or xhtml to the room or jabber-id
#
# :text=>nil | string
# :xhtml=>nil | xhtml-string
# :to=>nil | jabber-id | [jabber-id]
#
# requires :text or :xhtml
# if :to is nil, this will send to the room
def send(options)
text = options[:text]
xhtml = options[:xhtml]
to = options[:to]
return unless text || xhtml
if to
Array(to).flatten.each { |t|
xhtml ? @jabber.send_message_xhtml(t, xhtml, text) : @jabber.send_message(t, text)
}
else
xhtml ? @room.say_xhtml(xhtml,text) : @room.say(text)
end
end
# NOTE: this returns a new object, so can't add commands via this
# have to use add_command.
def commands(public_only=true)
@commands.select(&:enabled).reject{|c| public_only && !c.public?}
end
def add_command(command)
@commands << command
end
def public?
config[:is_public]
end
def publish(name,*params)
@pubsub.publish(name,*params)
end
def on(name,&callback)
@pubsub.on(name,&callback)
end
def before(name,&callback)
@pubsub.before(name,&callback)
end
private
# initialize our avatar_sha1 to nothing
@avatar_sha1 = nil
def valid_command?(message) #:nodoc:
(message.body? && !message.delay? && !message.sender.bot?) &&
(message.chat? ||
(message.room? && strip_prefix(message.body)))
end
# strip off the :chat_prefix by returning the first group match
def strip_prefix(body)
return unless p=body.to_s.strip.match(@config[:chat_prefix])
p[1]
end
def publish_command(message)
logger.debug("COMMAND: #{message.body}")
publish :command, message if public? || message.sender.admin?
end
def dispatch_command(message)
# make sure its proper command line format!
begin
args = parse_command(message)
@commands.each do |c|
begin
c.message(self,message,args)
rescue
send :to=> message.chat? ? message.sender.jid : nil, :text=>"Internal Bot Error!!! #{$!}"
error
end
end
rescue
send :to=> message.chat? ? message.sender.jid : nil, :text=>"Invalid command: #{$!}"
end
end
def parse_command(message)
message.body.to_s.shellsplit
end
end
# vim:ts=2:sw=2:expandtab