pyircbot/pyircbot/irccore.py

404 lines
14 KiB
Python
Raw Normal View History

"""
.. module:: IRCCore
:synopsis: IRC protocol class
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
"""
import socket
import asyncio
import logging
import traceback
2015-08-08 15:04:45 -07:00
import sys
2015-08-08 22:50:04 -07:00
from inspect import getargspec
from pyircbot.common import burstbucket, parse_irc_line
2016-11-06 15:00:15 -08:00
from collections import namedtuple
from io import StringIO
IRCEvent = namedtuple("IRCEvent", "command args prefix trailing")
2016-11-06 15:00:15 -08:00
UserPrefix = namedtuple("UserPrefix", "nick username hostname")
ServerPrefix = namedtuple("ServerPrefix", "hostname")
class IRCCore(object):
2016-11-06 15:00:15 -08:00
2017-11-24 16:06:55 -08:00
def __init__(self, servers, loop, rate_limit=True, rate_max=5.0, rate_int=1.1):
self._loop = loop
# rate limiting options
self.rate_limit = rate_limit
self.rate_max = float(rate_max)
self.rate_int = float(rate_int)
2016-11-06 15:00:15 -08:00
2017-12-03 20:54:39 -08:00
self.reconnect_delay = 3.0
2017-01-01 14:59:01 -08:00
self.connected = False
2015-08-08 15:04:45 -07:00
"""If we're connected or not"""
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
self.log = logging.getLogger('IRCCore')
"""Reference to logger object"""
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
self.buffer = StringIO()
"""cStringIO used as a buffer"""
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
self.alive = True
"""True if we should try to stay connected"""
2016-11-06 15:00:15 -08:00
self.server = 0
"""Current server index"""
self.servers = servers
"""List of server address"""
2015-08-08 15:04:45 -07:00
self.port = 0
"""Server port"""
2017-04-26 23:07:47 -07:00
self.connection_family = socket.AF_UNSPEC
"""Socket family. 0 will auto-detect ipv4 or v6. Change this to socket.AF_INET or socket.AF_INET6 force use of
ipv4 or ipv6."""
self.bind_addr = None
"""Optionally bind to a specific address. This should be a (host, port) tuple."""
2016-11-06 15:00:15 -08:00
self.nick = None
2015-08-08 15:04:45 -07:00
# Set up hooks for modules
self.initHooks()
2016-11-06 15:00:15 -08:00
self.outseq = 5
self.outputq = asyncio.PriorityQueue()
2017-12-03 20:54:39 -08:00
self._loop.call_soon_threadsafe(asyncio.ensure_future, self.outputqueue())
2017-11-24 16:06:55 -08:00
async def loop(self, loop):
while self.alive:
try:
# TODO support ipv6 again
self.reader, self.writer = await asyncio.open_connection(self.servers[self.server][0],
port=self.servers[self.server][1],
loop=loop,
2017-04-26 23:07:47 -07:00
ssl=None,
family=self.connection_family,
local_addr=self.bind_addr)
self.fire_hook("_CONNECT")
except (socket.gaierror, ConnectionRefusedError):
traceback.print_exc()
logging.warning("Non-fatal connect error, trying next server...")
self.server = (self.server + 1) % len(self.servers)
await asyncio.sleep(1, loop=loop)
continue
while self.alive:
try:
data = await self.reader.readuntil()
self.log.debug("<<< {}".format(repr(data)))
command, args, prefix, trailing = parse_irc_line(data.decode("UTF-8"))
self.fire_hook("_RECV", args=args, prefix=prefix, trailing=trailing)
if command not in self.hookcalls:
self.log.warning("Unknown command: cmd='{}' prefix='{}' args='{}' trailing='{}'"
.format(command, prefix, args, trailing))
else:
self.fire_hook(command, args=args, prefix=prefix, trailing=trailing)
except (ConnectionResetError, asyncio.streams.IncompleteReadError):
traceback.print_exc()
break
2017-10-12 23:35:42 -07:00
except (UnicodeDecodeError, ):
traceback.print_exc()
self.fire_hook("_DISCONNECT")
self.writer.close()
if self.alive:
# TODO ramp down reconnect attempts
2017-12-03 20:54:39 -08:00
logging.info("Reconnecting in {}s...".format(self.reconnect_delay))
await asyncio.sleep(self.reconnect_delay)
2017-11-24 16:06:55 -08:00
async def outputqueue(self):
self.bucket = burstbucket(self.rate_max, self.rate_int)
2017-11-24 16:06:55 -08:00
while True:
prio, line = await self.outputq.get()
# sleep until the bucket allows us to send
if self.rate_limit:
while True:
s = self.bucket.get()
2017-11-24 16:06:55 -08:00
if s == 0:
break
else:
await asyncio.sleep(s, loop=self._loop)
self.fire_hook('_SEND', args=None, prefix=None, trailing=None)
self.log.debug(">>> {}".format(repr(line)))
self.outputq.task_done()
2017-11-24 16:06:55 -08:00
try:
self.writer.write((line + "\r\n").encode("UTF-8"))
except Exception as e: # Probably fine if we drop messages while offline
print(e)
print(self.trace())
async def kill(self, message="Help! Another thread is killing me :(", forever=True):
"""Send quit message, flush queue, and close the socket
2016-11-06 15:00:15 -08:00
:param message: Quit message to send before disconnecting
:type message: str
"""
if forever:
self.alive = False
2017-01-01 14:59:01 -08:00
self.act_QUIT(message) # TODO will this hang if the socket is having issues?
await self.writer.drain()
self.writer.close()
self.log.info("Kill complete")
2016-11-06 15:00:15 -08:00
def sendRaw(self, data, priority=None):
"""
Send data on the wire. Lower priorities are sent first.
:param data: unicode data to send. will be converted to utf-8
:param priority: numerical priority value. If not None, the message will likely be sent first. Otherwise, an
ever-increasing sequence number is used to maintain order. For a minimum priority message,
use a priority value of sys.maxsize.
"""
if priority is None:
self.outseq += 1
priority = self.outseq
asyncio.run_coroutine_threadsafe(self.outputq.put((priority, data, )), self._loop)
2015-08-08 15:04:45 -07:00
" Module related code "
def initHooks(self):
"""Defines hooks that modules can listen for events of"""
self.hooks = [
2017-11-27 18:58:20 -08:00
'_ALL',
'_CONNECT',
'_DISCONNECT',
'_RECV',
'_SEND',
'NOTICE',
'MODE',
'PING',
'JOIN',
'QUIT',
'NICK',
'PART',
'PRIVMSG',
'KICK',
'INVITE',
'001',
'002',
'003',
'004',
'005',
'250',
'251',
'252',
'254',
'255',
'265',
'266',
2017-12-03 17:54:00 -08:00
'331',
'332',
'333',
'353',
'366',
'372',
'375',
'376',
2017-12-03 17:54:00 -08:00
'401',
'422',
'433',
2015-08-08 15:04:45 -07:00
]
" mapping of hooks to methods "
2017-01-01 14:59:01 -08:00
self.hookcalls = {command: [] for command in self.hooks}
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
def fire_hook(self, command, args=None, prefix=None, trailing=None):
"""Run any listeners for a specific hook
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param command: the hook to fire
:type command: str
:param args: the list of arguments, if any, the command was passed
:type args: list
:param prefix: prefix of the sender of this command
:type prefix: str
:param trailing: data payload of the command
:type trailing: str"""
2017-11-27 18:58:20 -08:00
for hook in self.hookcalls["_ALL"] + self.hookcalls[command]:
2015-08-08 15:04:45 -07:00
try:
2015-08-08 22:50:04 -07:00
if len(getargspec(hook).args) == 2:
hook(IRCCore.packetAsObject(command, args, prefix, trailing))
2015-08-08 22:50:04 -07:00
else:
hook(args, prefix, trailing)
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
except:
2017-01-01 14:59:01 -08:00
self.log.warning("Error processing hook: \n%s" % self.trace())
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
def addHook(self, command, method):
"""**Internal.** Enable (connect) a single hook of a module
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param command: command this hook will trigger on
:type command: str
:param method: callable method object to hook in
:type method: object"""
" add a single hook "
if command in self.hooks:
self.hookcalls[command].append(method)
else:
self.log.warning("Invalid hook - %s" % command)
return False
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
def removeHook(self, command, method):
"""**Internal.** Disable (disconnect) a single hook of a module
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param command: command this hook triggers on
:type command: str
:param method: callable method that should be removed
:type method: object"""
" remove a single hook "
if command in self.hooks:
for hookedMethod in self.hookcalls[command]:
if hookedMethod == method:
self.hookcalls[command].remove(hookedMethod)
else:
self.log.warning("Invalid hook - %s" % command)
return False
2016-11-06 15:00:15 -08:00
def packetAsObject(command, args, prefix, trailing):
2015-08-08 22:50:04 -07:00
"""Given an irc message's args, prefix, and trailing data return an object with these properties
2016-11-06 15:00:15 -08:00
2015-08-08 22:50:04 -07:00
:param args: list of args from the IRC packet
:type args: list
:param prefix: prefix object parsed from the IRC packet
:type prefix: ServerPrefix or UserPrefix
:param trailing: trailing data from the IRC packet
:type trailing: str
:returns: object -- a IRCEvent object with the ``args``, ``prefix``, ``trailing``"""
2016-11-06 15:00:15 -08:00
return IRCEvent(command, args,
2016-11-06 15:00:15 -08:00
IRCCore.decodePrefix(prefix) if prefix else None,
trailing)
2015-08-08 15:04:45 -07:00
" Utility methods "
@staticmethod
def decodePrefix(prefix):
"""Given a prefix like nick!username@hostname, return an object with these properties
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param prefix: the prefix to disassemble
:type prefix: str
2017-01-01 14:59:01 -08:00
:returns: object -- an UserPrefix object with the properties `nick`, `username`, `hostname` or a ServerPrefix
object with the property `hostname`
"""
2015-08-08 15:04:45 -07:00
if "!" in prefix:
2016-11-06 15:00:15 -08:00
nick, prefix = prefix.split("!")
username, hostname = prefix.split("@")
return UserPrefix(nick, username, hostname)
2015-08-08 15:04:45 -07:00
else:
2016-11-06 15:00:15 -08:00
return ServerPrefix(prefix)
2015-08-08 15:04:45 -07:00
@staticmethod
2015-08-08 21:48:51 -07:00
def trace():
"""Return the stack trace of the bot as a string"""
return traceback.format_exc()
2016-11-06 15:00:15 -08:00
@staticmethod
def fulltrace():
2015-08-08 15:04:45 -07:00
"""Return the stack trace of the bot as a string"""
result = ""
result += "\n*** STACKTRACE - START ***\n"
code = []
for threadId, stack in sys._current_frames().items():
code.append("\n# ThreadID: %s" % threadId)
for filename, lineno, name, line in traceback.extract_stack(stack):
code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
if line:
code.append(" %s" % (line.strip()))
for line in code:
result += line + "\n"
result += "\n*** STACKTRACE - END ***\n"
return result
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
" Data Methods "
def get_nick(self):
"""Get the bot's current nick
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:returns: str - the bot's current nickname"""
return self.nick
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
" Action Methods "
def act_PONG(self, data):
"""Use the `/pong` command - respond to server pings
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param data: the string or number the server sent with it's ping
:type data: str"""
self.sendRaw("PONG :%s" % data)
2016-11-06 15:00:15 -08:00
def act_USER(self, username, hostname, realname, priority=2):
2015-08-08 15:04:45 -07:00
"""Use the USER protocol command. Used during connection
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param username: the bot's username
:type username: str
:param hostname: the bot's hostname
:type hostname: str
:param realname: the bot's realname
:type realname: str"""
self.sendRaw("USER %s %s %s :%s" % (username, hostname, self.servers[self.server], realname), priority)
2016-11-06 15:00:15 -08:00
def act_NICK(self, newNick, priority=2):
2015-08-08 15:04:45 -07:00
"""Use the `/nick` command
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param newNick: new nick for the bot
:type newNick: str"""
self.nick = newNick
self.sendRaw("NICK %s" % newNick, priority)
2016-11-06 15:00:15 -08:00
def act_JOIN(self, channel, priority=3):
2015-08-08 15:04:45 -07:00
"""Use the `/join` command
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param channel: the channel to attempt to join
:type channel: str"""
self.sendRaw("JOIN %s" % channel, priority=3)
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
def act_PRIVMSG(self, towho, message):
"""Use the `/msg` command
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param towho: the target #channel or user's name
:type towho: str
:param message: the message to send
:type message: str"""
2017-01-01 14:59:01 -08:00
self.sendRaw("PRIVMSG %s :%s" % (towho, message))
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
def act_MODE(self, channel, mode, extra=None):
"""Use the `/mode` command
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param channel: the channel this mode is for
:type channel: str
:param mode: the mode string. Example: +b
:type mode: str
:param extra: additional argument if the mode needs it. Example: user@*!*
:type extra: str"""
2017-01-01 14:59:01 -08:00
if extra is not None:
self.sendRaw("MODE %s %s %s" % (channel, mode, extra))
2015-08-08 15:04:45 -07:00
else:
2017-01-01 14:59:01 -08:00
self.sendRaw("MODE %s %s" % (channel, mode))
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
def act_ACTION(self, channel, action):
"""Use the `/me <action>` command
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param channel: the channel name or target's name the message is sent to
:type channel: str
:param action: the text to send
:type action: str"""
2017-01-01 14:59:01 -08:00
self.sendRaw("PRIVMSG %s :\x01ACTION %s" % (channel, action))
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
def act_KICK(self, channel, who, comment=""):
"""Use the `/kick <user> <message>` command
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param channel: the channel from which the user will be kicked
:type channel: str
:param who: the nickname of the user to kick
:type action: str
:param comment: the kick message
:type comment: str"""
self.sendRaw("KICK %s %s :%s" % (channel, who, comment))
2016-11-06 15:00:15 -08:00
def act_QUIT(self, message, priority=2):
2015-08-08 15:04:45 -07:00
"""Use the `/quit` command
2016-11-06 15:00:15 -08:00
2015-08-08 15:04:45 -07:00
:param message: quit message
:type message: str"""
self.sendRaw("QUIT :%s" % message, priority)
2016-11-06 15:00:15 -08:00
2017-01-01 14:59:01 -08:00
def act_PASS(self, password):
"""
Send server password, for use on connection
"""
self.sendRaw("PASS %s" % password)