You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
3.2 KiB

  1. import datetime
  2. import sys
  3. assert len(sys.argv) == 2, 'Arg: filename'
  4. filename = sys.argv[1]
  5. date = filename.rsplit('/', 1)[-1][:10]
  6. with open(filename, 'rb') as fp:
  7. for line in fp:
  8. origLine = line
  9. if not (line[0:1] == b'[' and line[3:4] == b':' and line[6:8] == b'] ' and line[-1:] == b'\n'):
  10. print(f'MALFORMED LINE: {line!r}', file = sys.stderr)
  11. continue
  12. time = line[1:6].decode('ascii')
  13. line = line[8:-1]
  14. ts = datetime.datetime(int(date[:4]), int(date[5:7]), int(date[8:]), int(time[:2]), int(time[3:]), 0).replace(tzinfo = datetime.timezone.utc).timestamp()
  15. if line.startswith(b'<'): #PRIVMSG
  16. sys.stdout.buffer.write(f'{ts} PRIVMSG '.encode('ascii') + line + b'\n')
  17. elif line.startswith(b'* '): #ACTION
  18. sys.stdout.buffer.write(f'{ts} ACTION '.encode('ascii') + line[2:] + b'\n')
  19. elif line.startswith(b'*** '):
  20. words = line.split(b' ')[1:]
  21. if words[1:3] == [b'has', b'joined']: # JOIN
  22. sys.stdout.buffer.write(f'{ts} JOIN '.encode('ascii') + words[0] + b' joins' + b'\n')
  23. elif words[0] == b'Joins:': # JOIN
  24. sys.stdout.buffer.write(f'{ts} JOIN '.encode('ascii') + words[1] + b' joins' + b'\n')
  25. elif words[1:3] == [b'has', b'left']: # PART
  26. reason = (b' [' + b' '.join(words[3:]) + b']') if len(words) > 4 or words[3] != b'' else b''
  27. sys.stdout.buffer.write(f'{ts} PART '.encode('ascii') + words[0] + b' leaves' + reason + b'\n')
  28. elif words[0] == b'Parts:': # PART
  29. reason = (b' [' + b' '.join(words[3:])[1:-1] + b']') if len(words) > 4 or words[3] != b'()' else b''
  30. sys.stdout.buffer.write(f'{ts} PART '.encode('ascii') + words[1] + b' leaves' + reason + b'\n')
  31. elif words[1:4] == [b'has', b'quit', b'IRC']: # QUIT
  32. reason = (b' [' + b' '.join(words[4:])[1:-1] + b']') if len(words) > 4 or words[4] != b'()' else b''
  33. sys.stdout.buffer.write(f'{ts} QUIT '.encode('ascii') + words[0] + b' quits' + reason + b'\n')
  34. elif words[0] == b'Quits:': # QUIT
  35. reason = (b' [' + b' '.join(words[3:])[1:-1] + b']') if len(words) > 4 or words[3] != b'()' else b''
  36. sys.stdout.buffer.write(f'{ts} QUIT '.encode('ascii') + words[0] + b' quits' + reason + b'\n')
  37. elif words[1:4] == [b'was', b'kicked', b'by']: # KICK
  38. sys.stdout.buffer.write(f'{ts} KICK '.encode('ascii') + words[0] + b' is kicked by ' + words[4] + b' [' + b' '.join(words[5:])[1:-1] + b']' + b'\n')
  39. elif words[1:3] == [b'sets', b'mode:']: # MODE
  40. sys.stdout.buffer.write(f'{ts} MODE '.encode('ascii') + line[4:] + b'\n')
  41. elif words[1:4] == [b'changes', b'topic', b'to:']: # TOPIC
  42. sys.stdout.buffer.write(f'{ts} TOPIC '.encode('ascii') + words[0] + b' sets the topic to: ' + b' '.join(words[4:]) + b'\n')
  43. elif words[1:4] == [b'changes', b'topic', b'to']: # TOPIC
  44. sys.stdout.buffer.write(f'{ts} TOPIC '.encode('ascii') + words[0] + b' sets the topic to: ' + b' '.join(words[4:])[1:-1] + b'\n')
  45. elif words[1:5] == [b'is', b'now', b'known', b'as']: # NICK
  46. sys.stdout.buffer.write(f'{ts} NICK '.encode('ascii') + line[4:] + b'\n')
  47. elif words[1:3] == [b'starts', b'logging']: # Silently ignore (there's already a JOIN)
  48. pass
  49. else:
  50. print(f'MALFORMED LINE: {origLine!r}', file = sys.stderr)
  51. else:
  52. print(f'MALFORMED LINE: {origLine!r}', file = sys.stderr)