--- casetracker_mail.module	2007-12-14 22:40:23.000000000 +0000
+++ casetracker_mail.module.new	2007-12-14 22:45:31.000000000 +0000
@@ -1,250 +1,254 @@
-<?php
-// $Id: casetracker_mail.module,v 1.10 2006/12/19 03:53:43 morbus Exp $
-
-/**
- * @file
- * Enables mail sending and Mailhandler intregation for Case Tracker.
- */
-
-/**
- * Implementation of hook_help().
- */
-function casetracker_mail_help($section) {
-  switch ($section) {
-    case 'admin/modules#description':
-      return t('Enables mail sending and Mailhandler integation for Case Tracker.');
-
-    case 'admin/settings/casetracker_mail':
-      return '<p>'.t('You may configure the various Case Tracker mail options below.').'</p>';
-  }
-}
-
-/**
- * Sends out emails. Woot! Do people still say woot? Man, I'm old.
- *
- * @param $node
- *   The case $node object that is being inserted or modified.
- * @param $comment
- *   The $comment array, passed only if this a comment has been left.
- */
-function casetracker_mail_send($case, $comment = NULL) {
-  $project = db_fetch_object(db_query("SELECT cp.project_number, n.title FROM {casetracker_project} cp LEFT JOIN {node} n ON (n.vid = cp.vid) WHERE cp.nid = %d", $case->pid));
-
-  $variables = array(
-    '%project_id'       => $case->pid,
-    '%project_number'   => $project->project_number,
-    '%project_title'    => $project->title,
-    '%case_id'          => $case->nid,
-    '%case_number'      => $project->project_number .'-'. $case->case_number,
-    '%case_title'       => $case->title,
-    '%case_type'        => casetracker_case_state_load('type', $case->case_type_id),
-    '%case_priority'    => casetracker_case_state_load('priority', $case->case_priority_id),
-    '%case_status'      => casetracker_case_state_load('status', $case->case_status_id),
-    '%case_assigned'    => is_numeric($case->assign_to) ? casetracker_get_name($case->assign_to) : $case->assign_to,
-    '%case_author'      => casetracker_get_name($case->uid),
-    '%case_created'     => format_date($case->created, 'large'),
-    '%case_changed'     => format_date($case->changed, 'large'),
-    '%case_url'         => url('node/'.$case->nid, NULL, NULL, TRUE),
-    '%case_description' => $case->body, // @todo fails for CCK or non-body cases.
-    '%comment'          => NULL,
-  );
-
-  if (isset($comment)) { // make a master %comment variable that contains the values of this specific comment.
-    // @todo it'd be nice if we could display all the previous comments, as the project.module currently does.
-    $variables['%comment'] = strtr(variable_get('casetracker_mail_comment_message', _casetracker_mail_comment_message()), array(
-      '%comment_author'      => casetracker_get_name($comment['uid']),
-      '%comment_title'       => $comment['subject'],
-      '%comment_description' => $comment['comment'],
-    ));
-  }
-
-  // make our own message ID so we can log it and allow responses via mailhandler.
-  $msg_id = '<'. time() .'.'. mt_rand() .'@'. drupal_strtolower($_SERVER['SERVER_NAME']) .'>';
-  $headers  = "From: ". variable_get('casetracker_mail_address', variable_get('site_mail', ini_get('sendmail_from'))) ."\n";
-  $headers .= "Message-ID: ". $msg_id ."\n"; // this is the most exciting bit of code you've ever seen >_< fear my anime smileyfacicons!
-  $subject = strtr(variable_get('casetracker_mail_subject', _casetracker_mail_subject()), $variables);
-  $body    = strtr(variable_get('casetracker_mail_case_message', _casetracker_mail_case_message()), $variables);
-  db_query("INSERT INTO {casetracker_mail} (msg_id, nid, cid) VALUES ('%s', %d, %d)", $msg_id, $case->nid, isset($comment['cid']) ? $comment['cid'] : 0);
-
-  // @todo this currently sends to only author and assigned. there needs to be
-  // finer-grain control here, like an OG subscribers or all commenters, etc.
-  $results = db_query("SELECT mail FROM {users} WHERE uid IN (%d, %d, %d)", $comment['uid'], $case->uid, is_numeric($case->assign_to) ? $case->assign_to : casetracker_get_uid($case->assign_to));
-  while ($result = db_fetch_object($results)) {
-    if (!$result->mail) { continue; } // don't fire blanks.
-    $mail_status = user_mail($result->mail, $subject, $body, $headers);
-    if (!$mail_status) { // mail failure doesn't actually tell us much, since PHP returns no error string, but hey, feel good, right?
-      watchdog('casetracker_mail', t('E-mail notification failed for %address.', array('%address' => theme('placeholder', $result->mail))));
-    }
-  }
-}
-
-/**
- * Implementation of hook_comment().
- */
-function casetracker_mail_comment(&$comment, $op) {
-  switch ($op) {
-    case 'insert':
-      $node = node_load($comment['nid']); // checks type and compares existing case meta to submitted.
-      if (!in_array($node->type, variable_get('casetracker_case_node_types', array('casetracker_case')), TRUE)) {
-        return; // if this isn't a casetracker case node type, return without sullying our miserable code. MISERY!
-      } // NP: 'Caught' from Stu Phillips's album 'Knight Rider: The Stu Phillips Scores: Original Television Soundtrack'.
-
-      casetracker_mail_send($node, $comment);
-      break;
-
-    case 'delete':
-      db_query('DELETE FROM {casetracker_mail} WHERE cid = %d', $comment['cid']);
-      break;
-  }
-}
-
-/**
- * Implementation of hook_nodeapi().
- */
-function casetracker_mail_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
-  switch ($op) {
-    case 'insert':
-      if (in_array($node->type, variable_get('casetracker_case_node_types', array('casetracker_case')), TRUE)) {
-        casetracker_mail_send($node);
-      } break;
-
-    case 'delete':
-      if (in_array($node->type, variable_get('casetracker_case_node_types', array('casetracker_case')), TRUE)) {
-        db_query('DELETE FROM {casetracker_mail} WHERE nid = %d', $node->nid);
-      } break;
-  }
-}
-
-/**
- * Implementation of hook_mailhandler().
- *
- *   project_number: 500
- *   type: casetracker_case
- *   case_title: This is a case title! Yes!
- *   assign_to: Morbus Iff
- *   case_status: open
- *   case_priority: 1-high
- *   case_type: bug
- */
-function casetracker_mailhandler($node, $result, $msg_number, $header, $mailbox) {
-  $source_msg_id = $header->references; // used for comment replies. actually
-  // dependent on a few assumptions: the angle brackets are stored in the
-  // casetracker_mail table and that a reply can never be replied too. both
-  // are accurate, but it's not the most flexible of designs.
-
-  // determine the right values based on those in the email.
-  $node->title     = $node->case_title ? $node->case_title : $node->title;
-  $node->assign_to = $node->assign_to  ? $node->assign_to  : $node->uid;
-  foreach (array('priority', 'status', 'type') as $state) {
-    $options = casetracker_case_state_load($state);
-    $node->{'case_'.$state.'_id'} = $node->{'case_'.$state}
-      ? db_result(db_query("SELECT csid FROM {casetracker_case_states} WHERE case_state_name = LOWER('%s') AND case_state_realm = '%s'", drupal_strtolower($node->{'case_'.$state}), $state))
-      : variable_get('casetracker_default_case_'.$state, array_shift(array_keys($options)));
-  }
-
-  // @todo potential hack for CCK fields: explode on [ and make
-  // a new array level for every one you see. maybe, maybe.
-
-  $node->pid = db_result(db_query("SELECT nid FROM {casetracker_project} WHERE project_number = %d", $node->project_number));
-  if ($node->pid && !$source_msg_id) { // if we've got a project number, and this isn't a reply, make a new case.
-    $node->status = 1; return $node; // we'll publish the node by default, but @todo this should be configurable.
-  }
-  elseif ($source_msg_id) {
-    $comment = array(); // a source message ID exists, so this is a comment via email.
-    $result = db_fetch_object(db_query("SELECT msg_id, nid, cid FROM {casetracker_mail} WHERE msg_id = '%s'", $source_msg_id));
-    $case                        = node_load($result->nid);
-    $comment['nid']              = $result->nid;
-    $comment['pid']              = $result->cid;
-    $comment['comment']          = $node->body;
-    $comment['uid']              = $node->uid;
-    $comment['subject']          = $node->title;
-    $comment['case_priority_id'] = $case->case_priority_id;
-    $comment['case_type_id']     = $case->case_type_id;
-    $comment['case_status_id']   = $case->case_status_id;
-    $comment['case_title']       = $case->title;
-    $comment['pid']              = $case->pid;
-    $comment['assign_to']        = casetracker_get_name($case->assign_to);
-    // @todo allow emailed comments to change these states.
-    comment_save($comment);
-  }
-}
-
-/**
- * Implementation of hook_settings().
- */
-function casetracker_mail_settings() {
-  $form = array();
-
-  $form['casetracker_mail'] = array(
-    '#type'          => 'fieldset',
-    '#title'         => t('E-mail settings'),
-    '#collapsible'   => TRUE,
-    '#collapsed'     => FALSE,
-    '#description'   => t('Enter the From address, subject, case message, and comment message for Case Tracker generated mails. Available variables are %project_id (the node ID), %project_number, %project_title, %case_id (the node ID), %case_number, %case_title.'),
-  );
-  $form['casetracker_mail']['casetracker_mail_address'] = array(
-    '#type'          => 'textfield',
-    '#title'         => t('E-mail address'),
-    '#default_value' => variable_get('casetracker_mail_address', variable_get('site_mail', ini_get('sendmail_from'))),
-    '#description'   => t('A valid e-mail address used in the From: of all Case Tracker generated mails.'),
-  );
-  $form['casetracker_mail']['casetracker_mail_subject'] = array(
-    '#type'          => 'textfield',
-    '#title'         => t('Subject'),
-    '#default_value' => variable_get('casetracker_mail_subject', _casetracker_mail_subject()),
-    '#description'   => t('Enter the subject of all Case Tracker generated mails.'),
-  );
-  $form['casetracker_mail']['casetracker_mail_case_message'] = array(
-    '#rows'          => 15,
-    '#type'          => 'textarea',
-    '#title'         => t('Case message'),
-    '#default_value' => variable_get('casetracker_mail_case_message', _casetracker_mail_case_message()),
-    '#description'   => t('Enter the case message body of Case Tracker generated mails. Apart from the variables mentioned above, additional variables include %case_type, %case_priority, %case_status, %case_assigned, %case_author, %case_created, %case_changed, %case_url, %case_description, %comment (see below for the value of the %comment variable).'),
-  ); // @todo it is not exactly clear how CCK node types (or other custom node fields) will be available as %variables.
-  // @todo it also doesn't appear like we can give a list of all other comments made on this particular case.
-  $form['casetracker_mail']['casetracker_mail_comment_message'] = array(
-    '#rows'          => 10,
-    '#type'          => 'textarea',
-    '#title'         => t('Comment message'),
-    '#default_value' => variable_get('casetracker_mail_comment_message', _casetracker_mail_comment_message()),
-    '#description'   => t('Enter the comment message body of Case Tracker generated mails. The %comment variable in the \'Case message\' text above will be replaced by this value (if applicable). Available additional variables are %comment_author, %comment_title, %comment_description.'),
-  );
-
-  return $form;
-}
-
-/**
- * Returns the default message for Case Tracker case mails.
- */
-function _casetracker_mail_case_message() {
-  return
-    "Case status update for %case_url\n\n".
-    "Project:          %project_title\n".
-    "Type:             %case_type\n".
-    "Priority:         %case_priority\n".
-    "Status:           %case_status\n".
-    "Assigned:         %case_assigned\n".
-    "Opened by:        %case_author\n".
-    "Opened on:        %case_created\n".
-    "Last modified:    %case_changed\n\n".
-    "%case_description\n\n".
-    "%comment\n"
-  ;
-}
-
-function _casetracker_mail_comment_message() {
-  return // @todo it'd be nice if this looked like the project.module with a date.
-    "------------------------------------------------------------------------\n\n".
-    "Comment by:       %comment_author\n" .
-    "Comment title:    %comment_title\n\n".
-    "%comment_description\n\n"
-  ;
-}
-
-/**
- * Returns the default subject line for Case Tracker mails.
- */
-function _casetracker_mail_subject() {
-  return '[%project_title %case_type %case_number] %case_title';
-}
-
+<?php
+// $Id: casetracker_mail.module,v 1.10 2006/12/19 03:53:43 morbus Exp $
+
+/**
+ * @file
+ * Enables mail sending and Mailhandler intregation for Case Tracker.
+ */
+
+/**
+ * Implementation of hook_help().
+ */
+function casetracker_mail_help($section) {
+  switch ($section) {
+    case 'admin/modules#description':
+      return t('Enables mail sending and Mailhandler integation for Case Tracker.');
+
+    case 'admin/settings/casetracker_mail':
+      return '<p>'.t('You may configure the various Case Tracker mail options below.').'</p>';
+  }
+}
+
+/**
+ * Sends out emails. Woot! Do people still say woot? Man, I'm old.
+ *
+ * @param $node
+ *   The case $node object that is being inserted or modified.
+ * @param $comment
+ *   The $comment array, passed only if this a comment has been left.
+ */
+function casetracker_mail_send($case, $comment = NULL) {
+  $project = db_fetch_object(db_query("SELECT cp.project_number, n.title FROM {casetracker_project} cp LEFT JOIN {node} n ON (n.vid = cp.vid) WHERE cp.nid = %d", $case->pid));
+
+  $variables = array(
+    '%project_id'       => $case->pid,
+    '%project_number'   => $project->project_number,
+    '%project_title'    => $project->title,
+    '%case_id'          => $case->nid,
+    '%case_number'      => $project->project_number .'-'. $case->case_number,
+    '%case_title'       => $case->title,
+    '%case_type'        => casetracker_case_state_load('type', $case->case_type_id),
+    '%case_priority'    => casetracker_case_state_load('priority', $case->case_priority_id),
+    '%case_status'      => casetracker_case_state_load('status', $case->case_status_id),
+    '%case_assigned'    => is_numeric($case->assign_to) ? casetracker_get_name($case->assign_to) : $case->assign_to,
+    '%case_author'      => casetracker_get_name($case->uid),
+    '%case_created'     => format_date($case->created, 'large'),
+    '%case_changed'     => format_date($case->changed, 'large'),
+    '%case_url'         => url('node/'.$case->nid, NULL, NULL, TRUE),
+    '%case_description' => $case->body, // @todo fails for CCK or non-body cases.
+    '%comment'          => NULL,
+  );
+
+  if (isset($comment)) { // make a master %comment variable that contains the values of this specific comment.
+    // @todo it'd be nice if we could display all the previous comments, as the project.module currently does.
+    $variables['%comment'] = strtr(variable_get('casetracker_mail_comment_message', _casetracker_mail_comment_message()), array(
+      '%comment_author'      => casetracker_get_name($comment['uid']),
+      '%comment_title'       => $comment['subject'],
+      '%comment_description' => $comment['comment'],
+    ));
+  }
+
+  // make our own message ID so we can log it and allow responses via mailhandler.
+  $msg_id = '<'. time() .'.'. mt_rand() .'@'. drupal_strtolower($_SERVER['SERVER_NAME']) .'>';
+  $headers  = "From: ". variable_get('casetracker_mail_address', variable_get('site_mail', ini_get('sendmail_from'))) ."\n";
+  $headers .= "Message-ID: ". $msg_id ."\n"; // this is the most exciting bit of code you've ever seen >_< fear my anime smileyfacicons!
+  $subject = strtr(variable_get('casetracker_mail_subject', _casetracker_mail_subject()), $variables);
+  $body    = strtr(variable_get('casetracker_mail_case_message', _casetracker_mail_case_message()), $variables);
+  db_query("INSERT INTO {casetracker_mail} (msg_id, nid, cid) VALUES ('%s', %d, %d)", $msg_id, $case->nid, isset($comment['cid']) ? $comment['cid'] : 0);
+
+  // @todo this currently sends to only author and assigned. there needs to be
+  // finer-grain control here, like an OG subscribers or all commenters, etc.
+  $results = db_query("SELECT mail FROM {users} WHERE uid IN (%d, %d, %d)", $comment['uid'], $case->uid, is_numeric($case->assign_to) ? $case->assign_to : casetracker_get_uid($case->assign_to));
+  while ($result = db_fetch_object($results)) {
+    if (!$result->mail) { continue; } // don't fire blanks.
+    $mail_status = user_mail($result->mail, $subject, $body, $headers);
+    if (!$mail_status) { // mail failure doesn't actually tell us much, since PHP returns no error string, but hey, feel good, right?
+      watchdog('casetracker_mail', t('E-mail notification failed for %address.', array('%address' => theme('placeholder', $result->mail))));
+    }
+  }
+}
+
+/**
+ * Implementation of hook_comment().
+ */
+function casetracker_mail_comment(&$comment, $op) {
+  switch ($op) {
+    case 'insert':
+      $node = node_load($comment['nid']); // checks type and compares existing case meta to submitted.
+      if (!in_array($node->type, variable_get('casetracker_case_node_types', array('casetracker_case')), TRUE)) {
+        return; // if this isn't a casetracker case node type, return without sullying our miserable code. MISERY!
+      } // NP: 'Caught' from Stu Phillips's album 'Knight Rider: The Stu Phillips Scores: Original Television Soundtrack'.
+
+      casetracker_mail_send($node, $comment);
+      break;
+
+    case 'delete':
+      db_query('DELETE FROM {casetracker_mail} WHERE cid = %d', $comment['cid']);
+      break;
+  }
+}
+
+/**
+ * Implementation of hook_nodeapi().
+ */
+function casetracker_mail_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
+  switch ($op) {
+    case 'insert':
+      if (in_array($node->type, variable_get('casetracker_case_node_types', array('casetracker_case')), TRUE)) {
+        casetracker_mail_send($node);
+      } break;
+
+    case 'delete':
+      if (in_array($node->type, variable_get('casetracker_case_node_types', array('casetracker_case')), TRUE)) {
+        db_query('DELETE FROM {casetracker_mail} WHERE nid = %d', $node->nid);
+      } break;
+  }
+}
+
+/**
+ * Implementation of hook_mailhandler().
+ *
+ *   project_number: 500
+ *   type: casetracker_case
+ *   case_title: This is a case title! Yes!
+ *   assign_to: Morbus Iff
+ *   case_status: open
+ *   case_priority: 1-high
+ *   case_type: bug
+ */
+function casetracker_mailhandler($node, $result, $msg_number, $header, $mailbox) {
+  $source_msg_id = $header->references; // used for comment replies. actually
+  // dependent on a few assumptions: the angle brackets are stored in the
+  // casetracker_mail table and that a reply can never be replied too. both
+  // are accurate, but it's not the most flexible of designs.
+
+  // determine the right values based on those in the email.
+  $node->title     = $node->case_title ? $node->case_title : $node->title;
+  $node->assign_to = $node->assign_to  ? $node->assign_to  : $node->uid;
+  foreach (array('priority', 'status', 'type') as $state) {
+    $options = casetracker_case_state_load($state);
+    $node->{'case_'.$state.'_id'} = $node->{'case_'.$state}
+      ? db_result(db_query("SELECT csid FROM {casetracker_case_states} WHERE case_state_name = LOWER('%s') AND case_state_realm = '%s'", drupal_strtolower($node->{'case_'.$state}), $state))
+      : variable_get('casetracker_default_case_'.$state, array_shift(array_keys($options)));
+  }
+
+  // @todo potential hack for CCK fields: explode on [ and make
+  // a new array level for every one you see. maybe, maybe.
+
+  $node->pid = db_result(db_query("SELECT nid FROM {casetracker_project} WHERE project_number = %d", $node->project_number));
+  if ($node->pid && !$source_msg_id) { // if we've got a project number, and this isn't a reply, make a new case.
+    $node->status = 1; return $node; // we'll publish the node by default, but @todo this should be configurable.
+  }
+  elseif ($source_msg_id) {
+    $comment = array(); // a source message ID exists, so this is a comment via email.
+    $result = db_fetch_object(db_query("SELECT msg_id, nid, cid FROM {casetracker_mail} WHERE msg_id = '%s'", $source_msg_id));
+    $case                        = node_load($result->nid);
+    $comment['nid']              = $result->nid;
+    $comment['pid']              = $result->cid;
+    $comment['comment']          = $node->body;
+    $comment['uid']              = $node->uid;
+    $comment['subject']          = $node->title;
+    $comment['case_priority_id'] = $case->case_priority_id;
+    $comment['case_type_id']     = $case->case_type_id;
+    $comment['case_status_id']   = $case->case_status_id;
+    $comment['case_title']       = $case->title;
+    $comment['pid']              = $case->pid;
+    $comment['assign_to']        = casetracker_get_name($case->assign_to);
+    // @todo allow emailed comments to change these states.
+    comment_save($comment);
+  }
+  // else (if no pid) then pass the node back for default processing)
+  else {
+  	return $node;
+  }
+}
+
+/**
+ * Implementation of hook_settings().
+ */
+function casetracker_mail_settings() {
+  $form = array();
+
+  $form['casetracker_mail'] = array(
+    '#type'          => 'fieldset',
+    '#title'         => t('E-mail settings'),
+    '#collapsible'   => TRUE,
+    '#collapsed'     => FALSE,
+    '#description'   => t('Enter the From address, subject, case message, and comment message for Case Tracker generated mails. Available variables are %project_id (the node ID), %project_number, %project_title, %case_id (the node ID), %case_number, %case_title.'),
+  );
+  $form['casetracker_mail']['casetracker_mail_address'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('E-mail address'),
+    '#default_value' => variable_get('casetracker_mail_address', variable_get('site_mail', ini_get('sendmail_from'))),
+    '#description'   => t('A valid e-mail address used in the From: of all Case Tracker generated mails.'),
+  );
+  $form['casetracker_mail']['casetracker_mail_subject'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('Subject'),
+    '#default_value' => variable_get('casetracker_mail_subject', _casetracker_mail_subject()),
+    '#description'   => t('Enter the subject of all Case Tracker generated mails.'),
+  );
+  $form['casetracker_mail']['casetracker_mail_case_message'] = array(
+    '#rows'          => 15,
+    '#type'          => 'textarea',
+    '#title'         => t('Case message'),
+    '#default_value' => variable_get('casetracker_mail_case_message', _casetracker_mail_case_message()),
+    '#description'   => t('Enter the case message body of Case Tracker generated mails. Apart from the variables mentioned above, additional variables include %case_type, %case_priority, %case_status, %case_assigned, %case_author, %case_created, %case_changed, %case_url, %case_description, %comment (see below for the value of the %comment variable).'),
+  ); // @todo it is not exactly clear how CCK node types (or other custom node fields) will be available as %variables.
+  // @todo it also doesn't appear like we can give a list of all other comments made on this particular case.
+  $form['casetracker_mail']['casetracker_mail_comment_message'] = array(
+    '#rows'          => 10,
+    '#type'          => 'textarea',
+    '#title'         => t('Comment message'),
+    '#default_value' => variable_get('casetracker_mail_comment_message', _casetracker_mail_comment_message()),
+    '#description'   => t('Enter the comment message body of Case Tracker generated mails. The %comment variable in the \'Case message\' text above will be replaced by this value (if applicable). Available additional variables are %comment_author, %comment_title, %comment_description.'),
+  );
+
+  return $form;
+}
+
+/**
+ * Returns the default message for Case Tracker case mails.
+ */
+function _casetracker_mail_case_message() {
+  return
+    "Case status update for %case_url\n\n".
+    "Project:          %project_title\n".
+    "Type:             %case_type\n".
+    "Priority:         %case_priority\n".
+    "Status:           %case_status\n".
+    "Assigned:         %case_assigned\n".
+    "Opened by:        %case_author\n".
+    "Opened on:        %case_created\n".
+    "Last modified:    %case_changed\n\n".
+    "%case_description\n\n".
+    "%comment\n"
+  ;
+}
+
+function _casetracker_mail_comment_message() {
+  return // @todo it'd be nice if this looked like the project.module with a date.
+    "------------------------------------------------------------------------\n\n".
+    "Comment by:       %comment_author\n" .
+    "Comment title:    %comment_title\n\n".
+    "%comment_description\n\n"
+  ;
+}
+
+/**
+ * Returns the default subject line for Case Tracker mails.
+ */
+function _casetracker_mail_subject() {
+  return '[%project_title %case_type %case_number] %case_title';
+}
+
