#!/usr/bin/perl -w
###############################################################################
# HCCEmail.pm - A custom API module for CPanel
# Version 0.2.1 beta - 11/MAY/2005
# (c) 2003-2005 Juan R. Pozo
# http://html.conclase.net/cp/scripts/
# mailto:jrpozo@conclase.net
# Mailing-list: http://www.conclase.net/mailman/listinfo/cpanel_conclase.net
###############################################################################
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
###############################################################################
# For INSTALLATION and DOCUMENTATION please see:
# http://html.conclase.net/cp/scripts/HCCEmail.txt (plain text), or
# http://html.conclase.net/cp/scripts/HCCEmail.pdf (Acrobat)
###############################################################################
# Please consider making a donation today. Visit my amazon.com wishlist at:
# http://html.conclase.net/link/wishlist
# Thank you :)
###############################################################################
package Cpanel::HCCEmail;
require 5.004;
use strict;
use vars qw(@ISA @EXPORT $VERSION $prefix);
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(
HCCEmail_init
HCCEmail_ListPopAccounts
HCCEmail_AddAccounts
);
$VERSION = '0.2.1';
$prefix = "HCCEmail";
Cpanel::Lang::loadlang($Cpanel::CPDATA{LANG});
sub HCCEmail_init {
return(1);
}
sub HCCEmail_ListPopAccounts {
my ($first, $length, $order, $format, $domains, $navpos, $userchar,
$addmain, $port, $sslport, $delpoppg, $edquotapg, $edpasswdpg,
$imagedir, $delpopimg, $webimg, $edquotaimg, $edpasswdimg, $outlookimg,
$arrowimg, $agingpg, $agingimg) = @_;
# Default values
$first = ($first =~ /^\s*['"]?\s*(\d+)\s*['"]?\s*$/) ? $1 : 0;
$length = ($length =~ /^\s*['"]?\s*(\d+)\s*['"]?\s*$/) ? $1 : 0;
$order = ($order =~ /^\s*['"]?\s*(addr|revaddr|acct|revacct|quota|revquota|revdomain)\s*['"]?\s*/) ?
lc($1) : "domain";
$format = ($format =~ /^\s*['"]?((?:\s*[^'"\s]+\s*)+)['"]?\s*$/) ? lc $1 :
"address arrowimg login quota deleteimg webmailimg edquotaimg edpasswdimg outlook";
$domains = ($domains =~ /^\s*['"]?\s*all\s*['"]?\s*$/i) ? "all" : lc($domains);
$navpos = ($navpos =~ /^\s*['"]?\s*(top|both)\s*['"]?\s*$/i) ? lc($1) : "bottom";
$userchar = ($userchar =~ /^\s*['"]?\s*([\@\+\%])\s*['"]?\s*$/) ? $1 : "+";
$addmain = ($addmain =~ /^\s*['"]?\s*(no)\s*['"]?\s*$/i) ? lc($1) : "once";
if ($port !~ /^\s*['"]?\s*\d+\s*['"]?\s*$/ || $port < 1 || $port > 65535) {
$port = 2095;
}
if ($sslport !~ /^\s*['"]?\s*\d+\s*['"]?\s*$/ || $sslport < 1 || $sslport > 65535) {
$sslport = 2096;
}
$port = (defined($ENV{'HTTPS'})) ? $sslport : $port;
my $regex = "^\\s*['\"]?\\s*([^'\"\\s]+)\\s*['\"]?\\s*\$";
$delpoppg = ($delpoppg =~ $regex) ? $1 : "dodelpop.html";
$edquotapg = ($edquotapg =~ $regex) ? $1 : "editquota.html";
$edpasswdpg = ($edpasswdpg =~ $regex) ? $1 : "passwdpop.html";
$agingpg = ($agingpg =~ $regex) ? $1 : "aging.html";
$imagedir = ($imagedir =~ $regex) ? $1 : "images";
$delpopimg = ($delpopimg =~ $regex) ? $1 : "delete.gif";
$webimg = ($webimg =~ $regex) ? $1 : "webmail.gif";
$edquotaimg = ($edquotaimg =~ $regex) ? $1 : "quota.gif";
$edpasswdimg = ($edpasswdimg =~ $regex) ? $1 : "passwd.gif";
$outlookimg = ($outlookimg =~ $regex) ? $1 : "outlook.gif";
$arrowimg = ($arrowimg =~ $regex) ? $1 : "arrowr.gif";
$agingimg = ($agingimg =~ $regex) ? $1 : "aging.gif";
my $lang = $Cpanel::CPDATA{'LANG'};
$lang =~ s/[^[:ascii:]]/-/g;
# Get list of emails
my @accts = ::emailhandoffarray("listpops");
my %quotas = ::emailhandoffhash("listpopswithdisk");
my @inboxmain = stat($Cpanel::homedir."/mail/inbox");
my $quotamain = $inboxmain[7] . "/";
# Remove unwanted domains
if ($domains ne "all") {
my (%domains, @accts2);
my @domains = split(/\s+/, $domains);
foreach (@domains) { $domains{$_} = 1; }
foreach (@accts) {
push @accts2, $_ if ((/^.+\@([^\@]+)$/) && defined($domains{$1}));
}
@accts = @accts2;
}
# Order list of domains
ORDER: {
local $_ = $order;
/^addr$/ && do {
@accts = sort @accts;
};
/^revaddr$/ && do {
@accts = reverse sort @accts;
};
/^domain$/ && do {
@accts = sort {
my ($ua, $da) = ($a =~ /^(.+)\@([^\@]+)$/);
my ($ub, $db) = ($b =~ /^(.+)\@([^\@]+)$/);
"$da\@$ua" cmp "$db\@$ub";
} @accts;
};
/^revdomain$/ && do {
@accts = sort {
my ($ua, $da) = ($a =~ /^(.+)\@([^\@]+)$/);
my ($ub, $db) = ($b =~ /^(.+)\@([^\@]+)$/);
"$db\@$ub" cmp "$da\@$ua";
} @accts;
};
/^quota$/ && do {
@accts = ();
foreach my $key (
sort { ($quotas{$b}=~/^\s*([\d\.]+)/)[0] <=> ($quotas{$a}=~/^\s*([\d\.]+)/)[0] }
keys %quotas) {
push @accts, $key;
}
};
/^revquota$/ && do {
@accts = ();
foreach my $key (
sort { ($quotas{$a}=~/^\s*([\d\.]+)/)[0] <=> ($quotas{$b}=~/^\s*([\d\.]+)/)[0] }
keys %quotas) {
push @accts, $key;
}
};
}
if ($addmain eq "once") {
unshift @accts, $Cpanel::user;
$quotas{$Cpanel::user} = $quotamain;
}
# Place navigation bar here?
if ($length > 0 && $length < scalar @accts && ($navpos eq "top" || $navpos eq "both")) {
print_navbar($first, $length, scalar @accts);
}
if ($first < scalar @accts) {
# If outlook include the script
if ($format =~ /outlook/ && !-e "/var/cpanel/disableoea") {
print_outlook_script();
}
my @tokens = split(/\s/, $format);
# Print out table header
print "
\n\n";
for (my ($i, $neworder, $class) = (0, undef, undef);
$i < scalar @tokens ; $i++) {
$class = ($i == 0) ? "hcc-first" :
($i == (scalar @tokens)-1) ? "hcc-last" :
"hcc-col-" . ($i+1);
print "| ";
HEADER: {
local $_ = $tokens[$i];
/^address$/ && do {
$neworder = ($order eq "addr") ? "revaddr" : "addr";
print "" . lang('Address') . "";
last HEADER;
};
/^account$/ && do {
$neworder = ($order eq "addr") ? "revaddr" : "addr";
print "" . lang('Account') . "";
last HEADER;
};
/^login$/ && do {
$neworder = ($order eq "addr") ? "revaddr" : "addr";
print "" . lang('Login') . "";
last HEADER;
};
/^domain$/ && do {
$neworder = ($order eq "domain") ? "revdomain" : "domain";
print "" . lang('Domain') . "";
last HEADER;
};
/^quota$/ && do {
$neworder = ($order eq "quota") ? "revquota" : "quota";
print "" . lang('Quota') . "";
last HEADER;
};
print " ";
}
print " | ";
}
print "\n
\n";
# Print out rows according to desired format
my ($main, $account, $domain);
my $last = ($length) ? min($first+$length, scalar(@accts))
: scalar(@accts);
for (my $i = $first, my $class; $i < $last; $i++) {
$main = ($addmain eq "once" && $i == 0) ? 1 : 0;
if ($main) {
($account, $domain) = ($Cpanel::user, $Cpanel::CPDATA{'DNS'});
} else {
($account, $domain) = split(/@/, $accts[$i], 2);
}
$class = ($i % 2) ? "hcc-odd" : "hcc-even";
print "\n";
for (my $j = 0, my $class; $j < scalar @tokens; $j++) {
$class = ($j == 0) ? "hcc-first" :
($j == (scalar @tokens)-1) ? "hcc-last" :
"hcc-col-" . ($j+1);
print "";
SWITCH: {
local $_ = $tokens[$j];
/^account$/ && do {
print $account;
last SWITCH;
};
/^address$/ && do {
print $account . '@' . $domain;
last SWITCH;
};
/^login$/ && do {
if ($main) { print $account; }
else {
print $account . $userchar . $domain;
}
last SWITCH;
};
/^domain$/ && do {
print $domain;
last SWITCH;
};
/^arrow$/ && do {
print "=>";
last SWITCH;
};
/^arrowimg$/ && do {
print " ";
last SWITCH;
};
/^quota$/ && do {
my $index = ($main) ? $Cpanel::user : $account.'@'.$domain;
my $quota = get_used_space($main, $account, $domain);
my ($dummy, $quotamax) = split(/\//, $quotas{$index}, 2);
printf("%.2f", $quota/1024/1024);
if ($quotamax>0) { printf("/%.2f", $quotamax/1024/1024); }
print " MB";
last SWITCH;
};
/^delete$/ && do {
if ($main) { print " " }
else {
print "" . lang('Delete') . "";
}
last SWITCH;
};
/^deleteimg$/ && do {
if ($main) { print " " }
else {
print "";
print " ";
}
last SWITCH;
};
/^webmail$/ && do {
my $login = $account . (($main) ? "" : "+$domain");
my $scheme = "http" . (($ENV{'HTTPS'}) ? "s" : "");
print "" . lang("Webmail") . "";
last SWITCH;
};
/^webmailimg$/ && do {
my $login = $account . (($main) ? "" : "+$domain");
my $scheme = "http" . (($ENV{'HTTPS'}) ? "s" : "");
print "";
print " ";
last SWITCH;
};
/^edquota$/ && do {
if ($main) { print " " }
else {
print "" . lang('EditQuota') . "";
}
last SWITCH;
};
/^edquotaimg$/ && do {
if ($main) { print " " }
else {
print "";
print " ";
}
last SWITCH;
};
/^edpasswd$/ && do {
if ($main) { print " " }
else {
print "" . lang('EditPassword') . "";
}
last SWITCH;
};
/^edpasswdimg$/ && do {
if ($main) { print " " }
else {
print "";
print " ";
}
last SWITCH;
};
/^aging$/ && do {
if ($main) { print " " }
else {
print "" . lang('Aging') . "";
}
last SWITCH;
};
/^agingimg$/ && do {
if ($main) { print " " }
else {
print "";
print " ";
}
last SWITCH;
};
/^outlook$/ && do {
if (!-e "/var/cpanel/disableoea") {
print "";
}
last SWITCH;
};
/^{(.+)}$/ && do {
print $1;
last SWITCH;
};
/^#$/ && do {
print $i+1;
last SWITCH;
};
print " ";
}
print " | ";
}
print "\n
\n";
}
print "
\n\n";
}
# Place navigation bar here?
if ($length > 0 && $length < scalar @accts && ($navpos eq "bottom" || $navpos eq "both")) {
print_navbar($first, $length, scalar @accts);
}
}
sub print_outlook_script {
print "\n";
}
sub get_used_space {
my ($main, $account, $domain) = @_;
if ($main) {
return (-s "$Cpanel::homedir/mail/inbox");
} else {
return (-s "$Cpanel::homedir/mail/$domain/$account/inbox");
}
}
sub min {
return ($_[0]>$_[1])?$_[1]:$_[0];
}
sub max {
return ($_[0]>$_[1])?$_[0]:$_[1];
}
sub print_navbar {
my ($first, $length, $accounts) = @_;
print "";
}
sub HCCEmail_AddAccounts {
my $csl = shift;
my @csl = split(/\n/, $csl);
my $n = 0;
foreach my $line (@csl) {
# Increment line counter
$n++;
# Remove trailing spaces
$line =~ s/^\s*//;
$line =~ s/\s*$//;
# Each line has this format: account@domain, password[, quota]
my ($addr, $pass, $quota) = ("", "", undef);
my ($acct, $domain) = (undef, undef);
($addr, $pass, $quota) = split(/,\s*/, $line, 3);
if (!defined($quota)) { $quota = 0; }
if ($addr =~ /^(.+)\@([^\@]+)$/) {
$acct = $1;
$domain = $2;
} else {
print "
\nLine $n: " . lang("WrongAddressFormat") . "
\n";
next;
}
if ($pass eq "") {
print "
\nLine $n: " . lang("MissingPassword") . "
\n";
next;
}
print "
\n" . lang("AddingAccount");
print " $acct\@$domain
\n";
my @RARGS = ($acct, $pass, $quota, $domain);
::emailhandoff("addpop", @RARGS);
}
}
sub lang {
my ($key) = @_;
return $Cpanel::Lang::LANG{$Cpanel::CPDATA{LANG}}{"${prefix}_${key}"};
}
1;