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
  @user = []

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 (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

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#closeObject 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

Yields:

  • (@config)


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

def configure
  yield @config
  @conf = nil
end

#connectObject

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()
  @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?

Returns:

  • (Boolean)


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 dispatch_messages(type, messages)
  messages.each { |message| dispatch(type, message) }
  messages.length
end

#logObject

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_handlersObject

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.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 @user.include? nick
        @user << 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|
      @user.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 :)



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 #{} 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 send_message(msg, to=nil)
  @muc.say(msg.to_s, to)
end