<?vsp 
--  
--  $Id$
--  
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--  
--  Copyright (C) 1998-2018 OpenLink Software
--  
--  This project 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; only version 2 of the License, dated June 1991.
--  
--  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.,
--  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--  
--  
 http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
  declare _user_name, _login_ok varchar;
  declare _sid varchar;
  declare _err long varchar;
  declare _pop3old, _pop3new, _poppassold, _poppassnew, _to_do varchar;
  declare _popserver, _popport, _popusername, _poppassword varchar;
  declare _allowed, _num_of_popaccount integer;
  declare _allow_to_add, _popaccount_already_exists, _err_msg varchar;

  declare _err_msg varchar;
  declare _newpopid, abc, _random1,pop3_allowed, _num_of_popaccount integer;

  _random1 := rnd(9999999);
  _to_do := get_keyword('TO_DO', params);
  _sid := '';
  _sid := get_keyword('SESS', params);
   _login_ok := 'NO';
 _popaccount_already_exists := 'NO';
 _popserver := get_keyword('POPSERVER', params);
 _popport := get_keyword('POPPORT', params);
 _popusername := get_keyword('POPUSERNAME', params);
 _poppassword := get_keyword('POPPASSWORD', params);


 if (_sid   <> '')
 {
  for select _username, _userpass from wapsession where sid = _sid  do
  {
   _login_ok := 'YES';
   _user_name := _username;
   update wapsession set _time_last_active = datestring ( now() ) where  sid = _sid;
  }

  select _pop3_allowed into pop3_allowed from wapsetting where _username = _user_name;
  select count (*) into _num_of_popaccount from wappop3account where _username = _user_name;

  if (exists (select * from wappop3account where _username = _user_name and _pop_login_name = _popusername and _pop_server = _popserver))
  {
   _popaccount_already_exists := 'YES';
   _err_msg := 'Pop Account already exists. Can not add duplicate account';
  }else{
   if (pop3_allowed > _num_of_popaccount)
   {
    _allow_to_add := 'YES';
   }else{
    _allow_to_add := 'NO';
    _err_msg := 'Maximum number of POP Account reached';
   }
  }
 }else{
  _login_ok := 'NO';
  _err_msg := 'Login session expired';
 }

if (_login_ok = 'YES' and _allow_to_add = 'YES')
{
 if ( exists(select * from wappop3account where _username = _user_name))
 {
  _newpopid := (select max (_pop_id) from wappop3account where _username = _user_name) + 1;
 }else{
   _newpopid := 1;
 }
 insert into wappop3account (_username, _pop_login_name, _pop_password, _pop_port, _pop_server, _pop_id)
 values (_user_name, _popusername, _poppassword, _popport, _popserver, _newpopid);
 http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
<card id="list">
 <do type="accept" label="Mail Menu">
  <go href="checkmail.vsp?SESS=<?=_sid?>&amp;OPEN=Inbox&amp;REFRESH=<?vsp http(sprintf('%d',_random1));?>"/>
 </do>
 <p>
  Pop Account Added
 </p>
 <p><a href="checkmail.vsp?SESS=<?=_sid?>&amp;OPEN=Inbox&amp;REFRESH=<?vsp http(sprintf('%d',_random1));?>" title="Mail Menu">Mail Menu</a></p>
</card>
</wml>
<?vsp
}

if(_allow_to_add = 'NO' or _popaccount_already_exists = 'YES')
{ http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
  <card id="P1" title="Warning" >
 <do type="accept" label="Back">
  <go href="setting.vsp?SESS=<?=_sid?>"/>
 </do>
    <p> <?=_err_msg?>   </p>
    <p><a href="checkmail.vsp?SESS=<?=_sid?>&amp;OPEN=Inbox&amp;REFRESH=<?vsp http(sprintf('%d',_random1));?>" title="Mail Menu">Mail Menu</a></p>
  </card>
</wml>
<?vsp
}

if(_login_ok = 'NO')
{ http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
  <card id="P1" title="Wrong" >
 <do type="accept" label="Re-login">
  <go href="login.vsp"/>
 </do>
    <p>
 <?=_err_msg?>
    </p>
  </card>
</wml>
<?vsp
}
?>




