diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/exim_log.rb | 64 | ||||
-rw-r--r-- | app/models/exim_log_done.rb | 14 | ||||
-rw-r--r-- | app/models/info_request.rb | 3 |
3 files changed, 80 insertions, 1 deletions
diff --git a/app/models/exim_log.rb b/app/models/exim_log.rb new file mode 100644 index 000000000..7399428f4 --- /dev/null +++ b/app/models/exim_log.rb @@ -0,0 +1,64 @@ +# models/exim_log.rb: +# We load log file lines for requests in here, for display in the admin interface. +# +# Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved. +# Email: francis@mysociety.org; WWW: http://www.mysociety.org/ +# +# $Id: exim_log.rb,v 1.1 2009-01-27 17:12:31 francis Exp $ + +class EximLog < ActiveRecord::Base + belongs_to :info_request + belongs_to :exim_log_done + + # Load in exim log file from disk, or update if we already have it + # Assumes files are named with date, rather than cyclically. + # Doesn't do anything if file hasn't been modified since it was last loaded. + def EximLog.load_file(file_name) + modified = File::stat(file_name).mtime + raise "EximLog.load_file: file not found " + file_name if modified.nil? + + ActiveRecord::Base.transaction do + # see if we already have it + done = EximLogDone.find_by_filename(file_name) + if !done.nil? + if modified == done.last_stat + # already have that, nothing to do + return + end + ExmLog.delete_all "exim_log_dones.id = " + done.id + end + if !done + done = EximLogDone.new + done.filename = file_name + end + done.last_stat = modified + + # scan the file + f = File.open(file_name, 'r') + order = 0 + for line in f + order = order + 1 + email_domain = MySociety::Config.get("INCOMING_EMAIL_DOMAIN", "localhost") + emails = line.scan(/request-[^\s]+@#{email_domain}/).sort.uniq + for email in emails + info_request = InfoRequest.find_by_incoming_email(email) + if !info_request.nil? + STDERR.puts "adding log for " + info_request.url_title + " from " + file_name + " line " + line + exim_log = EximLog.new + exim_log.info_request = info_request + exim_log.exim_log_done = done + exim_log.line = line + exim_log.order = order + exim_log.save! + end + end + end + + # update done structure so we know when we last read this file + done.save! + end + end +end + + + diff --git a/app/models/exim_log_done.rb b/app/models/exim_log_done.rb new file mode 100644 index 000000000..a69f59194 --- /dev/null +++ b/app/models/exim_log_done.rb @@ -0,0 +1,14 @@ +# models/exim_log_done.rb: +# Stores that a particular exim file has been loaded in, see exim_log.rb +# +# Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved. +# Email: francis@mysociety.org; WWW: http://www.mysociety.org/ +# +# $Id: exim_log_done.rb,v 1.1 2009-01-27 17:12:31 francis Exp $ + +class EximLogDone < ActiveRecord::Base + has_many :exim_logs +end + + + diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 1a2b04410..0b02ae0b5 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -23,7 +23,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: info_request.rb,v 1.161 2009-01-26 12:12:17 francis Exp $ +# $Id: info_request.rb,v 1.162 2009-01-27 17:12:31 francis Exp $ require 'digest/sha1' require File.join(File.dirname(__FILE__),'../../vendor/plugins/acts_as_xapian/lib/acts_as_xapian') @@ -47,6 +47,7 @@ class InfoRequest < ActiveRecord::Base has_many :track_things, :order => 'created_at desc' has_many :comments, :order => 'created_at' has_many :censor_rules, :order => 'created_at desc' + has_many :exim_logs, :order => 'exim_log_done_id' # user described state (also update in info_request_event, admin_request/edit.rhtml) validates_inclusion_of :described_state, :in => [ |