I just need to know if it's decent or not.
import glob
import tornado
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket
class server(tornado.websocket.WebSocketHandler):
def on_message(self, message):
self.modDirectory = 'modules/*.py'
self.files = glob.glob(self.modDirectory)
#print(self.files)
#CALL ON INIT
self.modules = dict()
self.commands = list()
self.msg = dict()
self.ip = self.request.remote_ip
self.uip = {}
self.loadModules()
data = message.split(':', 1)
if len(data) == 1:
cmd, args = data[0]
else:
cmd, args = data
key = cmd
self.getCommand(cmd)
cmd = self.getCommand(key)
if cmd:
if cmd:
cmd.check(self, args)
def loadModules(self):
'''
load modules that are found in self.files>glob.glob('directory/*.py')
'''
self.modules = {}
self.commands = []
for f in self.files:
execfile(f, self.modules)
for key, value in self.modules.items():
if key == 'init':
value(self)
def callProtocal(self, evt):
'''
sends the protocals
'''
self.write_message(evt)
def clearCommands(self):
'''
clears protocal list
'''
self.commands = list()
def addCommand(self, *args, **kw):
'''
add the protocal to the list protocal>description>function>id
'''
co = Command(*args, **kw)
self.commands.append(co)
def getCommand(self, name):
'''
search for the protocal then returns the result
'''
name = name.lower()
for command in self.commands:
if command.name == name:
return command
return None
class Command(object):
def __init__(self, name, desc, func, number = 0):
self.name = name
self.desc = desc
self.number = int(number)
self.func = func
def run(self, mgr, args):
if not args: return False
else: return True
def check(self, mgr, args):
if not self.run(mgr, args): return
else: self.func(mgr, args)
if __name__=="__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r'/', server)])
__SERVER__ = tornado.httpserver.HTTPServer(app)
__SERVER__.listen(address='localhost', port=6444)
tornado.ioloop.IOLoop.instance().start()
mod.py
from db import database as self
from time import time
def init(mgr):
mgr.addCommand('delMsg', 'delete a message', delMsg, 3)
mgr.addCommand('delUserMsg', 'delete a users messages', delUserMsg, 4)
mgr.addCommand('addMod', 'add a moderator', addMod, 5)
mgr.addCommand('removeMod', 'remove a mod', removeMod, 6)
mgr.addCommand('banUser', 'ban a user', banUser, 7)
mgr.addCommand('unbanUser', 'unban a user', unBan, 8)
def delMsg(mgr, args):
room = args[0]
msgid = args[1]
self.delMsg(room, msgid)
mgr.callProtocal('msgremove:%s:%s:%s' % (room, msgid, time()))
def delUserMsg(mgr, args):
room = args[0]
user = args[1]
self.delUserMsg(room, user)
mgr.callProtocal('usermsgremove:%s:%s:%s' % (room, user, time()))
def addMod(mgr, args):
room = args[0]
user = args[1]
self.addMod(room, user)
mgr.callProtocal('mod:%s:%s:%s' % (room, user, time()))
def removeMod(mgr, args):
room = args[0]
user = args[1]
self.removeMod(room, user)
mgr.callProtocal('demod:%s:%s:%s' % (room, user, time()))
def banUser(mgr, args):
room = args[0]
user = args[1]
target = args[2]
self.banUser(room, user, target)
mgr.callProtocal('banned:%s:%s:%s:%s' % (room, user, target, time()))
def unbanUser(mgr, args):
room = args[0]
user = args[1]
target = args[2]
self.unbanUser(room, user, target)
mgr.callProtocal('unbanned:%s:%s:%s:%s' % (room, user, target, time()))
room.py
from db import database as self
import random
def init(mgr):
mgr.addCommand('bmsg', 'send a message to the room', message, number=1)
mgr.addCommand('bauth', 'login', login, number=2)
def message(mgr, args):
if self.isConnected(args[1]):
user = args[1]
room = args[2]
if self.getRoom(room) != False:
msg = args[3]
_ip = self.ip(self.uid(user))
params = "b:%s:%s:%s:%s:%s" % (user, room, msg, _ip, self.uid(user))
mgr.callProtocal(params + '<br />')
else:
mgr.callProtocal('NROOM:NO ROOM FOUND')
else:
mgr.callProtocal('NROOM:NO ROOM FOUND')
mgr.callProtocal('NT_CONNECTED:%s' % ('YOU ARE NOT SIGNED IN'))
def login(mgr, args):
status =args[0]
if status == 'bauth':
room = args[1]
usern = args[2]
password = args[3]
st = self.check(usern, password)
if st == 'ok':
id = self.uid(usern)
conip = mgr.ip
self.setConnected(usern, mgr.ip, room)
_ip = self.ip(id)
mgr.callProtocal('LGN_OK:%s:%s:%s:%s' % (room, usern, id, _ip))
elif st == 'denied':
anonid = mgr.setSsid(ip=_ip, room=room)
user = 'anon%s' % anonid
self.setConnected(user, mgr.ip, room)
mgr.callProtocal('LGN_DENIED:%s:%s' % (user, _ip))
if status == 'blogAnon' and not self.getRoom(room) == False:
anonid = random.randrange(1000, 9000)
self.setSsid(_ip, anonid)
user = 'anon%i' % anonid
self.setAnon(user ,_ip)
mgr.callProtocal('anon_login:%s:%s' % (user, _ip))
database.py
import time
import mid
import uid
import hashlib
import sqlite3 as m
global db
dbType = "sqlite"
if dbType == "sqlite":
db = m.connect('CHAT.sqlite3')
elif dbType == "mysql":
import MySQLdb as sql
host = ""
user = ""
password = ""
dbname = ""
db = sql.connect(host, user, password, dbname)
cur = db.cursor()
def ip(id, ip, mode='id'):
try:
cur.execute('select * from ip where id like("%s")' % (id))
ip = ''.join([i[1] for i in cur.fetchall()])
return ip
except:
ssid = ip.replace('anon', '')
cur.execute('select * from ssid where like("%s")' % ssid)
ip = ''.join([i[2] for i in cur.fetchall()])
return ip
def setIp(id, ip):
try:
cur.execute('delete from ip where id like("%s")' %(id))
cur.execute('insert into ip(id, ip) values("%s", "%s")' % (id, ip))
db.commit()
except:
cur.execute('insert into ip(id, ip) values("%s", "%s")' % (id, ip))
db.commit()
def setSsid(room, ip):
sid = random.randrange(1000, 9000)
try:
cur.execute('select room, ssid from ssid room="%s", ssid="%s"' % (room, str(sid)))
ssid = [ssid[1] for ssid in cur.fetchall()]
if str(sid) in ssid:
while True:
r = random.randrange(1000, 9000)
if r not in ssid:
cur.execute('insert into ssid(ssid, room, ip) values("%s", "%s", "%s")' % (r, room, ip))
db.commit()
return r
break
except:
cur.execute('insert into ssid(ssid, room, ip) values("%s", "%s", "%s")' % (r, room, ip))
db.commit()
return str(sid)
def setConnected(user, ip, room):
try:
cur.execute('delete from connected where room="%s", user="%s"' %(room, user))
cur.execute('insert into connected(user, ip) values("%s", "%s", "%s")' % (user, ip, room))
db.commit()
except:
cur.execute('insert into connected(user, ip) values("%s", "%s", "%s")' % (user, ip, room))
db.commit()
def getConnected(room, user):
try:
cur.execute('select room, user from connected where room="%s", user="%s"' % (room, user))
user = ''.join([i[0] for i in cur.fetchall()])
return True
except: return False
def setMessage(room, user, msg):
userid = UID(user)
ip = ip(userid)
msgid = mid.generate()
cur.execute('insert into messages(room, user, message, msgid, userid, ip) values("%s", "%s", "%s", "%s", "%s", "%s")' % (room, user, msg, userid, ip))
db.commit()
def getMessage(user):
cur.execute('select room, user from messages user="%s"' % user)
lis = [a[0:] for a in cur.fetchall()]
return lis
def delMsg(room, msgid):
cur.execute('delete from messages where room="%s" msgid="%s"' %(room, msgid))
db.commit()
def delUserMsg(room, user):
cur.execute('delete from messages where room="%s", user="%s"' %(room, msgid))
db.commit()
def banUser(room, user, target):
tid = UID(target)
try:
cur.execute('delete from banlist where room="%s", user="%s", target="%s", id="%s", ip="%s"' % (room, user, target, tid, ip))
cur.execute('insert into banlist(room, user, target, tid, ip, time) values("%s", "%s", "%s", "%s", "%s", "%s")' % (user, target, tid, ip, time.time()))
db.commit()
except:
cur.execute('insert into banlist(room, user, target, tid, ip, time) values("%s", "%s", "%s", "%s", "%s", "%s")' % (user, target, tid, ip, time.time()))
db.commit()
def unbanUser(room, user, target):
if UID(target) == 'ip':
ip = ip(user, mode = 'user')
try:
cur.execute('delete from banlist where room="%s", user="%s", target="%s", id="%s", ip="%s"' % (room, user, target, tid, ip))
db.commit()
except: pass
def getRoomOwner(room):
cur.execute('select * from rooms where room="%s"' % room)
owner = ''.join([n[1] for n in cur.fetchall()])
return owner
def getBanlist(room):
cur.execute('select * from banlist room="%s"' % room)
l = [a[1] for a in cur.fetchall()]
return l
def addMod(room, user):
try:
cur.execute('delete from mods where room="%s", user="%s"' % (room, user))
cur.execute('insert into mods(room, user, userid) values("%s", "%s", "%s")' % (room, user, time.time()))
db.commit()
except:
cur.execute('insert into mods(room, user, userid) values("%s", "%s", "%s")' % (room, user, time.time()))
db.commit()
def removeMod(room, user):
try:
cur.execute('delete from mods where room="%s", user="%s"' % (room, user))
db.commit()
except:
return False
def register(user, password):
cur.execute('insert into register(user, password userid, time) values("%s", "%s", "%s")' % (user, hashlib(password).hexdigest(), uid.generate(), time.time()))
db.commit()
def UID(user):
cur.execute('select * from register where user like("%s")' % user)
Uid = ''.join([n[2] for n in cur.fetchall()])
return Uid
def check(user, password):
cur.execute('select * from register where user like("%s")' % user)
auth = ''.join([n[1] for n in cur.fetchall()])
if hashlib(password).hexdigest == auth: return 'ok'
else: return 'denied'
def getMods(room):
cur.execute('select * from mods where room like("%s")' % mods)
l = [i[1] for i in cur.fetchall()]
return l
def getRoom(room, check = True):
cur.execute('select * from rooms where room like("%s")' % room)
r = ''.join([n[0] for n in cur.fechall()])
if check == True:
if r == room: return True
elif r != room: return False
elif check == False:
return room