#!/usr/bin/perl
#Copyright (c) 2009, Zane C. Bowers
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without modification,
#are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
#INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
#THE POSSIBILITY OF SUCH DAMAGE.

use warnings;
use strict;
use ZConf::Mail;
use Getopt::Std;
use Curses::UI;

$Getopt::Std::STANDARD_HELP_VERSION=1;

#version function
sub main::VERSION_MESSAGE {
        print "zcmailaccountmanage 0.0.0\n";
};

#print help
sub main::HELP_MESSAGE {
        print "\n";
		exit 1;
};

#gets the options
my %opts=();
getopts('a:', \%opts);

#
sub new{
	my %self=%{$_[0]};
	my $type=$_[1];

	#if there is not defined, there was an error with what ever called it
	if (!defined($type)) {
		return undef;
	}

	$self{newDesc}->text('Name for new '.$type.' account.');

	$self{newWin}->focus;
}

#creates a new one
sub createnew{
	my %self=%{$_[0]};

	#if this is called, it means new has never been called
	if (!defined($self{newType})) {
		return undef;
	}

	my %values;

	$values{type}=$self{newType};
	$values{account}=$self{newName}->get();

	#make sure the account name is not blank or undef
	if (!defined($values{account})){return undef;}
	if ($values{account} eq ''){return undef;}

	#initializes the account with blank values
	my $int=0;
	while (defined($self{mail}->{legal}{$self{newType}}[$int])) {
		$values{$self{mail}->{legal}{$self{newType}}[$int]}='';
		$int++;
	}

	#add it
	$self{mail}->createAccount({%values});
	

	loadlist(\%self, $self{newType});
}

#loads a list...
sub loadlist{
	my %self=%{$_[0]};
	my $type=$_[1];
	
	#gets the accounts
	my @accounts=$self{mail}->getAccounts;

	#puts the list together
	my $int=0;
	my @accountsA;
	my %accountsH;
	while (defined($accounts[$int])) {
		if ($accounts[$int] =~ /^$type\//) {
			my $account=$accounts[$int];
			$account=~s/^$type\///;
			
			push(@accountsA, $account);
			
			$accountsH{$account}=$account;
		}

		$int++;
	}

	$self{$type.'AccountLB'}->values(\@accountsA);
	$self{$type.'AccountLB'}->labels(\%accountsH);

	return 1;
}

my %self;
$self{newType}=undef;

#init ZConf::Mail
$self{mail}=ZConf::Mail->new;
#makes sure it is sendable
if ($self{mail}->{error}) {
	warn('zcmailaccountmanage: Error initiating ZConf::Mail.');
	exit $self{mail}->{error};
}

#init cui
$self{cui}=Curses::UI->new( -clear_on_exit => 1);

#creates the window
$self{win}=$self{cui}->add('window', 'Window');

#sets up the new window for when needed later...
$self{newWin}=$self{cui}->add('newWin','Window');
$self{newLabel}=$self{newWin}->add(undef, 'Label', -Text=>'name:',
								   -x=>'0', -y=>1);
$self{newDesc}=$self{newWin}->add(undef, 'Label', -x=>'0', -y=>0);

$self{newName}=$self{newWin}->add('newName', 'TextEntry', -x=>6, -y=>1);
$self{newCancel}=$self{newWin}->add('newCancel', 'Buttonbox', -y=>3, -width=>10,
									-buttons=>[{-label=>'Cancel',
												-onpress=>sub{
													$self{win}->focus;
												}},
											   ]);
$self{newCreate}=$self{newWin}->add('newCreate', 'Buttonbox', -y=>4, -width=>10,
									-buttons=>[{-label=>'OK',
												-onpress=>sub{
													createnew(\%self);
													$self{win}->focus;
												}},
											   ]);


#creates the notebook widget
$self{notebook}=$self{win}->add(undef, 'Notebook');

#adds the pages
$self{exec}=$self{notebook}->add_page('Exec');
$self{imap}=$self{notebook}->add_page('IMAP');
$self{maildir}=$self{notebook}->add_page('Maildir');
$self{mbox}=$self{notebook}->add_page('MBox');
$self{pop3}=$self{notebook}->add_page('POP3');
$self{smtp}=$self{notebook}->add_page('SMTP');

#add the list box
$self{execAccountLB}=$self{exec}->add('execAccountLB', 'Listbox', -y=>0,
									  -x=>0, -width=>20, -height=>8, -border=>1,
									  -onchange=>sub{
										  my $value=$self{execAccountLB}->get_active_value;
										  $self{execAccountName}->text('Name: exec/'.$value);
										  my %values=$self{mail}->getAccountArgs('exec/'.$value);
										  $self{execDeliver}->text($values{deliver});
									  }
									  );
loadlist(\%self, 'exec');

#add the list box
$self{imapAccountLB}=$self{imap}->add('imapAccountLB', 'Listbox', -y=>0,
									  -x=>0, -width=>20, -height=>8, -border=>1,
									  -onchange=>sub{
										  my $value=$self{imapAccountLB}->get_active_value;
										  $self{imapAccountName}->text('Name: imap/'.$value);
										  my %values=$self{mail}->getAccountArgs('imap/'.$value);
										  $self{imapServer}->text($values{server});
										  $self{imapPort}->text($values{port});
										  $self{imapUser}->text($values{user});
										  $self{imapPass}->text($values{pass});
										  $self{imapTimeout}->text($values{timeout});
										  $self{imapUseSSL}->text($values{useSSL});
										  $self{imapDeliverTo}->text($values{deliverTo});
										  $self{imapDeliverToFolder}->text($values{deliverToFolder});
										  $self{imapFetchable}->text($values{fetchable});
									  }
									  );
loadlist(\%self, 'imap');

#add the list box
$self{maildirAccountLB}=$self{maildir}->add('maildirAccountLB', 'Listbox', -y=>0,
									  -x=>0, -width=>20, -height=>8, -border=>1,
									  -onchange=>sub{
										  my $value=$self{maildirAccountLB}->get_active_value;
										  $self{maildirAccountName}->text('Name: maildir/'.$value);
										  my %values=$self{mail}->getAccountArgs('maildir/'.$value);
										  $self{maildirMaildir}->text($values{maildir});
										  $self{maildirDeliverTo}->text($values{deliverTo});
										  $self{maildirDeliverToFolder}->text($values{deliverToFolder});
										  $self{maildirFetchable}->text($values{fetchable});
									  }
									  );
loadlist(\%self, 'maildir');

#add the list box
$self{mboxAccountLB}=$self{mbox}->add('mboxAccountLB', 'Listbox', -y=>0,
									  -x=>0, -width=>20, -height=>8, -border=>1,
									  -onchange=>sub{
										  my $value=$self{mboxAccountLB}->get_active_value;
										  $self{mboxAccountName}->text('Name: mbox/'.$value);
										  my %values=$self{mail}->getAccountArgs('mbox/'.$value);
										  $self{mboxMBox}->text($values{mbox});
										  $self{mboxDeliverTo}->text($values{deliverTo});
										  $self{mboxDeliverToFolder}->text($values{deliverToFolder});
										  $self{mboxFetchable}->text($values{fetchable});
									  }
									  );
loadlist(\%self, 'mbox');

#add the list box
$self{pop3AccountLB}=$self{pop3}->add('pop3AccountLB', 'Listbox', -y=>0,
									  -x=>0, -width=>20, -height=>8, -border=>1,
									  -onchange=>sub{
										  my $value=$self{pop3AccountLB}->get_active_value;
										  $self{pop3AccountName}->text('Name: pop3/'.$value);
										  my %values=$self{mail}->getAccountArgs('pop3/'.$value);
										  $self{pop3Server}->text($values{server});
										  $self{pop3Port}->text($values{port});
										  $self{pop3User}->text($values{user});
										  $self{pop3Pass}->text($values{pass});
										  $self{pop3AuthType}->text($values{auth});
										  $self{pop3UseSSL}->text($values{useSSL});
										  $self{pop3DeliverTo}->text($values{deliverTo});
										  $self{pop3DeliverToFolder}->text($values{deliverToFolder});
										  $self{pop3Fetchable}->text($values{fetchable});
									  }
									  );
loadlist(\%self, 'pop3');

#add the list box
$self{smtpAccountLB}=$self{smtp}->add('smtpAccountLB', 'Listbox', -y=>0,
									  -x=>0, -width=>20, -height=>8, -border=>1,
									  -onchange=>sub{
										  my $value=$self{smtpAccountLB}->get_active_value;
										  $self{smtpAccountName}->text('Name: smtp/'.$value);
										  my %values=$self{mail}->getAccountArgs('smtp/'.$value);
										  $self{smtpServer}->text($values{server});
										  $self{smtpName}->text($values{name});
										  $self{smtpFrom}->text($values{from});
										  $self{smtpPort}->text($values{port});
										  $self{smtpUser}->text($values{user});
										  $self{smtpPass}->text($values{pass});
										  $self{smtpTimeout}->text($values{timeout});
										  $self{smtpAuthType}->text($values{auth});
										  $self{smtpUseSSL}->text($values{useSSL});
										  $self{smtpSaveTo}->text($values{saveTo});
										  $self{smtpSaveToFolder}->text($values{saveToFolder});
									  }
									  );
loadlist(\%self, 'smtp');

#sets up the labels for Exec
$self{execDeliverLabel}=$self{exec}->add('execDeliverLabel', 'Label', -Text=>'deliver:',
									   -x=>21, -y=>1
									   );
$self{execAccountName}=$self{exec}->add('execAccountName', 'Label', -Text=>'Name:',
									   -x=>21, -y=>0, -width=>100,
									   );

#sets up the labels IMAP
$self{imapAccountName}=$self{imap}->add('imapAccountName', 'Label', -Text=>'Name:',
									   -x=>21, -width=>100,
									   );
$self{imapServerLabel}=$self{imap}->add('imapServerLabel', 'Label', -Text=>'Server:',
									   -x=>21, -y=>1
									   );
$self{imapPortLabel}=$self{imap}->add('imapPortLabel', 'Label', -Text=>'Port:',
									   -x=>21, -y=>2
									   );
$self{imapUserLabel}=$self{imap}->add('imapUserLabel', 'Label', -Text=>'User:',
									   -x=>21, -y=>3
									   );
$self{imapPassLabel}=$self{imap}->add('imapPassLabel', 'Label', -Text=>'Pass:',
									   -x=>21, -y=>4
									   );
$self{imapTimeoutLabel}=$self{imap}->add('imapTimeoutLabel', 'Label', -Text=>'Time out:',
									   -x=>21, -y=>5
									   );
$self{imapUseSSLLabel}=$self{imap}->add('imapUseSSLLabel', 'Label', -Text=>'Use SSL:',
									   -x=>21, -y=>6
									   );
$self{imapDeliverToLabel}=$self{imap}->add('imapDeliverToLabel', 'Label', -Text=>'Deliver To:',
									   -x=>21, -y=>7
									   );
$self{imapDeliverToFolderLabel}=$self{imap}->add('imapDeliverToFolderLabel', 'Label', -Text=>'+-Folder:',
									   -x=>21, -y=>8
									   );
$self{imapFetchableLabel}=$self{imap}->add('imapFetchableLabel', 'Label', -Text=>'Fetchable:',
									   -x=>21, -y=>9
									   );

#sets up the labels for the maildir
$self{maildirAccountName}=$self{maildir}->add('maildirAccountName', 'Label', -Text=>'Name:',
									   -x=>21, -y=>0, -width=>100,
									   );
$self{maildirLabel}=$self{maildir}->add('maildirLabel', 'Label', -Text=>'Maildir:',
									   -x=>21, -y=>1
									   );
$self{maildirDeiverToLabel}=$self{maildir}->add('maildirDeliverToLabel', 'Label', -Text=>'Deliver To:',
									   -x=>21, -y=>2
									   );
$self{maildirDeiverToFolderLabel}=$self{maildir}->add('maildirDeliverToFolderLabel', 'Label', -Text=>'+-Folder:',
									   -x=>21, -y=>3
									   );
$self{maildirFetchableLabel}=$self{maildir}->add('maildirFetchableLabel', 'Label', -Text=>'Fetchable:',
									   -x=>21, -y=>4
									   );

#sets up the labels for the mbox
$self{mboxAccountName}=$self{mbox}->add('mboxAccountName', 'Label', -Text=>'Name:',
									   -x=>21, -y=>0, -width=>100,
									   );
$self{mboxLabel}=$self{mbox}->add('mboxLabel', 'Label', -Text=>'MBox:',
									   -x=>21, -y=>1
									   );
$self{mboxDeliverToLabel}=$self{mbox}->add('mboxDeliverToLabel', 'Label', -Text=>'Deliver To:',
									   -x=>21, -y=>2
									   );
$self{mboxDeliverToFolderLabel}=$self{mbox}->add('mboxDeliverToFolderLabel', 'Label', -Text=>'+-Folder:',
									   -x=>21, -y=>3
									   );
$self{mboxFetchableLabel}=$self{mbox}->add('mboxFetchableLabel', 'Label', -Text=>'Fetchable:',
									   -x=>21, -y=>4
									   );

#sets up the labels for the pop3
$self{pop3AccountName}=$self{pop3}->add('pop3AccountName', 'Label', -Text=>'Name:',
									   -x=>21, -y=>0, -width=>100,
									   );
$self{pop3ServerLabel}=$self{pop3}->add('pop3ServerLabel', 'Label', -Text=>'Server:',
									   -x=>21, -y=>1
									   );
$self{pop3PortLabel}=$self{pop3}->add('pop3PortLabel', 'Label', -Text=>'Port:',
									   -x=>21, -y=>2
									   );
$self{pop3UserLabel}=$self{pop3}->add('pop3UserLabel', 'Label', -Text=>'User:',
									   -x=>21, -y=>3
									   );
$self{pop3PassLabel}=$self{pop3}->add('pop3PassLabel', 'Label', -Text=>'Pass:',
									   -x=>21, -y=>4
									   );
$self{pop3AuthTypeLabel}=$self{pop3}->add('pop3AuthTypeLabel', 'Label', -Text=>'Auth Type:',
									   -x=>21, -y=>5
									   );
$self{pop3UseSSLLabel}=$self{pop3}->add('pop3UseSSLLabel', 'Label', -Text=>'Use SSL:',
									   -x=>21, -y=>6
									   );
$self{pop3DeliverToLabel}=$self{pop3}->add('pop3DeliverToLabel', 'Label', -Text=>'Deliver To:',
									   -x=>21, -y=>7
									   );
$self{pop3DeliverToFolderLabel}=$self{pop3}->add('pop3DeliverToFolderLabel', 'Label', -Text=>'+-Folder:',
									   -x=>21, -y=>8
									   );
$self{pop3FetchableLabel}=$self{pop3}->add('pop3FetchableLabel', 'Label', -Text=>'Fetchable:',
									   -x=>21, -y=>9
									   );

#sets up the labels for the pop3
$self{smtpAccountName}=$self{smtp}->add('smtpAccountName', 'Label', -Text=>'Name:',
									   -x=>21, -y=>0, -width=>100,
									   );
$self{smtpServerLabel}=$self{smtp}->add('smtpServerLabel', 'Label', -Text=>'Server:',
									   -x=>21, -y=>1
									   );
$self{smtpPortLabel}=$self{smtp}->add('smtpPortLabel', 'Label', -Text=>'Port:',
									   -x=>21, -y=>2
									   );
$self{smtpNameLabel}=$self{smtp}->add('smtpNameLabel', 'Label', -Text=>'Name:',
									   -x=>21, -y=>3
									   );
$self{smtpFromLabel}=$self{smtp}->add('smtpFromLabel', 'Label', -Text=>'From:',
									   -x=>21, -y=>4
									   );
$self{smtpUserLabel}=$self{smtp}->add('smtpUserLabel', 'Label', -Text=>'User:',
									   -x=>21, -y=>5
									   );
$self{smtpPassLabel}=$self{smtp}->add('smtpPassLabel', 'Label', -Text=>'Pass:',
									   -x=>21, -y=>6
									   );
$self{smtpPassLabel}=$self{smtp}->add('smtpTimeoutLabel', 'Label', -Text=>'Timeout:',
									   -x=>21, -y=>7
									   );
$self{smtpAuthTypeLabel}=$self{smtp}->add('smtpAuthTypeLabel', 'Label', -Text=>'Auth Type:',
									   -x=>21, -y=>8
									   );
$self{smtpUseSSLLabel}=$self{smtp}->add('smtpUseSSLLabel', 'Label', -Text=>'Use SSL:',
									   -x=>21, -y=>9
									   );
$self{smtpSaveToLabel}=$self{smtp}->add('smtpSaveToLabel', 'Label', -Text=>'Save To:',
									   -x=>21, -y=>10
									   );
$self{smtpSaveToFolderLabel}=$self{smtp}->add('smtpSaveToFolderLabel', 'Label', -Text=>'+-Folder:',
									   -x=>21, -y=>11
									   );

#sets up the text fields for exec
$self{execDeliver}=$self{exec}->add('execDeliver', 'TextEntry', -x=>31, -y=>1);

#sets up the text fields for IMAP
$self{imapServer}=$self{imap}->add('imapServer', 'TextEntry', -x=>29, -y=>1);
$self{imapPort}=$self{imap}->add('imapPort', 'TextEntry', -x=>27, -y=>2);
$self{imapUser}=$self{imap}->add('imapUser', 'TextEntry', -x=>27, -y=>3);
$self{imapPass}=$self{imap}->add('imapPass', 'TextEntry', -x=>27, -y=>4, -password=>'*');
$self{imapTimeout}=$self{imap}->add('imapTimeout', 'TextEntry', -x=>31, -y=>5);
$self{imapUseSSL}=$self{imap}->add('imapUseSSL', 'TextEntry', -x=>30, -y=>6);
$self{imapDeliverTo}=$self{imap}->add('imapDeliverTo', 'TextEntry', -x=>33, -y=>7);
$self{imapDeliverToFolder}=$self{imap}->add('imapDeliverToFolder', 'TextEntry', -x=>31, -y=>8);
$self{imapFetchable}=$self{imap}->add('imapFetchable',, 'TextEntry', -x=>32, -y=>9);


#sets up the text fields for pop3
$self{pop3Server}=$self{pop3}->add('pop3Server', 'TextEntry', -x=>29, -y=>1);
$self{pop3Port}=$self{pop3}->add('pop3Port', 'TextEntry', -x=>27, -y=>2);
$self{pop3User}=$self{pop3}->add('pop3User', 'TextEntry', -x=>27, -y=>3);
$self{pop3Pass}=$self{pop3}->add('pop3Pass', 'TextEntry', -x=>27, -y=>4, -password=>'*');
$self{pop3AuthType}=$self{pop3}->add('pop3AuthType', 'TextEntry', -x=>32, -y=>5,);
$self{pop3UseSSL}=$self{pop3}->add('pop3UseSSL', 'TextEntry', -x=>30, -y=>6);
$self{pop3DeliverTo}=$self{pop3}->add('pop3DeliverTo', 'TextEntry', -x=>33, -y=>7);
$self{pop3DeliverToFolder}=$self{pop3}->add('pop3DeliverToFolder', 'TextEntry', -x=>31, -y=>8);
$self{pop3Fetchable}=$self{pop3}->add('pop3Fetchable', 'TextEntry', -x=>31, -y=>9);

#sets up the text fields for maildir
$self{maildirMaildir}=$self{maildir}->add('maildirMaildir', 'TextEntry', -x=>30, -y=>1);
$self{maildirDeliverTo}=$self{maildir}->add('maildirDeliverTo', 'TextEntry', -x=>33, -y=>2);
$self{maildirDeliverToFolder}=$self{maildir}->add('maildirDeliverToFolder', 'TextEntry', -x=>31, -y=>3);
$self{maildirFetchable}=$self{maildir}->add('maildirFetchable','TextEntry', -x=>31, -y=>4);

#sets up the text fields for mbox
$self{mboxMBox}=$self{mbox}->add('mboxMBox', 'TextEntry', -x=>27, -y=>1);
$self{mboxDeliverTo}=$self{mbox}->add('mboxDeliverTo', 'TextEntry', -x=>33, -y=>2);
$self{mboxDeliverToFolder}=$self{mbox}->add('mboxDeliverToFolder', 'TextEntry', -x=>31, -y=>3);
$self{mboxFetchable}=$self{mbox}->add('mboxUseSSL', 'TextEntry', -x=>31, -y=>4);

#sets up the text fields for smtp
$self{smtpServer}=$self{smtp}->add('smtpServer', 'TextEntry', -x=>29, -y=>1);
$self{smtpPort}=$self{smtp}->add('smtpPort', 'TextEntry', -x=>27, -y=>2);
$self{smtpName}=$self{smtp}->add('smtpName', 'TextEntry', -x=>27, -y=>3);
$self{smtpFrom}=$self{smtp}->add('smtpFrom', 'TextEntry', -x=>27, -y=>4);
$self{smtpUser}=$self{smtp}->add('smtpUser', 'TextEntry', -x=>27, -y=>5);
$self{smtpPass}=$self{smtp}->add('smtpPass', 'TextEntry', -x=>27, -y=>6, -password=>'*');
$self{smtpTimeout}=$self{smtp}->add('smtpTimeout', 'TextEntry', -x=>30, -y=>7);
$self{smtpAuthType}=$self{smtp}->add('smtpAuthType', 'TextEntry', -x=>32, -y=>8);
$self{smtpUseSSL}=$self{smtp}->add('smtpUseSSL', 'TextEntry', -x=>30, -y=>9);
$self{smtpSaveTo}=$self{smtp}->add('smtpSaveTo', 'TextEntry', -x=>30, -y=>10);
$self{smtpSaveToFolder}=$self{smtp}->add('smtpSaveToFolder', 'TextEntry', -x=>31, -y=>11);

#creates the exec buttons
$self{execButtons}=$self{exec}->add('execButtons', 'Buttonbox', -y=>9, -width=>10,
									-buttons=>[{-label=>'New',
												-onpress=>sub{
													$self{newType}='exec';
													new(\%self, 'exec');
												}},
											   ]);
$self{execButtonDel}=$self{exec}->add('execButtonDel', 'Buttonbox', -y=>11, -width=>10,
									-buttons=>[{-label=>'Delete',
												-onpress=>sub{
													my $value=$self{execAccountLB}->get_active_value;
													$self{mail}->delAccount('exec/'.$value);
													loadlist(\%self, 'exec');
												}},
											   ]);
$self{execButtonReload}=$self{exec}->add('execButtonReload', 'Buttonbox', -y=>13, -width=>10,
										 -buttons=>[{-label=>'Reload',
												-onpress=>sub{
													my $value=$self{execAccountLB}->get_active_value;
													$self{execAccountName}->text('Name: exec/'.$value);
													my %values=$self{mail}->getAccountArgs('exec/'.$value);
													$self{execDeliver}->text($values{deliver});
												}},
											   ]);
$self{execButtonSave}=$self{exec}->add('execButtonSave', 'Buttonbox', -y=>15, -width=>10,
									-buttons=>[{-label=>'Save',
												-onpress=>sub{												
													my %values;
													$values{type}='exec';
													$values{account}=$self{execAccountLB}->get_active_value;
													$values{deliver}=$self{execDeliver}->get();
													$self{mail}->modAccount({%values})
												}},
											   ]);
$self{execButtonQuit}=$self{exec}->add('execButtonQuit', 'Buttonbox', -y=>17, -width=>10,
									-buttons=>[{-label=>'Quit', -onpress=>sub{exit 0}},
											   ]);

#creates the imap buttons
#these are all done as seperate button boxes for mouse selection purposes
$self{imapButtonNew}=$self{imap}->add('imapButtons', 'Buttonbox', -y=>9, -width=>10,
								-buttons=>[{-label=>'New',
											-onpress=>sub{
												$self{newType}='imap';
												new(\%self, 'imap');
											}},
										   ]);
$self{imapButtonDel}=$self{imap}->add('imapButtonDel', 'Buttonbox', -y=>11, -width=>10,
									-buttons=>[{-label=>'Delete',
												-onpress=>sub{
													my $value=$self{imapAccountLB}->get_active_value;
													$self{mail}->delAccount('imap/'.$value);
													loadlist(\%self, 'imap');
												}},
											   ]);
$self{imapButtonReload}=$self{imap}->add('imapButtonReload', 'Buttonbox', -y=>13, -width=>10,
									-buttons=>[{-label=>'Reload',
												-onpress=>sub{
													my $value=$self{imapAccountLB}->get_active_value;
													$self{imapAccountName}->text('Name: imap/'.$value);
													my %values=$self{mail}->getAccountArgs('imap/'.$value);
													$self{imapServer}->text($values{server});
													$self{imapPort}->text($values{port});
													$self{imapUser}->text($values{user});
													$self{imapPass}->text($values{pass});
													$self{imapTimeout}->text($values{timeout});
													$self{imapUseSSL}->text($values{useSSL});
													$self{imapDeliverTo}->text($values{deliverTo});
													$self{imapDeliverToFolder}->text($values{deliverToFolder});
													$self{imapFetchable}->text($values{fetchable});
												}},
											   ]);
$self{imapButtonSave}=$self{imap}->add('imapButtonSave', 'Buttonbox', -y=>15, -width=>10,
									-buttons=>[{-label=>'Save',
												-onpress=>sub{
													my %values;
													$values{type}='imap';
													$values{account}=$self{imapAccountLB}->get_active_value;
													$values{server}=$self{imapServer}->get();
													$values{port}=$self{imapPort}->get();
													$values{user}=$self{imapUser}->get();
													$values{pass}=$self{imapPass}->get();
													$values{timeout}=$self{imapTimeout}->get();
													$values{useSSL}=$self{imapUseSSL}->get();
													$values{deliverTo}=$self{imapDeliverTo}->get();
													$values{deliverToFolder}=$self{imapDeliverToFolder}->get();
													$values{fetchable}=$self{imapFetchable}->get();
													$self{mail}->modAccount({%values})
												}},
											   ]);
$self{imapButtonQuit}=$self{imap}->add('imapButtonQuit', 'Buttonbox', -y=>17, -width=>10,
									-buttons=>[{-label=>'Quit', -onpress=>sub{exit 0}},
											   ]);

#creates the maildir buttons
$self{maildirButtonNew}=$self{maildir}->add('maildirButtonNew', 'Buttonbox', -y=>9, -width=>10,
										  -buttons=>[{-label=>'New',
													  -onpress=>sub{
														  $self{newType}='maildir';
														  new(\%self, 'maildir');
													  }},
													 ]);
$self{maildirButtonDel}=$self{maildir}->add('maildirButtonDel', 'Buttonbox', -y=>11, -width=>10,
									-buttons=>[{-label=>'Delete',
												-onpress=>sub{
													my $value=$self{maildirAccountLB}->get_active_value;
													$self{mail}->delAccount('maildir/'.$value);
													loadlist(\%self, 'maildir');
												}},
											   ]);
$self{maildirButtonReload}=$self{maildir}->add('maildirButtonReload', 'Buttonbox', -y=>13, -width=>10,
									-buttons=>[{-label=>'Reload',
												-onpress=>sub{
													my $value=$self{maildirAccountLB}->get_active_value;
													$self{maildirAccountName}->text('Name: maildir/'.$value);
													my %values=$self{mail}->getAccountArgs('maildir/'.$value);
													$self{maildirMaildir}->text($values{maildir});
													$self{maildirDeliverTo}->text($values{deliverTo});
													$self{maildirDeliverToFolder}->text($values{deliverToFolder});
													$self{maildirFetchable}->text($values{fetchable});
									  }},
											   ]);
$self{maildirButtonSave}=$self{maildir}->add('maildirButtonSave', 'Buttonbox', -y=>15, -width=>10,
									-buttons=>[{-label=>'Save',
												-onpress=>sub{												
													my %values;
													$values{type}='maildir';
													$values{account}=$self{maildirAccountLB}->get_active_value;
													$values{deliverTo}=$self{maildirDeliverTo}->get();
													$values{maildir}=$self{maildirMaildir}->get();
													$values{deliverToFolder}=$self{maildirDeliverToFolder}->get();
													$values{fetchable}=$self{maildirFetchable}->get();
													$self{mail}->modAccount({%values});
												}},
											   ]);
$self{maildirButtonQuit}=$self{maildir}->add('maildirButtonQuit', 'Buttonbox', -y=>17, -width=>10,
									-buttons=>[{-label=>'Quit', -onpress=>sub{exit 0}},
											   ]);


#creates the maildir buttons
$self{mboxButtonNew}=$self{mbox}->add('mboxButtonNew', 'Buttonbox', -y=>9, -width=>10,
									  -buttons=>[{-label=>'New',
												  -onpress=>sub{
													  $self{newType}='mbox';
													  new(\%self, 'mbox');
												  }}
											   ]);
$self{mboxButtonDel}=$self{mbox}->add('mboxButtonDel', 'Buttonbox', -y=>11, -width=>10,
									-buttons=>[{-label=>'Delete',
												-onpress=>sub{
													my $value=$self{mboxAccountLB}->get_active_value;
													$self{mail}->delAccount('mbox/'.$value);
													loadlist(\%self, 'mbox');
												}},
											   ]);
$self{mboxButtonReload}=$self{mbox}->add('mboxButtonReload', 'Buttonbox', -y=>13, -width=>10,
									-buttons=>[{-label=>'Reload',
												-onpress=>sub{
													my $value=$self{mboxAccountLB}->get_active_value;
													$self{mboxAccountName}->text('Name: mbox/'.$value);
													my %values=$self{mail}->getAccountArgs('mbox/'.$value);
													$self{mboxMBox}->text($values{mbox});
													$self{mboxDeliverTo}->text($values{deliverTo});
													$self{mboxDeliverToFolder}->text($values{deliverToFolder});
													$self{mboxFetchable}->text($values{fetchable});
												}},
											   ]);
$self{mboxButtonSave}=$self{mbox}->add('mboxButtonSave', 'Buttonbox', -y=>15, -width=>10,
									-buttons=>[{-label=>'Save',
												-onpress=>sub{	
													my %values;
													$values{type}='mbox';
													$values{account}=$self{mboxAccountLB}->get_active_value;
													$values{deliverMBox}=$self{mboxMBox}->get();
													$values{deliverTo}=$self{mboxDeliverTo}->get();
													$values{deliverToFolder}=$self{mboxDeliverToFolder}->get();
													$values{fetchable}=$self{mboxFetchable}->get();
													$self{mail}->modAccount({%values});
												}},
											   ]);
$self{mboxButtonQuit}=$self{mbox}->add('mboxButtonQuit', 'Buttonbox', -y=>17, -width=>10,
									-buttons=>[{-label=>'Quit', -onpress=>sub{exit 0}},
											   ]);

#creates the pop3 buttons
$self{pop3ButtonNew}=$self{pop3}->add('pop3ButtonNew', 'Buttonbox', -y=>9, -width=>10,
									-buttons=>[{-label=>'New',
												-onpress=>sub{
													$self{newType}='pop3';
													new(\%self, 'pop3');
												}}
											   ]);
$self{pop3ButtonDel}=$self{pop3}->add('pop3ButtonDel', 'Buttonbox', -y=>11, -width=>10,
									-buttons=>[{-label=>'Delete',
												-onpress=>sub{
													my $value=$self{pop3AccountLB}->get_active_value;
													$self{mail}->delAccount('pop3/'.$value);
													loadlist(\%self, 'pop3');
												}},
											   ]);
$self{pop3ButtonReload}=$self{pop3}->add('pop3ButtonReload', 'Buttonbox', -y=>13, -width=>10,
									-buttons=>[{-label=>'Reload',
												-onpress=>sub{
													my $value=$self{pop3AccountLB}->get_active_value;
													$self{pop3AccountName}->text('Name: pop3/'.$value);
													my %values=$self{mail}->getAccountArgs('pop3/'.$value);
													$self{pop3Server}->text($values{server});
													$self{pop3Port}->text($values{port});
													$self{pop3User}->text($values{user});
													$self{pop3Pass}->text($values{pass});
													$self{pop3AuthType}->text($values{auth});
													$self{pop3UseSSL}->text($values{useSSL});
													$self{pop3DeliverTo}->text($values{deliverTo});
													$self{pop3DeliverToFolder}->text($values{deliverToFolder});
													$self{pop3Fetchable}->text($values{fetchable});
												}},
											   ]);
$self{pop3ButtonSave}=$self{pop3}->add('pop3ButtonSave', 'Buttonbox', -y=>15, -width=>10,
									-buttons=>[{-label=>'Save',
												-onpress=>sub{	
													my %values;
													$values{type}='pop3';
													$values{account}=$self{pop3AccountLB}->get_active_value;
													$values{server}=$self{pop3Server}->get();
													$values{port}=$self{pop3Port}->get();
													$values{user}=$self{pop3User}->get();
													$values{pass}=$self{pop3Pass}->get();
													$values{pass}=$self{pop3Pass}->get();
													$values{auth}=$self{pop3AuthType}->get();
													$values{useSSL}=$self{pop3UseSSL}->get();
													$values{deliverTo}=$self{pop3DeliverTo}->get();
													$values{deliverToFolder}=$self{pop3DeliverToFolder}->get();
													$values{fetchable}=$self{pop3Fetchable}->get();
													$self{mail}->modAccount({%values});
												}},
											   ]);
$self{pop3ButtonQuit}=$self{pop3}->add('pop3ButtonQuit', 'Buttonbox', -y=>17, -width=>10,
									-buttons=>[{-label=>'Quit', -onpress=>sub{exit 0}},
											   ]);

#creates the smtp buttons
$self{smtpButtonNew}=$self{smtp}->add('smtpButtonNew', 'Buttonbox', -y=>9, -width=>10,
									-buttons=>[{-label=>'New',
												-onpress=>sub{
													$self{newType}='smtp';
													new(\%self, 'smtp');
												}},
											   ]);
$self{smtpButtonDel}=$self{smtp}->add('smtpButtonDel', 'Buttonbox', -y=>11, -width=>10,
									-buttons=>[{-label=>'Delete',
												-onpress=>sub{
													my $value=$self{smtpAccountLB}->get_active_value;
													$self{mail}->delAccount('smtp/'.$value);
													loadlist(\%self, 'smtp');
												}},
											   ]);
$self{smtpButtonReload}=$self{smtp}->add('smtpButtonReload', 'Buttonbox', -y=>13, -width=>10,
									-buttons=>[{-label=>'Reload',
												-onpress=>sub{
													my $value=$self{smtpAccountLB}->get_active_value;
													$self{smtpAccountName}->text('Name: smtp/'.$value);
													my %values=$self{mail}->getAccountArgs('smtp/'.$value);
													$self{smtpServer}->text($values{server});
													$self{smtpName}->text($values{name});
													$self{smtpFrom}->text($values{from});
													$self{smtpPort}->text($values{port});
													$self{smtpUser}->text($values{user});
													$self{smtpPass}->text($values{pass});
													$self{smtpTimeout}->text($values{timeout});
													$self{smtpAuthType}->text($values{auth});
													$self{smtpUseSSL}->text($values{useSSL});
													$self{smtpSaveTo}->text($values{saveTo});
													$self{smtpSaveToFolder}->text($values{saveToFolder});
												}},
											   ]);
$self{smtpButtonSave}=$self{smtp}->add('smtpButtonSave', 'Buttonbox', -y=>15, -width=>10,
									-buttons=>[{-label=>'Save',
												-onpress=>sub{	
													my %values;
													$values{type}='smtp';
													$values{account}=$self{smtpAccountLB}->get_active_value;
													$values{server}=$self{smtpServer}->get();
													$values{name}=$self{smtpName}->get();
													$values{from}=$self{smtpFrom}->get();
													$values{port}=$self{smtpPort}->get();
													$values{user}=$self{smtpUser}->get();
													$values{pass}=$self{smtpPass}->get();
													$values{timeout}=$self{smtpTimeout}->get();
													$values{auth}=$self{smtpAuthType}->get();
													$values{useSSL}=$self{smtpUseSSL}->get();
													$values{saveTo}=$self{smtpSaveTo}->get();
													$values{saveToFolder}=$self{smtpSaveToFolder}->get();
													$self{mail}->modAccount({%values});
												}},
											   ]);
$self{smtpButtonQuit}=$self{smtp}->add('smtpButtonQuit', 'Buttonbox', -y=>17, -width=>10,
									-buttons=>[{-label=>'Quit', -onpress=>sub{exit 0}},
											   ]);

$self{cui}->mainloop;


=head1 NAME

zcmailaccountmanage - A Curses::UI interface to ZConf::Mail accounts.

=head1 SYNOPSIS

No options or etc are taken at this time. Please see the perldoc for
'ZConf::Mail' for more information.

=head1 AUTHOR

Copyright (c) 2009, Zame C. Bowers <vvelox@vvelox.net>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS` OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=head1 SCRIPT CATEGORIES

Desktop
Mail

=head1 OSNAMES

any

=head1 README

zcmailaccountmanage - A Curses::UI interface to ZConf::Mail accounts.

=cut
