File: funclib.cyrus

package info (click to toggle)
websieve 0.62-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 276 kB
  • ctags: 92
  • sloc: perl: 3,532; makefile: 53
file content (190 lines) | stat: -rw-r--r-- 4,091 bytes parent folder | download
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/perl
#Websieve funclib.pl library for Cyrus Perl admin modules
#Version 1.2a

use Cyrus::IMAP::Admin;
use Cyrus::SIEVE::managesieve;

my (@scripts,$password,$username);
$IMAPERROR='';
$SIEVEERROR='';

sub _cb_eof {
	my %cb=@_;
	$IMAPERROR= "Connection to server lost.\n";
	${$cb{-rock}}=undef;
}
	
sub openimap {
	my ($uid,$pass,$server,$port,$ssl,$separator)=@_;
	$port='143' if !$port;
	$IMAPERROR='';
	my $mech='LOGIN'; #if nothing will default to first available I believe
	#$mech can only be 'LOGIN'  at this time
	#since we can't pass the password for DIGEST-MD5 to imclient
	$imap=Cyrus::IMAP::Admin->new($server,$port);
	$imap->addcallback({-trigger=>'EOF',
			    -callback=>\&_cb_eof,
			    -rock=>\$imap}) if defined $imap;
	return '' if $IMAPERROR;
	my $err=$imap->authenticate(-user=>$uid, -password=>$pass, -mechanism=>$mech);
	$IMAPERROR=$imap->error;
	if (!$IMAPERROR && !$err) {
		$IMAPERROR="Error Authenticating to $server with id $uid";
	}
	return '' if $IMAPERROR;
	return $imap;

}
sub closeimap {
	$imap=undef;
}
sub listmailbox {
	my ($mbx)=@_;
	my (@mbxtmp,@mbx,$mbxname,$l);
	@mbxtmp=$imap->list($mbx);
 	foreach $mbxname (@mbxtmp) {
 		$l=$mbxname->[0];
		push (@mbx,$l);	
	#	if ($mbxname->[1] ne '') {
	#		$l .= ' (' .$mbxname->[1] . ')';
	#	}
	}
	$IMAPERROR=$imap->error;
	return @mbx;
}

sub getquota {
	my ($mbx) = @_;
	my ($root,%quota)=$imap->quotaroot($mbx);
	my (@quotatmp);
	foreach my $quota (keys %quota) {
		push (@quotatmp,$mbx);
		push (@quotatmp,$quota{$quota}[0]);
		push (@quotatmp,$quota{$quota}[1]);
	}
	$IMAPERROR=$imap->error;
	return @quotatmp;
}

sub getacl {
	my ($mbx)=@_;
	my (@acltmp,$acl,$acluser);
	my %acl=$imap->listacl($mbx);
 	my @aclkeys=keys %acl;
	while (@aclkeys) {
		$acluser=shift(@aclkeys);
		$acl=$acl{$acluser};
		push (@acltmp,$acluser);
		push (@acltmp,$acl);
	}
	$IMAPERROR=$imap->error;
	return @acltmp;
}
sub setacl {
	my ($mbx,$user,$acl)=@_;
	my $err=$imap->setacl($mbx,$user,$acl);
	$IMAPERROR=$imap->error;
	return $IMAPERROR;
}

sub setquota {
	my ($mbx,$quota)=@_;
	my $err=$imap->setquota($mbx,"STORAGE",$quota);
	$IMAPERROR=$imap->error;
	return $IMAPERROR;
}

sub createmailbox {
	my ($mbx,$partition)=@_;
	my $err=$imap->create($mbx,$partition);
	$IMAPERROR=$imap->error;
	return $IMAPERROR;
}
sub deletemailbox {
	my ($mbx)=@_;
	my $err=$imap->delete($mbx);
	$IMAPERROR=$imap->error;
	return $IMAPERROR;
}


sub prompt {
	my($type,$prompt)=@_;
	if (($type eq "username") && (defined $username)) {
		return $username;
	} elsif (($type eq "authname") && (defined $username)) {
		return $username;
	} elsif (($type eq "realm") && (defined $realm)) {
		return $realm;
	} elsif (($type eq "password") && (defined $password)) {
		return $password;
	}
	return "";
}

sub opensieve {
	my ($uid,$pass,$port,$server,$ssl)=@_;
	$username=$uid;
	$password=$pass;
	$realm="";
	$SIEVEERROR='';
	$ENV{SASL_SIEVE_MECH}="CRAM-MD5"; # only works with a small mod 
	$sieve=sieve_get_handle($server,"prompt","prompt","prompt","prompt");
	delete $ENV{SASL_SIEVE_MECH};
	undef $username;
	undef $password;
	if (!defined $sieve) {
    		my $err = sieve_get_global_error();
    		$SIEVEERROR="unable to connect to server: $err";
		return;
	}
	return $sieve;
}

sub closesieve {
	$sieve=undef;
}

sub list_cb {
	my($name,$isactive)=@_;
	if ($isactive == 1) {
		$name=$name."*";
	}
	push (@scripts,$name)
}
sub listscripts {
	@scripts=();
	my $err=sieve_list($sieve,"list_cb");
	$SIEVEERROR=sieve_get_error($sieve);
	return @scripts;
}
sub getscript {
	my ($scriptname)=@_;
	my $str='';
	my $err=sieve_get($sieve,$scriptname,$str);
	$SIEVEERROR=sieve_get_error($sieve);
	return $str;
}

sub putscript {
	my ($scriptname,$script)=@_;
	my $err=sieve_put($sieve,$scriptname,$script);
	$SIEVEERROR=sieve_get_error($sieve);
	return $SIEVEERROR;
}

sub setactive {
	my ($script)=@_;
	my $err=sieve_activate($sieve,$script);
	$SIEVEERROR=sieve_get_error($sieve);
	return $SIEVEERROR;
}

sub deletescript {
	my ($script)=@_;
	my $err=sieve_delete($sieve,$script);
	$SIEVEERROR=sieve_get_error($sieve);
	return $SIEVEERROR;
}
1;