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.

1161 lines
52 KiB

  1. import aiohttp
  2. import aiohttp.web
  3. import asyncio
  4. import base64
  5. import collections
  6. import functools
  7. import importlib.util
  8. import inspect
  9. import ircstates
  10. import irctokens
  11. import itertools
  12. import json
  13. import logging
  14. import math
  15. import os.path
  16. import signal
  17. import socket
  18. import ssl
  19. import string
  20. import sys
  21. import time
  22. import toml
  23. import warnings
  24. logger = logging.getLogger('http2irc')
  25. SSL_CONTEXTS = {'yes': True, 'no': False, 'insecure': ssl.SSLContext()}
  26. class InvalidConfig(Exception):
  27. '''Error in configuration file'''
  28. def is_valid_pem(path, withCert):
  29. '''Very basic check whether something looks like a valid PEM certificate'''
  30. try:
  31. with open(path, 'rb') as fp:
  32. contents = fp.read()
  33. # All of these raise exceptions if something's wrong...
  34. if withCert:
  35. assert contents.startswith(b'-----BEGIN CERTIFICATE-----\n')
  36. endCertPos = contents.index(b'-----END CERTIFICATE-----\n')
  37. base64.b64decode(contents[28:endCertPos].replace(b'\n', b''), validate = True)
  38. assert contents[endCertPos + 26:].startswith(b'-----BEGIN PRIVATE KEY-----\n')
  39. else:
  40. assert contents.startswith(b'-----BEGIN PRIVATE KEY-----\n')
  41. endCertPos = -26 # Please shoot me.
  42. endKeyPos = contents.index(b'-----END PRIVATE KEY-----\n')
  43. base64.b64decode(contents[endCertPos + 26 + 28: endKeyPos].replace(b'\n', b''), validate = True)
  44. assert contents[endKeyPos + 26:] == b''
  45. return True
  46. except: # Yes, really
  47. return False
  48. async def wait_cancel_pending(aws, paws = None, **kwargs):
  49. '''asyncio.wait but with automatic cancellation of non-completed tasks. Tasks in paws (persistent awaitables) are not automatically cancelled.'''
  50. if paws is None:
  51. paws = set()
  52. tasks = aws | paws
  53. logger.debug(f'waiting for {tasks!r}')
  54. done, pending = await asyncio.wait(tasks, **kwargs)
  55. logger.debug(f'done waiting for {tasks!r}; cancelling pending non-persistent tasks: {pending!r}')
  56. for task in pending:
  57. if task not in paws:
  58. logger.debug(f'cancelling {task!r}')
  59. task.cancel()
  60. logger.debug(f'awaiting cancellation of {task!r}')
  61. try:
  62. await task
  63. except asyncio.CancelledError:
  64. pass
  65. logger.debug(f'done cancelling {task!r}')
  66. logger.debug(f'done wait_cancel_pending {tasks!r}')
  67. return done, pending
  68. class Config(dict):
  69. def __init__(self, filename):
  70. super().__init__()
  71. self._filename = filename
  72. with open(self._filename, 'r') as fp:
  73. obj = toml.load(fp)
  74. # Sanity checks
  75. if any(x not in ('logging', 'irc', 'web', 'maps') for x in obj.keys()):
  76. raise InvalidConfig('Unknown sections found in base object')
  77. if any(not isinstance(x, collections.abc.Mapping) for x in obj.values()):
  78. raise InvalidConfig('Invalid section type(s), expected objects/dicts')
  79. if 'logging' in obj:
  80. if any(x not in ('level', 'format') for x in obj['logging']):
  81. raise InvalidConfig('Unknown key found in log section')
  82. if 'level' in obj['logging'] and obj['logging']['level'] not in ('DEBUG', 'INFO', 'WARNING', 'ERROR'):
  83. raise InvalidConfig('Invalid log level')
  84. if 'format' in obj['logging']:
  85. if not isinstance(obj['logging']['format'], str):
  86. raise InvalidConfig('Invalid log format')
  87. try:
  88. #TODO: Replace with logging.Formatter's validate option (3.8+); this test does not cover everything that could be wrong (e.g. invalid format spec or conversion)
  89. # This counts the number of replacement fields. Formatter.parse yields tuples whose second value is the field name; if it's None, there is no field (e.g. literal text).
  90. assert sum(1 for x in string.Formatter().parse(obj['logging']['format']) if x[1] is not None) > 0
  91. except (ValueError, AssertionError) as e:
  92. raise InvalidConfig('Invalid log format: parsing failed') from e
  93. if 'irc' in obj:
  94. if any(x not in ('host', 'port', 'ssl', 'family', 'nick', 'real', 'certfile', 'certkeyfile') for x in obj['irc']):
  95. raise InvalidConfig('Unknown key found in irc section')
  96. if 'host' in obj['irc'] and not isinstance(obj['irc']['host'], str): #TODO: Check whether it's a valid hostname
  97. raise InvalidConfig('Invalid IRC host')
  98. if 'port' in obj['irc'] and (not isinstance(obj['irc']['port'], int) or not 1 <= obj['irc']['port'] <= 65535):
  99. raise InvalidConfig('Invalid IRC port')
  100. if 'ssl' in obj['irc'] and obj['irc']['ssl'] not in ('yes', 'no', 'insecure'):
  101. raise InvalidConfig(f'Invalid IRC SSL setting: {obj["irc"]["ssl"]!r}')
  102. if 'family' in obj['irc']:
  103. if obj['irc']['family'] not in ('inet', 'INET', 'inet6', 'INET6'):
  104. raise InvalidConfig('Invalid IRC family')
  105. obj['irc']['family'] = getattr(socket, f'AF_{obj["irc"]["family"].upper()}')
  106. if 'nick' in obj['irc']:
  107. if not isinstance(obj['irc']['nick'], str) or not obj['irc']['nick']:
  108. raise InvalidConfig('Invalid IRC nick')
  109. if obj['irc']['nick'][0] not in string.ascii_letters + '[]\\`_^{|}' or obj['irc']['nick'].strip(string.ascii_letters + string.digits + '[]\\`_^{|}') != '':
  110. # The allowed characters in nicknames (per RFC 2812) are a strict subset of the ones for usernames, so no need to also check for the latter.
  111. raise InvalidConfig('Invalid IRC nick: contains illegal characters')
  112. if len(IRCClientProtocol.nick_command(obj['irc']['nick'])) > 510:
  113. raise InvalidConfig('Invalid IRC nick: NICK command too long')
  114. if 'real' in obj['irc'] and not isinstance(obj['irc']['real'], str):
  115. raise InvalidConfig('Invalid IRC realname')
  116. if len(IRCClientProtocol.user_command(obj['irc']['nick'], obj['irc']['real'])) > 510:
  117. raise InvalidConfig('Invalid IRC nick/realname combination: USER command too long')
  118. if ('certfile' in obj['irc']) != ('certkeyfile' in obj['irc']):
  119. raise InvalidConfig('Invalid IRC cert config: needs both certfile and certkeyfile')
  120. if 'certfile' in obj['irc']:
  121. if not isinstance(obj['irc']['certfile'], str):
  122. raise InvalidConfig('Invalid certificate file: not a string')
  123. obj['irc']['certfile'] = os.path.abspath(os.path.join(os.path.dirname(self._filename), obj['irc']['certfile']))
  124. if not os.path.isfile(obj['irc']['certfile']):
  125. raise InvalidConfig('Invalid certificate file: not a regular file')
  126. if not is_valid_pem(obj['irc']['certfile'], True):
  127. raise InvalidConfig('Invalid certificate file: not a valid PEM cert')
  128. if 'certkeyfile' in obj['irc']:
  129. if not isinstance(obj['irc']['certkeyfile'], str):
  130. raise InvalidConfig('Invalid certificate key file: not a string')
  131. obj['irc']['certkeyfile'] = os.path.abspath(os.path.join(os.path.dirname(self._filename), obj['irc']['certkeyfile']))
  132. if not os.path.isfile(obj['irc']['certkeyfile']):
  133. raise InvalidConfig('Invalid certificate key file: not a regular file')
  134. if not is_valid_pem(obj['irc']['certkeyfile'], False):
  135. raise InvalidConfig('Invalid certificate key file: not a valid PEM key')
  136. if 'web' in obj:
  137. if any(x not in ('host', 'port', 'maxrequestsize') for x in obj['web']):
  138. raise InvalidConfig('Unknown key found in web section')
  139. if 'host' in obj['web'] and not isinstance(obj['web']['host'], str): #TODO: Check whether it's a valid hostname (must resolve I guess?)
  140. raise InvalidConfig('Invalid web hostname')
  141. if 'port' in obj['web'] and (not isinstance(obj['web']['port'], int) or not 1 <= obj['web']['port'] <= 65535):
  142. raise InvalidConfig('Invalid web port')
  143. if 'maxrequestsize' in obj['web'] and (not isinstance(obj['web']['maxrequestsize'], int) or obj['web']['maxrequestsize'] <= 0):
  144. raise InvalidConfig('Invalid web maxrequestsize')
  145. if 'maps' in obj:
  146. seenWebPaths = {}
  147. for key, map_ in obj['maps'].items():
  148. if not isinstance(key, str) or not key:
  149. raise InvalidConfig(f'Invalid map key {key!r}')
  150. if not isinstance(map_, collections.abc.Mapping):
  151. raise InvalidConfig(f'Invalid map for {key!r}')
  152. if any(x not in ('webpath', 'ircchannel', 'auth', 'postauth', 'getauth', 'module', 'moduleargs', 'overlongmode') for x in map_):
  153. raise InvalidConfig(f'Unknown key(s) found in map {key!r}')
  154. if 'webpath' not in map_:
  155. map_['webpath'] = f'/{key}'
  156. if not isinstance(map_['webpath'], str):
  157. raise InvalidConfig(f'Invalid map {key!r} web path: not a string')
  158. if not map_['webpath'].startswith('/'):
  159. raise InvalidConfig(f'Invalid map {key!r} web path: does not start at the root')
  160. if map_['webpath'] == '/status':
  161. raise InvalidConfig(f'Invalid map {key!r} web path: cannot be "/status"')
  162. if map_['webpath'] in seenWebPaths:
  163. raise InvalidConfig(f'Invalid map {key!r} web path: collides with map {seenWebPaths[map_["webpath"]]!r}')
  164. seenWebPaths[map_['webpath']] = key
  165. if 'ircchannel' not in map_:
  166. map_['ircchannel'] = f'#{key}'
  167. if not isinstance(map_['ircchannel'], str):
  168. raise InvalidConfig(f'Invalid map {key!r} IRC channel: not a string')
  169. if not map_['ircchannel'].startswith('#') and not map_['ircchannel'].startswith('&'):
  170. raise InvalidConfig(f'Invalid map {key!r} IRC channel: does not start with # or &')
  171. if any(x in map_['ircchannel'][1:] for x in (' ', '\x00', '\x07', '\r', '\n', ',')):
  172. raise InvalidConfig(f'Invalid map {key!r} IRC channel: contains forbidden characters')
  173. if len(map_['ircchannel']) > 200:
  174. raise InvalidConfig(f'Invalid map {key!r} IRC channel: too long')
  175. # For backward compatibility, 'auth' gets treated as 'postauth'
  176. if 'auth' in map_:
  177. if 'postauth' in map_:
  178. raise InvalidConfig(f'auth and postauth are aliases and cannot be used together')
  179. warnings.warn('auth is deprecated, use postauth instead', DeprecationWarning)
  180. map_['postauth'] = map_['auth']
  181. del map_['auth']
  182. for k in ('postauth', 'getauth'):
  183. if k not in map_:
  184. continue
  185. if map_[k] is not False and not isinstance(map_[k], str):
  186. raise InvalidConfig(f'Invalid map {key!r} {k}: must be false or a string')
  187. if isinstance(map_[k], str) and ':' not in map_[k]:
  188. raise InvalidConfig(f'Invalid map {key!r} {k}: must contain a colon')
  189. if 'module' in map_:
  190. # If the path is relative, try to evaluate it relative to either the config file or this file; some modules are in the repo, but this also allows overriding them.
  191. for basePath in (os.path.dirname(self._filename), os.path.dirname(__file__)):
  192. if os.path.isfile(os.path.join(basePath, map_['module'])):
  193. map_['module'] = os.path.abspath(os.path.join(basePath, map_['module']))
  194. break
  195. else:
  196. raise InvalidConfig(f'Module {map_["module"]!r} in map {key!r} is not a file')
  197. if 'moduleargs' in map_:
  198. if not isinstance(map_['moduleargs'], list):
  199. raise InvalidConfig(f'Invalid module args for {key!r}: not an array')
  200. if 'module' not in map_:
  201. raise InvalidConfig(f'Module args cannot be specified without a module for {key!r}')
  202. if 'overlongmode' in map_:
  203. if not isinstance(map_['overlongmode'], str):
  204. raise InvalidConfig(f'Invalid map {key!r} overlongmode: not a string')
  205. if map_['overlongmode'] not in ('split', 'truncate'):
  206. raise InvalidConfig(f'Invalid map {key!r} overlongmode: unsupported value')
  207. # Default values
  208. finalObj = {
  209. 'logging': {'level': 'INFO', 'format': '{asctime} {levelname} {name} {message}'},
  210. 'irc': {'host': 'irc.hackint.org', 'port': 6697, 'ssl': 'yes', 'family': 0, 'nick': 'h2ibot', 'real': 'I am an http2irc bot.', 'certfile': None, 'certkeyfile': None},
  211. 'web': {'host': '127.0.0.1', 'port': 8080, 'maxrequestsize': 1048576},
  212. 'maps': {}
  213. }
  214. # Fill in default values for the maps
  215. for key, map_ in obj['maps'].items():
  216. # webpath is already set above for duplicate checking
  217. # ircchannel is set above for validation
  218. if 'postauth' not in map_:
  219. map_['postauth'] = False
  220. if 'getauth' not in map_:
  221. map_['getauth'] = False
  222. if 'module' not in map_:
  223. map_['module'] = None
  224. if 'moduleargs' not in map_:
  225. map_['moduleargs'] = []
  226. if 'overlongmode' not in map_:
  227. map_['overlongmode'] = 'split'
  228. # Load modules
  229. modulePaths = {} # path: str -> (extraargs: int, key: str)
  230. for key, map_ in obj['maps'].items():
  231. if map_['module'] is not None:
  232. if map_['module'] not in modulePaths:
  233. modulePaths[map_['module']] = (len(map_['moduleargs']), key)
  234. elif modulePaths[map_['module']][0] != len(map_['moduleargs']):
  235. raise InvalidConfig(f'Module {map_["module"]!r} process function extra argument inconsistency between {key!r} and {modulePaths[map_["module"]][1]!r}')
  236. modules = {} # path: str -> module: module
  237. for i, (path, (extraargs, _)) in enumerate(modulePaths.items()):
  238. try:
  239. # Build a name that is virtually guaranteed to be unique across a process.
  240. # Although importlib does not seem to perform any caching as of CPython 3.8, this is not guaranteed by spec.
  241. spec = importlib.util.spec_from_file_location(f'http2irc-module-{id(self)}-{i}', path)
  242. module = importlib.util.module_from_spec(spec)
  243. spec.loader.exec_module(module)
  244. except Exception as e: # This is ugly, but exec_module can raise virtually any exception
  245. raise InvalidConfig(f'Loading module {path!r} failed: {e!s}')
  246. if not hasattr(module, 'process'):
  247. raise InvalidConfig(f'Module {path!r} does not have a process function')
  248. if not inspect.iscoroutinefunction(module.process):
  249. raise InvalidConfig(f'Module {path!r} process attribute is not a coroutine function')
  250. nargs = len(inspect.signature(module.process).parameters)
  251. if nargs != 1 + extraargs:
  252. raise InvalidConfig(f'Module {path!r} process function takes {nargs} parameter{"s" if nargs > 1 else ""}, not {1 + extraargs}')
  253. modules[path] = module
  254. # Replace module value in maps
  255. for map_ in obj['maps'].values():
  256. if 'module' in map_ and map_['module'] is not None:
  257. map_['module'] = modules[map_['module']]
  258. # Merge in what was read from the config file and set keys on self
  259. for key in ('logging', 'irc', 'web', 'maps'):
  260. if key in obj:
  261. finalObj[key].update(obj[key])
  262. self[key] = finalObj[key]
  263. def __repr__(self):
  264. return f'<Config(logging={self["logging"]!r}, irc={self["irc"]!r}, web={self["web"]!r}, maps={self["maps"]!r})>'
  265. def reread(self):
  266. return Config(self._filename)
  267. class MessageQueue:
  268. # An object holding onto the messages received over HTTP for sending to IRC
  269. # This is effectively a reimplementation of parts of asyncio.Queue with some specific additional code.
  270. # Unfortunately, asyncio.Queue's extensibility (_init, _put, and _get methods) is undocumented, so I don't want to rely on that.
  271. # Differences to asyncio.Queue include:
  272. # - No maxsize
  273. # - No put coroutine (not necessary since the queue can never be full)
  274. # - Only one concurrent getter
  275. # - putleft_nowait to put to the front of the queue (so that the IRC client can put a message back when delivery fails)
  276. logger = logging.getLogger('http2irc.MessageQueue')
  277. def __init__(self):
  278. self._getter = None # None | asyncio.Future
  279. self._queue = collections.deque()
  280. async def get(self):
  281. if self._getter is not None:
  282. raise RuntimeError('Cannot get concurrently')
  283. if len(self._queue) == 0:
  284. self._getter = asyncio.get_running_loop().create_future()
  285. self.logger.debug('Awaiting getter')
  286. try:
  287. await self._getter
  288. except asyncio.CancelledError:
  289. self.logger.debug('Cancelled getter')
  290. self._getter = None
  291. raise
  292. self.logger.debug('Awaited getter')
  293. self._getter = None
  294. # For testing the cancellation/putting back onto the queue
  295. #self.logger.debug('Delaying message queue get')
  296. #await asyncio.sleep(3)
  297. #self.logger.debug('Done delaying')
  298. return self.get_nowait()
  299. def get_nowait(self):
  300. if len(self._queue) == 0:
  301. raise asyncio.QueueEmpty
  302. return self._queue.popleft()
  303. def put_nowait(self, item):
  304. self._queue.append(item)
  305. if self._getter is not None and not self._getter.cancelled():
  306. self._getter.set_result(None)
  307. def putleft_nowait(self, *item):
  308. self._queue.extendleft(reversed(item))
  309. if self._getter is not None and not self._getter.cancelled():
  310. self._getter.set_result(None)
  311. def qsize(self):
  312. return len(self._queue)
  313. class IRCClientProtocol(asyncio.Protocol):
  314. logger = logging.getLogger('http2irc.IRCClientProtocol')
  315. def __init__(self, http2ircMessageQueue, irc2httpBroadcaster, connectionClosedEvent, loop, config, channels):
  316. self.http2ircMessageQueue = http2ircMessageQueue
  317. self.irc2httpBroadcaster = irc2httpBroadcaster
  318. self.connectionClosedEvent = connectionClosedEvent
  319. self.loop = loop
  320. self.config = config
  321. self.lastRecvTime = None
  322. self.lastSentTime = None # float timestamp or None; the latter disables the send rate limit
  323. self.sendQueue = asyncio.Queue()
  324. self.buffer = b''
  325. self.connected = False
  326. self.channels = channels # Currently joined/supposed-to-be-joined channels; set(str)
  327. self.kickedChannels = set() # Channels the bot got KICKed from (for re-INVITE purposes; reset on config reloading)
  328. self.unconfirmedMessages = []
  329. self.pongReceivedEvent = asyncio.Event()
  330. self.sasl = bool(self.config['irc']['certfile'] and self.config['irc']['certkeyfile'])
  331. self.authenticated = False
  332. self.server = ircstates.Server(self.config['irc']['host'])
  333. self.capReqsPending = set() # Capabilities requested from the server but not yet ACKd or NAKd
  334. self.caps = set() # Capabilities acknowledged by the server
  335. self.whoxQueue = collections.deque() # Names of channels that were joined successfully but for which no WHO (WHOX) query was sent yet
  336. self.whoxChannel = None # Name of channel for which a WHO query is currently running
  337. self.whoxReply = [] # List of (nickname, account) tuples from the currently running WHO query
  338. self.whoxStartTime = None
  339. self.userChannels = collections.defaultdict(set) # List of which channels a user is known to be in; nickname:str -> {channel:str, ...}
  340. @staticmethod
  341. def nick_command(nick: str):
  342. return b'NICK ' + nick.encode('utf-8')
  343. @staticmethod
  344. def user_command(nick: str, real: str):
  345. nickb = nick.encode('utf-8')
  346. return b'USER ' + nickb + b' ' + nickb + b' ' + nickb + b' :' + real.encode('utf-8')
  347. def connection_made(self, transport):
  348. self.logger.info('IRC connected')
  349. self.transport = transport
  350. self.connected = True
  351. caps = [b'multi-prefix', b'userhost-in-names', b'away-notify', b'account-notify', b'extended-join']
  352. if self.sasl:
  353. caps.append(b'sasl')
  354. for cap in caps:
  355. self.capReqsPending.add(cap.decode('ascii'))
  356. self.send(b'CAP REQ :' + cap)
  357. self.send(self.nick_command(self.config['irc']['nick']))
  358. self.send(self.user_command(self.config['irc']['nick'], self.config['irc']['real']))
  359. def _send_join_part(self, command, channels):
  360. '''Split a JOIN or PART into multiple messages as necessary'''
  361. # command: b'JOIN' or b'PART'; channels: set[str]
  362. channels = [x.encode('utf-8') for x in channels]
  363. if len(command) + sum(1 + len(x) for x in channels) <= 510: # Total length = command + (separator + channel name for each channel, where the separator is a space for the first and then a comma)
  364. # Everything fits into one command.
  365. self.send(command + b' ' + b','.join(channels))
  366. return
  367. # List too long, need to split.
  368. limit = 510 - len(command)
  369. lengths = [1 + len(x) for x in channels] # separator + channel name
  370. chanLengthAcceptable = [l <= limit for l in lengths]
  371. if not all(chanLengthAcceptable):
  372. # There are channel names that are too long to even fit into one message on their own; filter them out and warn about them.
  373. # This should never happen since the config reader would already filter it out.
  374. tooLongChannels = [x for x, a in zip(channels, chanLengthAcceptable) if not a]
  375. channels = [x for x, a in zip(channels, chanLengthAcceptable) if a]
  376. lengths = [l for l, a in zip(lengths, chanLengthAcceptable) if a]
  377. for channel in tooLongChannels:
  378. self.logger.warning(f'Cannot {command} {channel}: name too long')
  379. runningLengths = list(itertools.accumulate(lengths)) # entry N = length of all entries up to and including channel N, including separators
  380. offset = 0
  381. while channels:
  382. i = next((x[0] for x in enumerate(runningLengths) if x[1] - offset > limit), -1)
  383. if i == -1: # Last batch
  384. i = len(channels)
  385. self.send(command + b' ' + b','.join(channels[:i]))
  386. offset = runningLengths[i-1]
  387. channels = channels[i:]
  388. runningLengths = runningLengths[i:]
  389. def update_channels(self, channels: set):
  390. channelsToPart = self.channels - channels
  391. channelsToJoin = channels - self.channels
  392. self.channels = channels
  393. self.kickedChannels = set()
  394. if self.connected:
  395. if channelsToPart:
  396. self._send_join_part(b'PART', channelsToPart)
  397. if channelsToJoin:
  398. self._send_join_part(b'JOIN', channelsToJoin)
  399. def send(self, data):
  400. self.logger.debug(f'Queueing for send: {data!r}')
  401. if len(data) > 510:
  402. raise RuntimeError(f'IRC message too long ({len(data)} > 510): {data!r}')
  403. self.sendQueue.put_nowait(data)
  404. def _direct_send(self, data):
  405. self.logger.debug(f'Send: {data!r}')
  406. time_ = time.time()
  407. self.transport.write(data + b'\r\n')
  408. if data.startswith(b'PRIVMSG '):
  409. # Send own messages to broadcaster as well
  410. command, channels, message = data.decode('utf-8').split(' ', 2)
  411. for channel in channels.split(','):
  412. assert channel.startswith('#') or channel.startswith('&'), f'invalid channel: {channel!r}'
  413. try:
  414. modes = self.get_mode_chars(self.server.channels[self.server.casefold(channel)].users.get(self.server.casefold(self.server.nickname)))
  415. except KeyError:
  416. # E.g. when kicked from a channel in the config
  417. # If the target channel isn't in self.server.channels, this *should* mean that the bot is not in the channel.
  418. # Therefore, don't send this to the broadcaster either.
  419. # TODO: Use echo-message instead and forward that to the broadcaster instead of emulating it here. Requires support from the network though...
  420. continue
  421. user = {
  422. 'nick': self.server.nickname,
  423. 'hostmask': f'{self.server.nickname}!{self.server.username}@{self.server.hostname}',
  424. 'account': self.server.account,
  425. 'modes': modes,
  426. }
  427. self.irc2httpBroadcaster.send(channel, {'time': time_, 'command': command, 'channel': channel, 'user': user, 'message': message})
  428. return time_
  429. async def send_queue(self):
  430. while True:
  431. self.logger.debug('Trying to get data from send queue')
  432. t = asyncio.create_task(self.sendQueue.get())
  433. done, pending = await wait_cancel_pending({t, asyncio.create_task(self.connectionClosedEvent.wait())}, return_when = asyncio.FIRST_COMPLETED)
  434. if self.connectionClosedEvent.is_set():
  435. break
  436. assert t in done, f'{t!r} is not in {done!r}'
  437. data = t.result()
  438. self.logger.debug(f'Got {data!r} from send queue')
  439. now = time.time()
  440. if self.lastSentTime is not None and now - self.lastSentTime < 1:
  441. self.logger.debug(f'Rate limited')
  442. await wait_cancel_pending({asyncio.create_task(self.connectionClosedEvent.wait())}, timeout = self.lastSentTime + 1 - now)
  443. if self.connectionClosedEvent.is_set():
  444. break
  445. time_ = self._direct_send(data)
  446. if self.lastSentTime is not None:
  447. self.lastSentTime = time_
  448. async def _get_message(self):
  449. self.logger.debug(f'Message queue {id(self.http2ircMessageQueue)} length: {self.http2ircMessageQueue.qsize()}')
  450. messageFuture = asyncio.create_task(self.http2ircMessageQueue.get())
  451. done, pending = await wait_cancel_pending({asyncio.create_task(self.connectionClosedEvent.wait())}, paws = {messageFuture}, return_when = asyncio.FIRST_COMPLETED)
  452. if self.connectionClosedEvent.is_set():
  453. if messageFuture in pending:
  454. self.logger.debug('Cancelling messageFuture')
  455. messageFuture.cancel()
  456. try:
  457. await messageFuture
  458. except asyncio.CancelledError:
  459. self.logger.debug('Cancelled messageFuture')
  460. pass
  461. else:
  462. # messageFuture is already done but we're stopping, so put the result back onto the queue
  463. self.http2ircMessageQueue.putleft_nowait(messageFuture.result())
  464. return None, None, None
  465. assert messageFuture in done, 'Invalid state: messageFuture not in done futures'
  466. return messageFuture.result()
  467. def _self_usermask_length(self):
  468. if not self.server.nickname or not self.server.username or not self.server.hostname:
  469. return 100
  470. return len(self.server.nickname) + 1 + len(self.server.username) + 1 + len(self.server.hostname) # nickname!username@hostname
  471. async def send_messages(self):
  472. while self.connected:
  473. self.logger.debug(f'Trying to get a message')
  474. channel, message, overlongmode = await self._get_message()
  475. self.logger.debug(f'Got message: {message!r}')
  476. if message is None:
  477. break
  478. channelB = channel.encode('utf-8')
  479. messageB = message.encode('utf-8')
  480. usermaskPrefixLength = 1 + self._self_usermask_length() + 1 # :usermask<SP>
  481. if usermaskPrefixLength + len(b'PRIVMSG ' + channelB + b' :' + messageB) > 510:
  482. # Message too long, need to split or truncate. First try to split on spaces, then on codepoints. Ideally, would use graphemes between, but that's too complicated.
  483. self.logger.debug(f'Message too long, overlongmode = {overlongmode}')
  484. prefix = b'PRIVMSG ' + channelB + b' :'
  485. prefixLength = usermaskPrefixLength + len(prefix) # Need to account for the origin prefix included by the ircd when sending to others
  486. maxMessageLength = 510 - prefixLength # maximum length of the message part within each line
  487. if overlongmode == 'truncate':
  488. maxMessageLength -= 3 # Make room for an ellipsis at the end
  489. messages = []
  490. while message:
  491. if overlongmode == 'truncate' and messages:
  492. break # Only need the first message on truncation
  493. if len(messageB) <= maxMessageLength:
  494. messages.append(message)
  495. break
  496. spacePos = messageB.rfind(b' ', 0, maxMessageLength + 1)
  497. if spacePos != -1:
  498. messages.append(messageB[:spacePos].decode('utf-8'))
  499. messageB = messageB[spacePos + 1:]
  500. message = messageB.decode('utf-8')
  501. continue
  502. # No space found, need to search for a suitable codepoint location.
  503. pMessage = message[:maxMessageLength] # at most 510 codepoints which expand to at least 510 bytes
  504. pLengths = [len(x.encode('utf-8')) for x in pMessage] # byte size of each codepoint
  505. pRunningLengths = list(itertools.accumulate(pLengths)) # byte size up to each codepoint
  506. if pRunningLengths[-1] <= maxMessageLength: # Special case: entire pMessage is short enough
  507. messages.append(pMessage)
  508. message = message[maxMessageLength:]
  509. messageB = message.encode('utf-8')
  510. continue
  511. cutoffIndex = next(x[0] for x in enumerate(pRunningLengths) if x[1] > maxMessageLength)
  512. messages.append(message[:cutoffIndex])
  513. message = message[cutoffIndex:]
  514. messageB = message.encode('utf-8')
  515. if overlongmode == 'split':
  516. for msg in reversed(messages):
  517. self.http2ircMessageQueue.putleft_nowait((channel, msg, overlongmode))
  518. elif overlongmode == 'truncate':
  519. self.http2ircMessageQueue.putleft_nowait((channel, messages[0] + '…', overlongmode))
  520. else:
  521. self.logger.info(f'Sending {message!r} to {channel!r}')
  522. self.unconfirmedMessages.append((channel, message, overlongmode))
  523. self.send(b'PRIVMSG ' + channelB + b' :' + messageB)
  524. async def confirm_messages(self):
  525. while self.connected:
  526. await wait_cancel_pending({asyncio.create_task(self.connectionClosedEvent.wait())}, return_when = asyncio.FIRST_COMPLETED, timeout = 60) # Confirm once per minute
  527. if not self.connected: # Disconnected while sleeping, can't confirm unconfirmed messages, requeue them directly
  528. self.http2ircMessageQueue.putleft_nowait(*self.unconfirmedMessages)
  529. self.unconfirmedMessages = []
  530. break
  531. if not self.unconfirmedMessages:
  532. self.logger.debug('No messages to confirm')
  533. continue
  534. self.logger.debug('Trying to confirm message delivery')
  535. self.pongReceivedEvent.clear()
  536. self._direct_send(b'PING :42')
  537. await wait_cancel_pending({asyncio.create_task(self.pongReceivedEvent.wait())}, return_when = asyncio.FIRST_COMPLETED, timeout = 5)
  538. self.logger.debug(f'Message delivery successful: {self.pongReceivedEvent.is_set()}')
  539. if not self.pongReceivedEvent.is_set():
  540. # No PONG received in five seconds, assume connection's dead
  541. self.logger.warning(f'Message delivery confirmation failed, putting {len(self.unconfirmedMessages)} messages back into the queue')
  542. self.http2ircMessageQueue.putleft_nowait(*self.unconfirmedMessages)
  543. self.transport.close()
  544. self.unconfirmedMessages = []
  545. def data_received(self, data):
  546. time_ = time.time()
  547. self.logger.debug(f'Data received: {data!r}')
  548. self.lastRecvTime = time_
  549. # If there's any data left in the buffer, prepend it to the data. Split on CRLF.
  550. # Then, process all messages except the last one (since data might not end on a CRLF) and keep the remainder in the buffer.
  551. # If data does end with CRLF, all messages will have been processed and the buffer will be empty again.
  552. if self.buffer:
  553. data = self.buffer + data
  554. messages = data.split(b'\r\n')
  555. for message in messages[:-1]:
  556. lines = self.server.recv(message + b'\r\n')
  557. assert len(lines) == 1, f'recv did not return exactly one line: {message!r} -> {lines!r}'
  558. self.message_received(time_, message, lines[0])
  559. self.server.parse_tokens(lines[0])
  560. self.buffer = messages[-1]
  561. def message_received(self, time_, message, line):
  562. self.logger.debug(f'Message received at {time_}: {message!r}')
  563. # Send to HTTP broadcaster
  564. # Note: WHOX is handled further down
  565. for d in self.line_to_dicts(time_, line):
  566. self.irc2httpBroadcaster.send(d['channel'], d)
  567. maybeTriggerWhox = False
  568. # PING/PONG
  569. if line.command == 'PING':
  570. self._direct_send(irctokens.build('PONG', line.params).format().encode('utf-8'))
  571. elif line.command == 'PONG':
  572. self.pongReceivedEvent.set()
  573. # IRCv3 and SASL
  574. elif line.command == 'CAP':
  575. if line.params[1] == 'ACK':
  576. for cap in line.params[2].split(' '):
  577. self.logger.debug(f'CAP ACK: {cap}')
  578. self.caps.add(cap)
  579. if cap == 'sasl' and self.sasl:
  580. self.send(b'AUTHENTICATE EXTERNAL')
  581. else:
  582. self.capReqsPending.remove(cap)
  583. elif line.params[1] == 'NAK':
  584. self.logger.warning(f'Failed to activate CAP(s): {line.params[2]}')
  585. for cap in line.params[2].split(' '):
  586. self.capReqsPending.remove(cap)
  587. if len(self.capReqsPending) == 0:
  588. self.send(b'CAP END')
  589. elif line.command == 'AUTHENTICATE' and line.params == ['+']:
  590. self.send(b'AUTHENTICATE +')
  591. elif line.command == ircstates.numerics.RPL_SASLSUCCESS:
  592. self.authenticated = True
  593. self.capReqsPending.remove('sasl')
  594. if len(self.capReqsPending) == 0:
  595. self.send(b'CAP END')
  596. elif line.command in ('902', ircstates.numerics.ERR_SASLFAIL, ircstates.numerics.ERR_SASLTOOLONG, ircstates.numerics.ERR_SASLABORTED, ircstates.numerics.RPL_SASLMECHS):
  597. self.logger.error('SASL error, terminating connection')
  598. self.transport.close()
  599. # NICK errors
  600. elif line.command in ('431', ircstates.numerics.ERR_ERRONEUSNICKNAME, ircstates.numerics.ERR_NICKNAMEINUSE, '436'):
  601. self.logger.error(f'Failed to set nickname: {message!r}, terminating connection')
  602. self.transport.close()
  603. # USER errors
  604. elif line.command in ('461', '462'):
  605. self.logger.error(f'Failed to register: {message!r}, terminating connection')
  606. self.transport.close()
  607. # JOIN errors
  608. elif line.command in (
  609. ircstates.numerics.ERR_TOOMANYCHANNELS,
  610. ircstates.numerics.ERR_CHANNELISFULL,
  611. ircstates.numerics.ERR_INVITEONLYCHAN,
  612. ircstates.numerics.ERR_BANNEDFROMCHAN,
  613. ircstates.numerics.ERR_BADCHANNELKEY,
  614. ):
  615. self.logger.error(f'Failed to join channel {line.params[1]}: {message!r}')
  616. errChannel = self.server.casefold(line.params[1])
  617. for channel in self.channels:
  618. if self.server.casefold(channel) == errChannel:
  619. self.channels.remove(channel)
  620. break
  621. # PART errors
  622. elif line.command == '442':
  623. self.logger.error(f'Failed to part channel: {message!r}')
  624. # JOIN/PART errors
  625. elif line.command == ircstates.numerics.ERR_NOSUCHCHANNEL:
  626. self.logger.error(f'Failed to join or part channel: {message!r}')
  627. # PRIVMSG errors
  628. elif line.command in (ircstates.numerics.ERR_NOSUCHNICK, '404', '407', '411', '412', '413', '414'):
  629. self.logger.error(f'Failed to send message: {message!r}')
  630. # Connection registration reply
  631. elif line.command == ircstates.numerics.RPL_WELCOME:
  632. self.logger.info('IRC connection registered')
  633. if self.sasl and not self.authenticated:
  634. self.logger.error('IRC connection registered but not authenticated, terminating connection')
  635. self.transport.close()
  636. return
  637. self.lastSentTime = time.time()
  638. self._send_join_part(b'JOIN', self.channels)
  639. asyncio.create_task(self.send_messages())
  640. asyncio.create_task(self.confirm_messages())
  641. # Bot getting KICKed
  642. elif line.command == 'KICK' and line.source and self.server.casefold(line.params[1]) == self.server.casefold(self.server.nickname):
  643. self.logger.warning(f'Got kicked from {line.params[0]}')
  644. kickedChannel = self.server.casefold(line.params[0])
  645. for channel in self.channels:
  646. if self.server.casefold(channel) == kickedChannel:
  647. self.channels.remove(channel)
  648. self.kickedChannels.add(channel) # Non-folded version so the set comparison in update_channels doesn't break.
  649. break
  650. # Bot getting INVITEd after a KICK
  651. elif line.command == 'INVITE' and line.source and self.server.casefold(line.params[0]) == self.server.casefold(self.server.nickname):
  652. invitedChannel = self.server.casefold(line.params[1])
  653. for channel in self.kickedChannels:
  654. if self.server.casefold(channel) == invitedChannel:
  655. self.channels.add(channel)
  656. self.kickedChannels.remove(channel)
  657. self._send_join_part(b'JOIN', {channel})
  658. break
  659. # WHOX on successful JOIN if supported to fetch account information
  660. elif line.command == 'JOIN' and self.server.isupport.whox and line.source and self.server.casefold(line.hostmask.nickname) == self.server.casefold(self.server.nickname):
  661. self.whoxQueue.extend(line.params[0].split(','))
  662. maybeTriggerWhox = True
  663. # WHOX response
  664. elif line.command == ircstates.numerics.RPL_WHOSPCRPL and line.params[1] == '042':
  665. self.whoxReply.append({'nick': line.params[4], 'hostmask': f'{line.params[4]}!{line.params[2]}@{line.params[3]}', 'account': line.params[5] if line.params[5] != '0' else None})
  666. # End of WHOX response
  667. elif line.command == ircstates.numerics.RPL_ENDOFWHO:
  668. # Patch ircstates account info; ircstates does not parse the WHOX reply itself.
  669. for entry in self.whoxReply:
  670. if entry['account']:
  671. self.server.users[self.server.casefold(entry['nick'])].account = entry['account']
  672. self.irc2httpBroadcaster.send(self.whoxChannel, {'time': time_, 'command': 'RPL_ENDOFWHO', 'channel': self.whoxChannel, 'users': self.whoxReply, 'whoxstarttime': self.whoxStartTime})
  673. self.whoxChannel = None
  674. self.whoxReply = []
  675. self.whoxStartTime = None
  676. maybeTriggerWhox = True
  677. # General fatal ERROR
  678. elif line.command == 'ERROR':
  679. self.logger.error(f'Server sent ERROR: {message!r}')
  680. self.transport.close()
  681. # Send next WHOX if appropriate
  682. if maybeTriggerWhox and self.whoxChannel is None and self.whoxQueue:
  683. self.whoxChannel = self.whoxQueue.popleft()
  684. self.whoxReply = []
  685. self.whoxStartTime = time.time() # Note, may not be the actual start time due to rate limiting
  686. self.send(b'WHO ' + self.whoxChannel.encode('utf-8') + b' c%tuhna,042')
  687. def get_mode_chars(self, channelUser):
  688. if channelUser is None:
  689. return ''
  690. prefix = self.server.isupport.prefix
  691. return ''.join(prefix.prefixes[i] for i in sorted((prefix.modes.index(c) for c in channelUser.modes if c in prefix.modes)))
  692. def line_to_dicts(self, time_, line):
  693. if line.source:
  694. sourceUser = self.server.users.get(self.server.casefold(line.hostmask.nickname)) if line.source else None
  695. get_modes = lambda channel, nick = line.hostmask.nickname: self.get_mode_chars(self.server.channels[self.server.casefold(channel)].users.get(self.server.casefold(nick)))
  696. get_user = lambda channel, withModes = True: {
  697. 'nick': line.hostmask.nickname,
  698. 'hostmask': str(line.hostmask),
  699. 'account': getattr(self.server.users.get(self.server.casefold(line.hostmask.nickname)), 'account', None),
  700. **({'modes': get_modes(channel)} if withModes else {}),
  701. }
  702. if line.command == 'JOIN':
  703. # Although servers SHOULD NOT send multiple channels in one message per the modern IRC docs <https://modern.ircdocs.horse/#join-message>, let's do the safe thing...
  704. account = {'account': line.params[-2] if line.params[-2] != '*' else None} if 'extended-join' in self.caps else {}
  705. for channel in line.params[0].split(','):
  706. # There can't be a mode set yet on the JOIN, so no need to use get_modes (which would complicate the self-join).
  707. yield {'time': time_, 'command': 'JOIN', 'channel': channel, 'user': {**get_user(channel, False), **account}}
  708. elif line.command in ('PRIVMSG', 'NOTICE'):
  709. channel = line.params[0]
  710. if channel not in self.server.channels:
  711. return
  712. if line.command == 'PRIVMSG' and line.params[1].startswith('\x01ACTION ') and line.params[1].endswith('\x01'):
  713. # CTCP ACTION (aka /me)
  714. yield {'time': time_, 'command': 'ACTION', 'channel': channel, 'user': get_user(channel), 'message': line.params[1][8:-1]}
  715. return
  716. yield {'time': time_, 'command': line.command, 'channel': channel, 'user': get_user(channel), 'message': line.params[1]}
  717. elif line.command == 'PART':
  718. for channel in line.params[0].split(','):
  719. yield {'time': time_, 'command': 'PART', 'channel': channel, 'user': get_user(channel), 'reason': line.params[1] if len(line.params) == 2 else None}
  720. elif line.command in ('QUIT', 'NICK', 'ACCOUNT'):
  721. if line.hostmask.nickname == self.server.nickname:
  722. channels = self.channels
  723. elif sourceUser is not None:
  724. channels = sourceUser.channels
  725. else:
  726. return
  727. for channel in channels:
  728. if line.command == 'QUIT':
  729. extra = {'reason': line.params[0] if len(line.params) == 1 else None}
  730. elif line.command == 'NICK':
  731. extra = {'newnick': line.params[0]}
  732. elif line.command == 'ACCOUNT':
  733. extra = {'account': line.params[0]}
  734. yield {'time': time_, 'command': line.command, 'channel': channel, 'user': get_user(channel), **extra}
  735. elif line.command == 'MODE' and line.params[0][0] in ('#', '&'):
  736. channel = line.params[0]
  737. yield {'time': time_, 'command': 'MODE', 'channel': channel, 'user': get_user(channel), 'args': line.params[1:]}
  738. elif line.command == 'KICK':
  739. channel = line.params[0]
  740. targetUser = self.server.users[self.server.casefold(line.params[1])]
  741. yield {
  742. 'time': time_,
  743. 'command': 'KICK',
  744. 'channel': channel,
  745. 'user': get_user(channel),
  746. 'targetuser': {'nick': targetUser.nickname, 'hostmask': targetUser.hostmask(), 'modes': get_modes(channel, targetUser.nickname), 'account': targetUser.account},
  747. 'reason': line.params[2] if len(line.params) == 3 else None
  748. }
  749. elif line.command == 'TOPIC':
  750. channel = line.params[0]
  751. channelObj = self.server.channels[self.server.casefold(channel)]
  752. oldTopic = {'topic': channelObj.topic, 'setter': channelObj.topic_setter, 'time': channelObj.topic_time.timestamp() if channelObj.topic_time else None} if channelObj.topic else None
  753. if line.params[1] == '':
  754. yield {'time': time_, 'command': 'TOPIC', 'channel': channel, 'user': get_user(channel), 'oldtopic': oldTopic, 'newtopic': None}
  755. else:
  756. yield {'time': time_, 'command': 'TOPIC', 'channel': channel, 'user': get_user(channel), 'oldtopic': oldTopic, 'newtopic': line.params[1]}
  757. elif line.command == ircstates.numerics.RPL_TOPIC:
  758. channel = line.params[1]
  759. yield {'time': time_, 'command': 'RPL_TOPIC', 'channel': channel, 'topic': line.params[2]}
  760. elif line.command == ircstates.numerics.RPL_TOPICWHOTIME:
  761. yield {'time': time_, 'command': 'RPL_TOPICWHOTIME', 'channel': line.params[1], 'setter': {'nick': irctokens.hostmask(line.params[2]).nickname, 'hostmask': line.params[2]}, 'topictime': int(line.params[3])}
  762. elif line.command == ircstates.numerics.RPL_ENDOFNAMES:
  763. channel = line.params[1]
  764. users = self.server.channels[self.server.casefold(channel)].users
  765. yield {'time': time_, 'command': 'NAMES', 'channel': channel, 'users': [{'nick': u.nickname, 'modes': self.get_mode_chars(u)} for u in users.values()]}
  766. async def quit(self):
  767. # The server acknowledges a QUIT by sending an ERROR and closing the connection. The latter triggers connection_lost, so just wait for the closure event.
  768. self.logger.info('Quitting')
  769. self.lastSentTime = 1.67e34 * math.pi * 1e7 # Disable sending any further messages in send_queue
  770. self._direct_send(b'QUIT :Bye')
  771. await wait_cancel_pending({asyncio.create_task(self.connectionClosedEvent.wait())}, timeout = 10)
  772. if not self.connectionClosedEvent.is_set():
  773. self.logger.error('Quitting cleanly did not work, closing connection forcefully')
  774. # Event will be set implicitly in connection_lost.
  775. self.transport.close()
  776. def connection_lost(self, exc):
  777. time_ = time.time()
  778. self.logger.info('IRC connection lost')
  779. self.connected = False
  780. self.connectionClosedEvent.set()
  781. self.irc2httpBroadcaster.send(Broadcaster.ALL_CHANNELS, {'time': time_, 'command': 'CONNLOST'})
  782. class IRCClient:
  783. logger = logging.getLogger('http2irc.IRCClient')
  784. def __init__(self, http2ircMessageQueue, irc2httpBroadcaster, config):
  785. self.http2ircMessageQueue = http2ircMessageQueue
  786. self.irc2httpBroadcaster = irc2httpBroadcaster
  787. self.config = config
  788. self.channels = {map_['ircchannel'] for map_ in config['maps'].values()}
  789. self._transport = None
  790. self._protocol = None
  791. def update_config(self, config):
  792. needReconnect = self.config['irc'] != config['irc']
  793. self.config = config
  794. if self._transport: # if currently connected:
  795. if needReconnect:
  796. self._transport.close()
  797. else:
  798. self.channels = {map_['ircchannel'] for map_ in config['maps'].values()}
  799. self._protocol.update_channels(self.channels)
  800. def _get_ssl_context(self):
  801. ctx = SSL_CONTEXTS[self.config['irc']['ssl']]
  802. if self.config['irc']['certfile'] and self.config['irc']['certkeyfile']:
  803. if ctx is True:
  804. ctx = ssl.create_default_context()
  805. if isinstance(ctx, ssl.SSLContext):
  806. ctx.load_cert_chain(self.config['irc']['certfile'], keyfile = self.config['irc']['certkeyfile'])
  807. return ctx
  808. async def run(self, loop, sigintEvent):
  809. connectionClosedEvent = asyncio.Event()
  810. while True:
  811. connectionClosedEvent.clear()
  812. try:
  813. self.logger.debug('Creating IRC connection')
  814. t = asyncio.create_task(loop.create_connection(
  815. protocol_factory = lambda: IRCClientProtocol(self.http2ircMessageQueue, self.irc2httpBroadcaster, connectionClosedEvent, loop, self.config, self.channels),
  816. host = self.config['irc']['host'],
  817. port = self.config['irc']['port'],
  818. ssl = self._get_ssl_context(),
  819. family = self.config['irc']['family'],
  820. ))
  821. # No automatic cancellation of t because it's handled manually below.
  822. done, _ = await wait_cancel_pending({asyncio.create_task(sigintEvent.wait())}, paws = {t}, return_when = asyncio.FIRST_COMPLETED, timeout = 30)
  823. if t not in done:
  824. t.cancel()
  825. await t # Raises the CancelledError
  826. self._transport, self._protocol = t.result()
  827. self.logger.debug('Starting send queue processing')
  828. sendTask = asyncio.create_task(self._protocol.send_queue()) # Quits automatically on connectionClosedEvent
  829. self.logger.debug('Waiting for connection closure or SIGINT')
  830. try:
  831. await wait_cancel_pending({asyncio.create_task(connectionClosedEvent.wait()), asyncio.create_task(sigintEvent.wait())}, return_when = asyncio.FIRST_COMPLETED)
  832. finally:
  833. self.logger.debug(f'Got connection closed {connectionClosedEvent.is_set()} / SIGINT {sigintEvent.is_set()}')
  834. if not connectionClosedEvent.is_set():
  835. self.logger.debug('Quitting connection')
  836. await self._protocol.quit()
  837. if not sendTask.done():
  838. sendTask.cancel()
  839. try:
  840. await sendTask
  841. except asyncio.CancelledError:
  842. pass
  843. self._transport = None
  844. self._protocol = None
  845. except (ConnectionError, ssl.SSLError, asyncio.TimeoutError, asyncio.CancelledError) as e:
  846. self.logger.error(f'{type(e).__module__}.{type(e).__name__}: {e!s}')
  847. await wait_cancel_pending({asyncio.create_task(sigintEvent.wait())}, timeout = 5)
  848. if sigintEvent.is_set():
  849. self.logger.debug('Got SIGINT, breaking IRC loop')
  850. break
  851. @property
  852. def lastRecvTime(self):
  853. return self._protocol.lastRecvTime if self._protocol else None
  854. class Broadcaster:
  855. ALL_CHANNELS = object() # Singleton for send's channel argument, e.g. for connection loss
  856. def __init__(self):
  857. self._queues = {}
  858. def subscribe(self, channel):
  859. queue = asyncio.Queue()
  860. if channel not in self._queues:
  861. self._queues[channel] = set()
  862. self._queues[channel].add(queue)
  863. return queue
  864. def _send(self, channel, j):
  865. for queue in self._queues[channel]:
  866. queue.put_nowait(j)
  867. def send(self, channel, d):
  868. if channel is self.ALL_CHANNELS and self._queues:
  869. channels = self._queues
  870. elif channel in self._queues:
  871. channels = [channel]
  872. else:
  873. return
  874. j = json.dumps(d, separators = (',', ':')).encode('utf-8')
  875. for channel in channels:
  876. self._send(channel, j)
  877. def unsubscribe(self, channel, queue):
  878. self._queues[channel].remove(queue)
  879. if not self._queues[channel]:
  880. del self._queues[channel]
  881. class WebServer:
  882. logger = logging.getLogger('http2irc.WebServer')
  883. def __init__(self, http2ircMessageQueue, irc2httpBroadcaster, ircClient, config):
  884. self.http2ircMessageQueue = http2ircMessageQueue
  885. self.irc2httpBroadcaster = irc2httpBroadcaster
  886. self.ircClient = ircClient
  887. self.config = config
  888. self._paths = {}
  889. # '/path' => ('#channel', postauth, getauth, module, moduleargs, overlongmode)
  890. # {post,get}auth are either False (access denied) or the HTTP header value for basic auth
  891. self._routes = [
  892. aiohttp.web.get('/status', self.get_status),
  893. aiohttp.web.post('/{path:.+}', functools.partial(self._path_request, func = self.post)),
  894. aiohttp.web.get('/{path:.+}', functools.partial(self._path_request, func = self.get)),
  895. ]
  896. self.update_config(config)
  897. self._configChanged = asyncio.Event()
  898. self.stopEvent = None
  899. def update_config(self, config):
  900. self._paths = {map_['webpath']: (
  901. map_['ircchannel'],
  902. f'Basic {base64.b64encode(map_["postauth"].encode("utf-8")).decode("utf-8")}' if map_['postauth'] else False,
  903. f'Basic {base64.b64encode(map_["getauth"].encode("utf-8")).decode("utf-8")}' if map_['getauth'] else False,
  904. map_['module'],
  905. map_['moduleargs'],
  906. map_['overlongmode']
  907. ) for map_ in config['maps'].values()}
  908. needRebind = self.config['web'] != config['web']
  909. self.config = config
  910. if needRebind:
  911. self._configChanged.set()
  912. async def run(self, stopEvent):
  913. self.stopEvent = stopEvent
  914. while True:
  915. app = aiohttp.web.Application(client_max_size = self.config['web']['maxrequestsize'])
  916. app.add_routes(self._routes)
  917. runner = aiohttp.web.AppRunner(app)
  918. await runner.setup()
  919. site = aiohttp.web.TCPSite(runner, self.config['web']['host'], self.config['web']['port'])
  920. await site.start()
  921. await wait_cancel_pending({asyncio.create_task(stopEvent.wait()), asyncio.create_task(self._configChanged.wait())}, return_when = asyncio.FIRST_COMPLETED)
  922. await runner.cleanup()
  923. if stopEvent.is_set():
  924. break
  925. self._configChanged.clear()
  926. async def get_status(self, request):
  927. self.logger.info(f'Received request {id(request)} from {request.remote!r} for {request.path!r}')
  928. return (aiohttp.web.Response if (self.ircClient.lastRecvTime or 0) > time.time() - 600 else aiohttp.web.HTTPInternalServerError)()
  929. async def _path_request(self, request, func):
  930. self.logger.info(f'Received request {id(request)} from {request.remote!r} for {request.method} {request.path!r} with body {(await request.read())!r}')
  931. try:
  932. pathConfig = self._paths[request.path]
  933. except KeyError:
  934. self.logger.info(f'Bad request {id(request)}: no path {request.path!r}')
  935. raise aiohttp.web.HTTPNotFound()
  936. auth = pathConfig[1] if request.method == 'POST' else pathConfig[2]
  937. authHeader = request.headers.get('Authorization')
  938. if not authHeader or not auth or authHeader != auth:
  939. self.logger.info(f'Bad request {id(request)}: authentication failed: {authHeader!r} != {auth}')
  940. raise aiohttp.web.HTTPForbidden()
  941. return (await func(request, *pathConfig))
  942. async def post(self, request, channel, postauth, getauth, module, moduleargs, overlongmode):
  943. if module is not None:
  944. self.logger.debug(f'Processing request {id(request)} using {module!r}')
  945. try:
  946. message = await module.process(request, *moduleargs)
  947. except aiohttp.web.HTTPException as e:
  948. raise e
  949. except Exception as e:
  950. self.logger.error(f'Bad request {id(request)}: exception in module process function: {type(e).__module__}.{type(e).__name__}: {e!s}')
  951. raise aiohttp.web.HTTPBadRequest()
  952. if message is None:
  953. self.logger.info(f'Accepted request {id(request)}, module returned None')
  954. raise aiohttp.web.HTTPOk()
  955. if '\r' in message or '\n' in message:
  956. self.logger.error(f'Bad request {id(request)}: module process function returned message with linebreaks: {message!r}')
  957. raise aiohttp.web.HTTPBadRequest()
  958. else:
  959. self.logger.debug(f'Processing request {id(request)} using default processor')
  960. message = await self._default_process(request)
  961. self.logger.info(f'Accepted request {id(request)}, putting message {message!r} for {channel} into message queue')
  962. self.http2ircMessageQueue.put_nowait((channel, message, overlongmode))
  963. raise aiohttp.web.HTTPOk()
  964. async def _default_process(self, request):
  965. try:
  966. message = await request.text()
  967. except Exception as e:
  968. self.logger.info(f'Bad request {id(request)}: exception while reading request data: {e!s}')
  969. raise aiohttp.web.HTTPBadRequest() # Yes, it's always the client's fault. :-)
  970. self.logger.debug(f'Request {id(request)} payload: {message!r}')
  971. # Strip optional [CR] LF at the end of the payload
  972. if message.endswith('\r\n'):
  973. message = message[:-2]
  974. elif message.endswith('\n'):
  975. message = message[:-1]
  976. if '\r' in message or '\n' in message:
  977. self.logger.info(f'Bad request {id(request)}: linebreaks in message')
  978. raise aiohttp.web.HTTPBadRequest()
  979. return message
  980. async def get(self, request, channel, postauth, getauth, module, moduleargs, overlongmode):
  981. self.logger.info(f'Subscribing listener from request {id(request)} for {channel}')
  982. queue = self.irc2httpBroadcaster.subscribe(channel)
  983. response = aiohttp.web.StreamResponse()
  984. response.enable_chunked_encoding()
  985. await response.prepare(request)
  986. try:
  987. while True:
  988. t = asyncio.create_task(queue.get())
  989. done, pending = await wait_cancel_pending({t, asyncio.create_task(self.stopEvent.wait()), asyncio.create_task(self._configChanged.wait())}, return_when = asyncio.FIRST_COMPLETED)
  990. if t not in done: # stopEvent or config change
  991. #TODO Don't break if the config change doesn't affect this connection
  992. break
  993. j = t.result()
  994. await response.write(j + b'\n')
  995. finally:
  996. self.irc2httpBroadcaster.unsubscribe(channel, queue)
  997. self.logger.info(f'Unsubscribed listener from request {id(request)} for {channel}')
  998. return response
  999. def configure_logging(config):
  1000. #TODO: Replace with logging.basicConfig(..., force = True) (Py 3.8+)
  1001. root = logging.getLogger()
  1002. root.setLevel(getattr(logging, config['logging']['level']))
  1003. root.handlers = [] #FIXME: Undocumented attribute of logging.Logger
  1004. formatter = logging.Formatter(config['logging']['format'], style = '{')
  1005. stderrHandler = logging.StreamHandler()
  1006. stderrHandler.setFormatter(formatter)
  1007. root.addHandler(stderrHandler)
  1008. async def main():
  1009. if len(sys.argv) != 2:
  1010. print('Usage: http2irc.py CONFIGFILE', file = sys.stderr)
  1011. sys.exit(1)
  1012. configFile = sys.argv[1]
  1013. config = Config(configFile)
  1014. configure_logging(config)
  1015. loop = asyncio.get_running_loop()
  1016. http2ircMessageQueue = MessageQueue()
  1017. irc2httpBroadcaster = Broadcaster()
  1018. irc = IRCClient(http2ircMessageQueue, irc2httpBroadcaster, config)
  1019. webserver = WebServer(http2ircMessageQueue, irc2httpBroadcaster, irc, config)
  1020. sigintEvent = asyncio.Event()
  1021. def sigint_callback():
  1022. global logger
  1023. nonlocal sigintEvent
  1024. logger.info('Got SIGINT, stopping')
  1025. sigintEvent.set()
  1026. loop.add_signal_handler(signal.SIGINT, sigint_callback)
  1027. def sigusr1_callback():
  1028. global logger
  1029. nonlocal config, irc, webserver
  1030. logger.info('Got SIGUSR1, reloading config')
  1031. try:
  1032. newConfig = config.reread()
  1033. except InvalidConfig as e:
  1034. logger.error(f'Config reload failed: {e!s} (old config remains active)')
  1035. return
  1036. config = newConfig
  1037. configure_logging(config)
  1038. irc.update_config(config)
  1039. webserver.update_config(config)
  1040. loop.add_signal_handler(signal.SIGUSR1, sigusr1_callback)
  1041. await asyncio.gather(irc.run(loop, sigintEvent), webserver.run(sigintEvent))
  1042. if __name__ == '__main__':
  1043. asyncio.run(main())