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
|
# File 'lib/colloquy_log_to_text/colloquy_log_to_text.rb', line 2
def self.convert(file)
if !File.exists?(file)
puts "Error: File does not exist."
exit 1
elsif !file.match(/(.+)\.colloquyTranscript$/)
puts "Error: File is not a Colloquy log."
exit 1
end
puts "Converting #{file}..."
f = File.open(file)
doc = Nokogiri::XML(f)
f.close
f = File.open(file.gsub(".colloquyTranscript", ".txt"), "w")
envelopes = doc.css("envelope")
envelopes.each { |envelope|
senders = envelope.css("sender")
senders.each { |sender|
@nick = sender.content
case sender.attr("class")
when "voice"
@class = "+"
when "operator"
@class = "@"
when "founder"
@class = "~"
when "administrator"
@class = "&"
end
}
messages = envelope.css("message")
messages.each { |message|
@printed_message = message.content
@timestamp_string = message.attr("received")
@timestamp = DateTime.strptime(@timestamp_string, "%F %H:%M:%S")
@is_action = message.attr("action")
if @is_action == "yes"
f.puts "[#{@timestamp.day}-#{@timestamp.month}-#{@timestamp.year} #{@timestamp.hour}:#{@timestamp.minute}] * #{@nick} #{@printed_message}"
else
f.puts "[#{@timestamp.day}-#{@timestamp.month}-#{@timestamp.year} #{@timestamp.hour}:#{@timestamp.minute}] <#{@class}#{@nick}> #{@printed_message}"
end
}
}
end
|