Skip to content

Commit

Permalink
Adding new mail capability!
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Alain Bandinelli committed Dec 30, 2012
1 parent 06307e1 commit 2ef927b
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*.sqlite3
config/database.yml
config/site.yml
config/app.yml
.bundle

# OS generated files #
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gem 'rails', '3.2.9'
gem 'sqlite3'
gem 'jquery-rails'
gem 'will_paginate'
gem 'mail'

# Use unicorn as the web server
# gem 'unicorn'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ PLATFORMS

DEPENDENCIES
jquery-rails
mail
paperclip
rails (= 3.2.9)
rdiscount
Expand Down
72 changes: 72 additions & 0 deletions app/controllers/auth_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'net/imap'
require 'mail'

class AuthController < ApplicationController
layout 'auth'

Expand All @@ -23,4 +26,73 @@ def logout
redirect_to(:controller=>"neurons", :action=>"index")
end


def checkbrainmailbox
#Check if token is present
logger.info params["token"]
if APP_CONFIG['token_action'].to_s == params["token"]
#Start the actual work
imap = Net::IMAP.new(APP_CONFIG['imap_host'], APP_CONFIG['imap_port'] , APP_CONFIG['imap_ssl'], nil, false)
imap.authenticate('LOGIN', APP_CONFIG['imap_user'], APP_CONFIG['imap_password'])
imap.select('INBOX')
@output = "Connected to IMAP server\n"

@n=0
#Select unseen messages only
imap.search(["NOT", "SEEN"]).each do |message_id|

#Get the full content
raw = imap.fetch(message_id, "BODY[]")[0].attr["BODY[]"]
imap.store(message_id, '+FLAGS', [:Seen])
#Parse it with mail library
mail = Mail.read_from_string(raw)
token = mail.to.to_s
#If multipart or auth token not included, then discard the mail and send a warning
if mail.multipart? or (not token.include?(APP_CONFIG['token_email'].to_s))
imap.copy(message_id, 'Untreated')
@output=@output+Time.now.getutc.to_s+" - 1 untreated mail\n"
send_warning_mail(mail.from, raw)
else
content = mail.body.decoded
name = mail.subject
date = mail.date
#Detect if labels are specified in first line
if content.lines.first.to_s[0]=="@"
labels = content.lines.first.chomp
content = content.lines.to_a[1..-1].join
end
#Here, create the neuron
@output=@output+Time.now.getutc.to_s+" - 1 neuron created from a mail\n"
puts "One neuron created with name '#{name}', labels '#{labels}' and content '#{content}'"
neuron = Neuron.new
neuron.name = name
neuron.content = content
neuron.labels = labels
neuron.date = date
neuron.save
imap.copy(message_id, 'Treated')
end
imap.store(message_id, '+FLAGS', [:Deleted])
@n+=1
end
imap.expunge #Delete all mails with deleted flags
imap.close
end
render :layout => false
end


private

def send_warning_mail(from, content)
warning_mail = Mail.new do
from APP_CONFIG['imap_email']
to APP_CONFIG['warning_email']
subject 'Non valid email received in brain mailbox'
body "A non valid email received in brain mailbox from "+from.to_s+"\n\n"+content.to_s
end
warning_mail.delivery_method :sendmail
warning_mail.deliver
end

end
2 changes: 2 additions & 0 deletions app/views/auth/checkbrainmailbox.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= @n %> emails treated.
<%= @output %>
9 changes: 9 additions & 0 deletions config/app.template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
imap_host: host
imap_port: 993
imap_ssl: true
imap_user: user
imap_email: email@host.fr
imap_password: password
warning_email: warnme@host.fr
token_email: 123456
token_action: 123456
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require File.expand_path('../boot', __FILE__)

require 'rails/all'
require 'yaml'
APP_CONFIG = YAML.load(File.read(File.expand_path('../app.yml', __FILE__)))


# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

match 'login' => 'auth#login'
match 'logout' => 'auth#logout'
match 'checkbrainmailbox' => 'auth#checkbrainmailbox'

resources :users

Expand Down

0 comments on commit 2ef927b

Please sign in to comment.