3
# Prints mails to standard output
6
#### Standard inits and get options
14
@fldnms= ("mail_from","mail_to","cc","date","time_zone","file","sbj","txt");
19
$opt_user= $opt_password= "";
20
$opt_socket= "/tmp/mysql.sock";
24
$opt_help=$opt_count=0;
29
GetOptions("help","count","port=i","db=s","table=s","host=s","password=s",
30
"user=s","socket=s", "thread","message_id") || usage();
34
$opt_host = "localhost";
37
if ($opt_help || !$ARGV[0])
43
#### Connect and parsing the query to MySQL
46
$dbh= DBI->connect("DBI:mysql:$opt_db:$opt_host:port=$opt_port:mysql_socket=$opt_socket", $opt_user,$opt_password, { PrintError => 0})
57
my ($row, $val, $q, $mail, $sth);
65
foreach $val (@fldnms)
77
$base_q.= ",message_id" if ($opt_thread || $opt_message_id);
78
$base_q.= " FROM $opt_table";
79
$q= " WHERE $ARGV[0]";
81
$sth= $dbh->prepare($base_q . $q);
84
print "$DBI::errstr\n";
88
for (; ($row= $sth->fetchrow_arrayref); $mail_count++)
90
for ($i= 0; $i < $fields; $i++)
94
$mail[$fields][$mail_count]= $row->[$fields];
95
$mail[$fields][$mail_count].= "\nNumber of Replies: " . get_nr_replies($row->[$fields]);
97
$mail[$i][$mail_count]= $row->[$i];
101
get_mail_by_message_id($row->[$fields], $mail);
108
#### Function, which fetches mail by searching in-reply-to with
109
#### a given message_id. Saves the value (mail) in mail variable.
110
#### Returns the message id of the mail found and searches again
111
#### and saves, until no more mails are found with that message_id.
114
sub get_mail_by_message_id
116
my ($message_id, $mail)= @_;
117
my ($q, $query, $i, $row, $sth);
119
$q= " WHERE in_reply_to = \"$message_id\"";
120
$query= $base_q . $q;
121
$sth= $dbh->prepare($query);
124
print "QUERY: $query\n$DBI::errstr\n";
128
while (($row= $sth->fetchrow_arrayref))
131
for ($i= 0; $i < $fields; $i++)
135
$mail[$fields][$mail_count]= $row->[$fields];
136
$mail[$fields][$mail_count].= "\nNumber of Replies: " . get_nr_replies($row->[$fields]);
138
$mail[$i][$mail_count]= $row->[$i];
140
$new_message_id= $row->[$fields];
141
if (defined($new_message_id) && length($new_message_id))
143
get_mail_by_message_id($new_message_id, $mail);
150
#### Get number of replies for a given message_id
155
my ($message_id)= @_;
156
my ($sth, $sth2, $q, $row, $row2, $nr_replies);
159
$q= "SELECT COUNT(*) FROM my_mail WHERE in_reply_to=\"$message_id\"";
160
$sth= $dbh->prepare($q);
163
print "QUERY: $q\n$DBI::errstr\n";
167
while (($row= $sth->fetchrow_arrayref))
169
if (($nr_replies= $row->[0]))
171
$q= "SELECT message_id FROM my_mail WHERE in_reply_to=\"$message_id\"";
172
$sth2= $dbh->prepare($q);
175
print "QUERY: $q\n$DBI::errstr\n";
179
while (($row2= $sth2->fetchrow_arrayref))
181
# There may be several replies to the same mail. Also the
182
# replies to the 'parent' mail may contain several replies
183
# and so on. Thus we need to calculate it recursively.
184
$nr_replies+= get_nr_replies($row2->[0]);
200
for ($i=0; $mail[0][$i]; $i++)
203
print " " . ($i+1) . ". Mail ";
208
print "Msg ID: $mail[$fields][$i]\n";
210
print "From: $mail[0][$i]\n";
211
print "To: $mail[1][$i]\n";
212
print "Cc:" . (defined($mail[2][$i]) ? $mail[2][$i] : "") . "\n";
213
print "Date: $mail[3][$i]\n";
214
print "Timezone: $mail[4][$i]\n";
215
print "File: $mail[5][$i]\n";
216
print "Subject: $mail[6][$i]\n";
217
print "Message:\n$mail[7][$i]\n";
224
print "matches the query ";
229
print "match the query ";
236
#### Count mails that matches the query, but don't show them
243
$sth= $dbh->prepare("select count(*) from $opt_table where $ARGV[0]");
246
print "$DBI::errstr\n";
250
while (($row= $sth->fetchrow_arrayref))
252
$mail_count= $row->[0];
254
if ($mail_count == 1)
256
print "$mail_count Mail matches the query.\n";
260
print "$mail_count Mails match the query.\n";
272
pmail version $VER by Jani Tolonen
274
Usage: pmail [options] "SQL where clause"
276
--help show this help
277
--count Shows how many mails matches the query, but not the mails.
278
--db= database to use (Default: $opt_db)
279
--host= Hostname which to connect (Default: $opt_host)
280
--socket= Unix socket to be used for connection (Default: $opt_socket)
281
--password= Password to use for mysql
282
--user= User to be used for mysql connection, if not current user
283
--port= mysql port to be used (Default: $opt_port)
284
--thread Will search for possible replies to emails found by the search
285
criteria. Replies, if found, will be displayed right after the
287
--message_id Display message_id on top of each mail. Useful when searching
288
email threads with --thread. On the second line is the number
289
of replies to the same thread, starting counting from that
290
mail (excluding possible parent mails).
291
"SQL where clause" is the end of the select clause,
292
where the condition is expressed. The result will
293
be the mail(s) that matches the condition and
294
will be displayed with the fields:
300
- File (Where from the current mail was loaded into the database)
303
The field names that can be used in the where clause are:
305
- message_id varchar(255) # Use with --thread and --message_id
306
- in_reply_to varchar(255) # Internally used by --thread
307
- mail_from varchar(120)
313
- time_zone varchar(6)
318
pmail "txt like '%libmysql.dll%' and sbj like '%delphi%'"
319
NOTE: the txt field is NOT case sensitive!