You are here

function mailhandler_node_process_message in Mailhandler 6

Same name and namespace in other branches
  1. 7 mailhandler.module \mailhandler_node_process_message()
1 call to mailhandler_node_process_message()
mailhandler_node_process_mailbox in ./mailhandler.module
Run message retrieval and node processing on a mailbox - is a wrapper around mailhandler_retrieve

File

./mailhandler.module, line 734
Mailhandler module code.

Code

function mailhandler_node_process_message($header, $origbody, $mailbox, $mimeparts) {
  mailhandler_user_switch();

  // we must process before authenticating because the password may be in Commands
  $node = mailhandler_node_prepare_message($header, $origbody, $mailbox);

  // Authenticate the message
  if (!($node = mailhandler_mailhandler_authenticate('execute', $mailbox['authentication'], array(
    $node,
    $header,
    $origbody,
    $mailbox,
  )))) {
    mailhandler_watchdog_record('Message failed authentication.', array(), WATCHDOG_NOTICE);
    return FALSE;
  }

  // Put $mimeparts on the node
  $node->mimeparts = $mimeparts;

  // we need to change the current user
  // this has to be done here to allow modules
  // to create users
  mailhandler_user_switch($node->uid);

  // modules may override node elements before submitting. they do so by returning the node.
  foreach (module_list() as $name) {
    if (module_hook($name, 'mailhandler')) {
      $function = $name . '_mailhandler';

      // @todo: hook_mailhandler implementation is broken. When this function
      // was moved out of the retrieve loop, some of the variables where not
      // updated.
      $result = $i = NULL;
      if (!($node = $function($node, $result, $i, $header, $mailbox))) {

        // Exit if a module has handled the submitted data.
        break;
      }
    }
  }
  if ($node) {
    if ($node->type == 'comment') {
      $saved = mailhandler_comment_submit($node, $header, $mailbox, $origbody);
      $type = 'comment';
    }
    else {
      $saved = mailhandler_node_submit($node, $header, $mailbox, $origbody);
      $type = 'node';
    }
  }

  // Invoke a second hook for modules to operate on the newly created/edited node/comment.
  foreach (module_list() as $name) {
    if (module_hook($name, 'mailhandler_post_save')) {
      $function = $name . '_mailhandler_post_save';

      // Pass in what's returned from saving the comment (cid) or node (object), the
      // object type, and the original node object (which is the same as $saved in
      // the case of saving a node).
      $function($saved, $type, $node);
    }
  }

  // switch back to original user
  mailhandler_user_switch();

  // Put something in the results array for the counter in the batch finished callback
  $context['results'][] = $mailbox['mail'];
  mailhandler_user_switch();
}