/
imscp-arpl-msgr
169 lines (137 loc) · 3.89 KB
/
imscp-arpl-msgr
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
#!/usr/bin/perl
# i-MSCP - internet Multi Server Control Panel
# Copyright (C) 2010 by internet Multi Server Control Panel
#
# 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 2
# of the License, or (at your option) 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# @category i-MSCP
# @copyright 2010 - 2012 by i-MSCP | http://i-mscp.net
# @author Daniel Andreca <sci2tech@gmail.com>
# @version SVN: $Id$
# @link http://i-mscp.net i-MSCP Home Site
# @license http://www.gnu.org/licenses/gpl-2.0.html GPL v2
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/..";
use lib "$FindBin::Bin/../PerlLib";
use lib "$FindBin::Bin/../PerlVendor";
use iMSCP::Debug;
use iMSCP::Boot;
use Mail::Address;
use MIME::Entity;
use Email::Simple;
use Data::Dumper;
use Encode;
newDebug('imscp-arpl-msgr/imscp-arpl-msgr.log');
sub arpl_start_up {
umask(027);
iMSCP::Boot->new()->init({nolock => 'yes', norequirements => 'yes'});
0;
}
sub arpl_engine {
my $from = $ARGV[0];
$from =~ s/imscp-arpl\.//gim;
my @msg = <STDIN>;
my $sql = "
DELETE FROM
`autoreplies_log`
WHERE
ADDTIME(`time`, '24:00:00') < NOW()
";
my $rdata = iMSCP::Database->factory()->doQuery('delete', $sql);
error("$rdata") if(ref $rdata ne 'HASH');
my $mail = Email::Simple->new(join('', @msg));
my ($to) = Mail::Address->parse($mail->header("From"));
if (not $to){
error("Couldn't parse a From address");
return 0;
}
my ($to) = lc($to->address);
if(
$mail->header('X-Mailer') && $mail->header('X-Mailer') =~ m/Autoreply Manager/i ||
$mail->header('Auto-Submitted') && $mail->header('Auto-Submitted') =~ m/auto-replied/i ||
$mail->header('Sender') && $mail->header('Sender') =~ m/autoresponder/i ||
$mail->header('X-Autoresponse-From')
) {
return 0;
}
$sql = "
SELECT
`time`
FROM
`autoreplies_log`
WHERE
`from` = ?
AND
`to` = ?
AND
ADDTIME(`time`, '24:00:00') > NOW()
";
$rdata = iMSCP::Database->factory()->doQuery('time', $sql, $from, $to);
error("$rdata") if(ref $rdata ne 'HASH');
return 0 if(scalar keys %{$rdata});
$sql = "
SELECT
CONVERT(`mail_auto_respond_text` using utf8) as `text`
FROM
`mail_users`
WHERE
`mail_addr` = ?
AND
`mail_auto_respond` = 1
";
$rdata = iMSCP::Database->factory()->doQuery('text', $sql, $from);
if(ref $rdata ne 'HASH'){
error("$rdata");
return 1;
}
return 0 unless(scalar keys %{$rdata});
my $text = (keys %{$rdata})[0];
my $out = new MIME::Entity;
$out->build(
From => $from,
To => $to,
Subject => "[Autoreply] ".$mail->header('Subject'),
Type => "multipart/mixed; charset=utf-8",
'X-Autoresponse-From' => $from,
'X-Mailer' => "i-MSCP $main::imscpConfig{'Version'} Autoreply Manager"
);
$out->attach(
Type => "text/plain; charset=utf-8",
Encoding => "7bit",
Description => "Mail User Autoreply Message",
Data => Encode::encode("UTF-8", $text)
);
$out->attach(
Type => "message/rfc822",
Description => "Original Message",
Data => join('', @msg)
);
open MAIL, "| /usr/sbin/sendmail -t -oi";
$out -> print(\*MAIL);
close MAIL;
# Log the autoreply
$sql = "INSERT INTO `autoreplies_log` (`time`, `from`, `to`) VALUES (NOW(), ?, ?)";
$rdata = iMSCP::Database->factory()->doQuery('insert', $sql, $from, $to);
if(ref $rdata ne 'HASH'){
error("$rdata");
return 1;
}
0;
}
exit 1 if arpl_start_up();
exit 1 if arpl_engine();
exit 0;