#!/usr/bin/python # gajim-export-logs -- Export gajim sqlite logs to plain text files. # Copyright (C) 2006 Gordon Messmer # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """Use: gajim-export-logs This script will export the logs contained in gajim's configured default location. """ import locale import os import sys import time sys.path.append('/usr/share/gajim/src/common/') import logger def openLogFile(jid, unixTime): # Build the directory and log file paths try: contactLogDir = '%s/export/%s' % (os.getenv('HOME'), jid) contactLogFile = '%s/%s' % (contactLogDir, time.strftime('%Y-%m-%d.%H%M%S-%Z', time.localtime(unixTime))) contactLogDir = contactLogDir.decode(sys.getfilesystemencoding()) contactLogFile = contactLogFile.decode(sys.getfilesystemencoding()) except: pass # Create the directory structure which will contain the log file try: os.makedirs(contactLogDir) except os.error: pass # Open the new log file try: logFile = open(contactLogFile, 'w') except: print "Couldn't open log file:", contactLogFile sys.exit(1) # Write a gaim-style header in the new log file. logFile.write('Conversation with %s at %s\n' % (jid, time.asctime(time.localtime(unixTime)))) return logFile logs = logger.Logger() for x in logs.jids_already_in: jid = x.lower() jid_id = logs.get_jid_id(jid) messages = logs.get_search_results_for_query(x, '') # messages: contact_name, time, kind, show, message, subject msgDate = () logFile = None for m in messages: newMsgDate = time.localtime(m[1])[0-2] if newMsgDate != msgDate: logFile = openLogFile(x, m[1]) msgDate = newMsgDate # Get message time msgTime = time.strftime('%H:%M:%S', time.localtime(m[1])) # Get message sender if m[2] in (logger.constants.KIND_SINGLE_MSG_RECV, logger.constants.KIND_CHAT_MSG_RECV): who = jid elif m[2] in (logger.constants.KIND_SINGLE_MSG_SENT, logger.constants.KIND_CHAT_MSG_SENT): who = 'You' elif m[2] == logger.constants.KIND_GC_MSG: who = m[0] # Put together log line msg = '(%s) %s: %s\n' % (msgTime, who, m[4]) logFile.write(msg.encode('utf-8'))