1 # This file is part of python-rwhoisd
3 # Copyright (C) 2003, David E. Blacka
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 class DirectiveProcessor:
25 rwhois_dir_exp = re.compile(r"V-(\d+\.\d+)", re.I)
27 def __init__(self, db):
30 "rwhois" : self.rwhois_directive,
31 "limit" : self.limit_directive,
32 "holdconnect" : self.hold_directive,
33 "directive" : self.directive_directive,
34 "xfer" : self.xfer_directive,
35 "status" : self.status_directive
38 def process_directive(self, session, line):
39 d_args = line.lstrip("-").split()
41 if not self.directives.has_key(d_args[0]):
42 session.wfile.write(Rwhois.error_message(400))
45 self.directives[d_args[0]](session, d_args[1:])
48 def rwhois_directive(self, session, arglist):
50 session.wfile.write(Rwhois.error_message(338))
53 mo = DirectiveProcessor.rwhois_dir_exp.match(arglist[0])
55 session.wfile.write(Rwhois.error_message(338))
58 # normally we would make sure that the version given was
61 session.wfile.write(config.banner_string)
62 session.wfile.write("\r\n")
64 def limit_directive(self, session, arglist):
66 limit = int(arglist[0])
67 except (IndexError, ValueError):
68 session.wfile.write(Rwhois.error_message(338))
71 if limit > config.max_limit:
72 limit = config.max_limit
73 elif limit < config.min_limit:
74 limit = config.min_limit
77 session.wfile.write(Rwhois.ok())
79 def hold_directive(self, session, arglist):
81 session.wfile.write(Rwhois.error_message(338))
84 arg = arglist[0].lower()
86 session.holdconnect = True
88 session.holdconnect = False
90 session.wfile.write(Rwhois.error_message(338))
93 session.wfile.write(Rwhois.ok())
95 def directive_directive(self, session, arglist):
98 dirs = self.directives.keys()
101 desc = dir.capitalize()
102 reslist.append("%%directive directive:%s" % dir)
103 reslist.append("%%directive description:%s directive" % desc)
104 session.wfile.write("\r\n".join(reslist))
105 session.wfile.write("\r\n")
106 session.wfile.write(Rwhois.ok())
108 if self.directives.has_key(arglist[0]):
110 desc = dir.capitalize()
111 session.wfile.write("%%directive directive:%s\r\n" % dir)
112 session.wfile.write("%%directive description:%s directive\r\n"
115 session.wfile.write(Rwhois.error_message(400))
117 session.wfile.write(Rwhois.ok())
120 def status_directive(self, session, arglist):
121 if session.holdconnect:
126 session.wfile.write("%%status limit: %d\r\n" % session.limit)
127 session.wfile.write("%%status holdconnect: %s\r\n" % hc_str)
128 session.wfile.write("%status forward: off\r\n")
129 session.wfile.write("%%status objects: %d\r\n"
130 % len(self.db.main_index))
131 session.wfile.write("%status display: dump\r\n")
132 session.wfile.write("%status contact: N/A\r\n")
133 session.wfile.write(Rwhois.ok())
135 def xfer_directive(self, session, arglist):
137 session.wfile.write(Rwhois.error_message(338))
140 aa = arglist[0].lower()
144 for arg in arglist[1:]:
145 if arg.startswith("class="):
147 elif arg.startswith("attribute="):
148 attr = arg[10:].lower()
149 if attr: attr_list.append(attr)
151 # check the constraints
152 if not self.db.is_autharea(aa):
153 session.wfile.write(Rwhois.error_message((340, aa)))
155 if oc and not self.db.is_objectclass(oc):
156 session.wfile.write(Rwhois.error_message((341, oc)))
159 for attr in attr_list:
160 if not self.db.is_attribute(attr):
161 session.wfile.write(Rwhois.error_message((342, attr)))
164 # now iterate over the entire dataset looking for objects that
165 # match our criteria.
167 objs = self.db.object_iterator()
170 # Note: in theory, we should leverage QueryProcessors
172 if obj.get_attr_value("auth-area").lower() != aa:
174 if oc and obj.get_attr_value("class-name").lower() != oc:
178 session.wfile.write(obj.attrs_to_wire_str(attr_list, "%xfer "))
180 session.wfile.write(obj.to_wire_str("%xfer "))
181 session.wfile.write("\r\n%xfer \r\n");
183 session.wfile.write(Rwhois.ok())
186 if __name__ == '__main__':
191 session = Session.Context()
192 session.wfile = sys.stdout
196 db.init_schema(sys.argv[1])
197 for data_file in sys.argv[2:]:
198 db.load_data(data_file)
201 dp = DirectiveProcessor(db)
203 directives = [ "-rwhois",
204 "-rwhois foo bar baz",
205 "-rwhois V-1.6 noise blah",
217 "-xfer a.com class=contact",
218 "-xfer a.com class=domain attribute=domain-name",
219 "-xfer foo class=bar",
220 "-xfer foo class=bar attribute=baz attribute=boo",
223 for dir in directives:
224 print "trying %r:" % dir
225 print dp.process_directive(session, dir)