// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;
import java.net.*;
import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;

public class ChangeChecker {
    private static String download(String urlstr) {
        StringBuffer sb = new StringBuffer();
        try {
            URL url = new URL(urlstr);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.connect();
            if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStream is = con.getInputStream();
                byte[] b = new byte[1000];
                int n;
                while ((n = is.read(b)) >= 0) {
                    sb.append(new String(b, 0, n));
                }
                is.close();
            }
            con.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static void sendEmail(String mailserver, String from, String to, String subject, String body) {
        try {
            Properties props = System.getProperties();
            props.put("mail.smtp.host", mailserver);
            Session session = Session.getDefaultInstance(props, null);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            message.setText(body);
            Transport.send(message);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    private final static String START = "<h3 class = \"title\">";
    private final static String END = "</h3>";
    public static String title(String content) {
        int ix1 = content.indexOf(START);
        if(ix1 >= 0) {
            ix1 += START.length();
            int ix2 = content.indexOf(END, ix1);
            if(ix2 >= 0) {
                return content.substring(ix1, ix2);
            } else {
                return "?";
            }
        } else {
            return "?";
        }
    }

    public static void monitor(int minutes, String urlstr, String mailserver, String from, String to) {
        String content = download(urlstr);
        for(;;) {
            try {
                Thread.sleep(minutes * 60 * 1000);
            } catch (InterruptedException e) {
            }
            String oldcontent = content;
            content = download(urlstr);
            if(!content.equals(oldcontent)) {
                sendEmail(mailserver, from, to, urlstr + " has changed", title(content));
            }
        }
    }

    public static void main(String[] args) {
        monitor(10, "http://www.wow-europe.com/", "192.168.1.10", "arne@arne", "arne@arne");
    }
}
