forked from erlehmann/logformat
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.rb
More file actions
82 lines (78 loc) · 3.03 KB
/
parser.rb
File metadata and controls
82 lines (78 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
73
74
75
76
77
78
79
80
81
82
require_relative 'models/message'
require_relative 'models/channel'
module Logformat
class Message
# day: string in format "YYYY-MM-DD"
def self.parse_irssi_line(day, channel_name, line)
line_regex = /^([012][0-9]:[0-6][0-9]) <[ @]*([^>]+)> (.*)$/
action_regex = /^([012][0-9]:[0-6][0-9]) +\* +([a-z0-9_\-\[\]\\^{}|`]+) (.*)$/i
join_regex = /^([012][0-9]:[0-6][0-9]) (?:-!-)? ?([a-z0-9_\-\[\]\\^{}|`]+) [\[(].*[\])] has joined /i
quit_regex = /^([012][0-9]:[0-6][0-9]) (?:-!-)? ?([a-z0-9_\-\[\]\\^{}|`]+) [\[(].*[\])] has quit [\[(](.*)[\])]$/i
leave_regex = /^([012][0-9]:[0-6][0-9]) (?:-!-)? ?([a-z0-9_\-\[\]\\^{}|`]+) [\[(].*[\])] has left (?:[#&][^\x07\x2C\s]+) [\[(](.*)[\])]$/i
kick_regex = /^([012][0-9]:[0-6][0-9]) (?:-!-)? ?([a-z0-9_\-\[\]\\^{}|`]+) was kicked from (?:[#&][^\x07\x2C\s]+) by ([a-z0-9_\-\[\]\\^{}|`]+) [\[(](.*)[\])]$/i
nickchange_regex = /^([012][0-9]:[0-6][0-9]) (?:-!-)? ?([a-z0-9_\-\[\]\\^{}|`]+) is now known as ([a-z0-9_\-\[\]\\^{}|`]+)/i
notice_regex = /^([012][0-9]:[0-6][0-9]) -([a-z0-9_\-\[\]\\^{}|`.]+)(?:\|[#&][^\x07\x2C\s]+)?- (.*)$/i
topic_regex = /^([012][0-9]:[0-6][0-9]) (?:-!-)? ?([a-z0-9_\-\[\]\\^{}|`.]+) changed the topic of [#&][^\x07\x2C\s]+ to: (.*)$/i
ignore_regex = /(?:\u0000|^--- |^([012][0-9]:[0-6][0-9]) (?:-!-)? ?(?:Irssi:|Netsplit|mode\/|ServerMode\/|You're now known as| channels :|(?:[012][0-9]:[0-6][0-9])|[0-9] ~\/logs))/
unless line.valid_encoding?
line = line.force_encoding('iso-8859-1').encode('utf-8')
end
case line
when line_regex
time = $1
nick = $2
text = $3
type = :message
when action_regex
time = $1
nick = $2
text = $3
type = :action
when join_regex
time = $1
nick = $2
text = ""
type = :join
when quit_regex
time = $1
nick = $2
text = $3
type = :quit
when leave_regex
time = $1
nick = $2
text = $3
type = :leave
when kick_regex
time = $1
nick = $2
text = $3
type = :kick
when nickchange_regex
time = $1
nick = $2
text = $3 # text == new nick
type = :nick
when notice_regex
time = $1
nick = $2
text = $3
type = :notice
when topic_regex
time = $1
nick = $2
text = $3
type = :topic
when ignore_regex
return nil
else
raise(ArgumentError, "Invalid line at #{channel_name}, #{day}: #{line}")
end
# FIXME: DateTime assumes UTC, so all times will be interpreted as such
# SHOULD BE: Berlin time at the time of logging (take DST into account if possible)
dt = DateTime.new(*day.split('-').map{|s| s.to_i}, *time.split(':').map{|s| s.to_i})
channel = Channel.find_or_create(:name => channel_name).freeze
Message.find_or_create(:time => dt, :nick => nick, :text => text, :channel => channel, :type => type.to_s).freeze
end
end
end