def send_message(self, stanza, to, body, log = False): """ 发送消息 `stanza` - 消息节 `to` - 接收人 接收人不在线发送离线消息 `body` - 消息主体 `log` - 记录历史消息 """ if log: add_history(stanza.from_jid, to, body) if is_online(to): mode = get_info('mode', to) if mode == 'talk' or not mode: if isinstance(to, (str, unicode)): to = JID(to) self.logger.debug("send '{0}' to {1!r}".format(body, to)) typ = stanza.stanza_type self._stream.send(self.make_message(to, typ, body)) else: body = NOW() + ' ' + body self.logger.debug("store offline message'{0}' for {1!r}" .format(body, to)) offline_message = get_info('offline_message', to, '') offline_message += '\n' + body add_info('offline_message', offline_message, to)
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]