summaryrefslogtreecommitdiff
path: root/lib/core.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/core.py')
-rw-r--r--lib/core.py87
1 files changed, 62 insertions, 25 deletions
diff --git a/lib/core.py b/lib/core.py
index e7ac571..2124763 100644
--- a/lib/core.py
+++ b/lib/core.py
@@ -18,43 +18,44 @@ class Daemon():
result = self.search_domain(q)
else:
# Try to find a person with the given query
- person = search_person(q)
+ person = self.search_person(q)
if person:
result = person
else:
result = self.get_help()
- return self.format_result(result)
-
- def format_result(self, values):
- """Receive a dictionary and return a string"""
- result = ""
- for k, v in values.items():
- if k == 'domains':
- result += '\nAssociated domains\n'
- for i, dom in enumerate(v):
- result += ' {0}\n'.format(dom.name)
- elif k == 'admins':
- result += '\nAssociated administrator/s\n '
- for i, adm in enumerate(v):
- result += ' {0} {1} <{2}>\n'.format(adm.name, adm.surname, adm.email)
- else:
- result += '{0}: {1}\n'.format(k, v)
+ return result.formatted_answer()
+
+# def format_result(self, values):
+# """Receive a dictionary and return a string"""
+# result = ""
+# for k, v in values.items():
+# if k == 'domains':
+# for i, dom in enumerate(v):
+# if len(v) == 1:
+# result += 'domain: {0}\n'.format(dom.name)
+# else:
+# result += 'domain-{0}: {1}\n'.format(i, dom.name)
+# elif k == 'admins':
+# for i, adm in enumerate(v):
+# if len(v) == 1:
+# result += 'Admin: {0} {1} <{2}>\n'.format(adm.name, adm.surname, adm.email)
+# else:
+# result += 'Admin-{0}: {1} {2} <{3}>\n'.format(i, adm.name, adm.surname, adm.email)
+# else:
+# result += '{0}: {1}\n'.format(k, v)
- return result
+# return result
def search_ip(self, ip):
- result = {}
-
# Iterate over all IP block elements
networks = self.data.get_networks()
for key in networks:
for block in networks[key].ip_blocks:
if ipaddr.IPAddress(ip) in ipaddr.IPNetwork(block):
- return networks[key].as_dict(self.data)
+ return networks[key].as_answer(self.data)
- result['error'] = "Network not found"
- return result
+ return Answer(AnswerValue(1, 'Error', 'Network not found'))
def search_domain(self, domain):
# Iterate over all network and check its domains
@@ -62,9 +63,9 @@ class Daemon():
domains = self.data.get_domains()
for network in networks.values():
if any(domains[d].name == domain for d in network.domains):
- return network.as_dict(self.data)
+ return network.as_answer(self.data)
- return {'error':'Domain not found'}
+ return Answer(AnswerValue(1, 'Error', 'Domain not found'))
# TODO
@@ -107,6 +108,42 @@ class Daemon():
return self.header
+class Answer():
+ def __init__(self):
+ self.values = []
+
+ def add_value(self, av):
+ self.values.append(av)
+
+ def formatted_answer(self):
+ res = ""
+ for av in sorted(self.values, key=lambda v: v.priority):
+ res += av.formatted_value() + "\n"
+
+ return res
+
+class AnswerValue():
+ def __init__(self, priority, key, val):
+ self.priority = priority
+ self.key = key
+ self.val = val
+
+ def value(self):
+ return self.val
+
+ def formatted_value(self):
+ return "{0}: {1}".format(self.key, self.value())
+
+class AnswerAdminValue(AnswerValue):
+ def __init__(self, priority, admin):
+ self.priority = priority
+ self.key = 'Admin'
+ self.adm = admin
+
+ def value(self):
+ return '{0} {1} <{2}>\n'.format(self.adm.name, self.adm.surname, self.adm.email)
+
+
class WhoisHandler(socketserver.BaseRequestHandler):
def setup(self):
nihil fit ex nihilo