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.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#close ⇒ Object
(also: #quit)
close connection.
-
#configure {|@config| ... } ⇒ Object
Configure bot.
-
#connect ⇒ Object
connect to Jabber and join channel.
-
#connected? ⇒ Boolean
still connected?.
-
#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.
-
#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 @user = [] rescue Exception => krash raise SystemExit.new(krash.) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
Map configuration settings
220 221 222 223 224 225 |
# File 'lib/jabbot/bot.rb', line 220 def method_missing(name, *args, &block) return super unless config.key?(name) self.class.send(:define_method, name) { config[name] } config[name] end |
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 |
#user ⇒ Object (readonly)
Returns the value of attribute user.
12 13 14 |
# File 'lib/jabbot/bot.rb', line 12 def user @user end |
Instance Method Details
#close ⇒ Object Also known as: quit
close connection
102 103 104 105 106 107 |
# File 'lib/jabbot/bot.rb', line 102 def close if connected? @connected = false client.close end end |
#configure {|@config| ... } ⇒ Object
Configure bot
211 212 213 214 |
# File 'lib/jabbot/bot.rb', line 211 def configure yield @config @conf = nil end |
#connect ⇒ Object
connect to Jabber and join channel
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 |
# File 'lib/jabbot/bot.rb', line 34 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" @user << config[:nick] end @client = Jabber::Client.new(@jid) @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?
95 96 97 |
# File 'lib/jabbot/bot.rb', line 95 def connected? @connected end |
#dispatch_messages(type, messages) ⇒ Object
Dispatch a collection of messages
192 193 194 195 |
# File 'lib/jabbot/bot.rb', line 192 def (type, ) .each { || dispatch(type, ) } .length end |
#log ⇒ Object
Return logger instance
200 201 202 203 204 205 206 |
# File 'lib/jabbot/bot.rb', line 200 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
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 |
# File 'lib/jabbot/bot.rb', line 121 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 @user.include? nick @user << 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| @user.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 :)
85 86 87 88 89 90 |
# File 'lib/jabbot/bot.rb', line 85 def poll while connected? break unless connected? sleep 1 end end |
#run! ⇒ Object
Run application
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/jabbot/bot.rb', line 69 def run! puts "Jabbot #{Jabbot::VERSION} imposing as #{login} on #{channel}@#{server}" trap(:INT) do puts "\nAnd it's a wrap. See ya soon!" exit end connect poll end |
#send_message(msg, to = nil) ⇒ Object
send message alternative: send query to user
114 115 116 |
# File 'lib/jabbot/bot.rb', line 114 def (msg, to=nil) @muc.say(msg.to_s, to) end |