write code to synchronise server logs from archiving with local DB
This commit is contained in:
		
							parent
							
								
									e4128f5c29
								
							
						
					
					
						commit
						72420b0e73
					
				
					 4 changed files with 115 additions and 5 deletions
				
			
		| 
						 | 
				
			
			@ -53,6 +53,9 @@ from common.commands import ConnectionCommands
 | 
			
		|||
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 @@ class ConnectionVcard:
 | 
			
		|||
				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):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -981,4 +981,34 @@ class Logger:
 | 
			
		|||
			(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:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 @@ class ConnectionArchive:
 | 
			
		|||
		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 @@ class ConnectionArchive:
 | 
			
		|||
		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 @@ class ConnectionArchive:
 | 
			
		|||
			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_)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3405,6 +3405,7 @@ class Interface:
 | 
			
		|||
		self.last_ftwindow_update = 0
 | 
			
		||||
 | 
			
		||||
		self.music_track_changed_signal = None
 | 
			
		||||
		self.create_ipython_window()
 | 
			
		||||
		
 | 
			
		||||
		
 | 
			
		||||
class PassphraseRequest:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue