/usr/bin/whups-mail-filter is in php-horde-whups 3.0.0~beta1-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | #!/usr/bin/env php
<?php
/**
* This script accepts a MIME message on standard input or from a mail server
* and creates a new ticket from its contents.
*/
function usage()
{
$argv = Console_Getopt::readPHPArgv();
$cmd = basename($argv[0]);
echo <<<EOU
Usage: $cmd [options]
This script parses MIME messages and creates new tickets from their contents.
Single messages can be passed to the script on standard input. If the --mail
parameters are used, all messages from a mail server folder are processed
instead.
Options:
-h, --help Give this help.
-a, --default-auth A default user to set as the ticket requester, if the
requester cannot be determined from the message.
-q, --queue-name The name of the queue where the ticket should be
added.
-Q, --queue-id The (numerical) ID of the queue where the ticket
should be added.
-g, --guess-queue Guess the correct queue name from the subject. If no
(substring) match is found, fall back to -Q or -q.
-k, --ticket Add the message as a comment to this ticket instead
of creating a new ticket.
--mail-host The IMAP/POP3 server to get the messages from.
Defaults to "localhost".
--mail-user The user name for the mail server.
--mail-pass The password for the mail server.
--mail-port The mail server port. Defaults to "143".
--mail-protocol The mail server protocol. Defaults to "imap/notls".
--mail-folder The folder on the mail server. Defaults to "INBOX".
IDs are preferred over names because they are faster to process and avoid
character set ambiguities.
EOU;
}
function _dump($hash)
{
$dump = '';
if (empty($hash)) {
return $dump;
}
$idlen = max(array_map('strlen', array_keys($hash)));
foreach ($hash as $id => $value) {
$dump .= sprintf("\n%${idlen}d: %s", $id, $value);
}
return $dump;
}
if (file_exists(__DIR__ . '/../../whups/lib/Application.php')) {
$baseDir = __DIR__ . '/../';
} else {
require_once 'PEAR/Config.php';
$baseDir = PEAR_Config::singleton()
->get('horde_dir', null, 'pear.horde.org') . '/whups/';
}
require_once $baseDir . 'lib/Application.php';
Horde_Registry::appInit('whups', array('cli' => true));
// Set server name.
$conf['server']['name'] = $conf['mail']['server_name'];
$conf['server']['port'] = $conf['mail']['server_port'];
// Read command-line parameters.
$info = array();
$mail = array('host' => 'localhost',
'pass' => '',
'port' => 143,
'protocol' => 'imap/notls',
'folder' => 'INBOX');
$from_mail = false;
// @TODO: Horde_Argv
$options = Console_Getopt::getopt(Console_Getopt::readPHPArgv(),
'ha:q:Q:gk:',
array('help',
'default-auth=',
'queue-name=', 'queue-id=',
'guess-queue',
'ticket=',
'mail-host=', 'mail-user=',
'mail-pass=', 'mail-port=',
'mail-protocol=', 'mail-folder='));
if (is_a($options, 'PEAR_Error')) {
usage();
$cli->fatal($options->getMessage());
}
// Convert options into a hash. This is possible because all options are only
// allowed once.
$opts_hash = array();
list($opts, $args) = $options;
foreach ($opts as $opt) {
list($optName, $optValue) = $opt;
switch ($optName) {
case 'h': $optName = '--help'; break;
case 'a': $optName = '--default-auth'; break;
case 'k': $optName = '--ticket'; break;
case 'q': $optName = '--queue-name'; break;
case 'Q': $optName = '--queue-id'; break;
case 'g': $optName = '--guess-queue'; break;
}
$opts_hash[$optName] = is_null($optValue) ? true : $optValue;
}
// Process options in this order because some depend on others.
if (isset($opts_hash['--help'])) {
usage();
exit;
}
if (isset($opts_hash['--default-auth'])) {
$info['default_auth'] = $opts_hash['--default-auth'];
$registry->setAuth($info['default_auth'], array());
}
if (isset($opts_hash['--ticket'])) {
$info['ticket'] = (int)$opts_hash['--ticket'];
}
if (isset($opts_hash['--guess-queue'])) {
$info['guess-queue'] = true;
}
if (isset($opts_hash['--queue-name'])) {
$queues = $whups_driver->getQueues();
foreach ($queues as $queueId => $queueName) {
if (strcasecmp($queueName, $opts_hash['--queue-name']) == 0) {
$info['queue'] = $queueId;
break;
}
}
}
if (isset($opts_hash['--queue-id'])) {
$queues = $whups_driver->getQueues();
foreach ($queues as $queueId => $queueName) {
if (strcasecmp($queueId, $opts_hash['--queue-id']) == 0) {
$info['queue'] = $queueId;
break;
}
}
}
foreach (array('host', 'user', 'pass', 'port', 'protocol', 'folder') as $opt) {
if (isset($opts_hash['--mail-' . $opt])) {
$mail[$opt] = $opts_hash['--mail-' . $opt];
}
}
// Sanity check options.
if (empty($info['ticket'])) {
if (empty($info['queue'])) {
usage();
$msg = _("--queue-name or --queue-id must specify a valid and public queue.");
if (isset($queues)) {
$msg .= ' ' . _("Available queues:") . _dump($queues);
}
$cli->fatal($msg);
}
}
// Read and parse the message.
if (empty($mail['user'])) {
try {
Whups_Mail::processMail($cli->readStdin(), $info);
} catch (Whups_Exception $e) {
$cli->fatal($e);
}
} else {
$messages = array();
$imap = imap_open(sprintf('{%s:%d/%s}%s',
$mail['host'],
$mail['port'],
$mail['protocol'],
$mail['folder']),
$mail['user'], $mail['pass']);
if (!$imap) {
$cli->fatal(_("Cannot authenticate at mail server:") . ' ' . implode('; ', imap_errors()));
}
_error();
$mailbox = imap_search($imap, 'ALL', SE_UID);
_error();
if ($mailbox) {
foreach ($mailbox as $uid) {
$message = imap_fetchheader($imap, $uid, FT_UID)
. imap_body($imap, $uid, FT_UID);
try {
Whups_Mail::processMail($message, $info);
imap_delete($imap, $uid, FT_UID);
} catch (Whups_Exception $e) {
$cli->message(_("Error processing message:") . ' ' . $e->getMessage(), 'cli.error');
}
}
}
imap_expunge($imap);
imap_close($imap);
}
exit(0);
|