Ejemplo n.º 1
0
def handle_userstateset(bot, ievent):
    try:
        (username, item, value) = ievent.args
    except ValueError:
        ievent.missing('<username> <item> <value>')
        return
    userstate = UserState(username)
    userstate[item] = value
    userstate.save()
    ievent.reply('userstate %s set to %s' % (item, value))
Ejemplo n.º 2
0
def handle_userstateset(bot, ievent):
    try:
        (username, item, value) = ievent.args
    except ValueError:
        ievent.missing('<username> <item> <value>')
        return
    userstate = UserState(username)
    userstate[item] = value
    userstate.save()
    ievent.reply('userstate %s set to %s' % (item, value))
Ejemplo n.º 3
0
def getemail(username):
    try:
        state = UserState(username)
        if state:
            return state['email']
    except KeyError:
        return
Ejemplo n.º 4
0
def handle_time(bot, ievent):
    """ show current time """

    authuser = users.getname(ievent.userhost)

    if authuser:

        if ievent.rest:
            if users.exist(ievent.rest.lower()):
                username = ievent.rest.lower()
            else:
                ievent.reply("We don't have a user %s" % ievent.rest)
                return
        else:
            username = authuser

        userstate = UserState(username)

        try:
            tz = userstate['TZ']
        except KeyError:
            if username == authuser:
                tz = handle_ask_timezone(bot, ievent)
                if tz and set_timezone(bot, ievent, userstate, tz):
                    tz = userstate['TZ']
                else:
                    return
            else:
                ievent.reply("%s doesn't have a timezone set" % username)
                return
        ievent.reply(get_time(tz, username, authuser))

    else:
        ievent.reply(get_time('UTC', '', ''))
Ejemplo n.º 5
0
def handle_userstate(bot, ievent):
    try:
        (item, value) = ievent.args
    except ValueError:
        item = value = None
    username = users.getname(ievent.userhost)
    userstate = UserState(username)
    if item and value:
        userstate[item] = value
        userstate.save()
    result = []
    for i, j in userstate.data.iteritems():
        result.append("%s=%s" % (i, j))
    if result:
        ievent.reply("userstate of %s: " % username, result, dot=True)
    else:
        ievent.reply('no userstate of %s known' % username)
Ejemplo n.º 6
0
def handle_userstate(bot, ievent):
    try:
        (item, value) = ievent.args
    except ValueError:
        item = value = None
    username = users.getname(ievent.userhost)
    userstate = UserState(username)
    if item and value:
        userstate[item] = value
        userstate.save()
    result = []
    for i, j in userstate.data.iteritems():
        result.append("%s=%s" % (i, j))
    if result:
        ievent.reply("userstate of %s: " % username, result, dot=True)
    else:
        ievent.reply('no userstate of %s known' % username)
Ejemplo n.º 7
0
def handle_userstatedel(bot, ievent):
    username = None
    try:
        (username, item)  = ievent.args
    except ValueError:
        try:
           item = ievent.args[0]
        except IndexError:
            ievent.missing('[username] <item>')
            return
    if not username:
        username = users.getname(ievent.userhost)
    userstate = UserState(username)
    try:
        del userstate.data[item]
    except KeyError:
        ievent.reply('no such item')
        return
    userstate.save()
    ievent.reply('item %s deleted' % item)
Ejemplo n.º 8
0
def handle_userstatedel(bot, ievent):
    username = None
    try:
        (username, item) = ievent.args
    except ValueError:
        try:
            item = ievent.args[0]
        except IndexError:
            ievent.missing('[username] <item>')
            return
    if not username:
        username = users.getname(ievent.userhost)
    userstate = UserState(username)
    try:
        del userstate.data[item]
    except KeyError:
        ievent.reply('no such item')
        return
    userstate.save()
    ievent.reply('item %s deleted' % item)
Ejemplo n.º 9
0
def handle_userstateget(bot, ievent):
    if not ievent.rest:
        ievent.missing('<username>')
        return
    userstate = UserState(ievent.rest)
    result = []
    for i, j in userstate.data.iteritems():
        result.append("%s=%s" % (i, j))
    if result:
        ievent.reply("userstate of %s: " % ievent.rest, result, dot=True)
    else:
        ievent.reply('no userstate of %s known' % ievent.rest)
Ejemplo n.º 10
0
def handle_timezone(bot, ievent):
    """ timezone .. set current timezone. """

    username = users.getname(ievent.userhost)

    if username:
        userstate = UserState(username)
        if ievent.rest:
            try:
                timezone = int(ievent.rest)

                set_timezone(bot, ievent, userstate, timezone)
            except ValueError:
                ievent.reply('TZ needs to be an integer')
                return
        else:
            timezone = handle_ask_timezone(bot, ievent)
            if timezone:
                set_timezone(bot, ievent, userstate, timezone)
            else:
                ievent.reply("can't determine timezone")
Ejemplo n.º 11
0
def handle_weather(bot, ievent):
    """ show weather using Google's weather API """
    userhost = ""
    loc = ""
    try:
        nick = ievent.options['--u']
        if nick:
            userhost = getwho(bot, nick)
            if not userhost:
                ievent.reply("can't determine username for %s" % nick)
                return
            else:
                try:
                    name = users.getname(userhost)
                    if not name:
                         ievent.reply("%s is not known with the bot" % nick)
                         return
                    us = UserState(name)
                    loc = us['location']
                except KeyError:
                    ievent.reply("%s doesn't have his location set in \
userstate" % nick)
                    return
    except KeyError:
         pass
    if not loc:
        if ievent.rest:
             loc = ievent.rest
        else:
             ievent.missing('--u <nick> or <location>')
             return
    query = urlencode({'weather':loc})
    weathertxt = geturl('http://www.google.ca/ig/api?%s' % query)
    if 'problem_cause' in weathertxt:
        rlog(10, 'weather', 'ERROR: %s' % weathertxt)
        ievent.reply('an error occured looking up data for %s' % loc)
        return
    resultstr = ""
    if len(weathertxt) > 135:
        gweather = minidom.parseString(weathertxt)
        gweather = gweather.getElementsByTagName('weather')[0]
        if ievent.command == "weather":
            info = gweather.getElementsByTagName('forecast_information')[0]
            if info:
                city = info.getElementsByTagName('city')[0].attributes["data"].value
                zip = info.getElementsByTagName('postal_code')[0].attributes["data"].value
                time = info.getElementsByTagName('current_date_time')[0].attributes["data"].value

                weather = gweather.getElementsByTagName('current_conditions')[0]
                condition = weather.getElementsByTagName('condition')[0].attributes["data"].value
                temp_f = weather.getElementsByTagName('temp_f')[0].attributes["data"].value
                temp_c = weather.getElementsByTagName('temp_c')[0].attributes["data"].value
                humidity = weather.getElementsByTagName('humidity')[0].attributes["data"].value
                wind = weather.getElementsByTagName('wind_condition')[0].attributes["data"].value

                try:
                    wind_km = round(int(wind[-6:-4]) * 1.609344)
                except ValueError:
                    wind_km = ""

                if (not condition == ""):
                    condition = " Oh, and it's " + condition + "."

                resultstr = "As of %s, %s (%s) has a temperature of %sC/%sF with %s. %s (%s km/h).%s" % (time, city, zip, temp_c, temp_f, humidity, wind, wind_km, condition)
        elif ievent.command == "forecast":
            forecasts = gweather.getElementsByTagName('forecast_conditions')
            for forecast in forecasts:
                condition = forecast.getElementsByTagName('condition')[0].attributes["data"].value
                low_f = forecast.getElementsByTagName('low')[0].attributes["data"].value
                high_f = forecast.getElementsByTagName('high')[0].attributes["data"].value
                day = forecast.getElementsByTagName('day_of_week')[0].attributes["data"].value
                low_c = round((int(low_f) - 32) * 5.0 / 9.0)
                high_c = round((int(high_f) - 32) * 5.0 / 9.0)
                resultstr += "[%s: F(%sl/%sh) C(%sl/%sh) %s]" % (day, low_f, high_f, low_c, high_c, condition)
    if not resultstr:
        ievent.reply('%s not found!' % loc)
        return
    else:
        ievent.reply(resultstr)