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