Exemple #1
0
def username(bot, update, args):

    response = messages.markdown("<u><b>Change username<b><u>\n\n",
                                 bot.messenger)

    userInfo = util.crossMessengerSplit(update)
    userId = userInfo['user'] if 'user' in userInfo else None

    #Get the user entry from the user which fired the command
    user = bot.database.getUser(userId)

    if user == None:

        response += messages.nodesRequired(bot.messenger)

    elif len(args) != 1:

        response += messages.userNameRequiredError(bot.messenger)

    elif not util.validateName(args[0]):

        response += messages.invalidNameError.format(args[0])

    else:

        old = user['name']

        bot.database.updateUsername(args[0], user['id'])

        response += "Username updated from {} to {}".format(
            messages.removeMarkdown(old), messages.removeMarkdown(args[0]))

    return response
Exemple #2
0
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
Exemple #3
0
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