Skip to content

Commit

Permalink
Identity format compatibility fixes on the IRC backend
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed Sep 27, 2012
1 parent fd100fc commit e2325d3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
22 changes: 16 additions & 6 deletions errbot/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ class Identifier(object):

def __init__(self, jid=None, node='', domain='', resource=''):
if jid:
self.node, self.domain = jid.split('@')
if self.domain.find('/') != -1:
self.domain, self.resource = self.domain.split('/')
if jid.find('@') != -1:
self.node, self.domain = jid.split('@')[0:2] # hack for IRC
if self.domain.find('/') != -1:
self.domain, self.resource = self.domain.split('/')[0:2] # hack for IRC where you can have several slashes here
else:
self.resource = self.node # put a default one
else:
self.resource = self.node # put a default one
self.node = jid
self.resource = None
self.domain = None
else:
self.node = node
self.domain = domain
Expand All @@ -41,10 +46,15 @@ def bareMatch(self, other):
return other.node == self.node

def getStripped(self):
return self.node + '@' + self.domain
if self.domain:
return self.node + '@' + self.domain
return self.node # if the backend has no domain notion


def getResource(self):
return self.resource
if self.resource:
return self.resource
return self.node # this is because if the backend has no resource notion we need to return the plain identifier

def __str__(self):
answer = self.getStripped()
Expand Down
11 changes: 7 additions & 4 deletions errbot/backends/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def __init__(self, callback, nickname='err'):

def send_message(self, mess):
if self.connected:
self.msg(mess.getTo(), utf8(mess.getBody()))
m = utf8(mess.getBody())
if m[-1] != '\n':
m+='\n'
self.msg(mess.getTo().node, m)
else:
logging.debug("Zapped message because the backend is not connected yet %s" % mess.getBody())

Expand All @@ -46,13 +49,13 @@ def lineReceived(self, line):
def irc_PRIVMSG(self, prefix, params):
fr, line = params
if fr == self.nickname: # it is a private message
fr = prefix.split('!')[0] # reextract the real from
fr = prefix.split('!')[1] # reextract the real from
typ = 'chat'
else:
typ = 'groupchat'
logging.debug('IRC message received from %s [%s]' % (fr, line))
msg = Message(line, typ=typ)
msg.setFrom(fr + '@' + prefix)
msg.setFrom(fr) # already a compatible format
self.callback.callback_message(self, msg)


Expand Down Expand Up @@ -107,7 +110,7 @@ def serve_forever(self):

def connect(self):
if not self.conn:
ircFactory = IRCFactory(self, self.jid)
ircFactory = IRCFactory(self, self.jid.split('@')[0])
self.conn = ircFactory.irc
reactor.connectTCP(self.server, self.port, ircFactory)

Expand Down
2 changes: 1 addition & 1 deletion errbot/builtins/chatRoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def callback_message(self, conn, mess):
self.send(room, body, message_type='groupchat')
elif mess_type == 'groupchat':
fr = mess.getFrom()
chat_room = fr.node + '@' + fr.domain
chat_room = fr.node + '@' + fr.domain if fr.domain else fr.node # some backends has no domain notion
if chat_room in REVERSE_CHATROOM_RELAY:
users_to_relay_to = REVERSE_CHATROOM_RELAY[chat_room]
logging.debug('Message to relay to %s.' % users_to_relay_to)
Expand Down

0 comments on commit e2325d3

Please sign in to comment.