Initial commit.
[webdispense.git] / balance.py
1 #!/usr/bin/python
2
3 import commands,re
4
5 print "Content-type: text/json"
6 print
7 import cgi,cgitb
8 import os, Cookie
9 cgitb.enable ()
10
11 result = {}
12 form = cgi.FieldStorage ()
13 cookies = Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE",""))
14
15 def checkdata ():
16         if not (cookies.has_key("username") and cookies.has_key("pin")):
17                 result = {"result" : "Missing information!"}
18                 return result
19         expr_name = re.compile ("^\w*$")
20         expr_pin = re.compile ("^\d{4}$")
21         username = cookies["username"].value.lower ()
22         pin = cookies["pin"].value
23         if (not expr_name.match (cookies["username"].value)) or (not expr_pin.match (cookies["pin"].value)):
24                  result = {"result" : "Incorrect login!"}
25                  return result
26         output = commands.getoutput ("sudo checkpin " + username + " " + pin)
27         if output == "False":
28                 result = {"result" : "Authentication failed!"}
29                 return result
30         if not output == "True":
31                 result = {"result" : "PIN not set up."}
32                 return result
33         output = commands.getoutput ("dispense acct " + username);
34         expr_bal = re.compile ("(?P<bal>\d*\.\d*)")
35         balance = expr_bal.search (output).groups()[0]
36
37         return {"result" : "success", "balance" : balance}
38
39
40 print str(checkdata ())

UCC git Repository :: git.ucc.asn.au