def nodeAdd(bot, update, args): userInfo = util.crossMessengerSplit(update) userId = userInfo['user'] if 'user' in userInfo else None userName = userInfo['name'] if 'name' in userInfo else None response = messages.markdown("<u><b>Add<b><u>\n\n", bot.messenger) logger.debug("add - " + " ".join(args)) logger.debug("add - user: {}".format(userId)) if len(args) == 0: response += messages.markdown(( "<b>ERROR<b>: Arguments required: <b>IPAddress_0;name_0 ... IPAddress_n;name_n<b>\n\n" "Example: <cb>add<ca> 43.121.56.87;Node1 56.76.27.100;Node2\n"), bot.messenger) valid = False else: with bot.nodeList as nodeList: for arg in args: valid = True newNode = arg.split(";") if len(newNode) != 2: response += messages.invalidParameterError( bot.messenger, arg) valid = False else: ip = util.validateIp(newNode[0]) name = util.validateName(newNode[1]) if not ip: response += messages.invalidIpError( bot.messenger, newNode[0]) valid = False if not name: response += messages.invalidNameError( bot.messenger, newNode[1]) valid = False if valid: node = nodeList.getNodeByIp(ip) if node == None: response += messages.nodeNotInListError( bot.messenger, ip) else: if bot.database.addNode(node.collateral, name, userId, userName): response += "Added node {}!\n".format(ip) else: response += messages.nodeExistsError( bot.messenger, ip) return response
def nodeUpdate(bot, update, args): userInfo = util.crossMessengerSplit(update) userId = userInfo['user'] if 'user' in userInfo else None userName = userInfo['name'] if 'name' in userInfo else None response = messages.markdown("<u><b>Update<b><u>\n\n", bot.messenger) logger.debug("update - " + " ".join(args)) logger.debug("update - user: {}".format(userId)) user = bot.database.getUser(userId) if user == None: response += messages.notActiveError(bot.messenger) elif not len(args): response += messages.markdown(( "<b>ERROR<b>: Argument(s) required: <b>ip0;newname0 ipN;newnameN<b>\n\n" "Where <b>ip<b> is the IP-Address of the node to update and <b>newname<b> the" " new name of the node.\n\n" "Example: <cb>update<ca> 23.132.143.34;MyNewNodeName\n"), bot.messenger) else: with bot.nodeList as nodeList: for arg in args: nodeEdit = arg.split(";") valid = True if len(nodeEdit) != 2: response += messages.invalidParameterError( bot.messenger, arg) valid = False else: ip = util.validateIp(nodeEdit[0]) name = util.validateName(nodeEdit[1]) if not ip: response += messages.invalidIpError( bot.messenger, nodeEdit[0]) valid = False if not name: response += messages.invalidNameError( bot.messenger, nodeEdit[1]) valid = False if valid: logger.info("update - {} {}".format(ip, user['id'])) node = nodeList.getNodeByIp(ip) if node == None: response += messages.nodeNotInListError( bot.messenger, ip) else: userNode = bot.database.getNodes( node.collateral, userId) if userNode == None: response += messages.nodeNotExistsError( bot.messenger, ip) else: bot.database.updateNode(node.collateral, user['id'], name) response += "Node successfully updated. {}\n".format( ip) return response