// The contents of this file are subject to the Mozilla Public License Version 1.1
// (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
// WARRANTY OF ANY KIND, either express or implied. See the License for the specific
// language governing rights and limitations under the License.
// 
// The Original Code is the Venice Web Communities System.
// 
// The Initial Developer of the Original Code is Eric J. Bowersox <erbo@ricochet.com>,
// for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
// Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
// 
// Contributor(s): 

importPackage(java.util);
importPackage(Packages.com.silverwrist.util);
importPackage(Packages.com.silverwrist.venice.core);
importPackage(Packages.com.silverwrist.venice.except);
importPackage(Packages.com.silverwrist.venice.ui);
importPackage(Packages.com.silverwrist.venice.ui.view);

// get the request object
rinput = bsf.lookupBean("request");

if (!(rinput.user.hasAdminAccess()))
{ // you don't have permission to administer the system
  vlib.output(new ErrorBox("Access Error","You do not have permission to administer the system.",null));
  vlib.done();

} // end if

// create the IP ban add dialog
dlg = rinput.getDialog("ipban.add");

if ("GET"==rinput.verb)
{ // fill in default values and put forth the dialog
  dlg.setValue("mask","255.255.255.255");
  dlg.setValue("etime","1");
  vlib.output(dlg);
  vlib.done();

} // end if

// everything that follows is for a POST operation
op = dlg.whichButton(rinput) + "";
if (op=="cancel")
{ // user cancelled create - bounce back to the target
  vlib.output(new Redirect("sysadmin/ip_bans.js.vs",LinkTypes.SERVLET));
  vlib.done();

} // end if

rc = null;
if (op=="add")
{ // load the dialog
  dlg.load(rinput);

  try
  { // validate the dialog
    dlg.validate();

    // load the admin interface
    adm = rinput.user.adminInterface;

    // determine the expire date
    exp_date = null;
    if (dlg.getValue("echeck"))
    { // create a GregorianCalendar and use it to figure the expiration date
      cal = new java.util.GregorianCalendar();
      unit = dlg.getValue("eunit");
      if (unit=="D")
        cal.add(Calendar.DATE,dlg.getValue("etime"));
      else if (unit=="W")
        cal.add(Calendar.DATE,dlg.getValue("etime") * 7);
      else if (unit=="M")
        cal.add(Calendar.MONTH,dlg.getValue("etime"));
      else if (unit=="Y")
        cal.add(Calendar.YEAR,dlg.getValue("etime"));
      exp_date = cal.getTime();

    } // end if

    msg = dlg.getValue("msg");
    if (StringUtil.isStringEmpty(msg))
      msg = "This IP address has been banned from logging in by an administrator.";
    adm.addIPBan(dlg.getValue("address"),dlg.getValue("mask"),exp_date,msg);
    rc = new Redirect("sysadmin/ip_bans.js.vs",LinkTypes.SERVLET);

  } // end try
  catch (e)
  { // whoops!
    etype = vlib.exceptionType(e) + "";
    if (etype.match("ValidationException"))
    { // put the dialog back up on a validation error
      dlg.setErrorMessage(e.message + " Please try again.");
      rc = dlg;

    } // end if
    else if (etype.match("DataException"))
      rc = new ErrorBox("Database Error",e.message);
    else
      rc = e;

  } // end catch

} // end if
else
{ // no dialog present
  logger.error("no known button click on POST to ip_ban_add.js");
  rc = new ErrorBox("Internal Error","Unknown command button pressed","sysadmin/ip_bans.js.vs");

} // end else

vlib.output(rc);
