<?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
--
--

 adm_pre_page(lines);

  declare _stmt, _group varchar;
  declare _maxres integer;
  declare inx, idx, len, idx2, _beg, _end integer;
  declare _u_id integer;
  declare _sid_id, _own varchar;
  declare _group_match varchar;
  declare msg_date, usr_date date;
  declare user_d, user_m, user_y, chois varchar;
  declare _subj, _from, rec varchar;
  declare head any;
  declare list any;
  declare st, st2, msg, msg2 varchar;
  declare res1, res2 any;

  _u_id := db.dba.adm_dav_check_auth (lines, 1);
  if (_u_id < 1)
    {
      http_request_status ('HTTP/1.1 401 Unauthorized');
      http ( concat ('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">',
	    '<HTML><HEAD>',
	    '<TITLE>401 Unauthorized</TITLE>',
	    '</HEAD><BODY><H1>Unauthorized</H1>',
            'Access to page is forbidden.</BODY></HTML>'));
      return (0);
    }

  list :=  deserialize (decode_base64 (get_keyword ('list', params, '')));
  _beg :=  atoi (get_keyword ('begin', params, '0'));
  _stmt := get_keyword ('stmt', params, '');
  _group_match := get_keyword ('group', params, '');
  user_d := get_keyword ('date_d', params);
  user_m := get_keyword ('date_m', params);
  user_y := get_keyword ('date_y', params);
  st := '';
  msg := '';
  st2 := '';
  msg2 := '';

  if ('' <> get_keyword ('next', params))
    _beg := _beg + 20;

  if ('' <> get_keyword ('prev', params))
    _beg := _beg - 20;

	     if (not isarray (list))
    	       {
	         list := vector ();
		 usr_date := NULL;
		 _beg := 0;
		 chois := get_keyword ('chois', params, '');

		 if (user_d <> '' and user_m <> '' and user_y <> '')
		   {
		      usr_date := concat (user_m, ' ', user_d, ' ', user_y);
		      exec ('select cast (? as date)', st, msg, vector (usr_date), 1, res1, res2);
		      if (st = '')
		        usr_date := aref (aref (res2, 0), 0);
		   }

		 exec ('vt_parse (?)', st2, msg2, vector (_stmt));
		 if (st2 <> '')
		   goto err;

		 for (select NM_HEAD, NM_ID, NM_BODY_ID, NM_BODY, NM_ID, NM_READ
		   from DB.DBA.NEWS_MSG_NNTP
		     where contains (NM_BODY, _stmt)) do
		   {
		     head := deserialize (NM_HEAD);
		     if (isarray (head))
		       head := aref (head, 0);
		     else
		       head := aref (mime_tree (NM_BODY), 0);
		     _group := get_keyword ('Newsgroups', head, '');
		     if (not matches_like (_group, concat ('%', _group_match, '%')))
		       goto next;
		     rec := get_keyword ('Date', head, '');
		     if (usr_date is not NULL)
		       {
			  msg_date := date822_to_date (rec);
			  if (chois = 'exact' and usr_date <> msg_date)
			    goto next;
			  if (chois = 'new' and usr_date > msg_date)
			    goto next;
			  if (chois = 'old' and usr_date < msg_date)
			    goto next;
		       }
		     list := vector_concat (list, vector (NM_BODY_ID));
		     next:;
		    }
	       }
err:
?>

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="<?=adm_what_css()?>">
    <meta name="AUTHOR" content="OpenLink Software">
    <meta name="DESCRIPTION" content="Interactive text search using Virtuoso VSP">
    <title>Internet News Search</title>
  </head>
<?vsp adm_page_header('Internet News Text Search Results', NULL); ?>

<FORM name="main_form" method="POST" action="news_search_result.vsp">

<?vsp
  if (st2 = '')
    {
   if (st <> '')
      {
?>

	<TABLE CLASS="genlist" BORDER="0" CELLPADDING="0">
	<TR><TD CLASS="errorhead">Error: <?=msg?></TD></TR>
	<TR><TD CLASS="AdmBorders"><IMG SRC="../images/1x1.gif" WIDTH="1" HEIGHT="2" ALT=""></TD></TR>
	<TR><TD CLASS="genlisthead"><STRONG>The date interval is ignored</STRONG>
	</TD></TR>
	</TABLE>

<?vsp } ?>

<?vsp
	idx := 0;
	idx2 := 0;
	len := length (list);
	idx := _beg;
	_end := idx + 20;

	if (_end > len)
    	  _end := len;
        http ('<TABLE BORDER="0" CELLPADDING="0" CLASS="genlist">');
	if (len = 0)
	  http ('<tr><td class="gendata">*** Nothing matches the query ***</td></tr>');
	else
	  http (sprintf ('<tr><td CLASS="genhead" colspan="4">Displayed %d - %d from %d entities matching your query</td></tr>', idx + 1, _end, len));
	http('<TR><TD CLASS="AdmBorders" COLSPAN="4"><IMG SRC="../images/1x1.gif" WIDTH="1" HEIGHT="2" ALT=""></TD></TR>');
	while (idx < _end)
	  {
	    if (idx2 = 0)
	      {
?>
    		<TR>
    		<TD CLASS="genlistheadt">From</TD>
    		<TD CLASS="genlistheadt">Subject</TD>
    		<TD CLASS="genlistheadt">Received</TD>
    		<TD CLASS="genlistheadt">Action</TD>
		</TR>
<?vsp
	      }
		    for (select NM_HEAD, NM_ID, NM_BODY_ID, NM_BODY, NM_ID, NM_READ
		       from DB.DBA.NEWS_MSG
		         where NM_BODY_ID = aref (list, idx)) do
		      {
		        head := deserialize (NM_HEAD);
		     if (isarray (head))
		       head := aref (head, 0);
		     else
		       head := aref (mime_tree (NM_BODY), 0);
		       _subj := get_keyword ('Subject', head, '');
		       _from := get_keyword ('From', head, '');
		       _group := get_keyword ('Newsgroups', head, '');
		       rec := get_keyword ('Date', head, '');
		         {
?>
			   <TR><TD CLASS="gendata"><a href="mailto:<?=mail_to(_from)?>"><?=_from?></a></TD>
			   <TD CLASS="gendata"><?=substring(_subj,1,29)?></TD>
			   <TD CLASS="gendata"><?=substring(rec,1,19)?></TD>
<?vsp http (sprintf ('<TD CLASS="gendata"><a href="mime_display.vsp?mail_no=%U&type=news">View</a></TD>', NM_ID)); ?>
			 </TD></TR>
<?vsp
			};
		      }
	       done:
idx := idx + 1;
idx2 := idx2 + 1;
	   }
	     http ('<TR><TD COLSPAN="4" ALIGN="middle">');
?>

<BR />

<?vsp
  if (_beg <> 0)
    http ('<INPUT TYPE="submit" NAME="prev" VALUE=" Prev 20 ">  ');
  if (_end <> len)
    http ('<INPUT TYPE="submit" NAME="next" VALUE=" Next 20 ">');

    }
  else
    {
?>

	<TABLE CLASS="genlist" BORDER="0" CELLPADDING="0">
	<TR><TD CLASS="errorhead">Error in query:</TD></TR>
	<TR><TD CLASS="AdmBorders"><IMG SRC="/admin/images/1x1.gif" WIDTH="1" HEIGHT="2" ALT=""></TD></TR>
	<TR><TD CLASS="genlisthead"><STRONG><?=msg2?></STRONG>
	</TD></TR>
	</TABLE>

<?vsp
    }
?>
  <INPUT type=hidden name="list" value="<?=encode_base64 (serialize (list))?>">
  <INPUT type=hidden name="begin" value="<?=_beg?>">
  <INPUT TYPE=hidden name="date_m" value="<?=user_m?>" size="2">
  <INPUT TYPE=hidden name="date_d" value="<?=user_d?>" size="2">
  <INPUT TYPE=hidden name="date_y" value="<?=user_y?>" size="2">
  <INPUT TYPE=hidden name="group" value="<?=_group_match?>" size="2">
  <INPUT TYPE=hidden name="stmt" value="<?=_stmt?>" size="2"></FORM>

<FORM name="back_form" method="POST" action="news_search.vsp">
  <INPUT TYPE=hidden name="date_m" value="<?=user_m?>" size="2">
  <INPUT TYPE=hidden name="date_d" value="<?=user_d?>" size="2">
  <INPUT TYPE=hidden name="date_y" value="<?=user_y?>" size="2">
  <INPUT TYPE=hidden name="group" value="<?=_group_match?>" size="2">
  <INPUT TYPE=hidden name="stmt" value="<?=_stmt?>" size="2">
  <INPUT TYPE="submit" NAME="back" VALUE="Back"></FORM>
</TD></TR></TABLE>
<?vsp adm_page_footer(); ?>
</html>

