plugins/disabled/log_plugin.py
changeset 35 8daaa0b29b71
parent 34 535d03a56f9d
child 36 ca538a004a30
equal deleted inserted replaced
34:535d03a56f9d 35:8daaa0b29b71
     1 #$ neutron_plugin 01
       
     2 
       
     3 import re
       
     4 
       
     5 LOG_CACHE_FILE = 'dynamic/logcache.txt'
       
     6 
       
     7 initialize_file(LOG_CACHE_FILE, '{}')
       
     8 LOG_FILENAME_CACHE = eval(read_file(LOG_CACHE_FILE))
       
     9 
       
    10 def log_write_header(fp, source, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)):
       
    11     fp.write("""<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
       
    12 <head>
       
    13 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       
    14 <style type="text/css">
       
    15 <!--
       
    16 .timestamp {color: #AAAAAA;}
       
    17 .system {color: #009900; font-weight: bold;}
       
    18 .emote {color: #AA0099;}
       
    19 .self {color: #CC0000;}
       
    20 .normal {color: #0000AA;}
       
    21 h1 { color: #336699; font-family: sans-serif; border-bottom: #224466 solid 3pt; letter-spacing: 3px; margin-left: 20pt; }
       
    22 h2 { color: #663399; font-family: sans-serif; letter-spacing: 2px; text-align: center }
       
    23 //-->
       
    24 </style>
       
    25 </head>
       
    26 <body>
       
    27 <div style="color: #AAAAAA; text-align: right; font-family: monospace; letter-spacing: 3px">neutron log</div>
       
    28 <h1>""" + source + """</h1>
       
    29 <h2>""" + time.strftime('%A, %B %d, %Y', (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)) + """</h2>
       
    30 <br />
       
    31 <tt>
       
    32 
       
    33 """)
       
    34 
       
    35 def log_write_footer(fp):
       
    36     fp.write('\n</tt>\n</body>\n</html>')
       
    37 
       
    38 def log_get_fp(type, source, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings)):
       
    39     if type == 'public':
       
    40         logdir = PUBLIC_LOG_DIR
       
    41     else:
       
    42         logdir = PRIVATE_LOG_DIR
       
    43     if logdir[-1] == '/':
       
    44         logdir = LOGDIR[:-1]
       
    45     str_year = str(year)
       
    46     str_month = str(month)
       
    47     str_day = str(day)
       
    48     filename = logdir + '/' + source + '/' + str_year + '/' + str_month + '/' + str_day + '.html'
       
    49     alt_filename = logdir + '/' + source + '/' + str_year + '/' + str_month + '/' + str_day + '_alt.html'
       
    50     if not os.path.exists(logdir):
       
    51         os.mkdir(logdir)
       
    52     if not os.path.exists(logdir + '/' + source):
       
    53         os.mkdir(logdir + '/' + source)
       
    54     if not os.path.exists(logdir + '/' + source + '/' + str_year):
       
    55         os.mkdir(logdir + '/' + source + '/' + str_year)
       
    56     if not os.path.exists(logdir + '/' + source + '/' + str_year + '/' + str_month):
       
    57         os.mkdir(logdir + '/' + source + '/' + str_year + '/' + str_month)
       
    58     if LOG_FILENAME_CACHE.has_key(source):
       
    59         if LOG_FILENAME_CACHE[source] != filename:
       
    60             fp_old = file(LOG_FILENAME_CACHE[source], 'a')
       
    61             log_write_footer(fp_old)
       
    62             fp_old.close()
       
    63         if os.path.exists(filename):
       
    64             fp = file(filename, 'a')
       
    65             return fp
       
    66         else:
       
    67             LOG_FILENAME_CACHE[source] = filename
       
    68             write_file(LOG_CACHE_FILE, str(LOG_FILENAME_CACHE))
       
    69             fp = file(filename, 'w')
       
    70             log_write_header(fp, source, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings))
       
    71             return fp
       
    72     else:
       
    73         if os.path.exists(filename):
       
    74             LOG_FILENAME_CACHE[source] = filename
       
    75             write_file(LOG_CACHE_FILE, str(LOG_FILENAME_CACHE))
       
    76             fp = file(alt_filename, 'a')
       
    77             # log_write_header(fp, source, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings))
       
    78             return fp
       
    79         else:
       
    80             LOG_FILENAME_CACHE[source] = filename
       
    81             write_file(LOG_CACHE_FILE, str(LOG_FILENAME_CACHE))
       
    82             fp = file(filename, 'w')
       
    83             log_write_header(fp, source, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings))
       
    84             return fp
       
    85 
       
    86 def log_get_timestamp(hour, minute):
       
    87     timestamp = '['
       
    88     if hour < 10:
       
    89         timestamp += '0'
       
    90     timestamp += str(hour)
       
    91     timestamp += ':'
       
    92     if minute < 10:
       
    93         timestamp += '0'
       
    94     timestamp += str(minute)
       
    95     timestamp += ']'
       
    96     return timestamp
       
    97 
       
    98 def log_regex_url(matchobj):
       
    99     # 06.03.05(Sun) slipstream@yandex.ru urls parser
       
   100     return '<a href="' + matchobj.group(0) + '">' + matchobj.group(0) + '</a>'
       
   101 
       
   102 def log_handler_message(type, source, body):
       
   103     if not body:
       
   104         return
       
   105     (year, month, day, hour, minute, second, weekday, yearday, daylightsavings) = time.gmtime()
       
   106     if type == 'public' and PUBLIC_LOG_DIR:
       
   107         groupchat = source[1]
       
   108         nick = source[2]
       
   109         # 06.03.05(Sun) slipstream@yandex.ru urls parser & line ends
       
   110         body = body.replace('&', '&amp;').replace('"', '&quot;').replace('<', '&lt;').replace('>', '&gt;')
       
   111         body = re.sub('(http|ftp)(\:\/\/[^\s<]+)', log_regex_url, body)
       
   112         body = body.replace('\n', '<br />')
       
   113         body = body.encode('utf-8');
       
   114         nick = nick.encode('utf-8');
       
   115         timestamp = log_get_timestamp(hour, minute)
       
   116         fp = log_get_fp('public', groupchat, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings))
       
   117         fp.write('<font class="timestamp">' + timestamp + '</font> ')
       
   118         if not nick:
       
   119             fp.write('<font class="system">' + body + '</font><br />\n')
       
   120         elif body[:3].lower() == '/me':
       
   121             fp.write('<font class="emote">* ' + nick + body[3:] + '</font><br />\n')
       
   122         else:
       
   123             # 08.03.05(Tue) slipstream@yandex.ru encoding
       
   124             if nick == get_nick(groupchat).encode('utf-8'):
       
   125                 fp.write('<font class="self">&lt;' + nick + '&gt;</font> ')
       
   126             else:
       
   127                 fp.write('<font class="normal">&lt;' + nick + '&gt;</font> ')
       
   128             fp.write(body + '<br />\n')
       
   129         fp.close()
       
   130     elif type == 'private' and PRIVATE_LOG_DIR:
       
   131         jid = get_true_jid(source)
       
   132         nick = string.split(jid, '@')[0]
       
   133         # 06.03.05(Sun) slipstream@yandex.ru urls parser, line ends & encoding
       
   134         body = body.replace('&', '&amp;').replace('"', '&quot;').replace('<', '&lt;').replace('>', '&gt;')
       
   135         body = re.sub('(http|ftp)(\:\/\/[^\s<]+)', log_regex_url, body)
       
   136         body = body.replace('\n', '<br />')
       
   137         body = body.encode('utf-8');
       
   138         nick = nick.encode('utf-8');
       
   139         timestamp = log_get_timestamp(hour, minute)
       
   140         fp = log_get_fp('private', jid, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings))
       
   141         fp.write('<font class="timestamp">' + timestamp + '</font> ')
       
   142         if body[:3].lower() == '/me':
       
   143             fp.write('<font class="emote">* ' + nick + body[3:] + '</font><br />\n')
       
   144         else:
       
   145             fp.write('<font class="normal">&lt;' + nick + '&gt;</font> ' + body + '<br />\n')
       
   146         fp.close()
       
   147 
       
   148 def log_handler_outgoing_message(target, body):
       
   149     if GROUPCHATS.has_key(target) or not body:
       
   150         return
       
   151     (year, month, day, hour, minute, second, weekday, yearday, daylightsavings) = time.gmtime()
       
   152     jid = get_true_jid(target)
       
   153     nick = 'neutron'
       
   154     # 06.03.05(Sun) slipstream@yandex.ru urls parser, line ends & encoding
       
   155     body = body.replace('&', '&amp;').replace('"', '&quot;').replace('<', '&lt;').replace('>', '&gt;')
       
   156     body = re.sub('(http|ftp)(\:\/\/[^\s<]+)', log_regex_url, body)
       
   157     body = body.replace('\n', '<br />')
       
   158     body = body.encode('utf-8');
       
   159     nick = nick.encode('utf-8');
       
   160     timestamp = log_get_timestamp(hour, minute)
       
   161     fp = log_get_fp('private', jid, (year, month, day, hour, minute, second, weekday, yearday, daylightsavings))
       
   162     fp.write('<font class="timestamp">' + timestamp + '</font> ')
       
   163     if body[:3].lower() == '/me':
       
   164         fp.write('<font class="emote">* ' + nick + body[3:] + '</font><br />\n')
       
   165     else:
       
   166         fp.write('<font class="self">&lt;' + nick + '&gt;</font> ' + body + '<br />\n')
       
   167     fp.close()
       
   168 
       
   169 if PUBLIC_LOG_DIR or PRIVATE_LOG_DIR:
       
   170     register_message_handler(log_handler_message)
       
   171 if PRIVATE_LOG_DIR:
       
   172     register_outgoing_message_handler(log_handler_outgoing_message)