Class: Campfire::PollingBot

Inherits:
Bot
  • Object
show all
Defined in:
lib/campfire/polling_bot.rb,
lib/campfire/polling_bot/plugin.rb

Defined Under Namespace

Classes: Plugin

Constant Summary collapse

HEARTBEAT_INTERVAL =

seconds

3

Instance Attribute Summary collapse

Attributes inherited from Bot

#campfire, #config, #debug, #name, #room

Instance Method Summary collapse

Methods inherited from Bot

#base_uri, #logger, #method_missing, #other_person, #say, #say_random

Constructor Details

#initialize(config) ⇒ PollingBot

Returns a new instance of PollingBot.


13
14
15
16
17
# File 'lib/campfire/polling_bot.rb', line 13

def initialize(config)
  # load plugin queue, sorting by priority
  super
  self.plugins = Plugin.load_all(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Campfire::Bot

Instance Attribute Details

#pluginsObject

Returns the value of attribute plugins


10
11
12
# File 'lib/campfire/polling_bot.rb', line 10

def plugins
  @plugins
end

Instance Method Details

#addressed_to_me?(message) ⇒ Boolean

determine if a message is addressed to the bot. if so, store the command in the message

Returns:

  • (Boolean)

112
113
114
115
116
# File 'lib/campfire/polling_bot.rb', line 112

def addressed_to_me?(message)
  m = message.body.match(/^#{name}(?:[,:]\s*|\s+)(.*)/i)
  m ||= message.body.match(/^\s*(.*?)(?:,\s+)?\b#{name}[.!?\s]*$/i)
  message.command = m[1] if m
end

#log_error(e) ⇒ Object


118
119
120
121
122
# File 'lib/campfire/polling_bot.rb', line 118

def log_error(e)
  msg = "Exception: #{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
  logger.error(msg)
  paste(msg) if debug
end

#process(message) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/campfire/polling_bot.rb', line 85

def process(message)
  logger.debug "processing #{message} (#{message.person} - #{message.body})"

  # ignore messages from ourself
  return if [message.person, message.person_full_name].include? self.name

  plugins.each do |plugin|
    begin
      if plugin.accepts?(message)
        logger.debug "sending to plugin #{plugin} (priority #{plugin.priority})"
        status = plugin.process(message)
        if status == Plugin::HALT
          logger.debug "plugin chain halted"
          break
        end
      end
    rescue Exception => e
      say("Oops, #{plugin.class} threw an exception. Enable debugging to see it.") unless debug
      log_error(e)
      break
    end
  end

  logger.debug "done processing #{message}"
end

#runObject

main event loop


20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/campfire/polling_bot.rb', line 20

def run
  # set up a heartbeat thread for plugins that want them
  Thread.new do
    loop do
      plugins.each {|p| p.heartbeat if p.respond_to?(:heartbeat)}
      sleep HEARTBEAT_INTERVAL
    end
  end

  host = "https://#{config.subdomain}.campfirenow.com"
  conn = Firering::Connection.new(host) do |c|
    c.token = config.api_token
    c.logger = logger
    c.retry_delay = 2
  end

  EM.run do
    conn.room(room.id) do |room|
      room.stream do |data|

        begin
          klass = Campfire.const_get(data.type)
          message = klass.new(data)

          if data.from_user?
            data.user do |user|
              dbuser = User.first(:campfire_id => user.id)

              if dbuser.nil?
                dbuser = User.create(
                  :campfire_id => user.id,
                  :name => user.name
                )
              else
                dbuser.update(:name => user.name)
              end

              message.user = dbuser
              process(message)
            end
          else
            process(message)
          end

        rescue => e
          log_error(e)
        end

      end
    end

    trap("INT") { EM.stop; raise SystemExit }
  end

rescue Exception => e # leave the room if we crash
  if e.kind_of?(SystemExit)
    room.leave
    exit 0
  else
    log_error(e)
    room.leave
    exit 1
  end
end