diff -r 535d03a56f9d -r 8daaa0b29b71 plugins/log_plugin.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/log_plugin.py Mon May 07 11:59:17 2007 +0200 @@ -0,0 +1,172 @@ +#$ neutron_plugin 01 + +import re + +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)): + fp.write(""" + + + + + +
neutron log
+

""" + source + """

+

""" + time.strftime('%A, %B %d, %Y', (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)) + """

+
+ + +""") + +def log_write_footer(fp): + fp.write('\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 = logdir + '/' + source + '/' + str_year + '/' + str_month + '/' + str_day + '.html' + alt_filename = logdir + '/' + source + '/' + str_year + '/' + str_month + '/' + str_day + '_alt.html' + if not os.path.exists(logdir): + os.mkdir(logdir) + if not os.path.exists(logdir + '/' + source): + os.mkdir(logdir + '/' + source) + if not os.path.exists(logdir + '/' + source + '/' + str_year): + os.mkdir(logdir + '/' + source + '/' + str_year) + if not os.path.exists(logdir + '/' + source + '/' + str_year + '/' + str_month): + os.mkdir(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') + # log_write_header(fp, source, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)) + 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 + +def log_get_timestamp(hour, minute): + timestamp = '[' + if hour < 10: + timestamp += '0' + timestamp += str(hour) + timestamp += ':' + if minute < 10: + timestamp += '0' + timestamp += str(minute) + timestamp += ']' + return timestamp + +def log_regex_url(matchobj): + # 06.03.05(Sun) slipstream@yandex.ru urls parser + return '' + matchobj.group(0) + '' + +def log_handler_message(type, source, body): + if not body: + return + (year, month, day, hour, minute, second, weekday, yearday, daylightsavings) = time.gmtime() + if type == 'public' and PUBLIC_LOG_DIR: + groupchat = source[1] + nick = source[2] + # 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 = log_get_timestamp(hour, minute) + fp = log_get_fp('public', groupchat, (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('* ' + nick + body[3:] + '
\n') + else: + # 08.03.05(Tue) slipstream@yandex.ru encoding + if nick == get_nick(groupchat).encode('utf-8'): + fp.write('<' + nick + '> ') + else: + fp.write('<' + nick + '> ') + fp.write(body + '
\n') + fp.close() + elif type == 'private' and PRIVATE_LOG_DIR: + jid = get_true_jid(source) + nick = string.split(jid, '@')[0] + # 06.03.05(Sun) slipstream@yandex.ru urls parser, line ends & encoding + 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 = log_get_timestamp(hour, minute) + fp = log_get_fp('private', jid, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)) + fp.write('' + timestamp + ' ') + if body[:3].lower() == '/me': + fp.write('* ' + nick + body[3:] + '
\n') + else: + fp.write('<' + nick + '> ' + body + '
\n') + fp.close() + +def log_handler_outgoing_message(target, body): + if GROUPCHATS.has_key(target) or not body: + return + (year, month, day, hour, minute, second, weekday, yearday, daylightsavings) = time.gmtime() + jid = get_true_jid(target) + nick = 'neutron' + # 06.03.05(Sun) slipstream@yandex.ru urls parser, line ends & encoding + 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 = log_get_timestamp(hour, minute) + fp = log_get_fp('private', jid, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)) + fp.write('' + timestamp + ' ') + if body[:3].lower() == '/me': + fp.write('* ' + nick + body[3:] + '
\n') + else: + fp.write('<' + nick + '> ' + body + '
\n') + fp.close() + +if PUBLIC_LOG_DIR or PRIVATE_LOG_DIR: + register_message_handler(log_handler_message) +if PRIVATE_LOG_DIR: + register_outgoing_message_handler(log_handler_outgoing_message)