Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. import java.util.*;
  33. import java.io.*;
  34. import javax.mail.*;
  35. import javax.mail.internet.*;
  36. import javax.mail.search.*;
  37. import javax.activation.*;
  38.  
  39. /*
  40.  * Search the given folder for messages matching the given
  41.  * criteria.
  42.  *
  43.  * @author John Mani
  44.  */
  45.  
  46. public class search {
  47.  
  48.     static String protocol = "imap";
  49.     static String host = null;
  50.     static String user = null;
  51.     static String password = null;
  52.     static String mbox = "INBOX";
  53.     static String url = null;
  54.     static boolean debug = false;
  55.  
  56.     public static void main(String argv[]) {
  57.         int optind;
  58.  
  59.         String subject = null;
  60.         String from = null;
  61.         boolean or = false;
  62.         boolean today = false;
  63.  
  64.         for (optind = 0; optind < argv.length; optind++) {
  65.             if (argv[optind].equals("-T")) {
  66.                 protocol = argv[++optind];
  67.             } else if (argv[optind].equals("-H")) {
  68.                 host = argv[++optind];
  69.             } else if (argv[optind].equals("-U")) {
  70.                 user = argv[++optind];
  71.             } else if (argv[optind].equals("-P")) {
  72.                 password = argv[++optind];
  73.             } else if (argv[optind].equals("-or")) {
  74.                 or = true;
  75.             } else if (argv[optind].equals("-D")) {
  76.                 debug = true;
  77.             } else if (argv[optind].equals("-f")) {
  78.                 mbox = argv[++optind];
  79.             } else if (argv[optind].equals("-L")) {
  80.                 url = argv[++optind];
  81.             } else if (argv[optind].equals("-subject")) {
  82.                 subject = argv[++optind];
  83.             } else if (argv[optind].equals("-from")) {
  84.                 from = argv[++optind];
  85.             } else if (argv[optind].equals("-today")) {
  86.                 today = true;
  87.             } else if (argv[optind].equals("--")) {
  88.                 optind++;
  89.                 break;
  90.             } else if (argv[optind].startsWith("-")) {
  91.                 System.out.println(
  92.                  "Usage: search [-D] [-L url] [-T protocol] [-H host] " +
  93.                  "[-U user] [-P password] [-f mailbox] " +
  94.                  "[-subject subject] [-from from] [-or] [-today]");
  95.                 System.exit(1);
  96.             } else {
  97.                 break;
  98.             }
  99.         }
  100.  
  101.         try {
  102.  
  103.             if ((subject == null) && (from == null) && !today) {
  104.                 System.out.println("Specify either -subject, -from or -today");
  105.                 System.exit(1);
  106.             }
  107.  
  108.             // Get a Properties object
  109.             Properties props = System.getProperties();
  110.  
  111.             // Get a Session object
  112.             Session session = Session.getInstance(props, null);
  113.             session.setDebug(debug);
  114.  
  115.             // Get a Store object
  116.             Store store = null;
  117.             if (url != null) {
  118.                 URLName urln = new URLName(url);
  119.                 store = session.getStore(urln);
  120.                 store.connect();
  121.             } else {
  122.                 if (protocol != null)          
  123.                     store = session.getStore(protocol);
  124.                 else
  125.                     store = session.getStore();
  126.  
  127.                 // Connect
  128.                 if (host != null || user != null || password != null)
  129.                     store.connect(host, user, password);
  130.                 else
  131.                     store.connect();
  132.             }
  133.            
  134.  
  135.             // Open the Folder
  136.  
  137.             Folder folder = store.getDefaultFolder();
  138.             if (folder == null) {
  139.                 System.out.println("Cant find default namespace");
  140.                 System.exit(1);
  141.             }
  142.  
  143.             folder = folder.getFolder(mbox);
  144.             if (folder == null) {
  145.                 System.out.println("Invalid folder");
  146.                 System.exit(1);
  147.             }
  148.  
  149.             folder.open(Folder.READ_ONLY);
  150.             SearchTerm term = null;
  151.  
  152.             if (subject != null)
  153.                 term = new SubjectTerm(subject);
  154.             if (from != null) {
  155.                 FromStringTerm fromTerm = new FromStringTerm(from);
  156.                 if (term != null) {
  157.                     if (or)
  158.                         term = new OrTerm(term, fromTerm);
  159.                     else
  160.                         term = new AndTerm(term, fromTerm);
  161.                 }
  162.                 else
  163.                     term = fromTerm;
  164.             }
  165.             if (today) {
  166.                 Calendar c = Calendar.getInstance();
  167.                 c.set(Calendar.HOUR, 0);
  168.                 c.set(Calendar.MINUTE, 0);
  169.                 c.set(Calendar.SECOND, 0);
  170.                 c.set(Calendar.MILLISECOND, 0);
  171.                 c.set(Calendar.AM_PM, Calendar.AM);
  172.                 ReceivedDateTerm startDateTerm =
  173.                  new ReceivedDateTerm(ComparisonTerm.GE, c.getTime());
  174.                 c.add(Calendar.DATE, 1);        // next day
  175.                 ReceivedDateTerm endDateTerm =
  176.                  new ReceivedDateTerm(ComparisonTerm.LT, c.getTime());
  177.                 SearchTerm dateTerm = new AndTerm(startDateTerm, endDateTerm);
  178.                 if (term != null) {
  179.                     if (or)
  180.                         term = new OrTerm(term, dateTerm);
  181.                     else
  182.                         term = new AndTerm(term, dateTerm);
  183.                 }
  184.                 else
  185.                     term = dateTerm;
  186.             }
  187.  
  188.             Message[] msgs = folder.search(term);
  189.             System.out.println("FOUND " + msgs.length + " MESSAGES");
  190.             if (msgs.length == 0) // no match
  191.                 System.exit(1);
  192.  
  193.             // Use a suitable FetchProfile
  194.             FetchProfile fp = new FetchProfile();
  195.             fp.add(FetchProfile.Item.ENVELOPE);
  196.             folder.fetch(msgs, fp);
  197.  
  198.             for (int i = 0; i < msgs.length; i++) {
  199.                 System.out.println("--------------------------");
  200.                 System.out.println("MESSAGE #" + (i + 1) + ":");
  201.                 dumpPart(msgs[i]);
  202.             }
  203.  
  204.             folder.close(false);
  205.             store.close();
  206.         } catch (Exception ex) {
  207.             System.out.println("Oops, got exception! " + ex.getMessage());
  208.             ex.printStackTrace();
  209.         }
  210.  
  211.         System.exit(1);
  212.     }
  213.  
  214.     public static void dumpPart(Part p) throws Exception {
  215.         if (p instanceof Message) {
  216.             Message m = (Message)p;
  217.             Address[] a;
  218.             // FROM
  219.             if ((a = m.getFrom()) != null) {
  220.                 for (int j = 0; j < a.length; j++)
  221.                     System.out.println("FROM: " + a[j].toString());
  222.             }
  223.  
  224.             // TO
  225.             if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
  226.                 for (int j = 0; j < a.length; j++)
  227.                     System.out.println("TO: " + a[j].toString());
  228.             }
  229.  
  230.             // SUBJECT
  231.             System.out.println("SUBJECT: " + m.getSubject());
  232.  
  233.             // DATE
  234.             Date d = m.getSentDate();
  235.             System.out.println("SendDate: " +
  236.                 (d != null ? d.toLocaleString() : "UNKNOWN"));
  237.  
  238.             // FLAGS:
  239.             Flags flags = m.getFlags();
  240.             StringBuffer sb = new StringBuffer();
  241.             Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags
  242.  
  243.             boolean first = true;
  244.             for (int i = 0; i < sf.length; i++) {
  245.                 String s;
  246.                 Flags.Flag f = sf[i];
  247.                 if (f == Flags.Flag.ANSWERED)
  248.                     s = "\\Answered";
  249.                 else if (f == Flags.Flag.DELETED)
  250.                     s = "\\Deleted";
  251.                 else if (f == Flags.Flag.DRAFT)
  252.                     s = "\\Draft";
  253.                 else if (f == Flags.Flag.FLAGGED)
  254.                     s = "\\Flagged";
  255.                 else if (f == Flags.Flag.RECENT)
  256.                     s = "\\Recent";
  257.                 else if (f == Flags.Flag.SEEN)
  258.                     s = "\\Seen";
  259.                 else
  260.                     continue;   // skip it
  261.                 if (first)
  262.                     first = false;
  263.                 else
  264.                     sb.append(' ');
  265.                 sb.append(s);
  266.             }
  267.  
  268.             String[] uf = flags.getUserFlags(); // get the user flag strings
  269.             for (int i = 0; i < uf.length; i++) {
  270.                 if (first)
  271.                     first = false;
  272.                 else
  273.                     sb.append(' ');
  274.                 sb.append(uf[i]);
  275.             }
  276.             System.out.println("FLAGS = " + sb.toString());
  277.         }
  278.  
  279.         System.out.println("CONTENT-TYPE: " + p.getContentType());
  280.  
  281.         /* Dump input stream
  282.         InputStream is = ((MimeMessage)m).getInputStream();
  283.         int c;
  284.         while ((c = is.read()) != -1)
  285.             System.out.write(c);
  286.         */
  287.  
  288.         Object o = p.getContent();
  289.         if (o instanceof String) {
  290.             System.out.println("This is a String");
  291.             System.out.println((String)o);
  292.         } else if (o instanceof Multipart) {
  293.             System.out.println("This is a Multipart");
  294.             Multipart mp = (Multipart)o;
  295.             int count = mp.getCount();
  296.             for (int i = 0; i < count; i++)
  297.                 dumpPart(mp.getBodyPart(i));
  298.         } else if (o instanceof InputStream) {
  299.             System.out.println("This is just an input stream");
  300.             InputStream is = (InputStream)o;
  301.             int c;
  302.             while ((c = is.read()) != -1)
  303.                 System.out.write(c);
  304.         }
  305.     }
  306. }
  307.