71 lines
1.8 KiB
Python
71 lines
1.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
# Copyright 2010 Maurizio Porrato <maurizio.porrato@gmail.com>
|
|
# See LICENSE.txt for copyright info
|
|
|
|
from HTMLParser import HTMLParser
|
|
from random import choice
|
|
|
|
|
|
def responseToChallange(kp):
|
|
if len(kp) != 6:
|
|
return 'ERROR'
|
|
aa, bb, cc = int(kp[:2]), int(kp[2:4]), int(kp[4:6])
|
|
defgh = "%05d" % ((cc+7)*(cc+4)+(bb+1)*(aa+2), )
|
|
return defgh[3]+defgh[0]+defgh[2]+defgh[4]+defgh[1]
|
|
|
|
|
|
def makeRandomChallange():
|
|
return ''.join([choice('0123456789') for i in range(6)])
|
|
|
|
|
|
class SimpleXMLParser(HTMLParser):
|
|
"""Dirty FRN-specific hack to handle bogus one-level nesting only
|
|
XML-like syntax"""
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
if not hasattr(self, 'fields'):
|
|
self.fields = {}
|
|
self.next_field = None
|
|
self.next_data = ''
|
|
if self.next_field is None:
|
|
self.next_field = tag
|
|
self.next_data = ''
|
|
else:
|
|
if attrs:
|
|
a = ' '+' '.join(['%s="%s"' % (k,v) for k,v in attrs])
|
|
else:
|
|
a = ''
|
|
self.next_data += "<%s%s>" % (tag,a)
|
|
|
|
def handle_data(self, data):
|
|
if self.next_field is not None:
|
|
self.next_data += data
|
|
|
|
def handle_endtag(self, tag):
|
|
if tag == self.next_field:
|
|
self.fields[self.next_field] = self.next_data
|
|
self.next_field = None
|
|
self.next_data = ''
|
|
else:
|
|
self.next_data += "</%s>" % tag
|
|
|
|
def get_fields(self):
|
|
return self.fields
|
|
|
|
|
|
def parseSimpleXML(xml):
|
|
p = SimpleXMLParser()
|
|
p.feed(xml)
|
|
return p.get_fields()
|
|
|
|
|
|
def formatSimpleXML(elements):
|
|
if hasattr(elements, 'items'):
|
|
items = elements.items()
|
|
else:
|
|
items = elements
|
|
return ''.join(["<%s>%s</%s>" % (k.upper(),v,k.upper()) for k,v in items])
|
|
|
|
# vim: set et ai sw=4 ts=4 sts=4:
|