mailcvt 0.1.18 → 0.1.19

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,11 +7,7 @@ log=$outputdir/hourly-runmailcvt.log
7
7
  lock=$outputdir/runmailcvt.lock
8
8
 
9
9
  if mkdir $lock &> /dev/null; then
10
- if [ ! -z $3 ] && [ $3 == "-D" ]; then
11
- ./bin/runmailcvt $1 $2 $3 >> $log 2>&1
12
- else
13
- $bindir/runmailcvt $1 $2 >> $log 2>&1
14
- fi
10
+ $bindir/runmailcvt $1 $2 >> $log 2>&1
15
11
  rmdir $lock
16
12
  else
17
13
  echo "lock failed. abort at $(date +'%Y/%m/%d %T')." >> $log
@@ -40,32 +40,58 @@ command :write do |c|
40
40
  end
41
41
  end
42
42
 
43
- desc 'Describe read here'
44
- arg_name 'Describe arguments to read here'
45
- command :read do |c|
43
+ desc 'Parse grep output to mail log.'
44
+ command :parse do |c|
46
45
  c.action do |global_options,options,args|
47
- err = "read usage: mailcvt read [input dir] [output dir]"
46
+ err = "parse usage: mailcvt parse [daily dir] [timestamp]"
48
47
  raise err if args.length != 2
49
48
 
50
- raise "Cannot find #{args[0]}" unless Dir.exists?(args[0])
51
- Dir.mkdir(args[1]) unless Dir.exists?(args[1])
49
+ raise "Cannot find daily dir #{args[0]}" unless Dir.exists?(args[0])
52
50
  mp = MailParser.new(args[0], args[1])
53
51
  mp.parse
54
52
  end
55
53
  end
56
54
 
57
- command :setup do |c|
55
+ desc 'Parse long to or cc to mail log.'
56
+ command :parsetocc do |c|
58
57
  c.action do |global_options,options,args|
59
- mi = SetupMailcvt.new
60
- mi.setup
58
+ err = "parsetocc usage: mailcvt parsetocc [daily dir] [timestamp]"
59
+ raise err if args.length != 2
60
+
61
+ raise "Cannot find daily dir #{args[0]}" unless Dir.exists?(args[0])
62
+ tp = ToCcParser.new(args[0], args[1])
63
+ tp.parse
61
64
  end
62
65
  end
63
66
 
64
- desc 'Describe daemon here'
65
- arg_name 'Describe arguments to daemon here'
66
- command :daemon do |c|
67
+ desc 'Remove parsed mails and move unprocessed files to special folder.'
68
+ command :clean do |c|
67
69
  c.action do |global_options,options,args|
68
- puts "daemon command ran"
70
+ err = "clean usage: mailcvt clean [daily dir] [timestamp]"
71
+ raise err if args.length != 2
72
+
73
+ raise "Cannot find daily dir #{args[0]}" unless Dir.exists?(args[0])
74
+ mp = MailCleaner.new(args[0], args[1])
75
+ mp.clean
76
+ end
77
+ end
78
+
79
+ desc 'Move unprocessed tocc files to others folder.'
80
+ command :cleantocc do |c|
81
+ c.action do |global_options,options,args|
82
+ err = "cleantocc usage: mailcvt cleantocc [daily dir] [timestamp]"
83
+ raise err if args.length != 2
84
+
85
+ raise "Cannot find daily dir #{args[0]}" unless Dir.exists?(args[0])
86
+ mp = MailCleaner.new(args[0], args[1])
87
+ mp.cleantocc
88
+ end
89
+ end
90
+
91
+ command :setup do |c|
92
+ c.action do |global_options,options,args|
93
+ mi = SetupMailcvt.new
94
+ mi.setup
69
95
  end
70
96
  end
71
97
 
@@ -6,41 +6,75 @@ function now {
6
6
  echo $(date +'%Y/%m/%d %T')
7
7
  }
8
8
 
9
+ function copyonly1 {
10
+ input=$(cd $1 && pwd)
11
+ filecount=$(ls -1 $input | wc -l)
12
+ if [ $filecount -eq 1 ]; then
13
+ for f in $input/*
14
+ do
15
+ echo "[$(now)] cp $f COPY_$f"
16
+ cp $f "COPY_$f"
17
+ done
18
+ fi
19
+ }
20
+
9
21
  set -e
10
22
  timestamp=$(date +"%Y%m%d%H")
23
+ maildir=$(cd $1 && pwd)
24
+ dailydir=$(cd $2 && pwd)/$(date +"%Y%m%d")
11
25
 
12
26
  echo "[$(now)] runmailcvt started."
13
27
  wdir=$(pwd)
14
- isize=$(du -h $1)
15
- echo "[$(now)] working dir = $wdir, input = $isize, output = $2"
28
+ isize=$(du -h $maildir)
29
+ echo "[$(now)] working dir = $wdir, input = $isize, output = $dailydir"
16
30
 
17
- if [ ! -d $2 ]; then
18
- echo "[$(now)] create directory $2"
19
- mkdir $2
31
+ if [ ! -d $dailydir ]; then
32
+ echo "[$(now)] create daily output directory $dailydir"
33
+ mkdir $dailydir
20
34
  fi
21
- fngrep="$(cd $2 && pwd)/mout$timestamp.grep"
22
- if [ -f $fngrep ]; then
23
- echo "abort runmailcvt at $(now). $fngrep exists!!! "
35
+ grepout=$dailydir/mout$timestamp.grep
36
+ if [ -f $grepout ]; then
37
+ echo "abort runmailcvt at $(now). $grepout exists!!! "
24
38
  exit
25
39
  fi
26
40
 
27
- allmail="$(cd $1 && pwd)/*"
28
- echo "[$(now)] grep -E -A9 \"^Date: |^Message-ID: |^Subject: |^From: |^To: |^Cc: |^Content-Disposition: attachment;\" $allmail > $fngrep"
29
- grep -E -A9 "^Date: |^Message-ID: |^Subject: |^From: |^To: |^Cc: |^Content-Disposition: attachment;" $allmail > $fngrep
30
- echo "[$(now)] grep finished."
31
-
32
-
33
- if [ ! -z $3 ] && [ $3 == "-D" ]; then
34
- bundle exec bin/mailcvt read $1 $2
35
- else
36
- bindir=$(cd $(dirname $0) && pwd)
37
- rubydir=$(cd $(dirname $(dirname $(dirname $bindir))) && pwd)
38
- rvmdir=$(cd $(dirname $(dirname $rubydir)) && pwd)
39
- source $rvmdir/environments/default
40
- echo "[$(now)] mailcvt read $1 $2"
41
- mailcvt read $1 $2
42
- #$rubydir/bin/mailcvt read $1 $2
41
+ copyonly1 $maildir
42
+ allmail=$maildir/*
43
+ grepexp="^Date: |^Message-ID: |^Subject: |^From: |^To: |^Cc: |^Content-Disposition: attachment;"
44
+ echo "[$(now)] grep -E -A9 \"$grepexp\" $allmail > $grepout"
45
+ grep -E -A9 "$grepexp" $allmail > $grepout
46
+
47
+ bindir=$(cd $(dirname $0) && pwd)
48
+ rubydir=$(cd $(dirname $(dirname $(dirname $bindir))) && pwd)
49
+ rvmdir=$(cd $(dirname $(dirname $rubydir)) && pwd)
50
+ source $rvmdir/environments/default
51
+ echo "[$(now)] mailcvt parse $dailydir $timestamp"
52
+ mailcvt parse $dailydir $timestamp
53
+
54
+ echo "[$(now)] mailcvt clean $dailydir $timestamp"
55
+ mailcvt clean $dailydir $timestamp
56
+
57
+ dtocc="$dailydir/dtocc$timestamp"
58
+ if [ -d $dtocc ]; then
59
+ copyonly1 $dtocc
60
+ alldtocc="$dtocc/*"
61
+ echo "[$(now)] grep -E -A500 \"^To: |^Cc: \" $alldtocc > $grepout.tocc"
62
+ grep -E -A500 "^To: |^Cc: " $alldtocc > "$grepout.tocc"
63
+
64
+ echo "[$(now)] mailcvt parsetocc $dailydir $timestamp"
65
+ mailcvt parsetocc $dailydir $timestamp
66
+
67
+ echo "[$(now)] mailcvt cleantocc $dailydir $timestamp"
68
+ mailcvt cleantocc $dailydir $timestamp
43
69
  fi
44
70
 
71
+ echo "[$(now)] update reject size."
72
+ recipient=/etc/postfix/recipient_access
73
+ size=$({ du -sh $maildir; du -sh $dailydir; } | sed "s/\t.*$//g" | tr '\n' ' ')
74
+ cursec=$(date +"%m%d%H%M%S")
75
+ sed -i "s/REJECT\t.*$/REJECT\t$size$cursec/g" $recipient
76
+ postmap $recipient
77
+
78
+
45
79
  echo "[$(now)] runmailcvt finished."
46
80
  echo "-------------------------------------------------------------------------------"
@@ -61,6 +61,24 @@ class GrepMail
61
61
 
62
62
  def tolog
63
63
  lh = LogHelper.new(@groups)
64
- lh.tolog
64
+ mlog = lh.tolog
65
+
66
+ toccok = lh.tocc_complete?
67
+ othersok = lh.others_complete?
68
+ @iscomplete = false
69
+ @istocc = false
70
+ if toccok and othersok
71
+ @iscomplete = true
72
+ else if othersok
73
+ @istocc = true
74
+ end
75
+ end
76
+
77
+ def iscomplete?
78
+ @iscomplete
79
+ end
80
+
81
+ def istocc?
82
+ @istocc
65
83
  end
66
84
  end
@@ -14,8 +14,18 @@ class LogHelper
14
14
  @groups = groups
15
15
  end
16
16
 
17
+ def tocc_complete?
18
+ @valends[4] && @valends[5]
19
+ end
20
+
21
+ def others_complete?
22
+ @valends[0] && @valends[1]
23
+ && @valends[2] && @valends[3] && @valends[6]
24
+ end
25
+
17
26
  def tolog
18
- cols = Array.new(7)
27
+ @valends = Array.new(7, true)
28
+ cols = Array.new(6)
19
29
 
20
30
  @groups.each do |g|
21
31
  next if g[:key].nil? or g[:key].length < 2
@@ -24,8 +34,12 @@ class LogHelper
24
34
 
25
35
  if cap == 'C' and g[:key][1] == 'o'
26
36
  cols << concatattach(g)
37
+ @valends[-1] &= false unless g[:valend]
27
38
  else
28
- cols[@@ids[cap]] = concatval(g)
39
+ id = @@ids[cap]
40
+ next if cols[id]
41
+ cols[id] = concatval(g)
42
+ @valends[id] = false unless g[:valend]
29
43
  end
30
44
  end
31
45
 
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fileutils'
4
+
5
+ class MailRemover
6
+ def initialize(output, timestamp)
7
+ @fh = OutputHelper.new(output, timestamp)
8
+ end
9
+
10
+ def clean
11
+ removeok
12
+ move 'tocc'
13
+ move 'others', @fh.getdir("daily-leftovers", true)
14
+ end
15
+
16
+ def cleantocc
17
+ move 'tocc2', @fh.getdir("daily-leftovers", true)
18
+
19
+ d = @fh.getdir("dtocc")
20
+ FileUtils.rm_r(d) if Dir.exists?(d)
21
+ end
22
+
23
+ def removeok
24
+ fok = @fh.getmout('.fok')
25
+ raise "Can't find #{fok}." unless File.exist?(fok)
26
+
27
+ File.foreach(fok) do |fn|
28
+ begin
29
+ File.delete fn
30
+ rescue Exception
31
+ puts "Can't remove file #{fn}"
32
+ end
33
+ end
34
+ end
35
+
36
+ def move(suffix, d = nil)
37
+ f = @fh.getmout(".f#{suffix}")
38
+ return unless File.exist?(f)
39
+
40
+ unless d
41
+ d = @fh.getdir("d#{suffix}")
42
+ raise "#{d} exits." if Dir.exist?(d)
43
+ end
44
+
45
+ Dir.mkdir d
46
+ File.foreach(f) do |fn|
47
+ begin
48
+ filename = File.basename(fn)
49
+ File.rename fn, File.join(d, filename)
50
+ rescue Exception
51
+ puts "Can't move file #{fn} to #{d}"
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,117 +1,53 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- #require 'line_converter'
4
- #require 'open3'
5
3
  require 'grep_mail'
6
4
  require 'output_helper'
7
5
 
8
6
  class MailParser
9
- def initialize(input, output)
10
- @fh = OutputHelper.new(input, output)
7
+ def initialize(output, timestamp)
8
+ @fh = OutputHelper.new(output, timestamp)
11
9
  end
12
10
 
13
11
  def parse
14
12
  grep = @fh.getmout('.grep')
15
- raise "can't find #{grep}." unless File.exist?(grep)
16
- mcvt = @fh.getmout('.mcvt')
17
- raise "#{mcvt} exits." if File.exist?(mcvt)
18
- del = @fh.getmout('.del')
19
- raise "#{del} exits." if File.exist?(del)
13
+ raise "Can't find #{grep}." unless File.exist?(grep)
14
+ mlog = @fh.getmout('.mlog')
15
+ raise "#{mlog} exits." if File.exist?(mlog)
16
+ fok = @fh.getmout('.fok')
17
+ raise "#{fok} exits." if File.exist?(fok)
20
18
 
21
19
  lines = []
22
20
  curfile = nil
23
- @fh.output(mcvt, del) do |omcvt, odel|
21
+ @fh.output(mlog, fok) do |omlog, ofok|
24
22
  File.foreach(grep) do |line|
25
- curfile = getfile(line) unless curfile
23
+ curfile = @fh.getfile(line) unless curfile
26
24
  if line != "--\n" and !line.start_with?(curfile)
27
- convert curfile, lines, omcvt, odel
25
+ convert curfile, lines, omlog, ofok
28
26
 
29
- curfile = getfile(line)
27
+ curfile = @fh.getfile(line)
30
28
  lines = []
31
29
  end
32
30
  lines << line
33
31
  end
34
32
 
35
- convert curfile, lines, omcvt, odel
33
+ convert curfile, lines, omlog, ofok
36
34
  end
35
+ @fh.closetocc
36
+ @fh.closeothers
37
37
  end
38
38
 
39
- def convert(curfile, lines, omcvt, odel)
39
+ def convert(curfile, lines, omlog, ofok)
40
40
  return if lines.length == 0
41
41
 
42
42
  gm = GrepMail.new(curfile, lines)
43
- omcvt.puts gm.tolog
44
- odel.puts curfile
45
- end
46
-
47
- def getfile(line)
48
-
49
- pos = line.index(':')
50
- return "" if pos.nil?
51
- line[0..(pos-1)]
43
+ mlog = gm.tolog
44
+ if gm.iscomplete?
45
+ omlog.puts mlog
46
+ ofok.puts curfile
47
+ else if gm.istocc?
48
+ @fh.outputtocc(curfile, mlog)
49
+ else
50
+ @fh.outputothers(curfile)
51
+ end
52
52
  end
53
-
54
-
55
- # def parse
56
- # start = Time.now
57
- # parseall_grep
58
- # #parseall
59
- # diff = Time.now - start
60
- # if diff / 60 < 1
61
- # puts "Elapsed time: #{diff.round(3)} seconds."
62
- # else
63
- # puts "Elapsed time: #{(diff / 60).round(1)} minutes."
64
- # end
65
- # system("du -h #{@output}")
66
- # end
67
-
68
- # def parseall_grep
69
- # cmd = 'grep -E -A30 "^Date: |^Message-ID: |^Subject: |^From: |^To: |^Cc: |^Content-Disposition: attachment;" ' + File.join(@input, '*')
70
- #
71
- # Open3.popen3(cmd) do |i, o, e, t|
72
- # puts "Get keywords from mails by grep[#{t.pid}]."
73
- # puts "#{cmd}"
74
- #
75
- # lines = []
76
- # curfile = nil
77
- # File.open(File.join(@output, "test_log.log"), 'w') do |out|
78
- # o.each do |line|
79
- # puts "DEBUG> MailParser> line = #{line}"
80
- # curfile = getfile(line) unless curfile
81
- # if line != "--\n" and !line.start_with?(curfile)
82
- # gm = GrepMail.new(curfile, lines)
83
- # out.puts gm.tolog
84
- # curfile = getfile(line)
85
- # lines = []
86
- # end
87
- # lines << line
88
- # end
89
- #
90
- # if lines.length > 0
91
- # gm = GrepMail.new(curfile, lines)
92
- # out.puts gm.tolog
93
- # end
94
- # end
95
- #
96
- # puts "from grep[#{t.pid}]> #{t.value}."
97
- # end
98
- # end
99
- #
100
- #
101
- # def parseall
102
- # File.open(File.join(@output, "test_log.log"), 'w') do |out|
103
- # Dir.glob(File.join(@input, '*')) do |file|
104
- # parsefile file, out
105
- # end
106
- # end
107
- # end
108
- #
109
- # def parsefile(file, out)
110
- # cvt = LineConverter.new
111
- # File.foreach(file) {|line| cvt.convert line}
112
- #
113
- # out.puts(cvt.log)
114
- # out.flush
115
- # #File.delete file
116
- # end
117
53
  end
@@ -6,3 +6,5 @@ require 'fileutils'
6
6
  require 'big_mail_generator'
7
7
  require 'mail_parser'
8
8
  require 'setup_mailcvt'
9
+ require 'to_cc_parser'
10
+ require 'mail_cleaner'
@@ -1,3 +1,3 @@
1
1
  module Mailcvt
2
- VERSION = '0.1.18'
2
+ VERSION = '0.1.19'
3
3
  end
@@ -1,18 +1,30 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  class OutputHelper
4
- attr_reader :output, :input
5
-
6
- def initialize(input, output)
7
- @input = input
4
+ def initialize(output, timestamp)
8
5
  @output = output
9
- @timestamp = Time.new.strftime("%Y%m%d%H")
6
+ @timestamp = timestamp
7
+ end
8
+
9
+ def getfile(line)
10
+ pos = line.index(':')
11
+ return "" if pos.nil?
12
+ line[0..(pos-1)]
10
13
  end
11
14
 
12
15
  def getmout(ext)
13
16
  File.join(@output, "mout#{@timestamp}#{ext}")
14
17
  end
15
18
 
19
+ def getdir(prefix, notimestamp = false)
20
+ if notimestamp
21
+ dirname = prefix
22
+ else
23
+ dirname = "#{prefix}#{@timestamp}"
24
+ end
25
+ File.join(@output, dirname)
26
+ end
27
+
16
28
  def output(f1, f2)
17
29
  tmp1 = f1 + '.tmp'
18
30
  tmp2 = f2 + '.tmp'
@@ -24,4 +36,47 @@ class OutputHelper
24
36
  File.rename(tmp1, f1)
25
37
  File.rename(tmp2, f2)
26
38
  end
39
+
40
+ def outputtocc(mailpath, mlog)
41
+ unless @oftocc
42
+ ftocc = @fh.getmout('.ftocc')
43
+ @oftocc = File.open(ftocc, 'w')
44
+ mlogtocc = @fh.getmout('.mtocc')
45
+ @omlogtocc = File.open(mlogtocc, 'w')
46
+ end
47
+
48
+ @oftocc.puts mailpath
49
+ @omlogtocc.puts "#{mailpath}\t#{mlog}"
50
+ end
51
+
52
+ def closetocc
53
+ @oftocc.close if @oftocc
54
+ @omlogtocc.close if @omlogtocc
55
+ end
56
+
57
+ def outputothers(mailpath)
58
+ unless @ofothers
59
+ fothers = @fh.getmout('.fothers')
60
+ @ofothers = File.open(fothers, 'w')
61
+ end
62
+
63
+ @ofothers.puts mailpath
64
+ end
65
+
66
+ def closeothers
67
+ @ofothers.close if @ofothers
68
+ end
69
+
70
+ def outputtocc2(mailpath)
71
+ unless @oftocc2
72
+ ftocc2 = @fh.getmout('.ftocc2')
73
+ @oftocc2 = File.open(ftocc2, 'w')
74
+ end
75
+
76
+ @oftocc2.puts mailpath
77
+ end
78
+
79
+ def closetocc2
80
+ @oftocc2.close if @oftocc2
81
+ end
27
82
  end
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'grep_mail'
4
+ require 'output_helper'
5
+
6
+ class ToCcParser
7
+ def initialize(output, timestamp)
8
+ @fh = OutputHelper.new(output, timestamp)
9
+ end
10
+
11
+ def parse
12
+ grep = @fh.getmout('.grep.tocc')
13
+ raise "Can't find #{grep}." unless File.exist?(grep)
14
+ mtocc = @fh.getmout('.mtocc')
15
+ raise "Can't find #{mtocc}." unless File.exist?(mtocc)
16
+ mlog = @fh.getmout('.mlog')
17
+ raise "Can't find #{mlog}." unless File.exist?(mlog)
18
+
19
+ parsetocc grep
20
+ @fh.closetocc2
21
+
22
+ idTo = LogHelper.ids['T']
23
+ idCc = LogHelper.ids['C']
24
+ File.open(mlog, 'a') do |omlog|
25
+ File.foreach(mtocc) do |line|
26
+ next unless line
27
+ parts = line.split("\t")
28
+ next if parts.nil? or parts.length < 7
29
+
30
+ fn = parts[0]
31
+ if @toccs.has_key?(fn)
32
+ tocc = @toccs[fn]
33
+ parts[idTo+1] = tocc[idTo]
34
+ parts[idCc+1] = tocc[idCc]
35
+ end
36
+ omlog.puts parts[1..-1].join("\t")
37
+ end
38
+ end
39
+ end
40
+
41
+ def parsetocc(grep)
42
+ @toccs = {}
43
+ lines = []
44
+ curfile = nil
45
+ File.foreach(grep) do |line|
46
+ curfile = @fh.getfile(line) unless curfile
47
+ if line != "--\n" and !line.start_with?(curfile)
48
+ convert curfile, lines
49
+
50
+ curfile = @fh.getfile(line)
51
+ lines = []
52
+ end
53
+ lines << line
54
+ end
55
+
56
+ convert curfile, lines
57
+ end
58
+
59
+ def convert(curfile, lines)
60
+ return if lines.length == 0
61
+
62
+ gm = GrepMail.new(curfile, lines)
63
+ mlog = lh.tolog
64
+ if gm.istocc?
65
+ @fh.outputtocc2(mlog)
66
+ else
67
+ @toccs[curfile] = mlog unless @toccs.has_key?(curfile)
68
+ end
69
+ end
70
+ end
71
+
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.1.18
4
+ version: 0.1.19
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-22 00:00:00.000000000 Z
12
+ date: 2013-07-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -96,6 +96,8 @@ files:
96
96
  - lib/log_helper.rb
97
97
  - lib/output_helper.rb
98
98
  - lib/setup_mailcvt.rb
99
+ - lib/mail_cleaner.rb
100
+ - lib/to_cc_parser.rb
99
101
  - README.rdoc
100
102
  - mailcvt.rdoc
101
103
  homepage: http://github.com/ryu-kahou/mailcvt
@@ -118,7 +120,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
120
  version: '0'
119
121
  segments:
120
122
  - 0
121
- hash: 731630712782139179
123
+ hash: 3926626864906930717
122
124
  required_rubygems_version: !ruby/object:Gem::Requirement
123
125
  none: false
124
126
  requirements:
@@ -127,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
129
  version: '0'
128
130
  segments:
129
131
  - 0
130
- hash: 731630712782139179
132
+ hash: 3926626864906930717
131
133
  requirements: []
132
134
  rubyforge_project:
133
135
  rubygems_version: 1.8.25