def _switch_channel(self, email, channel): """ 切换频道 """ oldch = get_info("channel", email) self._logger.info("{0} switch channel from {1} to {2}".format(email, oldch, channel)) del_channel_user(oldch) add_info("channel", channel, email) add_channel_user(channel)
def send_offline_message(self, stanza): """ 发送离线消息 """ show = stanza.show frm = stanza.from_jid offline_message = get_info('offline_message', frm) if offline_message: offline_message = "离线期间的消息:\n" + offline_message m = self.make_message(frm, 'normal', offline_message) self._stream.send(m) set_online(frm, show) add_info('offline_message', '', frm)
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 cd(self, stanza, *args): """ 进入模式/频道 """ mode = args[0] if len(args) >= 1 else None channels = get_channel() cnames = [v.get("name") for v in channels] if not mode or (mode not in self._modes and mode not in cnames): body = "Usage:\n\ -cd MODE/CHANNEL 进入MODE模式,使用-ls m查看允许的模式" else: if mode in self._modes: add_info("mode", mode, stanza.from_jid) body = " 你已进入{0}".format(self._modes[mode]) else: current_channel = [v for v in channels if v.get("name") == mode][0] uc = get_info("channel", stanza.from_jid) uc = uc if uc else "main" if uc == current_channel.get("name"): body = u"你已经在 {0} 频道".format(mode) return self._send_cmd_result(stanza, body) else: del_channel_user(uc) if current_channel.get("passwd"): pwd = args[1] if len(args) == 2 else None if pwd == current_channel.get("passwd"): add_info("channel", mode, stanza.from_jid) add_channel_user(mode) body = " 你已进入 {0} 频道".format(mode) else: body = " 频道密码错误" if not pwd: body = u"频道已加密,需要密码" else: add_info("channel", mode, stanza.from_jid) add_channel_user(mode) body = " 你已进入 {0} 频道".format(mode) self._send_cmd_result(stanza, body)