2005-05-12 15:43:17 +02:00
|
|
|
## logger.py
|
2003-11-30 17:02:00 +01:00
|
|
|
##
|
|
|
|
## Gajim Team:
|
2005-05-12 15:43:17 +02:00
|
|
|
## - Yann Le Boulanger <asterix@lagaule.org>
|
|
|
|
## - Vincent Hanquez <tab@snarc.org>
|
2005-05-20 02:05:18 +02:00
|
|
|
## - Nikos Kouremenos <kourem@gmail.com>
|
2003-11-30 17:02:00 +01:00
|
|
|
##
|
2005-01-07 22:52:38 +01:00
|
|
|
## Copyright (C) 2003-2005 Gajim Team
|
2003-11-30 17:02:00 +01:00
|
|
|
##
|
|
|
|
## 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; version 2 only.
|
|
|
|
##
|
|
|
|
## 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.
|
|
|
|
##
|
|
|
|
|
|
|
|
import os
|
|
|
|
import time
|
2005-08-25 18:50:02 +02:00
|
|
|
|
2005-04-16 00:02:13 +02:00
|
|
|
import common.gajim
|
2004-05-17 01:47:14 +02:00
|
|
|
from common import i18n
|
|
|
|
_ = i18n._
|
2005-09-11 15:41:21 +02:00
|
|
|
import helpers
|
2003-11-30 17:02:00 +01:00
|
|
|
|
2005-09-02 23:45:35 +02:00
|
|
|
|
2005-04-16 00:02:13 +02:00
|
|
|
class Logger:
|
|
|
|
def __init__(self):
|
2005-10-30 12:20:24 +01:00
|
|
|
pass
|
2005-07-02 01:13:46 +02:00
|
|
|
|
2005-04-16 00:02:13 +02:00
|
|
|
def write(self, kind, msg, jid, show = None, tim = None):
|
2005-11-03 09:50:35 +01:00
|
|
|
jid = jid.lower()
|
2005-04-16 00:02:13 +02:00
|
|
|
if not tim:
|
|
|
|
tim = time.time()
|
|
|
|
else:
|
|
|
|
tim = time.mktime(tim)
|
|
|
|
|
|
|
|
if not msg:
|
|
|
|
msg = ''
|
2005-07-21 23:39:47 +02:00
|
|
|
|
2005-07-02 14:36:21 +02:00
|
|
|
msg = helpers.to_one_line(msg)
|
2005-06-08 11:19:54 +02:00
|
|
|
if len(jid.split('/')) > 1:
|
|
|
|
ji, nick = jid.split('/', 1)
|
|
|
|
else:
|
|
|
|
ji = jid
|
|
|
|
nick = ''
|
2005-04-16 00:02:13 +02:00
|
|
|
files = []
|
2005-08-16 13:35:14 +02:00
|
|
|
if kind == 'status': # we save time:jid:show:msg
|
2005-04-16 00:02:13 +02:00
|
|
|
if not show:
|
|
|
|
show = 'online'
|
2005-05-12 19:46:17 +02:00
|
|
|
if common.gajim.config.get('log_notif_in_user_file'):
|
2005-10-30 12:20:24 +01:00
|
|
|
path_to_file = os.path.join(common.gajim.LOGPATH, ji)
|
2005-06-08 11:19:54 +02:00
|
|
|
if os.path.isdir(path_to_file):
|
2005-06-13 16:46:08 +02:00
|
|
|
jid = 'gcstatus'
|
|
|
|
msg = show + ':' + msg
|
|
|
|
show = nick
|
2005-06-08 11:19:54 +02:00
|
|
|
files.append(ji + '/' + ji)
|
|
|
|
if os.path.isfile(jid):
|
|
|
|
files.append(jid)
|
|
|
|
else:
|
|
|
|
files.append(ji)
|
2005-05-12 19:46:17 +02:00
|
|
|
if common.gajim.config.get('log_notif_in_sep_file'):
|
2005-04-16 00:02:13 +02:00
|
|
|
files.append('notify.log')
|
2005-04-16 19:03:21 +02:00
|
|
|
elif kind == 'incoming': # we save time:recv:message
|
2005-10-30 12:20:24 +01:00
|
|
|
path_to_file = os.path.join(common.gajim.LOGPATH, ji)
|
2005-06-08 11:19:54 +02:00
|
|
|
if os.path.isdir(path_to_file):
|
|
|
|
files.append(jid)
|
|
|
|
else:
|
|
|
|
files.append(ji)
|
2005-04-16 00:02:13 +02:00
|
|
|
jid = 'recv'
|
|
|
|
show = msg
|
|
|
|
msg = ''
|
2005-04-16 19:03:21 +02:00
|
|
|
elif kind == 'outgoing': # we save time:sent:message
|
2005-10-30 12:20:24 +01:00
|
|
|
path_to_file = os.path.join(common.gajim.LOGPATH, ji)
|
2005-06-08 11:19:54 +02:00
|
|
|
if os.path.isdir(path_to_file):
|
|
|
|
files.append(jid)
|
|
|
|
else:
|
|
|
|
files.append(ji)
|
2005-04-16 00:02:13 +02:00
|
|
|
jid = 'sent'
|
|
|
|
show = msg
|
|
|
|
msg = ''
|
2005-06-08 11:19:54 +02:00
|
|
|
elif kind == 'gc': # we save time:gc:nick:message
|
|
|
|
# create the folder if needed
|
2005-10-30 12:20:24 +01:00
|
|
|
ji_fn = os.path.join(common.gajim.LOGPATH, ji)
|
2005-06-08 11:19:54 +02:00
|
|
|
if os.path.isfile(ji_fn):
|
|
|
|
os.remove(ji_fn)
|
|
|
|
if not os.path.isdir(ji_fn):
|
2005-08-25 19:33:04 +02:00
|
|
|
os.mkdir(ji_fn, 0700)
|
2005-06-08 11:19:54 +02:00
|
|
|
files.append(ji + '/' + ji)
|
|
|
|
jid = 'gc'
|
2005-04-16 00:02:13 +02:00
|
|
|
show = nick
|
2005-08-26 14:34:11 +02:00
|
|
|
# convert to utf8 before writing to file if needed
|
|
|
|
if isinstance(tim, unicode):
|
2005-08-26 02:52:44 +02:00
|
|
|
tim = tim.encode('utf-8')
|
2005-08-26 14:34:11 +02:00
|
|
|
if isinstance(jid, unicode):
|
2005-08-26 02:52:44 +02:00
|
|
|
jid = jid.encode('utf-8')
|
2005-08-26 14:34:11 +02:00
|
|
|
if isinstance(show, unicode):
|
2005-08-26 02:52:44 +02:00
|
|
|
show = show.encode('utf-8')
|
2005-08-26 14:34:11 +02:00
|
|
|
if msg and isinstance(msg, unicode):
|
2005-08-26 02:52:44 +02:00
|
|
|
msg = msg.encode('utf-8')
|
2005-04-16 00:02:13 +02:00
|
|
|
for f in files:
|
2005-10-30 12:20:24 +01:00
|
|
|
path_to_file = os.path.join(common.gajim.LOGPATH, f)
|
2005-08-19 12:32:42 +02:00
|
|
|
if os.path.isdir(path_to_file):
|
|
|
|
return
|
2005-08-25 20:33:40 +02:00
|
|
|
# this does it rw-r-r by default but is in a dir with 700 so it's ok
|
|
|
|
fil = open(path_to_file, 'a')
|
2005-07-24 21:04:29 +02:00
|
|
|
fil.write('%s:%s:%s' % (tim, jid, show))
|
2005-04-16 11:36:18 +02:00
|
|
|
if msg:
|
2005-07-24 21:04:29 +02:00
|
|
|
fil.write(':' + msg)
|
|
|
|
fil.write('\n')
|
|
|
|
fil.close()
|
2005-04-16 00:02:13 +02:00
|
|
|
|
2005-06-13 16:46:08 +02:00
|
|
|
def __get_path_to_file(self, fjid):
|
|
|
|
jid = fjid.split('/')[0]
|
2005-10-30 12:20:24 +01:00
|
|
|
path_to_file = os.path.join(common.gajim.LOGPATH, jid)
|
2005-06-13 16:46:08 +02:00
|
|
|
if os.path.isdir(path_to_file):
|
|
|
|
if fjid == jid: # we want to read the gc history
|
2005-10-30 12:20:24 +01:00
|
|
|
path_to_file = os.path.join(common.gajim.LOGPATH, jid + '/' + jid)
|
2005-06-13 16:46:08 +02:00
|
|
|
else: #we want to read pm history
|
2005-10-30 12:20:24 +01:00
|
|
|
path_to_file = os.path.join(common.gajim.LOGPATH, fjid)
|
2005-06-13 16:46:08 +02:00
|
|
|
return path_to_file
|
|
|
|
|
2005-07-24 21:04:29 +02:00
|
|
|
def get_no_of_lines(self, fjid):
|
2005-11-03 22:31:04 +01:00
|
|
|
'''returns total number of lines in a log file
|
|
|
|
returns 0 if log file does not exist'''
|
2005-11-03 09:50:35 +01:00
|
|
|
fjid = fjid.lower()
|
2005-06-13 16:46:08 +02:00
|
|
|
path_to_file = self.__get_path_to_file(fjid)
|
2005-08-14 23:46:57 +02:00
|
|
|
if not os.path.isfile(path_to_file):
|
2005-05-30 23:00:04 +02:00
|
|
|
return 0
|
2005-11-20 22:58:08 +01:00
|
|
|
f = open(path_to_file, 'r')
|
|
|
|
return len(f.readlines()) # number of lines
|
2005-04-16 00:02:13 +02:00
|
|
|
|
2005-11-20 22:58:08 +01:00
|
|
|
# FIXME: remove me when refactor in TC is done
|
|
|
|
def read_from_line_to_line(self, fjid, begin_from_line, end_line):
|
|
|
|
'''returns the text in the lines (list),
|
|
|
|
returns empty list if log file does not exist'''
|
2005-11-03 09:50:35 +01:00
|
|
|
fjid = fjid.lower()
|
2005-06-13 16:46:08 +02:00
|
|
|
path_to_file = self.__get_path_to_file(fjid)
|
2005-08-14 23:46:57 +02:00
|
|
|
if not os.path.isfile(path_to_file):
|
2005-11-20 22:58:08 +01:00
|
|
|
return []
|
|
|
|
|
2005-04-16 00:02:13 +02:00
|
|
|
lines = []
|
2005-11-20 22:58:08 +01:00
|
|
|
|
|
|
|
fil = open(path_to_file, 'r')
|
|
|
|
#fil.readlines(begin_from_line) # skip the previous lines
|
|
|
|
no_of_lines = begin_from_line # number of lines between being and end
|
|
|
|
while (no_of_lines < begin_from_line and fil.readline()):
|
2005-07-24 21:04:29 +02:00
|
|
|
no_of_lines += 1
|
2005-11-20 22:58:08 +01:00
|
|
|
|
|
|
|
print begin_from_line, end_line
|
2005-07-24 21:04:29 +02:00
|
|
|
while no_of_lines < end_line:
|
2005-08-26 02:52:44 +02:00
|
|
|
line = fil.readline().decode('utf-8')
|
2005-11-20 22:58:08 +01:00
|
|
|
print `line`, '@', no_of_lines
|
2005-04-16 00:02:13 +02:00
|
|
|
if line:
|
2005-07-02 14:36:21 +02:00
|
|
|
line = helpers.from_one_line(line)
|
2005-04-16 00:02:13 +02:00
|
|
|
lineSplited = line.split(':')
|
|
|
|
if len(lineSplited) > 2:
|
|
|
|
lines.append(lineSplited)
|
2005-11-20 22:58:08 +01:00
|
|
|
no_of_lines += 1
|
|
|
|
else: # emplty line (we are at the end of file)
|
|
|
|
break
|
|
|
|
return lines
|
|
|
|
|
|
|
|
def get_last_conversation_lines(self, jid, how_many_lines, timeout):
|
|
|
|
'''accepts how many lines to restore and when to time them out
|
|
|
|
(mark them as too old), returns the lines (list), empty list if log file
|
|
|
|
does not exist'''
|
|
|
|
fjid = fjid.lower()
|
|
|
|
path_to_file = self.__get_path_to_file(fjid)
|
|
|
|
if not os.path.isfile(path_to_file):
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
def get_conversation_for_date(self, fjid, year, month, day):
|
|
|
|
'''returns the text in the lines (list),
|
|
|
|
returns empty list if log file does not exist'''
|
|
|
|
fjid = fjid.lower()
|
|
|
|
path_to_file = self.__get_path_to_file(fjid)
|
|
|
|
if not os.path.isfile(path_to_file):
|
|
|
|
return []
|
|
|
|
|
|
|
|
lines = []
|
|
|
|
f = open(path_to_file, 'r')
|
|
|
|
done = False
|
|
|
|
found_first_line_that_matches = False
|
|
|
|
while not done:
|
2005-11-21 07:33:46 +01:00
|
|
|
line = f.readline()
|
2005-11-20 22:58:08 +01:00
|
|
|
if line:
|
|
|
|
line = helpers.from_one_line(line)
|
|
|
|
splitted_line = line.split(':')
|
|
|
|
if len(splitted_line) > 2:
|
2005-11-21 07:33:46 +01:00
|
|
|
# line[0] is date, line[1] is type of message
|
|
|
|
# line[2:] is message
|
|
|
|
date = splitted_line[0]
|
|
|
|
date = time.localtime(float(date))
|
|
|
|
# eg. 2005
|
|
|
|
line_year = int(time.strftime('%Y', date))
|
|
|
|
# (01 - 12)
|
|
|
|
line_month = int(time.strftime('%m', date))
|
|
|
|
# (01 - 31)
|
|
|
|
line_day = int(time.strftime('%d', date))
|
|
|
|
|
|
|
|
# now check if that line is one of the lines we want
|
|
|
|
# (if it is in the date we want)
|
|
|
|
if line_year == year and line_month == month and line_day == day:
|
|
|
|
if found_first_line_that_matches is False:
|
|
|
|
found_first_line_that_matches = True
|
|
|
|
lines.append(splitted_line)
|
|
|
|
else:
|
|
|
|
if found_first_line_that_matches: # we had a match before
|
|
|
|
done = True # but no more. so we're done with that date
|
2005-11-20 22:58:08 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
done = True
|
|
|
|
|
|
|
|
return lines
|