summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof Klinikowski <kkszysiu@gmail.com>2010-03-22 18:52:12 +0100
committerKrzysztof Klinikowski <kkszysiu@gmail.com>2010-03-22 18:52:12 +0100
commit689574c6ebcfe49d9117966909c09afb9fbd936a (patch)
treedb15c26046563df90d640a45ef8c57ee913aa55a
parente0472f44211280287c6cf6bba7c0e204547cb1ae (diff)
Initial conferences support implemented.
-rw-r--r--sunshine/channel/text.py50
-rw-r--r--sunshine/channel_manager.py23
-rw-r--r--sunshine/connection.py123
-rw-r--r--sunshine/handle.py7
-rw-r--r--[-rwxr-xr-x]sunshine/lqsoft/__init__.py0
5 files changed, 171 insertions, 32 deletions
diff --git a/sunshine/channel/text.py b/sunshine/channel/text.py
index 05e8470..c72fe58 100644
--- a/sunshine/channel/text.py
+++ b/sunshine/channel/text.py
@@ -78,6 +78,56 @@ class SunshineTextChannel(telepathy.server.ChannelTypeText):
return telepathy.server.ChannelTypeText.ListPendingMessages(self, clear)
+class SunshineRoomTextChannel(telepathy.server.ChannelTypeText, telepathy.server.ChannelInterfaceGroup):
+
+ def __init__(self, conn, manager, conversation, props):
+ _, surpress_handler, handle = manager._get_type_requested_handle(props)
+ self._recv_id = 0
+ self._conn_ref = weakref.ref(conn)
+ self.conn = conn
+
+ self.handle = handle
+ telepathy.server.ChannelTypeText.__init__(self, conn, manager, props)
+ telepathy.server.ChannelInterfaceGroup.__init__(self)
+
+ self.GroupFlagsChanged(telepathy.CHANNEL_GROUP_FLAG_CAN_ADD, 0)
+
+ def Send(self, message_type, text):
+ if message_type == telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
+ logger.info("Sending message to %s, id %s, body: '%s'" % (str(self.handle.name), str(self.handle.id), unicode(text)))
+ msg = text.encode('windows-1250')
+ self.conn.gadu_client.sendTo(int(self.handle.name), str(text), str(msg))
+ else:
+ raise telepathy.NotImplemented("Unhandled message type")
+ self.Sent(int(time.time()), message_type, text)
+
+ def Close(self):
+ telepathy.server.ChannelTypeText.Close(self)
+ self.remove_from_connection()
+
+ # Redefine GetSelfHandle since we use our own handle
+ # as Butterfly doesn't have channel specific handles
+ def GetSelfHandle(self):
+ return self._conn.GetSelfHandle()
+
+ # Rededefine AcknowledgePendingMessages to remove offline messages
+ # from the oim box.
+ def AcknowledgePendingMessages(self, ids):
+ telepathy.server.ChannelTypeText.AcknowledgePendingMessages(self, ids)
+# messages = []
+# for id in ids:
+# if id in self._pending_offline_messages.keys():
+# messages.append(self._pending_offline_messages[id])
+# del self._pending_offline_messages[id]
+# self._oim_box_ref().delete_messages(messages)
+
+ # Rededefine ListPendingMessages to remove offline messages
+ # from the oim box.
+ def ListPendingMessages(self, clear):
+ return telepathy.server.ChannelTypeText.ListPendingMessages(self, clear)
+
+
+
# if clear:
# messages = self._pending_offline_messages.values()
# self._oim_box_ref().delete_messages(messages)
diff --git a/sunshine/channel_manager.py b/sunshine/channel_manager.py
index 8cff4b9..cd02197 100644
--- a/sunshine/channel_manager.py
+++ b/sunshine/channel_manager.py
@@ -25,7 +25,7 @@ import telepathy
from sunshine.channel.contact_list import SunshineContactListChannelFactory
from sunshine.channel.group import SunshineGroupChannel
-from sunshine.channel.text import SunshineTextChannel
+from sunshine.channel.text import SunshineTextChannel, SunshineRoomTextChannel
#from sunshine.channel.media import SunshineMediaChannel
from sunshine.handle import SunshineHandleFactory
@@ -42,6 +42,11 @@ class SunshineChannelManager(telepathy.server.ChannelManager):
self._implement_channel_class(telepathy.CHANNEL_TYPE_TEXT,
self._get_text_channel, fixed, [])
+ fixed = {telepathy.CHANNEL_INTERFACE + '.ChannelType': telepathy.CHANNEL_TYPE_TEXT,
+ telepathy.CHANNEL_INTERFACE + '.TargetHandleType': dbus.UInt32(telepathy.HANDLE_TYPE_ROOM)}
+ self._implement_channel_class(telepathy.CHANNEL_TYPE_TEXT,
+ self._get_text_channel, fixed, [])
+
fixed = {telepathy.CHANNEL_INTERFACE + '.ChannelType': telepathy.CHANNEL_TYPE_CONTACT_LIST}
self._implement_channel_class(telepathy.CHANNEL_TYPE_CONTACT_LIST,
self._get_list_channel, fixed, [])
@@ -66,13 +71,19 @@ class SunshineChannelManager(telepathy.server.ChannelManager):
def _get_text_channel(self, props, conversation=None):
_, surpress_handler, handle = self._get_type_requested_handle(props)
- if handle.get_type() != telepathy.HANDLE_TYPE_CONTACT:
- raise telepathy.NotImplemented('Only contacts are allowed')
+ if handle.get_type() == telepathy.HANDLE_TYPE_CONTACT:
+ logger.debug('New text channel for contact handle, name: %s, id: %s, type: %s' % (handle.name, handle.id, handle.type))
- logger.debug('New text channel for handle, name: %s, id: %s, type: %s' % (handle.name, handle.id, handle.type))
+ channel = SunshineTextChannel(self._conn, self, conversation, props)
+ return channel
+ elif handle.get_type() == telepathy.HANDLE_TYPE_ROOM:
+ logger.debug('New text channel for room handle, name: %s, id: %s, type: %s' % (handle.name, handle.id, handle.type))
+
+ channel = SunshineRoomTextChannel(self._conn, self, conversation, props)
+ return channel
+ else:
+ raise telepathy.NotImplemented('Unknown handle for text channel.')
- channel = SunshineTextChannel(self._conn, self, conversation, props)
- return channel
# def _get_media_channel(self, props, call=None):
# _, surpress_handler, handle = self._get_type_requested_handle(props)
diff --git a/sunshine/connection.py b/sunshine/connection.py
index 9cf81aa..9b9b7ba 100644
--- a/sunshine/connection.py
+++ b/sunshine/connection.py
@@ -256,6 +256,7 @@ class SunshineConnection(telepathy.server.Connection,
self._channel_manager = SunshineChannelManager(self)
self._recv_id = 0
+ self._conf_id = 0
self.pending_contacts_to_group = {}
self._status = None
self.profile.contactsLoop = None
@@ -309,7 +310,7 @@ class SunshineConnection(telepathy.server.Connection,
"""
handle_id = 0
for handle in self._handles.values():
- if handle.get_name() == name:
+ if handle.get_name() == name and handle.type == handle_type:
handle_id = handle.get_id()
break
@@ -362,7 +363,8 @@ class SunshineConnection(telepathy.server.Connection,
else:
handle = SunshineHandleFactory(self, 'contact',
str(contact_name), None)
-
+ elif handle_type == telepathy.HANDLE_TYPE_ROOM:
+ handle = SunshineHandleFactory(self, 'room', name)
elif handle_type == telepathy.HANDLE_TYPE_LIST:
handle = SunshineHandleFactory(self, 'list', name)
elif handle_type == telepathy.HANDLE_TYPE_GROUP:
@@ -519,30 +521,99 @@ class SunshineConnection(telepathy.server.Connection,
logger.info("Method on_updateContact called, status changed for UIN: %s, id: %s, status: %s, description: %s" % (contact.uin, handle.id, contact.status, contact.get_desc()))
self._presence_changed(handle, contact.status, contact.get_desc())
- @async
+ #@async
def on_messageReceived(self, msg):
- handle_id = self.get_handle_id_by_name(telepathy.constants.HANDLE_TYPE_CONTACT,
- str(msg.sender))
- if handle_id != 0:
- handle = self.handle(telepathy.constants.HANDLE_TYPE_CONTACT, handle_id)
- else:
- handle = SunshineHandleFactory(self, 'contact',
- str(msg.sender), None)
+ if hasattr(msg.content.attrs, 'conference') and msg.content.attrs.conference != None:
+ recipients = msg.content.attrs.conference.recipients
+ #recipients.append(self.profile.uin)
+ print msg.sender
+ print 'recipients:', recipients
+ recipients = map(str, recipients)
+ recipients.append(str(msg.sender))
+ print 'recipients:', recipients
+ recipients = sorted(recipients)
+ conf_name = ', '.join(map(str, recipients))
+ print 'conf_name:', conf_name
+
+ #active handle for current writting contact
+ ahandle_id = self.get_handle_id_by_name(telepathy.constants.HANDLE_TYPE_CONTACT,
+ str(msg.sender))
+
+ if ahandle_id != 0:
+ ahandle = self.handle(telepathy.constants.HANDLE_TYPE_CONTACT, ahandle_id)
+ else:
+ ahandle = SunshineHandleFactory(self, 'contact',
+ str(msg.sender), None)
+
+ #now we need to preapare a new room and make initial users in it
+ room_handle_id = self.get_handle_id_by_name(telepathy.constants.HANDLE_TYPE_ROOM, str(conf_name))
+ print 'room_handle_id:', room_handle_id
+
+ handles = []
- if int(msg.content.klass) == 9:
- timestamp = int(msg.time)
+ if room_handle_id == 0:
+ room_handle = SunshineHandleFactory(self, 'room', str(conf_name))
+
+ for number in recipients:
+ handle_id = self.get_handle_id_by_name(telepathy.constants.HANDLE_TYPE_CONTACT,
+ number)
+ if handle_id != 0:
+ handle = self.handle(telepathy.constants.HANDLE_TYPE_CONTACT, handle_id)
+ else:
+ handle = SunshineHandleFactory(self, 'contact',
+ number, None)
+
+ handles.append(handle)
+ else:
+ room_handle = self.handle(telepathy.constants.HANDLE_TYPE_ROOM, room_handle_id)
+
+ props = self._generate_props(telepathy.CHANNEL_TYPE_TEXT,
+ room_handle, False)
+ channel = self._channel_manager.channel_for_props(props,
+ signal=True, conversation=None)
+
+ if handles:
+ print handles
+ channel.MembersChanged('', handles, [], [], [],
+ 0, telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE)
+
+ if int(msg.content.klass) == 9:
+ timestamp = int(msg.time)
+ else:
+ timestamp = int(time.time())
+ type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
+ logger.info("User %s sent a message" % ahandle.name)
+
+ logger.info("Msg from %r %d %d [%r] [%r]" % (msg.sender, msg.content.offset_plain, msg.content.offset_attrs, msg.content.plain_message, msg.content.html_message))
+
+ message = "%s" % unicode(str(msg.content.plain_message).replace('\x00', '').replace('\r', '').decode('windows-1250').encode('utf-8'))
+ #print 'message: ', message
+ channel.Received(self._recv_id, timestamp, ahandle, type, 0, message)
+ self._recv_id += 1
+
else:
- timestamp = int(time.time())
- type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
- logger.info("User %s sent a message" % handle.name)
-
- logger.info("Msg from %r %d %d [%r] [%r]" % (msg.sender, msg.content.offset_plain, msg.content.offset_attrs, msg.content.plain_message, msg.content.html_message))
-
- props = self._generate_props(telepathy.CHANNEL_TYPE_TEXT,
- handle, False)
- channel = self._channel_manager.channel_for_props(props,
- signal=True, conversation=None)
- message = "%s" % unicode(str(msg.content.plain_message).replace('\x00', '').replace('\r', '').decode('windows-1250').encode('utf-8'))
- #print 'message: ', message
- channel.Received(self._recv_id, timestamp, handle, type, 0, message)
- self._recv_id += 1
+ handle_id = self.get_handle_id_by_name(telepathy.constants.HANDLE_TYPE_CONTACT,
+ str(msg.sender))
+ if handle_id != 0:
+ handle = self.handle(telepathy.constants.HANDLE_TYPE_CONTACT, handle_id)
+ else:
+ handle = SunshineHandleFactory(self, 'contact',
+ str(msg.sender), None)
+
+ if int(msg.content.klass) == 9:
+ timestamp = int(msg.time)
+ else:
+ timestamp = int(time.time())
+ type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
+ logger.info("User %s sent a message" % handle.name)
+
+ logger.info("Msg from %r %d %d [%r] [%r]" % (msg.sender, msg.content.offset_plain, msg.content.offset_attrs, msg.content.plain_message, msg.content.html_message))
+
+ props = self._generate_props(telepathy.CHANNEL_TYPE_TEXT,
+ handle, False)
+ channel = self._channel_manager.channel_for_props(props,
+ signal=True, conversation=None)
+ message = "%s" % unicode(str(msg.content.plain_message).replace('\x00', '').replace('\r', '').decode('windows-1250').encode('utf-8'))
+ #print 'message: ', message
+ channel.Received(self._recv_id, timestamp, handle, type, 0, message)
+ self._recv_id += 1
diff --git a/sunshine/handle.py b/sunshine/handle.py
index 4b965ba..140d2e5 100644
--- a/sunshine/handle.py
+++ b/sunshine/handle.py
@@ -30,6 +30,7 @@ logger = logging.getLogger('Sunshine.Handle')
def SunshineHandleFactory(connection, type, *args):
mapping = {'self': SunshineSelfHandle,
'contact': SunshineContactHandle,
+ 'room': SunshineRoomHandle,
'list': SunshineListHandle,
'group': SunshineGroupHandle}
handle = mapping[type](connection, *args)
@@ -109,6 +110,12 @@ class SunshineContactHandle(SunshineHandle):
result = self._connection.gadu_client.get_contact(int(self.account))
return result
+class SunshineRoomHandle(SunshineHandle):
+ def __init__(self, connection, id, name):
+ handle_type = telepathy.HANDLE_TYPE_ROOM
+ handle_name = str(name)
+ self._connection = connection
+ SunshineHandle.__init__(self, connection, id, handle_type, handle_name)
class SunshineListHandle(SunshineHandle):
def __init__(self, connection, id, list_name):
diff --git a/sunshine/lqsoft/__init__.py b/sunshine/lqsoft/__init__.py
index e69de29..e69de29 100755..100644
--- a/sunshine/lqsoft/__init__.py
+++ b/sunshine/lqsoft/__init__.py