Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
logger function added, for more easily write log and debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtew committed Oct 12, 2014
1 parent 9b84935 commit 31607cc
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions irccat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,28 @@


def irccat_config(bot):
"""Return our configuration or False"""
if not bot.config.has_option('irccat', 'address') or \
not bot.config.has_option('irccat', 'port'):
return False
else:
return [bot.config.irccat.address, bot.config.irccat.port]


def irccat_logger(bot, src, body, level=False):
"""
Write messages to our log with timestamp and source IP.
Also write to debug log if we set a level
"""
message = str(datetime.now()) + ': From: ' + src + ':' + body
logfile = open(os.path.join(bot.config.logdir, 'irccat.log'), 'a')
logfile.write(message)
logfile.close()

if level is not False:
bot.debug('irccat', message, level)


def irccat_targets(bot, targets):
"""
Go through our potential targets and place them in an array so we can
Expand Down Expand Up @@ -80,20 +95,17 @@ def netpipe(bot, trigger):
data = conn.recv(1024)

if not len(data.split()) >= 2:
errmsg = str(datetime.now()) + ': from: ' + addr[0]
errmsg += ': Warning: Received message too short: ' + data
bot.debug('irccat', errmsg, 'warning')
errmsg = 'Too short message received'
irccat_logger(bot, addr[0], errmsg + ': ' + data, 'warning')
conn.close()
continue

logfile = open(os.path.join(bot.config.logdir, 'irccat.log'), 'a')
logfile.write(str(datetime.now()) + ' msg ' + addr[0] + ': ' + data)
irccat_logger(bot, addr[0], 'Received: ' + data)

target, message = data.split(' ', 1)
for chat in irccat_targets(bot, target):
bot.msg(chat, message)

conn.close()
logfile.close()

sock.close()

0 comments on commit 31607cc

Please sign in to comment.