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
|
#!/usr/bin/env ruby
require 'rubygems'
require 'xmpp4r'
require 'xmpp4r/version'
require 'xmpp4r/muc'
require 'xmpp4r/delay/x/delay'
require 'xmpp4r/pubsub'
require 'xmpp4r/pubsub/helper/servicehelper.rb'
require 'xmpp4r/pubsub/helper/nodebrowser.rb'
require 'xmpp4r/pubsub/helper/nodehelper.rb'
require 'xmpp4r/muc/helper/mucbrowser'
require 'net/http'
require 'cgi'
require 'iconv'
require 'xmlsimple'
require 'open-uri'
require 'json'
include Jabber
Jabber::debug = true
def count_users()
jid, password, muc_jid = Jabber::JID.new($botjid), $botpass, Jabber::JID.new($mucjid)
cl = Jabber::Client.new(jid)
cl.connect
cl.auth(password)
browser = Jabber::MUC::MUCBrowser.new(cl)
print "Querying #{muc_jid} for identity..."; $stdout.flush
name = browser.muc_name(muc_jid)
if name.nil?
puts " Sorry, but the queried MUC component doesn't seem to support MUC or Groupchat."
else
puts " #{name}"
print "Querying #{muc_jid} for its rooms..."; $stdout.flush
rooms = browser.muc_rooms(muc_jid)
puts " #{rooms.size} rooms found"
max_room_length = 0
rooms.each_key { |jid| max_room_length = jid.to_s.size if jid.to_s.size > max_room_length }
rooms.each { |jid,name|
puts "#{jid.to_s.ljust(max_room_length)} #{name}"
}
end
return rooms.size
cl.close
end
def time(stan)
time = nil
stan.each_element('x') { |x|
if x.kind_of?(Delay::XDelay)
time = x.stamp
end
}
return time
end
def send_response(answer,msg_body)
if answer != nil
response = Jabber::Message.new()
response.type = :chat
response.body = answer
if msg_body != nil
case msg_body
when /^!google+/
response.add_element(prepare_html(response.body))
responsenohtml = []
response.body.scan(/<a href=\"(.+?)\">(.+?)<\/a>/) { |url,title|
responsenohtml.push("#{title} -> #{url}")
}
response.body = responsenohtml.join("\n").gsub(/<.*?>/, '')
end
return response
end
end
end
def prepare_html(text)
h = REXML::Element::new("html")
h.add_namespace('http://jabber.org/protocol/xhtml-im')
b = REXML::Element::new("body")
b.add_namespace('http://www.w3.org/1999/xhtml')
t = REXML::Text.new(text.gsub("\n","<br />"), false, nil, true, nil, %r/.^/ )
b.add(t)
h.add(b)
h
end
class RbJbBot
begin
config = XmlSimple.xml_in('config.xml', { 'KeyAttr' => 'name' })
p config['bot'][0]['botjid'][0]
client = Jabber::Client.new(Jabber::JID.new("#{config['bot'][0]['botjid'][0]}/#{config['bot'][0]['botnick'][0]}"))
client.connect
client.auth(config['bot'][0]['botpass'][0])
Jabber::Version::SimpleResponder.new(client, config['bot'][0]['NAME'][0], config['bot'][0]['VERSION'][0], config['bot'][0]['DISTRIB'][0])
muc = Jabber::MUC::MUCClient.new(client)
muc.join(Jabber::JID.new(config['bot'][0]['mucjid'][0] + '/' + config['bot'][0]['botnick'][0]), config['bot'][0]['mucpass'][0])
module_list = config['bot'][0]['module']
module_list.each do |mod| require "modules/#{mod}" end
muc.add_message_callback { |msg|
sender_nick = msg.from.resource
unless time(msg)
if sender_nick != $botnick
answer = nil
module_list.each do |mod| answer_tmp = eval "module_#{mod}(msg.body, sender_nick, config)"; if answer_tmp != nil and answer != ""; answer=answer_tmp; end; end
if answer != nil and answer != ""
muc.send(send_response(answer,msg.body))
end
end
end
}
end
RbJbBot.new
Thread.stop
client.close
end
|