#!/usr/bin/perl -w

# mailbox check module

# Version: $Revision: 1.3 $
# Author: Eric Garrido
# Date: $Date: 2006/03/02 02:02:51 $
#
# Copyright (c) 2006
# The Trustees of Columbia University in the City of New York
# Columbia University Information Technology
# 
# License restrictions apply, see doc/license.html for details. 

use strict;
use FindBin;
use lib "$FindBin::Bin/../common";
use Survivor::Check;
use Mail::IMAPClient;

my %argformat = ('protocol' => 'optional string one(imap,imaps) default(imaps)',
		 'username' => 'string',
		 'password' => 'password',
		 'delete'  => 'optional boolean default(true)',
		 );

sub MailboxValidate {
    return(MODEXEC_OK, 0, "Test OK");  
}

sub MailboxCheck {

    my $self = shift;
    my $host = shift 
	|| return(MODEXEC_MISCONFIG, 0, "no hostname provided");

    if($host eq ""){
	return(MODEXEC_MISCONFIG, 0, "no hostname provided");
    }

    my $username = $self->Arg('username');
    my $password = $self->Arg('password');
    my $imapserver = $host;
    
    my $rc = MODEXEC_OK;
    my $err;

    my $imap = new Mail::IMAPClient;

    if($self->Arg('protocol') eq 'imaps') {
	# create an ssl socket and give it to IMAP
	my $ssl_socket = new IO::Socket::SSL("$imapserver:imaps");
	unless(defined $ssl_socket) {
	    return(MODEXEC_PROBLEM, 0, "Cannot create SSL socket: $@");
	}
	$imap->Socket($ssl_socket);
	$imap->State(1);
 
    } 
    else {
	# do a regular connect on the imap server
	$imap->Server($imapserver);
	unless($imap->connect()) {
	    return(MODEXEC_PROBLEM, 0, 'Connection refused for imap protocol');
	}
    }
    
    # login to the imap server
    $imap->User($username);
    $imap->Password($password);
     unless($imap->login()) {
	return(MODEXEC_PROBLEM, 0, "Cannot login as $username");
    } 
    else {
	$err = "Success logging in as $username";
    }


    my $folder_name = $host . ' ' . localtime(); 
    
    # create and then select a new mailbox
    if($imap->create($folder_name)) {
	$err = "Success on creating a new mailbox";
    } 
    else {
	return(MODEXEC_PROBLEM, 0,  "Cannot create a new mailbox");
    }

    # make sure the new mailbox exists
    if($imap->exists($folder_name)){
	$err = "Mailbox seems to exist after creation";
    }
    else {
	return(MODEXEC_PROBLEM, 0,  "Mailbox does not exist after creation");
    }
    
    # select the new mailbox
    if($imap->select($folder_name)) {
	$err = "Success on selecting the new mailbox";
    } 
    else {
    	return(MODEXEC_PROBLEM, 0,  "Cannot select the new mailbox");
    }

    # close the new mailbox
    if($imap->close()) {
	$err = "Success on closing the new mailbox";
    }
    else {
	return(MODEXEC_PROBLEM, 0,  "Cannot close the new mailbox after selection");
    }

    # rename the new mailbox
    if($imap->rename($folder_name, $folder_name . '1')){
	$err = "Success on renaming the new mailbox";
    }
    else {
	return(MODEXEC_PROBLEM, 0,  "Cannot rename the new mailbox");
    }
  
    # delete the new mailbox, if specified
    if($self->Arg('delete') == 1){
	if($imap->delete($folder_name . '1')){
	    $err = "Mailbox successfully deleted after creation and renaming";
	} 
	else {
	    return(MODEXEC_PROBLEM, 0,  "Cannot delete the new mailbox");
	}
    }

    # logout
    unless($imap->logout()) {
	return(MODEXEC_PROBLEM, 0, "Cannot logout as $username");
    }

    if($rc == MODEXEC_OK) {
	return($rc, 1, $err);
    } else {
	return($rc, 0, $err);
    }

}

# begin main block
{

    my $sc = new Survivor::Check();
    
    $sc->GetOpt(\%argformat, \&MailboxValidate);
    
    if($sc->Arg('protocol') eq 'imaps') {
	require IO::Socket::SSL;
	import IO::Socket::SSL;
    }
    
    if($sc->Arg('protocol') ne 'imap') {
	# Disable threading pending verification that SSL is threadsafe
	$sc->UseThreads(0);
    } 
    
    my $result = $sc->Exec(\&MailboxCheck);
    
    exit($result);
    
}
