/[drupal]/contributions/modules/mailhandler/mailhandler.module
ViewVC logotype

Diff of /contributions/modules/mailhandler/mailhandler.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.87, Thu Feb 1 05:34:50 2007 UTC revision 1.87.2.18, Thu Dec 25 13:01:20 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: mailhandler.module,v 1.86 2006/08/08 23:01:24 weitzman Exp $  // $Id: mailhandler.module,v 1.87.2.17 2008/09/17 16:29:36 heine Exp $
3    
4  /**  /**
5   * Retrieve all msgs from a given mailbox and process them.   * Retrieve all msgs from a given mailbox and process them.
# Line 32  function mailhandler_retrieve($mailbox) Line 32  function mailhandler_retrieve($mailbox)
32          continue;          continue;
33        }        }
34    
35        $mime = explode(',', variable_get('mime', 'TEXT/HTML,TEXT/PLAIN'));        $mime = explode(',', $mailbox['mime']);
36    
37          // Get the first text part - this will be the node body
38        $origbody = mailhandler_get_part($result, $i, $mime[0]);        $origbody = mailhandler_get_part($result, $i, $mime[0]);
39    
40          // If we didn't get a body from our first attempt, try the alternate format (HTML or PLAIN)
41        if (!$origbody) {        if (!$origbody) {
42          $origbody = mailhandler_get_part($result, $i, $mime[1]);          $origbody = mailhandler_get_part($result, $i, $mime[1]);
43          if (!$origbody) {        }
44            // @TODO: Log that we got an empty email?  
45            continue;        // Parse MIME parts, so all mailhandler modules have access to
46          }        // the full array of mime parts without having to process the email.
47          $mimeparts = mailhandler_get_parts($result, $i);
48    
49          // Is this an empty message with no body and no mimeparts?
50          if (!$origbody && !$mimeparts) {
51            // @TODO: Log that we got an empty email?
52            continue;
53        }        }
54    
55        $num_processed++;        $num_processed++;
# Line 50  function mailhandler_retrieve($mailbox) Line 60  function mailhandler_retrieve($mailbox)
60        // check if mail originates from an authenticated user        // check if mail originates from an authenticated user
61        $node = mailhandler_authenticate($node, $header, $origbody, $mailbox);        $node = mailhandler_authenticate($node, $header, $origbody, $mailbox);
62    
63          // Put $mimeparts on the node
64          $node->mimeparts = $mimeparts;
65    
66        // we need to change the current user        // we need to change the current user
67        // this has to be done here to allow modules        // this has to be done here to allow modules
68        // to create users        // to create users
# Line 87  function mailhandler_retrieve($mailbox) Line 100  function mailhandler_retrieve($mailbox)
100    }    }
101    else {    else {
102      if ($err) {      if ($err) {
103        watchdog('error', t('Mailhandler %c connection failed: %m', array('%c' => ($mailbox['imap'] ? 'imap' : 'POP3'), '%m' => $mailbox['mail'])));        watchdog('mailhandler', t('Mailhandler %c connection failed: %m', array('%c' => ($mailbox['imap'] ? 'imap' : 'POP3'), '%m' => $mailbox['mail'])), WATCHDOG_ERROR);
104        return t('Mailhandler %c connection failed: %m', array('%c' => ($mailbox['imap'] ? 'imap' : 'POP3'), '%m' => $mailbox['mail']));        return t('Mailhandler %c connection failed: %m', array('%c' => ($mailbox['imap'] ? 'imap' : 'POP3'), '%m' => $mailbox['mail']));
105      }      }
106      else {      else {
107        watchdog('error', t('Mailhandler: Could not access local folder: %m', array('%m' => $mailbox['mail'])));        watchdog('mailhandler', t('Mailhandler: Could not access local folder: %m', array('%m' => $mailbox['mail'])), WATCHDOG_ERROR);
108        return t('Mailhandler could not access local folder: %m', array('%m' => $mailbox['mail']));        return t('Mailhandler could not access local folder: %m', array('%m' => $mailbox['mail']));
109      }      }
110    }    }
# Line 114  function mailhandler_comment_submit($nod Line 127  function mailhandler_comment_submit($nod
127      list($fromaddress, $fromname) = mailhandler_get_fromaddress($header, $mailbox);      list($fromaddress, $fromname) = mailhandler_get_fromaddress($header, $mailbox);
128      $error_txt = t("Sorry, your comment experienced an error and was not posted. Possible reasons are\n- you have insufficient permission to post comments\n- The node is no longer open for comments.\n\n");      $error_txt = t("Sorry, your comment experienced an error and was not posted. Possible reasons are\n- you have insufficient permission to post comments\n- The node is no longer open for comments.\n\n");
129      $error = $error_txt. t("\n\nYou sent:\n\nFrom: %f\nSubject: %t\nBody:\n%b", array('%f' => $fromaddress, '%t' => $header->subject, '%b' => $origbody));      $error = $error_txt. t("\n\nYou sent:\n\nFrom: %f\nSubject: %t\nBody:\n%b", array('%f' => $fromaddress, '%t' => $header->subject, '%b' => $origbody));
130      $sitemail = variable_get('site_mail', ini_get('sendmail_from'));      drupal_mail('mailhandler_error_comment', $fromaddress, t('Email submission to %sn failed - %subj', array('%sn' => variable_get('site_name', 'Drupal'), '%subj' => $header->subject)));
     $headers = "From: $sitemail\nReply-to: $sitemail\nX-Mailer: Drupal\nReturn-path: $sitemail\nErrors-to: $sitemail";  
     drupal_mail($fromaddress, t('Email submission to %sn failed - %subj', array('%sn' => variable_get('site_name', 'Drupal'), '%subj' => $header->subject)), $error, $headers);  
131      $watchdog = t('Mailhandler: comment submission failure: %subject.', array('%subject' => $edit['subject']));      $watchdog = t('Mailhandler: comment submission failure: %subject.', array('%subject' => $edit['subject']));
132      watchdog('error', $watchdog);      watchdog('mailhandler', $watchdog, WATCHDOG_ERROR);
133    }    }
134  }  }
135    
# Line 139  function mailhandler_node_submit($node, Line 150  function mailhandler_node_submit($node,
150    
151    //dprint_r($node); //DEBUG    //dprint_r($node); //DEBUG
152    
153      // Drupal 5.x & 6.x don't support multiple validations: each node_validate()
154      // call will ADD error messages to previous ones, so if some validation error
155      // occours in one message it will be reported in all messages after it.
156      // Since there is no way to reset form errors, the only method to avoid this
157      // problem is working with $_SESSION['messages'], used by form_set_error().
158      // See http://drupal.org/node/271975 for more info.
159      // Warning: with this method, if the same error message is reported for 2+ different
160      // fields it will be detected only for the last one.
161      if (!isset($_SESSION['messages'])) {
162        $_SESSION['messages'] = array();
163      }
164      $saved_errors = is_array($_SESSION['messages']['error']) ? $_SESSION['messages']['error'] : array();
165      $_SESSION['messages']['error'] = array();
166    node_validate($node);    node_validate($node);
167    $error = form_get_errors();    $error = array();
168      if (count($_SESSION['messages']['error'])) {
169        $allerrors = form_get_errors();
170        foreach ($_SESSION['messages']['error'] as $message) {
171          $keys = array_keys($allerrors, $message);
172          if (!$keys || !count($keys)) {
173            // Not a validation error (but an error, i'll print it)
174            $saved_errors[] = $message;
175          } else {
176            // This is a validation error, i take the last field with it (previous fields
177            // should be about previous validations)
178            $error[$keys[count($keys) - 1]] = $message;
179          }
180        }
181      }
182      if (is_array($saved_errors) && count($saved_errors)) {
183        $_SESSION['messages']['error'] = $saved_errors;
184      }
185      else {
186        unset($_SESSION['messages']['error']);
187      }
188    
189    if (!$error) {    if (!$error) {
190      // Prepare the node for save and allow modules make changes      // Prepare the node for save and allow modules make changes
191      $node = node_submit($node);      $node = node_submit($node);
# Line 148  function mailhandler_node_submit($node, Line 193  function mailhandler_node_submit($node,
193      if ($node->nid) {      if ($node->nid) {
194        if (node_access('update', $node)) {        if (node_access('update', $node)) {
195          node_save($node);          node_save($node);
196          watchdog('special', t("Mailhandler: Updated '%t' by %f", array('%t' => $node->title, '%f' => $fromaddress)));          watchdog('mailhandler', t("Mailhandler: Updated '%t' by %f", array('%t' => $node->title, '%f' => $fromaddress)), WATCHDOG_NOTICE);
197        }        }
198        else {        else {
199          $errortxt = t("The e-mail address '%f' may not update %t items.", array('%f' => $fromaddress, '%t' => $node->type));          $errortxt = t("The e-mail address '%f' may not update %t items.", array('%f' => $fromaddress, '%t' => $node->type));
# Line 157  function mailhandler_node_submit($node, Line 202  function mailhandler_node_submit($node,
202      else {      else {
203        if (node_access('create', $node)) {        if (node_access('create', $node)) {
204          node_save($node);          node_save($node);
205          watchdog('special', t("Mailhandler: Added '%t' by %f", array('%t' => $node->title, '%f' => $fromaddress)));          watchdog('mailhandler', t("Mailhandler: Added '%t' by %f", array('%t' => $node->title, '%f' => $fromaddress)), WATCHDOG_NOTICE);
206        }        }
207        else {        else {
208          $errortxt = t("The e-mail address '%f' may not create %t items.", array('%f' => $fromaddress, '%t' => $node->type));          $errortxt = t("The e-mail address '%f' may not create %t items.", array('%f' => $fromaddress, '%t' => $node->type));
# Line 172  function mailhandler_node_submit($node, Line 217  function mailhandler_node_submit($node,
217    }    }
218    
219    if ($errortxt) {    if ($errortxt) {
220      watchdog('error', "Mailhandler: $errortxt");      watchdog('mailhandler', "Mailhandler: $errortxt", WATCHDOG_ERROR);
221      if ($mailbox['replies']) {      if ($mailbox['replies']) {
222        $errortxt .= t("\n\nYou sent:\n\nFrom: %f\nSubject: %t\nBody:\n%b", array('%f' => $fromaddress, '%t' => $header->subject, '%b' => $origbody));        $errortxt .= t("\n\nYou sent:\n\nFrom: %f\nSubject: %t\nBody:\n%b", array('%f' => $fromaddress, '%t' => $header->subject, '%b' => $origbody));
223        $sitemail = variable_get('site_mail', ini_get('sendmail_from'));        drupal_mail('mailhandler_error_node', $fromaddress, t('Email submission to %sn failed - %subj', array('%sn' => variable_get('site_name', 'Drupal'), '%subj' => $node->title)), $errortxt);
       $headers = "From: $sitemail\nReply-to: $sitemail\nX-Mailer: Drupal\nReturn-path: $sitemail\nErrors-to: $sitemail";  
       drupal_mail($fromaddress, t('Email submission to %sn failed - %subj', array('%sn' => variable_get('site_name', 'Drupal'), '%subj' => $node->title)), $errortxt, $headers);  
224      }      }
225    }    }
226  }  }
# Line 216  function mailhandler_process_message($he Line 259  function mailhandler_process_message($he
259      $body = trim($mailbox['commands']) ."\n". $body;      $body = trim($mailbox['commands']) ."\n". $body;
260    }    }
261    
262      // We set the type now, because we need it in the next block
263      if (!$node->type) $node->type = 'blog';
264    
265      // Reset $node->taxonomy
266      $node->taxonomy = array();
267    
268    // process the commands and the body    // process the commands and the body
269    $lines = explode("\n", $body);    $lines = explode("\n", $body);
270    for ($i = 0; $i < count($lines); $i++) {    for ($i = 0; $i < count($lines); $i++) {
# Line 236  function mailhandler_process_message($he Line 285  function mailhandler_process_message($he
285        // if needed, map term names into IDs. this should move to taxonomy_mailhandler()        // if needed, map term names into IDs. this should move to taxonomy_mailhandler()
286        if ($data[0] == 'taxonomy' && !is_numeric($data[1][0])) {        if ($data[0] == 'taxonomy' && !is_numeric($data[1][0])) {
287          array_walk($data[1], 'mailhandler_term_map');          array_walk($data[1], 'mailhandler_term_map');
288            $node->taxonomy = array_merge($node->taxonomy, $data[1]);
289            unset($data[0]);
290          }
291          else if (substr($data[0], 0, 9) == 'taxonomy[' && substr($data[0], -1, 1) == ']'){
292            // make sure a valid vid is passed in:
293            $vid = substr($data[0], 9, -1);
294            $vocabulary = taxonomy_get_vocabulary($vid);
295            // if the vocabulary is not activated for that node type, unset $data[0], so the command will be ommited from $node
296            // TODO: add an error message
297            if (!in_array($node->type, $vocabulary->nodes)) {
298              unset($data[0]);
299            }
300            else if (!$vocabulary->tags) {
301              array_walk($data[1], 'mailhandler_term_map');
302              $node->taxonomy = array_merge($node->taxonomy, $data[1]);
303              unset($data[0]);
304            }
305            else if ($vocabulary->tags) {
306              // for freetagging vocabularies, we just pass the list of terms
307              $node->taxonomy['tags'][$vid] = implode(',', $data[1]);
308              unset($data[0]); // unset, so it won't be included when populating the node object
309            }
310          }
311          if (!empty($data[0])) {
312            $node->$data[0] = $data[1];
313        }        }
       $node->$data[0] = $data[1];  
314      }      }
315      else {      else {
316        if (is_null($endcommands)) $endcommands = $i;        if (is_null($endcommands)) $endcommands = $i;
# Line 256  function mailhandler_process_message($he Line 329  function mailhandler_process_message($he
329    $node->body = implode("\n", $tmp);    $node->body = implode("\n", $tmp);
330    
331    if (!$node->teaser) $node->teaser = node_teaser($node->body);    if (!$node->teaser) $node->teaser = node_teaser($node->body);
332    if (!$node->type) $node->type = 'blog';  
333    // decode encoded subject line    // decode encoded subject line
334    $subjectarr = imap_mime_header_decode($header->subject);    $subjectarr = imap_mime_header_decode($header->subject);
335    for ($i = 0; $i < count($subjectarr); $i++) {    for ($i = 0; $i < count($subjectarr); $i++) {
# Line 266  function mailhandler_process_message($he Line 339  function mailhandler_process_message($he
339        $node->title .= $subjectarr[$i]->text;        $node->title .= $subjectarr[$i]->text;
340    }    }
341    
342    $node->created = $header->udate;    $node->date = format_date($header->udate, 'custom', 'Y-m-d H:i:s O');
   $node->changed = $header->udate;  
343    $node->format = $mailbox['format'];    $node->format = $mailbox['format'];
344    
345    return $node;    return $node;
# Line 278  function mailhandler_process_message($he Line 350  function mailhandler_process_message($he
350   */   */
351  function mailhandler_term_map(&$term) {  function mailhandler_term_map(&$term) {
352    // provide case insensitive and trimmed map so as to maximize likelihood of successful mapping    // provide case insensitive and trimmed map so as to maximize likelihood of successful mapping
353    $term = db_result(db_query("SELECT tid FROM {term_data} WHERE LOWER('". trim($term) ."') LIKE LOWER(name)"));    $term = db_result(db_query("SELECT tid FROM {term_data} WHERE LOWER('%s') LIKE LOWER(name)", trim($term)));
354  }  }
355    
356  /**  /**
# Line 293  function mailhandler_authenticate($node, Line 365  function mailhandler_authenticate($node,
365      $node->name = $from_user->name;      $node->name = $from_user->name;
366    }    }
367    else if (function_exists('mailalias_user')) { // since $fromaddress failed, try e-mail aliases    else if (function_exists('mailalias_user')) { // since $fromaddress failed, try e-mail aliases
368      $result = db_query("SELECT mail FROM {users} WHERE data LIKE '%%". $fromaddress ."%%'");      $result = db_query("SELECT mail FROM {users} WHERE data LIKE '%%%s%%'",  $fromaddress);
369      while ($alias = db_result($result)) {      while ($alias = db_result($result)) {
370        if ($from_user = mailhandler_user_load($alias, $node->pass, $mailbox)) {        if ($from_user = mailhandler_user_load($alias, $node->pass, $mailbox)) {
371          $node->uid = $from_user->uid; // success!          $node->uid = $from_user->uid; // success!
# Line 325  function mailhandler_switch_user($uid = Line 397  function mailhandler_switch_user($uid =
397    static $orig_user = array();    static $orig_user = array();
398    
399    if (isset($uid)) {    if (isset($uid)) {
400        session_save_session(FALSE);
401      $user = user_load(array('uid' => $uid));      $user = user_load(array('uid' => $uid));
402    }    }
403    // retrieve the initial user, can be called multiple times    // retrieve the initial user, can be called multiple times
404    else if (count($orig_user)) {    else if (count($orig_user)) {
405      $user = array_shift($orig_user);      $user = array_shift($orig_user);
406        session_save_session(TRUE);
407      array_unshift($orig_user, $user);      array_unshift($orig_user, $user);
408    }    }
409    // store the initial user    // store the initial user
# Line 387  function mailhandler_get_part($stream, $ Line 461  function mailhandler_get_part($stream, $
461          $part_number = '1';          $part_number = '1';
462        }        }
463        $text = imap_fetchbody($stream, $msg_number, $part_number);        $text = imap_fetchbody($stream, $msg_number, $part_number);
464        if ($structure->encoding == 3) {        if ($structure->encoding == ENCBASE64) {
465          return drupal_convert_to_utf8(imap_base64($text), $encoding);          return drupal_convert_to_utf8(imap_base64($text), $encoding);
466        }        }
467        else if ($structure->encoding == 4) {        else if ($structure->encoding == ENCQUOTEDPRINTABLE) {
468          return drupal_convert_to_utf8(quoted_printable_decode($text), $encoding);          return drupal_convert_to_utf8(quoted_printable_decode($text), $encoding);
469        }        }
470        else {        else {
471          return drupal_convert_to_utf8($text, $encoding);          return drupal_convert_to_utf8($text, $encoding);
472        }        }
473      }      }
474      if ($structure->type == 1) { /* multipart */      if ($structure->type == TYPEMULTIPART) { /* multipart */
475        while (list($index, $sub_structure) = each ($structure->parts)) {        while (list($index, $sub_structure) = each ($structure->parts)) {
476          if ($part_number) {          if ($part_number) {
477            $prefix = $part_number .'.';            $prefix = $part_number .'.';
# Line 413  function mailhandler_get_part($stream, $ Line 487  function mailhandler_get_part($stream, $
487    return false;    return false;
488  }  }
489    
490    
491    /**
492     * Returns an array of parts as file objects
493     *
494     * @param
495     * @param $structure
496     *   A message structure, usually used to recurse into specific parts
497     * @param $max_depth
498     *   Maximum Depth to recurse into parts.
499     * @param $depth
500     *   The current recursion depth.
501     * @param $part_number
502     *   A message part number to track position in a message during recursion.
503     * @return
504     *   An array of file objects.
505     */
506    function mailhandler_get_parts($stream, $msg_number, $max_depth = 10, $depth = 0, $structure = FALSE, $part_number = FALSE) {
507      $parts = array();
508    
509      // Load Structure.
510      if (!$structure && !$structure = imap_fetchstructure($stream, $msg_number)) {
511        watchdog('mailhandler', t('Could not fetch structure for message number %msg_number', array('%msg_number' => $msg_number)), WATCHDOG_NOTICE);
512        return $parts;
513      }
514    
515      // Recurse into multipart messages.
516      if ($structure->type == TYPEMULTIPART) {
517        // Restrict recursion depth.
518        if ($depth >= $max_depth) {
519          watchdog('mailhandler', t('Maximum recursion depths met in mailhander_get_structure_part for
520                      message number %msg_number.',  array('%msg_number' => $msg_number)), WATCHDOG_NOTICE);
521          return $parts;
522        }
523        foreach($structure->parts as $index => $sub_structure) {
524          // If a part number was passed in and we are a multitype message, prefix the
525          // the part number for the recursive call to match the imap4 dot seperated part indexing.
526          if ($part_number) {
527            $prefix = $part_number .'.';
528          }
529          $sub_parts =  mailhandler_get_parts($stream, $msg_number, $max_depth, $depth + 1,
530            $sub_structure, $prefix . ($index + 1));
531          $parts = array_merge($parts, $sub_parts);
532        }
533        return $parts;
534      }
535    
536      // Per Part Parsing.
537    
538      // Initalize file object like part structure.
539      $part = new StdClass();
540      $part->attributes = array();
541      $part->filename = 'unnamed_attachment';
542      if (!$part->filemime = mailhandler_get_mime_type($structure)) {
543        watchdog('mailhandler', t('Could not fetch mime type for message part. Defaulting to application/octet-stream.'),
544          WATCHDOG_NOTICE);
545        $part->filemime = 'application/octet-stream';
546      }
547    
548      if ($structure->ifparameters) {
549        foreach ($structure->parameters as $parameter) {
550          switch (strtoupper($parameter->attribute)) {
551            case 'NAME':
552            case 'FILENAME':
553              $part->filename = $parameter->value;
554              break;
555            default:
556              // put every thing else in the attributes array;
557              $part->attributes[$parameter->attribute] = $parameter->value;
558          }
559        }
560      }
561    
562      // Handle Content-Disposition parameters for non-text types.
563      if ($structure->type != TYPETEXT && $structure->ifdparameters) {
564        foreach ($structure->dparameters as $parameter) {
565          switch (strtoupper($parameter->attribute)) {
566            case 'NAME':
567            case 'FILENAME':
568              $part->filename = $parameter->value;
569              break;
570            // put every thing else in the attributes array;
571            default:
572              $part->attributes[$parameter->attribute] = $parameter->value;
573          }
574        }
575      }
576    
577      // Retrieve part  convert MIME encoding to UTF-8
578      if(!$part->data = imap_fetchbody($stream, $msg_number, $part_number)) {
579        watchdog('mailhandler', 'No Data!!', WATCHDOG_ERROR);
580        return $parts;
581      }
582    
583      // convert text attachment to UTF-8.
584      if ($structure->type == TYPETEXT) {
585        $part->data = imap_utf8($part->data);
586      }
587      else {
588        // If not text then decode as necessary
589        if ($structure->encoding == ENCBASE64) {
590          $part->data = imap_base64($part->data);
591        }
592        else if ($structure->encoding == ENCQUOTEDPRINTABLE) {
593          $part->data = quoted_printable_decode($part->data);
594        }
595      }
596    
597      //always return an array to satisfy array_merge in recursion catch, and array return value.
598      $parts[] = $part;
599      return $parts;
600    }
601    
602  /**  /**
603   * Retrieve MIME type of the message structure.   * Retrieve MIME type of the message structure.
604   */   */
# Line 426  function mailhandler_get_mime_type(&$str Line 612  function mailhandler_get_mime_type(&$str
612  }  }
613    
614  /**  /**
  * Implementation of hook_user().  
  */  
 function mailhandler_user($type, &$edit, &$account, $category = NULL) {  
   if ($type == 'view') {  
     // @TODO: We may add a new option in mailboxes to choose which roles can post.  
     if ((user_access('edit own blog') && $GLOBALS['user']->uid == $account->uid) || user_access('administer users')) {  
       // for now, just show the first mailbox address to user.  
       $mailbox = db_fetch_array(db_query('SELECT * FROM {mailhandler} WHERE enabled = 1 ORDER BY mail'));  
       if ($mailbox) {  
         if ($mailbox['security'] == 1) {  
           $form = array(  
             '#title' => t('Mail Handler'),  
             '#value' => t('You may post to <a href="@blog">your blog</a> by sending an e-mail to %mail. Be sure to include your password at the top of your e-mail body (e.g. <em>pass=mypassword</em>).', array('@blog' => url("blog/$account->uid"), '%mail' => $mailbox['mail']))  
           );  
         }  
         else {  
           $form = array(  
             '#title' => t('Mail Handler'),  
             '#value' => t('You may post to <a href="@blog">your blog</a> by sending an e-mail to %mail.', array('@blog' => url("blog/$account->uid"), '%mail' => $mailbox['mail']))  
           );  
         }  
         return array(t('Mail Handler') => array('mailhandler' => theme('item', $form)));  
       }  
     }  
   }  
 }  
   
 /**  
615   * Implementation of hook_cron(). Process msgs from all enabled mailboxes.   * Implementation of hook_cron(). Process msgs from all enabled mailboxes.
616   */   */
617  function mailhandler_cron() {  function mailhandler_cron() {
# Line 484  function mailhandler_menu($may_cache) { Line 642  function mailhandler_menu($may_cache) {
642    if ($may_cache) {    if ($may_cache) {
643      $items[] = array('path' => 'admin/content/mailhandler', 'title' => t('Mailhandler'),      $items[] = array('path' => 'admin/content/mailhandler', 'title' => t('Mailhandler'),
644        'callback' => 'mailhandler_admin',        'callback' => 'mailhandler_admin',
645          'description' => t('Manage mailboxes and retrieve messages.'),
646        'access' => $admin_access);        'access' => $admin_access);
647      $items[] = array('path' => 'admin/content/mailhandler/retrieve', 'title' => t('Retrieve'),      $items[] = array('path' => 'admin/content/mailhandler/retrieve', 'title' => t('Retrieve'),
648        'callback' => 'mailhandler_admin_retrieve',        'callback' => 'mailhandler_admin_retrieve',
# Line 526  function mailhandler_admin() { Line 685  function mailhandler_admin() {
685   */   */
686  function mailhandler_display() {  function mailhandler_display() {
687    $destination = drupal_get_destination();    $destination = drupal_get_destination();
688    $header = array(t('Mailbox'), array('data' => t('Operations'), 'colspan' => 3));    $header = array(t('Mailbox'), t('Folder'), array('data' => t('Operations'), 'colspan' => 3));
689    $rows = array();    $rows = array();
690    $result = db_query('SELECT * FROM {mailhandler} ORDER BY mail');    $result = db_query('SELECT * FROM {mailhandler} ORDER BY mail');
691    while ($mailbox = db_fetch_object($result)) {    while ($mailbox = db_fetch_object($result)) {
692      $rows[] = array(      $rows[] = array(
693        "<a href=\"mailto:$mailbox->mail\">$mailbox->mail</a>",        "<a href=\"mailto:$mailbox->mail\">$mailbox->mail</a>",
694          $mailbox->folder ? check_plain($mailbox->folder) : '',
695        l(t('Retrieve'), "admin/content/mailhandler/retrieve/$mailbox->mid", array('title' => t('Retrieve and process pending e-mails in this mailbox')), $destination),        l(t('Retrieve'), "admin/content/mailhandler/retrieve/$mailbox->mid", array('title' => t('Retrieve and process pending e-mails in this mailbox')), $destination),
696        l(t('Edit'), "admin/content/mailhandler/edit/$mailbox->mid", array('title' => t('Edit this mailbox configuration')), $destination),        l(t('Edit'), "admin/content/mailhandler/edit/$mailbox->mid", array('title' => t('Edit this mailbox configuration')), $destination),
697        l(t('Delete'), "admin/content/mailhandler/delete/$mailbox->mid", array('title' => t('Delete this mailbox')), $destination)        l(t('Delete'), "admin/content/mailhandler/delete/$mailbox->mid", array('title' => t('Delete this mailbox')), $destination)
# Line 590  function mailhandler_form($edit = array( Line 750  function mailhandler_form($edit = array(
750    $form['mailto'] = array('#type' => 'textfield', '#title' => t('Second E-mail address'), '#default_value' => $edit['mailto'], '#description' => t('Optional. The e-mail address to which modules should send generated content.'));    $form['mailto'] = array('#type' => 'textfield', '#title' => t('Second E-mail address'), '#default_value' => $edit['mailto'], '#description' => t('Optional. The e-mail address to which modules should send generated content.'));
751    $form['folder'] = array('#type' => 'textfield', '#title' => t('Folder'), '#default_value' => $edit['folder'], '#description' => t('Optional. The folder where the mail is stored. If you want this mailbox to read from a local folder, give the full path. Leave domain, port, name, and pass empty below. Remember to set the folder to readable and writable by the webserver.'));    $form['folder'] = array('#type' => 'textfield', '#title' => t('Folder'), '#default_value' => $edit['folder'], '#description' => t('Optional. The folder where the mail is stored. If you want this mailbox to read from a local folder, give the full path. Leave domain, port, name, and pass empty below. Remember to set the folder to readable and writable by the webserver.'));
752    $form['imap'] = array('#type' => 'select', '#title' => t('POP3 or IMAP Mailbox'), '#options' => array('POP3', 'IMAP'), '#default_value' => $edit['imap'], '#description' => t('If you wish to retrieve mail from a POP3 or IMAP mailbox instead of a Folder, select POP3 or IMAP. Also, complete the Mailbox items below.'));    $form['imap'] = array('#type' => 'select', '#title' => t('POP3 or IMAP Mailbox'), '#options' => array('POP3', 'IMAP'), '#default_value' => $edit['imap'], '#description' => t('If you wish to retrieve mail from a POP3 or IMAP mailbox instead of a Folder, select POP3 or IMAP. Also, complete the Mailbox items below.'));
753    $form['domain'] = array('#type' => 'textfield', '#title' => t('Mailbox domain'), '#default_value' => $edit['domain'], '#description' => t('The domain of the server used to collect mail.'), '#required' => TRUE);    $form['domain'] = array('#type' => 'textfield', '#title' => t('Mailbox domain'), '#default_value' => $edit['domain'], '#description' => t('The domain of the server used to collect mail.'));
754    $form['port'] = array('#type' => 'textfield', '#title' => t('Mailbox port'), '#size' => 5, '#maxlength' => 5, '#default_value' => $edit['port'], '#description' => t('The port of the mailbox used to collect mail (usually 110 for POP3, 143 for IMAP).'));    $form['port'] = array('#type' => 'textfield', '#title' => t('Mailbox port'), '#size' => 5, '#maxlength' => 5, '#default_value' => $edit['port'], '#description' => t('The port of the mailbox used to collect mail (usually 110 for POP3, 143 for IMAP).'));
755    $form['name'] = array('#type' => 'textfield', '#title' => t('Mailbox username'), '#default_value' => $edit['name'], '#description' => t('This username is used while logging into this mailbox during mail retrieval.'));    $form['name'] = array('#type' => 'textfield', '#title' => t('Mailbox username'), '#default_value' => $edit['name'], '#description' => t('This username is used while logging into this mailbox during mail retrieval.'));
756    $form['pass'] = array('#type' => 'textfield', '#title' => t('Mailbox password'), '#default_value' => $edit['pass'], '#description' => t('The password corresponding to the username above. Consider using a non-vital password, since this field is stored without encryption in the database.'));    $form['pass'] = array('#type' => 'textfield', '#title' => t('Mailbox password'), '#default_value' => $edit['pass'], '#description' => t('The password corresponding to the username above. Consider using a non-vital password, since this field is stored without encryption in the database.'));
# Line 601  function mailhandler_form($edit = array( Line 761  function mailhandler_form($edit = array(
761    $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['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.'));
762    $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['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.'));
763    $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['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.'));
764    $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'))));
765    $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['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.'));
766    $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['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.'));
767    $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.'));    $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.'));
# Line 649  function mailhandler_form_submit($form_i Line 809  function mailhandler_form_submit($form_i
809    if ($edit['mid']) {    if ($edit['mid']) {
810      // Includes fields to allow administrators to add extra IMAP commands,      // Includes fields to allow administrators to add extra IMAP commands,
811      // and select the format of saved nodes/comments      // and select the format of saved nodes/comments
812      db_query("UPDATE {mailhandler} SET mail = '%s', mailto = '%s', domain = '%s', port = %d, folder = '%s', name = '%s', pass = '%s', extraimap = '%s', mime = '%s', imap = '%s', security = %d, replies = %d, fromheader = '%s', commands = '%s', sigseparator = '%s', enabled = %d, delete_after_read = %d, format = %d WHERE mid = '%s'", $edit['mail'], $edit['mailto'], $edit['domain'], $edit['port'], $edit['folder'], $edit['name'], $edit['pass'], $edit['extraimap'], $edit['mime'], $edit['imap'], $edit['security'], $edit['replies'], $edit['fromheader'], $edit['commands'], $edit['sigseparator'], $edit['enabled'], $edit['delete_after_read'], $edit['format'], $edit['mid']);      db_query("UPDATE {mailhandler} SET mail = '%s', mailto = '%s', domain = '%s', port = %d, folder = '%s', name = '%s', pass = '%s', extraimap = '%s', mime = '%s', imap = '%s', security = %d, replies = %d, fromheader = '%s', commands = '%s', sigseparator = '%s', enabled = %d, delete_after_read = %d, format = %d WHERE mid = %d", $edit['mail'], $edit['mailto'], $edit['domain'], $edit['port'], $edit['folder'], $edit['name'], $edit['pass'], $edit['extraimap'], $edit['mime'], $edit['imap'], $edit['security'], $edit['replies'], $edit['fromheader'], $edit['commands'], $edit['sigseparator'], $edit['enabled'], $edit['delete_after_read'], $edit['format'], $edit['mid']);
813      drupal_set_message(t('Mailbox updated'));      drupal_set_message(t('Mailbox updated'));
814    }    }
815    else {    else {
# Line 681  function mailhandler_admin_delete_confir Line 841  function mailhandler_admin_delete_confir
841  function mailhandler_admin_delete_confirm_submit($form_id, $form_values) {  function mailhandler_admin_delete_confirm_submit($form_id, $form_values) {
842    $info = db_fetch_object(db_query("SELECT mid, mail FROM {mailhandler} WHERE mid = %d", $form_values['mid']));    $info = db_fetch_object(db_query("SELECT mid, mail FROM {mailhandler} WHERE mid = %d", $form_values['mid']));
843    db_query("DELETE FROM {mailhandler} WHERE mid = %d", $form_values['mid']);    db_query("DELETE FROM {mailhandler} WHERE mid = %d", $form_values['mid']);
844    watchdog('special', t('Mailhandler: Mailbox %mailbox deleted', array('%mailbox' => $info->mail)));    watchdog('mailhandler', t('Mailhandler: Mailbox %mailbox deleted', array('%mailbox' => $info->mail)), WATCHDOG_NOTICE);
845    drupal_set_message(t('Mailbox %mailbox deleted', array('%mailbox' => $info->mail)));    drupal_set_message(t('Mailbox %mailbox deleted', array('%mailbox' => $info->mail)));
846    drupal_goto('admin/content/mailhandler');    drupal_goto('admin/content/mailhandler');
847  }  }
# Line 693  function mailhandler_help($section = 'ad Line 853  function mailhandler_help($section = 'ad
853    $output = '';    $output = '';
854    $link->add = l(t('Add mailbox'), 'admin/content/mailhandler/add');    $link->add = l(t('Add mailbox'), 'admin/content/mailhandler/add');
855    
856      // Gather examples of useful commands, and build a definition list with them:
857      $commands[] = array('command' => 'taxonomy: [term1, term2]',
858                          'description' => t('Use this to add the terms <em>term1</em> and <em>term2</em> to the node.<br />
859                          Both of the terms should already exist. In case they do not exist already, they will be quietly ommitted'));
860      $commands[] = array('command' => 'taxonomy[v]: [term1, term2]',
861                          '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 />
862                          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'));
863    
864      $commands_list = '<dl>';
865      foreach ($commands as $command) {
866        $commands_list .= '<dt>'. $command['command'] .'</dt>';
867        $commands_list .= '<dl>'. $command['description'] .'</dl>';
868      }
869      $commands_list .= '</dl>';
870    
871    switch ($section) {    switch ($section) {
872      case 'admin/help#mailhandler':      case 'admin/help#mailhandler':
873        $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>';        $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>';
# Line 705  function mailhandler_help($section = 'ad Line 880  function mailhandler_help($section = 'ad
880  <li>administer mailhandler at <a href="@admin-mailhandler">administer &gt;&gt; mailhandler</a>.</li>  <li>administer mailhandler at <a href="@admin-mailhandler">administer &gt;&gt; mailhandler</a>.</li>
881  <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>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>
882  <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>';  <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>';
883          $output .= '<h3 id="commands">'. t('Useful Commands') .'</h3>';
884          $output .= $commands_list;
885        $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>';        $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>';
886        return $output;        return $output;
887      case 'admin/content/mailhandler':      case 'admin/content/mailhandler':

Legend:
Removed from v.1.87  
changed lines
  Added in v.1.87.2.18

  ViewVC Help
Powered by ViewVC 1.1.2