-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtaskmail
executable file
·145 lines (114 loc) · 3.08 KB
/
taskmail
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/perl
##############################################################################
# taskmail - reads an eml file and writes its plain text into a notes file for use
# with taskwarrior and taskopen/tasknote.
#
# Copyright 2011, Johannes Schlatow.
# All rights reserved.
#
# This program 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.
#
# This program 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 should have received a copy of the GNU General Public License along with
# this program; if not, write to the
#
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA
# 02110-1301
# USA
#
###############################################################################
use strict;
use warnings;
use MIME::Parser;
#MIME::Tools->debugging(1);
#MIME::Tools->quiet(0);
######################
# config
my $task_bin = "/usr/bin/task";
my $notes_path = "$ENV{HOME}/Dropbox/.task/notes/";
my $notes_file_ext = ".txt";
sub parse_multipart
{
my $entity;
($entity) = @_;
my $parts = $entity->parts();
for (my $i=0; $i < $parts; $i++) {
my $subpart = $entity->parts($i);
if ($subpart->effective_type =~ /multipart/) {
$subpart = parse_multipart($subpart);
}
if ($subpart->effective_type =~ /text\/plain/) {
return $subpart;
}
}
return 0;
}
######################
# init
my $inputfile = $ARGV[0];
if (!$inputfile || !-e $inputfile) {
die "input file does not exist\n";
}
shift(@ARGV);
my $args = join(" ", @ARGV);
my $parser = new MIME::Parser;
$parser->output_dir("/tmp/");
$parser->decode_headers(1);
######################
# parse
my $entity = $parser->parse_open($inputfile) or die "parse failed\n";
# multipart
my $body;
my $type = $entity->effective_type;
if ($type =~ /(message|multipart)/) {
my $res = parse_multipart($entity);
if (!$res) {
die "no text/plain found in multipart message\n";
}
else {
$body = $res->bodyhandle;
}
} else {
$body = $entity->bodyhandle;
print "is $type";
}
my $head = $entity->head;
my $subject = $head->get('Subject');
my $from = $head->get('From');
if (!$subject) {
die "no subject found, file may be broken\n";
}
$subject =~ s/([#\$!\(\)\[\]\{\}'"`&;<>\*\|])/\\$1/g;
######################
# insert task
my $output = qx{$task_bin add $args -- $subject};
if ($output !~ /Created task/) {
die $output;
}
#find UUID from task
my $id = qx{$task_bin _ids | tail -1};
chomp($id);
my $uuid = qx{$task_bin $id uuids};
chomp($uuid);
#annotate task
qx{$task_bin $id annotate Notes};
######################
# create notes file
open(OUTFILE, ">$notes_path$uuid$notes_file_ext");
print OUTFILE "$uuid\n\n";
print OUTFILE "# $subject";
print OUTFILE "From: $from\n";
$body->print(\*OUTFILE);
######################
# cleanup
close OUTFILE;
$entity->purge();