Index: mailhandler.drush.inc
===================================================================
RCS file: mailhandler.drush.inc
diff -N mailhandler.drush.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ mailhandler.drush.inc	13 Oct 2009 20:28:04 -0000
@@ -0,0 +1,91 @@
+<?php
+
+/**
+ * Implementation of hook_drush_command().
+ *
+ * @return
+ *   An associative array of commands.
+ */
+function mailhandler_drush_command() {
+  $items = array();
+
+  $items['mailhandler retrieve'] = array(
+    'callback' => 'mailhandler_drush_retrieve',
+    'description' => "Retrieve mailhandler mail(s)",
+    'examples' => array(
+      'drush mailhandler list' => 'List available mailboxes for retrieval.',
+    ),
+  );
+
+  $items['mailhandler list'] = array(
+    'callback' => 'mailhandler_drush_list',
+    'description' => "List mailhandler mailboxes",
+    'examples' => array(
+      'drush mailhandler retrieve example@example.org' => 'Retrieve mail for the mailbox example@example.org.',
+      'drush mailhandler retrieve' => 'Retrieve mail for all mailboxes configured to run automatically.',
+    ),
+  );
+
+  return $items;
+}
+
+/**
+ * Retrieve one or all mailhandler mails.
+ *
+ * This function is the callback for the mailhandler retrieve command.
+ * It retrieves mails for a mailbox or all mailboxes configured to
+ * execute during cron runs.
+ * 
+ * @param
+ *   A string with the email address to retrieve. If omitted, will 
+ *   retrieve all mailboxes enabled for cron retrieval.
+ */
+function mailhandler_drush_retrieve($mail=NULL) {
+  // Include mailhandler retrieval functions
+  module_load_include('inc', 'mailhandler', 'mailhandler.retrieve');
+
+  if ( is_null($mail) ) {
+    $result = db_query('SELECT * FROM {mailhandler} WHERE enabled = 1 ORDER BY mail');
+    while ($mailbox = db_fetch_array($result)) {
+      drush_print(dt("Retrieving mail for !mail",array('!mail'=>$mailbox['mail'])));
+      mailhandler_cron_retrieve($mailbox);
+      $success = TRUE ;
+    }
+  }
+  else {
+    $result = db_query("SELECT * FROM {mailhandler} WHERE mail LIKE '%s'", $mail);
+    while ($mailbox = db_fetch_array($result)) {
+      drush_print(dt("Retrieving mail for !mail",array('!mail'=>$mail)));
+      mailhandler_cron_retrieve($mailbox);
+      $success = TRUE ;
+    }
+  }
+
+  if (!$success) {
+    if ( is_null($mail) ) {
+      drush_set_error('DRUSH_MAILHANDLER_MAILBOX_NONE', dt("No mailhandler mailboxes found."));
+    }
+    else {
+      drush_set_error('DRUSH_MAILHANDLER_MAILBOX_UNKNOWN', dt("Mailhandler mailbox !mail not found.", array('!mail'=>$mail)));
+    }  
+  }
+}
+
+/**
+ * List mailboxes available to retrieve.
+ */
+function mailhandler_drush_list() {
+  $result = db_query("SELECT mail FROM {mailhandler} ORDER BY mail");
+  $msg = dt("Available mailboxes: ");
+  while ($mail = db_fetch_array($result)) {
+    $mailboxes[] = $mail['mail'] ;
+    $success = TRUE ;
+  }
+  
+  if ( $success ) {
+    drush_print(dt("Available mailhandler mailboxes: !mailboxes", array('!mailboxes'=>implode(', ',$mailboxes))));
+  }
+  else {
+    drush_set_error('DRUSH_MAILHANDLER_NONE', dt("No mailhandler mailboxes found.")) ;
+  }
+}
