2011-09-06 00:45:53 +00:00
|
|
|
|
# Copyright 2010-2011 Florent Le Coz <louiz@louiz.org>
|
2010-01-10 20:14:17 +00:00
|
|
|
|
#
|
|
|
|
|
# This file is part of Poezio.
|
|
|
|
|
#
|
|
|
|
|
# Poezio is free software: you can redistribute it and/or modify
|
2011-09-11 15:10:05 +00:00
|
|
|
|
# it under the terms of the zlib license. See the COPYING file.
|
2010-01-10 20:14:17 +00:00
|
|
|
|
|
2010-03-18 19:43:44 +00:00
|
|
|
|
from os import environ, makedirs
|
2010-08-22 14:57:47 +00:00
|
|
|
|
import os
|
2013-06-21 22:17:55 +00:00
|
|
|
|
import re
|
2010-01-26 17:10:37 +00:00
|
|
|
|
from datetime import datetime
|
2010-01-26 19:33:40 +00:00
|
|
|
|
from config import config
|
2011-05-27 21:36:52 +00:00
|
|
|
|
from xhtml import clean_text
|
2013-06-21 22:17:55 +00:00
|
|
|
|
from theming import dump_tuple, get_theme
|
2010-03-18 19:43:44 +00:00
|
|
|
|
|
2011-01-02 16:58:40 +00:00
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
2013-06-21 22:17:55 +00:00
|
|
|
|
log_dir = config.get('log_dir', '') or os.path.join(environ.get('XDG_DATA_HOME') or os.path.join(environ.get('HOME'), '.local', 'share'), 'poezio', 'logs')
|
|
|
|
|
log_dir = os.path.expanduser(log_dir)
|
|
|
|
|
|
|
|
|
|
message_log_re = re.compile('MR (\d{4})(\d{2})(\d{2})T(\d{2}):(\d{2}):(\d{2})Z (\d+) <([^ ]+)> (.*)')
|
|
|
|
|
info_log_re = re.compile('MI (\d{4})(\d{2})(\d{2})T(\d{2}):(\d{2}):(\d{2})Z (\d+) (.*)')
|
|
|
|
|
|
|
|
|
|
def parse_message_line(msg):
|
|
|
|
|
if re.match(message_log_re, msg):
|
|
|
|
|
return [i for i in re.split(message_log_re, msg) if i]
|
|
|
|
|
elif re.match(info_log_re, msg):
|
|
|
|
|
return [i for i in re.split(info_log_re, msg) if i]
|
|
|
|
|
return False
|
|
|
|
|
|
2010-03-18 19:43:44 +00:00
|
|
|
|
|
2010-01-10 20:14:17 +00:00
|
|
|
|
class Logger(object):
|
|
|
|
|
"""
|
|
|
|
|
Appends things to files. Error/information/warning logs
|
|
|
|
|
and also log the conversations to logfiles
|
|
|
|
|
"""
|
2011-01-02 16:58:40 +00:00
|
|
|
|
def __init__(self):
|
2010-01-26 19:33:40 +00:00
|
|
|
|
self.logfile = config.get('logfile', 'logs')
|
2011-06-18 16:31:57 +00:00
|
|
|
|
self.roster_logfile = None
|
2011-01-02 16:58:40 +00:00
|
|
|
|
# a dict of 'groupchatname': file-object (opened)
|
2011-01-12 17:34:04 +00:00
|
|
|
|
self.fds = dict()
|
2010-01-26 19:33:40 +00:00
|
|
|
|
|
2011-01-02 16:58:40 +00:00
|
|
|
|
def __del__(self):
|
2011-01-12 17:34:04 +00:00
|
|
|
|
for opened_file in self.fds.values():
|
2012-04-02 16:30:06 +00:00
|
|
|
|
if opened_file:
|
2013-04-05 21:57:53 +00:00
|
|
|
|
try:
|
|
|
|
|
opened_file.close()
|
|
|
|
|
except: # Can't close? too bad
|
|
|
|
|
pass
|
2012-04-02 16:30:06 +00:00
|
|
|
|
|
|
|
|
|
def reload_all(self):
|
|
|
|
|
"""Close and reload all the file handles (on SIGHUP)"""
|
|
|
|
|
for opened_file in self.fds.values():
|
|
|
|
|
if opened_file:
|
|
|
|
|
opened_file.close()
|
|
|
|
|
log.debug('All log file handles closed')
|
|
|
|
|
for room in self.fds:
|
|
|
|
|
self.fds[room] = self.check_and_create_log_dir(room)
|
|
|
|
|
log.debug('Log handle for %s re-created', room)
|
2011-01-02 16:58:40 +00:00
|
|
|
|
|
|
|
|
|
def check_and_create_log_dir(self, room):
|
2010-03-18 19:43:44 +00:00
|
|
|
|
"""
|
2011-01-02 16:58:40 +00:00
|
|
|
|
Check that the directory where we want to log the messages
|
|
|
|
|
exists. if not, create it
|
2010-03-18 19:43:44 +00:00
|
|
|
|
"""
|
2012-12-16 12:42:22 +00:00
|
|
|
|
if config.get_by_tabname('use_log', 'false', room) == 'false':
|
|
|
|
|
return
|
2010-03-18 19:43:44 +00:00
|
|
|
|
try:
|
2013-06-21 22:17:55 +00:00
|
|
|
|
makedirs(log_dir)
|
2010-07-01 22:01:09 +00:00
|
|
|
|
except OSError:
|
2013-06-19 18:14:05 +00:00
|
|
|
|
pass
|
2010-07-01 22:01:09 +00:00
|
|
|
|
try:
|
2013-06-21 22:17:55 +00:00
|
|
|
|
fd = open(os.path.join(log_dir, room), 'a')
|
2011-01-12 17:34:04 +00:00
|
|
|
|
self.fds[room] = fd
|
2011-01-02 16:58:40 +00:00
|
|
|
|
return fd
|
2010-07-01 22:01:09 +00:00
|
|
|
|
except IOError:
|
2012-12-16 12:42:22 +00:00
|
|
|
|
return
|
2011-01-02 16:58:40 +00:00
|
|
|
|
|
2012-12-15 21:57:57 +00:00
|
|
|
|
def get_logs(self, jid, nb=10):
|
2012-11-11 15:01:53 +00:00
|
|
|
|
"""
|
|
|
|
|
Get the log history for the given jid
|
|
|
|
|
"""
|
2012-12-16 12:42:22 +00:00
|
|
|
|
if config.get_by_tabname('load_log', 10, jid) <= 0:
|
2012-12-15 22:23:12 +00:00
|
|
|
|
return
|
|
|
|
|
|
2012-11-11 15:01:53 +00:00
|
|
|
|
if nb <= 0:
|
2012-12-15 22:23:12 +00:00
|
|
|
|
return
|
2012-11-11 15:01:53 +00:00
|
|
|
|
try:
|
2013-06-21 22:17:55 +00:00
|
|
|
|
fd = open(os.path.join(log_dir, jid), 'rb')
|
2012-11-11 15:01:53 +00:00
|
|
|
|
except:
|
2012-12-15 22:23:12 +00:00
|
|
|
|
return
|
|
|
|
|
if not fd:
|
|
|
|
|
return
|
2012-11-11 15:01:53 +00:00
|
|
|
|
|
2012-12-15 22:23:12 +00:00
|
|
|
|
pos = fd.seek(0, 2)
|
2013-06-21 22:17:55 +00:00
|
|
|
|
reads = 0
|
|
|
|
|
check_next = False
|
|
|
|
|
while reads < nb:
|
2012-12-16 12:42:22 +00:00
|
|
|
|
if pos == 0:
|
|
|
|
|
break
|
2013-06-21 22:17:55 +00:00
|
|
|
|
pos -= 1
|
2012-12-15 22:23:12 +00:00
|
|
|
|
if pos < 0:
|
2012-12-16 12:42:22 +00:00
|
|
|
|
pos = 0
|
2012-12-15 22:23:12 +00:00
|
|
|
|
fd.seek(pos)
|
2013-06-21 22:17:55 +00:00
|
|
|
|
char = fd.read(1)
|
|
|
|
|
if char == b'\n':
|
|
|
|
|
if fd.read(2) in (b'MR', b'MI'):
|
|
|
|
|
reads += 1
|
|
|
|
|
fd.seek(-2, 1)
|
|
|
|
|
logs = fd.readlines()
|
2012-11-11 15:01:53 +00:00
|
|
|
|
fd.close()
|
2013-06-21 22:17:55 +00:00
|
|
|
|
lines = []
|
|
|
|
|
|
|
|
|
|
for line in logs:
|
|
|
|
|
lines.append(line.decode(errors='replace')[:-1])
|
|
|
|
|
log.debug(lines)
|
|
|
|
|
|
|
|
|
|
messages = []
|
|
|
|
|
color = '\x19%s}' % dump_tuple(get_theme().COLOR_INFORMATION_TEXT)
|
|
|
|
|
|
|
|
|
|
idx = 0
|
|
|
|
|
while idx < len(lines):
|
|
|
|
|
if lines[idx].startswith(' '): # should not happen ; skip
|
|
|
|
|
idx += 1
|
|
|
|
|
log.debug('fail?')
|
|
|
|
|
continue
|
|
|
|
|
tup = parse_message_line(lines[idx])
|
|
|
|
|
idx += 1
|
|
|
|
|
if not tup or 7 > len(tup) > 10 : # skip
|
|
|
|
|
log.debug('format? %s', tup)
|
|
|
|
|
continue
|
|
|
|
|
time = [int(i) for index, i in enumerate(tup) if index < 6]
|
|
|
|
|
message = {'lines': [], 'history': True, 'time': datetime(*time)}
|
|
|
|
|
size = int(tup[6])
|
|
|
|
|
if len(tup) == 8: #info line
|
|
|
|
|
message['lines'].append(color+tup[7])
|
|
|
|
|
else: # message line
|
|
|
|
|
message['nickname'] = tup[7]
|
|
|
|
|
message['lines'].append(color+tup[8])
|
|
|
|
|
while size != 0 and idx < len(lines):
|
|
|
|
|
message['lines'].append(lines[idx][1:])
|
|
|
|
|
size -= 1
|
|
|
|
|
idx += 1
|
|
|
|
|
message['txt'] = '\n'.join(message['lines'])
|
|
|
|
|
del message['lines']
|
|
|
|
|
messages.append(message)
|
|
|
|
|
|
|
|
|
|
return messages
|
2012-11-11 15:01:53 +00:00
|
|
|
|
|
2013-06-09 01:47:16 +00:00
|
|
|
|
def log_message(self, jid, nick, msg, date=None, typ=1):
|
2011-01-02 16:58:40 +00:00
|
|
|
|
"""
|
2011-01-12 17:34:04 +00:00
|
|
|
|
log the message in the appropriate jid's file
|
2013-06-09 01:47:16 +00:00
|
|
|
|
type: 1 = Message
|
|
|
|
|
2 = Status/whatever
|
2011-01-02 16:58:40 +00:00
|
|
|
|
"""
|
2013-06-09 01:47:16 +00:00
|
|
|
|
jid = str(jid).replace('/', '\\')
|
2013-04-06 09:45:34 +00:00
|
|
|
|
if config.get_by_tabname('use_log', 'false', jid) != 'true':
|
|
|
|
|
return True
|
2011-01-12 17:34:04 +00:00
|
|
|
|
if jid in self.fds.keys():
|
|
|
|
|
fd = self.fds[jid]
|
2011-01-02 16:58:40 +00:00
|
|
|
|
else:
|
2011-01-12 17:34:04 +00:00
|
|
|
|
fd = self.check_and_create_log_dir(jid)
|
2011-01-02 16:58:40 +00:00
|
|
|
|
if not fd:
|
2013-04-05 21:57:53 +00:00
|
|
|
|
return True
|
2011-02-21 20:00:23 +00:00
|
|
|
|
try:
|
2011-05-27 21:36:52 +00:00
|
|
|
|
msg = clean_text(msg)
|
2013-03-31 14:33:10 +00:00
|
|
|
|
if date is None:
|
2013-06-21 22:17:55 +00:00
|
|
|
|
str_time = datetime.now().strftime('%Y%m%dT%H:%M:%SZ')
|
2013-03-31 14:33:10 +00:00
|
|
|
|
else:
|
2013-06-21 22:17:55 +00:00
|
|
|
|
str_time = date.strftime('%Y%m%dT%H:%M:%SZ')
|
2013-06-09 01:47:16 +00:00
|
|
|
|
if typ == 1:
|
|
|
|
|
prefix = 'MR'
|
|
|
|
|
else:
|
|
|
|
|
prefix = 'MI'
|
2013-06-21 22:17:55 +00:00
|
|
|
|
lines = msg.split('\n')
|
|
|
|
|
first_line = lines.pop(0)
|
|
|
|
|
nb_lines = str(len(lines)).zfill(3)
|
|
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
|
self.roster_logfile.write(' %s\n' % line)
|
|
|
|
|
|
2011-02-21 20:00:23 +00:00
|
|
|
|
if nick:
|
2013-06-09 01:47:16 +00:00
|
|
|
|
nick = '<' + nick + '>'
|
2013-06-21 22:17:55 +00:00
|
|
|
|
fd.write(' '.join((prefix, str_time, nb_lines, nick, ' '+first_line, '\n')))
|
2011-02-21 20:00:23 +00:00
|
|
|
|
else:
|
2013-06-21 22:17:55 +00:00
|
|
|
|
fd.write(' '.join((prefix, str_time, nb_lines, first_line, '\n')))
|
|
|
|
|
for line in lines:
|
|
|
|
|
fd.write(' %s\n' % line)
|
2013-04-05 21:57:53 +00:00
|
|
|
|
except:
|
|
|
|
|
return False
|
2010-06-13 02:19:01 +00:00
|
|
|
|
else:
|
2013-04-05 21:57:53 +00:00
|
|
|
|
try:
|
|
|
|
|
fd.flush() # TODO do something better here?
|
|
|
|
|
except:
|
|
|
|
|
return False
|
|
|
|
|
return True
|
2010-03-18 19:43:44 +00:00
|
|
|
|
|
2011-06-18 16:31:57 +00:00
|
|
|
|
def log_roster_change(self, jid, message):
|
2013-04-06 09:45:34 +00:00
|
|
|
|
"""
|
|
|
|
|
Log a roster change
|
|
|
|
|
"""
|
|
|
|
|
if config.get_by_tabname('use_log', 'false', jid) != 'true':
|
|
|
|
|
return True
|
2011-06-18 16:31:57 +00:00
|
|
|
|
if not self.roster_logfile:
|
|
|
|
|
try:
|
2013-06-21 22:17:55 +00:00
|
|
|
|
self.roster_logfile = open(os.path.join(log_dir, 'roster.log'), 'a')
|
2011-06-18 16:31:57 +00:00
|
|
|
|
except IOError:
|
2013-04-05 21:57:53 +00:00
|
|
|
|
return False
|
|
|
|
|
try:
|
2013-06-21 22:17:55 +00:00
|
|
|
|
str_time = datetime.now().strftime('%Y%m%dT%H:%M:%SZ')
|
2013-06-09 10:56:51 +00:00
|
|
|
|
message = clean_text(message)
|
2013-06-21 22:17:55 +00:00
|
|
|
|
lines = message.split('\n')
|
|
|
|
|
first_line = lines.pop(0)
|
|
|
|
|
nb_lines = str(len(lines)).zfill(3)
|
|
|
|
|
self.roster_logfile.write('MI %s %s %s %s\n' % (str_time, nb_lines, jid, first_line))
|
|
|
|
|
for line in lines:
|
|
|
|
|
self.roster_logfile.write(' %s\n' % line)
|
2013-04-05 21:57:53 +00:00
|
|
|
|
self.roster_logfile.flush()
|
|
|
|
|
except:
|
|
|
|
|
return False
|
|
|
|
|
return True
|
2011-06-18 16:31:57 +00:00
|
|
|
|
|
2010-01-26 17:10:37 +00:00
|
|
|
|
logger = Logger()
|