-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgoldfish.php
executable file
·362 lines (295 loc) · 10.8 KB
/
goldfish.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/*
goldfish - the PHP auto responder for postfix
Copyright © 2007-2015 - Authors:
(c) 2007-2009 Remo Fritzsche (Main application programmer)
(c) 2009 Karl Herrick (Bugfix)
(c) 2007-2008 Manuel Aller (Additional programming)
(c) 2015 Dirk Groenen (Additional programming)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Version 1.1
*/
ini_set('display_errors', true);
error_reporting( E_ALL );
######################################
# Check PHP version #
######################################
if ( version_compare( PHP_VERSION, "5.0.0" ) == - 1 )
{
echo "Error, you are currently not running PHP 5 or later. Exiting.\n";
exit;
}
######################################
# Configuration #
######################################
/* General */
$conf['cycle'] = 5 * 60;
/* Logging */
$conf['log_file_path'] = "/var/log/goldfish";
$conf['write_log'] = true;
/* Database information */
$conf['mysql_host'] = "localhost";
$conf['mysql_user'] = "mailuser";
$conf['mysql_password'] = "password";
$conf['mysql_database'] = "mailserver";
/* Database Queries */
# This query has to return the path (`path`) of the corresponding maildir-Mailbox with email-address %m
$conf['q_mailbox_path'] = "SELECT CONCAT('/var/mail/vhosts/', SUBSTRING_INDEX(email,'@',-1), '/', SUBSTRING_INDEX(email,'@',1), '/') AS `path` FROM virtual_users WHERE `email` = '%m'";
# This query has to return the following fields from the autoresponder table: `from`, `to`, `email`, `message` where `enabled` = 2
$conf['q_forwardings'] = "SELECT * FROM `autoresponder` WHERE `enabled` = 1 AND `force_disabled` = 0"; //modified for the autoresponder plugin
# This query has to disable every autoresponder entry which ended in the past
$conf['q_disable_forwarding'] = "UPDATE `autoresponder` SET `enabled` = 0 WHERE `to` < CURDATE();";
# This query has to activate every autoresponder entry which starts today
$conf['q_enable_forwarding'] = "UPDATE `autoresponder` SET `enabled` = 1 WHERE `from` <= CURDATE() AND (`to` >= CURDATE() OR `to`='0000-00-00');"; //modified for the autoresponder plugin
# This query has to return the message of an autoresponder entry identified by email %m
$conf['q_messages'] = "SELECT `message` FROM `autoresponder` WHERE `email` = '%m'";
# This query has to return the subject of the autoresponder entry identified by email %m
$conf['q_subject'] = "SELECT `subject` FROM `autoresponder` WHERE `email` = '%m'";
############################################################################
#
# Don't edit anything below here unless you know what you're doing!
#
############################################################################
######################################
# Logger class #
######################################
class Logger
{
var $logfile;
var $str;
function addLine($str)
{
$str = date("Y-m-d h:i:s")." ".$str;
$this->str .= "\n$str";
echo $str."\n";
}
function writeLog(&$conf)
{
if (! $conf['write_log'] ) return;
if (is_writable($conf['log_file_path']))
{
$this->addLine("--------- End execution ------------");
if (!$handle = fopen($conf['log_file_path'], 'a'))
{
echo "Cannot open file ({$conf['log_file_path']})";
exit;
}
if (fwrite($handle, $this->str) === FALSE)
{
echo "Cannot write to file)";
exit;
}
else
{
echo "Wrote log successfully.";
}
fclose($handle);
}
else
{
echo "Error: The log file is not writeable.\n";
echo "The log has not been written.\n";
}
}
}
######################################
# Create log object #
######################################
$log = new Logger();
######################################
# function endup() #
######################################
function endup(&$log, &$conf)
{
$log->writeLog($conf);
exit;
}
######################################
# Database connection #
######################################
$link = @mysql_connect($conf['mysql_host'], $conf['mysql_user'], $conf['mysql_password']);
if (!$link)
{
$log->addLine("Could not connect to database. Abborting.");
endup($log, $conf);
}
else
{
$log->addLine("Connection to database established successfully");
if (!mysql_select_db($conf['mysql_database']))
{
$log->addLine("Could not select database ".$conf['mysql_database']);
endup($log, $conf);
}
else
{
$log->addLine("Database selected successfully");
}
}
######################################
# Update database entries #
######################################
$result = mysql_query($conf['q_disable_forwarding']);
if (!$result)
{
$log->addLine("Error in query ".$conf['q_disable_forwarding']."\n".mysql_error());
}
else
{
$log->addLine("Successfully updated database (disabled entries)");
}
mysql_query($conf['q_enable_forwarding']);
if (!$result)
{
$log->addLine("Error in query ".$conf['q_enable_forwarding']."\n".mysql_error());
}
else
{
$log->addLine("Successfully updated database (enabled entries)");
}
######################################
# Catching dirs of autoresponders mailboxes #
######################################
// Corresponding email addresses
$result = mysql_query($conf['q_forwardings']);
if (!$result)
{
$log->addLine("Error in query ".$conf['q_forwardings']."\n".mysql_error());
exit;
}
$num = mysql_num_rows($result);
for ($i = 0; $i < $num; $i++)
{
$emails[] = mysql_result($result, $i, "email");
$name[] = mysql_result($result, $i, "descname");
}
// Fetching directories
for ($i = 0; $i < $num; $i++)
{
$result = mysql_query(str_replace("%m", $emails[$i], $conf['q_mailbox_path']));
if (!$result)
{
$log->addLine("Error in query ".$conf['q_mailbox_path']."\n".mysql_error()); exit;
}
else
{
$log->addLine("Successfully fetched maildir directories");
}
$paths[] = mysql_result($result, 0, 'path') . 'new/';
}
######################################
# Reading new mails #
######################################
if ($num > 0)
{
$log->addLine("Reading new emails: new emails found: " . $num);
$i = 0;
foreach ($paths as $path)
{
$log->addLine("Start scanning directory " . $path);
foreach(scandir($path) as $entry)
{
if ($entry != '.' && $entry != '..')
{
$log->addLine("Found entry [" . $entry . "] in directory " . $path);
if (time() - filemtime($path . $entry) - $conf['cycle'] <= 0)
{
$mails[] = $path . $entry;
###################################
# Send response #
###################################
// Reading mail address
$mail = file($path.$entry);
foreach ($mail as $line)
{
$line = trim($line);
if (substr($line, 0, 12) == 'Return-Path:')
{
$returnpath = substr($line, strpos($line, '<') + 1, strpos($line, '>') - strpos($line, '<')-1)."\n";
}
if (substr($line, 0, 5) == 'From:' && strstr($line,"@"))
{
$address = substr($line, strpos($line, '<') + 1, strpos($line, '>') - strpos($line, '<')-1)."\n";
break;
}
elseif(substr($line,0,5) == 'From:' && !strstr($line,"@") && !empty ($returnpath))
{
$address = $returnpath;
break;
}
}
// Check: Is this mail allready answered
if (empty($address))
{
$log->addLine("Error, could not parse mail $path");
}
else
{
// Get data of current mail
$email = $emails[$i];
// Get subject
$result = mysql_query(str_replace("%m", $emails[$i], $conf['q_subject']));
if (!$result)
{
$log->addLine("Error in query ".$conf['q_subject']."\n".mysql_error()); exit;
}
else
{
$log->addLine("Successfully fetched subject of {$emails[$i]}");
}
$subject = mysql_result($result, 0, 'subject');
// Get Message
$result = mysql_query(str_replace("%m", $emails[$i], $conf['q_messages']));
if (!$result)
{
$log->addLine("Error in query ".$conf['q_messages']."\n".mysql_error()); exit;
}
else
{
$log->addLine("Successfully fetched message of {$emails[$i]}");
}
$message = mysql_result($result, 0, 'message');
$headers = "From: ".$name[$i]."<".$emails[$i].">";
// Check if mail is allready an answer:
if (strstr($mail, $message))
{
$log->addLine("Mail from {$emails[$i]} allready answered");
break;
}
// strip the line break from $address for checks
// fix by Karl Herrick, thank's a lot
if ( substr($address,0,strlen($address)-1) == $email )
{
$log->addLine("Email address from autoresponder table is the same as the intended recipient! Not sending the mail!");
break;
}
$sent = mail($address, $subject, $message, $headers);
if($sent){
$log->addLine("Autoresponse e-mail was sent to: " . $address);
}
else{
$log->addLine("Autoresponse was not sent. Something went wrong");
}
}
}
}
}
$i++;
}
}
else
{
$log->addLine("No new email found. Doing nothing...");
}
$log->writeLog($conf);
echo "End execution.";
?>