def mv(self, stanza, *args): """ 移动用户到指定频道 """ if len(args) < 2: body = u"Usage: -mv NICK CHANNLE" return self._send_cmd_result(stanza, body) if args[1] == ".": distch = get_info("channel", stanza.from_jid) else: distch = args[1] if args[0] == "*": members = get_members(stanza.from_jid) emails = [v for v in members if get_info("channel", v) != distch] else: info = get_member(nick=args[0]) emails = [info.get("email")] channel = get_channel(distch) if not emails: body = u"{0} 成员不存在".format(args[0]) return self._send_cmd_result(stanza, body) if not channel: body = u"{0} 频道不存在".format(args[1]) return self._send_cmd_result(stanza, body) nick = get_nick(stanza.from_jid) body = "{0} 将你移动到 {1} 频道".format(nick, distch) nicks = [] for email in emails: nicks.append(get_nick(email)) self._switch_channel(email, distch) self._message_bus.send_message(stanza, email, body) body = "你将 {0} 移动到 {1} 频道".format(",".join(nicks), distch) self._send_cmd_result(stanza, body)
def _run_cmd(self, stanza, body): """ 执行命令 """ cmd = body[1:] c, args = self._parse_args(cmd) email = get_email(stanza.from_jid) cmds = [v.get("name") for v in self._get_cmd()] cmds.append("_ping") cmds.append("_tq") if c not in cmds: self._message_bus.send_all_msg(stanza, body) return try: self._logger.info("%s run cmd %s", email, c) m = getattr(self, c)(stanza, *args) except Exception as e: self._logger.warning(e.message) errorinfo = traceback.format_exc() body = u"{0} run command {1} happend an error:\ {2}".format( get_nick(email), c, errorinfo ) self._message_bus.send_to_admin(stanza, body) self._message_bus.send_back_msg(stanza, c + " 命令异常,已通知管理员") return return m
def handle_presence_unsubscribe(self, stanza): self.logger.info(u"{0} canceled presence subscription" .format(stanza.from_jid)) presence = Presence(to_jid = stanza.from_jid.bare(), stanza_type = "unsubscribe") nick = get_nick(stanza.from_jid) self.message_bus.send_sys_msg(stanza, u'{0} 离开群'.format(nick)) del_member(stanza.from_jid.bare()) r =[stanza.make_accept_response(), presence] return r
def _show_channel(self, stanza, name): channel = get_channel(name) name = channel.get("name") usernum = channel.get("usernum") passwd = channel.get("passwd") isencrypt = u"是" if passwd else u"否" owner = channel.get("owner") nick = get_nick(owner) if owner != "bot" else owner body = "频道名称: {0} 频道人数: {1}\n" "是否加密: {2} 拥有者:{3}".format(name, usernum, isencrypt, nick) if owner == stanza.from_jid.bare().as_string() and passwd: body += "\n频道密码: " + passwd self._send_cmd_result(stanza, body)
def code(self, stanza, *args): """<type> <code> 贴代码,使用-ls ct 查看允许的代码类型""" if len(args) <= 1: return self.help(stanza, "code") nick = get_nick(stanza.from_jid) typ = args[0] codes = add_commends(args[1:], typ, nick) codes = "".join(codes[0:2]) + " ".join(codes[2:]) poster = "Pythoner Club: %s" % nick r = paste_code(poster, typ, codes) if r: self._message_bus.send_all_msg(stanza, r) self._send_cmd_result(stanza, r) else: self._send_cmd_result(stanza, "代码服务异常,通知管理员")
def nick(self, stanza, *args): """更改昵称 eg. -nick yournewnickname""" if len(args) < 1: return self.help(stanza, "nick") nick = " ".join(args[0:]) channels = [v.get("name") for v in get_channel()] if nick in self._invaild or nick in channels: return self._send_cmd_result(stanza, "昵称不合法") frm = stanza.from_jid oldnick = get_nick(frm) r = edit_member(frm, nick=nick) if r: body = "%s 更改昵称为 %s" % (oldnick, nick) self._message_bus.send_sys_msg(stanza, body) self._send_cmd_result(stanza, u"你的昵称现在的已经已经更改为 {0}".format(nick)) else: self._send_cmd_result(stanza, "昵称已存在")
def send_all_msg(self, stanza, body): """ 给除了自己的所有成员发送消息 """ if cityid(body.strip()): return self.send_command(stanza, '-_tq ' + body.strip()) if body.strip() == 'help': return self.send_command(stanza, '-help') if body.strip() == 'ping': return self.send_command(stanza, '-_ping') mode = get_info('mode', stanza.from_jid) if mode == 'quiet': body = u'你处于{0},请使用-cd命令切换到 {1} '\ u'后发言'.format(MODES[mode], MODES['talk']) return self.send_back_msg(stanza, body) add_history(stanza.from_jid, 'all', body) members = get_members(stanza.from_jid) current = get_info('channel', stanza.from_jid, 'main') members = [m for m in members if get_info('channel', m, 'main') == current] self.logger.info("{0} send message {1} to {2!r}" .format(stanza.from_jid, body, members)) nick = get_nick(stanza.from_jid) body = "[{0}] {1}".format(nick, body) [self.send_message(stanza, m, body) for m in members]
def send_private_msg(self, stanza, to, body): """ 发送私信 """ frm = stanza.from_jid nick = get_nick(frm) body = "[%s 悄悄对你说] %s" % (nick, body) self.send_message(stanza, to, body, True)