Example #1
0
    def __init__(self,
                 cookies_path,
                 config_path,
                 command_char='/',
                 max_retries=5):
        self._client = None
        self._cookies_path = cookies_path
        self._max_retries = max_retries
        self._command_char = command_char

        # These are populated by on_connect when it's called.
        self._conv_list = None  # hangups.ConversationList
        self._user_list = None  # hangups.UserList
        self._message_handler = None  # MessageHandler

        # Load config file
        self.config = ConfigDict.ConfigDict(config_path)
        self.devmode = self.get_config_suboption('', 'development_mode')

        self.database = "database.db"
        UtilDB.setDatabase(self.database)

        # Handle signals on Unix
        # (add_signal_handler is not implemented on Windows)
        try:
            loop = asyncio.get_event_loop()
            for signum in (signal.SIGINT, signal.SIGTERM):
                loop.add_signal_handler(signum, lambda: self.stop())
        except NotImplementedError:
            pass
Example #2
0
    def __init__(self, cookies_path, config_path, command_char='/', max_retries=5):
        self._client = None
        self._cookies_path = cookies_path
        self._max_retries = max_retries
        self._command_char = command_char

        # These are populated by on_connect when it's called.
        self._conv_list = None  # hangups.ConversationList
        self._user_list = None  # hangups.UserList
        self._message_handler = None  # MessageHandler

        # Load config file
        self.config = ConfigDict.ConfigDict(config_path)
        self.devmode = self.get_config_suboption('', 'development_mode')

        self.database = "database.db"
        UtilDB.setDatabase(self.database)

        # Handle signals on Unix
        # (add_signal_handler is not implemented on Windows)
        try:
            loop = asyncio.get_event_loop()
            for signum in (signal.SIGINT, signal.SIGTERM):
                loop.add_signal_handler(signum, lambda: self.stop())
        except NotImplementedError:
            pass
Example #3
0
def change_karma(user_id, karma):
    user_karma = UtilDB.get_value_by_user_id("karma", user_id)
    if user_karma is not None:
        user_karma = user_karma[1]
    else:
        user_karma = 0
    UtilDB.set_value_by_user_id("karma", user_id, "karma", (user_karma + karma))
    return user_karma + karma
Example #4
0
def add_reminder(conv_id, message, time):
    db_file = UtilDB.get_database()
    database = sqlite3.connect(db_file)
    cursor = database.cursor()
    cursor.execute("INSERT INTO reminders VALUES (?, ?, ?)", (conv_id, message, time))
    database.commit()
    database.close()
Example #5
0
def get_all_reminders(conv_id=None):
    db_file = UtilDB.get_database()
    database = sqlite3.connect(db_file)
    cursor = database.cursor()
    if not conv_id:
        return cursor.execute("SELECT * FROM reminders").fetchall()
    else:
        return cursor.execute("SELECT * FROM reminders WHERE conv_id = ?", (conv_id,)).fetchall()
Example #6
0
def load_images_from_folder(bot, event, *args):
    folder = args[0]
    # loop through folder
    for filename in glob(os.path.join('images', folder, '*')):
        # if filename not in imageids, upload it and store filename,id
        filetail = os.path.split(filename)[1]
        filehead = os.path.split(os.path.split(filename)[0])[1]
        filekey = os.path.join(filehead, filetail)
        print(filekey)
        image_id = UtilDB.get_imageid_for_filename(filekey)
        if image_id is None:
            bot.send_message(event.conv,
                             "Uploading {}".format(filekey))
            image_id = yield from UtilBot.upload_image(bot, filename)
            UtilDB.set_imageid_for_filename(filekey, image_id)
            ####os.remove(filename)
        UtilDB.set_alias_for_filename(filekey, folder)
    bot.send_message(event.conv, "Done.")
Example #7
0
def delete_reminder(conv_id, message, time):
    db_file = UtilDB.get_database()
    database = sqlite3.connect(db_file)
    cursor = database.cursor()
    timestamp = datetime.now() + timedelta(seconds=time)
    # I have an issue with the timestamps not being exact. I need a better way of being exact.
    # This should work for most cases, but will fail under some circumstances.
    cursor.execute(
        'DELETE FROM reminders WHERE conv_id = ? AND message = ? AND timestamp - ? <= 20',
        (conv_id, message, timestamp))
    database.commit()
    database.close()
Example #8
0
def img(bot, event, *args):
    if len(args) > 0 and args[0] == 'list':
        aliases = UtilDB.get_list_of_aliases()
        segments = []
        for alias in aliases:
            segments.append(hangups.ChatMessageSegment(alias))
            segments.append(hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK))
        bot.send_message_segments(event.conv, segments)
    elif len(args) > 0 and args[0] == 'add':
        if len(args) < 3:
           bot.send_message(event.conv, "Error: not enough arguments")
           return
        # alias is all arguments except the first and last
        alias = ''.join(args[1:len(args)-1])
        # strip spaces and non-alphanumeric characters
        alias = ''.join(filter(str.isalnum, alias))
        alias = alias.lower()
        url = args[len(args)-1]
        if UtilDB.get_urls_for_alias(alias) is not None:
            bot.send_message(event.conv, "Error: that alias already exists")
            return
        print(str(is_valid_url(url)))
        if not is_valid_url(url):
            bot.send_message(event.conv, "Error: invalid URL")
            return
        UtilDB.set_alias_for_url(url, alias)
        bot.send_message(event.conv, "Alias {alias} saved with URL {url}".format(alias=alias,url=url))
#no special arguments
    elif len(args) > 0:
        url = args[0]
        is_alias = False
        alias = ''.join(args)
        # strip spaces and non-alphanumeric characters
        alias = ''.join(filter(str.isalnum, alias))
        alias = alias.lower()
        alias_url_list = UtilDB.get_urls_for_alias(alias)
        if alias_url_list is not None:
            random_url = random.choice(alias_url_list)
            if random_url is not None:
                url = random_url
                is_alias = True
        image_id_list = UtilDB.get_imageids_for_alias(alias)
        image_id = None
        if image_id_list is not None:
            image_id = random.choice(image_id_list)
            is_alias = True
        if not is_valid_url(url):
            url = 'http://' + url
            if not is_valid_url(url) and image_id is None:
                bot.send_message(event.conv, "Error: invalid alias or URL.")
                return            
        if image_id is None:
            image_id = UtilDB.get_imageid_for_url(url)
        desc = None
        if not is_alias:
            image_info = UtilBot.get_image_info(url)
            url, desc = image_info
        if desc is None and not is_alias:
            desc = ' '.join(args[1:])
        print(image_id)
        if image_id is None:
            filename = UtilBot.download_image(url, 'images', False)
            image_id = yield from UtilBot.upload_image(bot, filename)
            UtilDB.set_imageid_for_url(url, image_id)
            os.remove(filename)
        bot.send_message_segments(event.conv,
            [hangups.ChatMessageSegment(desc)] if desc else None,
            image_id)
Example #9
0
def get_current_karma(user_id):
    user_karma = UtilDB.get_value_by_user_id("karma", user_id)
    if user_karma is not None:
        return user_karma[1]
    else:
        return 0