Permalink
Browse files

Basic support for events triggered by another person

  • Loading branch information...
1 parent 0d96c9b commit fc49364cc11e30d92f5f000ba6abcbe500b7155b @jamesu committed Jul 11, 2012
Showing with 81 additions and 7 deletions.
  1. +2 −1 html_dumper.rb
  2. +4 −2 log_entry.rb
  3. +29 −3 parsers/colloquy_parser.rb
  4. +1 −1 parsers/json_parser.rb
  5. +45 −0 spec/colloquy_spec.rb
View
@@ -69,7 +69,7 @@ def write(fs)
</head><body>\n")
fs.write("<table>\n")
last_date = nil
-last_sender = nil
+last_sender = -1
@collection.entries.each do |entry|
dt = entry.occurred
dt = Date.civil(dt.year, dt.month, dt.day)
@@ -86,6 +86,7 @@ def write(fs)
end
content = CGI::escapeHTML((entry.is_action ? "* #{entry.content}" : entry.content))
fs.write("<tr class=\"#{entry.type}\"><td class=\"sender\">#{sender}</td><td class=\"content\">#{content}</td><td class=\"date\">#{entry.occurred.strftime('%H:%M:%S')}</td></tr>\n")
+ #fs.write("<tr class=\"debug\"><td class=\"sender\">DEBUG</td><td class=\"content\">#{CGI::escapeHTML(entry.inspect)}</td><td class=\"date\"></td></tr>\n")
end
fs.write("</table>\n")
fs.write("</body></html>\n")
View
@@ -1,5 +1,5 @@
class LogEntry
- attr_accessor :occurred, :content, :sender, :type, :source
+ attr_accessor :occurred, :content, :sender, :by, :type, :source
# Types
# :event => generic event
@@ -11,8 +11,9 @@ class LogEntry
# :message => message
# :action => /msg or notice
- def initialize(entry, sender=nil)
+ def initialize(entry, sender=nil, by=nil)
@sender = sender
+ @by = by
@occurred = entry[:occurred]
@content = entry[:content] || ''
@source = entry[:source]
@@ -29,6 +30,7 @@ def to_loghash(opts={})
:occurred => @occurred,
:content => @content,
:source => @source,
+ :by => @by,
:type => @type
}
end
View
@@ -45,14 +45,36 @@ def parse_node(entry)
xpath = entry.xpath("sender").first
sender = xpath ? {:ident => xpath["identifier"],
:name => xpath.inner_text,
- :hostmask => xpath["hostmask"]} : @current_sender
- @current_sender = sender
+ :hostmask => xpath["hostmask"]} : nil
+
+ # for join & part events
+ if sender.nil?
+ xpath = entry.xpath("who").first
+ sender = xpath ? {:ident => xpath["identifier"],
+ :name => xpath.inner_text,
+ :hostmask => xpath["hostmask"]} : nil
+ end
+
+ sender ||= @current_sender
+ #@current_sender = sender
+
+ # for actions caused by someone else
+ xpath = entry.xpath("by").first
+ by = xpath ? {:ident => xpath["identifier"],
+ :name => xpath.inner_text,
+ :hostmask => xpath["hostmask"]} : nil
if sender && sender[:ident].nil? && sender[:hostmask]
# match from hostmask
match = sender[:hostmask].match(HOSTMASK_MATCH)
sender[:ident] = match[1]
end
+
+ if by && by[:ident].nil? && by[:hostmask]
+ # match from hostmask
+ match = by[:hostmask].match(HOSTMASK_MATCH)
+ by[:ident] = match[1]
+ end
entry_type = entry.name
@@ -72,16 +94,20 @@ def parse_node(entry)
time = Time.parse(entry["received"])
content = entry.inner_text
elsif entry.name == "envelope"
- return entry.xpath("message").map do |sub_entry|
+ @current_sender = sender
+ list = entry.xpath("message").map do |sub_entry|
parse_node(sub_entry)
end
+ @current_sender = nil
+ return list
else
time = nil
content = "???"
end
{:type => entry_type,
:sender => sender,
+ :by => by,
:source => nil,
:occurred => time,
:content => content
View
@@ -18,7 +18,7 @@ def parse(input)
:content => entry['content'],
:source => entry['source'],
:type => entry['type'].to_sym
- }, @collection.senders[entry['sender']])
+ }, @collection.senders[entry['sender']], @collection.senders[entry['by']])
end
end
end
View
@@ -37,4 +37,49 @@
event[:occurred].utc.to_s.should == Time.utc(2010, 9, 2, 15, 30, 17).to_s
event[:sender].should == nil
end
+
+ it "should handle who events" do
+ join = Nokogiri::XML.parse(<<-EOL
+ <event id="HELEG6BMEP3" name="memberJoined" occurred="2010-09-02 17:41:53 +0100">
+ <message><span class="member">foo</span> joined the chat room.</message>
+ <who hostmask="foo@host.com">foo</who>
+ </event>
+ EOL
+ )
+
+ leave = Nokogiri::XML.parse(<<-EOL
+ <event id="AGZ4MZINEP3" name="memberParted" occurred="2012-09-02 17:43:53 +0100">
+ <message><span class="member">foo</span> left the chat room.</message>
+ <who hostmask="foo@host.com">foo</who>
+ <reason>Ping timeout: 121 seconds</reason>
+ </event>
+ EOL
+ )
+
+ join_event = @parser.parse_node(join.children[0])
+ leave_event = @parser.parse_node(leave.children[0])
+
+ join_event[:type].should == :userAvailable
+ join_event[:sender][:ident].should == "foo"
+
+ leave_event[:type].should == :userLeft
+ leave_event[:sender][:ident].should == "foo"
+ end
+
+ it "should handle by events" do
+ op = Nokogiri::XML.parse(<<-EOL
+ <event id="APKA3PHJWW3" name="memberPromotedToOperator" occurred="2012-07-02 10:08:13 +0100">
+ <message><span class="member">Foo</span> was promoted to operator by <span class="member">ChanServ</span>.</message>
+ <who hostmask="Foo@127.0.0.1" identifier="foo" class="operator">Foo</who>
+ <by hostmask="ChanServ@services.int" identifier="chanserv" class="server operator">ChanServ</by>
+ </event>
+ EOL
+ )
+
+ op_event = @parser.parse_node(op.children[0])
+
+ op_event[:type].should == :memberPromotedToOperator
+ op_event[:sender][:ident].should == "foo"
+ op_event[:by][:ident].should == "chanserv"
+ end
end

0 comments on commit fc49364

Please sign in to comment.