<?vsp 
--  
--  $Id: save_draft.vsp,v 1.2 2006/08/15 19:30:34 source Exp $
--  
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--  
--  Copyright (C) 1998-2006 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_id, _mm_is_readed varchar;
   declare _user_name, _login_ok varchar;
  declare _server varchar;

 declare _login_ok varchar;
  declare _num1, _counter1, _msg_idd integer;
  declare _num2 varchar;
  declare _sid, _action, _to, _msg, _subj, _msg_id, _from, _msg1, _folder varchar;
  declare _err long varchar;
  declare _offset, _list integer;

  _sid := '';
  _sid := get_keyword('SESS', params);
  _action := get_keyword('ACTION', params);
   _to := get_keyword('TO', params);
   _msg := get_keyword('MSG', params);
   _subj := get_keyword('SUBJ', params);

  _server := 'oecsvr.tzo.com';
   _login_ok := 'NO';


 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;
   _folder := (select _sess_folder from wapsession where sid = _sid);
   _list := (select _sess_list from wapsession where sid = _sid);
   _msg_id := (select _sess_msg_id from wapsession where sid = _sid);

    for select _return_email from wapsetting where wapsetting._username = _user_name do
    {
     if (length(_return_email) > 5)
     {
      _from := _return_email;
     }else{
       _from := concat(_user_name, '@openlinksw.com.my');
     }
    }
  }
  }else{
    _login_ok := 'NO';

  }

if(_login_ok = 'YES'){
 if(_action = 'SAVE_DRAFT')
 {
  _msg := concat('Subject: ', _subj, '\r\nFrom: ', _from, '\r\n\r\n', _msg);

  if( not exists(select 1 from MAIL_MESSAGE where MM_OWN = _user_name ))
  {
    _msg_idd := 1;
   insert into MAIL_MESSAGE (MM_ID, MM_OWN, MM_FLD, MM_BODY, MM_TO, MM_FROM, MM_SUBJ)
     values (_msg_idd, _user_name, 'Draft', _msg,  _to,  _from, _subj);
  }else{
   if (exists (select 1 from MAIL_MESSAGE where MM_OWN = _user_name and MM_FLD = 'Draft' and MM_ID = atoi (get_keyword('MSG_ID', params, '')) ) )
   {
    _msg_idd := atoi (get_keyword('MSG_ID', params));
    update MAIL_MESSAGE set MM_BODY = _msg, MM_TO = _to, MM_SUBJ = _subj, MM_FROM = _from
     where MM_OWN = _user_name and MM_ID = _msg_idd and MM_FLD = 'Draft';
   }else{
    _msg_idd := (select max (MM_ID) from MAIL_MESSAGE where MM_OWN = _user_name)  + 1;
    insert into MAIL_MESSAGE (MM_ID, MM_OWN, MM_FLD, MM_BODY, MM_TO, MM_FROM, MM_SUBJ)
     values (_msg_idd, _user_name, 'Draft', _msg,  _to,  _from, _subj);
   }
  }


 }
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="Compose" >
  <onevent type="onenterforward">
   <refresh>
 <setvar name="to" value=""/>
 <setvar name="subj" value=""/>
 <setvar name="msg_body" value=""/>
 </refresh>
  </onevent>
  <do type="accept" label="Mail Menu">
   <go href="checkmail.vsp?SESS=<?=_sid?>&amp;LIST=<?vsp http(sprintf('%d',_list));?>&amp;FOLDER=<?=_folder?>"/>
  </do>
    <p>
Draft Saved to Draft Folder<br/>
To: <?vsp http_value(replace(_to,'$','$$'));?><br/>
Subj: <?vsp http_value(replace(_subj,'$','$$'));?><br/>
    </p>
    <p> <a href="checkmail.vsp?SESS=<?=_sid?>&amp;LIST=<?vsp http(sprintf('%d',_list));?>&amp;FOLDER=<?=_folder?>" title="Mail Menu">Mail Menu</a></p>
  </card>
</wml>
<?vsp
}else{ 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
}
?>

