<%@ page import="java.util.*" %>
<%@ include file="newscfg.jsp" %>
<%
  int counter=0;
  NewsClient news = new NewsClient();
  if (!news.connect(server,user,pw,0)) {
     out.println("<H1>Can't connect to news server</H1>");
   }
  else {
   int current;
   int first;
   String firstStr=request.getParameter("first");
   int pagesize=25;
   String nxtfirstStr="-1";
   int nxtfirst;
   String group=request.getParameter("group");
   if (group==null || group.equals("")) group="alt.test";
   out.println("<H1>Welcome to WNews (" + group + ")</H1>");
   news.setGroup(group);
   if (firstStr!=null && !firstStr.equals("")) 
     first=Integer.parseInt(firstStr);
   else
     first=news.last;
   current=first;
%>
<TABLE WIDTH=100% BORDER=1 BGCOLOR=cornsilk>
<TR><TD WIDTH=50%><B>Message</B></TD><TD WIDTH=25%><B>From</B></TD>
<TD WIDTH=25%><B>Sent</B></TD></TR></TABLE>
<%
   do {
     MailMessage msg=news.readMessage(current--,"HEAD");
     if (msg==null) break; // whoops?
     if (++counter>pagesize) break; // only read some messages
     String contenttype=(String)msg.headers.get("content-type");
     nxtfirstStr=(String)msg.headers.get("X-server-number");  
     if (contenttype==null || contenttype.substring(0,10).equals("text/plain")) {
 String sender=HtmlEncoder.encode(msg.sender.trim());
 if (sender==null || sender.equals("")) sender="&nbsp;";
 if (sender.length()>20) sender=sender.substring(0,17) + "...";
%>
<TABLE BGCOLOR=cornsilk WIDTH=100% BORDER=1>
<TR><TD WIDTH=50%><A HREF=newsread.jsp?group=<%= group %>&art=<%= nxtfirstStr %>&first=<%=Integer.toString(first)%>><%= HtmlEncoder.encode(msg.subject) %></A></TD>
<TD WIDTH=25%><FONT SIZE=2><%= sender %></FONT></TD><TD WIDTH=25%>
<FONT SIZE=2><%=(String)msg.headers.get("date")%></FONT></TD></TR></TABLE>
<%
       out.flush(); // won't really work with a single table
       }
     } while (counter<=pagesize);
%>
  </TABLE><HR>
<%
   nxtfirst=Integer.parseInt(nxtfirstStr);
   if (nxtfirst!=-1 && ++nxtfirst>news.first) 
     out.println("<A HREF=news.jsp?group=" + group +
     "&first="+Integer.toString(nxtfirst)+
     ">View earlier messages</A><BR>");
   if (first!=news.last) {
     int nxt=first+pagesize;  // guess next number
     if (nxt>news.last) nxt=news.last;
     out.println("<A HREF=news.jsp?group=" + group +
      "&first="+Integer.toString(nxt)+
      ">View newer messages</A><BR>");
   }
}
%>
<A HREF=newsfront.jsp>Pick a new group</A>
