logging basically done, more cleaning
This commit is contained in:
parent
666a2fe3ce
commit
db1997d529
10
bot.py
10
bot.py
|
@ -96,13 +96,11 @@ class Input(object):
|
||||||
self.host = host
|
self.host = host
|
||||||
self.paraml = paraml
|
self.paraml = paraml
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
if command == "PRIVMSG":
|
|
||||||
if paraml[0] != bot.nick:
|
|
||||||
self.chan = paraml[0]
|
self.chan = paraml[0]
|
||||||
else:
|
if self.chan == bot.nick:
|
||||||
self.chan = nick
|
self.chan = nick
|
||||||
else:
|
elif command =='JOIN':
|
||||||
self.chan = ""
|
self.chan = msg
|
||||||
|
|
||||||
|
|
||||||
class FakeBot(object):
|
class FakeBot(object):
|
||||||
|
@ -148,7 +146,7 @@ while True:
|
||||||
try:
|
try:
|
||||||
input = sieve(bot, input, func, args)
|
input = sieve(bot, input, func, args)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
print 'filter error:', e
|
print 'sieve error:', e
|
||||||
input = None
|
input = None
|
||||||
if input == None:
|
if input == None:
|
||||||
break
|
break
|
||||||
|
|
|
@ -45,7 +45,6 @@ def goog_trans(text, slang, tlang):
|
||||||
json = urllib.urlopen(url).read()
|
json = urllib.urlopen(url).read()
|
||||||
parsed = yaml.load(json)
|
parsed = yaml.load(json)
|
||||||
if not 200 <= parsed['responseStatus'] < 300:
|
if not 200 <= parsed['responseStatus'] < 300:
|
||||||
print parsed
|
|
||||||
raise IOError('error with the translation server: %d: %s' % (
|
raise IOError('error with the translation server: %d: %s' % (
|
||||||
parsed['responseStatus'], ''))
|
parsed['responseStatus'], ''))
|
||||||
return unescape(parsed['responseData']['translatedText'])
|
return unescape(parsed['responseData']['translatedText'])
|
||||||
|
@ -56,7 +55,6 @@ def babel_gen(inp):
|
||||||
inp = inp.encode('utf8')
|
inp = inp.encode('utf8')
|
||||||
trans = goog_trans(inp, 'en', language).encode('utf8')
|
trans = goog_trans(inp, 'en', language).encode('utf8')
|
||||||
inp = goog_trans(trans, language, 'en')
|
inp = goog_trans(trans, language, 'en')
|
||||||
print language, trans, inp
|
|
||||||
yield language, trans, inp
|
yield language, trans, inp
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,15 @@ log_fds = {} # '%(net)s %(chan)s' : (filename, fd)
|
||||||
|
|
||||||
timestamp_format = '%H:%M:%S'
|
timestamp_format = '%H:%M:%S'
|
||||||
|
|
||||||
|
formats = {'PRIVMSG': '<%(nick)s> %(msg)s',
|
||||||
|
'PART': '-!- %(nick)s [%(user)s@%(host)s] has left %(chan)s',
|
||||||
|
'JOIN': '-!- %(nick)s [%(user)s@%(host)s] has joined %(chan)s',
|
||||||
|
'MODE': '-!- mode/%(chan)s [%(param_tail)s] by %(nick)s',
|
||||||
|
'KICK': '-!- %(param1)s was kicked from %(chan)s by %(nick)s [%(msg)s]',
|
||||||
|
'TOPIC': '-!- %(nick)s changed the topic of %(chan)s to: %(msg)s'
|
||||||
|
}
|
||||||
|
|
||||||
|
ctcp_formats = {'ACTION': '* %(nick)s %(ctcpmsg)s'}
|
||||||
|
|
||||||
def get_log_filename(dir, network, chan):
|
def get_log_filename(dir, network, chan):
|
||||||
return os.path.join(dir, 'log', gmtime('%Y'), network,
|
return os.path.join(dir, 'log', gmtime('%Y'), network,
|
||||||
|
@ -26,6 +35,29 @@ def gmtime(format):
|
||||||
return time.strftime(format, time.gmtime())
|
return time.strftime(format, time.gmtime())
|
||||||
|
|
||||||
|
|
||||||
|
def beautify(input):
|
||||||
|
format = formats.get(input.command, '%(raw)s')
|
||||||
|
args = vars(input)
|
||||||
|
leng = len(args['paraml'])
|
||||||
|
for n, p in enumerate(args['paraml']):
|
||||||
|
args['param' + str(n)] = p
|
||||||
|
args['param_' + str(abs(n - leng))] = p
|
||||||
|
|
||||||
|
args['param_tail'] = ' '.join(args['paraml'][1:])
|
||||||
|
|
||||||
|
if input.command == 'PRIVMSG' and input.msg.count('\x01') >= 2:
|
||||||
|
#ctcp
|
||||||
|
ctcp = input.msg.split('\x01', 2)[1].split(' ', 1)
|
||||||
|
if len(ctcp) == 1:
|
||||||
|
ctcp += ['']
|
||||||
|
args['ctcpcmd'], args['ctcpmsg'] = ctcp
|
||||||
|
format = ctcp_formats.get(args['ctcpcmd'],
|
||||||
|
'%(nick)s [%(user)s@%(host)s] requested unknown CTCP '
|
||||||
|
'%(ctcpcmd)s from %(chan)s: %(ctcpmsg)s')
|
||||||
|
|
||||||
|
return format % args
|
||||||
|
|
||||||
|
|
||||||
def get_log_fd(dir, network, chan):
|
def get_log_fd(dir, network, chan):
|
||||||
fn = get_log_filename(dir, network, chan)
|
fn = get_log_filename(dir, network, chan)
|
||||||
cache_key = '%s %s' % (network, chan)
|
cache_key = '%s %s' % (network, chan)
|
||||||
|
@ -48,9 +80,11 @@ def get_log_fd(dir, network, chan):
|
||||||
def log(bot, input):
|
def log(bot, input):
|
||||||
".remember <word> <data> -- maps word to data in the memory"
|
".remember <word> <data> -- maps word to data in the memory"
|
||||||
with lock:
|
with lock:
|
||||||
|
timestamp = gmtime(timestamp_format)
|
||||||
|
|
||||||
fd = get_log_fd(bot.persist_dir, bot.network, 'raw')
|
fd = get_log_fd(bot.persist_dir, bot.network, 'raw')
|
||||||
fd.write(gmtime(timestamp_format) + ' ' + input.raw + '\n')
|
fd.write(timestamp + ' ' + input.raw + '\n')
|
||||||
|
|
||||||
if input.chan:
|
if input.chan:
|
||||||
fd = get_log_fd(bot.persist_dir, bot.network, input.chan)
|
fd = get_log_fd(bot.persist_dir, bot.network, input.chan)
|
||||||
fd.write(gmtime(timestamp_format) + ' ' + input.raw + '\n')
|
fd.write(timestamp + ' ' + beautify(input) + '\n')
|
||||||
|
|
|
@ -3,8 +3,6 @@ import hook
|
||||||
|
|
||||||
@hook.event('KICK INVITE')
|
@hook.event('KICK INVITE')
|
||||||
def rejoin(bot, input):
|
def rejoin(bot, input):
|
||||||
print input.command, input.inp
|
|
||||||
|
|
||||||
if input.command == 'KICK':
|
if input.command == 'KICK':
|
||||||
if input.paraml[1] == bot.bot.nick:
|
if input.paraml[1] == bot.bot.nick:
|
||||||
if input.paraml[0] == bot.bot.channel:
|
if input.paraml[0] == bot.bot.channel:
|
||||||
|
|
|
@ -68,9 +68,7 @@ def forget(bot, input):
|
||||||
if not input.inp.strip():
|
if not input.inp.strip():
|
||||||
return forget.__doc__
|
return forget.__doc__
|
||||||
|
|
||||||
print input.inp
|
|
||||||
low = input.inp.strip().lower()
|
low = input.inp.strip().lower()
|
||||||
print repr(low)
|
|
||||||
if low not in memory[filename]:
|
if low not in memory[filename]:
|
||||||
return "I don't know about that."
|
return "I don't know about that."
|
||||||
if not hasattr(input, 'chan'):
|
if not hasattr(input, 'chan'):
|
||||||
|
@ -88,6 +86,5 @@ def question(bot, input):
|
||||||
memory.setdefault(filename, load_memory(filename))
|
memory.setdefault(filename, load_memory(filename))
|
||||||
|
|
||||||
word = input.inp.split()[0].lower()
|
word = input.inp.split()[0].lower()
|
||||||
print memory[filename]
|
|
||||||
if word in memory[filename]:
|
if word in memory[filename]:
|
||||||
bot.say("%s" % memory[filename][word])
|
bot.say("%s" % memory[filename][word])
|
||||||
|
|
Loading…
Reference in New Issue