home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.urbanrage.com
/
2015-02-07.ftp.urbanrage.com.tar
/
ftp.urbanrage.com
/
pub
/
perl
/
tkCyrus
< prev
next >
Wrap
Text File
|
2002-04-21
|
10KB
|
372 lines
#!/usr/local/bin/perl -- #-*- perl -*- for emacs
use strict;
use Tk;
use Tk::NoteBook;
use Tk::LabEntry;
use Tk::Label;
use IMAP::Admin 0.8.0;
use ConfFile 0.6 qw(array insert_order);
if (scalar(@ARGV) != 1) {
print "usage: $0 configuration_file\n";
exit;
}
# declare vars
my ($top, %info, $domain, $conf, %dev);
%dev = (
tcp => ["11", "42"],
udp => ["11", "41"],
zero => ["13", "12"],
ticotsord => ["105", "1"]
);
# read configuration file
$conf = ConfFile->new($ARGV[0]);
$domain = $conf->get("Domain");
# setup initial window
$top = MainWindow->new;
¬ebook_setup($top);
MainLoop;
sub notebook_setup($$) {
my ($top) = @_;
my ($note, %note, $key, $item);
$note = $top->NoteBook(-ipadx => 6, -ipady => 6);
$note{'add'} = $note->add("add", -label => "Add/Modify", -underline => 0);
$note{'delete'} = $note->add("del", -label => "Delete", -underline => 0);
# $note{'mass_add'} = $note->add("mad", -label => "Mass Add", -underline => 0);
$note{'exit'} = $note->add("exit", -label => "Exit", -underline => 0);
$note{'debug'} = $note->add("debug", -label => "Debug",
-raisecmd => sub { exec "$0 @ARGV"; });
&create_note($note{'add'}, "add");
&create_note($note{'delete'}, "del");
# &create_note($note{'mass_add'}, "mad");
$note{'exit'}->Button(-command => [\&clean_up],
-text => "Exit"
)->pack(-side => "top", -expand => "yes", -fill => 'both');
$note->pack;
}
sub create_note($$) {
my ($note, $type) = @_;
my ($opt, $frame);
if ($type ne "mad") {
$note->LabEntry(-label => "Mailbox:",
-labelPack => [-side => "left", -anchor => "w"],
-width => 33,
-textvariable => \$info{'mailbox'}
)->pack(-side => "top", -anchor => "nw");
$note->LabEntry(-label => "ACL:",
-labelPack => [-side => "left", -anchor => "w"],
-width => 36,
-textvariable => \$info{'acl'}
)->pack(-side => "top", -anchor => "nw");
$frame = $note->Frame()->pack(-side => "top", -anchor => "nw");
$frame->LabEntry(-label => "Quota:",
-labelPack => [-side => "left", -anchor => "w"],
-width => 14,
-textvariable => \$info{'quota'}
)->pack(-side => "left", -anchor => "nw");
$frame->LabEntry(-label => "Used:",
-labelPack => [-side => "left", -anchor => "w"],
-width => 13,
-textvariable => \$info{'quota_used'}
)->pack(-side => "left", -anchor => "nw");
} else {
$note->LabEntry(-label => "File:",
-labelPack => [-side => "left", -anchor => "w"],
-width => 40,
-textvariable => \$info{'file'}
)->pack(-side => "top", -anchor => "nw");
}
$frame = $note->Frame()->pack(-side => "top", -anchor => "nw");
$frame->Label(-text => "Domain:")->pack(-side => "left");
$opt = $frame->Optionmenu(-variable => \$info{'domain'});
$opt->addOptions("huh?", insert_order($domain));
$opt->pack(-side => "left", -anchor => "nw");
$frame = $note->Frame();
if ($type ne "mad") {
$frame->Button(-command => [\&get_user, \%info],
-text => "Get Info"
)->pack(-side => "left", -anchor => "s");
}
if ($type eq 'add') {
$frame->Button(-command => [\&add_user, \%info],
-text => "Nike"
)->pack(-side => "left", -anchor => "e",
-expand => 'yes');
} elsif ($type eq 'del') {
$frame->Button(-command => [\&del_user, \%info],
-text => "Nike"
)->pack(-side => "left", -anchor => "e",
-expand => 'yes');
} elsif ($type eq 'mad') {
$frame->Button(-command => [\&mass_add, \%info],
-text => "Nike"
)->pack(-side => "left", -anchor => "e",
-expand => 'yes');
}
$frame->pack(-side => "bottom", -anchor => "s", -expand => 'yes', -fill => 'x');
}
sub ged { # generic_error_dialog
my ($text) = @_;
my $dialog = $top->Dialog(-title => "An error occurred",
-text => $text,
-justify => 'center');
$dialog->Show;
}
sub get_user {
my ($ref) = @_;
my (@info, @acl, $acl_string, @quota, $the_domain, $imap, $key, @list);
if (length($ref->{'mailbox'}) == 0) {
&ged("no mailbox information specified");
return;
}
if ($ref->{'domain'} eq "huh?") {
&ged("no domain selected [$ref->{'domain'}]");
return;
} else {
$the_domain = $domain->{$ref->{'domain'}};
if (!defined($the_domain->{'Server'})) {
&ged("Server not defined for $ref->{'domain'}, check your configuration file");
return;
}
if (!defined($the_domain->{'Login'})) {
&ged("Login not defined for $ref->{'domain'}, check your configuration file");
return;
}
if (!defined($the_domain->{'Password'})) {
&ged("Password not defined for $ref->{'domain'}, check your configuration file");
return;
}
}
if (!eval { $imap = IMAP::Admin->new('Server' => $the_domain->{'Server'},
'Login' => $the_domain->{'Login'},
'Password' => $the_domain->{'Password'});}){
&ged("Couldn't open imap connection to $the_domain->{'Server'}");
# delete user ?
return;
}
$ref->{'acl'} = "";
$ref->{'quota'} = "";
$ref->{'quota_used'} = "";
undef @list;
@list = $imap->list($ref->{'mailbox'});
if (scalar (@list) == 0) {
&ged ("Mailbox doesn't exist");
$imap->close;
return;
}
if (scalar (@list) != 1) {
print "@list\n";
&ged("Mailbox has more than one matching entry");
$imap->close;
return;
}
$list[0] =~ tr/\"//d; # " to balance quotes for emacs
$ref->{'mailbox'} = $list[0];
undef @acl;
@acl = $imap->get_acl($ref->{'mailbox'});
$ref->{'acl'} = join(" ", @acl);
undef @quota;
@quota = $imap->get_quota($ref->{'mailbox'});
$ref->{'quota'} = "";
if (scalar(@quota) > 0) {
$ref->{'quota_used'} = $quota[1];
$ref->{'quota'} = $quota[2];
}
$imap->close;
return;
}
sub mass_add {
my ($ref) = @_;
my (@info);
if (length($ref->{'file'}) == 0) {
&ged("no file specified");
return;
}
if ($ref->{'domain'} eq "huh?") {
&ged("no domain selected [$ref->{'domain'}]");
return;
}
open (FILE, $ref->{'file'}) || do {
&ged("Couldn't open $ref->{'file'} : $!");
return;
};
while(<FILE>) {
chomp;
@info = split ':';
$ref->{'mailbox'} = $info[0];
$ref->{'acl'} = $info[1];
$ref->{'quota'} = $info[2];
&add_user($ref);
}
close FILE;
return;
}
sub add_user {
my ($ref) = @_;
my (@info, @acl, @list, $the_domain, $imap, $err, $user, $crmod, $index);
if (length($ref->{'mailbox'}) == 0) {
&ged("no login information specified");
return;
}
if ($ref->{'domain'} eq "huh?") {
&ged("no domain selected [$ref->{'domain'}]");
return;
} else {
$the_domain = $domain->{$ref->{'domain'}};
if (!defined($the_domain->{'Server'})) {
&ged("Server not defined for $ref->{'domain'}, check your configuration file");
return;
}
if (!defined($the_domain->{'Login'})) {
&ged("Login not defined for $ref->{'domain'}, check your configuration file");
return;
}
if (!defined($the_domain->{'Password'})) {
&ged("Password not defined for $ref->{'domain'}, check your configuration file");
return;
}
}
undef @info;
if (!eval { $imap = IMAP::Admin->new('Server' => $the_domain->{'Server'},
'Login' => $the_domain->{'Login'},
'Password' => $the_domain->{'Password'});}){
&ged("Couldn't open imap connection to $the_domain->{'Server'}");
# delete user ?
return;
}
$crmod = 0; # 0 equal modify, 1 = create
@list = $imap->list($ref->{'mailbox'});
if (scalar (@list) == 0) {
$crmod = 1;
} elsif (scalar (@list) != 1) {
print "@list\n";
&ged("Mailbox has more than one matching entry");
$imap->close;
return;
} else {
$list[0] =~ tr/\"//d; # " to balance quotes for emacs
$ref->{'mailbox'} = $list[0];
}
$user = substr($ref->{'mailbox'}, rindex($ref->{'mailbox'}, ".")+1);
if ($crmod == 1) {
$err = $imap->create($ref->{'mailbox'});
if ($err != 0) {
&ged($imap->{'Error'});
$imap->close;
# delete user ?
return;
}
}
undef @acl;
@acl = $imap->get_acl($ref->{'mailbox'});
if (scalar(@acl) > 2) {
for ($index = 2; $index < scalar(@acl); $index += 2) {
$err = $imap->delete_acl($ref->{'mailbox'}, $acl[$index]);
}
}
@acl = split(' ', $ref->{'acl'});
if (scalar(@acl) == 0) {
$err = $imap->set_acl($ref->{'mailbox'}, $user,
$the_domain->{'ACL'}, $the_domain->{'Login'}, "d");
} else {
$err = $imap->set_acl($ref->{'mailbox'}, @acl);
}
if ($err != 0) {
&ged($imap->{'Error'});
}
$err = 0;
if (length($ref->{'quota'}) == 0) {
if (defined($the_domain->{'Quota'})) {
$err = $imap->set_quota($ref->{'mailbox'}, $the_domain->{'Quota'});
if ($err != 0) {
&ged($imap->{'Error'});
}
}
} else {
$err = $imap->set_quota($ref->{'mailbox'}, $ref->{'quota'});
}
if ($err != 0) {
&ged($imap->{'Error'});
}
$imap->close;
&get_user($ref);
return;
}
sub del_user {
my ($ref) = @_;
my (@info, $the_domain, $imap, $err, $database);
if (length($ref->{'mailbox'}) == 0) {
&ged("no mailbox information specified");
return;
}
if ($ref->{'domain'} eq "huh?") {
&ged("no domain selected [$ref->{'domain'}]");
return;
} else {
$the_domain = $domain->{$ref->{'domain'}};
if (!defined($the_domain->{'Server'})) {
&ged("Server not defined for $ref->{'domain'}, check your configuration file");
return;
}
if (!defined($the_domain->{'Login'})) {
&ged("Login not defined for $ref->{'domain'}, check your configuration file");
return;
}
if (!defined($the_domain->{'Password'})) {
&ged("Password not defined for $ref->{'domain'}, check your configuration file");
return;
}
}
if (!eval { $imap = IMAP::Admin->new('Server' => $the_domain->{'Server'},
'Login' => $the_domain->{'Login'},
'Password' => $the_domain->{'Password'});}){
&ged("Couldn't open imap connection to $the_domain->{'Server'}");
return;
}
$err = $imap->delete($ref->{'mailbox'});
if ($err != 0) {
&ged($imap->{'Error'});
} else {
$ref->{'acl'} = "";
$ref->{'quota'} = "";
$ref->{'quota_used'} = "";
}
$imap->close;
return;
}
sub clean_up {
exit;
}
sub reload {
exec $0 @ARGV;
}