示例#1
0
def listautok(word=False, gid=0):
    """
    Lists the autok pairs defined for a word, or all the autok
    :param gid: filter to group id
    :param word: word to return value for or everything
    :return: table with autok stored
    """

    logger = logging.getLogger(__name__)
    wordtext = ""

    if not word:
        sql = "select key,value from autokarma ORDER BY key ASC;"
    else:
        string = (word, gid)
        sql = "SELECT key,value FROM autokarma WHERE key='%s' AND gid='%s' ORDER by key ASC;" % string
        wordtext = _("for word %s for gid %s") % (word, gid)

    cur = stampy.stampy.dbsql(sql)

    try:
        # Get value from SQL query
        text = _("Defined autokarma triggers %s:\n") % wordtext
        table = from_db_cursor(cur)
        text = "%s\n```%s```" % (text, table.get_string())

    except:
        # Value didn't exist before
        text = _("%s has no trigger autokarma") % word

    logger.debug(msg=_L("Returning autokarma %s for word %s") % (text, word))
    return text
示例#2
0
def listforward(source=False):
    """
    Lists the forwards defined for a source or all defined
    :param source: chatid
    :return: table with forwards defined
    """

    logger = logging.getLogger(__name__)
    if source:
        # if source is provided, return the forwards for that source
        string = (source, )
        sql = "SELECT source,target FROM forward WHERE source='%s' ORDER by source ASC;" % string
        cur = stampy.stampy.dbsql(sql)
        target = cur.fetchone()

        try:
            # Get value from SQL query
            target = target[1]

        except:
            # Value didn't exist before, return 0 value
            target = ""
        text = _("%s has a forward %s") % (source, target)

    else:
        sql = "SELECT source,target from forward ORDER BY source ASC;"
        cur = stampy.stampy.dbsql(sql)
        text = _("Defined forwards:\n")
        table = from_db_cursor(cur)
        text = "%s\n```%s```" % (text, table.get_string())
    logger.debug(msg=text)
    return text
示例#3
0
def chanshowslave(message):
    """
    Shows slaves associated to current channel
    :param message: Message to process
    :return:
    """
    logger = logging.getLogger(__name__)
    msgdetail = stampy.stampy.getmsgdetail(message)

    chat_id = msgdetail["chat_id"]
    message_id = msgdetail["message_id"]
    masterid = stampy.plugin.config.config(key='link', default=False, gid=chat_id)

    if masterid:
        # We've a master channel, report it
        text = _("This channel %s is slave of channel %s") % (chat_id, masterid)
    else:
        # We nee to check database to see if we've any slave
        sql = "SELECT id from config where key='link' and value='%s'" % chat_id
        cur = stampy.stampy.dbsql(sql=sql)

        text = _("Defined slaves:\n")
        table = from_db_cursor(cur)
        text = "%s\n```%s```" % (text, table.get_string())

    stampy.stampy.sendmessage(chat_id=chat_id, text=text,
                              reply_to_message_id=message_id,
                              disable_web_page_preview=True,
                              parse_mode="Markdown")
    return
示例#4
0
def chanunlink(message):
    """
    Unlinks channel
    :param message: Message to process
    :return:
    """
    logger = logging.getLogger(__name__)
    msgdetail = stampy.stampy.getmsgdetail(message)

    chat_id = msgdetail["chat_id"]
    message_id = msgdetail["message_id"]
    masterid = stampy.plugin.config.config(key='link', default=False, gid=chat_id)

    if masterid:
        # Delete link from slave
        stampy.plugin.config.deleteconfig(key='link', gid=chat_id)

        # Notify master channel of slave linked
        text = _("Channel *%s* with name *%s* has been unlinked as *SLAVE*") % (chat_id, msgdetail['chat_name'])

        stampy.stampy.sendmessage(chat_id=masterid, text=text,
                                  disable_web_page_preview=True,
                                  parse_mode="Markdown")

        # Notify slave of master linked
        text = _("This channel has been unliked as *SLAVE* from *MASTER* channel *%s*") % masterid
        text = text + _("\n\nChannel has also been enabled as running in "
                        "isolated mode, use ```/gconfig delete isolated``` to "
                        "revert back to global karma")

        stampy.stampy.sendmessage(chat_id=chat_id, text=text,
                                  reply_to_message_id=message_id,
                                  disable_web_page_preview=True,
                                  parse_mode="Markdown")
    return
示例#5
0
def listhilight(uid, word=False):
    """
    Lists the hilight defined for a gid or all
    :param uid: filter to group id
    :param word: word to return value for or everything
    :return: table with hilight stored
    """

    logger = logging.getLogger(__name__)
    wordtext = ""

    if not word:
        sql = "select word from hilight WHERE gid='%s' ORDER BY word ASC;" % uid
    else:
        string = (word, uid)
        sql = "SELECT word FROM hilight WHERE word='%s' AND gid='%s' ORDER by word ASC;" % string
        wordtext = _("for word %s for uid %s") % (word, uid)

    cur = stampy.stampy.dbsql(sql)

    try:
        # Get value from SQL query
        text = _("Defined hilight triggers %s:\n") % wordtext
        table = from_db_cursor(cur)
        text = "%s\n```%s```" % (text, table.get_string())

    except:
        # Value didn't exist before
        text = _("%s has no trigger hilight") % word

    logger.debug(msg=_L("Returning hilight %s for word %s") % (text, word))
    return text
示例#6
0
def uptime(message):
    """
    Processes uptime commands in the messages
    :param message: message to process
    :return:
    """

    logger = logging.getLogger(__name__)

    msgdetail = stampy.stampy.getmsgdetail(message)

    chat_id = msgdetail["chat_id"]
    message_id = msgdetail["message_id"]

    datelast = stampy.stampy.utize(dateutil.parser.parse(stampy.plugin.config.config(key='uptime', gid=0)))
    datelastfor = datelast.strftime('%Y/%m/%d %H:%M:%S')
    datelastts = time.mktime(datelast.timetuple())
    date = stampy.stampy.utize(datetime.datetime.now())
    datefor = date.strftime('%Y/%m/%d %H:%M:%S')
    dateforts = time.mktime(date.timetuple())
    elapsed = dateforts - datelastts

    text = _("Bot was started at: %s\n") % datelastfor
    text += _("Now it is: %s\n") % datefor
    text += _("Elapsed time: %s (seconds)\n") % elapsed
    text += _("Elapsed time: %s \n") % format_timedelta(datetime.timedelta(seconds=elapsed), locale=stampy.stampy.language)

    logger.debug(msg=_L("Returning %s") % text)

    stampy.stampy.sendmessage(chat_id=chat_id, text=text,
                              reply_to_message_id=message_id,
                              disable_web_page_preview=True,
                              parse_mode='markdown')
    return
示例#7
0
def listalias(word=False, gid=0):
    """
    Lists the alias defined for a word, or all the aliases
    :param gid: Group ID to work on
    :param word: word to return value for or everything
    :return: table with alias stored
    """

    logger = logging.getLogger(__name__)
    if word:
        # if word is provided, return the alias for that word
        string = (word, gid)
        sql = "SELECT key,value FROM alias WHERE key='%s' AND gid='%s' ORDER by key ASC;" % string
        cur = stampy.stampy.dbsql(sql)
        value = cur.fetchone()

        try:
            # Get value from SQL query
            value = value[1]

        except:
            # Value didn't exist before, return 0 value
            value = 0
        text = _("%s has an alias %s") % (word, value)

    else:
        sql = "select key,value from alias WHERE gid='%s' ORDER BY key ASC;" % gid
        cur = stampy.stampy.dbsql(sql)
        text = _("Defined aliases:\n")
        table = from_db_cursor(cur)
        text = "%s\n```%s```" % (text, table.get_string())
    logger.debug(msg=_L("Returning aliases %s for word %s for gid %s") %
                 (text, word, gid))
    return text
示例#8
0
def info(message):
    """
    Processes info commands in the messages
    :param message: message to process
    :return:
    """

    logger = logging.getLogger(__name__)

    msgdetail = stampy.stampy.getmsgdetail(message)

    chat_id = msgdetail["chat_id"]
    message_id = msgdetail["message_id"]

    text = _("This is update *%s* ") % msgdetail["update_id"]
    text += _("with message id *%s*.\n") % msgdetail["message_id"]
    text += _("This has been sent on chat *%s*, named *%s* on *%s*\n") % (
        msgdetail["chat_id"], msgdetail["chat_name"], msgdetail["datefor"])
    text += _(
        "This message was sent by user id *%s*, with given name *%s*, long name *%s* and username *%s*\n"
    ) % (msgdetail["who_id"], msgdetail["who_gn"], msgdetail["who_ln"],
         msgdetail["who_un"])

    logger.debug(msg=_L("Returning %s") % text)

    stampy.stampy.sendmessage(chat_id=chat_id,
                              text=text,
                              reply_to_message_id=message_id,
                              disable_web_page_preview=True,
                              parse_mode='markdown')
    return
示例#9
0
def showconfig(key=False, gid=0):
    """
    Shows configuration in database for a key or all values
    :param gid: group ID to check
    :param key: key to return value for
    :return: Value stored
    """
    logger = logging.getLogger(__name__)
    if key:
        # if word is provided, return the config for that key
        string = (key, )
        sql = "SELECT key,value FROM config WHERE key='%s' AND id='%s';" % (
            string, gid)
        cur = stampy.stampy.dbsql(sql)
        value = cur.fetchone()

        try:
            # Get value from SQL query
            value = value[1]

        except:
            # Value didn't exist before, return 0 value
            value = 0
        text = _("%s has a value of %s for id %s") % (key, value, gid)

    else:
        sql = "select key,value from config WHERE id='%s' ORDER BY key ASC;" % gid
        cur = stampy.stampy.dbsql(sql)
        text = _("Defined configurations for gid %s:\n") % gid
        table = from_db_cursor(cur)
        text = "%s\n```%s```" % (text, table.get_string())
    logger.debug(msg=_L("Returning config %s for key %s for id %s") %
                 (text, key, gid))
    return text
示例#10
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """

    commandtext = ""
    if stampy.stampy.is_owner_or_admin(message):
        commandtext += _(
            "Use `/kick <username or id>` to kick user from chat\n\n")
        commandtext += _(
            "Use `/kickban <username or id>` to kick user from chat and ban to forbid new entry\n\n"
        )
        commandtext += _("Use `/unban <user ID>` to unban user\n\n")
        commandtext += _("Use `/op <user ID>` to grant all privileges\n\n")
        commandtext += _("Use `/deop <user ID>` to remove all privileges\n\n")
        commandtext += _("Use `/opall` to grant op to all\n\n")
        commandtext += _("Use `/deopall` to remove all ops\n\n")
        commandtext += _("Use `/topic <topic>` to change title\n\n")
        commandtext += _(
            "Use `/mute <user ID>` to forbid sending messages\n\n")
        commandtext += _(
            "Use `/unmute <user ID>` to allow sending messages\n\n")
    return commandtext
示例#11
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """
    commandtext = _("Use `/help` to display commands help\n\n")
    commandtext += _("Read about announcements at https://t.me/redkennews\n\n")
    return commandtext
示例#12
0
def hilightcommands(message):
    """
    Processes hilight commands in the message texts
    :return:
    """

    logger = logging.getLogger(__name__)

    msgdetail = stampy.stampy.getmsgdetail(message)

    texto = msgdetail["text"]
    chat_id = msgdetail["chat_id"]
    message_id = msgdetail["message_id"]
    who_un = msgdetail["who_un"]
    who_id = msgdetail["who_id"]

    logger.debug(msg=_L("Command: %s by user: %s") % (texto, who_un))
    try:
        command = texto.split(' ')[1]
    except:
        command = False
    try:
        word = texto.split(' ')[2]
    except:
        word = ""

    for case in stampy.stampy.Switch(command):
        if case('list'):
            text = listhilight(word=word, uid=who_id)
            stampy.stampy.sendmessage(chat_id=chat_id, text=text,
                                      reply_to_message_id=message_id,
                                      disable_web_page_preview=True,
                                      parse_mode="Markdown")
            break
        if case('delete'):
            text = _("Deleting hilight for `%s`") % word
            stampy.stampy.sendmessage(chat_id=chat_id, text=text,
                                      reply_to_message_id=message_id,
                                      disable_web_page_preview=True,
                                      parse_mode="Markdown")
            deletehilight(word=word, uid=who_id)
            break

        if case('add'):
            text = _("Adding hilight for `%s`") % word
            stampy.stampy.sendmessage(chat_id=chat_id, text=text,
                                      reply_to_message_id=message_id,
                                      disable_web_page_preview=True,
                                      parse_mode="Markdown")
            createhilight(word=word, uid=who_id)
            break

        if case():
            break

    return
示例#13
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """

    commandtext = ""
    if stampy.stampy.is_owner(message):
        commandtext = _("Use `/autok <key>=<value>` to autokarma <value> every time key is in the conversation. Multiple values for same <key> can be added\n\n")
        commandtext += _("Use `/autok delete <key>=<value>` to delete autokarma <value> for <key>\n\n")
        commandtext += _("Use `/autok list` to list autokarma <key> <value> pairs\n\n")
    return commandtext
示例#14
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """
    commandtext = _(
        "Use `/quote add <id> <text>` to add a quote for that username\n")
    commandtext += _(
        "Use `/quote <id>` to get a random quote from that username\n\n")
    if stampy.stampy.is_owner_or_admin(message):
        commandtext += _("Use `/quote del <quoteid>` to remove a quote\n\n")
    return commandtext
示例#15
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """

    commandtext = ""
    commandtext += _("Use `/comic list` to list comics defined\n")
    commandtext += _("Use `/comic all` to show all actual comics\n")
    if stampy.stampy.is_owner(message):
        commandtext += _(
            "Use `/comic trigger` to send comics to chats defined\n\n")
    return commandtext
示例#16
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """
    commandtext = ""

    if stampy.stampy.is_owner_or_admin(message):
        commandtext = _(
            "Use `/alias <key>=<value>` to assign an alias for karma\n")
        commandtext += _("Use `/alias list` to list aliases\n")
        commandtext += _("Use `/alias delete <key>` to remove an alias\n\n")
    return commandtext
示例#17
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """
    commandtext = ""

    if stampy.stampy.is_owner(message):
        commandtext = _("Use `/sudo gid=<gid>` to assign group to work on\n")
        commandtext += _(
            "Use `/sudo command` to execute command as if chat id were the one defined\n"
        )
    return commandtext
示例#18
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """

    commandtext = ""
    if stampy.stampy.is_owner_or_admin(message):
        commandtext += _("Use `/admin unlink` to remove channel linking\n")
        commandtext += _("Use `/admin link master` to generate and store linking token to be used on slaves\n")
        commandtext += _("Use `/admin link slave <token> to use provided token to link against master channel\n")
        commandtext += _("Use `/admin link show to list linked channel\n")
    return commandtext
示例#19
0
def showstats(type=False, name=None):
    """
    Shows stats for defined type or all if missing
    :param name: name to search in the stats database
    :param type: user or chat or empy for combined
    :return: table with the results
    """
    logger = logging.getLogger(__name__)
    if type:
        sql = "select type,id,name,date,count from stats WHERE type='%s'" % type

        if name:
            string = "%" + "%s" % name + "%"
            sql = sql + " and name like '%s'" % string
    else:
        sql = "select type,id,name,date,count from stats"

        if name:
            string = "%" + "%s" % name + "%"
            sql = sql + " WHERE name like '%s'" % string

    sql = sql + " ORDER BY count DESC LIMIT 10"

    cur = stampy.stampy.dbsql(sql)
    table = from_db_cursor(cur)
    text = _("Defined stats:\n")
    text = "%s\n```%s```" % (text, table.get_string())
    logger.debug(msg=_L("Returning stats %s") % text)
    return text
示例#20
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """

    commandtext = _("Use `/hilight add <word>` to add word to your "
                    "hilight list so messages in channels you're member "
                    "will be forwarded privately to you (need to start "
                    "prior conversation with bot)\n\n")
    commandtext += _("Use `/hilight delete <word>` to delete word "
                     "from your hilight list\n\n")
    commandtext += _("Use `/hilight list` to list hilights enabled "
                     "for your user\n\n")
    return commandtext
示例#21
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """

    commandtext = ""
    if stampy.stampy.is_owner(message):
        commandtext = _(
            "Use `/forward <source>=<target>` to assign a forwarder\n")
        commandtext += _("Use `/forward list` to list forwards defined\n")
        commandtext += _(
            "Use `/forward delete <source>=<target>` to remove a forwarding\n\n"
        )
    return commandtext
示例#22
0
def stampyphant(chat_id="", karma=0):
    """
    Returns a sticker for big karma values
    :param chat_id:
    :param karma:
    :return:
    """

    logger = logging.getLogger(__name__)
    karma = "%s" % karma
    # Sticker definitions for each rank
    x00 = "BQADBAADYwAD17FYAAEidrCCUFH7AgI"
    x000 = "BQADBAADZQAD17FYAAEeeRNtkOWfBAI"
    x0000 = "BQADBAADZwAD17FYAAHHuNL2oLuShwI"
    x00000 = "BQADBAADaQAD17FYAAHzIBRZeY4uNAI"

    sticker = ""
    if karma[-5:] == "00000":
        sticker = x00000
    elif karma[-4:] == "0000":
        sticker = x0000
    elif karma[-3:] == "000":
        sticker = x000
    elif karma[-2:] == "00":
        sticker = x00

    text = _("Sticker for %s karma points") % karma

    if sticker != "":
        stampy.stampy.sendsticker(chat_id=chat_id,
                                  sticker=sticker,
                                  text="%s" % text)
        logger.debug(msg=text)
    return
示例#23
0
def stock(message):
    """
    Processes stock commands
    :param message: Message with the command
    :return:
    """

    logger = logging.getLogger(__name__)
    c = IEXAPI()

    msgdetail = stampy.stampy.getmsgdetail(message)

    texto = msgdetail["text"]
    chat_id = msgdetail["chat_id"]
    message_id = msgdetail["message_id"]
    who_un = msgdetail["who_un"]

    logger.debug(msg=_L("Command: %s by %s" % (texto, who_un)))

    # We might be have been given no command, just stock
    try:
        command = texto.split(' ')[1]
    except:
        command = False

    if not command:
        stock = stampy.plugin.config.gconfig(key="stock",
                                             default="RHT",
                                             gid=chat_id).split(" ")
    else:
        stock = texto.split(" ")[1::]

    text = "```\n"
    currency = stampy.plugin.config.gconfig(key="currency",
                                            default="EUR",
                                            gid=chat_id)
    if currency != 'USD':
        rate = get_currency_rate('USD', currency)
    else:
        rate = 1
    text += _("USD/%s rate " % currency + str(rate) + "\n")
    for ticker in stock:
        try:
            quote = c.get(ticker.upper())
            text += "%s Quote " % quote["t"] + " " + str(
                quote["l_cur"]) + " " + str(
                    quote["c"]) + " (%s%%)" % str(quote["cp"])
            quoteUSD = quote["l_cur"]
            quoteEur = float(quoteUSD * rate)
            text += " (%s %s)\n" % ("{0:.2f}".format(quoteEur), currency)
        except:
            text += ""

    text += "```"
    stampy.stampy.sendmessage(chat_id=chat_id,
                              text=text,
                              reply_to_message_id=message_id,
                              disable_web_page_preview=True,
                              parse_mode="Markdown")
示例#24
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """
    commandtext = _("Use `/cn <word>` to get a random Chuck Norris quote based on word\n\n")
    return commandtext
示例#25
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """
    commandtext = _("Use `/espp <amount>` to get estimated ESPP earnings\n\n")
    return commandtext
示例#26
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """
    commandtext = _("Use `stock <ticker>` to get stock trading price\n\n")
    return commandtext
示例#27
0
def chanlinkslave(message, token=""):
    """
    Link a channel as slave of another and sets relevant configuration
    :param message: Message with the command
    :param token: Extracted token provided on command line
    :return:
    """
    logger = logging.getLogger(__name__)
    msgdetail = stampy.stampy.getmsgdetail(message)

    chat_id = msgdetail["chat_id"]
    message_id = msgdetail["message_id"]

    masterid = token.split(':')[0]

    logger.debug(msg=_L("chanenslave: %s, master-id: %s, master-token:%s") % (chat_id, masterid, token.split(':')[1]))

    if stampy.plugin.config.config(key='link-master', default=False,
                                   gid=masterid) == token:
        # In master GID, token is the same as the one provided

        # Delete link-master from master
        stampy.plugin.config.deleteconfig(key='link-master', gid=masterid)

        # Define 'link' and 'isolated' on slave
        stampy.plugin.config.setconfig(key='link', gid=chat_id, value=masterid)
        stampy.plugin.config.setconfig(key='isolated', gid=chat_id, value=True)

        # Notify master channel of slave linked
        text = _("Channel *%s* with name *%s* has been linked as *SLAVE*") % (chat_id, msgdetail['chat_name'])

        stampy.stampy.sendmessage(chat_id=masterid, text=text,
                                  disable_web_page_preview=True,
                                  parse_mode="Markdown")

        # Notify slave of master linked

        text = _("This channel has been set as *SLAVE* from *MASTER* channel *%s*") % masterid

        stampy.stampy.sendmessage(chat_id=chat_id, text=text,
                                  reply_to_message_id=message_id,
                                  disable_web_page_preview=True,
                                  parse_mode="Markdown")

    return
示例#28
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """

    commandtext = ""
    if stampy.stampy.is_owner(message):
        commandtext = _(
            "Use `/config show` to get a list of defined config settings\n")
        commandtext += _(
            "Use `/config set <key>=<value>` to define a value for key\n")
        commandtext += _("Use `/config delete <key>` to delete key\n\n")
    if stampy.stampy.is_owner_or_admin(message):
        commandtext += _(
            "/gconfig acts on 'effective chat' while /lconfig on 'local chat' (for linked))\n"
        )
        commandtext += _(
            "Use `/[g|l]config show` to get a list of defined group config settings\n"
        )
        commandtext += _(
            "Use `/[g|l]gconfig set <key>=<value>` to define a value for key\n"
        )
        commandtext += _("Use `/[g|l]gconfig delete <key>` to delete key\n\n")
    return commandtext
示例#29
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """

    commandtext = _("Use `/uptime` to return information about running time of the bot\n\n")
    return commandtext
示例#30
0
def help(message):  # do not edit this line
    """
    Returns help for plugin
    :param message: message to process
    :return: help text
    """

    commandtext = ""
    commandtext += _("Use `/feed list` to list feeds defined\n")
    commandtext += _("Use `/feed <name>` show items from feed\n")
    commandtext += _(
        "Use `/feed add <name> <url> <interval:min>` to add a new feed (default interval 1440 mins)\n"
    )
    commandtext += _("Use `/feed delete <name>` to remove an existing feed\n")
    if stampy.stampy.is_owner(message):
        commandtext += _(
            "Use `/feed trigger` to send feeds to chats defined\n\n")
    return commandtext