Class: Muzang::Plugins::Grep

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/muzang-plugins/muzang-grep.rb

Defined Under Namespace

Classes: Message

Constant Summary collapse

DATABASE_FILE =
'irclog.sqlite3'
DATABASE_CONFIG =
{
  :database => 'irclog.sqlite3',
  :encoding => 'utf8',
  :adapter => 'sqlite3'
}

Instance Method Summary collapse

Methods included from Helpers

#create_database, #match, #on_channel, #on_join

Constructor Details

#initialize(bot) ⇒ Grep

Returns a new instance of Grep.



43
44
45
46
# File 'lib/muzang-plugins/muzang-grep.rb', line 43

def initialize(bot)
  @bot = bot
  open_database
end

Instance Method Details

#call(connection, message) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/muzang-plugins/muzang-grep.rb', line 48

def call(connection, message)
  on_channel(message) do
    match(message, /^[^~(Searched)]/) do
      persist_message(message)
    end
    match(message, /^~ (.*)/) do |what|
      search_for_term(connection, message, what[1])
    end
  end
rescue
  puts $!
  puts $!.backtrace
  raise
end

#persist_message(message) ⇒ Object



63
64
65
66
67
# File 'lib/muzang-plugins/muzang-grep.rb', line 63

def persist_message(message)
  Message.create!(:channel => message.channel,
                  :user => message.user,
                  :content => message.message)
end

#search_for_term(connection, message, term) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/muzang-plugins/muzang-grep.rb', line 69

def search_for_term(connection, message, term)
  results = Message.like(message.channel, term)
  if results.size > 0
    link = save_results(term, results)
    connection.msg(message.channel, "Searched for '#{term}', found #{results.size} matches: #{link}")
  else
    connection.msg(message.channel, "Searched for '#{term}', nothing found")
  end
end