-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-mailinglists.pl
145 lines (120 loc) · 4.2 KB
/
generate-mailinglists.pl
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
#!/usr/bin/perl
#
# Install the following perl dependencies in ArchLinux:
# yaourt -S perl-lwp-protocol-https perl-xml-libxml
#
use strict;
use warnings;
no warnings 'experimental::smartmatch';
use LWP::UserAgent;
use XML::LibXML;
use Getopt::Long;
my $verbose;
sub usage {
print "ERROR: Unknown parameter \"@_\".\n" if (@_);
print " \n"
. "usage: generate-mailinglists.pl \n"
. "--user|-u <username> \n"
. "--pass|-p <password> \n"
. "--host|-h <hostname> \n"
. "[--catch-all <email>] \n"
. "[--private <private-list-id>] \n"
. "[--error-mail-from <address>] \n"
. "[--verbose|-v] \n"
. "[--help|-?] \n\n"
. "Example: \n"
. "./generate-mailinglists.pl -u admin -p secret -h owncloud.domain.com --private intern --private workgroup\n\n";
exit;
}
sub http_get {
( my $uri, my $user, my $pass ) = @_;
# define user agent
my $ua = LWP::UserAgent->new();
$ua->agent("USER/AGENT/IDENTIFICATION");
# make request
my $request = HTTP::Request->new( GET => $uri );
$request->header("OCS-APIRequest" => "true");
# authenticate
$request->authorization_basic( $user, $pass );
# request data
my $response = $ua->request($request);
die "Error " . $response->status_line . ": " . $response->content unless $response->is_success;
# get content of response
return $response->content();
}
sub check_status_ok {
( my $dom ) = @_;
die "Error reading data from owncloud"
unless ( ( $dom->findnodes('/ocs/meta/status') ) eq "ok" );
}
sub get_list_from_api {
( my $apicall, my $user, my $pass, my $xpath ) = @_;
my $xml = http_get( $apicall, $user, $pass );
my $dom = XML::LibXML->load_xml( string => $xml );
check_status_ok($dom);
return $dom->findnodes($xpath);
}
#
# Main
#
# config parameters
my ( $help, $user, $pass, $server, @private, $error_mf, $catchall );
$user = "";
$pass = "";
$server = "";
@private = ();
$error_mf = "";
$catchall = "";
# parse commandline parameters
usage()
if (
!GetOptions(
'help|?' => \$help,
'verbose|v' => \$verbose,
'user|u=s' => \$user,
'pass|p=s' => \$pass,
'host|h=s' => \$server,
'private:s' => \@private,
'error-mail-from:s' => \$error_mf,
'catch-all:s' => \$catchall,
)
or defined $help
);
print "Requesting data from owncloud...\n" if defined $verbose;
my $api = "https://$server/ocs/v1.php/cloud";
my $result = "# Exim filter <<-- Do not edit or remove this line\n#\n\n";
my @list = get_list_from_api( "$api/groups", $user, $pass,
'/ocs/data/groups/element/text()' );
foreach (@list) {
print "Processing group $_\n" if defined $verbose;
my @list = get_list_from_api( "$api/groups/$_", $user, $pass,
'/ocs/data/users/element/text()' );
my $deliver;
my $fromcheck;
my $n = $#list;
foreach (@list) {
my @list = get_list_from_api( "$api/users/$_", $user, $pass,
'/ocs/data/email/text()' );
my $email = $list[0];
$deliver .= " if \$header_from does not contain \"" . $email . "\" then deliver $email endif" . ( !$n ? "" : "\n" );
$fromcheck .= " \$header_from does contain \"$email\" "
. ( !$n-- ? "" : "or\n" );
}
$result .=
"if\n \$original_local_part is \"$_\""
. ( $_ ~~ @private ? "\nthen if\n (\n$fromcheck\n )" : "" )
. "\nthen\n"
. "$deliver\n"
. ( $_ ~~ @private ? "else\n seen mail to \$sender_address from \"$error_mf\" subject \"Re: \$header_subject\" text \"Die Email konnte nicht zugestellt werden, da Ihre Emailadresse für diesen Verteiler nicht freigeschaltet ist.\"\nendif\n" : "" )
. "endif\n\n";
}
if ( $catchall ne "" ) {
$result .=
"# Catch all\nif not delivered then\n" . " deliver $catchall\nendif";
}
print "Writing new .forward file\n" if defined $verbose;
open( my $fh, ">$ENV{'HOME'}/.forward" )
or die "Could not open .forward file: $!";
print $fh $result;
close $fh;
print "All done!\n" if defined $verbose;