-
Notifications
You must be signed in to change notification settings - Fork 0
/
biz_mail_processor.rb
204 lines (173 loc) · 7.55 KB
/
biz_mail_processor.rb
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
require 'rubygems'
require 'yaml'
require 'date'
require 'tmail'
require 'biz_log.rb'
require 'biz_log_context.rb'
require 'biz_target_context.rb'
require 'singlekpi_report_generator.rb'
require 'aggregated_persons_report_generator.rb'
require 'aggregated_items_report_generator.rb'
require 'combined_ratio_biz_log_context.rb'
require 'biz_log_status.rb'
require 'multikpi_report_generator.rb'
class BizMailProcessor
def initialize
@config = YAML.load_file($CONFIG_YAML)
@report_from = nil
@report_date = nil
@bizmail_user = nil
@bizmail_person = ''
@bizmail_kpi = ''
end
def parse_mail(from, to, date, subject, body)
@report_from = from
to = [to] if to.kind_of?(String)
to.each do |t|
laddr = t.split(/@/)[0]
dom = t.split(/@/)[1]
if dom == $MYDOMAIN then
(@report_user, param) = laddr.split(/[+]/)
@current_config = @config[@report_user]
(date, kpi, person) = param.split(/-/)
@report_date = Date.new(date[0, 4].to_i, date[4, 2].to_i, date[6, 2].to_i)
@bizmail_kpi = @current_config['kpi'][kpi] if kpi != "" && @current_config['kpi']
@bizmail_person = @current_config['item'][person] if person != "" && @current_config['item']
@bizmail_person = @current_config['person'][person] if person != "" && @current_config['person']
@bizmail_item = @bizmail_person
end
end
value = nil
body.each do |r|
r = r.strip.rstrip
value = r.to_i if /^[-.0-9]*$/ =~ r && value == nil
end
bizlog_context = BizLogContext.new(@report_user, @bizmail_kpi, @bizmail_person)
bizlog = BizLog.new(@report_date, value, nil)
bizlog_context.insert(bizlog)
end
def generate_report_mail(&block)
tmail = TMail::Mail.new
tmail.content_type = 'text/plain'
tmail.charset = 'iso-2022-jp'
tmail.date = Time.now
tmail.from = @report_user + "@" + $MYDOMAIN
tmail.reply_to = @report_user + "@" + $MYDOMAIN
if @current_config['type'] == 'single_kpi' then
bizlog_context = BizLogContext.new(@report_user, @bizmail_kpi, @bizmail_person)
biztarget_context = BizTargetContext.new(@report_user, @bizmail_kpi, @bizmail_person)
report_gen = SingleKPIReportGenerator.new(bizlog_context, biztarget_context, $BIZMAIL_DIR + @current_config['report'])
(subject, body) = report_gen.generate_report(@report_date)
elsif @current_config['type'] == 'multi_kpi' then
return nil
elsif @current_config['type'] == 'aggregated_persons' then
if @current_config['report'] != nil then
bizlog_context = BizLogContext.new(@report_user, @bizmail_kpi, @bizmail_person)
biztarget_context = BizTargetContext.new(@report_user, @bizmail_kpi, @bizmail_person)
report_gen = SingleKPIReportGenerator.new(bizlog_context, biztarget_context, $BIZMAIL_DIR + @current_config['report'])
(subject, body) = report_gen.generate_report(@report_date)
else
return nil
end
elsif @current_config['type'] == 'aggregated_items' then
return nil
end
tmail.to = to_addr_substitute(@current_config['report_to'], @report_from)
tmail.subject = '=?ISO-2022-JP?B?' + NKF.nkf('-j', subject).split(//, 1).pack('m').chomp + '?='
tmail.body = NKF.nkf("--jis", body)
block.call(tmail)
end
def generate_combined_report_mail(&block)
tmail = TMail::Mail.new
tmail.content_type = 'text/plain'
tmail.charset = 'iso-2022-jp'
tmail.date = Time.now
tmail.from = @report_user + "@" + $MYDOMAIN
tmail.reply_to = @report_user + "@" + $MYDOMAIN
if @current_config['type'] == 'single_kpi' then
return nil
elsif @current_config['type'] == 'multi_kpi' then
blstat = BizLogStatus.new(@report_user)
reported_kpi = blstat.kpi_status(@report_date, @bizmail_person)
@current_config['kpi'].each do |k, v|
return nil if !reported_kpi.include?(v)
end
bizlog_contexts = []
biztarget_contexts = []
@current_config['kpi'].each do |k, v|
bizlog_contexts.push(BizLogContext.new(@report_user, v, @bizmail_person))
biztarget_contexts.push(BizTargetContext.new(@report_user, v, @bizmail_person))
end
@current_config['combined_kpi'].each do |k, v|
if v['type'] == "combined_ratio" then
bizlog_contexts.push(CombinedRatioBizLogContext.new(v['name'],
BizLogContext.new(@report_user, @current_config['kpi'][v['kpi'][0]], @bizmail_person),
BizLogContext.new(@report_user, @current_config['kpi'][v['kpi'][1]], @bizmail_person)))
biztarget_contexts.push(BizTargetContext.new(@report_user, v['name'], @bizmail_person))
end
end
report_gen = MultiKPIReportGenerator.new(bizlog_contexts, biztarget_contexts, $BIZMAIL_DIR + @current_config['combined_report'])
(subject, body) = report_gen.generate_report(@report_date)
elsif @current_config['type'] == 'aggregated_persons' then
blstat = BizLogStatus.new(@report_user)
reported_persons = blstat.person_status(@report_date, @bizmail_kpi)
@current_config['person'].each do |k, v|
return nil if !reported_persons.include?(v)
end
bizlog_contexts = []
biztarget_contexts = []
@current_config['person'].each do |k, v|
bizlog_contexts.push(BizLogContext.new(@report_user, @bizmail_kpi, v))
biztarget_contexts.push(BizTargetContext.new(@report_user, @bizmail_kpi, v))
end
report_gen = AggregatedPersonsReportGenerator.new(bizlog_contexts, biztarget_contexts, $BIZMAIL_DIR + @current_config['combined_report'])
(subject, body) = report_gen.generate_report(@report_date)
elsif @current_config['type'] == 'aggregated_items' then
blstat = BizLogStatus.new(@report_user)
reported_items = blstat.person_status(@report_date, @bizmail_kpi)
@current_config['item'].each do |k, v|
return nil if !reported_items.include?(v)
end
bizlog_contexts = []
biztarget_contexts = []
@current_config['item'].each do |k, v|
bizlog_contexts.push(BizLogContext.new(@report_user, @bizmail_kpi, v))
biztarget_contexts.push(BizTargetContext.new(@report_user, @bizmail_kpi, v))
end
report_gen = AggregatedItemsReportGenerator.new(bizlog_contexts, biztarget_contexts, $BIZMAIL_DIR + @current_config['combined_report'])
(subject, body) = report_gen.generate_report(@report_date)
end
tmail.to = to_addr_substitute(@current_config['combined_report_to'], @report_from)
tmail.subject = '=?ISO-2022-JP?B?' + NKF.nkf('-j', subject).split(//, 1).pack('m').chomp + '?='
tmail.body = NKF.nkf("--jis", body)
block.call(tmail)
end
def to_addr_substitute(to_addrs, from)
ret = []
to_addrs.each do |t|
t = from if t == '__FROM__'
ret.push(t)
end
return ret
end
end
if __FILE__ == $0 then
require 'rubygems'
require 'date'
require 'date-util'
$MYDOMAIN = "dmail.pgw.jp"
$BIZMAIL_DIR = "/Users/hiropipi/Documents/workspace/BizMail/"
$DBFILE = "/Users/hiropipi/Documents/workspace/BizMail/dbfile.sqlite3"
$CONFIG_YAML = "/Users/hiropipi/Documents/workspace/BizMail/config.yaml"
processor = BizMailProcessor.new
processor.parse_mail("[email protected]", "[email protected]",
Date.new(2011, 9, 20), "THIS IS TEST", "23")
processor.generate_report_mail do |tmail|
tmail.write_back
puts NKF.nkf('--utf8', tmail.encoded)
end
processor.generate_combined_report_mail do |tmail|
tmail.write_back
puts NKF.nkf('--utf8', tmail.encoded)
end
end