Class: Cinch::Plugins::Define

Inherits:
Object
  • Object
show all
Includes:
Admin, Cinch::Plugin
Defined in:
lib/cinch/plugins/define.rb

Constant Summary collapse

LAST_EDITED =
"(last edited by %s, %s ago)"
DOES_NOT_EXIST =
"I do not know what \"%s\" is."
NOT_OP =
"You do not have the proper access! (not +qaoh)"

Constants included from Admin

Admin::TYPES

Instance Method Summary collapse

Methods included from Admin

#add_admin, #add_trusted, #delete_admin, #delete_trusted, #each_controlchannel, #each_online_admin, #each_online_trusted, #get_admins, #get_trusted, #is_admin?, #is_trusted?, #no_admins?

Methods included from Helpers

#check_user, #is_channel_disabled?, table_format

Instance Method Details

#execute_define(m, term, dfn) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cinch/plugins/define.rb', line 18

def execute_define(m, term, dfn)
  if shared[:redis].exists("term:"+term.downcase)
    execute_redefine(m, term, dfn) # pass to redefine
  else
    # create new dfn
    entry = {
      "term.case" => term, 
      "dfn" => dfn, 
      "edited.by" => m.user.nick, 
      "edited.time" => Time.now.to_s
    }
    shared[:redis].hmset "term:#{term.downcase}", *entry
    m.reply "I now know that #{Format(:bold,entry['term.case'])} is \"#{entry['dfn']}\"!"
  end
end

#execute_forget(m, term) ⇒ Object


46
47
48
49
50
51
52
53
# File 'lib/cinch/plugins/define.rb', line 46

def execute_forget(m, term)
  return m.reply(NOT_OP, true) unless check_user(m.channel, m.user)
  m.reply term_exists?(term) {|key, entry|
    shared[:redis].del key
    edited_time = ChronicDuration.output(Time.now.to_i - Time.parse(entry["edited.time"]).to_i)
    "I have forgotten #{Format(:bold,entry['term.case'])} which was \"#{entry['dfn']}\" #{LAST_EDITED}" % [entry['edited.by'], edited_time]
  }
end

#execute_getdefs(m) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cinch/plugins/define.rb', line 65

def execute_getdefs(m)
  return unless is_admin?(m.user)
  keys = shared[:redis].keys('term:*');
  defs = keys.map {|key|
    shared[:redis].hgetall(key)
  }

  file = Tempfile.new ["#{@bot.nick}_definitions_#{Time.now.to_i}", ".json"]
  begin
    file.write defs.to_json
    file.close
    m.user.dcc_send(file.open(), "#{@bot.nick}_definitions_#{Time.now.to_i}.json")
  ensure
    file.unlink
  end
end

#execute_lookup(m, term) ⇒ Object


57
58
59
60
61
62
# File 'lib/cinch/plugins/define.rb', line 57

def execute_lookup(m, term)
  m.reply term_exists?(term) {|key, entry|
    edited_time = ChronicDuration.output(Time.now.to_i - Time.parse(entry["edited.time"]).to_i)
    "#{Format(:bold,entry['term.case'])} is \"#{entry['dfn']}\" #{LAST_EDITED}" % [entry['edited.by'], edited_time]
  }
end

#execute_redefine(m, term, dfn) ⇒ Object


35
36
37
38
39
40
41
42
43
# File 'lib/cinch/plugins/define.rb', line 35

def execute_redefine(m, term, dfn)
  return m.reply(NOT_OP, true) unless check_user(m.channel, m.user)
  m.reply term_exists?(term) {|key, entry|
    edited_entry = entry.merge 'dfn' => dfn, 'edited.by' => m.user.nick, 'edited.time' => Time.now.to_s
    shared[:redis].hmset key, *edited_entry
    edited_time = ChronicDuration.output(Time.now.to_i - Time.parse(entry["edited.time"]).to_i)
    "I now know that #{Format(:bold,entry['term.case'])} is \"#{edited_entry['dfn']}\", rather than \"#{entry['dfn']}\"! #{LAST_EDITED}" % [entry['edited.by'], edited_time]
  }
end