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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
#!/usr/bin/env perl -w
# Copyright (C) 2000, 2005 MySQL AB
# Use is subject to license terms
#
# 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; version 2 of the License.
#
# 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-1335 USA
#
# Prints mails to standard output
#
####
#### Standard inits and get options
####
use DBI;
use Getopt::Long;
$VER="2.0";
@fldnms= ("mail_from","mail_to","cc","date","time_zone","file","sbj","txt");
my $fields= 0;
my $base_q= "";
my $mail_count= 0;
$opt_user= $opt_password= "";
$opt_socket= "/tmp/mysql.sock";
$opt_port= 3306;
$opt_db="mail";
$opt_table="my_mail";
$opt_help=$opt_count=0;
$opt_thread= 0;
$opt_host= "";
$opt_message_id= 0;
GetOptions("help","count","port=i","db=s","table=s","host=s","password=s",
"user=s","socket=s", "thread","message_id") || usage();
if ($opt_host eq '')
{
$opt_host = "localhost";
}
if ($opt_help || !$ARGV[0])
{
usage();
}
####
#### Connect and parsing the query to MySQL
####
$dbh= DBI->connect("DBI:MariaDB:$opt_db:$opt_host:port=$opt_port:mariadb_socket=$opt_socket", $opt_user,$opt_password, { PrintError => 0})
|| die $DBI::errstr;
main();
####
#### main
####
sub main
{
my ($row, $val, $q, $mail, $sth);
if ($opt_count)
{
count_mails();
}
$base_q= "SELECT ";
foreach $val (@fldnms)
{
if (!$fields)
{
$base_q.= "$val";
}
else
{
$base_q.= ",$val";
}
$fields++;
}
$base_q.= ",message_id" if ($opt_thread || $opt_message_id);
$base_q.= " FROM $opt_table";
$q= " WHERE $ARGV[0]";
$sth= $dbh->prepare($base_q . $q);
if (!$sth->execute)
{
print "$DBI::errstr\n";
$sth->finish;
die;
}
for (; ($row= $sth->fetchrow_arrayref); $mail_count++)
{
for ($i= 0; $i < $fields; $i++)
{
if ($opt_message_id)
{
$mail[$fields][$mail_count]= $row->[$fields];
$mail[$fields][$mail_count].= "\nNumber of Replies: " . get_nr_replies($row->[$fields]);
}
$mail[$i][$mail_count]= $row->[$i];
}
if ($opt_thread)
{
get_mail_by_message_id($row->[$fields], $mail);
}
}
print_mails($mail);
}
####
#### Function, which fetches mail by searching in-reply-to with
#### a given message_id. Saves the value (mail) in mail variable.
#### Returns the message id of the mail found and searches again
#### and saves, until no more mails are found with that message_id.
####
sub get_mail_by_message_id
{
my ($message_id, $mail)= @_;
my ($q, $query, $i, $row, $sth);
$q= " WHERE in_reply_to = \"$message_id\"";
$query= $base_q . $q;
$sth= $dbh->prepare($query);
if (!$sth->execute)
{
print "QUERY: $query\n$DBI::errstr\n";
$sth->finish;
die;
}
while (($row= $sth->fetchrow_arrayref))
{
$mail_count++;
for ($i= 0; $i < $fields; $i++)
{
if ($opt_message_id)
{
$mail[$fields][$mail_count]= $row->[$fields];
$mail[$fields][$mail_count].= "\nNumber of Replies: " . get_nr_replies($row->[$fields]);
}
$mail[$i][$mail_count]= $row->[$i];
}
$new_message_id= $row->[$fields];
if (defined($new_message_id) && length($new_message_id))
{
get_mail_by_message_id($new_message_id, $mail);
}
}
return;
}
####
#### Get number of replies for a given message_id
####
sub get_nr_replies
{
my ($message_id)= @_;
my ($sth, $sth2, $q, $row, $row2, $nr_replies);
$nr_replies= 0;
$q= "SELECT COUNT(*) FROM my_mail WHERE in_reply_to=\"$message_id\"";
$sth= $dbh->prepare($q);
if (!$sth->execute)
{
print "QUERY: $q\n$DBI::errstr\n";
$sth->finish;
die;
}
while (($row= $sth->fetchrow_arrayref))
{
if (($nr_replies= $row->[0]))
{
$q= "SELECT message_id FROM my_mail WHERE in_reply_to=\"$message_id\"";
$sth2= $dbh->prepare($q);
if (!$sth2->execute)
{
print "QUERY: $q\n$DBI::errstr\n";
$sth->finish;
die;
}
while (($row2= $sth2->fetchrow_arrayref))
{
# There may be several replies to the same mail. Also the
# replies to the 'parent' mail may contain several replies
# and so on. Thus we need to calculate it recursively.
$nr_replies+= get_nr_replies($row2->[0]);
}
}
return $nr_replies;
}
}
####
#### Print mails
####
sub print_mails
{
my ($mail)= @_;
my ($i);
for ($i=0; $mail[0][$i]; $i++)
{
print "#" x 33;
print " " . ($i+1) . ". Mail ";
print "#" x 33;
print "\n";
if ($opt_message_id)
{
print "Msg ID: $mail[$fields][$i]\n";
}
print "From: $mail[0][$i]\n";
print "To: $mail[1][$i]\n";
print "Cc:" . (defined($mail[2][$i]) ? $mail[2][$i] : "") . "\n";
print "Date: $mail[3][$i]\n";
print "Timezone: $mail[4][$i]\n";
print "File: $mail[5][$i]\n";
print "Subject: $mail[6][$i]\n";
print "Message:\n$mail[7][$i]\n";
}
print "#" x 20;
print " Summary: ";
if ($i == 1)
{
print "$i Mail ";
print "matches the query ";
}
else
{
print "$i Mails ";
print "match the query ";
}
print "#" x 20;
print "\n";
}
####
#### Count mails that matches the query, but don't show them
####
sub count_mails
{
my ($sth);
$sth= $dbh->prepare("select count(*) from $opt_table where $ARGV[0]");
if (!$sth->execute)
{
print "$DBI::errstr\n";
$sth->finish;
die;
}
while (($row= $sth->fetchrow_arrayref))
{
$mail_count= $row->[0];
}
if ($mail_count == 1)
{
print "$mail_count Mail matches the query.\n";
}
else
{
print "$mail_count Mails match the query.\n";
}
exit;
}
####
#### Usage
####
sub usage
{
print <<EOF;
pmail version $VER by Jani Tolonen
Usage: pmail [options] "SQL where clause"
Options:
--help show this help
--count Shows how many mails matches the query, but not the mails.
--db= database to use (Default: $opt_db)
--host= Hostname which to connect (Default: $opt_host)
--socket= Unix socket to be used for connection (Default: $opt_socket)
--password= Password to use for mysql
--user= User to be used for mysql connection, if not current user
--port= mysql port to be used (Default: $opt_port)
--thread Will search for possible replies to emails found by the search
criteria. Replies, if found, will be displayed right after the
original mail.
--message_id Display message_id on top of each mail. Useful when searching
email threads with --thread. On the second line is the number
of replies to the same thread, starting counting from that
mail (excluding possible parent mails).
"SQL where clause" is the end of the select clause,
where the condition is expressed. The result will
be the mail(s) that matches the condition and
will be displayed with the fields:
- From
- To
- Cc
- Date
- Timezone
- File (Where from the current mail was loaded into the database)
- Subject
- Message text
The field names that can be used in the where clause are:
Field Type
- message_id varchar(255) # Use with --thread and --message_id
- in_reply_to varchar(255) # Internally used by --thread
- mail_from varchar(120)
- date datetime
- sbj varchar(200)
- txt mediumtext
- cc text
- mail_to text
- time_zone varchar(6)
- reply varchar(120)
- file varchar(32)
- hash int(11)
An example of pmail:
pmail "txt like '%libmysql.dll%' and sbj like '%delphi%'"
NOTE: the txt field is NOT case sensitive!
EOF
exit(0);
}
|