-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_base.rb
150 lines (126 loc) · 3.36 KB
/
bot_base.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
#!/usr/bin/env ruby
require 'rubygems'
require 'xmpp4r'
require 'xmpp4r/roster'
require 'yaml'
class BotBase
def initialize(username, password, status="I am a bot")
@commands = {}
@friends_sent_to = []
@friends_online = {}
@state = nil
@mainthread = Thread.current
@itunes = OSA.app('itunes')
@ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
@last_message = nil
load_commands
login(username, password)
listen_for_subscription_requests
listen_for_messages
send_initial_presence(status)
Thread.stop
end
def Command(value, &proc)
@commands[value] = proc
end
def load_commands
begin
Dir['commands/*.rb'].each do |command|
file = ""
File.open(command, "r") do |infile|
while (line = infile.gets)
file << line
end
end
instance_eval file
end
rescue StandardError => e
"#{e.message}"
end
end
def login(username, password)
@jid = Jabber::JID.new(username)
@client = Jabber::Client.new(@jid)
@client.connect
@client.auth(password)
end
def escape(input)
@ic.iconv(input)
end
def logout
@mainthread.wakeup
@client.close
end
def send_initial_presence(status)
@client.send(Jabber::Presence.new.set_status(status))
end
def listen_for_subscription_requests
@roster = Jabber::Roster::Helper.new(@client)
@roster.add_subscription_request_callback do |item, pres|
if pres.from.domain == @jid.domain
log "ACCEPTING AUTHORIZATION REQUEST FROM: " + pres.from.to_s
@roster.accept_subscription(pres.from)
end
end
end
def send_message_to(incoming, outgoing)
msg = Jabber::Message.new(incoming.from, escape( outgoing ))
msg.type = :chat
@client.send(msg)
end
def reload_commands(m)
@commands = {}
output = load_commands
send_message_to(m, "#{output} #{@commands.inspect}")
end
def listen_for_messages
@client.add_message_callback do |m|
if m.type != :error && @state == nil
if !@friends_sent_to.include?(m.from)
send_message_to(m, "Hello. I am robot. You are connecting for the first time.")
@friends_sent_to << m.from
end
if m.body == 'reload!'
self.reload_commands(m)
else
send_message_to(m, response_to( m.body ) )
end
end
end
end
def listen_for_presence_notifications
@client.add_presence_callback do |m|
case m.type
when nil # status: available
log "PRESENCE: #{m.from.to_short_s} is online"
@friends_online[m.from.to_short_s] = true
when :unavailable
log "PRESENCE: #{m.from.to_short_s} is offline"
@friends_online[m.from.to_short_s] = false
end
end
end
def log(message)
puts(message) if Jabber::debug
end
def response_to(value)
result = "I don't know how you respond to that :-( "
@commands.each do |k,proc|
regexp = case(k)
when String
Regexp.new("^#{k}$", Regexp::IGNORECASE)
when Regexp
k
else
raise "Commands must be Regexp or String!!!"
end
matches = regexp.match(value)
begin
result = proc.call(matches.to_a[1..-1]) unless matches.nil?
rescue StandardError => e
result = e.message
end
end
result
end
end