Ejemplo n.º 1
0
def auth_user(message):
    global url
    global users

    # allows users to log in via 64 byte token (really it length is 32 byte,
    # but it has 64 symbols in hexademical mode);
    # Also checks if length of typed token is true;
    if 60 < len(message[2]) < 75:
        # loading list of tokens
        f = open('files/tokens.db', 'r')
        tokens = f.read().split('\n')
        f.close()
        if not tokens:
            return

        # getting hash (other name is publickey) of the token (secretkey)
        publickey = gethash(message[2][6:71].strip())

        # checking if hash(publickey) is in token`s list and if this user is not logged in
        if publickey in tokens and not get_tl_user(message[1]):
            # writing new user to user table
            users[message[1]] = tl.get_users_name(url, message), 'all'
            # sending welcome message with some info about user
            write_users_table()
            tl.sendmsg(url, message[1], "Welcome! You are now successfully authenticated as {0}."
                                        "\nYou can change your nicname via /chname <new_nick> or you can "
                                        "view a help message via /help.".format(users[message[1]][0]))
            # writing list of publickeys without recently used publickey
            tokens.pop(tokens.index(publickey))
            g = open('files/tokens.db', 'w')
            g.write('\n'.join(tokens))
            g.close()
        # if computed hash not in the list of available publickeys increase number of tryes of current user
        elif publickey not in tokens and not get_tl_user(message[1])[0]:
            # sending warning message
            tl.sendmsg(url, message[1], 'Wrong key.')
        # sending message if user already logged in or typed absolutely wrong token or user is in Black_List
        else:
            tl.sendmsg(url, message[1], 'You are already logged in. You can change your nick via /chname <new_nick>')
Ejemplo n.º 2
0
def main():
    # getting password
    psswd = gethash(getpass(), mode="pass")

    # decrypting and loading settings
    settings = fdecrypt("files/vk.settings", psswd)
    # print(settings)
    login = "".join(re.findall(r"login=(.+)#endlogin", settings))
    password = "".join(re.findall(r"password=(.+)#endpass", settings))
    chatid = int("".join(re.findall(r"chatid=(\d+)#endchatid", settings)))
    albumid = int("".join(re.findall(r"album_id=(\d+)#endalbumid", settings)))
    userid = int("".join(re.findall(r"userid=(\d+)#enduserid", settings)))

    state_auth = False
    # getting session
    while not state_auth:
        try:
            vk_session = vk_api.VkApi(login, password, captcha_handler=captcha_handler)
            vk_session.authorization()
            vk = vk_session.get_api()
            state_auth = True
        except:
            state_auth = False
            tsleep(30)
            exception("smth goes wrong at geting api\n")

    # getting url
    url = geturl(psswd)

    # Lists of messages
    msg_man = Manager()
    list_of_cmds, list_of_alles, list_of_imnts, tl_msgs = msg_man.list(), msg_man.list(), msg_man.list(), msg_man.list()

    # accounting the speed
    iterations_vk = Value("i", 0)
    iterations_tl = Value("i", 0)

    # file`s safe work
    msgshistory = io.SharedFile("files/msgshistory.db")

    # stats Manager
    stat_man = Manager()
    curr_stat = stat_man.dict()
    curr_stat["temp"] = 0
    curr_stat["iter_tl"] = 0
    curr_stat["iter_vk"] = 0
    curr_stat["PID_VK"] = 0
    curr_stat["PID_TL"] = 0

    # starting bot
    print("Logged in, starting bot...")

    vk_process = Process(
        target=vk_run.run_vk_bot,
        args=(
            vk,
            chatid,
            albumid,
            userid,
            msgshistory,
            tl_msgs,
            list_of_alles,
            list_of_imnts,
            list_of_cmds,
            iterations_vk,
            curr_stat,
        ),
    )
    tl_process = Process(
        target=telegrambot.tlmain,
        args=(url, tl_msgs, msgshistory, list_of_alles, list_of_imnts, iterations_tl, curr_stat),
    )

    print("Starting VK bot...")
    vk_process.start()

    print("Starting TL bot...")
    tl_process.start()

    # checking if admin gave argument "--screen"
    if len(argv) > 1 and argv[1] == "--screen":
        import curses

        stdscr = curses.initscr()
        curses.noecho()
        stdscr.keypad(True)

    # crutch
    iterations_tl.value = 1
    iterations_vk.value = 1

    while True:
        out_string = """Temp: {0} C; \nSpeed_TL: {1}; \nSpeed_VK: {2};"""
        tempfile = open("/sys/class/thermal/thermal_zone0/temp", "r")
        # print('Temp: ' + str(float(tempfile.read().strip())/1000) + ' C ', end='')
        ctemp = str(float(tempfile.read().strip()) / 1000)
        tempfile.close()

        curr_stat["temp"] = ctemp
        curr_stat["iter_tl"] = iterations_tl.value
        curr_stat["iter_vk"] = iterations_vk.value

        if len(argv) > 1 and argv[1] == "--screen":
            stdscr.clear()
            stdscr.addstr(out_string.format(ctemp, iterations_tl.value, iterations_vk.value))
            stdscr.refresh()

        # checking if process is alive. If not, restarting process
        if iterations_tl.value == 0:
            tl_process.terminate()
            tl_process = Process(
                target=telegrambot.tlmain,
                args=(url, tl_msgs, msgshistory, list_of_alles, list_of_imnts, iterations_tl, curr_stat),
            )
            print("Restarting TL bot...")
            tl_process.start()
            # iterations_tl.value = 1

        if iterations_vk.value == 0:
            vk_process.terminate()
            vk_process = Process(
                target=vk_run.run_vk_bot,
                args=(
                    vk,
                    chatid,
                    albumid,
                    userid,
                    msgshistory,
                    tl_msgs,
                    list_of_alles,
                    list_of_imnts,
                    list_of_cmds,
                    iterations_vk,
                    curr_stat,
                ),
            )
            print("Restarting VK bot...")
            vk_process.start()
            # iterations_vk.value = 1

        # stdout.flush()
        iterations_tl.value = 0
        iterations_vk.value = 0
        tsleep(60)

    tl_process.join()
    vk_process.join()

    curses.nocbreak()
    stdscr.keypad(False)
    curses.echo()
    print("Exit...")