-
Notifications
You must be signed in to change notification settings - Fork 6
/
news2mail.php
executable file
·188 lines (151 loc) · 5.69 KB
/
news2mail.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
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
#!/usr/bin/php
<?php
define("IN_MYBB", 1);
$basepath = dirname($_SERVER["SCRIPT_FILENAME"]);
require_once $basepath."/../global.php";
require MYBB_ROOT.'/syncom/config.php';
require_once "mybbapi.php";
function message2mail($fid, $tid, $message)
{
global $db, $syncom, $mybb;
$pos = strpos($message, "\r\n\r\n");
if ($pos == 0)
return("");
$header = substr($message, 0, $pos);
$body = substr($message, $pos+4);
$subject = "";
$newsgroups = "";
$lines = explode("\r\n", $header);
$newheader = array();
$inheader = false;
foreach ($lines as $line) {
$ignore = false;
if (! (in_array(substr($line, 0, 1), array("\t", " ")))) $inheader = false;
// Header entfernen
if (strtoupper(substr($line, 0, 18)) == "NNTP-POSTING-DATE:") $ignore = true;
if (strtoupper(substr($line, 0, 5)) == "XREF:") $ignore = true;
if (strtoupper(substr($line, 0, 6)) == "LINES:") $ignore = true;
if (strtoupper(substr($line, 0, 16)) == "X-COMPLAINTS-TO:") $ignore = true;
if ($inheader)
$subject .= $line;
if (strtoupper(substr($line, 0, 8)) == "SUBJECT:") {
$inheader = true;
$subject = substr($line, 9);
$ignore = true;
}
// Header abaendern
if (strtoupper(substr($line, 0, 11)) == "NEWSGROUPS:") {
$newsgroups = trim(substr($line, 11));
$line = "X-".$line;
}
if (strtoupper(substr($line, 0, 7)) == "SENDER:")
$line = "X-".$line;
if (!$ignore)
$newheader[] = $line;
}
// Header hinzufuegen
$query = $db->simple_select("forums", "syncom_newsgroup", "fid=".$db->escape_string($fid), array('limit' => 1));
if (!($forum = $db->fetch_array($query)))
return("");
$group = $forum["syncom_newsgroup"];
$url = $mybb->settings["bburl"];
$newheader[] = "Precedence: list";
//$newheader[] = "To: <".$group."@".$syncom["mailhostname"].">";
$newheader[] = "X-BeenThere: ".$group."@".$syncom["mailhostname"];
//$newheader[] = "Reply-To: ".$group." <".$group."@".$syncom["mailhostname"].">";
$newheader[] = "Reply-To: ".$group."@".$syncom["mailhostname"];
$newheader[] = "List-Id: <".$group.">";
$newheader[] = "List-Unsubscribe: <".$url."/forumdisplay.php?fid=".$fid.">";
$newheader[] = "List-Archive: <".$url."/forumdisplay.php?fid=".$fid.">";
$newheader[] = "List-Post: <mailto:".$group."@".$syncom["mailhostname"].">";
//$newheader[] = "List-Help: <mailto:[email protected]?subject=help>";
$newheader[] = "List-Subscribe: <".$url."/forumdisplay.php?fid=".$fid.">";
//$newheader[] = "Sender: ".$group." <".$group."-bounces@".$syncom["mailhostname"].">";
$newheader[] = "Sender: ".$group."-bounces@".$syncom["mailhostname"];
$newheader[] = "Errors-To: ".$group."-bounces@".$syncom["mailhostname"];
// Crossposts ermitteln
$newgrouplist = array();
$grouplist = explode(",", $newsgroups);
// Nur den ersten Post verarbeiten
if (sizeof($grouplist)>0)
if (trim($grouplist[0]) != $group)
return("@");
foreach ($grouplist as $id=>$groupname) {
if ($group != $groupname)
$newgrouplist[] = $groupname."@".$syncom["mailhostname"];
}
//$list = $group." <".$group."@".$syncom["mailhostname"].">";
$list = $group."@".$syncom["mailhostname"];
if (sizeof($newgrouplist)>0)
$newheader[] = "CC: ".implode(", ", $newgrouplist);
//$list .= ", ".implode(", ", $newgrouplist);
// To-Do:
// - Tag im Subject
// - Footertext
return(array("list"=>$list, "subject"=>$subject, "header"=>implode("\r\n", $newheader), "body"=>$body));
}
function processmail($fid, $tid, $message) {
global $db, $subuser;
$mail = message2mail($fid, $tid, $message);
if ($mail == "")
return(false);
if ($mail == "@")
return(true);
//echo $mail."\n";
//echo "----------------------------------------------------\n";
$user = array();
// Nach Forenabonnenten suchen
$query = $db->simple_select("forumsubscriptions", "uid", "fid=".$db->escape_string($fid));
while ($forensub = $db->fetch_array($query)) {
$user[$forensub["uid"]] = $forensub["uid"];
}
// Nach Threadabonnenten suchen
$query = $db->simple_select("threadsubscriptions", "uid", "tid=".$db->escape_string($tid)." and notification=1");
while ($forensub = $db->fetch_array($query)) {
$user[$forensub["uid"]] = $forensub["uid"];
}
// Keine Abonnenten, also raus
if (sizeof($user) == 0)
return(true);
foreach ($user as $target) {
// Ist der Empfaenger in der Liste der Mailabonnenten
if (array_key_exists($target, $subuser)) {
// Berechtigungen
$fpermissions = forum_permissions($fid, $target);
if ($fpermissions['canviewthreads']) {
// To-Do:
// Mit proc_open das Mailprogramm direkt öffnen, um den Empfänger per Envelope zu übergeben
//if (!mail($mail['list'], $mail['subject'], $mail['body'], $mail['header']."\r\nBCC: ".$subuser[$target]))
if (!imap_mail($mail['list'], $mail['subject'], $mail['body'], $mail['header'], "", $subuser[$target]))
return(false);
echo $target."-".$subuser[$target]."\n";
}
}
}
return(true);
}
function processmails()
{
global $db, $syncom, $subuser;
$subuser = array();
$query = $db->simple_select("users", "uid, email", "syncom_mailinglist");
while ($user = $db->fetch_array($query))
$subuser[$user["uid"]] =$user["email"];
$dir = scandir($syncom['mailout-spool'].'/');
foreach ($dir as $spoolfile) {
$file = $syncom['mailout-spool'].'/'.$spoolfile;
if (!is_dir($file) and (file_exists($file))) {
$message = unserialize(file_get_contents($file));
if (processmail($message["info"]["fid"], $message["info"]["tid"], $message["message"]))
@unlink($file);
else
rename($file, $syncom['mailout-spool'].'/error/'.$spoolfile);
}
}
}
/*$file = "/srv/www/news01/syncom/sample/crosspost2-3.msg";
$message = unserialize(file_get_contents($file));
processmail($message["info"]["fid"], $message["info"]["tid"], $message["message"]);
*/
processmails();
?>