aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Landauer <matthew@openaustralia.org>2012-10-10 14:20:30 +1100
committerMatthew Landauer <matthew@openaustralia.org>2012-10-10 14:20:30 +1100
commit227f04b9393d9258e7013afce9ceb01a653a841c (patch)
tree09d6989c70fe81791094f984e3803b978413864d
parent79c26d3b5fa7d77ca22f77b6c70d42212bd7a578 (diff)
Add method for loading postfix log data
-rw-r--r--app/models/exim_log.rb21
-rw-r--r--spec/models/exim_log_spec.rb31
2 files changed, 52 insertions, 0 deletions
diff --git a/app/models/exim_log.rb b/app/models/exim_log.rb
index cbe2277c5..4c3e104af 100644
--- a/app/models/exim_log.rb
+++ b/app/models/exim_log.rb
@@ -73,6 +73,27 @@ class EximLog < ActiveRecord::Base
end
end
+ def EximLog.load_postfix_log_data(f, done)
+ order = 0
+ emails = scan_for_postfix_queue_ids(f)
+ # Go back to the beginning of the file
+ f.rewind
+ f.each do |line|
+ order = order + 1
+ queue_id = extract_queue_id_from_syslog_line(line)
+ if emails.has_key?(queue_id)
+ emails[queue_id].each do |email|
+ info_request = InfoRequest.find_by_incoming_email(email)
+ if info_request
+ info_request.exim_logs.create!(:line => line, :order => order, :exim_log_done => done)
+ else
+ puts "Warning: Could not find request with email #{email}"
+ end
+ end
+ end
+ end
+ end
+
def EximLog.scan_for_postfix_queue_ids(f)
result = {}
f.each do |line|
diff --git a/spec/models/exim_log_spec.rb b/spec/models/exim_log_spec.rb
index 563ca5d06..5ce3c6e79 100644
--- a/spec/models/exim_log_spec.rb
+++ b/spec/models/exim_log_spec.rb
@@ -73,6 +73,37 @@ describe EximLog do
"Oct 3 16:39:38 host postfix/qmgr[15615]: CB55836EE58C: removed",
]}
+ describe ".load_postfix_log_data" do
+ # Postfix logs for a single email go over multiple lines. They are all tied together with the Queue ID.
+ # See http://onlamp.com/onlamp/2004/01/22/postfix.html
+ it "loads the postfix log and untangles seperate email transactions using the queue ID" do
+ Configuration.stub!(:incoming_email_domain).and_return("example.com")
+ log.stub!(:rewind)
+ ir1 = info_requests(:fancy_dog_request)
+ ir2 = info_requests(:naughty_chicken_request)
+ InfoRequest.should_receive(:find_by_incoming_email).with("request-14-e0e09f97@example.com").any_number_of_times.and_return(ir1)
+ InfoRequest.should_receive(:find_by_incoming_email).with("request-10-1234@example.com").any_number_of_times.and_return(ir2)
+ EximLog.load_postfix_log_data(log, EximLogDone.new(:filename => "foo", :last_stat => DateTime.now))
+ # TODO: Check that each log line is attached to the correct request
+ ir1.exim_logs.count.should == 5
+ ir1.exim_logs[0].order.should == 1
+ ir1.exim_logs[0].line.should == "Oct 3 16:39:35 host postfix/pickup[2257]: CB55836EE58C: uid=1003 from=<foitest+request-14-e0e09f97@example.com>"
+ ir1.exim_logs[1].order.should == 2
+ ir1.exim_logs[1].line.should == "Oct 3 16:39:35 host postfix/cleanup[7674]: CB55836EE58C: message-id=<ogm-15+506bdda7a4551-20ee@example.com>"
+ ir1.exim_logs[2].order.should == 4
+ ir1.exim_logs[2].line.should == "Oct 3 16:39:35 host postfix/qmgr[15615]: CB55836EE58C: from=<foitest+request-14-e0e09f97@example.com>, size=1695, nrcpt=1 (queue active)"
+ ir1.exim_logs[3].order.should == 5
+ ir1.exim_logs[3].line.should == "Oct 3 16:39:38 host postfix/smtp[7676]: CB55836EE58C: to=<foi@some.gov.au>, relay=aspmx.l.google.com[74.125.25.27]:25, delay=2.5, delays=0.13/0.02/1.7/0.59, dsn=2.0.0, status=sent (250 2.0.0 OK 1349246383 j9si1676296paw.328)"
+ ir1.exim_logs[4].order.should == 7
+ ir1.exim_logs[4].line.should == "Oct 3 16:39:38 host postfix/qmgr[15615]: CB55836EE58C: removed"
+ ir2.exim_logs.count.should == 2
+ ir2.exim_logs[0].order.should == 3
+ ir2.exim_logs[0].line.should == "Oct 3 16:39:35 host postfix/qmgr[1673]: 9634B16F7F7: from=<foitest+request-10-1234@example.com>, size=368, nrcpt=1 (queue active)"
+ ir2.exim_logs[1].order.should == 6
+ ir2.exim_logs[1].line.should == "Oct 3 16:39:38 host postfix/smtp[1681]: 9634B16F7F7: to=<kdent@example.com>, relay=none, delay=46, status=deferred (connect to 216.150.150.131[216.150.150.131]: No route to host)"
+ end
+ end
+
describe ".scan_for_postfix_queue_ids" do
it "returns the queue ids of interest with the connected email addresses" do
Configuration.stub!(:incoming_email_domain).and_return("example.com")