File: wicked-mail-filter

package info (click to toggle)
php-horde-wicked 2.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,528 kB
  • ctags: 1,236
  • sloc: php: 5,386; xml: 1,027; makefile: 10; sh: 3
file content (122 lines) | stat: -rwxr-xr-x 3,153 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env php
<?php
/*
 * This script accepts a MIME message on standard input and creates a new
 * wiki page from it.  It can also append the e-mail to the end of another
 * page.
 */

function headerValue($headers, $name)
{
    $val = null;
    foreach ($headers as $headerName => $headerVal) {
        if (!strcasecmp($name, $headerName)) {
            if (is_array($headerVal)) {
                $thisVal = join(', ', $headerVal);
            } else {
                $thisVal = $headerVal;
            }
            if (is_null($val)) {
                $val = $thisVal;
            } else {
                $val .= ", " . $thisVal;
            }
        }
    }
    return $val;
}

require_once __DIR__ . '/../lib/Application.php';
Horde_Registry::appInit('wicked', array('cli' => true));

$dateFormat = "F j, Y";
$keepHeaders = array('From', 'To', 'Subject', 'Cc', 'Date');

$text = '';
while (!feof(STDIN)) {
    $text .= fgets(STDIN, 512);
}

if (preg_match("/^(.*?)\r?\n\r?\n/s", $text, $matches)) {
    $hdrText = $matches[1];
} else {
    $hdrText = $text;
}
$message = Horde_Mime_Part::parseMessage($text);
$headers = Horde_Mime_Headers::parseHeaders($hdrText);

// Format the message into a pageBody.
$pageBody = "";
foreach ($headers as $name => $vals) {
    foreach ($keepHeaders as $kh) {
        if (!strcasecmp($kh, $name)) {
            if (is_array($vals)) {
                foreach ($vals as $val) {
                    $pageBody .= "'''" . $name . ":''' " . $val . " _\n";
                }
            } else {
                $pageBody .= "'''" . $name . ":''' " . $vals . " _\n";
            }
        }
    }
}
$pageBody .= "\n\n";

// Create a new name for the page.
$pageName = headerValue($headers, 'Subject');
if (empty($pageName)) {
    $pageName = 'no subject';
}
$pageName .= " -- ";

$msgFrom = headerValue($headers, 'From');
if (preg_match('/^\s*"?(.*?)"?\s*<.*>/', $msgFrom, $matches)) {
    $msgFrom = $matches[1];
} elseif (preg_match('/<(.*)>/', $msgFrom, $matches)) {
    $msgFrom = $matches[1];
}
if (!empty($msgFrom)) {
    $pageName .= $msgFrom . " ";
}

$msgDate = headerValue($headers, 'Date');
if (empty($msgDate)) {
    $time = time();
} else {
    $time = strtotime($msgDate);
}
$pageName .= date($dateFormat, $time);

// We could have two messages with the same name, so append a number.
if ($wicked->pageExists($pageName)) {
    $counter = 2;
    while ($wicked->pageExists($pageName . " (" . $counter . ")")) {
        $counter++;
    }
    $pageName .= " (" . $counter . ")";
}

// Look for a text part.
// FIXME: this is _extremely_ crude.
if ($message->getType() == 'text/plain') {
    $pageBody .= $message->getContents();
} elseif ($message->getType() == 'multipart/alternative') {
    foreach ($message->getParts() as $part) {
        if ($part->getType() == 'text/plain') {
            $pageBody .= $part->getContents();
            break;
        }
    }
} else {
    $pageBody .= "[ Could not render body of message. ]";
}

$pageBody .= "\n";

if (is_null($pageName)) {
    $pageName = "EmailMessage" . ucfirst(md5(uniqid('wicked')));
}

$wicked->newPage($pageName, $pageBody);

exit(0);