diff options
-rw-r--r-- | app/controllers/user_controller.rb | 4 | ||||
-rw-r--r-- | app/models/info_request.rb | 38 | ||||
-rw-r--r-- | app/models/request_mailer.rb | 32 | ||||
-rw-r--r-- | config/general-example | 9 | ||||
-rwxr-xr-x | script/mailin | 7 | ||||
-rw-r--r-- | todo.txt | 2 |
6 files changed, 84 insertions, 8 deletions
diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 13b2d64cd..2bcb0203a 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -4,11 +4,11 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: user_controller.rb,v 1.2 2007-10-09 17:29:43 francis Exp $ +# $Id: user_controller.rb,v 1.3 2007-10-26 18:00:26 francis Exp $ class UserController < ApplicationController def index - @display_users = User.find(:all, :conditions => [ "name = ?", params[:name] ]) + @display_users = User.find(:all, :conditions => [ "name = ?", params[:name] ], :order => "created_at desc") end private diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 248f9423e..763e751be 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -4,7 +4,9 @@ # 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.6 2007-09-17 10:13:24 francis Exp $ +# $Id: info_request.rb,v 1.7 2007-10-26 18:00:26 francis Exp $ + +require 'digest/sha1' class InfoRequest < ActiveRecord::Base validates_presence_of :title @@ -16,5 +18,39 @@ class InfoRequest < ActiveRecord::Base validates_presence_of :public_body_id has_many :outgoing_messages + + # Email which public body should use to respond to request. This is in + # the format PREFIXrequest-ID-HASH@DOMAIN. Here ID is the id of the + # FOI request, and HASH is a signature for that id. + def incoming_email + incoming_email = MySociety::Config.get("INCOMING_EMAIL_PREFIX", "") + incoming_email += "request-" + self.id.to_s + incoming_email += "-" + Digest::SHA1.hexdigest(self.id.to_s + MySociety::Config.get("INCOMING_EMAIL_SECRET"))[0,8] + incoming_email += "@" + MySociety::Config.get("INCOMING_EMAIL_DOMAIN", "localhost") + return incoming_email + end + + # Return info request corresponding to an incoming email address, or nil if + # none found. Checks the hash to ensure the email came from the public body - + # only they are sent the email address with the has in it. + def self.find_by_incoming_email(incoming_email) + incoming_email =~ /request-(\d+)-([a-z0-9]+)/ + id = $1.to_i + hash = $2 + + expected_hash = Digest::SHA1.hexdigest(id.to_s + MySociety::Config.get("INCOMING_EMAIL_SECRET"))[0,8] + #print "expected: " + expected_hash + "\nhash: " + hash + "\n" + if hash != expected_hash + return nil + else + return self.find(id) + end + end + + # A new incoming email to this request + def receive(email) + raise "TBD" + end end + diff --git a/app/models/request_mailer.rb b/app/models/request_mailer.rb index 0e1c2be12..0825e4b03 100644 --- a/app/models/request_mailer.rb +++ b/app/models/request_mailer.rb @@ -4,12 +4,12 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: request_mailer.rb,v 1.2 2007-10-24 11:39:37 francis Exp $ +# $Id: request_mailer.rb,v 1.3 2007-10-26 18:00:26 francis Exp $ class RequestMailer < ActionMailer::Base def initial_request(info_request, outgoing_message) - @from = info_request.user.email + @from = info_request.incoming_email if MySociety::Config.getbool("STAGING_SITE", 1) @recipients = @from else @@ -19,4 +19,32 @@ class RequestMailer < ActionMailer::Base @body = {:info_request => info_request, :outgoing_message => outgoing_message} end + def receive(email) + # Find which info requests the email is for + info_requests = [] + for address in (email.to || []) + (email.cc || []) + info_request = InfoRequest.find_by_incoming_email(address) + info_requests.push(info_request) if info_request + end + + # Deal with each on + for info_request in info_requests + info_request.receive(email) + end + + # email.cc +# page = Page.find_by_address(email.to.first) +# page.emails.create( +# :subject => email.subject, :body => email.body +# ) +# +# if email.has_attachments? +# for attachment in email.attachments +# page.attachments.create({ +# :file => attachment, :description => email.subject +# }) +# end +# end + end + end diff --git a/config/general-example b/config/general-example index 172145343..ad5bd117b 100644 --- a/config/general-example +++ b/config/general-example @@ -14,14 +14,17 @@ * Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. * Email: francis@mysociety.org; WWW: http://www.mysociety.org * - * $Id: general-example,v 1.1 2007-10-24 11:39:38 francis Exp $ + * $Id: general-example,v 1.2 2007-10-26 18:00:27 francis Exp $ * */ // URL for use in emails etc. define('OPTION_BASE_URL', 'http://127.0.0.1:3000'); -// For test sites -define('OPTION_STAGING_SITE', 1); +// Incoming email +define('OPTION_INCOMING_EMAIL_DOMAIN', 'localhost'); // e.g. 'foifa.com' +define('OPTION_INCOMING_EMAIL_PREFIX', ''); // e.g. 'foi+' +define('OPTION_INCOMING_EMAIL_SECRET', 'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'); // Used for hash in request email address + ?> diff --git a/script/mailin b/script/mailin new file mode 100755 index 000000000..6411af615 --- /dev/null +++ b/script/mailin @@ -0,0 +1,7 @@ +#!/bin/bash + +LOC=`dirname $0` + +$LOC/runner 'RequestMailer.receive(STDIN.read)' + + @@ -5,7 +5,9 @@ Send actual FOIFA request Make it say "dear" as default letter Make sure index for every controller shows all, and the /controller/:id URLs go to a show action +so body/index.rhtml should go? and request/index.rhtml should be show.rhtml? +Make sure all models use self. for variables to reduce confusion Escape/simplify short name properly in URLs of public bodies For public bodies whose short names are renamed, make old URL still work and redirect |