#===istalismanplugin=== # -*- coding: utf-8 -*- # Talisman plugin # log_plugin.py # Initial Copyright © Anaлl Verrier # Modifications Copyright © 2007 Als # 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. import re, os, math, time LOG_CACHE_FILE = 'dynamic/logcache.txt' initialize_file(LOG_CACHE_FILE, '{}') LOG_FILENAME_CACHE = eval(read_file(LOG_CACHE_FILE)) def log_write_header(fp, source, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)): date = time.strftime('%A, %B %d, %Y', (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)) fp.write(""" %s
Talisman log

%s

%s

""" % (' - '.join([source, date]), source, source, date)) def log_write_footer(fp): fp.write('\n\n
\n\n') def log_get_fp(type, source, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)): if type == 'public': logdir = PUBLIC_LOG_DIR else: logdir = PRIVATE_LOG_DIR if logdir[-1] == '/': logdir = logdir[:-1] str_year = str(year) str_month = str(month) str_day = str(day) filename = '.'.join(['/'.join([logdir, source, str_year, str_month, str_day]), 'html']) alt_filename = '.'.join(['/'.join([logdir, source, str_year, str_month, str_day]), '_alt.html']) if not os.path.exists('/'.join([logdir, source, str_year, str_month])): os.makedirs('/'.join([logdir, source, str_year, str_month])) if LOG_FILENAME_CACHE.has_key(source): if LOG_FILENAME_CACHE[source] != filename: fp_old = file(LOG_FILENAME_CACHE[source], 'a') log_write_footer(fp_old) fp_old.close() if os.path.exists(filename): fp = file(filename, 'a') return fp else: LOG_FILENAME_CACHE[source] = filename write_file(LOG_CACHE_FILE, str(LOG_FILENAME_CACHE)) fp = file(filename, 'w') log_write_header(fp, source, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)) return fp else: if os.path.exists(filename): LOG_FILENAME_CACHE[source] = filename write_file(LOG_CACHE_FILE, str(LOG_FILENAME_CACHE)) fp = file(alt_filename, 'a') return fp else: LOG_FILENAME_CACHE[source] = filename fp = file(filename, 'w') log_write_header(fp, source, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)) return fp def log_regex_url(matchobj): # 06.03.05(Sun) slipstream@yandex.ru urls parser return '' + matchobj.group(0) + '' def log_handler_message(raw, type, source, body): if not body: return if type == 'public' and PUBLIC_LOG_DIR: groupchat = source[1] nick = source[2] if groupchat in GROUPCHATS and nick in GROUPCHATS[groupchat] and 'ismoder' in GROUPCHATS[groupchat][nick] and GROUPCHATS[groupchat][nick]['ismoder'] == 1: ismoder=1 else: ismoder=0 log_write(body, nick, type, groupchat, ismoder) elif type == 'private' and PRIVATE_LOG_DIR: jid = get_true_jid(source) log_write(body, jid.split('@')[0], type, jid) def log_handler_outgoing_message(target, body, obody): if GROUPCHATS.has_key(target) or not body: return log_write(body, DEFAULT_NICK, 'private', get_true_jid(target)) def log_write(body, nick, type, jid, ismoder=0): if not jid in GROUPCHATS.keys(): jid = get_true_jid(jid) decimal = str(int(math.modf(time.time())[0]*100000)) (year, month, day, hour, minute, second, weekday, yearday, daylightsavings) = time.localtime() # 06.03.05(Sun) slipstream@yandex.ru urls parser & line ends body = body.replace('&', '&').replace('"', '"').replace('<', '<').replace('>', '>') body = re.sub('(http|ftp)(\:\/\/[^\s<]+)', log_regex_url, body) body = body.replace('\n', '
') body = body.encode('utf-8'); nick = nick.encode('utf-8'); timestamp = '[%.2i:%.2i:%.2i]' % (hour, minute, second) fp = log_get_fp(type, jid, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)) fp.write('' + timestamp + ' ') if not nick: fp.write('' + body + '
\n') elif body[:3].lower() == '/me': fp.write('* %s%s
\n' % (nick, body[3:])) elif type == 'public' or nick == DEFAULT_NICK: if nick=='@$$leave$$@': fp.write('' + body + '
\n') elif nick=='@$$join$$@': fp.write('' + body + '
\n') elif nick=='@$$status$$@': fp.write('' + body + '
\n') elif nick=='@$$ra$$@': fp.write('' + body + '
\n') elif nick=='@$$userkick$$@': fp.write('' + body + '
\n') elif nick=='@$$userban$$@': fp.write('' + body + '
\n') elif nick=='@$$nickchange$$@': fp.write('' + body + '
\n') else: if ismoder: fp.write('<%s> %s
\n' % (nick, body)) else: fp.write('<%s> %s
\n' % (nick, body)) else: fp.write('<%s> %s
\n' % (nick, body)) fp.close() def log_handler_join(groupchat, nick, aff, role): log_write('%s joins the room as %s and %s' % (nick, role, aff), '@$$join$$@', 'public', groupchat) def log_handler_leave(groupchat, nick, reason, code): if code: if code == '307': if reason: log_write('%s has been kicked (%s)' % (nick,reason), '@$$userkick$$@', 'public', groupchat) else: log_write('%s has been kicked' % (nick), '@$$userkick$$@', 'public', groupchat) elif code == '301': if reason: log_write('%s has been banned (%s)' % (nick,reason), '@$$userban$$@', 'public', groupchat) else: log_write('%s has been banned' % (nick), '@$$userban$$@', 'public', groupchat) else: if reason: log_write('%s leaves the room (%s)' % (nick,reason), '@$$leave$$@', 'public', groupchat) else: log_write('%s leaves the room' % (nick), '@$$leave$$@', 'public', groupchat) def log_handler_presence(prs): stmsg,status,code,reason,newnick='','','','','' groupchat = prs.getFrom().getStripped() nick = prs.getFrom().getResource() code = prs.getStatusCode() reason = prs.getReason() if code == '303': newnick = prs.getNick() log_write('%s now is known as %s' % (nick,newnick), '@$$nickchange$$@', 'public', groupchat) else: if not prs.getType()=='unavailable': try: stmsg = prs.getStatus() except: stmsg='' try: status = prs.getShow() except: status = 'online' if not status: status = 'online' if stmsg: log_write('%s is now %s (%s)' % (nick,status,stmsg), '@$$status$$@', 'public', groupchat) else: log_write('%s is now %s' % (nick,status), '@$$status$$@', 'public', groupchat) if PUBLIC_LOG_DIR: register_message_handler(log_handler_message) register_join_handler(log_handler_join) register_leave_handler(log_handler_leave) register_presence_handler(log_handler_presence) if PRIVATE_LOG_DIR: register_outgoing_message_handler(log_handler_outgoing_message)