-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: David A. Cuadrado <krawek@gmail.com>
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Log | ||
include MongoMapper::Document | ||
|
||
timestamps! | ||
|
||
key :_id, String | ||
key :_type, String | ||
|
||
key :channel_id, String | ||
belongs_to :channel | ||
|
||
key :messages, Array | ||
key :messages_count, Integer, :default => 0 | ||
|
||
|
||
def self.format_message(msg) | ||
if msg["type"] == "normal" | ||
"[#{msg["date"]}] <#{msg["nick"]}> #{msg["text"]}" | ||
elsif msg["type"] == "action" | ||
"[#{msg["date"]}] * #{msg["nick"]} #{msg["text"]}" | ||
elsif msg["type"] == "join" | ||
"[#{msg["date"]}] JOIN #{msg["nick"]}" | ||
elsif msg["type"] == "part" | ||
"[#{msg["date"]}] PART #{msg["nick"]}" | ||
elsif msg["type"] == "ctcp" | ||
"[#{msg["date"]}] CTCP #{msg["nick"]} #{msg["text"]}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
class LoggerPlugin | ||
include NinjaPlugin | ||
|
||
match /grep (.+)/, :method => :grep | ||
|
||
listen_to :channel, :method => :log_channel | ||
listen_to :join, :method => :log_channel | ||
listen_to :part, :method => :log_channel | ||
|
||
def log_channel(m) | ||
return if !m.channel? | ||
|
||
message = m.message | ||
nick = m.user.nick | ||
type = "normal" | ||
if m.events.include?(:notice) | ||
type = "notice" | ||
elsif m.events.include?(:ctcp) | ||
type = "ctcp" | ||
message = m.ctcp_message | ||
|
||
if message =~ /^ACTION / | ||
type = "action" | ||
message.gsub!(/^ACTION /, "") | ||
end | ||
elsif m.events.include?(:join) | ||
type = "join" | ||
message = "" | ||
elsif m.events.include?(:part) | ||
type = "part" | ||
end | ||
|
||
data = { | ||
:nick => nick, | ||
:date => Time.now, | ||
:type => type | ||
} | ||
if !message.empty? | ||
data[:text] = message | ||
end | ||
|
||
Channel.add_log_message(m.channel.name, data) | ||
end | ||
|
||
def grep(m, query) | ||
if m.channel? | ||
regexp = Regexp.new(query) rescue Regexp.new(Regexp.escape(query)) | ||
matches = [] | ||
count = 0 | ||
Log.find_each(:"messages.text" => regexp, :channel_id => m.channel.name, :limit => 5, :order => "created_at desc") do |log| | ||
if count >= 10 | ||
m.user.send ">>>> TOO MANY RESULTS<<<<<" | ||
break | ||
end | ||
|
||
log.messages.each do |message| | ||
if message["text"] =~ regexp && message["text"] !~ /^\!grep\s/ | ||
m.user.send Log.format_message(message) | ||
|
||
count += 1 | ||
end | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
||
register_plugin LoggerPlugin |