diff --git a/.gitignore b/.gitignore index 1de886f..53f5ac0 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ *.sqlite3 config/database.yml config/site.yml +config/app.yml .bundle # OS generated files # diff --git a/Gemfile b/Gemfile index ed91691..eedb888 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 024498d..40e28d1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -97,6 +97,7 @@ PLATFORMS DEPENDENCIES jquery-rails + mail paperclip rails (= 3.2.9) rdiscount diff --git a/app/controllers/auth_controller.rb b/app/controllers/auth_controller.rb index a15d2d2..3a2ea77 100644 --- a/app/controllers/auth_controller.rb +++ b/app/controllers/auth_controller.rb @@ -1,3 +1,6 @@ +require 'net/imap' +require 'mail' + class AuthController < ApplicationController layout 'auth' @@ -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 diff --git a/app/views/auth/checkbrainmailbox.html.erb b/app/views/auth/checkbrainmailbox.html.erb new file mode 100644 index 0000000..e854149 --- /dev/null +++ b/app/views/auth/checkbrainmailbox.html.erb @@ -0,0 +1,2 @@ +<%= @n %> emails treated. +<%= @output %> diff --git a/config/app.template.yml b/config/app.template.yml new file mode 100644 index 0000000..a297f33 --- /dev/null +++ b/config/app.template.yml @@ -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 diff --git a/config/application.rb b/config/application.rb index db79a4d..0c86f00 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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. diff --git a/config/routes.rb b/config/routes.rb index 7857d9d..404fabc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ match 'login' => 'auth#login' match 'logout' => 'auth#logout' + match 'checkbrainmailbox' => 'auth#checkbrainmailbox' resources :users