<?vsp 
--  
--  $Id: checkmail.vsp,v 1.2.2.1 2012/03/08 12:55:15 source Exp $
--  
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--  
--  Copyright (C) 1998-2012 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 _mm_bcc, _mm_body, _mm_cc, _mm_fld, _mm_from, _mm_own, _mm_rec_date, _mm_snd_time, _mm_subj, _mm_to varchar;
 declare _mm_is_readed varchar;
 declare _mm_id integer;
   declare _user_name, _user_pass, _login_ok varchar;
  declare  _counter1, _num_of_msg,  _next integer;
  declare _list_begins_with, _list_ends_at integer;
  declare _how_many_to_list, _next_fetch_ends_with integer;
  declare _total_msg integer;
  declare _sid, _folder, _action varchar;
  declare _random1, counter integer;

  _random1 := rnd(9999999);

  _sid := '';
  _sid := get_keyword ('SESS', params);
  _user_name := get_keyword ('USERNAME', params);
  _action := get_keyword ('ACTION', params, '');
 _how_many_to_list := 5;
  _total_msg := 0;

  if (equ(_folder, ''))
  {
   _folder := 'Inbox';
  }

 declare cr cursor for select MM_ID, MM_FROM, MM_TO, MM_REC_DATE, MM_SUBJ, MM_BODY, MM_FLD from MAIL_MESSAGE where MM_OWN = _user_name and MM_FLD = _folder order by MM_ID;

   _login_ok := 'NO';
   _counter1 := 1;
   _num_of_msg := 0;

 if (_sid <> '' ) {
  for select * from wapsession where sid = {?'SESS'} do
  {
   _login_ok := 'YES';
   _user_name := _username;

   if (get_keyword('MORELIST', params) = 'NEXT')
   { update wapsession set _sess_list = (_sess_list + _how_many_to_list)  where sid = _sid; }

   if (get_keyword('MORELIST', params) = 'PREV')
   { update wapsession set _sess_list = (_sess_list - _how_many_to_list)  where sid = _sid; }

   if (get_keyword('OPEN', params) <> '')
   {
    update wapsession set _sess_folder = get_keyword('OPEN', params), _sess_list = 1, _time_last_active = datestring ( now () ) where sid = _sid;
   }

   _folder := (select _sess_folder from wapsession where sid = _sid);
   _list_begins_with := (select _sess_list from wapsession where sid = _sid);
  }
  }else{
    _user_pass := get_keyword ( 'POPPASS', params );
    for select _username, _userpass from mailpwd where _username = _user_name and _userpass = _user_pass do
  {
    _login_ok := 'YES';
    _sid := md5(concat (datestring(now()), _user_name));
    delete from wapsession where _username = _user_name;
     insert into wapsession (sid, _username, _userpass, _time, _sess_folder, _sess_list, _sess_page) values(_sid, _user_name, _user_pass, datestring(now()),'Inbox',1,1);
   _folder := (select _sess_folder from wapsession where sid = _sid);
   _list_begins_with := (select _sess_list from wapsession where sid = _sid);
    }

  }

   if ( _login_ok = 'YES' ) {
     _num_of_msg := (select count (MM_ID) as _num_of_msgs from MAIL_MESSAGE where MM_OWN = _user_name and MM_FLD = _folder);
     _total_msg := _num_of_msg;

    if  (_num_of_msg <= (_list_begins_with + _how_many_to_list -1 ) )
    {
    _list_ends_at := _num_of_msg;
  } else {
     _list_ends_at := (_list_begins_with + _how_many_to_list -1);
  }

  if (_num_of_msg <= (_list_ends_at + _how_many_to_list ))
  {
   _next_fetch_ends_with := _num_of_msg;
  }else{
    _next_fetch_ends_with := _list_ends_at + _how_many_to_list ;
  }

 }

if ( (_login_ok = 'YES' ) and ( _action <> 'DELETE' ) )
{  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>
<?vsp
  if ( (_list_begins_with > 1)  or (_action = 'SKIPMENU') )
  {_login_ok := 'YES';}else{
?>
  <card id="menu1" title="OpenLink" >
   <onevent type ="onenterforward">
    <refresh>
   <setvar name="username1" value=""/>
   <setvar name="poppass1" value=""/>
   <setvar name="to" value=""/>
   <setvar name="subj" value=""/>
   <setvar name="msg_body" value=""/>
           <setvar name="sid1" value="<?=_sid?>"/>
  </refresh>
   </onevent>
    <do type="options" label="Options">
     <go href="#opt1"/>
    </do>
    <p><b>Folder:<?=ucase(_folder)?></b></p>
  <?vsp
   http(sprintf('<p><a href="#list" title="Read Msgs">List Msgs (%d-%d)</a></p>\n',
      _list_begins_with, _list_ends_at));
   http(sprintf('<p><a href="compose.vsp?SESS=$(sid1)" title="Compose">Compose Mail</a></p>\n'));
   http(sprintf('<p><a href="getmail.vsp?SESS=$(sid1)&amp;GETPOP3=GET&amp;REF=%d" title="Get">Get Pop3 Emails</a></p>\n',_random1));
   http(sprintf('<p><a href="folders.vsp?SESS=$(sid1)&amp;TO_DO=LIST&amp;REF=%d" title="Folders">Folders</a></p>\n', _random1));
   http(sprintf('<p><a href="addressbook.vsp?SESS=$(sid1)&amp;TO_DO=LIST&amp;REF=%d" title="Addr Book">Address Book</a></p>\n', _random1));
   http(sprintf('<p><a href="setting.vsp?SESS=$(sid1)&amp;REF=%d" title="Setting">Personal Setting</a></p>\n', _random1));
  ?>
  </card>
  <?vsp
  }
  ?>
  <card id="list" title="Message <?vsp http(sprintf('%d',_list_begins_with));?> to <?vsp http(sprintf('%d',_list_ends_at));?>">
   <onevent type ="onenterforward">
    <refresh>
           <setvar name="sid1" value="<?=_sid?>"/>
  </refresh>
   </onevent>
   <do type="options" label="Options">
  <go href="#opt1"/>
   </do>
   <?vsp
     _counter1 := 1;
      open cr;

 if (_total_msg > 0)
 {
  while (_counter1 <= _list_ends_at)
  {
   if (_counter1 <> _list_ends_at) {_next := _counter1 + 1;}else{_next := 1;}
    fetch cr into  _mm_id, _mm_from, _mm_to, _mm_rec_date, _mm_subj, _mm_body, _mm_fld;

   if (_counter1 >= _list_begins_with)
   {
           if (_mm_fld <> 'Draft')
           {
     http(sprintf('<p mode="nowrap"><a href="read.vsp?REF=%d&amp;MSG_ID=%d&amp;SESS=$(sid1)" title="Read%d">', _random1,_mm_id, _counter1));
     http_value(sprintf('%s(%s)', replace(trim(substring(_mm_subj,1,7)), '"', '"'), replace(trim(substring(_mm_from,1,7)), '"', '"')    ));
     http(sprintf('</a></p>\n'));
    }else{
     http(sprintf('<p mode="nowrap"><a href="compose.vsp?REF=%d&amp;MSG_ID=%d&amp;SESS=$(sid1)&amp;DRAFT=YES" title="Read%d">',_random1,_mm_id,  _counter1));
      http_value(sprintf('%s', concat(substring(_mm_subj,1,7), ' (' , substring(_mm_to,1,7), ')' )));
     http(sprintf('</a></p>\n'));
    }
   }
   _counter1 := _counter1 + 1;
  }
   if(_num_of_msg > _list_ends_at)
   {
    http(sprintf('<p mode="nowrap"><b><a href="checkmail.vsp?SESS=$(sid1)&amp;MORELIST=NEXT&amp;REF=%d" title="More Msgs">More Msg(%d-%d)</a></b></p>\n', _random1,(_list_ends_at+1), _next_fetch_ends_with ));
   }
 }else
 {
  if (_folder = 'Inbox')
  {
          http(sprintf('<p>
              You have No message<br/>
          Go back to Main Menu &amp;
              try Get Pop3 Email
                     </p>'));
   }
   else
   {
          http(sprintf('<p>
              You have No messages<br/>
          in this folder.
                     </p>'));
   }
 }
 ?>
  </card>
  <card id="opt1" title="Options">
     <do type="options" label="Exit">
      <go href="#list"/>
     </do>
     <p mode="nowrap"><a href="checkmail.vsp?SESS=<?=_sid?>&amp;ACTION=DELETE&amp;REF=<?vsp http(sprintf('%d',_random1));?>" title="Del Msgs">Delete Msg</a></p>
     <p mode="nowrap"><a href="checkmail.vsp?SESS=<?=_sid?>&amp;OPEN=<?=_folder?>&amp;REF=<?vsp http(sprintf('%d',_random1));?>" title="Mail Menu">Mail Menu</a></p>
     <p mode="nowrap"><a href="logout.vsp?SESS=<?=_sid?>">Logout</a></p>
     <p mode="nowrap"><a href="#list" title="Exit">Exit</a></p>
  </card>
</wml>
<?vsp
}

   if ( (_login_ok = 'YES' ) and  ( _action = 'DELETE' ) )
   {  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="delete" title="Delete Msg">
 <do type="options" label="Back">
  <prev/>
 </do>
 <p>Delete Msg:</p>
  <?vsp
  if (exists(select * from MAIL_MESSAGE where MM_OWN = _user_name and MM_FLD = _folder))
  {
     _counter1 := 1;
    open cr;
   while (_counter1 <= _list_ends_at)
   {
    if (_counter1 <> _list_ends_at) {_next := _counter1 + 1;}else{_next := 1;}
     fetch cr into  _mm_id, _mm_from, _mm_to, _mm_rec_date, _mm_subj, _mm_body, _mm_fld;

    if (_counter1 >= _list_begins_with)
    {
     http(sprintf('<p mode="nowrap"><a href="delete.vsp?MSG_ID=%d&amp;SESS=%s" title="Delete">',_mm_id, _sid));
     if (_folder = 'DRAFT')
     {
      http_value(sprintf('%s (%s)', trim(substring(_mm_subj,1,7)), trim(substring(_mm_to,1,7)) ));
     }else{
      http_value(sprintf('%s (%s)', trim(substring(_mm_subj,1,7)), trim(substring(_mm_from,1,7)) ));
     }
     http(sprintf('</a></p>\n'));
    }
    _counter1 := _counter1 + 1;
   }
   close cr;
  }else
  {
   http(sprintf('<p>Your have no message in this folder</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" >
    <p mode="nowrap">
   Wrong Login
    </p>
  </card>
</wml>
<?vsp
}
?>

