function mailhandler_node_prepare_message in Mailhandler 7
Same name and namespace in other branches
- 6 mailhandler.module \mailhandler_node_prepare_message()
Append default commands. Separate commands from body. Strip signature. Return a node object.
1 call to mailhandler_node_prepare_message()
File
- ./
mailhandler.module, line 341
Code
function mailhandler_node_prepare_message($header, $body, $mailbox) {
// Initialise a node object
$node = new stdClass();
$node->pass = NULL;
// Initialize parameters
$sep = $mailbox['sigseparator'];
// Copy any name/value pairs from In-Reply-To or References e-mail headers to $node. Useful for maintaining threading info.
if (!empty($header->references)) {
// we want the final element in references header, watching out for white space
$threading = substr(strrchr($header->references, '<'), 0);
}
else {
if (!empty($header->in_reply_to)) {
$threading = str_replace(strstr($header->in_reply_to, '>'), '>', $header->in_reply_to);
// Some MUAs send more info in that header.
}
}
if (isset($threading) && ($threading = rtrim(ltrim($threading, '<'), '>'))) {
//strip angle brackets
if ($threading) {
$node->threading = $threading;
}
parse_str($threading, $tmp);
if ($tmp['host']) {
$tmp['host'] = ltrim($tmp['host'], '@');
// strip unnecessary @ from 'host' element
}
foreach ($tmp as $key => $value) {
$node->{$key} = $value;
}
}
// Prepend the default commands for this mailbox
if ($mailbox['commands']) {
$body = trim($mailbox['commands']) . "\n" . $body;
}
if ($commands = mailhandler_commands_parse($body, $sep)) {
// The node type must be set first in order to properly initialize the node
foreach ($commands['commands'] as $command) {
if ($command[0] == 'type') {
$node->type = $command[1];
}
}
}
// Set a default type if none provided
if (!isset($node->type)) {
$node->type = mailhandler_default_type();
}
// Apply defaults to the $node object, and allow modules to add default values
require_once drupal_get_path('module', 'node') . '/node.pages.inc';
node_object_prepare($node);
// If cron is called by anon then node_object_prepare sets uid to 0, but otherwise global $user.
// Manually set this to 0 since that's what the rest of the code expects.
$node->uid = 0;
// In order to fall back to the permission system for comment status, the status property must
// be unset if type is comment. It will get set by explicit commands, and if not, by
// comment_save itself.
if ($node->type == 'comment') {
unset($node->status);
}
// Execute the commands
if (!empty($commands['commands'])) {
mailhandler_node_process_message_commands($node, $commands['commands']);
}
// Isolate the body from the commands and the sig
$tmp = array_slice($commands['lines'], $commands['endcommands'], $commands['i'] - $commands['endcommands']);
// flatten and assign the body to node object. note that filter() is called within node_save() [tested with blog post]
$type = node_get_types('type', $node->type);
if ($type->has_body) {
$node->body = implode("\n", $tmp);
}
if (empty($node->teaser)) {
$node->teaser = node_teaser($node->body);
}
// decode encoded subject line
$subjectarr = imap_mime_header_decode($header->subject);
if (empty($subjectarr)) {
$node->title = truncate_utf8(trim(decode_entities(strip_tags(check_markup($node->body)))), 29, TRUE);
}
else {
for ($i = 0; $i < count($subjectarr); $i++) {
if ($subjectarr[$i]->charset != 'default') {
$node->title .= drupal_convert_to_utf8($subjectarr[$i]->text, $subjectarr[$i]->charset);
}
else {
$node->title .= $subjectarr[$i]->text;
}
}
}
$node->date = $node->changed = format_date($header->udate, 'custom', 'Y-m-d H:i:s O');
$node->format = $mailbox['format'];
// If an nid command was supplied, and type is not 'comment', append the revision number
if (isset($node->nid) && !empty($node->nid) && $node->type != 'comment') {
$vid = db_result(db_query('SELECT n.vid FROM {node} n WHERE n.nid = %d', $node->nid));
if ($vid) {
$node->revision = $node->vid = $vid;
}
}
return $node;
}