www.pudn.com > mailfetcher2.rar > Report.java


package mfetcher;

//
// Report
//

//
// Copyright (C) 1999 John Mettraux
//
// This program 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.
//
// 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

import java.util.*;
import java.io.IOException;
import java.text.SimpleDateFormat;


public class Report
{

public final static String REPORT_SUBJECT = "Report";

SimpleDateFormat sdf
= new SimpleDateFormat (" dd MMM yyyy, HH:mm:ss", Locale.UK);

//
// DATAS

int popBoxes;
String recipient;
String smtpHost;

int reportsReceived = 0;
boolean reportSent = false;

Vector messageBody = new Vector();

String version = null;

SmtpMailer smtpMailer;

//
// CONSTRUCTORS

public Report (int popBoxes, String recipient, String source, String smtpHost)
{
this.popBoxes = popBoxes;
this.recipient = recipient;
this.smtpHost = smtpHost;

smtpMailer = new SmtpMailer(smtpHost, source);

//
// report message header
messageBody.addElement("to: " + recipient);
messageBody.addElement("from: " + source);
messageBody.addElement("subject: " + REPORT_SUBJECT);
messageBody.addElement("date: "
+ sdf.format(new Date()) + " " + MailFetcher.timeZoneModifier);
messageBody.addElement("");
//
// top of report message body
messageBody.addElement(MailFetcher.version);
messageBody.addElement("");
}

//
// METHODS

public void addPopReport (String popReport)
throws IOException, MessageException
{
if (reportSent)
//
// report sent so no need for another report
return;

reportsReceived++;
messageBody.addElement(popReport);

//System.out.println("reportsReceived = "+reportsReceived);
//System.out.println("popBoxes = "+popBoxes);

if (reportsReceived >= popBoxes) {
//
// send the report...
reportSent = true;

if (version != null)
smtpMailer.setVersion(version);

//
// copyright + GPL info
messageBody.addElement("");
messageBody.addElement(MailFetcher.copyright);
messageBody.addElement(".");

String[] mb = new String[messageBody.size()];
for (int i=0; i<messageBody.size(); i++)
mb[i] = (String)messageBody.elementAt(i);

smtpMailer.sendMail(recipient, mb);
}
}

public void addMessage (String message)
{
if (reportSent)
//
// report sent so no need for another report
return;

messageBody.addElement(message);
}

public void setVersion (String version)
{
this.version = version;
}

public static Report parseReport (int _popBoxes, String line)
throws Exception
//
// reportto <email_address> from <source> smtphost <smtphost>
//
{
StringTokenizer st = new StringTokenizer(line);

st.nextToken();
// reportto
String _recipient = st.nextToken();

st.nextToken();
// from
String _source = st.nextToken();
if (_source.indexOf('@') == -1)
throw new Exception("Source field incomplete (@ missing)");

st.nextToken();
// smtphost
String _smtpHost = st.nextToken();

return new Report(_popBoxes, _recipient, _source, _smtpHost);
}
}