Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support concept of fallback encoding #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions nanobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,26 @@ def remote_leave(self, channel, reason):
class NanoBotProtocol(irc.IRCClient):
ping_delay = 180

def __init__(self, reactor, server, bot):
def __init__(self, reactor, server, bot, fallback_encoding):
self._reactor = reactor
self.server = server
self.bot = bot
self.fallback_encoding = fallback_encoding

@property
def channels(self):
return self.server.channels

def lineReceived(self, line):
try:
text = line.decode("utf-8")
except UnicodeDecodeError:
if self.fallback_encoding is not None:
text = line.decode(fallback_encoding)
else:
text = line.decode("utf-8", "ignore")
super().lineReceived(text)

def connectionMade(self):
irc.IRCClient.connectionMade(self)
log.info(f"Connected to {self.server.hostname}")
Expand Down Expand Up @@ -72,10 +83,11 @@ def __init__(self, reactor, network_config, bot):
self._reactor = reactor
self.bot = bot
self.network_config = network_config
self.encoding = self.network_config.get('encoding', None)
self.fallback_encoding = self.network_config.get('fallback_encoding', None)

def buildProtocol(self, addr):
protocol = self.protocol(self._reactor, server=self, bot=self.bot)
protocol = self.protocol(self._reactor, server=self, bot=self.bot,
fallback_encoding=self.fallback_encoding)
if self.bot.nickname:
protocol.nickname = self.bot.nickname
if self.bot.realname:
Expand Down