home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / examples / tests / pmail.pl < prev    next >
Encoding:
Perl Script  |  2004-10-28  |  3.9 KB  |  196 lines

  1. #!/usr/bin/perl
  2. #                                  
  3. # Prints mails to standard output  
  4. #                                  
  5. ####
  6. #### Standard inits and get options
  7. ####
  8.  
  9. use DBI;
  10. use Getopt::Long;
  11.  
  12. $VER="1.5";
  13.  
  14. @fldnms= ("mail_from","mail_to","cc","date","time_zone","file","sbj","txt");
  15. $fields=8;
  16. @mail= (@from,@to,@cc,@date,@time_zone,@file,@sbj,@txt);
  17.  
  18. $opt_user= $opt_password= "";
  19. $opt_socket= "/tmp/mysql.sock";
  20. $opt_port= 3306;
  21. $opt_db="mail";
  22. $opt_table="mails";
  23. $opt_help=$opt_count=0;
  24.  
  25. GetOptions("help","count","port=i","db=s","table=s","host=s","password=s",
  26.        "user=s","socket=s") || usage();
  27.  
  28. if ($opt_host eq '')
  29. {
  30.   $opt_host = "localhost";
  31. }
  32.  
  33. if ($opt_help || !$ARGV[0])
  34. {
  35.   usage();
  36. }
  37.  
  38. ####
  39. #### Connect and parsing the query to MySQL
  40. ####
  41.  
  42. $dbh= DBI->connect("DBI:mysql:$opt_db:$opt_host:port=$opt_port:mysql_socket=$opt_mysql_socket", $opt_user,$opt_password, { PrintError => 0})
  43. || die $DBI::errstr;
  44.  
  45. if ($opt_count)
  46. {
  47.   count_mails();
  48. }
  49.  
  50. $fields=0;
  51. $query = "select ";
  52. foreach $val (@fldnms)
  53. {
  54.   if (!$fields)
  55.   {
  56.     $query.= "$val";
  57.   }
  58.   else
  59.   {
  60.     $query.= ",$val";
  61.   }
  62.   $fields++;
  63. }
  64. $query.= " from $opt_table where $ARGV[0] order by date desc";
  65.  
  66. ####
  67. #### Send query and save result
  68. ####
  69.  
  70. $sth= $dbh->prepare($query);
  71. if (!$sth->execute)
  72. {
  73.   print "$DBI::errstr\n";
  74.   $sth->finish;
  75.   die;
  76. }
  77. for ($i=0; ($row= $sth->fetchrow_arrayref); $i++)
  78. {
  79.   for ($j=0; $j < $fields; $j++)
  80.   {
  81.     $mail[$j][$i]= $row->[$j];
  82.   }
  83. }
  84.  
  85. ####
  86. #### Print to stderr
  87. ####
  88.  
  89. for ($i=0; $mail[0][$i]; $i++)
  90. {
  91.   print "#" x 33;
  92.   print " " . ($i+1) . ". Mail ";
  93.   print "#" x 33;
  94.   print "\nFrom: $mail[0][$i]\n";
  95.   print "To: $mail[1][$i]\n";
  96.   print "Cc: $mail[2][$i]\n";
  97.   print "Date: $mail[3][$i]\n";
  98.   print "Timezone: $mail[4][$i]\n";
  99.   print "File: $mail[5][$i]\n";
  100.   print "Subject: $mail[6][$i]\n";
  101.   print "Message:\n$mail[7][$i]\n";
  102. }
  103. print "#" x 20;
  104. print " Summary: ";
  105. if ($i == 1) 
  106. {
  107.   print "$i Mail ";
  108.   print "matches the query ";
  109. }
  110. else
  111. {
  112.   print "$i Mails ";
  113.   print "match the query ";
  114. }
  115. print "#" x 20;
  116. print "\n";
  117.  
  118. ####
  119. #### Count mails that matches the query, but don't show them
  120. ####
  121.  
  122. sub count_mails
  123. {
  124.   $sth= $dbh->prepare("select count(*) from $opt_table where $ARGV[0]");
  125.   if (!$sth->execute)
  126.   {
  127.     print "$DBI::errstr\n";
  128.     $sth->finish;
  129.     die;
  130.   }
  131.   while (($row= $sth->fetchrow_arrayref))
  132.   {
  133.     $mail_count= $row->[0];
  134.   }
  135.   if ($mail_count == 1)
  136.   {  
  137.     print "$mail_count Mail matches the query.\n";
  138.   }
  139.   else
  140.   {
  141.     print "$mail_count Mails match the query.\n";
  142.   }
  143.   exit;
  144. }
  145.  
  146. ####
  147. #### Usage
  148. ####
  149.  
  150. sub usage
  151. {
  152.   print <<EOF;
  153.   pmail version $VER by Jani Tolonen
  154.  
  155.   Usage: pmail [options] "SQL where clause"
  156.   Options:
  157.   --help      show this help
  158.   --count     Shows how many mails matches the query, but not the mails.
  159.   --db=       database to use (Default: $opt_db)
  160.   --table=    table to use    (Default: $opt_table)
  161.   --host=     Hostname which to connect (Default: $opt_host)
  162.   --socket=   Unix socket to be used for connection (Default: $opt_socket)
  163.   --password= Password to use for mysql
  164.   --user=     User to be used for mysql connection, if not current user
  165.   --port=     mysql port to be used (Default: $opt_port)
  166.   "SQL where clause" is the end of the select clause,
  167.   where the condition is expressed. The result will
  168.   be the mail(s) that matches the condition and
  169.   will be displayed with the fields:
  170.   - From
  171.   - To
  172.   - Cc
  173.   - Date
  174.   - Timezone
  175.   - File (Where from the current mail was loaded into the database)
  176.   - Subject
  177.   - Message text
  178.   The field names that can be used in the where clause are:
  179.     Field      Type 
  180.   - mail_from  varchar(120)
  181.   - date       datetime
  182.   - sbj        varchar(200)
  183.   - txt        mediumtext
  184.   - cc         text
  185.   - mail_to    text
  186.   - time_zone  varchar(6)
  187.   - reply      varchar(120)
  188.   - file       varchar(32)
  189.   - hash       int(11)
  190.   An example of the pmail:
  191.   pmail "txt like '%libmysql.dll%' and sbj like '%delphi%'"
  192.   NOTE: the txt field is NOT case sensitive!
  193. EOF
  194.   exit(0);
  195. }
  196.