Class: Jabbot::Bot
Overview
Main bot “controller” class
Defined Under Namespace
Classes: Message
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#users ⇒ Object
readonly
Returns the value of attribute users.
Instance Method Summary collapse
-
#close ⇒ Object
(also: #quit)
close connection.
-
#config ⇒ Object
Return configuration.
-
#configure {|@config| ... } ⇒ Object
Configure bot.
-
#connect ⇒ Object
connect to Jabber and join channel.
-
#connected? ⇒ Boolean
still connected?.
-
#debug! ⇒ Object
Enable debugging mode.
-
#dispatch_messages(type, messages) ⇒ Object
Dispatch a collection of messages.
-
#initialize(options = nil) ⇒ Bot
constructor
A new instance of Bot.
-
#log ⇒ Object
Return logger instance.
-
#method_missing(name, *args, &block) ⇒ Object
Map configuration settings.
-
#muc_handlers ⇒ Object
defines what to do on different actions.
-
#poll ⇒ Object
just a lame infinite loop to keep the bot alive while he is connected :).
-
#run! ⇒ Object
Run application.
-
#send_message(msg, to = nil) ⇒ Object
send message alternative: send query to user.
Methods included from Handlers
#add_handler, #dispatch, #handlers, #handlers=
Constructor Details
#initialize(options = nil) ⇒ Bot
Returns a new instance of Bot.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/jabbot/bot.rb', line 20 def initialize( = nil) @conf = nil @config = || Jabbot::Config.default << Jabbot::FileConfig.new @log = nil @abort = false @users = [] rescue Exception => krash raise SystemExit.new(krash.) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
11 12 13 |
# File 'lib/jabbot/bot.rb', line 11 def client @client end |
#users ⇒ Object (readonly)
Returns the value of attribute users.
12 13 14 |
# File 'lib/jabbot/bot.rb', line 12 def users @users end |
Instance Method Details
#close ⇒ Object Also known as: quit
close connection
123 124 125 126 127 128 |
# File 'lib/jabbot/bot.rb', line 123 def close if connected? @connected = false client.close end end |
#config ⇒ Object
Return configuration
250 251 252 253 |
# File 'lib/jabbot/bot.rb', line 250 def config return @conf if @conf @conf = @config.to_hash end |
#configure {|@config| ... } ⇒ Object
Configure bot
232 233 234 235 |
# File 'lib/jabbot/bot.rb', line 232 def configure yield @config @conf = nil end |
#connect ⇒ Object
connect to Jabber and join channel
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 |
# File 'lib/jabbot/bot.rb', line 43 def connect @jid = Jabber::JID.new(login) @mucjid = Jabber::JID.new("#{channel}@#{server}") if @jid.node.nil? raise "Your Jabber ID must contain a user name and therefore contain one @ character." elsif @jid.resource raise "If you intend to set a custom resource, put that in the right text field. Remove the slash!" elsif @mucjid.node.nil? raise "Please set a room name, e.g. [email protected]" elsif @mucjid.resource raise "The MUC room must not contain a resource. Remove the slash!" else @jid.resource = config[:resource] || "jabbot" @mucjid.resource = config[:nick] || "jabbot" @users << config[:nick] end @client = Jabber::Client.new(@jid) @client.on_exception do |*args| $stderr.puts "got an intern EXCEPTION, args where:" $stderr.puts args.inspect $stderr.puts "exiting..." exit end @connected = true begin @client.connect @client.auth(password) @muc = Jabber::MUC::SimpleMUCClient.new(@client) muc_handlers.call(@muc) @muc.join(@mucjid) rescue => errmsg $stderr.write "#{errmsg.class}\n#{errmsg}, #{errmsg.backtrace.join("\n")}" exit 1 end end |
#connected? ⇒ Boolean
still connected?
116 117 118 |
# File 'lib/jabbot/bot.rb', line 116 def connected? @connected end |
#debug! ⇒ Object
Enable debugging mode. All xmpp4r-internal calls to Jabber::Debuglog are printed to $stderr by default. You may change the logger by using
Jabber::Logger = Logger.new(…)
36 37 38 |
# File 'lib/jabbot/bot.rb', line 36 def debug! Jabber::debug = true end |
#dispatch_messages(type, messages) ⇒ Object
Dispatch a collection of messages
213 214 215 216 |
# File 'lib/jabbot/bot.rb', line 213 def (type, ) .each { || dispatch(type, ) } .length end |
#log ⇒ Object
Return logger instance
221 222 223 224 225 226 227 |
# File 'lib/jabbot/bot.rb', line 221 def log return @log if @log os = config[:log_file] ? File.open(config[:log_file], "a") : $stdout @log = Logger.new(os) @log.level = Logger.const_get(config[:log_level] ? config[:log_level].upcase : "INFO") @log end |
#muc_handlers ⇒ Object
defines what to do on different actions
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 |
# File 'lib/jabbot/bot.rb', line 142 def muc_handlers Proc.new do |muc| muc. do |time, nick, text| if time.nil? begin (:message, [Message.new(nick, text, Time.now)]) unless nick == config[:nick] rescue Exception => boom log.fatal boom.inspect log.fatal boom.backtrace[0..5].join("\n") end end end muc. do |time, nick, text| if time.nil? begin (:private, [Message.new(nick, text, Time.now)]) unless nick == config[:nick] rescue Exception => boom log.fatal boom.inspect log.fatal boom.backtrace[0..5].join("\n") end end end muc.on_join do |time, nick| unless @users.include? nick @users << nick end if time.nil? begin (:join, [Message.new(nick, "join", Time.now)]) unless nick == config[:nick] rescue Exception => boom log.fatal boom.inspect log.fatal boom.backtrace[0..5].join("\n") end end end muc.on_leave do |time, nick| @users.delete(nick) if time.nil? begin (:leave, [Message.new(nick, "leave", Time.now)]) rescue Exception => boom log.fatal boom.inspect log.fatal boom.backtrace[0..5].join("\n") end end end muc.on_subject do |time, nick, subject| if time.nil? begin (:subject, [Message.new(nick, subject, Time.now)]) rescue Exception => boom log.fatal boom.inspect log.fatal boom.backtrace[0..5].join("\n") end end end # not working #muc.on_self_leave do |*args| # p args #end end end |
#poll ⇒ Object
just a lame infinite loop to keep the bot alive while he is connected :)
106 107 108 109 110 111 |
# File 'lib/jabbot/bot.rb', line 106 def poll while connected? break unless connected? sleep 1 end end |
#run! ⇒ Object
Run application
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/jabbot/bot.rb', line 85 def run! puts "Jabbot #{Jabbot::VERSION} imposing as #{login} on #{channel}@#{server}" onclose_block = proc { close puts "\nAnd it's a wrap. See ya soon!" exit } Kernel.trap(:INT, onclose_block) Kernel.trap(:QUIT, onclose_block) debug! if config[:debug] connect poll end |
#send_message(msg, to = nil) ⇒ Object
send message alternative: send query to user
135 136 137 |
# File 'lib/jabbot/bot.rb', line 135 def (msg, to=nil) @muc.say(msg.to_s, to) end |