#!/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/') # taken from gajim/src/gajim.py # prepare to import common.gajim profile = unicode('', locale.getpreferredencoding()) import common.configpaths common.configpaths.gajimpaths.init(None) common.configpaths.gajimpaths.init_profile(profile) del profile import common.i18n import common.gajim import common.logger GAIM_LOG_DIR = os.path.expanduser("~/.gaim/logs/") USER_NAME = "gajim" def openLogFile(jid, unixTime): # Build the directory and log file paths try: contactLogDir = '%s/gajim/%s/%s' % (GAIM_LOG_DIR, USER_NAME, jid) contactLogFile = '%s/%s.txt' % (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 = common.logger.Logger() for x in logs.jids_already_in: jid = x.lower() jidId = logs.get_jid_id(jid) cmd = "SELECT message, time, kind, contact_name FROM logs WHERE jid_id = %s ORDER BY time" % (jidId,) logs.cur.execute(cmd) messages = logs.cur.fetchall() if not messages: continue msgDate = () logFile = None for m in messages: dbMsg = m[0] dbTime = m[1] dbKind = m[2] dbContactName = m[3] newMsgDate = time.localtime(dbTime)[0-2] if newMsgDate != msgDate: logFile = openLogFile(x, dbTime) msgDate = newMsgDate # Get message time msgTime = time.strftime('%H:%M:%S', time.localtime(dbTime)) # Get message sender if dbContactName: who = dbContactName elif dbKind in (common.logger.constants.KIND_SINGLE_MSG_RECV, common.logger.constants.KIND_CHAT_MSG_RECV, common.logger.constants.KIND_GC_MSG): who = jid elif dbKind in (common.logger.constants.KIND_SINGLE_MSG_SENT, common.logger.constants.KIND_CHAT_MSG_SENT): who = 'You' # Put together log line msg = '(%s) %s: %s\n' % (msgTime, who, dbMsg) logFile.write(msg.encode('utf-8'))