You are here

support_mailcmd.module in Support Ticketing System 6

Same filename and directory in other branches
  1. 7 support_mailcmd/support_mailcmd.module

Bugzilla style ticket control via email.

File

support_mailcmd/support_mailcmd.module
View source
<?php

/**
 * @file
 * Bugzilla style ticket control via email.
 */

/**
 * Implementation of hook_support_fetch_message_alter().
 */
function support_mailcmd_support_fetch_message_alter(&$message, $client) {
  if (isset($message['uid'])) {
    $account = user_load($message['uid']);
  }
  else {
    $account = support_account_load($message['from'], $message['nid'], $message['subject'], $client);
  }
  $ticket = support_ticket_load($message['nid']);
  $commands = array();
  $body = explode("\n", $message['body']);

  // Following block inspired by parse_mail() subroutine in bugzilla email_in.pl.
  // ------------------------ BEGIN ---------------------
  // Trim leading whitespace
  while (!empty($body) && trim(reset($body)) == '') {
    array_shift($body);
  }
  if (preg_match('/^\\s*@/s', reset($body))) {
    $current_field = NULL;
    while ($line = array_shift($body)) {
      if ($line == '--') {

        // Don't eat sigline
        array_unshift($body, $line);
        break;
      }
      if (trim($line) == '') {

        // Stop when hitting a blank line.
        break;
      }
      $match = array();
      if (preg_match('/^@(\\w+)\\s*(?:=|\\s|$)\\s*(.*)\\s*/', $line, $match)) {
        $current_field = strtolower($match[1]);
        $commands[$current_field] = $match[2];
      }
      else {
        $commands[$current_field] .= " " . $match[2];
      }
    }

    // Remove commands and leading whitespace from message.
    $message['body'] = implode("\n", $body);
  }

  // ------------------------ END ------------------------
  foreach ($commands as $cmd => $value) {
    $tvalue = trim(strtolower($value));
    switch ($cmd) {
      case 'state':
      case 'status':
        if (user_access('can select state', $account) || user_access('administer support', $account) || user_access('can administer state', $account)) {
          $states = _support_states(FALSE, $ticket->state, $account);
          foreach ($states as $k => $v) {
            if (strtolower($v) == $tvalue) {
              $message['state'] = $k;
            }
          }
        }
        break;
      case 'priority':
        if (user_access('can select priority', $account) || user_access('administer support', $account)) {

          // Priorities aren't subject to access control.
          $priorities = _support_priorities();
          foreach ($priorities as $k => $v) {
            if (strtolower($v) == $tvalue) {
              $message['priority'] = $k;
            }
          }
        }
        break;
      case 'client':

        // Currently, switching clients isn't supported well by support.module.
        // It's too easy at the moment to get into a state where the assignee is invalid.
        break;
      case 'assigned':
      case 'assign':
        if (user_access('can assign tickets to self', $account) || user_access('can assign tickets to any user', $account) || user_access('administer support', $account)) {
          $node = node_load($message['nid']);
          $users = _support_assigned(isset($ticket->assigned) ? $ticket->assigned : NULL, $node);
          if (($tvalue == 'unassign' || $tvalue == 'unassigned') && isset($users[0])) {

            // Change the value to match the "not assigned" choice.
            $tvalue = $users[0];
          }
          foreach ($users as $k => $v) {
            if (strtolower($v) == $tvalue) {
              if (!($node && $node->nid)) {

                // For new nodes, override autoassign.
                $message['_support_extra_fields']['assigned'] = $k;
              }
              $message['assigned'] = $k;
            }
          }
        }
        break;
      case 'suppress':
      case 'suppressed':
      case 'suppress_notification':
      case 'supress':
        if (user_access('can suppress notification', $account)) {
          $message['suppress'] = _support_mailcmd_boolean_value($tvalue);
        }
        break;
      case 'notification':
      case 'subscribe':
      case 'subscribed':

        // @@@ What are the *access restrictions* on this activity? The support.module code is unclear.

        //$message['_support_extra_fields']['notification'] = _support_mailcmd_boolean_value($tvalue);
        break;
    }
  }
  drupal_alter('support_mailcmd_command', $message, $client, $account, $commands);
}

/**
 * Convert a textual yes/no into a boolean.
 */
function _support_mailcmd_boolean_value($val) {
  $val = trim(strtolower($val));
  if ($val == 'y' || $val == 'yes' || $val == 'true' || $val == 't' || $val == 'enable' || $val == 'enabled') {
    return TRUE;
  }
  return FALSE;
}

Functions

Namesort descending Description
support_mailcmd_support_fetch_message_alter Implementation of hook_support_fetch_message_alter().
_support_mailcmd_boolean_value Convert a textual yes/no into a boolean.