def _project(self, username, projectname): return self.db.fetchone( table='projects', filters={ 'userId': self.userModule._getUid(util.toBytes(username)), 'name': util.toBytes(projectname) } )
def _addUser(self, username, password): """Add a user to the database.""" self.db.insert( table='users', data={ 'nickname': util.toBytes(username), 'password': hashlib.sha256(util.toBytes(password)).hexdigest(), 'server': self.server.address } )
def _removeProject(self, username, projectName): user = self.userModule._getUid(util.toBytes(username)) if user is not None: self.db.delete( table='projects', filters={ 'userId': user, 'name': util.toBytes(projectName) } ) return True return False
def _addProject(self, username, projectName, path): user = self.userModule._getUid(util.toBytes(username)) if user is not None: self.db.insert( table='projects', data={ 'userID': user, 'name': util.toBytes(projectName), 'dir': util.toBytes(path) } ) return True return False
def mod(self): """Modify a project value in the database. Usage: project.mod <user> <project> {user,name,dir,repo} <value>""" # userId INTEGER, name TEXT, dir TEXT, repo try: user, projectName, field, value = self.args.split() except ValueError: pass newData = {} if field == "user": try: newUser["user"] = self.userModule._getUid(util.toBytes(value)) except: self.reply("No valid user!") newData["user"] = newUser elif field == "dir": newData["dir"] = value elif field == "repo": newData["repo"] = value elif field == "name": newData["name"] = value self.db.update( table="project", data=newData, filters={ "user": user, "name": projectName } ) self.reply("Modified %s on project %s too %s"%(field, projectName, value))
def _removeUser(self, username): """Remove a user from the database.""" self.db.delete( table='users', filters={ 'nickname': util.toBytes(username), 'server': self.server.address } )
def _getUser(self, username): """Get the id of a specific user.""" return self.db.fetchone( table='users', filters={ 'nickname': util.toBytes(username), 'server': self.server.address } )
def __init__(self, cmdHandler, cmdArgs=None, authRequired=None): super(ModuleBase, self).__init__() self.cmdHandler = cmdHandler self.reply = self.cmdHandler.replyWithMessage self.server = self.cmdHandler.server self.username = self.cmdHandler.user self.loggedInUsers = self.cmdHandler.server.loggedInUsers() self._db = None self.args = cmdArgs self.bargs = util.toBytes(cmdArgs) self.authRequired = [] if authRequired is not None: self.authRequired = authRequired
def replyWithMessage(self, text, msgType=None): """Send a message to the channel from which we received the command.""" text = util.toUnicode(text) recipient = self.user if self.channel.startswith("#"): recipient = self.channel if msgType is None: msgType = self.msgType # Avoid flooding of a channel splitText = text.split("\n") timeout = 0.2 if len(splitText) > 10: timeout = 0.5 for txt in splitText: txt = "%s %s :%s\r\n" % (msgType, recipient, txt) util.write(txt) self.sock.send(util.toBytes(txt)) time.sleep(timeout)
def identify(self): """Identify yourself to the system (do this in a pm to the bot). Usage: user.identify <password>.""" user = self._getUser(self.username) if user is None: self.reply( "No user with nickname '%s' was found :(" % self.username ) return False userInfo = self.server.users[self.username] if user[2] == hashlib.sha256(util.toBytes(self.args)).hexdigest(): lastLogin = '******' if userInfo.loggedInTime != 0: lastLogin = time.strftime( "%a %b %d %H:%M:%S", time.gmtime(userInfo.loggedInTime) ) self._logInUser(self.username) self.reply("Last login %s" % lastLogin) if userInfo.failedLoginAttempts > 0: self.reply("Failed login attempts since last login: %s" % userInfo.failedLoginAttempts) else: userInfo.failedLoginAttempts += 1 self.reply("Incorrect password :/")
def test_that_toBytes_converts_unicode_to_bytes(self): testString = u"This is a string" newString = util.toBytes(testString) self.assertNotEqual(testString, newString) self.assertEqual(type(newString), bytes)
def sendRawMessage(self, text): """Send a raw message to the socket.""" text = "%s\r\n" % text util.write(text) self.sock.send(util.toBytes(text))
def _getUid(self, username): """Get the id of a specific user.""" user = self._getUser(util.toBytes(username)) if user is not None: return user[0] return None