#!/usr/bin/python

#
# (C) 2006 Robert Sander <spamcop@gurubert.de>
#
# Released under GPL
#
# Use at your own risk
#

import htmllib, formatter, urllib, sys, re

class SpamcopParser(htmllib.HTMLParser):

    inside = 0
    args = {}

    def start_form(self, attrs):
        # print "starting form: ",
        # print attrs
        for (name, value) in attrs:
            if name == 'name' and value == 'sendreport':
                self.inside = 1
        # print 'inside: %d' % inside

    def end_form(self):
        # print "ending form"
        self.inside = 0

    def do_input(self, attrs):
        # print "doing input: ",
        # print attrs

        argname = ''
        argvalue = ''
        
        if self.inside:
            for (name, value) in attrs:
                if name == 'name':
                    if value == 'preview' or value == 'cancel':
                        break
                    argname = value
                if name == 'value':
                    argvalue = value
            if argname:
                self.args[argname] = argvalue

def process(url):
    global respat
    
    parser = SpamcopParser(formatter.NullFormatter())

    parser.feed(urllib.urlopen(url).read())
    parser.close()

    print parser.args

    if parser.args:

        params = urllib.urlencode(parser.args)

        # print params

        data = urllib.urlopen('http://www.spamcop.net/sc', params).read()

        match = respat.search(data)
        if match:
            print "OK"
        else:
            print data
	print

scpat = re.compile('^(http://www.spamcop.net/sc\?id=z.*z)$')
respat = re.compile("/dev/null'ing report for mole@devnull.spamcop.net", re.M)

line = sys.stdin.readline()

i = 0

while line:

    match = scpat.match(line)
    if match:
        print i, match.group(1)

        process(match.group(1))

	i = i + 1
    
    line = sys.stdin.readline()


