Browse Source

Wrap some overlong lines

master
JustAnotherArchivist 3 years ago
parent
commit
c3573ad5d4
1 changed files with 61 additions and 7 deletions
  1. +61
    -7
      irclog.py

+ 61
- 7
irclog.py View File

@@ -219,7 +219,13 @@ class Config(dict):
raise InvalidConfig(f'Invalid channel {key!r} extrasearchchannels: refers to auth-required channel whose auth differs from this channel\'s')

# Default values
finalObj = {'logging': {'level': 'INFO', 'format': '{asctime} {levelname} {name} {message}'}, 'storage': {'path': os.path.abspath(os.path.dirname(self._filename)), 'flushTime': 60}, 'irc': {'host': 'irc.hackint.org', 'port': 6697, 'ssl': 'yes', 'nick': 'irclogbot', 'real': 'I am an irclog bot.', 'certfile': None, 'certkeyfile': None}, 'web': {'host': '127.0.0.1', 'port': 8080}, 'channels': {}}
finalObj = {
'logging': {'level': 'INFO', 'format': '{asctime} {levelname} {name} {message}'},
'storage': {'path': os.path.abspath(os.path.dirname(self._filename)), 'flushTime': 60},
'irc': {'host': 'irc.hackint.org', 'port': 6697, 'ssl': 'yes', 'nick': 'irclogbot', 'real': 'I am an irclog bot.', 'certfile': None, 'certkeyfile': None},
'web': {'host': '127.0.0.1', 'port': 8080},
'channels': {}
}
# Default values for channels are already set above.

# Merge in what was read from the config file and set keys on self
@@ -444,7 +450,13 @@ class IRCClientProtocol(asyncio.Protocol):
self.transport.close()

# JOIN errors
elif line.command in (ircstates.numerics.ERR_TOOMANYCHANNELS, ircstates.numerics.ERR_CHANNELISFULL, ircstates.numerics.ERR_INVITEONLYCHAN, ircstates.numerics.ERR_BANNEDFROMCHAN, ircstates.numerics.ERR_BADCHANNELKEY):
elif line.command in (
ircstates.numerics.ERR_TOOMANYCHANNELS,
ircstates.numerics.ERR_CHANNELISFULL,
ircstates.numerics.ERR_INVITEONLYCHAN,
ircstates.numerics.ERR_BANNEDFROMCHAN,
ircstates.numerics.ERR_BADCHANNELKEY,
):
self.logger.error(f'Failed to join channel: {message!r}, terminating connection')
self.transport.close()

@@ -566,7 +578,8 @@ class IRCClientProtocol(asyncio.Protocol):
channel = line.params[1]
yield 'TOPIC', channel, f'Topic of {channel}: {line.params[2]}'
elif line.command == ircstates.numerics.RPL_TOPICWHOTIME:
yield 'TOPICWHO', line.params[1], f'Topic set by {irctokens.hostmask(line.params[2]).nickname} at {datetime.datetime.utcfromtimestamp(int(line.params[3])).replace(tzinfo = datetime.timezone.utc):%Y-%m-%d %H:%M:%SZ}'
date = datetime.datetime.utcfromtimestamp(int(line.params[3])).replace(tzinfo = datetime.timezone.utc)
yield 'TOPICWHO', line.params[1], f'Topic set by {irctokens.hostmask(line.params[2]).nickname} at {date:%Y-%m-%d %H:%M:%SZ}'
elif line.command == ircstates.numerics.RPL_ENDOFNAMES:
channel = line.params[1]
users = self.server.channels[self.server.casefold(channel)].users
@@ -636,7 +649,12 @@ class IRCClient:
connectionClosedEvent.clear()
try:
self.logger.debug('Creating IRC connection')
self._transport, self._protocol = await loop.create_connection(lambda: IRCClientProtocol(self.messageQueue, connectionClosedEvent, loop, self.config, self.channels), self.config['irc']['host'], self.config['irc']['port'], ssl = self._get_ssl_context())
self._transport, self._protocol = await loop.create_connection(
protocol_factory = lambda: IRCClientProtocol(self.messageQueue, connectionClosedEvent, loop, self.config, self.channels),
host = self.config['irc']['host'],
port = self.config['irc']['port'],
ssl = self._get_ssl_context(),
)
self.logger.debug('Starting send queue processing')
asyncio.create_task(self._protocol.send_queue()) # Quits automatically on connectionClosedEvent
self.logger.debug('Waiting for connection closure or SIGINT')
@@ -881,7 +899,20 @@ class WebServer:
#TODO Implement this in a better way...
fn = date.strftime('%Y-%m.log')
lines = list(self._raw_to_lines(self._file_iter_with_path(os.path.join(self.config['storage']['path'], request.match_info["path"], fn), request.match_info["path"]), filter = lambda path, ts, command, content: dateStart <= ts <= dateEnd))
return aiohttp.web.Response(text = f'<!DOCTYPE html><html lang="en"><head><title>{html.escape(self._paths[request.match_info["path"]][0])} log for {date:%Y-%m-%d}</title>{self.logStyleTag}</head><body><a href="/{html.escape(request.match_info["path"])}/{(date - datetime.timedelta(days = 1)).strftime("%Y-%m-%d")}">Previous day</a> <a href="/{html.escape(request.match_info["path"])}/{(date + datetime.timedelta(days = 1)).strftime("%Y-%m-%d")}">Next day</a><br /><br />' + self._render_log(lines) + '</body></html>', content_type = 'text/html')
return aiohttp.web.Response(
text = ''.join([
'<!DOCTYPE html><html lang="en">',
f'<head><title>{html.escape(self._paths[request.match_info["path"]][0])} log for {date:%Y-%m-%d}</title>{self.logStyleTag}</head>',
'<body>',
f'<a href="/{html.escape(request.match_info["path"])}/{(date - datetime.timedelta(days = 1)).strftime("%Y-%m-%d")}">Previous day</a> ',
f'<a href="/{html.escape(request.match_info["path"])}/{(date + datetime.timedelta(days = 1)).strftime("%Y-%m-%d")}">Next day</a>',
'<br /><br />',
self._render_log(lines),
'</body>',
'</html>',
]),
content_type = 'text/html'
)

async def search(self, request):
self.logger.info(f'Received request {id(request)} from {request.remote!r} for {request.path!r}')
@@ -890,7 +921,20 @@ class WebServer:
return aiohttp.web.HTTPNotFound()

if 'q' not in request.query:
return aiohttp.web.Response(text = f'<!DOCTYPE html><html lang="en"><head><title>{html.escape(self._paths[request.match_info["path"]][0])} search</title></head><body><form><input name="q" /><input type="submit" value="Search!" /></form></body></html>', content_type = 'text/html')
return aiohttp.web.Response(
text = ''.join([
'<!DOCTYPE html><html lang="en">'
f'<head><title>{html.escape(self._paths[request.match_info["path"]][0])} search</title></head>',
'<body>',
'<form>',
'<input name="q" />',
'<input type="submit" value="Search!" />',
'</form>',
'</body>',
'</html>'
]),
content_type = 'text/html'
)

cmd = ['grep', '--fixed-strings', '--recursive', '--with-filename', '--null', '--line-number', request.query['q']]
for path in itertools.chain((request.match_info['path'],), self._paths[request.match_info['path']][3]):
@@ -899,7 +943,17 @@ class WebServer:
#TODO Limit size and runtime
stdout, _ = await proc.communicate()
lines = self._raw_to_lines(self._stdout_with_path(stdout))
return aiohttp.web.Response(text = f'<!DOCTYPE html><html lang="en"><head><title>{html.escape(self._paths[request.match_info["path"]][0])} search results for "{html.escape(request.query["q"])}"</title>{self.logStyleTag}</head><body>' + self._render_log(lines, withDate = True) + '</body></html>', content_type = 'text/html')
return aiohttp.web.Response(
text = ''.join([
'<!DOCTYPE html><html lang="en">',
f'<head><title>{html.escape(self._paths[request.match_info["path"]][0])} search results for "{html.escape(request.query["q"])}"</title>{self.logStyleTag}</head>',
'<body>',
self._render_log(lines, withDate = True),
'</body>',
'</html>'
]),
content_type = 'text/html'
)


def configure_logging(config):


Loading…
Cancel
Save