| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
import logging |
|---|
| 21 |
|
|---|
| 22 |
from pyxmpp.jid import JID |
|---|
| 23 |
|
|---|
| 24 |
from common import normalize,nick_to_node |
|---|
| 25 |
|
|---|
| 26 |
class IRCUser: |
|---|
| 27 |
def __init__(self,session,nick,user="",host=""): |
|---|
| 28 |
self.__logger=logging.getLogger("jjigw.IRCUser") |
|---|
| 29 |
self.sync_delay=0 |
|---|
| 30 |
self.session=session |
|---|
| 31 |
if "!" in nick: |
|---|
| 32 |
nick,tmp=nick.split("!",1) |
|---|
| 33 |
if "@" in tmp: |
|---|
| 34 |
user,host=tmp.split("@",1) |
|---|
| 35 |
else: |
|---|
| 36 |
user=tmp |
|---|
| 37 |
host="" |
|---|
| 38 |
self.nick=nick |
|---|
| 39 |
self.user=user |
|---|
| 40 |
self.host=host |
|---|
| 41 |
self.mode={} |
|---|
| 42 |
self.channels={} |
|---|
| 43 |
self.current_thread=None |
|---|
| 44 |
|
|---|
| 45 |
def descr(self): |
|---|
| 46 |
if self.user and self.host: |
|---|
| 47 |
return "%s(%s@%s)" % (self.nick,self.user,self.host) |
|---|
| 48 |
else: |
|---|
| 49 |
return self.nick |
|---|
| 50 |
|
|---|
| 51 |
def sync_in_channel(self,channel,status=None): |
|---|
| 52 |
if self.sync_delay>0: |
|---|
| 53 |
return |
|---|
| 54 |
elif self.sync_delay<0: |
|---|
| 55 |
self.__logger.debug("Warning: %r.sync_delay<0" % (self,)) |
|---|
| 56 |
return channel.sync_user(self,status=status) |
|---|
| 57 |
|
|---|
| 58 |
def join_channel(self,channel): |
|---|
| 59 |
self.channels[normalize(channel.name)]=channel |
|---|
| 60 |
self.sync_in_channel(channel) |
|---|
| 61 |
|
|---|
| 62 |
def leave_channel(self,channel,status=None): |
|---|
| 63 |
try: |
|---|
| 64 |
del self.channels[normalize(channel.name)] |
|---|
| 65 |
self.sync_in_channel(channel,status=status) |
|---|
| 66 |
except KeyError: |
|---|
| 67 |
pass |
|---|
| 68 |
|
|---|
| 69 |
def leave_all(self): |
|---|
| 70 |
for channel in self.channels.values(): |
|---|
| 71 |
self.leave_channel(channel) |
|---|
| 72 |
|
|---|
| 73 |
def sync_all(self): |
|---|
| 74 |
for channel in self.channels.values(): |
|---|
| 75 |
self.sync_in_channel(channel) |
|---|
| 76 |
|
|---|
| 77 |
def whoreply(self,params): |
|---|
| 78 |
if params[4]!=self.nick: |
|---|
| 79 |
return |
|---|
| 80 |
if len(params)!=7: |
|---|
| 81 |
return |
|---|
| 82 |
channel,user,host,server,nick,flags,rest=params |
|---|
| 83 |
fullname=rest.split(None,1)[1] |
|---|
| 84 |
if channel and channel!="*": |
|---|
| 85 |
channel=self.session.channels.get(normalize(channel)) |
|---|
| 86 |
if not channel: |
|---|
| 87 |
self.__logger.debug("Ignoring WHO reply: %r - unknown channel" % (params,)) |
|---|
| 88 |
return |
|---|
| 89 |
else: |
|---|
| 90 |
channel=None |
|---|
| 91 |
self.sync_delay+=1 |
|---|
| 92 |
try: |
|---|
| 93 |
self.nick=nick |
|---|
| 94 |
self.host=host |
|---|
| 95 |
self.user=user |
|---|
| 96 |
if channel: |
|---|
| 97 |
self.join_channel(channel) |
|---|
| 98 |
if "@" in flags: |
|---|
| 99 |
channel.set_mode("o",self) |
|---|
| 100 |
elif "+" in flags: |
|---|
| 101 |
channel.set_mode("v",self) |
|---|
| 102 |
else: |
|---|
| 103 |
channel.reset_mode("o",self) |
|---|
| 104 |
channel.reset_mode("v",self) |
|---|
| 105 |
if "G" in flags: |
|---|
| 106 |
self.mode["a"]=1 |
|---|
| 107 |
else: |
|---|
| 108 |
self.mode["a"]=0 |
|---|
| 109 |
finally: |
|---|
| 110 |
self.sync_delay-=1 |
|---|
| 111 |
if channel: |
|---|
| 112 |
channel.sync_user(self) |
|---|
| 113 |
|
|---|
| 114 |
def jid(self): |
|---|
| 115 |
if self.user and self.host: |
|---|
| 116 |
res=unicode(self.user+'@'+self.host,self.session.default_encoding,"replace") |
|---|
| 117 |
else: |
|---|
| 118 |
res=u"" |
|---|
| 119 |
return JID(nick_to_node(self.nick,self.session.default_encoding), |
|---|
| 120 |
self.session.network.jid.domain,res) |
|---|
| 121 |
|
|---|
| 122 |
def __repr__(self): |
|---|
| 123 |
return "<IRCUser %r: %r>" % (id(self),self.nick) |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|