#!/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>/) { |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","
"), 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