'Mail Comment',
'description' => 'Configure automatic mail responses.',
'page callback' => 'drupal_get_form',
'page arguments' => array('mailcomment_admin_settings'),
'access arguments' => array('administer notifications'),
);
$items['mailcomment/redirect'] = array(
'title' => 'Mail Comment',
'description' => 'Redirect Mail Comment links to original message.',
'page callback' => 'mailcomment_message_redirect',
'page arguments' => array(2),
'access arguments' => array('access content'),
);
return $items;
}
/**
* Admin settings form
*/
function mailcomment_admin_settings() {
$form['mailcomment_mailbox'] = array(
'#title' => t('Master mailcomment mailbox'),
'#type' => 'select',
'#options' => mailcomment_mailbox_list(),
'#default_value' => variable_get('mailcomment_mailbox', ''),
'#description' => t('E-mail account used to set Reply-To for outgoing e-mail notifications. Other modules can override this, but in the event
no other modules do, this address will be used by default.'),
);
$form['mailcomment_mailboxes'] = array(
'#title' => t('Mail Comment-enabled mailboxes'),
'#type' => 'select',
'#multiple' => TRUE,
'#options' => mailcomment_mailbox_list(),
'#default_value' => variable_get('mailcomment_mailboxes', ''),
'#description' => t('E-mail accounts that Mail Comment will check when checking for new messages. It is required to select the mailbox you set
for "Master mailcomment mailbox" and then you may select any other available mailboxes.'),
);
$form['mailcomment_mailboxes_catchalls'] = array(
'#title' => t('Mail Comment catchall mailboxes'),
'#type' => 'select',
'#multiple' => TRUE,
'#options' => mailcomment_mailbox_list(),
'#required' => FALSE,
'#default_value' => variable_get('mailcomment_mailboxes_catchalls', ''),
'#description' => t('Select which Mail Comment mailboxes are catchall email addresses, if any.'),
);
// Expiration time
$period = drupal_map_assoc(array(60, 3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200), 'format_interval');
$period[0] = t('Never');
$form['mailcomment_expiration'] = array(
'#title' => t('Expiration time'),
'#type' => 'select',
'#options' => $period,
'#required' => TRUE,
'#default_value' => variable_get('mailcomment_expiration', 0),
'#description' => t('Time after which the signature of outgoing e-mails expires and responses won\'t be accepted anymore.'),
);
$form['mailcomment_reply_text'] = array(
'#title' => t('Reply text'),
'#type' => 'textfield',
'#default_value' => variable_get('mailcomment_reply_text', t('((( Reply ABOVE this LINE to POST a COMMENT )))')),
'#description' => t('Text to separate reply from the rest of the e-mail. Leave blank for not using this feature.'),
);
$form['mailcomment_insert_reply_text'] = array(
'#title' => t('Insert reply to text'),
'#type' => 'checkbox',
'#default_value' => variable_get('mailcomment_insert_reply_text', 1),
'#description' => t('If this box is checked, mail comment will insert the text specified as the Reply text at the top of messages sent. You may uncheck this box and insert the text manually into a messaging template (for example, above the footer).'),
);
$form['mailcomment_error_bounce'] = array(
'#title' => t('Bounce rejected emails'),
'#type' => 'checkbox',
'#default_value' => variable_get('mailcomment_error_bounce', 1),
'#description' => t('If this box is checked, a reply will be sent for wrong emails with some information about the cause of rejection.'),
);
$form['mailcomment_server_string'] = array(
'#title' => t('Server string for Message Id'),
'#type' => 'textfield',
'#default_value' => variable_get('mailcomment_server_string', $_SERVER['HTTP_HOST']),
'#description' => t('Server name to be used in Message Id\'s. It will be included in outgoing emails and checked on responses.'),
);
$form['mailcomment_alter_subjects'] = array(
'#type' => 'radios',
'#title' => t('Use mailing list-style subjects'),
'#description' => t('When enabled this setting will alter the subjects of outgoing messages to look more like subjects used by mailing lists. For
example the subject will look like "[My Site] Subject of Message". This style makes message threading work better in certain email clients that
do not pay attention to the in-reply-to header parameter.'),
'#default_value' => variable_get('mailcomment_alter_subjects', 1),
'#options' => array(t('Off'), t('On')),
);
$form['mailcomment_cleaner'] = array(
'#type' => 'radios',
'#title' => t('Enable message cleaner'),
'#description' => t('Attempt to remove email client gunk at bottom of emails, like "On Jan 26, 2009, John Doe wrote:"?
If parts of emails are getting stripped out or appearing empty, turn this off.'),
'#default_value' => variable_get('mailcomment_cleaner', 1),
'#options' => array(t('Off'), t('On')),
);
$options = array();
$ntypes = node_get_types('names');
foreach ($ntypes as $type => $name) {
$options[$type] = $name;
}
$form['mailcomment_nodetypes'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Mail Comment content types'),
'#description' => t('Choose on which content types to enable mailcomment. Enabling mailcomment for a content type will ensure
that the proper headers are added to the outgoing message.'),
'#options' => $options,
'#default_value' => variable_get('mailcomment_nodetypes', array()),
);
$form['mailcomment_passthru'] = array(
'#type' => 'radios',
'#title' => t('Passthru to mailhandler module'),
'#description' => t('If you want messages that are sent to the mailcomment address (but which are missing the mailcomment paramaters needed
to properly post the comment) to be passed on to mailhandler, enable this. If this is enabled mailcomment will not throw away the node object,
but instead will return it to mailhandler to decide how to handle it.'),
'#default_value' => variable_get('mailcomment_passthru', 0),
'#options' => array(t('Off'), t('On')),
);
module_load_include('inc', 'mailhandler', 'mailhandler.retrieve');
$options = array();
$descriptions = '';
$options[0] = t('None');
$methods = mailhandler_mailhandler_authenticate('info');
foreach ($methods as $method) {
foreach ($method as $name => $info) {
$options[$name] = t($info['title']);
// TODO: theme function
$descriptions .= t($info['title']) .": ". t($info['description']) . "\n";
}
}
$form['mailcomment_passthru_authentication'] = array(
'#type' => 'select',
'#title' => t('Use default mailhandler authentication during passthru'),
'#description' => t('Authenticate messages using Mailhandler\'s default authentication method which authenticates based on the sender\'s email address.
This is less secure but often more convenient. This option is only useful if relying on "Passthru to mailhandler module".'),
'#default_value' => variable_get('mailcomment_passthru_authentication', ''),
'#options' => $options,
);
return system_settings_form($form);
}
/**
* Validate the mailcomment settings form.
*
* @see mailcomment_admin_settings()
*/
function mailcomment_admin_settings_validate($form, &$form_state) {
$mailbox = $form_state['values']['mailcomment_mailbox'];
$mailboxes = $form_state['values']['mailcomment_mailboxes'];
if ($mailbox && !in_array($mailbox, $mailboxes)) {
form_set_error('mailcomment_mailboxes', t('For the "Mail Comment-Enabled Mailboxes" setting you must select at least
the mailbox you chose for the "Master Mail Comment Mailbox".'));
}
}
/**
* Get list of available mailboxes
*/
function mailcomment_mailbox_list() {
$list = array();
$result = db_query('SELECT mid, mail FROM {mailhandler} ORDER BY mail');
while ($mailbox = db_fetch_object($result)) {
$list[$mailbox->mid] = $mailbox->mail;
}
return $list;
}
/**
* Get mail to be used as reply to.
*
* Get data from settings and mailbox with static caching.
*/
function mailcomment_mailbox_mail() {
static $mail;
if (!isset($mail)) {
if (($mid = variable_get('mailcomment_mailbox', 0)) && ($mailbox = mailhandler_get_mailbox($mid))) {
$mail = $mailbox['mail'];
}
else {
$mail = '';
}
}
return $mail;
}
/**
* Get mail to be used as reply to.
*
* Get data from settings and mailbox with static caching.
*/
function mailcomment_mailboxes() {
static $mailboxes = array();
if (empty($mailboxes)) {
$setting = variable_get('mailcomment_mailboxes', '');
if (!empty($setting)) {
foreach ($setting as $mailbox) {
$mailboxes[] = db_result(db_query("SELECT mail FROM {mailhandler} WHERE mid = %d", $mailbox));
}
}
}
return $mailboxes;
}
/**
* Implementation of hook_message_alter().
*
* Adds message headers into outgoing emails for notifications
*/
function mailcomment_message_alter(&$message, $info) {
$params = array();
// For now, just for non digested emails
if (!empty($message->notifications) && ($account = $message->account)
&& empty($message->notifications['digest']) && $info['group'] == 'mail') {
$event = current($message->notifications['events']);
if ($event->type == 'node' && !empty($event->objects['node'])) {
$params['uid'] = $event->objects['node']->uid;
$params['nid'] = $event->objects['node']->nid;
$params['cid'] = 0;
$params['time'] = $event->objects['node']->created;
if ($event->action == 'comment' && !empty($event->objects['comment'])) {
$params['uid'] = $event->objects['comment']->uid;
$params['cid'] = $event->objects['comment']->cid;
$ancestor_msg_id = mailcomment_mail_comment_ancestor_message_id($params['nid'], $event->objects['comment']->cid);
$params['time'] = $event->objects['comment']->timestamp;
}
}
}
// If we've got some params out of the message, embed them into the message id for emails only
// and only if the recipient of the message is allowed to post comments.
if ($params && ($reply = mailcomment_mailbox_mail()) && user_access('post comments', $account) && in_array($event->objects['node']->type, variable_get('mailcomment_nodetypes', array()))) {
// Alter message subjects
if (variable_get('mailcomment_alter_subjects', 1)) {
if ($event->type == 'node' && !empty($event->objects['node'])) {
$message->subject = variable_get('site_name', '') ? '[' . variable_get('site_name', '') . '] ' . $event->objects['node']->title : $message->subject;
}
if ($event->action == 'comment') {
$message->subject = t('Re:') . ' ' . $message->subject;
}
}
$message->params['mail']['headers']['Message-ID'] = mailcomment_build_messageid($params);
$message->params['mail']['headers']['Reply-To'] = $reply;
if (isset($ancestor_msg_id)) {
$message->params['mail']['headers']['In-Reply-To'] = $ancestor_msg_id;
}
// Add marker text into the message header part taking care of already existing text
$insert_reply_text = variable_get('mailcomment_insert_reply_text', 1);
$text = variable_get('mailcomment_reply_text', t('((( Reply ABOVE this LINE to POST a COMMENT )))'));
if ($text && $insert_reply_text) {
$prefix = array($text);
if (!empty($message->body['#prefix'])) {
$prefix[] = $message->body['#prefix'];
}
// This glue text is a best guess, may cause trouble though, also with filtering (?).
// So we better explicitly set glue text for all sending methods
$info += array('glue' => "\n");
$message->body['#prefix'] = implode($info['glue'], $prefix);
}
// Alter $params in order to provide recipient uid in message signature
$params['uid'] = $account->uid;
$message->body['#footer'] .= ' View original post: '. url('mailcomment/redirect/' . mailcomment_build_messageid($params), array('absolute' => TRUE));
}
}
/**
* Implementation of hook_mailhandler().
*/
function mailcomment_mailhandler($node, $result, $i, $header, $mailbox) {
$matched = FALSE;
// Build and static cache list of catchall email address hosts.
static $hosts;
if (!is_array($hosts)) {
$hosts = _mailcomment_catchall_hosts();
}
$to = $header->to[0]->mailbox . '@' . $header->to[0]->host;
// If not a catchall, check only for direct matches
if (in_array($to, mailcomment_mailboxes())) {
$matched = TRUE;
}
// Do host match for catchalls
else if (in_array($header->to[0]->host, $hosts)) {
$matched = TRUE;
}
if ($matched && $node->threading && empty($node->mailcomment['params']['error'])) {
// Now check user id , just go ahead if they match and it is a valid user
if ($node->uid && $node->uid == $node->mailcomment['params']['uid']) {
// Add params into the node object, other modules may use them
// Set comment parameters
$node->type = 'comment';
$node->nid = $node->mailcomment['params']['nid'];
$node->pid = $node->mailcomment['params']['cid'];
// Let comment_save handle the comment status.
unset($node->status);
// Now trim out the rest of the message if separator text exists
// @ TODO May fail for html mails
if ($marker = variable_get('mailcomment_reply_text', t('((( Reply ABOVE this LINE to POST a COMMENT )))'))) {
// Allow other modules to act on the text and node object before we start stripping things from it.
mailcomment_invoke_mailcomment_alter($node, 'pre');
// Now the dirty part. May need some more clean up for line endings, spare html, etc...
$pos = strpos($node->body, $marker);
if ($pos !== FALSE) {
// Mailhandler brings this in as a full node with teaser/body but we need to change context to comment
$node->body = drupal_substr($node->body, 0, $pos);
// Run the default message cleaner
if (variable_get('mailcomment_cleaner', 0)) {
// Then get the offset of the last two line breaks, and substring again, if the cleaner is enabled.
$search = preg_match_all("/
|\r\n\r\n|\n\n[^
|\r\n\r\n|\n\n]+$/m", $node->body, $matches, PREG_OFFSET_CAPTURE);
if (!empty($matches[0])) {
$key = max(array_keys($matches[0]));
$offset = $matches[0][$key][1];
$node->body = drupal_substr($node->body, 0, $offset);
}
}
// Allow other modules to act on the text and node object after delimiter has been stripped and the message cleaned.
mailcomment_invoke_mailcomment_alter($node, 'post');
}
}
// We return the "node" but it will be saved in mailhandler as a comment
return $node;
}
else {
// Users in parameters and mail don't match
$node->mailcomment['params']['error'] = MAILCOMMENT_ERROR_USER;
}
}
// If we reach here, there has been an error. Check error code or send a generic one.
// This part doesn't return a node so it won't be further processed by mailhandler
if (count($node->mailcomment['params']) > 1 && $header->toaddress == $mbox_name) {
mailcomment_error($node->mailcomment['params']['error'], $node, $header);
}
// If there are no params, return the node back to mailhandler, regardless of to address.
else {
if (variable_get('mailcomment_passthru', 0) == 1) {
return $node;
}
else {
mailcomment_error(MAILCOMMENT_ERROR_PARAMS, $node, $header);
}
}
}
/**
* Handle errors and bounce mail when authentication or validation fail
*
* We handle the incoming email carefully and don't add any user data in the response
* because everything in the mail can be forged.
*
* @param $error
* Error code
* @param $node
* Node object
* @param $header
* Message headers
*/
function mailcomment_error($error, $node, $header, $params = array()) {
$text_vars = array(
'!site' => variable_get('site_name', 'Drupal'),
'@subject' => $header->subject,
'@to' => $header->toaddress,
'@from' => $header->fromaddress,
);
$reply = !empty($header->reply_to[0]->mailbox) ? $header->reply_to[0]->mailbox . '@' . $header->reply_to[0]->host
: $header->from[0]->mailbox . '@' . $header->from[0]->host;
$message = array();
switch ($error) {
case MAILCOMMENT_ERROR_SIGNATURE:
watchdog('mailcomment', 'Received an email without signed parameters from @from: @subject', $text_vars, WATCHDOG_WARNING);
break;
case MAILCOMMENT_ERROR_EXPIRED:
watchdog('mailcomment', 'Received an email with a expired signature from @from: @subject', $text_vars);
break;
case MAILCOMMENT_ERROR_USER:
watchdog('mailcomment', 'Received an e-mail without a valid user id from @from: @subject', $text_vars, WATCHDOG_WARNING);
break;
case MAILCOMMENT_ERROR_PARAMS:
default:
watchdog('mailcomment', 'Received an email with no parameters from @from: @subject', $text_vars, WATCHDOG_WARNING);
break;
}
// Send out bounce mail only if the mail address is valid and the feature is enabled.
// @ TODO Reply using the original messaging mail method used.
if ($reply && variable_get('mailcomment_error_bounce', 1) && valid_email_address($reply)) {
if ($node->uid && ($account = user_load(array('uid' => $node->uid)))) {
$language = user_preferred_language($account);
}
else {
$language = language_default();
}
$params['error'] = $error;
$params['text_vars'] = $text_vars;
drupal_mail('mailcomment', 'bounce', $reply, $language, $params);
}
}
/**
* Implementation of hook_mail().
*/
function mailcomment_mail($key, &$message, $params) {
$language = $message['language'];
$text_vars = $params['text_vars'];
$message['subject'] = t('There was a problem with your email to !site (@subject)', $text_vars, $language->language);
// The error code will determine the main text
switch ($params['error']) {
case MAILCOMMENT_ERROR_SIGNATURE:
$text[] = t('The email you sent to @to was rejected because there was a signature error.', $text_vars, $language->language);
break;
case MAILCOMMENT_ERROR_EXPIRED:
$text[] = t('The email you sent to @to was rejected because it was sent after the allowed response time for the original email.', $text_vars, $language->language);
break;
case MAILCOMMENT_ERROR_USER:
$text[] = t('The email you sent to @to was rejected because we couldn\'t authenticate it.', $text_vars, $language->language);
break;
case MAILCOMMENT_ERROR_PARAMS:
default:
$text[] = t('The email you sent to @to was rejected because there was a validation error.', $text_vars, $language->language);
break;
}
// More explanatory information
$text[] = ''; // Blank line
$text[] = t('In order for emails to be accepted by !site:', $text_vars, $language->language);
$text[] = t('- They must be sent in reply to a valid notification email.', array(), $language->language);
$text[] = t('- The reply must be done from the same email address the notification was sent to.', array(), $language->language);
if ($expire = variable_get('mailcomment_expiration', 0)) {
$text[] = t('- You can only reply within the time allotted by the system which is @expiration', array('@expiration' => format_interval($expire, 2, $language->language)), $language->language);
}
// Add node link if we have it
if (!empty($params['nid'])) {
$text[] = ''; // Blank line
$text[] = t('You may post comments directly by visiting !node-url', array('!node-url' => url('node/' . $params['nid'], NULL, NULL, TRUE)), $language->language);
}
$message['body'] = implode("\n", $text);
}
/**
* Build messageid embedding the parameters
*
* Not all chars are valid for our message-id, as some of them cause the PHP imap
* functions to retrieve an empty In-Reply-To header.
*
* Valid formats: numbers separated by dots
*/
function mailcomment_build_messageid($params) {
$elements = array($params['uid'], $params['nid'], $params['cid'], $params['time']);
// Add signature
$elements[] = mailcomment_signature($elements);
return '<'. implode('.', $elements) .'@'. variable_get('mailcomment_server_string', 'example.com') .'>';
}
/**
* Gets the mail comment parameters based on mail comment signature
*
* It will check the digital signature and only return parameters if they match
*
* @param $identifier
* String - the mail comment signature string
*
* @return
* Array of parameters.
*/
function mailcomment_check_messageparams($identifier) {
if ($params = mailcomment_parse_messageparams($identifier)) {
$signature = $params['signature'];
unset($params['signature']);
// Check digital signature and expiration time if set
if ($signature && $signature == mailcomment_signature($params)) {
// Check signature has not expired
if (($expire = variable_get('mailcomment_expiration', 0)) && $params['time'] + $expire < time()) {
$params['error'] = MAILCOMMENT_ERROR_EXPIRED;
}
return $params;
}
else {
$params['error'] = MAILCOMMENT_ERROR_SIGNATURE;
}
}
else {
$params['error'] = MAILCOMMENT_ERROR_PARAMS;
return $params;
}
}
/**
* Parse message id into parameters
*
* The message id should have this form:
* uid.nid.cid.time.signature@server string
* @param $messageid
* Incoming message id
*
* @return
* Array of mail comment parameters parsed from signature string
*/
function mailcomment_parse_messageparams($messageid) {
$params = array();
$parts = explode('@', $messageid);
if (count($parts) == 2 && $parts[1] == variable_get('mailcomment_server_string', 'example.com')) {
$parts = explode('.', $parts[0]);
if (count($parts) == 5) {
$params['uid'] = $parts[0];
$params['nid'] = $parts[1];
$params['cid'] = $parts[2];
$params['time'] = $parts[3];
$params['signature'] = $parts[4];
}
}
return $params;
}
/**
* Produce / verify digital signature
*/
function mailcomment_signature($params) {
$params[] = drupal_get_private_key();
return md5(implode('-', $params));
}
/**
* Determines whether the mail comment signature exists in the body
*
* @param $body
* raw email body
*
* @return
* String signature or FALSE if not found.
*/
function _mailcomment_get_signature($body) {
if (preg_match("/([0-9]+\.){4}+[a-z0-9]+@([a-z0-9])([-a-z0-9_])+([a-z0-9])(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*/i", $body, $matches)) {
// Check to make sure the match has our mailcomment domain
$domain = variable_get('mailcomment_server_string', 'example.com');
if (strpos($matches[0], $domain)) {
$signature = $matches[0];
// We have a match for the messageid and the mailcomment domain string so we are good
return $signature;
}
}
else {
// No matches found so assume we have no messageid and node->threading is not set
return FALSE;
}
}
/**
* Invoke any hook_mailcomment_alter operations in all modules. See mailcomment_mailhandler
*
* @param $node
* A node object.
* @param $op
* Operation. Implemented operations are 'pre' and 'post'
* 'pre' is run before the incoming email had been altered/stripped of junk.
* 'post' is run after everything below the delimiter has been stripped.
* @return An array. The node is passed by reference and therefore can be altered.
*/
function mailcomment_invoke_mailcomment_alter(&$node, $op) {
$return = array();
foreach (module_implements('mailcomment_alter') as $name) {
$function = $name .'_mailcomment_alter';
$result = $function($node, $op);
if (isset($result) && is_array($result)) {
$return = array_merge($return, $result);
}
elseif (isset($result)) {
$return[] = $result;
}
}
return $return;
}
/**
* Implementation of hook_mailhandler_authenticate_info()
*/
function mailcomment_mailhandler_authenticate_info() {
$info = array(
'mailcomment_default' => array(
'title' => 'MailComment Default',
'description' => 'Checks whether the sender matches a valid user in the database',
'callback' => 'mailcomment_authenticate_default',
'module' => 'mailhandler',
'extension' => NULL,
'basename' => NULL,
)
);
return $info;
}
/**
* Authenticates a message based on mailcomment parameters or tokenauth token.
*
* @param $node
* Object - a node
* @param $header
* Object - message header information
* @param $origbody
* String - body of message
* @param $mailbox
* Array - mailbox configuration
*
* @return Object - a node
*/
function mailcomment_authenticate_default($node, $header, $origbody, $mailbox) {
// Check and parse messageid for parameters. URL will be encoded.
$identifier = _mailcomment_get_signature(rawurldecode($node->body));
$params = mailcomment_check_messageparams($identifier);
// Assign the params to the node object.
$node->mailcomment['params'] = $params;
if ($params['uid']) {
$account = user_load($params['uid']);
$node->uid = $account->uid;
$node->name = $account->name;
}
// If user is not determined from parameters, given tokenauth a chance
elseif (function_exists('mailhandler_authenticate_tokenauth')) {
mailhandler_authenticate_tokenauth($node, $header, $oridbody, $mailbox);
}
if (!$node->uid && variable_get('mailcomment_passthru', 0) && variable_get('mailcomment_passthru_authentication', '')) {
module_load_include('inc', 'mailhandler', 'mailhandler.retrieve');
mailhandler_mailhandler_authenticate('execute', variable_get('mailcomment_passthru_authentication', ''), array($node, $header, $origbody, $mailbox));
}
return $node;
}
/**
* Returns array of hostnames based on mailboxes configured as catchall addresses.
*
* @return Array of hostnames, excluding TLD
*/
function _mailcomment_catchall_hosts() {
$hosts = array();
$catchalls = variable_get('mailcomment_mailboxes_catchalls', '');
foreach ($catchalls as $mid) {
$catchall = db_result(db_query("SELECT mail FROM {mailhandler} WHERE mid = %d", $mid));
$hosts[$catchall] = substr($catchall, strpos($catchall, "@") +1);
}
return $hosts;
}
/**
* Redirect request to a node and comment anchor
*
* A link is added to the message body which contains the mail comment signature.
* If this link is clicked it will reach this callback which will redirect the request
* to the original node and comment anchor, if applicable.
*
* @param $string
* String - the mail comment signature
*
* @return drupal_goto
*/
function mailcomment_message_redirect($string) {
$params = mailcomment_parse_messageparams(str_replace(array("<", ">"), "", $string));
$params['cid'] ? $fragment = '#comment-' . $params['cid'] : $fragment = NULL;
drupal_goto('node/' . $params['nid'], NULL, $fragment);
}
/**
* Returns Message-ID for the immediate ancestor of a comment.
*
* The parent node is considered an ancestor with a cid value of zero.
*
* @param $nid
* Primary key of comment for which ancestors are sought.
* @param $cid
* Primary key of comment for which ancestors are sought.
*/
function mailcomment_mail_comment_ancestor_message_id($nid, $cid) {
$msg_ids = array();
if ($cid != 0) {
// Every comment has a node ancestor.
$pid = db_result(db_query("SELECT pid from {comments} WHERE cid = %d", $cid));
if ($pid) {
// A positive pid means this is a comment. If it's 0 that's a node.
$query = "SELECT uid, nid, cid, timestamp as time FROM {comments} WHERE cid = %d";
$result = db_fetch_array(db_query($query, $pid));
return mailcomment_build_messageid($result);
}
else {
$query = "SELECT uid, nid, 0 as cid, created as time FROM {node} WHERE nid = %d";
$result = db_fetch_array(db_query($query, $nid));
return mailcomment_build_messageid($result);
}
}
return '';
}