<?vsp 
--  
--  $Id: send.vsp,v 1.2.2.1 2012/03/08 12:55:16 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_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, _rec, _return_email1 varchar;
  declare _arr_rec any;
  declare _err long varchar;
  declare _offset, _list integer;
  declare _random1 integer;

  _arr_rec := vector ();
  _random1 := rnd(9999999);
  _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 := 'mail.openlinksw.com.my';
  _server := null;
 _return_email1 := '';
   _login_ok := 'NO';


 if (_sid   <> '') {
  for select * from wapsession where sid = _sid  do
  {
   _login_ok := 'YES';
   _user_name := _username;
   update wapsession set _time_last_active = datestring ( now() ) where  sid = _sid;
   _list := _sess_list;
   _folder := _sess_folder;
   _msg_id := _sess_msg_id;

    _return_email1 := ( select wapsetting._return_email from wapsetting where wapsetting._username = _user_name );

    if ((length(_return_email1)) > 5)
    {
     _from := _return_email1;
    }else{
      _from := concat(_user_name, '@openlinksw.com.my');
    }

  }
  }else{
    _login_ok := 'NO';

  }

if(_login_ok = 'YES'){
 if(_action = 'REPLY' or _action = 'FORWARD' )
 {
  for select MM_BODY from MAIL_MESSAGE where MM_ID = _msg_id and MM_OWN = _user_name and MM_FLD = _folder do
  {

   _offset := strstr(MM_BODY, '\r\n\r\n') + 4;
   _msg1 := substring(MM_BODY, _offset, (length(MM_BODY) - _offset));
   _msg := concat(_msg, '\r\n----------------Original Mail------------\n',_msg1,
        '\r\n---------------------End-----------------\n');

  }
 }
 _msg := concat('Subject: ', _subj, '\r\nFrom: ', _from, '\r\n', _msg);
 _to := replace ( _to, ';', ',' );
 _arr_rec := split_string_return_array (_to, ',' );
 _rec := '';

      _rec := concat(  _rec, '<', trim(aref(_arr_rec, 0)), '>'  );

 if (not exists (select 1 from wapaddresses where _username = _user_name and _email_address = aref(_arr_rec, 0) ))
 {
  insert into wapaddresses (_username, _email_address, _email_desc) values (_user_name, aref(_arr_rec, 0), aref(_arr_rec, 0) );
 }

   _counter1 := 1;
   while (_counter1 < length(_arr_rec))
   {
      if (  strchr(aref(_arr_rec, _counter1), '<') is null  )
      {
           _rec := concat(  _rec, ', <', trim(aref(_arr_rec, _counter1)), '>'  );
      } else{
         _rec := concat(  _rec, ', ', aref(_arr_rec, _counter1)  );
      }

  if (not exists (select 1 from wapaddresses where _username = _user_name and _email_address = aref(_arr_rec, _counter1) ) and (length(aref(_arr_rec, _counter1)) > 0) )
  {
   insert into wapaddresses (_username, _email_address, _email_desc) values (_user_name, aref(_arr_rec, _counter1), aref(_arr_rec, _counter1) );
  }

      _counter1 := _counter1 + 1;
   }
         _err := smtp_send (_server, _from, _rec, _msg);
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;REFRESH=<?vsp http(sprintf('%d',_random1));?>"/>
  </do>
    <p>
Mail Sent<br/>
To: <?vsp http_value(replace(_to,'$','$$'));?><br/>
Subj: <?vsp http_value(replace(_subj,'$','$$'));?><br/>
    </p>
    <p><a href="checkmail.vsp?SESS=<?=_sid?>&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" >
    <p mode="nowrap">
   Wrong Login
    </p>
  </card>
</wml>
<?vsp
}
?>

