wesabe / wesabot

Wesabe's Campfire bot framework

This URL has Read+Write access

wesabot / campfire / polling_bot / plugins / status_plugin.rb
100644 98 lines (86 sloc) 2.652 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class StatusPlugin < Campfire::PollingBot::Plugin
  accepts :text_message
  priority -1
 
  def process(message)
    if message.addressed_to_me?
      case message.body
      when /(what's|what is)\s+(everyone\s+)?(up(\s+to)?|doing|going\s+on|happening)/i
        show_statuses
        return HALT
      end
    end
 
    case message.body
    when /(?:what's|what is|where's|where is)\s+(\w+)/i
      person = $1
      if person =~ /everyone/i
        show_statuses
      else
        status = Status.first(:person => person.downcase)
        if status.nil?
          # I don't know
          return unless message.addressed_to_me?
          bot.say("I don't know what #{person} is doing.")
        else
          bot.say("#{person} is #{status.value}")
        end
      end
 
      return HALT
    end
 
    return unless message.addressed_to_me?
 
    case message.command
    when /(?:set\s+)?(?:my\s+)?status(?:\s+is|\s+to)(?:\s*:)?\s*(.+)/i
      status = strip_quotes($1)
      update_status(message.person, status)
      return HALT
    when /(?:I'm|I am)\s+(.+)/i
      status = strip_quotes($1)
      update_status(message.person, status)
      return HALT
    when /(?:show|list)\s+(\S+)?\s*status(?:es)?/i
      person = $1 ? $1.sub(/'s$/i, '') : 'all'
      if person =~ /(everyone|all)/i
        show_statuses
      else
        show_status_for(person)
      end
      return HALT
    end
  end
 
  def help
    [["set my status to: <status>", "set your status"],
     ["show <person>'s status", "show the status for <person>"],
     ["list all statuses", "show the statuses for everyone"],
     ["what's <person> up to?", "show the status for <person> -- works without addressing me"]]
  end
 
  private
 
  def strip_quotes(string)
    return string && string.gsub(/(['"])([^\1]*)(\1)/) { $2 }
  end
 
  def show_statuses
    statuses = Status.all(:order => [:person])
    if statuses.any?
      bot.say("Here's what everyone said they were doing:")
      statuses.each { |status| bot.say(" #{status.person} - #{status.value}") }
    else
      bot.say("Sorry, no one wants to share their activities with a lowly bot.")
    end
  end
 
  def show_status_for(person)
    person.downcase!
    status = Status.first(:person => person)
    if status
      bot.say("#{person} is #{status.value}")
    else
      bot.say("I don't know what #{person} is doing.")
    end
  end
 
  def update_status(person, value)
    person.downcase!
    if status = Status.first(:person => person)
      status.update_attributes(:value => value)
    else
      Status.create(:person => person, :value => value)
    end
    bot.say("Duly noted, #{person}.")
  end
end