Alias client.plugin["xep_0045"] only once

This commit is contained in:
Alexei Sorokin 2016-08-24 17:17:33 +03:00
parent 96c975233c
commit 3bb0824e75
1 changed files with 11 additions and 16 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging import logging
import os import os
import re import re
@ -9,7 +9,7 @@ import subprocess
import sys import sys
import time import time
import sleekxmpp import sleekxmpp
opts = { opts = {
"muc": "room@conference.example.com", "muc": "room@conference.example.com",
"nick": "botname", "nick": "botname",
@ -30,6 +30,7 @@ class Zhobe:
opts["password"]) opts["password"])
self.client.register_plugin("xep_0199") # XMPP Ping. self.client.register_plugin("xep_0199") # XMPP Ping.
self.client.register_plugin("xep_0045") # XMPP MUC. self.client.register_plugin("xep_0045") # XMPP MUC.
self.muc_obj = self.client.plugin["xep_0045"]
self.jid = opts["jid"] self.jid = opts["jid"]
self.connect = opts["connect"] self.connect = opts["connect"]
@ -45,12 +46,10 @@ class Zhobe:
self.on_muc_presence) self.on_muc_presence)
def join_muc(self): def join_muc(self):
muc_plugin = self.client.plugin["xep_0045"] if self.muc in self.muc_obj.getJoinedRooms():
self.muc_obj.leaveMUC(self.muc, self.bot_nick,
if self.muc in muc_plugin.getJoinedRooms():
muc_plugin.leaveMUC(self.muc, self.bot_nick,
msg="Replaced by new connection") msg="Replaced by new connection")
muc_plugin.joinMUC(self.muc, self.bot_nick, wait=True) self.muc_obj.joinMUC(self.muc, self.bot_nick, wait=True)
@classmethod @classmethod
def log_exception(cls, ex): def log_exception(cls, ex):
@ -63,11 +62,10 @@ class Zhobe:
event["type"], event["body"])) event["type"], event["body"]))
def is_muc_admin(self, muc, nick): def is_muc_admin(self, muc, nick):
muc_plugin = self.client.plugin["xep_0045"] if nick not in self.muc_obj.rooms[self.muc]:
if nick not in muc_plugin.rooms[self.muc]:
return False return False
affiliation = muc_plugin.getJidProperty(muc, nick, "affiliation") affiliation = self.muc_obj.getJidProperty(muc, nick, "affiliation")
return True if affiliation in ("admin", "owner") else False return True if affiliation in ("admin", "owner") else False
_trim_regexp = re.compile("(`|\\$|\\.\\.)") _trim_regexp = re.compile("(`|\\$|\\.\\.)")
@ -136,8 +134,6 @@ class Zhobe:
return reply, err return reply, err
def handle_cmd(self, body, nick, from_id, is_admin=False): def handle_cmd(self, body, nick, from_id, is_admin=False):
muc_plugin = self.client.plugin["xep_0045"]
reply = "" reply = ""
err = None err = None
@ -152,8 +148,8 @@ class Zhobe:
if is_admin and victim != self.bot_nick: if is_admin and victim != self.bot_nick:
if is_bot_admin and not is_victim_admin and \ if is_bot_admin and not is_victim_admin and \
victim in muc_plugin.rooms[self.muc]: victim in self.muc_obj.rooms[self.muc]:
muc_plugin.setRole(self.muc, victim, "none") self.muc_obj.setRole(self.muc, victim, "none")
else: else:
reply = "%s: Can't megakick %s." % (nick, victim) reply = "%s: Can't megakick %s." % (nick, victim)
else: else:
@ -229,7 +225,6 @@ class Zhobe:
self.log_exception(e) self.log_exception(e)
def on_muc_presence(self, event): def on_muc_presence(self, event):
muc_plugin = self.client.plugin["xep_0045"]
try: try:
typ = event["muc"]["type"] typ = event["muc"]["type"]
from_id = event["from"] from_id = event["from"]
@ -238,7 +233,7 @@ class Zhobe:
if not typ: if not typ:
typ = event["type"] typ = event["type"]
if not nick: if not nick:
nick = muc_plugin.getNick(self.muc, from_id) nick = self.muc_obj.getNick(self.muc, from_id)
if typ == "error": if typ == "error":
if event["error"]["code"] == "409": if event["error"]["code"] == "409":