Welcome Guest Login Register Member List
ExpressionEngine Forums
Advanced Search
Username: Password:
Remember Me? forgot password?
You are here: Forum Home  >  Usage  >  Tips and Tricks  >  Thread
   
 
Submit Tasks to your Tracks through Email
 
desrod
Posted: 02 December 2008 01:47 AM   [ Ignore ]  
Newbie
Avatar
Rank
Total Posts:  7
Joined  2008-11-24

I posted a reply in a lower thread that was buried, so I’ll re-post the script here. I’ve taken some ideas from the work previously mentioned here and refactored it a LOT to reduce the size and complexity of the code involved, and leveraged the use of upstream modules to do the heavy lifting for me.

Here is a simple script that allows you to email your Tasks to your tracks install, creating new entries and contexts and due dates at the same time (and yes, my code really IS shorter than my comments):

#!/usr/bin/perl
#        _._   
#       /_ _`.      (c) 2008, David A. Desrosiers
#       (.(.)|                   setuid@gmail.com
#       |\_/'|   
#       )____`\     Email-to-Tracks interface
#      //_V _\ \ 
#     ((  |  `(_)   If you find this useful, please drop me 
#    / \> '   / \   an email, or send me bug reports if you find
#    \  \.__./  /   problems with it. I accept PayPal too! =-)
#     `-'    `-' 
#
##############################################################################
# 
# License
#
##############################################################################
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# The copyright notice must remain intact to remain in compliance with use.
# 
# This script is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
##############################################################################
#
# You'll need to set this up as an alias in your MTA configuration (typically
# /etc/aliases), as follows:
#
# tracks@your-site.tld: "|/path/to/gtd-tracks-mail.pl"
#
# Don't forget to run newaliases(1) after you've modified this so it can
# update the aliases table with the new entry.
#
# Email format can be any of the following:
#
#       Subject: The main description of the Task
#
#       @Context
#       01/02/2003
#       This is my note text
#
# or:
# 
#       Subject: Default Task description
#
#       c: @context
#       d: 2008/12/31
#       n: The note text to insert
#
# Most valid date formats are accepted, and this will do its best to
# "correct" and normalize them. You can also prefix your lines with the
# modifiers above, or read the regexes below for more. For example: 
# c:, context:, cxt:, con:, ct: are all valid prefixes for "context"
# n: and note: are all valid prefixes for "notes", and so on.
#
##############################################################################
use strict;
use 
warnings;
use 
XML::Simple;
use 
LWP::UserAgent;
use 
URI::Escape;
use 
Email::Abstract;
use 
Date::Manip;
use 
Date::Parse;

my $url                 = "http://your-site.tld/todos.xml";
my $contextUrl          = "http://your-site.tld/contexts.xml";

# The default contextid where you want the Task added
# SELECT id,name FROM contexts;
my $contextid           = "8";

my $user                = "yourname";
my $password            = "yourpass";

# Leave these tokens alone.  They are valid as of Tracks 1.5 RESTful API.
my %todo = map { +($_ => "todo[$_]") } qw(notes context_id description due);

# Get the context legend in order to match by name
my $ua                  = new LWP::UserAgent;
my $req                 = new HTTP::Request 'GET',$contextUrl;
$req->authorization_basic($user,$password);
my $res                 = $ua->request($req);
my $contexts            = XMLin($res->content);

# Split apart the email into Subject and Body
my $message             = do { local $/; <STDIN> };
my $email               = Email::Abstract->new($message);
my $subject             = $email->get_header("Subject");
my $body                = $email->get_body;

# These can probably be cleaned up a bit
my ($context_line)      = $body =~ /^(?:c:|ct:|cxt:|con:|context:|@)\s*(.+)$/mi;
my ($date_line)         = $body =~ /^(?:d:|date:)\s*(\d.*)$/m;
$date_line              = UnixDate(ParseDate("today"), "%g") if (length($date_line) == 0);
my $time                = str2time($date_line);
my $due_date            = UnixDate(scalar gmtime($time), "%m/%d/%Y");
my ($note_line)         = $body =~ /^(?:n:|note:)\s*(.*?)$/mi; 

# Concatenate the data here before we send POST to the Tracks server
my $post_data = 
        
$todo{'context_id'} . "=" . $contextid . "&" . 
        
$todo{'description'}. "=" . uri_escape($subject) . "&" . 
        
$todo{'notes'} . "=" . uri_escape($note_line) . "&" . 
        
$todo{'due'} . "=" . uri_escape($due_date);

# Use LWP to do the posting ($ua was created earlier)
$req = new HTTP::Request 'POST',$url;
$req->content_type('application/x-www-form-urlencoded');
$req->content($post_data);
$req->authorization_basic($user,$password);
$res = $ua->request($req); 
Profile
 
fucheeno
Posted: 25 December 2008 07:34 PM   [ Ignore ]   [ # 1 ]  
Newbie
Rank
Total Posts:  5
Joined  2008-12-25

This seems like an amazing script but I can’t for the life of me get it to work.  I have verified that the script is getting run from my mail.log and I have installed all the required perl modules but nothing ever gets submitted to tracks.  Is there any way I can debug this further from the command line perhaps?  Thanks for any help you can provide.

Profile
 
fucheeno
Posted: 16 January 2009 04:53 PM   [ Ignore ]   [ # 2 ]  
Newbie
Rank
Total Posts:  5
Joined  2008-12-25

bumping my own thread in case anyone else has this working and can offer some troubleshooting suggestions.  thanks.

Profile
 
desrod
Posted: 16 January 2009 05:21 PM   [ Ignore ]   [ # 3 ]  
Newbie
Avatar
Rank
Total Posts:  7
Joined  2008-11-24

I wrote this script (well, cleaned up and rewrote it from scratch), and I can verify that it does indeed work as expected.

Are you running Tracks 1.6?
Does mail work on your system?
What MTA?
What do your logs say when you crank up verbosity to maximum?
What happens if you just output the data to a file instead of a mail pipe?
Does the resulting file contain what you expect? (this is how I tested and debugged it)

Let’s start there.

Profile
 
fucheeno
Posted: 17 January 2009 03:59 AM   [ Ignore ]   [ # 4 ]  
Newbie
Rank
Total Posts:  5
Joined  2008-12-25

Thanks for getting back to me desrod.  I am using 1.7rc.  Mail is working for the user I put in the /etc/aliases file.  I really don’t know why it’s not working.  Here’s a tail from the mail.log

Jan 16 19:56:39 tracks postfix/smtpd[9926]: connect from rv-out-0506.google.com[209.85.198.235]
Jan 16 19:56:39 tracks postfix/smtpd[9926]: 2BB1621A95D2: client=rv-out-0506.google.com[209.85.198.235]
Jan 16 19:56:39 tracks postfix/cleanup[9930]: 2BB1621A95D2: message-id=<D97FF910-4B52-416E-933A-FB141B3FDBFB@google.com>
Jan 16 19:56:39 tracks postfix/qmgr[5993]: 2BB1621A95D2: from=<mike@google.com>, size=1162, nrcpt=1 (queue active)
Jan 16 19:56:39 tracks postfix/local[9931]: 2BB1621A95D2: to=<fmaster@tracks.blah.com>, relay=local, delay=0.56, delays=0.11/0.01/0/0.45, dsn=2.0.0, status=sent (delivered to command: /usr/local/bin/mailsubmitter.pl)
Jan 16 19:56:39 tracks postfix/qmgr[5993]: 2BB1621A95D2: removed
Jan 16 19:57:09 tracks postfix/smtpd[9926]: disconnect from rv-out-0506.google.com[209.85.198.235]


So the mail definitely looks like it’s getting sent to the command properly.  How exactly can I test your script from the command line using just the script.  Can you give me some usage examples that you used yourself during your debugging?  I am kind of green with some of this stuff.  Thanks so much for your help so far!

Profile
 
Reinier Balt
Posted: 20 January 2009 08:11 PM   [ Ignore ]   [ # 5 ]  
Sr. Member
RankRankRankRank
Total Posts:  580
Joined  2006-10-05

you can also try to check your Tracks logs to see if the scripts tries to sent the email to Tracks.

look for postings to todos.xml

Profile
 
Happy Chap
Posted: 28 February 2009 12:50 PM   [ Ignore ]   [ # 6 ]  
Newbie
Rank
Total Posts:  2
Joined  2009-02-28

Hi, I’ve setup this script and, AFAIK, all the component parts seem to work, in so far as emails do get submitted to Tracks. However, it seems to be failing at Tracks with ActionController::InvalidAuthenticityToken error.

In the script, I’ve replaced the $user and $password with the username and password that I log in interactively to Tracks with. The next comment line says ‘leave tokens alone’, so I have but does the script need my API token anywhere (maybe I’ve just missed this in the scrip)?

Any ideas how to get this to work?

I’m using Tracks 1.7RC2 on openSUSE 10.3 and the latest PERL and Ruby packages.

Thanks, David

Profile
 
Happy Chap
Posted: 28 February 2009 04:01 PM   [ Ignore ]   [ # 7 ]  
Newbie
Rank
Total Posts:  2
Joined  2009-02-28

I followed the problem through to a comment in the request_forgery_protection.rb and, whilst I didn’t understand how to submit th token Tracks is expecting I did see the comment about adding

config.action_controller.allow_forgery_protection=false

to production.rb, however that doesn’t seem to work - I’m still getting the same error.

Any ideas anyone?

Thanks, David.

Profile
 
Reinier Balt
Posted: 01 March 2009 04:45 PM   [ Ignore ]   [ # 8 ]  
Sr. Member
RankRankRankRank
Total Posts:  580
Joined  2006-10-05

Please check the integrations page. You need to set the content-type explicitly for this to work. Otherwise Tracks (rails) will revert to normal web operations, requiring the auth.token.

Profile
 
MattKatz
Posted: 27 April 2009 01:08 PM   [ Ignore ]   [ # 9 ]  
Newbie
Rank
Total Posts:  2
Joined  2009-04-27

I really liked this idea, but I’m on dreamhost, so I can’t really use it. 
I wrote a quick and dirty ruby script that checks an email address via POP3, posts emails as todos to tracks, then deletes the email.  It’s very short, so you should be able to adapt it to whatever you like.  If I need more features I’ll add them, but for now it does what I want.
I’ve put it up on my subversion repo.

#!/usr/bin/ruby
# you'll probably need your own ruby gems if you are running this on a shared host.
# I did this approach: http://railstips.org/2008/11/25/rubygems-yours-mine-and-ours
require 'net/pop'
require 'rubygems'
# if you do not have this installed just type 'gem install tmail'
require 'tmail'
# important - this needs to be the path from this script
# to the tracks_api_wrapper.rb
# it is usually under YOURTRACKSINSTALL/doc/tracks_api_wrapper
require 'tracks_api_wrapper'
#mail configuration
mail_user = 'your_login@yoursite.com'
mail_password = 'y0ur_p@$$w0rd'
mail_server = 'mail.yoursite.com'
#tracks configuration
tracks_user = 'your_tracks_user'
tracks_password = 's00p3r_s3cur3_p@$$'
tracks_server = 'your.tracksinstall.com/full_path_to_it'
tracks_default_context = 3 #this is just the context we will put your mail under
Tracks::Base.site = "http://" + tracks_user +":"+ tracks_password+"@"+tracks_server
#open each mail on the server
pop = Net::POP3.new(mail_server)
pop.start(mail_user,mail_password) 
  
pop.each_mail do |mail|
    
#parse the mail
    
email = TMail::Mail.parse(mail.pop)
    
t = Tracks::Todo.new
    
t.description = email.subject
    t
.notes = email.body + "\nfrom: " + email.from.to_s
    t
.context_id = tracks_default_context
    t
.save
    
#get rid of the email.  
    
mail.delete
  end
pop
.finish 

This was super easy to write thanks to ruby and the tracks_api_wrapper under the docs folder.  Hope this helps some folks!

Profile
 
   
 
 
‹‹ an excessive usage story and some feature ideas      Right way to unhide an orphan hidden action? ››

Powered By ExpressionEngine
Template Design By Sonnenvogel.com
Select a theme:

ExpressionEngine Discussion Forum - Version 2.1.2 (20091002)
Script Executed in 0.1400 seconds

Atom Feed
RSS 2.0