Class: Robut::Plugin::Alias

Inherits:
Object
  • Object
show all
Includes:
Robut::Plugin
Defined in:
lib/robut/plugin/alias.rb

Overview

Alias robut commands:

Later if @robut receives the message “@robut something” he will repond as if he received “@robut some long message”

Valid use:

@robut alias this "something long"
@robut alias "this thing" "something long"
@robut alias this something_long

Listing all aliases

@robut aliases

Removing aliases

@robut remove alias "this alias"
@robut remove alias this
@robut remove clear aliases # removes everything

Note: you probably want the Alias plugin as one of the first things in the plugin array (since plugins are executed in order).

Instance Attribute Summary

Attributes included from Robut::Plugin

#connection, #private_sender, #reply_to

Instance Method Summary collapse

Methods included from Robut::Plugin

#at_nick, #fake_message, included, #initialize, #nick, #reply, #sent_to_me?, #store, #without_nick, #words

Instance Method Details

#aliasesObject



90
91
92
# File 'lib/robut/plugin/alias.rb', line 90

def aliases
  store['aliases'] ||= {}
end

#aliases=(v) ⇒ Object



94
95
96
# File 'lib/robut/plugin/alias.rb', line 94

def aliases=(v)
  store['aliases'] = v
end

#get_alias(msg) ⇒ Object

Given a message, returns what it is aliased to (or nil)



75
76
77
# File 'lib/robut/plugin/alias.rb', line 75

def get_alias(msg)
  (store['aliases'] || {})[msg]
end

#handle(time, sender_nick, message) ⇒ Object

Perform the calculation specified in message, and send the result back.



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
# File 'lib/robut/plugin/alias.rb', line 44

def handle(time, sender_nick, message)
  if new_message = get_alias(message)
    # Apply the alias
    fake_message Time.now, sender_nick, new_message
  elsif sent_to_me?(message)
    message = without_nick message
    if message =~ /^remove alias (.*)/
      # Remove the alias
      key = parse_alias_key($1)
      remove_alias key
      return true
    elsif message =~ /^clear aliases$/
      self.aliases = {}
      return true
    elsif message =~ /^alias (.*)/
      # Create a new alias
      message = $1
      key, value = parse_alias message
      store_alias key, value
      return true # hault plugin execution chain
    elsif words(message).first == 'aliases'
      # List all aliases
      m, a = [], aliases # create reference to avoid going to the store every time
      a.keys.sort.each { |key| m << "#{key} => #{a[key]}" }
      reply m.join("\n")
      return true
    end
  end
end

#parse_alias(str) ⇒ Object

Returns alias and command



99
100
101
102
103
# File 'lib/robut/plugin/alias.rb', line 99

def parse_alias(str)
  r = Shellwords.shellwords str
  return r[0], r[1] if r.length == 2
  return r[0], r[1..-1].join(' ')
end

#parse_alias_key(str) ⇒ Object



105
106
107
# File 'lib/robut/plugin/alias.rb', line 105

def parse_alias_key(str)
  Shellwords.shellwords(str).join(' ')
end

#remove_alias(key) ⇒ Object



84
85
86
87
88
# File 'lib/robut/plugin/alias.rb', line 84

def remove_alias(key)
  new_aliases = aliases
  new_aliases.delete(key)
  store['aliases'] = new_aliases
end

#store_alias(key, value) ⇒ Object



79
80
81
82
# File 'lib/robut/plugin/alias.rb', line 79

def store_alias(key, value)
  aliases[key] = value
  store['aliases'] = aliases
end

#usageObject

Returns a description of how to use this plugin



33
34
35
36
37
38
39
40
# File 'lib/robut/plugin/alias.rb', line 33

def usage
  [
    "#{at_nick} alias <words> <expansion> - when #{nick} sees <words>, pretend it saw <expansion> instead",
    "#{at_nick} aliases - show all aliases",
    "#{at_nick} remove alias <words> - remove <words> as an alias",
    "#{at_nick} clear aliases - remove all aliases"
  ]
end