]> git.proxmox.com Git - pmg-api.git/blame - PMG/Utils.pm
depend on clamav-daemon
[pmg-api.git] / PMG / Utils.pm
CommitLineData
758c7b6b
DM
1package PMG::Utils;
2
3use strict;
4use warnings;
5use Carp;
6use DBI;
b8ea5d5d 7use Net::Cmd;
758c7b6b 8use Net::SMTP;
cad3d400 9use File::stat;
758c7b6b
DM
10use MIME::Words;
11use MIME::Parser;
12
13use PVE::SafeSyslog;
14
c881fe35
DM
15sub msgquote {
16 my $msg = shift || '';
17 $msg =~ s/%/%%/g;
18 return $msg;
19}
20
758c7b6b
DM
21sub lastid {
22 my ($dbh, $seq) = @_;
23
24 return $dbh->last_insert_id(
25 undef, undef, undef, undef, { sequence => $seq});
26}
27
cad3d400
DM
28sub file_older_than {
29 my ($filename, $lasttime) = @_;
30
31 my $st = stat($filename);
32
33 return 0 if !defined($st);
34
35 return ($lasttime >= $st->ctime);
36}
37
758c7b6b
DM
38sub extract_filename {
39 my ($head) = @_;
40
41 if (my $value = $head->recommended_filename()) {
42 chomp $value;
43 if (my $decvalue = MIME::Words::decode_mimewords($value)) {
44 $decvalue =~ s/\0/ /g;
45 $decvalue = trim ($decvalue);
46 return $decvalue;
47 }
48 }
49
50 return undef;
51}
52
53sub remove_marks {
54 my ($entity, $add_id, $id) = @_;
55
56 $id //= 1;
57
58 foreach my $tag (grep {/^x-proxmox-tmp/i} $entity->head->tags) {
59 $entity->head->delete ($tag);
60 }
61
62 $entity->head->replace('X-Proxmox-tmp-AID', $id) if $add_id;
63
64 foreach my $part ($entity->parts) {
65 $id = remove_marks($part, $add_id, $id + 1);
66 }
67
68 return $id;
69}
70
71sub subst_values {
72 my ($body, $dh) = @_;
73
74 return if !$body;
75
76 foreach my $k (keys %$dh) {
77 my $v = $dh->{$k};
78 if (defined ($v)) {
79 $body =~ s/__${k}__/$v/gs;
80 }
81 }
82
83 return $body;
84}
85
86sub reinject_mail {
87 my ($entity, $sender, $targets, $xforward, $me, $nodsn) = @_;
88
89 my $smtp;
90 my $resid;
91 my $rescode;
92 my $resmess;
93
94 eval {
95 my $smtp = Net::SMTP->new('127.0.0.1', Port => 10025, Hello => $me) ||
96 die "unable to connect to localhost at port 10025";
97
98 if (defined($xforward)) {
99 my $xfwd;
100
101 foreach my $attr (keys %{$xforward}) {
102 $xfwd .= " $attr=$xforward->{$attr}";
103 }
104
105 if ($xfwd && $smtp->command("XFORWARD", $xfwd)->response() != CMD_OK) {
106 syslog('err', "xforward error - got: %s %s", $smtp->code, scalar($smtp->message));
107 }
108 }
109
110 if (!$smtp->mail($sender)) {
111 syslog('err', "smtp error - got: %s %s", $smtp->code, scalar ($smtp->message));
112 die "smtp from: ERROR";
113 }
114
115 my $dsnopts = $nodsn ? {Notify => ['NEVER']} : {};
116
117 if (!$smtp->to (@$targets, $dsnopts)) {
118 syslog ('err', "smtp error - got: %s %s", $smtp->code, scalar($smtp->message));
119 die "smtp to: ERROR";
120 }
121
122 # Output the head:
123 #$entity->sync_headers ();
124 $smtp->data();
125
126 my $out = PMG::SMTPPrinter->new($smtp);
127 $entity->print($out);
128
129 # make sure we always have a newline at the end of the mail
130 # else dataend() fails
131 $smtp->datasend("\n");
132
133 if ($smtp->dataend()) {
134 my @msgs = $smtp->message;
135 $resmess = $msgs[$#msgs];
136 ($resid) = $resmess =~ m/Ok: queued as ([0-9A-Z]+)/;
137 $rescode = $smtp->code;
138 if (!$resid) {
139 die sprintf("unexpected SMTP result - got: %s %s : WARNING", $smtp->code, $resmess);
140 }
141 } else {
142 my @msgs = $smtp->message;
143 $resmess = $msgs[$#msgs];
144 $rescode = $smtp->code;
145 die sprintf("sending data failed - got: %s %s : ERROR", $smtp->code, $resmess);
146 }
147 };
148 my $err = $@;
149
150 $smtp->quit if $smtp;
151
152 if ($err) {
153 syslog ('err', $err);
154 }
155
156 return wantarray ? ($resid, $rescode, $resmess) : $resid;
157}
158
159
1601;