# -*- coding: utf-8 -*- # This file is part of emesene. # # Emesene 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. # # emesene 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 emesene; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA from os.path import exists, join import time HTML_HEADER = ''' emesene - %s ''' class Log( object ): '''this class represent a log instance (a log of a conversation)''' def __init__( self, config, members ): '''Constructor''' self.log = [] self.config = config self.path = self.config.getLogPath() self.logFormats = int(self.config.getUserValue( 'logFormats', 0 )) self.members = ', '.join(members) if len(members) > 1: self.user = _( 'Group chat' ) else: self.user = members[0] date = time.strftime('%d%b%y') baseFilename = join(self.path, '%s %s.' % (self.user, date)) num = 1 while exists(baseFilename + str(num) + '.txt') or exists(baseFilename + str(num) + '.htm'): num += 1 self.filename = baseFilename + str(num) def add( self, type, nick, text, timestamp, html ): '''add text to the log''' self.log.append({'type':type,'nick':nick,'time':timestamp,'content':text,'html':html,}) if len(self.log) > 5: self.autosave() def autosave( self ): if self.config.getUserValue( 'saveLogsAutomatically' ) == '1': self.save() def save( self ): '''save the file to the given path + filename return True on success''' if exists(self.filename + '.txt') or exists(self.filename + '.htm'): mode = 'a' else: mode = 'w' if self.logFormats & 1: # text try: fd = open( self.filename + '.txt', mode ) except Exception, e: print 'Error opening file:', e return False if mode == 'w': fd.write( self.members + '\n\n' ) for i in self.log: timestamp = time.strftime( '[%H:%M]', time.localtime(i['time']) ) nick = i['nick'].strip()[:10].ljust(10) message = '%s %s: %s\n' % ( timestamp, nick, i['content'] ) fd.write( message ) fd.close() if self.logFormats & 2: # html try: fd = open( self.filename + '.htm', mode ) except Exception, e: print 'Error opening file:', e return False # if it's starting the file, write a header if mode=='w': fd.write( HTML_HEADER % self.members ) for i in self.log: fd.write( '
' + i['html'] + '
\n' ) #i'm not worrying about 100% valid html this time.. #fd.write( '' ) fd.close() # remove the saved text to allow updates self.log = [] return True