forked from zbycz/npress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload-mails.php
151 lines (128 loc) · 3.77 KB
/
load-mails.php
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
<?php
//die;
// WARNING: needs extension=php_imap.dll .. otherwise UTF encoded strings are wrong
define("DONT_RUN_NPRESS_APP", true);
require_once "index.php"; //db connection
error_reporting(0);
if (!function_exists('gzdecode')) {
function gzdecode($data)
{
return gzinflate(substr($data, 10, -8));
}
}
if(isset($_GET['when']) && preg_match('/^[12][90][0-9][0-9]-[0-1][0-9]$/', $_GET['when'])){
//import for requested month
//can be used to import missing emails (waiting for moderator aproval on end of month
// run as: https://openstreetmap.cz/load-mails.php?when=2020-11
$url = "https://lists.openstreetmap.org/pipermail/talk-cz/" . date('Y-F', strtotime($_GET['when']."-10")) . ".txt";
echo "Requested import for ".$_GET['when']." from $url\n";
} else {
// get current mbox archive
$url = "https://lists.openstreetmap.org/pipermail/talk-cz/" . date('Y-F') . ".txt";
}
$mbox = file_get_contents($url);
if (!$mbox) {
$url .= ".gz";
$mbox = gzdecode(file_get_contents($url));
}
/*
encoding is set per mime part in emails, so convert the whole mailbox is
bad idea! */
/*
$mbox_enc=mb_detect_encoding($mbox, ['ASCII', 'UTF-8', 'ISO-8859-2']);
if($mbox_enc == 'ISO-8859-2'){
$mbox = mb_convert_encoding($mbox, 'UTF-8', $mbox_enc);
}
*/
insertMailsFromMbox($mbox);
/*/
set_time_limit(10*60);
// fetch all from 2007-1 til now
for ($y = 2007; $y <= date('Y'); $y++) {
for ($m = 1; $m <= 12; $m++) {
$url = "https://lists.openstreetmap.org/pipermail/talk-cz/" . date('Y-F', strtotime("$y-$m-10")) . ".txt.gz";
echo "<hr>$url<br>";
$mbox = gzdecode(file_get_contents($url));
insertMailsFromMbox($mbox);
flush();
sleep(1);
//TODO encode corectly mails in CP1250
if ($y == date('Y') && $m == date('n')) //current month of current year
break;
}
}
//*/
/**
* @param $mbox plain text format
*/
function insertMailsFromMbox($mbox) //{{{
{
if (!$mbox) {
echo "Blank mbox. End.";
return;
}
foreach (preg_split('/\nFrom .+?\n/', $mbox) as $r) {
$e = new PlancakeEmailParser($r);
if (
dibi::fetch(
"SELECT 1 FROM mailarchive WHERE msgid = %s",
$e->getHeader('message-id')
)
) {
//echo "msgid ".$e->getHeader('message-id')."exists for ".$e->getSubject()." - skipping<br>";
continue;
}
$from = $e->getFieldDecoded('from');
$name = "";
if (preg_match("/(.*) [an][ta] (.*) \((.*)\)/", $from, $matches)) {
$from = "$matches[1]@$matches[2]";
$name = $matches[3];
}
$subject = $e->getSubject();
$subject = preg_replace('/^\[[Tt]alk-cz\] */', '', $subject);
// find conversation
$cid = dibi::fetchSingle(
"
SELECT conversationid
FROM mailarchive
WHERE msgid = %s",
$e->getHeader('in-reply-to'),
"
OR BINARY subject = %s",
$subject
);
if (!$cid) {
$cid = dibi::fetchSingle("SELECT max(conversationid)+1 FROM mailarchive");
}
if (!$cid) {
$cid = 1;
}
if (
$from == "=?UTF-8?Q?Petr_Mor=c3=a1vek_ (=?UTF-8?Q?Petr_Mor=c3=a1vek_)"
) {
// mailman broken
$name = "Petr Morávek [Xificurk]";
$from = "[email protected]";
}
try {
dibi::query("INSERT INTO mailarchive", array(
"msgid" => $e->getHeader('message-id'),
"replyid" => $e->getHeader('in-reply-to'),
"date" => date("Y-m-d H:i:s", strtotime($e->getHeader('date'))),
"from" => $from,
"name" => $name,
"subject" => $subject,
"text" => $e->getPlainBody(),
"conversationid" => $cid
));
} catch(DibiException $e) {
if ($e->getCode() === 1062) {
echo "DUP";
}
else {
throw $e;
}
}
echo ".";
}
} //}}}