@Override
 protected Long compute() {
   long count = 0L;
   List<RecursiveTask<Long>> forks = new LinkedList<>();
   for (Folder subFolder : folder.getSubFolders()) {
     FolderSearchTask task = new FolderSearchTask(subFolder, searchedWord);
     forks.add(task);
     task.fork();
   }
   for (Document document : folder.getDocuments()) {
     DocumentSearchTask task = new DocumentSearchTask(document, searchedWord);
     forks.add(task);
     task.fork();
   }
   for (RecursiveTask<Long> task : forks) {
     count = count + task.join();
   }
   return count;
 }
Example #2
0
  public void setFolder(Folder newFolder) throws MessagingException {
    if (folder != null) {
      try {
        folder.removeMessageCountListener(messageCountListener);
        folder.close(true);
      } catch (Exception ex) {
        // Failure here seems to happen relatively often.
        // enp has a corrupt folder, for example, and enh finds that Exchange seems to sometimes
        // close folders on him, maybe because he's running Outlook at the same time.
        // It's not currently obvious that there's much to be gained by showing this error to the
        // user.
        // It does seem that we should blunder on, rather than back out, because the UI's already
        // been updated by this point.
        // If we back out here, we really need to back out the UI change too, and going that route
        // seems like a mistake.
        ex.printStackTrace();
      }
    }

    this.folder = newFolder;
    this.messages = Collections.<Message>emptyList();

    if (folder != null) {
      scanWholeFolder();
      folder.addMessageChangedListener(messageChangedListener);
      folder.addMessageCountListener(messageCountListener);
    }
  }
 public MessagesWrapper search(List<SearchTerm> term) throws MessagingException {
   if (!folder.isOpen()) {
     folder.open(Folder.READ_ONLY);
   }
   Message[] messages = folder.search(and(term));
   return new MessagesWrapper(logger, Arrays.asList(messages), folder);
 }
Example #4
0
 private void checkCurrentFolder(Folder folder) {
   Preconditions.checkState(
       folder.equals(currentFolder),
       "You must have opened folder %s"
           + " before attempting to read from it (%s is currently open).",
       folder.getName(),
       (currentFolder == null ? "No folder" : currentFolder.getName()));
 }
  public AbstractMacroBaseUnitTestCase() {
    // log4j:
    PropertyConfigurator.configure(
        AbstractMacroBaseUnitTestCase.class.getClassLoader().getResource("log4j.properties"));

    // CMIS Repository
    Map<String, ConfluenceCMISRepository> repoConfigs =
        new WeakHashMap<String, ConfluenceCMISRepository>();
    ConfluenceCMISRepository repoConfig =
        new ConfluenceCMISRepository(
            TEST_SERVER_NAME, TEST_REALM, TEST_USERNAME, TEST_PASSWORD, null);

    repoConfigs.put(TEST_SERVER_NAME, repoConfig);

    bandanaManager = mock(BandanaManager.class);
    when(bandanaManager.getValue((BandanaContext) anyObject(), anyString()))
        .thenReturn(repoConfigs);

    // CMIS
    ObjectId documentObjectId = mock(ObjectId.class);
    when(documentObjectId.getId()).thenReturn(TEST_DOCUMENT_ID);

    Document documentObject = getTestDocument();

    List<CmisObject> documentList = new ArrayList<CmisObject>();
    documentList.add(documentObject);

    ObjectId folderObjectId = mock(ObjectId.class);
    when(folderObjectId.getId()).thenReturn(TEST_FOLDER_ID);

    ItemIterable<CmisObject> children = mock(ItemIterable.class);
    when(children.getTotalNumItems()).thenReturn(1l);
    when(children.getHasMoreItems()).thenReturn(true).thenReturn(false);
    when(children.iterator()).thenReturn(documentList.iterator());

    Folder folderObject =
        createMockedCmisObject(
            new String[][] {
              {PropertyIds.NAME, "Name", "A folder"},
              {PropertyIds.BASE_TYPE_ID, "Base Type Id", "cmis:folder"},
              {PropertyIds.OBJECT_TYPE_ID, "Object Type Id", "cmis:folder"},
              {PropertyIds.OBJECT_ID, "Object Type Id", TEST_FOLDER_ID}
            },
            Folder.class);
    when(folderObject.getId()).thenReturn(TEST_FOLDER_ID);
    when(folderObject.getChildren()).thenReturn(children);

    Session session = mock(Session.class);

    when(session.createObjectId(TEST_DOCUMENT_ID)).thenReturn(documentObjectId);
    when(session.createObjectId(TEST_FOLDER_ID)).thenReturn(folderObjectId);
    when(session.getObject(documentObjectId)).thenReturn(documentObject);
    when(session.getObject(folderObjectId)).thenReturn(folderObject);

    confluenceCMISRepository = mock(ConfluenceCMISRepository.class);
    when(confluenceCMISRepository.getSession()).thenReturn(session);
  }
 Long countOccurrencesOnSingleThread(Folder folder, String searchedWord) {
   long count = 0;
   for (Folder subFolder : folder.getSubFolders()) {
     count = count + countOccurrencesOnSingleThread(subFolder, searchedWord);
   }
   for (Document document : folder.getDocuments()) {
     count = count + occurrencesCount(document, searchedWord);
   }
   return count;
 }
Example #7
0
  public int handleFirstTime(
      Account account,
      FolderHelper folderHelper,
      int folderDepth,
      String messageStoreBucket,
      Set<String> storedMessageIds,
      String context,
      StopWatch watch)
      throws Exception {
    int newMessages = 0;
    Date lastMessageReceivedDate = null;
    log.info(String.format("handling first mailcheck for %s", context));

    Folder folder;
    while ((folder = folderHelper.next()) != null) {
      String folderContext = context + " ,folderName=" + folder.getFullName();
      IMAPFolder imapFolder = (IMAPFolder) folder;

      Collection<Integer> messages = getFirstTimeMessages(imapFolder, folderContext);

      if (messages.size() > 0) {
        storedMessageIds = new HashSet<String>();

        for (Integer messageNumber : messages) {
          Message message = imapFolder.getMessage(messageNumber);

          if (lastMessageReceivedDate == null
              || lastMessageReceivedDate.before(message.getReceivedDate())) {
            lastMessageReceivedDate = message.getReceivedDate();
          }

          String messageId = getId(account, imapFolder, message);

          if (processMessage(
              account,
              message,
              messageNumber,
              messageId,
              imapFolder,
              storedMessageIds,
              messageStoreBucket,
              folderContext,
              watch,
              folderHelper)) {
            newMessages++;
          }
        }
      }
    }
    updateAccount(account, null, 0, folderDepth, lastMessageReceivedDate);
    return newMessages;
  }
Example #8
0
  protected boolean areMessagesInReverseOrder(Folder inbox) throws MessagingException {
    try {
      Message msgFirst = inbox.getMessage(1);
      Message msgLast = inbox.getMessage(inbox.getMessageCount());

      Date firstMessageDate = getDateToDetermineOrder(msgFirst);
      Date lastMessageDate = getDateToDetermineOrder(msgLast);
      return firstMessageDate != null
          && lastMessageDate != null
          && firstMessageDate.after(lastMessageDate);
    } catch (Exception e) {
      return false;
    }
  }
 public MessagesWrapper markAsRead(List<Message> messagez)
     throws IOException, MessagingException {
   if (folder.isOpen() && folder.getMode() != Folder.READ_WRITE) {
     folder.close(true);
   }
   if (!folder.isOpen()) {
     folder.open(Folder.READ_WRITE);
   }
   for (Message message : messagez) {
     message.setFlag(Flags.Flag.SEEN, true);
   }
   logger.info("Marked email(s) as read : " + messagez.size());
   return this;
 }
  public static void main(String[] args) throws IOException {
    WordCounter wordCounter = new WordCounter();
    Folder folder = Folder.fromDirectory(new File(args[0]));

    final int repeatCount = Integer.decode(args[2]);
    long counts;
    long startTime;
    long stopTime;

    long[] singleThreadTimes = new long[repeatCount];
    long[] forkedThreadTimes = new long[repeatCount];

    for (int i = 0; i < repeatCount; i++) {
      startTime = System.currentTimeMillis();
      counts = wordCounter.countOccurrencesOnSingleThread(folder, args[1]);
      stopTime = System.currentTimeMillis();
      singleThreadTimes[i] = (stopTime - startTime);
      System.out.println(counts + " , single thread search took " + singleThreadTimes[i] + "ms");
    }

    for (int i = 0; i < repeatCount; i++) {
      startTime = System.currentTimeMillis();
      counts = wordCounter.countOccurrencesInParallel(folder, args[1]);
      stopTime = System.currentTimeMillis();
      forkedThreadTimes[i] = (stopTime - startTime);
      System.out.println(counts + " , fork / join search took " + forkedThreadTimes[i] + "ms");
    }

    System.out.println("\nCSV Output:\n");
    System.out.println("Single thread,Fork/Join");
    for (int i = 0; i < repeatCount; i++) {
      System.out.println(singleThreadTimes[i] + "," + forkedThreadTimes[i]);
    }
    System.out.println();
  }
Example #11
0
  public void scanWholeFolder() throws MessagingException {
    if (folder.isOpen() == false) {
      folder.open(Folder.READ_WRITE);
    }

    // Bulk-fetch the message envelopes.
    Message[] newMessages = folder.getMessages();
    FetchProfile fetchProfile = new FetchProfile();
    // FIXME: add CONTENT_INFO if we start to display the size
    // fetchProfile.add(FetchProfile.Item.CONTENT_INFO);
    fetchProfile.add(FetchProfile.Item.ENVELOPE);
    fetchProfile.add(FetchProfile.Item.FLAGS);
    folder.fetch(newMessages, fetchProfile);

    this.messages = new ArrayList<Message>();
    messages.addAll(Arrays.asList(newMessages));

    fireTableDataChanged();
  }
  private Object receiveMail() throws Exception {
    Folder folder = null;
    Store store = null;
    ConfigurationPropertyManager configManager =
        ConfigurationPropertyManager.getConfigurationPropertyManager();
    final String email_address = configManager.getPropValues("gmail_address");
    final String password = configManager.getPropValues("gmail_pw");

    try {
      Properties props = System.getProperties();
      props.setProperty("mail.store.protocol", "imaps");

      // Session session = Session.getDefaultInstance(props, null);
      Session session = Session.getInstance(props, new GMailAuthenticator(email_address, password));
      store = session.getStore("imaps");
      store.connect("imap.gmail.com", email_address, password);
      folder = store.getFolder("Inbox");
      folder.open(Folder.READ_WRITE);
      // Message messages[] = folder.getMessages();
      // search for all "unseen" messages
      Flags seen = new Flags(Flags.Flag.SEEN);
      FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
      Message messages[] = folder.search(unseenFlagTerm);

      for (int i = 0; i < messages.length; i++) {
        folder.getMessage(
            messages[i].getMessageNumber()); // this will mark the retrieved messages as READ	    	
      }
      String s = updateEmailrepository(messages);
      // System.out.println(s);
      StringTerm st = new StringTermImpl(s);
      return st;

    } catch (Exception e) {
      System.out.println(e.toString());
    } finally {
      if (folder != null) {
        folder.close(true);
      }
    }
    return null;
  }
  public static void main(String[] args) throws Throwable {
    String mailbox = null;
    boolean DEBUG = false;

    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-debug")) {
        DEBUG = true;

      } else if (mailbox == null) {
        mailbox = args[i];

      } else {
        usage("Spurious command line: " + args);
      }
    }

    if (mailbox == null) {
      usage("Missing mailbox");
    }

    // Get a connection to the mail server
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(DEBUG);

    Provider[] providers = session.getProviders();
    for (int i = 0; i < providers.length; i++) {
      Provider provider = providers[i];
      out.println(provider);
    }

    Store store = session.getStore("imap");
    Folder folder = store.getFolder(mailbox);
    Message[] messages = folder.getMessages();
    out.println("Folder " + folder.getName() + " with " + messages.length + " messages");
    for (int i = 0; i < messages.length; i++) {
      Message message = messages[i];
      out.println("  " + i + ": " + message.getSubject());
    }

    out.println("");
  }
 static Folder fromDirectory(File dir) throws IOException {
   List<Document> documents = new LinkedList<>();
   List<Folder> subFolders = new LinkedList<>();
   for (File entry : dir.listFiles()) {
     if (entry.isDirectory()) {
       subFolders.add(Folder.fromDirectory(entry));
     } else {
       documents.add(Document.fromFile(entry));
     }
   }
   return new Folder(subFolders, documents);
 }
Example #15
0
  public Collection<Integer> getFirstTimeMessages(Folder inbox, String context) throws Exception {
    log.debug(String.format("handling first mailcheck for %s", context));

    Collection<Integer> messages = new ArrayList<Integer>();

    // we need to get 5 most recent messages in this case
    if (inbox.getMessageCount() > 0) {
      if (areMessagesInReverseOrder(inbox)) {
        log.debug(String.format("order is reverse chronological for %s", context));
        for (int i = 0; i < 5; i++) {
          messages.add(i + 1);
        }
      } else {
        log.debug(String.format("order is chronological for %s", context));
        for (int messageNumber = inbox.getMessageCount();
            messageNumber > 0 && messageNumber > inbox.getMessageCount() - 5;
            messageNumber--) {
          messages.add(messageNumber);
        }
      }
    }

    return messages;
  }
Example #16
0
 public int compare(Folder o1, Folder o2) {
   return o1.getName().compareToIgnoreCase(o2.getName());
 }
  public static void ClaimsMsgShow(
      java.lang.String argvs[],
      Folder folder,
      Store store,
      biz.systempartners.claims.ClaimsViewer claimsViewer) {
    if (claimsViewer == null) {}

    int msgnum = -1;
    int optind = 0;
    InputStream msgStream = System.in;

    if (argvs != null) {

      for (optind = 0; optind < argvs.length; optind++) {
        if (argvs[optind].equals("-T")) {
          protocol = argvs[++optind];
        } else if (argvs[optind].equals("-H")) {
          host = argvs[++optind];
        } else if (argvs[optind].equals("-U")) {
          user = argvs[++optind];
        } else if (argvs[optind].equals("-P")) {
          password = argvs[++optind];
        } else if (argvs[optind].equals("-v")) {
          verbose = true;
        } else if (argvs[optind].equals("-D")) {
          debug = true;
        } else if (argvs[optind].equals("-f")) {
          mbox = argvs[++optind];
        } else if (argvs[optind].equals("-L")) {
          url = argvs[++optind];
        } else if (argvs[optind].equals("-p")) {
          port = Integer.parseInt(argvs[++optind]);
        } else if (argvs[optind].equals("-s")) {
          showStructure = true;
        } else if (argvs[optind].equals("-S")) {
          saveAttachments = true;
        } else if (argvs[optind].equals("-m")) {
          showMessage = true;
        } else if (argvs[optind].equals("-a")) {
          showAlert = true;
        } else if (argvs[optind].equals("--")) {
          optind++;
          break;
        } else if (argvs[optind].startsWith("-")) {
          System.out.println("Usage: msgshow [-L url] [-T protocol] [-H host] [-p port] [-U user]");
          System.out.println("\t[-P password] [-f mailbox] [msgnum] [-v] [-D] [-s] [-S] [-a]");
          System.out.println("or     msgshow -m [-v] [-D] [-s] [-S] < msg");
          System.exit(1);
        } else {
          break;
        }
      }
    }

    try {
      if (optind < argvs.length) msgnum = Integer.parseInt(argvs[optind]);
      //            msgnum = 1;

      // Get a Properties object
      Properties props = System.getProperties();

      // Get a Session object
      Session session = Session.getInstance(props, null);
      session.setDebug(debug);

      if (showMessage) {
        MimeMessage msg = new MimeMessage(session, msgStream);
        dumpPart(msg, claimsViewer);
        //	System.exit(0);
      }
      /*
                 // Get a Store object
                 Store store = null;
                 if (url != null) {
                     URLName urln = new URLName(url);
                     store = session.getStore(urln);
                     if (showAlert) {
                         store.addStoreListener(new StoreListener() {
                             public void notification(StoreEvent e) {
                                 String s;
                                 if (e.getMessageType() == StoreEvent.ALERT)
                                     s = "ALERT: ";
                                 else
                                     s = "NOTICE: ";
                                 System.out.println(s + e.getMessage());
                             }
                         });
                     }
                     store.connect();
                 } else {
                     if (protocol != null)
                         store = session.getStore(protocol);
                     else
                         store = session.getStore();

                     // Connect
                     if (host != null || user != null || password != null)
                         store.connect(host, port, user, password);
                     else
                         store.connect();
                 }


                 // Open the Folder

                 Folder folder = store.getDefaultFolder();
                 if (folder == null) {
                     System.out.println("No default folder");
                     System.exit(1);
                 }
      */
      //	    folder = folder.getFolder(mbox);
      if (folder == null) {
        System.out.println("Invalid folder");
        System.exit(1);
      }

      // try to open read/write and if that fails try read-only
      try {
        folder.open(Folder.READ_WRITE);
      } catch (MessagingException ex) {
        folder.open(Folder.READ_ONLY);
      }
      int totalMessages = folder.getMessageCount();

      if (totalMessages == 0) {
        System.out.println("Empty folder");
        folder.close(false);
        store.close();
        System.exit(1);
      }

      if (verbose) {
        int newMessages = folder.getNewMessageCount();
        System.out.println("Total messages = " + totalMessages);
        System.out.println("New messages = " + newMessages);
        System.out.println("-------------------------------");
      }

      if (msgnum == -1) {
        // Attributes & Flags for all messages ..
        Message[] msgs = folder.getMessages();

        // Use a suitable FetchProfile
        FetchProfile fp = new FetchProfile();
        fp.add(FetchProfile.Item.ENVELOPE);
        fp.add(FetchProfile.Item.FLAGS);
        fp.add("X-Mailer");
        folder.fetch(msgs, fp);

        for (int i = 0; i < msgs.length; i++) {
          System.out.println("--------------------------");
          System.out.println("MESSAGE #" + (i + 1) + ":");
          dumpEnvelope(msgs[i]);
          // dumpPart(msgs[i]);
        }
      } else {
        Message[] msgs = folder.getMessages();
        for (int n = 0; n < msgs.length; n++) {
          System.out.println("Getting message number: " + n + 1);
          Message m = null;

          try {
            m = folder.getMessage(msgnum + 1);
            // m.setDisposition(
            dumpPart(m, claimsViewer);
            //        m.setExpunged(true);
          } catch (IndexOutOfBoundsException iex) {
            System.out.println("Message number out of range");
          }
        }
        folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true);
      }
      //            folder.expunge();
      folder.close(true);

      store.close();
    } catch (Exception ex) {
      System.out.println("Oops, got exception! " + ex.getMessage());
      ex.printStackTrace();
      //	    System.exit(1);
    }
    //	System.exit(0);
  }
 public void close() throws MessagingException {
   folder.close(true);
 }
Example #19
0
 /** Process all events for keys queued to the watcher */
 private static void forwardToItopic(WatchEvent.Kind<Path> kind, Path dir) {
   boolean isDir = Files.isDirectory(dir);
   if (kind == ENTRY_CREATE) {
     if (!isDir) {
       Action act = new Action("add_file", Folder.getInternalPath(dir));
       // Folder.getFileFromDiskToWinSafe(act.getPath());
       Folder.loadFileFromFSToInternal(dir);
       topic.publish(act);
     } else {
       if (Folder.isEmptyFSFolder(dir)) {
         Folder.createEmptyFolderInInternal(dir);
         topic.publish(new Action("create_empty_folder", Folder.getInternalPath(dir)));
       } else {
         Folder.loadFolderFromFSToInternal(dir);
         topic.publish(new Action("create_folder", Folder.getInternalPath(dir)));
       }
     }
   } else if (kind == ENTRY_DELETE) {
     // todo
     Folder.deleteFromInternal(dir);
     topic.publish(new Action("delete_entry", Folder.getInternalPath(dir)));
   } else if (kind == ENTRY_MODIFY) {
     // todo
     if (!isDir) {
       Folder.loadFileFromFSToInternal(dir);
       topic.publish(new Action("edit_file", Folder.getInternalPath(dir)));
     } else {
       if (Folder.isEmptyFSFolder(dir)) {
         Folder.createEmptyFolderInInternal(dir);
         topic.publish(new Action("create_empty_folder", Folder.getInternalPath(dir)));
       } else {
         Folder.loadFolderFromFSToInternal(dir);
         topic.publish(new Action("edit_folder", Folder.getInternalPath(dir)));
       }
     }
   } else {
     // TODO
     System.out.println("[forwardToItopic] Unexpected Event - kind=" + kind + "dir=" + dir);
   }
 }
Example #20
0
  // void processEvents() {
  public void run() {
    System.out.println("WatchDir Thread INFO: priority=" + Thread.currentThread().getPriority());
    for (; ; ) {
      // wait for key to be signalled
      System.out.println("WatchDir INFO: restarting loop...acquiring new key");
      WatchKey key;
      try {
        key = watcher.take();
      } catch (InterruptedException x) {
        return;
      }

      Path dir = keys.get(key);
      if (dir == null) {
        System.err.println("WatchKey not recognized!!");
        continue;
      }

      for (WatchEvent<?> event : key.pollEvents()) {
        WatchEvent.Kind kind = event.kind();
        // TBD - provide example of how OVERFLOW event is handled
        if (kind == OVERFLOW) {
          System.out.println("Encountered OVERFLOW Event - " + event);
          continue;
        }

        // Context for directory entry event is the file name of entry
        WatchEvent<Path> ev = cast(event);
        Path name = ev.context();
        Path child = dir.resolve(name);

        // print out event
        System.out.format("[WatchDir] %s: %s\n", event.kind().name(), child);

        // if directory is created, and watching recursively, then
        // register it and its sub-directories
        if (recursive && (kind == ENTRY_CREATE)) {
          try {
            if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
              registerAll(child);
            }
          } catch (IOException x) {
            // ignore to keep sample readbale
          }
        }

        long t = System.currentTimeMillis();
        if (!Folder.dontWatch.contains(Folder.getInternalPath(child))) {
          Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
          System.out.println(
              "WatchDir#"
                  + key
                  + " INFO: path="
                  + child
                  + ", internal="
                  + Folder.getInternalPath(child)
                  + " is NOT in don't watch list. Forwarding it to other peers. @"
                  + Main.timeToString(t)); // DEBUG
          forwardToItopic(kind, child);
        } else {
          Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
          System.out.println(
              "WatchDir#"
                  + key
                  + " INFO: path="
                  + child
                  + ", internal="
                  + Folder.getInternalPath(child)
                  + " IS in the don't watch list. NOT forwarding. @"
                  + Main.timeToString(t)); // DEBUG
          // try{
          //     Thread.sleep(minDelayBtwnWatchEvents);
          // } catch(InterruptedException ex) {
          //     System.err.println("Exception:"+ex+" while trying to sleep WatchDir thread");
          //     ex.printStackTrace();
          // }
        }
      }

      // reset key and remove from set if directory no longer accessible
      boolean valid = key.reset();
      if (!valid) {
        keys.remove(key);

        // all directories are inaccessible
        if (keys.isEmpty()) {
          break;
        }
      }
    }
  }
Example #21
0
  public static void main(String argv[]) {
    int optind;

    String subject = null;
    String from = null;
    boolean or = false;
    boolean today = false;

    for (optind = 0; optind < argv.length; optind++) {
      if (argv[optind].equals("-T")) {
        protocol = argv[++optind];
      } else if (argv[optind].equals("-H")) {
        host = argv[++optind];
      } else if (argv[optind].equals("-U")) {
        user = argv[++optind];
      } else if (argv[optind].equals("-P")) {
        password = argv[++optind];
      } else if (argv[optind].equals("-or")) {
        or = true;
      } else if (argv[optind].equals("-D")) {
        debug = true;
      } else if (argv[optind].equals("-f")) {
        mbox = argv[++optind];
      } else if (argv[optind].equals("-L")) {
        url = argv[++optind];
      } else if (argv[optind].equals("-subject")) {
        subject = argv[++optind];
      } else if (argv[optind].equals("-from")) {
        from = argv[++optind];
      } else if (argv[optind].equals("-today")) {
        today = true;
      } else if (argv[optind].equals("--")) {
        optind++;
        break;
      } else if (argv[optind].startsWith("-")) {
        System.out.println(
            "Usage: search [-D] [-L url] [-T protocol] [-H host] "
                + "[-U user] [-P password] [-f mailbox] "
                + "[-subject subject] [-from from] [-or] [-today]");
        System.exit(1);
      } else {
        break;
      }
    }

    try {

      if ((subject == null) && (from == null) && !today) {
        System.out.println("Specify either -subject, -from or -today");
        System.exit(1);
      }

      // Get a Properties object
      Properties props = System.getProperties();

      // Get a Session object
      Session session = Session.getDefaultInstance(props, null);
      session.setDebug(debug);

      // Get a Store object
      Store store = null;
      if (url != null) {
        URLName urln = new URLName(url);
        store = session.getStore(urln);
        store.connect();
      } else {
        if (protocol != null) store = session.getStore(protocol);
        else store = session.getStore();

        // Connect
        if (host != null || user != null || password != null) store.connect(host, user, password);
        else store.connect();
      }

      // Open the Folder

      Folder folder = store.getDefaultFolder();
      if (folder == null) {
        System.out.println("Cant find default namespace");
        System.exit(1);
      }

      folder = folder.getFolder(mbox);
      if (folder == null) {
        System.out.println("Invalid folder");
        System.exit(1);
      }

      folder.open(Folder.READ_ONLY);
      SearchTerm term = null;

      if (subject != null) term = new SubjectTerm(subject);
      if (from != null) {
        FromStringTerm fromTerm = new FromStringTerm(from);
        if (term != null) {
          if (or) term = new OrTerm(term, fromTerm);
          else term = new AndTerm(term, fromTerm);
        } else term = fromTerm;
      }
      if (today) {
        ReceivedDateTerm dateTerm = new ReceivedDateTerm(ComparisonTerm.EQ, new Date());
        if (term != null) {
          if (or) term = new OrTerm(term, dateTerm);
          else term = new AndTerm(term, dateTerm);
        } else term = dateTerm;
      }

      Message[] msgs = folder.search(term);
      System.out.println("FOUND " + msgs.length + " MESSAGES");
      if (msgs.length == 0) // no match
      System.exit(1);

      // Use a suitable FetchProfile
      FetchProfile fp = new FetchProfile();
      fp.add(FetchProfile.Item.ENVELOPE);
      folder.fetch(msgs, fp);

      for (int i = 0; i < msgs.length; i++) {
        System.out.println("--------------------------");
        System.out.println("MESSAGE #" + (i + 1) + ":");
        dumpPart(msgs[i]);
      }

      folder.close(false);
      store.close();
    } catch (Exception ex) {
      System.out.println("Oops, got exception! " + ex.getMessage());
      ex.printStackTrace();
    }

    System.exit(1);
  }