Example #1
0
def get_status(frm):
    if isinstance(frm , JID):
        where = "`email`='{0}' and `resource`='{1}'".format(get_email(frm),
                                                            frm.resource)
    else:
        where = "`email`='{0}'".format(get_email(frm))

    with MySQLContext('status') as op:
        r = op.select(where = where)
    return [v.get('statustext') for v in r]
Example #2
0
def set_online(frm, statustext):
    email = get_email(frm)
    resource = frm.resource if frm.resource else ''
    with MySQLContext('status') as op:
        if get_status(frm):
            set_dict = dict(status=1)
            where = "email='{0}' and resource='{1}'".format(email, resource)
            r = op.update(set_dict, where)
        else:
            r = op.insert(('status', 'statustext', 'email', 'resource'),
                          (1, statustext, get_email(frm), resource))
    return r
Example #3
0
def is_online(frm):
    email = get_email(frm)
    where = "`email`='{0}'".format(email)
    with MySQLContext('status') as op:
        r = op.select(where = where)
    stats = [v.get('status') for v in r]
    return 1 in stats
Example #4
0
def add_history(frm, to, content):
    email = get_email(frm)
    fields = ("frmemail", "toemail", "content", "date")
    values = (email, to, content, NOW())
    add_info("last_say", NOW(), frm)
    with MySQLContext("history") as op:
        return op.insert(fields, values)
Example #5
0
def get_user_info(frm):
    """ 获取用户信息 """
    isonline = '在线' if is_online(frm) else '离线'
    status = get_status(frm)
    status = status[1] if len(status) == 2 else None
    status = status if status else isonline
    resource = get_resource(frm)
    join_time = get_info('join_time', frm, '创造之始')
    last_say = get_info('last_say', frm, '从未发言')
    last_change_nick = get_info('last_change_nick', frm, '从未修改')
    change_nick_times = get_info('change_nick_times', frm, 0)
    last_online_time = get_info('last_online', frm, join_time)
    level = "管理员" if get_email(frm) in ADMINS else "成员"
    mode = get_info('mode', frm, 'talk')
    mode = MODES[mode]
    channel = get_info('channel', frm, 'main')
    nick = get_nick(frm)
    result = dict(isonline = isonline, level = level, status = status,
                  join_time = join_time, mode = mode, last_say = last_say,
                  last_change_nick = last_change_nick, nick = nick,
                  resource = ','.join(resource), channel = channel,
                  last_online_time = last_online_time,
                  change_nick_times = change_nick_times)
    body = user_info_template.substitute(result)
    return body
Example #6
0
def get_info(key, frm, default = None):
    email = get_email(frm)
    where = "`key`='{0}' and `email`='{1}'".format(key, email)
    with Op(TABLE) as op:
        r = op.select_one(where=where).get('value')
        if default is not None:
            r = r if r else default
        return r
Example #7
0
def set_offline(frm):
    resource = frm.resource
    email = get_email(frm)
    where = "email='{0}' and resource='{1}'".format(email, resource)
    with MySQLContext('status') as op:
        r = op.remove(where)
    add_info('last_online', NOW(), frm)
    return r
Example #8
0
def change_channel_pwd(frm, name, passwd):
    channel = get_channel(name = name)
    if channel.get('owner') != get_email(frm):
        return False

    with MC(TABLE) as op:
        where = "`name`='{0}'".format(op.escape(name))
        set_dict = {'passwd' : passwd}
        return op.update(set_dict, where)
Example #9
0
def add_member(frm, name = None):
    if not name: name = frm.local
    email = get_email(frm)
    if get_member(frm): return

    fields = ('email', 'name', 'nick', 'last', 'lastchange', 'date')
    values = (email, name, name, now, now, now)
    add_info('join_time', now, frm)
    with MySQLContext(TABLE) as op:
        return op.insert(fields, values)
Example #10
0
def get_members(frm = None):
    where = None
    if frm:
        email = get_email(frm)
        where = "email!='{0}'".format(email)

    with MySQLContext(TABLE) as op:
        r = op.select(fields = ('email',), where = where)

    emails = [v.get('email') for v in r]
    return emails
Example #11
0
def add_info(key, value, frm):
    email = get_email(frm)
    where = "`key`='{0}' and `email`='{1}'".format(key, email)
    with Op(TABLE) as op:
        if get_info(key, frm) is not None:
            set_dic = dict(value=value)
            return op.update(set_dic, where)
        else:
            fields = ('email', 'key', 'value')
            values = (email, key, value)
            return op.insert(fields, values)
Example #12
0
def add_channel(frm, name, passwd=None):
    """ 创建频道 """
    owner = get_email(frm)
    if name in MODES.keys() or name == 'main':
        return False
    with MC(TABLE) as op:
        if get_channel(name):
            return False
        fields = ('name', 'passwd', 'owner')
        values = (name, passwd, owner)
        return op.insert(fields, values)
Example #13
0
 def send_command(self, stanza,  body):
     """ 处理命令
         为防止阻塞使用线程池处理命令
     """
     email = get_email(stanza.from_jid)
     self.logger.info("{0} run command {1}".format(stanza.from_jid, body))
     if email in ADMINS:
         target = self.admin_cmd_handler._run_cmd
     else:
         target = self.cmd_handler._run_cmd
     self._thread_pool.add_job(target, stanza, body)
Example #14
0
def edit_member(frm, nick):
    email = get_email(frm)
    with MySQLContext(TABLE) as op:
        where = "nick='{0}'".format(nick)
        if op.select(where=where): return False
        where = "email='{0}'".format(email)
        set_dict = dict(nick = nick, lastchange=now)
        add_info('last_change_nick', now, frm)
        times = get_info('change_nick_times', frm)
        times = int(times) if times else 0
        add_info('change_nick_times', times + 1, frm)
        return op.update(set_dict, where)
Example #15
0
def get_member(frm = None, uid = None, nick = None):
    wh_list = []
    if frm:
        email = get_email(frm)
        wh_list.append('`email`="{0}"'.format(email))

    if uid:
        wh_list.append('`id`="{0}"'.format(uid))

    if nick:
        wh_list.append('`nick`="{0}"'.format(nick))

    where = ' and '.join(wh_list)
    with MySQLContext(TABLE) as op:
        return op.select_one(where = where)
Example #16
0
def get_nick(frm):
    email = get_email(frm)
    with MySQLContext(TABLE) as op:
        return op.select_one(where="email='{0}'".format(email)).get('nick')
Example #17
0
def del_member(frm):
    email = get_email(frm)
    where = "email='{0}'".format(email)
    with MySQLContext(TABLE) as op:
        return op.remove(where)
Example #18
0
def get_resource(frm):
    where = "email='{0}'".format(get_email(frm))
    with MySQLContext('status') as op:
        r = op.select(where = where)
    return [v.get('resource') for v in r]