#!/usr/bin/python
# -*- coding: utf-8 -*-
# --------------------------------------------------------------------------- #
# #
# Plugin for iSida Jabber Bot #
# Copyright (C) diSabler #
# Copyright (C) Vit@liy #
# #
# 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 3 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. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see . #
# #
# --------------------------------------------------------------------------- #
# translate: chat_log_join,away_log_join,xa_log_join,dnd_log_join,online_log_join,chat_log_change,away_log_change,xa_log_change,dnd_log_change,online_log_change
smile_dictionary = {}
last_log_file = {}
last_log_presence = {}
log_header = ['','\n' % logs_css_path][GT('html_logs_enable')]
if not os.path.exists(log_folder % ''): os.mkdir(log_folder % '')
if not os.path.exists(public_log % ''): os.mkdir(public_log % '')
if not os.path.exists(system_log % '') and GT('syslogs_enable'): os.mkdir(system_log % '')
for smilepack in smiles_dirs_case:
smile_dictionary[smilepack] = {}
if smilepack[0] != '.':
data = readfile(os.path.join(sm_f,smilepack,smile_descriptor))
for icon in re.findall('((?:\s|.)+?)', data):
img = re.search('', icon).group(1)
for pattern in re.findall('(.+?)', icon): smile_dictionary[smilepack][pattern.replace('<','<').replace('>','>')] = img
def smile_replace(room, text):
global smile_dictionary
smilepack = smiles_dirs_case[smiles_dirs.index(get_config(getRoom(room),'smiles'))]
newtext = ' %s ' % text.replace(' ',' ').replace(' ', ' ')
for smile in sorted(smile_dictionary[smilepack], key = lambda x: len(x))[::-1]:
tmp_smile = ' %s ' % html_escape(smile.decode('utf-8')).replace('&', '&')
while tmp_smile in newtext:
sm_tag = ' ' % ('../../../%s/%s/%s' % (smile_folder, smilepack, smile_dictionary[smilepack][smile]),smile.decode('utf-8'))
newtext = newtext.replace(tmp_smile, sm_tag)
return newtext.replace(' ',' ').replace(' ',' ')[1:-1]
def initial_log_users(room,ott):
txt = []
for tmp in megabase:
if tmp[0] == room: txt.append(tmp[1])
txt = non_case_sort(txt)
log_body = ['[%s] ' % ott,'
'][GT('html_logs_enable')]
return log_body
def append_message_to_log(room,jid,nick,type,text):
global public_log, system_log
lr = cur_execute_fetchone('select * from log_rooms where room=%s;',(room,))
if lr:
if GT('html_logs_enable'): text,jid,nick = html_escape(text).replace('\n',' '), html_escape(jid), html_escape(nick)
if type == 'groupchat' and text != 'None':
if get_config(getRoom(room),'smiles') != 'off': ptext = smile_replace(room, text)
else: ptext = text
msg_logger(room,jid,nick,type,ptext,public_log)
if type == 'chat': nick = 'chat >>> %s' % nick
if GT('syslogs_enable'): msg_logger(room,jid,nick,type,text,system_log)
def write_log_with_end(curr_path,curr_file,log_body,log_he):
global last_log_file
if not os.path.isfile(curr_file):
fl = open(curr_file, 'a')
fl.write(log_he.encode('utf-8'))
fl.write(log_body.encode('utf-8'))
try:
ll = last_log_file[curr_path]
if os.path.isfile(ll):
ender = ['\n',' %s' % GT('html_logs_end_text')][GT('html_logs_enable')]
fle = open(ll, 'a')
fle.write(ender.encode('utf-8'))
fle.close()
except: pass
else:
fl = open(curr_file, 'a')
fl.write(log_body.encode('utf-8'))
fl.close()
last_log_file[curr_path] = curr_file
def msg_logger(room,jid,nick,type,text,logfile):
if GT('chatlogs_logs_gmt'): lt = tuple(time.gmtime())
else: lt = tuple(time.localtime())
curr_path = logfile % room
if not os.path.exists(curr_path): os.mkdir(curr_path)
curr_path = '%s/%s' % (curr_path,lt[0])
if not os.path.exists(curr_path): os.mkdir(curr_path)
curr_path = '%s/%02d' % (curr_path,lt[1])
if not os.path.exists(curr_path): os.mkdir(curr_path)
curr_file = ['%s/%02d.txt','%s/%02d.html'][GT('html_logs_enable')] % (curr_path,lt[2])
ott = onlytimeadd(lt)
log_body = ['[%s] ' % ott,'
%s ' % (ott,ott,ott,ott)][GT('html_logs_enable')]
if nick == '': log_body += ['*** %s\n','%s
'][GT('html_logs_enable')] % text
else:
if text[:4] == '/me ': log_body += ['*%s %s\n','%s %s
\n'][GT('html_logs_enable')] % (nick,text[4:])
else:
if ' ' + ' '*8 in text:
msg_type = '%s: %s\n'
if not text.startswith(' '): text = ' %s' % text
text = text.replace(' '*8,'')
elif ' '*8 in text:
no_tab_text,cnt = [],0
for t in text.split(' '):
if not ' '*8 in t:
no_tab_text.append(t)
cnt += 1
else: break
text = ' '.join(text.split(' ')[cnt:])
no_tab_text = ' '.join(no_tab_text)
msg_type = '%s: ' + no_tab_text + '%s\n'
text = '
'][GT('html_logs_enable')] % [topics[room],html_escape(topics[room]).replace('\n',' ')][GT('html_logs_enable')]
except: pass
write_log_with_end(logfile+room,curr_file,log_body,log_he)
global execute
message_control = [append_message_to_log]
presence_control = [append_presence_to_log]
def log_room(type, jid, nick, text):
if type == 'groupchat': msg = L('This command available only in private!','%s/%s'%(jid,nick))
else:
text = text.strip().lower().split(' ',1)
hmode = text[0]
try: hroom = text[1]
except: hroom = jid
if not hmode:
if cur_execute_fetchone('select * from log_rooms where room=%s;',(hroom,)): msg = L('Logs for %s enabled.','%s/%s'%(jid,nick)) % hroom
else: msg = L('Logs for %s disabled.','%s/%s'%(jid,nick)) % hroom
elif hmode == 'show':
lr = cur_execute_fetchall('select * from log_rooms;')
if lr: msg = '%s\n%s' % (L('Logged conferences:','%s/%s'%(jid,nick)),'\n'.join([t[0] for t in lr]))
else: msg = L('Logs are turned off in all conferences.','%s/%s'%(jid,nick))
elif hmode == 'add':
if not cur_execute_fetchall('select * from conference where room ilike %s;', ('%s/%%'%getRoom(hroom),)): msg = L('I am not in the %s','%s/%s'%(jid,nick)) % hroom
elif cur_execute_fetchone('select * from log_rooms where room=%s;',(hroom,)): msg = L('Logs for %s already enabled.','%s/%s'%(jid,nick)) % hroom
else:
cur_execute('insert into log_rooms values (%s);',(hroom,))
msg = L('Logs for %s enabled.','%s/%s'%(jid,nick)) % hroom
elif hmode == 'del':
if cur_execute_fetchone('select * from log_rooms where room=%s;',(hroom,)):
cur_execute('delete from log_rooms where room=%s;',(hroom,))
msg = L('Logs for %s disabled.','%s/%s'%(jid,nick)) % hroom
else: msg = L('Logs for %s not enabled.','%s/%s'%(jid,nick)) % hroom
else: msg = L('What?','%s/%s'%(jid,nick))
send_msg(type, jid, nick, msg)
execute = [(9, 'log', log_room, 2, 'Logging history conference.\nlog [add|del|show][ room@conference.server.tld]')]