changeset d875ab6b8a95 in /home/hg/repos/gajim

details:http://hg.gajim.org/gajim?cmd=changeset;node=d875ab6b8a95
description: write code to synchronise server logs from archiving with local DB

diffstat:

 src/common/connection_handlers.py |  69 ++++++++++++++++++++++++++++++++++
 src/common/logger.py              |  30 +++++++++++++++
 src/common/message_archiving.py   |  20 +++++++--
 src/gui_interface.py              |   1 +
 4 files changed, 115 insertions(+), 5 deletions(-)

diffs (187 lines):

diff -r 42d118f04b78 -r d875ab6b8a95 src/common/connection_handlers.py
--- a/src/common/connection_handlers.py Fri Nov 13 19:45:11 2009 +0100
+++ b/src/common/connection_handlers.py Sun Nov 15 11:33:05 2009 +0100
@@ -53,6 +53,9 @@
 from common.pubsub import ConnectionPubSub
 from common.caps import ConnectionCaps
 from common.message_archiving import ConnectionArchive
+from common.message_archiving import ARCHIVING_COLLECTIONS_ARRIVED
+from common.message_archiving import ARCHIVING_COLLECTION_ARRIVED
+from common.message_archiving import ARCHIVING_MODIFICATIONS_ARRIVED
 
 if gajim.HAVE_FARSIGHT:
        from common.jingle import ConnectionJingle
@@ -1235,6 +1238,72 @@
                                form = 
common.dataforms.ExtendForm(node=form_tag)
                                self.dispatch('PEP_CONFIG', (node, form))
 
+               elif self.awaiting_answers[id_][0] == 
ARCHIVING_COLLECTIONS_ARRIVED:
+                       # TODO
+                       pass
+
+               elif self.awaiting_answers[id_][0] == 
ARCHIVING_COLLECTION_ARRIVED:
+                       def save_if_not_exists(with_, direction, tim, payload):
+                               assert len(payload) == 1, 'got several 
archiving messages in the' +\
+                                       ' same time %s' % ''.join(payload)
+                               if payload[0].getName() == 'body':
+                                       gajim.logger.save_if_not_exists(with_, 
direction, tim,
+                                               msg=payload[0].getData())
+                               elif payload[0].getName() == 'message':
+                                       print 'Not implemented'
+                       chat = iq_obj.getTag('chat')
+                       if chat:
+                               with_ = chat.getAttr('with')
+                               start_ = chat.getAttr('start')
+                               tim = helpers.datetime_tuple(start_)
+                               tim = timegm(tim)
+                               nb = 0
+                               for element in chat.getChildren():
+                                       try:
+                                               secs = 
int(element.getAttr('secs'))
+                                       except TypeError:
+                                               secs = 0
+                                       if secs:
+                                               tim += secs
+                                       if element.getName() == 'from':
+                                               save_if_not_exists(with_, 
'from', localtime(tim),
+                                                       element.getPayload())
+                                               nb += 1
+                                       if element.getName() == 'to':
+                                               save_if_not_exists(with_, 'to', 
localtime(tim),
+                                                       element.getPayload())
+                                               nb += 1
+                               set_ = chat.getTag('set')
+                               first = set_.getTag('first')
+                               if first:
+                                       try:
+                                               index = 
int(first.getAttr('index'))
+                                       except TypeError:
+                                               index = 0
+                               try:
+                                       count = int(set_.getTagData('count'))
+                               except TypeError:
+                                       count = 0
+                               if count > index + nb:
+                                       # Request the next page
+                                       try:
+                                               after = 
int(element.getTagData('last'))
+                                       except TypeError:
+                                               after = index + nb
+                                       self.request_collection_page(with_, 
start_, after=after)
+
+               elif self.awaiting_answers[id_][0] == 
ARCHIVING_MODIFICATIONS_ARRIVED:
+                       modified = iq_obj.getTag('modified')
+                       if modified:
+                               for element in modified.getChildren():
+                                       if element.getName() == 'changed':
+                                               with_ = element.getAttr('with')
+                                               start_ = 
element.getAttr('start')
+                                               
self.request_collection_page(with_, start_)
+                                       elif element.getName() == 'removed':
+                                               # do nothing
+                                               pass
+
                del self.awaiting_answers[id_]
 
        def _vCardCB(self, con, vc):
diff -r 42d118f04b78 -r d875ab6b8a95 src/common/logger.py
--- a/src/common/logger.py      Fri Nov 13 19:45:11 2009 +0100
+++ b/src/common/logger.py      Sun Nov 15 11:33:05 2009 +0100
@@ -981,4 +981,34 @@
                        (account_jid_id,))
                self.con.commit()
 
+       def save_if_not_exists(self, with_, direction, tim, msg=''):
+               if tim:
+                       time_col = int(float(time.mktime(tim)))
+               else:
+                       time_col = int(float(time.time()))
+               if msg:
+                       if self.jid_is_from_pm(with_):
+                               # We cannot know if it's a pm or groupchat 
message because we only
+                               # get body of the message
+                               type_ = 'gc_msg'
+                       else:
+                               if direction == 'from':
+                                       type_ = 'chat_msg_recv'
+                               elif direction == 'to':
+                                       type_ = 'chat_msg_sent'
+               jid_id = self.get_jid_id(with_)
+               where_sql = 'jid_id = %s AND message="%s"' % (jid_id, msg)
+               start_time = time_col - 300 # 5 minutes arrount given time
+               end_time = time_col + 300 # 5 minutes arrount given time
+               self.cur.execute('''
+                       SELECT log_line_id FROM logs
+                       WHERE (%s)
+                       AND time BETWEEN %d AND %d
+                       ORDER BY time
+                       ''' % (where_sql, start_time, end_time))
+               results = self.cur.fetchall()
+               if results:
+                       return
+               self.write(type_, with_, message=msg, tim=tim)
+
 # vim: se ts=3:
diff -r 42d118f04b78 -r d875ab6b8a95 src/common/message_archiving.py
--- a/src/common/message_archiving.py   Fri Nov 13 19:45:11 2009 +0100
+++ b/src/common/message_archiving.py   Sun Nov 15 11:33:05 2009 +0100
@@ -20,6 +20,9 @@
 
 import common.xmpp
 
+ARCHIVING_COLLECTIONS_ARRIVED = 'archiving_collections_arrived'
+ARCHIVING_COLLECTION_ARRIVED = 'archiving_collection_arrived'
+ARCHIVING_MODIFICATIONS_ARRIVED = 'archiving_modifications_arrived'
 
 class ConnectionArchive:
        def __init__(self):
@@ -192,6 +195,9 @@
                set_.setTagData('max', max)
                if after:
                        set_.setTagData('after', after)
+               id_ = self.connection.getAnID()
+               iq_.setID(id_)
+               self.awaiting_answers[id_] = (ARCHIVING_COLLECTIONS_ARRIVED, )
                self.connection.send(iq_)
 
        def request_collection_page(self, with, start, end=None, after=None,
@@ -205,6 +211,9 @@
                set_.setTagData('max', max)
                if after:
                        set_.setTagData('after', after)
+               id_ = self.connection.getAnID()
+               iq_.setID(id_)
+               self.awaiting_answers[id_] = (ARCHIVING_COLLECTION_ARRIVED, )
                self.connection.send(iq_)
                
        def remove_collection(self, with='', start=None, end=None,
@@ -223,12 +232,13 @@
                        remove.setAttr('open', 'true')
                self.connection.send(iq_)
        
-       def request_modifications_page(self, start, version, after=None, 
max=30):
+       def request_modifications_page(self, start, max=30):
                iq_ = common.xmpp.Iq('get')
                moified = iq_.setTag('modified', 
namespace=common.xmpp.NS_ARCHIVE,
-                       attrs={'start': start, 'version': version})
-               set_ = retrieve.setTag('set', namespace=common.xmpp.NS_RSM)
+                       attrs={'start': start})
+               set_ = moified.setTag('set', namespace=common.xmpp.NS_RSM)
                set_.setTagData('max', max)
-               if after:
-                       set_.setTagData('after', after)
+               id_ = self.connection.getAnID()
+               iq_.setID(id_)
+               self.awaiting_answers[id_] = (ARCHIVING_MODIFICATIONS_ARRIVED, )
                self.connection.send(iq_)
diff -r 42d118f04b78 -r d875ab6b8a95 src/gui_interface.py
--- a/src/gui_interface.py      Fri Nov 13 19:45:11 2009 +0100
+++ b/src/gui_interface.py      Sun Nov 15 11:33:05 2009 +0100
@@ -3405,6 +3405,7 @@
                self.last_ftwindow_update = 0
 
                self.music_track_changed_signal = None
+               self.create_ipython_window()
                
                
 class PassphraseRequest:
_______________________________________________
Commits mailing list
[email protected]
http://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to