Index: mailhandler.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mailhandler/Attic/mailhandler.module,v
retrieving revision 1.87.2.13
diff -u -p -r1.87.2.13 mailhandler.module
--- mailhandler.module	15 Jul 2008 14:25:07 -0000	1.87.2.13
+++ mailhandler.module	17 Jul 2008 13:36:35 -0000
@@ -259,6 +259,12 @@ function mailhandler_process_message($he
     $body = trim($mailbox['commands']) ."\n". $body;
   }

+  // We set the type now, because we need it in the next block
+  if (!$node->type) $node->type = 'blog';
+
+  // Reset $node->taxonomy
+  $node->taxonomy = array();
+
   // process the commands and the body
   $lines = explode("\n", $body);
   for ($i = 0; $i < count($lines); $i++) {
@@ -279,6 +285,28 @@ function mailhandler_process_message($he
       // if needed, map term names into IDs. this should move to taxonomy_mailhandler()
       if ($data[0] == 'taxonomy' && !is_numeric($data[1][0])) {
         array_walk($data[1], 'mailhandler_term_map');
+        $node->taxonomy = array_merge($node->taxonomy, $data[1]);
+        unset($data[0]);
+      }
+      else if (substr($data[0], 0, 9) == 'taxonomy[' && substr($data[0], -1, 1) == ']'){
+        // make sure a valid vid is passed in:
+        $vid = substr($data[0], 9, -1);
+        $vocabulary = taxonomy_get_vocabulary($vid);
+        // if the vocabulary is not activated for that node type, unset $data[0], so the command will be ommited from $node
+        // TODO: add an error message
+        if (!in_array($node->type, $vocabulary->nodes)) {
+          unset($data[0]);
+        }
+        else if (!$vocabulary->tags) {
+          array_walk($data[1], 'mailhandler_term_map');
+          $node->taxonomy = array_merge($node->taxonomy, $data[1]);
+          unset($data[0]);
+        }
+        else if ($vocabulary->tags) {
+          // for freetagging vocabularies, we just pass the list of terms
+          $node->taxonomy['tags'][$vid] = implode(',', $data[1]);
+          unset($data[0]); // unset, so it won't be included when populating the node object
+        }
       }
       if (!empty($data[0])) {
         $node->$data[0] = $data[1];
@@ -301,7 +329,7 @@ function mailhandler_process_message($he
   $node->body = implode("\n", $tmp);

   if (!$node->teaser) $node->teaser = node_teaser($node->body);
-  if (!$node->type) $node->type = 'blog';
+
   // decode encoded subject line
   $subjectarr = imap_mime_header_decode($header->subject);
   for ($i = 0; $i < count($subjectarr); $i++) {
@@ -767,7 +795,7 @@ function mailhandler_form($edit = array(
   $form['security'] = array('#type' => 'radios', '#title' => t('Security'), '#options' => array(t('Disabled'), t('Require password')), '#default_value' => $edit['security'], '#description' => t('Disable security if your site does not require a password in the Commands section of incoming e-mails. Note: Security=Enabled and Mime preference=HTML is an unsupported combination.'));
   $form['replies'] = array('#type' => 'radios', '#title' => t('Send error replies'), '#options' => array(t('Disabled'), t('Enabled')), '#default_value' => $edit['replies'], '#description' => t('Send helpful replies to all unsuccessful e-mail submissions. Consider disabling when a listserv posts to this mailbox.'));
   $form['fromheader'] = array('#type' => 'textfield', '#title' => t('From header'), '#default_value' => $edit['fromheader'], '#description' => t('Use this e-mail header to determine the author of the resulting node. Admins usually leave this field blank (thus using the <strong>From</strong> header), but <strong>Sender</strong> is also useful when working with listservs.'));
-  $form['commands'] = array('#type' => 'textarea', '#title' => t('Default commands'), '#default_value' => $edit['commands'], '#description' => t('A set of commands which are added to each message. One command per line. See %link.', array('%link' => l(t('Commands'), 'admin/help/mailhandler/#commands'))));
+  $form['commands'] = array('#type' => 'textarea', '#title' => t('Default commands'), '#default_value' => $edit['commands'], '#description' => t('A set of commands which are added to each message. One command per line. See !link.', array('!link' => l(t('Commands'), 'admin/help/mailhandler#commands'))));
   $form['sigseparator'] = array('#type' => 'textfield', '#title' => t('Signature separator'), '#default_value' => $edit['sigseparator'], '#description' => t('All text after this string will be discarded. A typical value is <strong>"-- "</strong> that is two dashes followed by a blank in an otherwise empty line. Leave blank to include signature text in nodes.'));
   $form['delete_after_read'] = array('#type' => 'checkbox', '#title' => t('Delete messages after they are processed?'), '#default_value' => $edit['delete_after_read'], '#description' => t('Uncheck this box to leave read messages in the mailbox. They will not be processed again unless they become marked as unread.'));
   $form['enabled'] = array('#type' => 'radios', '#title' => t('Cron processing'), '#options' => array(t('Disabled'), t('Enabled')), '#default_value' => $edit['enabled'], '#description' => t('Select disable to temporarily stop cron processing for this mailbox.'));
@@ -859,6 +887,21 @@ function mailhandler_help($section = 'ad
   $output = '';
   $link->add = l(t('Add mailbox'), 'admin/content/mailhandler/add');

+  // Gather examples of useful commands, and build a definition list with them:
+  $commands[] = array('command' => 'taxonomy: [term1, term2]',
+                      'description' => t('Use this to add the terms <em>term1</em> and <em>term2</em> to the node.<br />
+                      Both of the terms should already exist. In case they do not exist already, they will be quietly ommitted'));
+  $commands[] = array('command' => 'taxonomy[v]: [term1, term2]',
+                      'description' => t('Similar to the above: adds the terms <em>term1</em> and <em>term2</em> to the node, but uses the vocabulary with the vocabulary id <em>v</em>. For example <em>taxonomy[3]</em> will chose only terms from the vocabulary which id is 3.<br />
+                      In case some of the terms do not exist already, the behavior will depend on whether the vocabulary is a free tagging vocabulary or not. If it is a free tagging vocabulary, the term will be added, otherwise, it will be quietly ommitted'));
+
+  $commands_list = '<dl>';
+  foreach ($commands as $command) {
+    $commands_list .= '<dt>'. $command['command'] .'</dt>';
+    $commands_list .= '<dl>'. $command['description'] .'</dl>';
+  }
+  $commands_list .= '</dl>';
+
   switch ($section) {
     case 'admin/help#mailhandler':
       $output = '<p>'. t('The mailhandler module allows registered users to create or edit nodes and comments via e-mail. Users may post taxonomy terms, teasers, and other post attributes using the mail commands capability.  This module is useful because e-mail is the preferred method of communication by community members.') .'</p>';
@@ -871,6 +914,8 @@ function mailhandler_help($section = 'ad
 <li>administer mailhandler at <a href="@admin-mailhandler">administer &gt;&gt; mailhandler</a>.</li>
 <li>set default commands, (password, type, taxonomy, promote, status), for how to work with incoming mail at <a href="%admin-mailhandler">admin >> mailhandler</a> select <strong>edit</strong> for the email address being handled.  Set commands in the default command field.</li>
 <li>post email, such as from a mailing list, to a forum by adding the term id (number found in the URL) to the default commands using <strong>tid: #</strong>.', array('@admin-mailhandler-add' => url('admin/content/mailhandler/add'), '@admin-mailhandler' => url('admin/content/mailhandler'))) .'</ul>';
+      $output .= '<h3 id="commands">'. t('Useful Commands') .'</h3>';
+      $output .= $commands_list;
       $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%mailhandler">Mailhandler page</a>.', array('%mailhandler' => 'http://www.drupal.org/handbook/modules/mailhandler/')) .'</p>';
       return $output;
     case 'admin/content/mailhandler':
