예제 #1
0
def nodeRemove(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>Remove<b><u>\n\n", bot.messenger)

    logger.debug("remove - " + " ".join(args))
    logger.debug("remove - user: {}".format(userId))

    user = bot.database.getUser(userId)

    if user == None:

        response += messages.notActiveError(bot.messenger)

    elif len(args) < 1:

        response += messages.markdown(
            ("<b>ERROR<b>: Argument(s) required: <b>:ip0 :ipN<b>\n\n"
             "Example remove one: <cb>remove<ca> 21.23.34.44\n"
             "Example remove more: <cb>remove<ca> 21.23.34.44 21.23.34.43\n"
             "Example remove all: <cb>remove<ca> all\n"), bot.messenger)

    else:

        # Check if the user wants to remove all nodes.
        if len(args) == 1 and args[0] == 'all':

            bot.database.deleteNodesForUser(userId)
            response += messages.markdown(
                "Successfully removed <b>all<b> your nodes!\n", bot.messenger)

        else:

            # Else go through the parameters
            for arg in args:

                ip = util.validateIp(arg)

                if not ip:

                    response += messages.invalidIpError(bot.messenger, arg)
                    valid = False

                else:

                    with bot.nodeList as nodeList:

                        logger.info("remove - valid {}".format(ip))

                        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.deleteNode(
                                    node.collateral, user['id'])
                                response += messages.markdown(
                                    "Node successfully removed. <b>{}<b>\n".
                                    format(ip), bot.messenger)

    return response
예제 #2
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