1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
#!/usr/bin/python2.3 -O
# maybe you need to change the above line
import os, sys, socket, string
from stat import *
import timeout_socket
import SocketServer
from utils import CRLF, TAB, quote_reply, strategies
import log
import debug
from databases import *
class BangDb:
def __init__(self):
self.info = "!"
self.name = "!"
def match(self, strat, word):
matches = []
for i in list_of_databases():
r = databases[i].match(strat, word)
if r:
return r
return []
def define(self, word):
matches = []
for i in list_of_databases():
r = databases[i].define(word)
if r:
return r
return []
class StarDb:
def __init__(self):
self.info = "*"
self.name = "*"
def match(self, strat, word):
matches = []
for i in list_of_databases():
r = databases[i].match(strat, word)
matches = matches+r
return matches
def define(self, word):
matches = []
for i in list_of_databases():
r = databases[i].define(word)
matches = matches+r
return matches
execfile("config.py")
execfile("config-dbs.py")
databases = {}
for i in dbases:
databases[i.name] = i
def list_of_databases():
return filter(lambda x: x not in ("!", "*"), databases.keys())
class Session:
def __init__(self, rfile, wfile, client_address):
self.rfile = rfile
self.wfile = wfile
self.client_address = client_address
self.optionmime = 0
self.reply("220 hello <> msg")
self.cmddict = { #command, function, nr of arguments
"QUIT" : (self.cmd_quit, 0),
"DEFINE" : (self.cmd_define, 2),
"MATCH" : (self.cmd_match, 3),
"CLIENT" : (self.cmd_client, 1),
"STATUS" : (self.cmd_status, 0),
"SHOW" : (self.cmd_show, 1),
"HELP" : (self.cmd_help, 0),
"OPTION" : (self.cmd_option, 1),
}
def reply(self, x):
debug("---> "+repr(x))
#debug("---> "+str(x))
try:
self.wfile.write(x + CRLF)
self.wfile.flush()
except:
debug('FAILED')
def complete(self):
self.reply("250 Command complete")
def cmd_quit(self):
self.reply("221 bye bye")
raise "session_exit"
def cmd_status(self):
self.reply("210 I'm well thanks")
def cmd_option(self, wh):
wh = string.upper(wh)
if wh=='MIME':
self.optionmime = 1
self.reply("250 MIME will be used")
else:
self.reply("500 I'be be damned if I knew what you want")
def cmd_help(self):
self.reply("113 help text follows")
self.reply("help me")
self.reply(".")
self.complete()
def cmd_define(self, db, word):
if word[0]=='"':
word = word[1:-1]
if not databases.has_key(db):
self.reply("550 Not here")
return
try:
word = unicode(word, 'utf-8').lower()
except UnicodeDecodeError:
word = unicode(word, 'iso-8859-1').lower()
word = word.encode('utf-8')
dbi = databases[db]
r = dbi.define(word)
if not r:
self.reply("552 Not here")
return
nod = len(r) # number of definitions
self.reply("150 %i here you are" % nod)
for i in r:
db = i[0]
self.reply('151 "%s" %s "%s"' % (word, db, databases[db].info))
if self.optionmime: # a hack
mimeheader = "Content-Type: text/plain; charset=utf8"+CRLF
self.reply(quote_reply(mimeheader+i[1]))
else:
self.reply(quote_reply(i[1]))
self.reply(".")
self.complete()
def cmd_match(self, db, strat, word):
if word[0]=='"':
word = word[1:-1]
if not databases.has_key(db):
self.reply("550 Not here")
return
try:
word = unicode(word, 'utf-8').lower()
except UnicodeDecodeError:
word = unicode(word, 'iso-8859-1').lower()
word = word.encode('utf-8')
dbi = databases[db]
r = dbi.match(strat, word)
if not r:
self.reply("552 Not here")
return
self.reply("152 %i here you are" % len(r))
for i in r:
db = i[0]
self.reply('%s "%s"' %(db, i[1]))
self.reply(".")
self.complete()
def cmd_client(self, text):
self.reply("250 nice to meet you")
def cmd_show(self, wh):
what = string.upper(wh)
if what in ('DB', 'DATABASES'):
n = len(databases)
#self.reply("554 No db available")
self.reply("110 %i databases here" % n)
for i in list_of_databases():
d = databases[i]
self.reply('%s "%s"' % (i, d.info))
self.reply(".")
elif what in ('STRAT', 'STRATEGIES'):
#self.reply("555 No strategies available")
nrs = len(strategies)
self.reply("111 %i strategies here" % nrs)
for i, v in strategies.items():
self.reply('%s "%s"' % (i, v[1]))
self.reply(".")
elif what[:4]=='INFO':
db = string.split(wh, None, 1)[1]
#self.reply('550 Invalid database, use "SHOW DB" for list of databases')
self.reply("112 database information follows")
self.reply("this is db %s" % db)
self.reply(".")
elif what == 'SERVER':
self.reply("114 server information follows")
self.reply("serpento")
self.reply(".")
self.complete()
# parses the command and eventually calls the appropriate routine
def docmd(self, cmd):
debug("<--- "+repr(cmd))
# if the connection has broken... we have to shut down:
if cmd == "":
raise "session_exit"
# filter suspicious chars first
log.logr(self.client_address[0], string.rstrip(cmd))
cmd2 = filter(lambda x: ord(x) >= 32, cmd)
lcmd2 = string.split(cmd2, None, 1) or [""]
command = string.upper(lcmd2[0])
if self.cmddict.has_key(command):
args = []
nargs = self.cmddict[command][1]
if nargs>0:
if len(lcmd2)<=1:
self.reply("501 syntax error, illegal parameters")
return
lcmd2 = string.lstrip(lcmd2[1])
args = string.split(lcmd2, None, nargs-1)
else:
self.reply("500 I'd be be damned if I knew what you want")
return
c = cmd2
try:
self.cmddict[command][0](*args)
except TypeError: # bad number of arguments
self.reply("501 syntax error, illegal parameters")
def loop(self):
while 1:
try:
l = self.rfile.readline()
except timeout_socket.Timeout:
debug("timeout")
break
except socket.error: # but if anythin happened with control connection, go out
break
try:
self.docmd(l)
except "session_exit":
break
debug("Connection closed.")
def watchdog():
while 1:
time.sleep(2)
cur_time = time.time()
print sessions
for i in range(len(sessions)):
print sessions[i].ip
print
# the main routine
# ----------------
class NoneClass:
pass
class DummyLock:
def acquire(self):
pass
def release(self):
pass
try:
import thread
threading = 1
except:
threading = 0
if threading:
thlock = thread.allocate_lock()
else:
thlock = DummyLock()
forking = hasattr(os, "fork")
sessions = {}
t_socket = timeout_socket.timeout_socket
#test if we are running from inetd
inetd = 1
inetdsock = 0
try:
inetdsock = socket.fromfd(sys.stdin.fileno(), socket.AF_INET, socket.SOCK_STREAM)
inetdsock.getsockname()
except (socket.error, AttributeError):
inetd = 0
del inetdsock
log = log.Log(logfile)
d = debug.Debug(debuglevel)
debug = d.debug
log.log("Serpento started")
if inetd:
client_addr = inetdsock.getpeername()
session = Session(sys.stdin, sys.stdout, client_addr)
session.loop()
del session
sys.exit(0)
if threading:
mixin = SocketServer.ThreadingMixIn
elif forking:
mixin = SocketServer.ForkingMixIn
else:
mixin = NoneClass
class MyStreamRequestHandler(SocketServer.StreamRequestHandler):
def finish(self):
self.wfile.flush()
self.wfile.close()
self.rfile.close()
self.request.close()
class TCPServer(mixin, SocketServer.TCPServer):
def server_bind(self):
"""Called by constructor to bind the socket.
"""
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
def get_request(self):
"""Get the request and client address from the socket.
"""
conn, adr = self.socket.accept()
conn = t_socket(sock=conn, timeout=timeout)
return conn, adr
class DictServer(MyStreamRequestHandler):
def handle(self):
session = Session(self.rfile, self.wfile, self.client_address)
session.loop()
del session
address = ('', port)
server = TCPServer(address, DictServer)
server.serve_forever()
|