From 3bb0824e75e35707244c83df2c74ef1b6a1dcdb7 Mon Sep 17 00:00:00 2001 From: Alexei Sorokin Date: Wed, 24 Aug 2016 17:17:33 +0300 Subject: [PATCH] Alias client.plugin["xep_0045"] only once --- hptoad.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/hptoad.py b/hptoad.py index 9294bc9..6182001 100755 --- a/hptoad.py +++ b/hptoad.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - + import logging import os import re @@ -9,7 +9,7 @@ import subprocess import sys import time import sleekxmpp - + opts = { "muc": "room@conference.example.com", "nick": "botname", @@ -30,6 +30,7 @@ class Zhobe: opts["password"]) self.client.register_plugin("xep_0199") # XMPP Ping. self.client.register_plugin("xep_0045") # XMPP MUC. + self.muc_obj = self.client.plugin["xep_0045"] self.jid = opts["jid"] self.connect = opts["connect"] @@ -45,12 +46,10 @@ class Zhobe: self.on_muc_presence) def join_muc(self): - muc_plugin = self.client.plugin["xep_0045"] - - if self.muc in muc_plugin.getJoinedRooms(): - muc_plugin.leaveMUC(self.muc, self.bot_nick, + if self.muc in self.muc_obj.getJoinedRooms(): + self.muc_obj.leaveMUC(self.muc, self.bot_nick, 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 def log_exception(cls, ex): @@ -63,11 +62,10 @@ class Zhobe: event["type"], event["body"])) def is_muc_admin(self, muc, nick): - muc_plugin = self.client.plugin["xep_0045"] - if nick not in muc_plugin.rooms[self.muc]: + if nick not in self.muc_obj.rooms[self.muc]: 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 _trim_regexp = re.compile("(`|\\$|\\.\\.)") @@ -136,8 +134,6 @@ class Zhobe: return reply, err def handle_cmd(self, body, nick, from_id, is_admin=False): - muc_plugin = self.client.plugin["xep_0045"] - reply = "" err = None @@ -152,8 +148,8 @@ class Zhobe: if is_admin and victim != self.bot_nick: if is_bot_admin and not is_victim_admin and \ - victim in muc_plugin.rooms[self.muc]: - muc_plugin.setRole(self.muc, victim, "none") + victim in self.muc_obj.rooms[self.muc]: + self.muc_obj.setRole(self.muc, victim, "none") else: reply = "%s: Can't megakick %s." % (nick, victim) else: @@ -229,7 +225,6 @@ class Zhobe: self.log_exception(e) def on_muc_presence(self, event): - muc_plugin = self.client.plugin["xep_0045"] try: typ = event["muc"]["type"] from_id = event["from"] @@ -238,7 +233,7 @@ class Zhobe: if not typ: typ = event["type"] if not nick: - nick = muc_plugin.getNick(self.muc, from_id) + nick = self.muc_obj.getNick(self.muc, from_id) if typ == "error": if event["error"]["code"] == "409":