#!/usr/bin/python

# University of Maryland Wireless Network Automatic Login
# version 0.3
#
# Copyright (C) 2006-2008 by Daniel Lenski <lenski@umd.edu>
# Time-stamp: <2008-04-28 16:13:24 dlenski>
#
# Released under the terms of the
# GNU General Public License version 2 or later

import mechanize, urllib2, re
import readline
from getpass import getpass
from time import sleep, time
from sys import exit

login_url = re.compile("https://ptx-cca-s1.net.umd.edu/auth")

# ask for username and password
u = raw_input("username: ")
p = getpass("password: ")

# do the login, over and over and over...
br = mechanize.Browser()
br.set_handle_robots(False)

while 1:
    # attempt to login via web page
    print "%f: trying to log in" % time()
    br.open("https://wlogin.umd.edu")
    if login_url.match(br.geturl()):
        br.select_form(nr=0)
        br["username"] = u
        br["password"] = p
        br.submit()
        if login_url.match(br.geturl()):
            print "%f: wrong login!" % time()
            exit()
        else:
            print "%f: login successful" % time()
    else:
        print "%f: already logged in" % time()

    # wait a while, then try it again
    delay = 60*60
    print "%f: going to wait %d seconds" % (time(), delay)
    sleep(delay)
