mailcvt 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/grep_mail.rb +66 -0
- data/lib/line_converter.rb +2 -2
- data/lib/log_helper.rb +50 -0
- data/lib/mail_parser.rb +49 -1
- data/lib/mailcvt/version.rb +1 -1
- metadata +6 -4
data/lib/grep_mail.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'log_helper'
|
4
|
+
|
5
|
+
class GrepMail
|
6
|
+
def initialize(file, lines)
|
7
|
+
@pcontent= file.length + 1
|
8
|
+
grouplines(lines)
|
9
|
+
end
|
10
|
+
|
11
|
+
def grouplines(lines)
|
12
|
+
@groups = []
|
13
|
+
valend = false
|
14
|
+
lines.each do |line|
|
15
|
+
dlm = line[@pcontent-1]
|
16
|
+
if dlm == ':'
|
17
|
+
finish_multiline_value
|
18
|
+
|
19
|
+
valend = false
|
20
|
+
k,v = keyvalue(line)
|
21
|
+
@groups << {key: k, value: v}
|
22
|
+
else
|
23
|
+
next if valend
|
24
|
+
|
25
|
+
v = valueonly(line)
|
26
|
+
if v
|
27
|
+
curgrp = @groups[-1]
|
28
|
+
curgrp[:afterlines] = [] unless curgrp[:afterlines]
|
29
|
+
curgrp[:afterlines] << v
|
30
|
+
else
|
31
|
+
valend = true
|
32
|
+
finish_multiline_value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def finish_multiline_value
|
39
|
+
return if @groups.length == 0
|
40
|
+
curgrp = @groups[-1]
|
41
|
+
curgrp[:valend] = true if curgrp[:afterlines]
|
42
|
+
end
|
43
|
+
|
44
|
+
def keyvalue(line)
|
45
|
+
if line[@pcontent] == 'C' and line[@pcontent] == 'o'
|
46
|
+
dlm = ';'
|
47
|
+
else
|
48
|
+
dlm = ':'
|
49
|
+
end
|
50
|
+
pos = line.index(dlm, @pcontent)
|
51
|
+
return nil, nil unless pos
|
52
|
+
|
53
|
+
return line[@pcontent..pos], line[(pos+2)..-2]
|
54
|
+
end
|
55
|
+
|
56
|
+
def valueonly(line)
|
57
|
+
cap = line[@pcontent]
|
58
|
+
return nil if cap != ' ' and cap != "\t"
|
59
|
+
line[(@pcontent+1)..-2].strip
|
60
|
+
end
|
61
|
+
|
62
|
+
def tolog
|
63
|
+
lh = LogHelper.new(@groups)
|
64
|
+
lh.tolog
|
65
|
+
end
|
66
|
+
end
|
data/lib/line_converter.rb
CHANGED
@@ -31,7 +31,7 @@ class LineConverter
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def convertheader(cap, line)
|
34
|
-
if cap == "\n"
|
34
|
+
if cap == nil or cap == "\n" or cap == ''
|
35
35
|
@hfinished = true
|
36
36
|
return
|
37
37
|
end
|
@@ -49,7 +49,7 @@ class LineConverter
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def convertbody(cap, line)
|
52
|
-
if cap == 'C' and line
|
52
|
+
if cap == 'C' and line.start_with?(ATTACH)
|
53
53
|
@attach << "\a" if not @attach.empty?
|
54
54
|
@attach << line[43..-3] # filename"\n
|
55
55
|
end
|
data/lib/log_helper.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
class LogHelper
|
4
|
+
@@ids = {
|
5
|
+
'D' => 0,
|
6
|
+
'M' => 1,
|
7
|
+
'S' => 2,
|
8
|
+
'F' => 3,
|
9
|
+
'T' => 4,
|
10
|
+
'C' => 5
|
11
|
+
}
|
12
|
+
|
13
|
+
def initialize(groups)
|
14
|
+
@groups = groups
|
15
|
+
end
|
16
|
+
|
17
|
+
def tolog
|
18
|
+
@cols = Array.new(7)
|
19
|
+
@groups.each do |g|
|
20
|
+
next if g[:key].nil? or g[:key].length < 2
|
21
|
+
cap = g[:key][0]
|
22
|
+
next unless @@ids.has_key?(cap)
|
23
|
+
|
24
|
+
if cap == 'C' and g[:key][1] == 'o'
|
25
|
+
@cols[-1] = concatattach(g)
|
26
|
+
else
|
27
|
+
@cols[@@ids[cap]] = concatval(g)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
@cols.join("\a")
|
32
|
+
end
|
33
|
+
|
34
|
+
def concatattach(group)
|
35
|
+
v = concatval(group)
|
36
|
+
|
37
|
+
pstart = v.index('filename="')
|
38
|
+
pend = v.rindex('"')
|
39
|
+
|
40
|
+
return '' if pstart.nil? or pend.nil?
|
41
|
+
v[(pstart+10)..(pend-1)]
|
42
|
+
end
|
43
|
+
|
44
|
+
def concatval(group)
|
45
|
+
return group[:value] unless group[:afterlines]
|
46
|
+
|
47
|
+
al = group[:afterlines].join("\a")
|
48
|
+
"#{group[:value]}\a#{al}"
|
49
|
+
end
|
50
|
+
end
|
data/lib/mail_parser.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require 'line_converter'
|
4
|
+
require 'grep_mail'
|
5
|
+
require 'open3'
|
4
6
|
|
5
7
|
class MailParser
|
6
8
|
def initialize(input, output)
|
@@ -10,7 +12,8 @@ class MailParser
|
|
10
12
|
|
11
13
|
def parse
|
12
14
|
start = Time.now
|
13
|
-
|
15
|
+
parseall_grep
|
16
|
+
#parseall
|
14
17
|
diff = Time.now - start
|
15
18
|
if diff / 60 < 1
|
16
19
|
puts "Elapsed time: #{diff.round(3)} seconds."
|
@@ -20,6 +23,51 @@ class MailParser
|
|
20
23
|
system("du -h #{@output}")
|
21
24
|
end
|
22
25
|
|
26
|
+
def parseall_grep
|
27
|
+
cmd = 'grep -E -A30 "^Date: |^Message-ID: |^Subject: |^From: |^To: |^Cc: |^Content-Disposition: attachment;" ' + File.join(@input, '*')
|
28
|
+
|
29
|
+
Open3.popen3(cmd) do |i, o, e, t|
|
30
|
+
puts "Get keywords from mails by grep[#{t.pid}]."
|
31
|
+
puts "#{cmd}"
|
32
|
+
|
33
|
+
lines = []
|
34
|
+
File.open(File.join(@output, "test_log.log"), 'w') do |out|
|
35
|
+
o.each do |line|
|
36
|
+
if line == "--\n" and lines.length > 0
|
37
|
+
gm = GrepMail.new(getfile(lines[0]), lines)
|
38
|
+
out.puts gm.tolog
|
39
|
+
lines = []
|
40
|
+
else
|
41
|
+
lines << line
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
puts "from grep[#{t.pid}]> #{t.value}."
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def filechanged?(line)
|
51
|
+
changed = false
|
52
|
+
if @curfile.nil?
|
53
|
+
@curfile = getfile(line)
|
54
|
+
else
|
55
|
+
changed = !line.start_with?(@curfile)
|
56
|
+
end
|
57
|
+
|
58
|
+
if changed
|
59
|
+
@prevfile = @curfile
|
60
|
+
@curfile = getfile(line)
|
61
|
+
end
|
62
|
+
changed
|
63
|
+
end
|
64
|
+
|
65
|
+
def getfile(line)
|
66
|
+
pos = line.index(':')
|
67
|
+
return "" if pos.nil?
|
68
|
+
line[0..(pos-1)]
|
69
|
+
end
|
70
|
+
|
23
71
|
def parseall
|
24
72
|
File.open(File.join(@output, "test_log.log"), 'w') do |out|
|
25
73
|
Dir.glob(File.join(@input, '*')) do |file|
|
data/lib/mailcvt/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mailcvt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -90,6 +90,8 @@ files:
|
|
90
90
|
- lib/big_mail_generator.rb
|
91
91
|
- lib/mail_parser.rb
|
92
92
|
- lib/line_converter.rb
|
93
|
+
- lib/grep_mail.rb
|
94
|
+
- lib/log_helper.rb
|
93
95
|
- README.rdoc
|
94
96
|
- mailcvt.rdoc
|
95
97
|
homepage: http://github.com/ryu-kahou/mailcvt
|
@@ -112,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
114
|
version: '0'
|
113
115
|
segments:
|
114
116
|
- 0
|
115
|
-
hash:
|
117
|
+
hash: -4410244498873720589
|
116
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
119
|
none: false
|
118
120
|
requirements:
|
@@ -121,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
123
|
version: '0'
|
122
124
|
segments:
|
123
125
|
- 0
|
124
|
-
hash:
|
126
|
+
hash: -4410244498873720589
|
125
127
|
requirements: []
|
126
128
|
rubyforge_project:
|
127
129
|
rubygems_version: 1.8.25
|