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