Class: Jabbot::Bot

Inherits:
Object
  • Object
show all
Includes:
Handlers
Defined in:
lib/jabbot/bot.rb

Overview

Main bot “controller” class

Defined Under Namespace

Classes: Message

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = nil)
  @conf = nil
  @config = options || Jabbot::Config.default << Jabbot::FileConfig.new
  @log = nil
  @abort = false
  @users = []

rescue Exception => krash
  raise SystemExit.new(krash.message)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Map configuration settings



230
231
232
233
234
235
# File 'lib/jabbot/bot.rb', line 230

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

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/jabbot/bot.rb', line 11

def client
  @client
end

#usersObject (readonly)

Returns the value of attribute users.



12
13
14
# File 'lib/jabbot/bot.rb', line 12

def users
  @users
end

Instance Method Details

#closeObject Also known as: quit

close connection



113
114
115
116
117
118
# File 'lib/jabbot/bot.rb', line 113

def close
  if connected?
    @connected = false
    client.close
  end
end

#configObject

Return configuration



240
241
242
243
# File 'lib/jabbot/bot.rb', line 240

def config
  return @conf if @conf
  @conf = @config.to_hash
end

#configure {|@config| ... } ⇒ Object

Configure bot

Yields:



222
223
224
225
# File 'lib/jabbot/bot.rb', line 222

def configure
  yield @config
  @conf = nil
end

#connectObject

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
# File 'lib/jabbot/bot.rb', line 43

def connect
  #Jabber::debug = true
  @jid = Jabber::JID.new()
  @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)
  @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?

Returns:

  • (Boolean)


106
107
108
# File 'lib/jabbot/bot.rb', line 106

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



203
204
205
206
# File 'lib/jabbot/bot.rb', line 203

def dispatch_messages(type, messages)
  messages.each { |message| dispatch(type, message) }
  messages.length
end

#logObject

Return logger instance



211
212
213
214
215
216
217
# File 'lib/jabbot/bot.rb', line 211

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_handlersObject

defines what to do on different actions



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
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/jabbot/bot.rb', line 132

def muc_handlers
  Proc.new do |muc|
    muc.on_message do |time, nick, text|
      if time.nil?
        begin
          dispatch_messages(: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.on_private_message do |time, nick, text|
      if time.nil?
        begin
          dispatch_messages(: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
          dispatch_messages(: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
          dispatch_messages(: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
          dispatch_messages(: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

#pollObject

just a lame infinite loop to keep the bot alive while he is connected :)



96
97
98
99
100
101
# File 'lib/jabbot/bot.rb', line 96

def poll
  while connected?
    break unless connected?
    sleep 1
  end
end

#run!Object

Run application



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jabbot/bot.rb', line 79

def run!
  puts "Jabbot #{Jabbot::VERSION} imposing as #{} on #{channel}@#{server}"

  trap(:INT) do
    puts "\nAnd it's a wrap. See ya soon!"
    exit
  end

  debug! if config[:debug]
  connect
  poll
end

#send_message(msg, to = nil) ⇒ Object

send message alternative: send query to user



125
126
127
# File 'lib/jabbot/bot.rb', line 125

def send_message(msg, to=nil)
  @muc.say(msg.to_s, to)
end