Class: CeilingCat::Plugin::Messages

Inherits:
Base
  • Object
show all
Defined in:
lib/ceiling_cat/plugins/messages.rb

Instance Attribute Summary

Attributes inherited from Base

#event

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#body, #body_without_command, #body_without_nick, #body_without_nick_or_command, #commands, #initialize, #pluralize, #reply, #room, #store, store, #user, #words

Constructor Details

This class inherits a constructor from CeilingCat::Plugin::Base

Class Method Details

.add(opts = {}) ⇒ Object



55
56
57
58
59
# File 'lib/ceiling_cat/plugins/messages.rb', line 55

def self.add(opts={})
  return false unless opts[:to] && opts[:from] && opts[:body]
  store["messages"] ||= []
  store["messages"] = (store["messages"] + [{:to => opts[:to], :from => opts[:from], :body => opts[:body]}]).uniq
end

.commandsObject

If you want the plugin to run when certain text is sent use commands instead of handle. Ceiling Cat will watch for “![command]” or “[name]: [command” and execute the method for that command.



29
30
31
# File 'lib/ceiling_cat/plugins/messages.rb', line 29

def self.commands
  [{:command => "message for", :description => "Leave a message for someone. i.e. '!message for John Doe: You forgot to lock the door after work last night.'", :method => "save_message", :public => true}]
end

.descriptionObject



33
34
35
# File 'lib/ceiling_cat/plugins/messages.rb', line 33

def self.description
  "A plugin called Messages"
end

.listObject



66
67
68
# File 'lib/ceiling_cat/plugins/messages.rb', line 66

def self.list
  store["messages"] ||= []
end

.messages_for(name) ⇒ Object



70
71
72
73
# File 'lib/ceiling_cat/plugins/messages.rb', line 70

def self.messages_for(name)
  store["messages"] ||= []
  store["messages"].find_all{ |message| message[:to].downcase == name.downcase }
end

.nameObject



37
38
39
# File 'lib/ceiling_cat/plugins/messages.rb', line 37

def self.name
  "Messages"
end

.public?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/ceiling_cat/plugins/messages.rb', line 41

def self.public?
  false
end

.remove(*messages) ⇒ Object



61
62
63
64
# File 'lib/ceiling_cat/plugins/messages.rb', line 61

def self.remove(*messages)
  store["messages"] ||= []
  store["messages"] = store["messages"].reject{ |message| messages.flatten.include?(message) }
end

Instance Method Details

#handleObject

handle manages a plugin’s entire interaction with an event. If you only want to execute commands - “![command]” - leave handle alone (or remove it and define commands below)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ceiling_cat/plugins/messages.rb', line 11

def handle
  if event.type == :entrance
    messages = []
    messages_for_user = self.class.messages_for(user.name)
    if messages_for_user.size > 0
      messages << "Hey #{user.name}! I have a message to deliver to you:"
      messages += messages_for_user.collect{|message| "From #{message[:from]}: #{message[:body]}"}
      reply messages
    end
    self.class.remove(messages_for_user)
    false
  else
    super
  end
end

#save_messageObject



45
46
47
48
49
50
51
52
53
# File 'lib/ceiling_cat/plugins/messages.rb', line 45

def save_message
  recipient,*body = body_without_nick_or_command("message for").split(":")
  if room.users_in_room.any?{|user| user.name == recipient.strip}
    reply "Why leave that messsage? #{recipient.strip} is here!"
  else
    self.class.add(:to => recipient.strip, :from => user.name, :body => body.join(":").strip)
    reply "Message saved! I'll deliver it the next time #{recipient.strip} is around."
  end
end