Some code cleanup and clarification in comments.

This commit is contained in:
Griatch 2009-08-28 01:33:15 +00:00
parent 2640bd057a
commit 4bd567386f
12 changed files with 351 additions and 66 deletions

0
src/irc/__init__.py Normal file
View file

11
src/irc/admin.py Normal file
View file

@ -0,0 +1,11 @@
"""
This sets up a few fields in the admin interface for connecting IRC channels
to evennia channels.
"""
from src.irc.models import IRCChannelMapping
from django.contrib import admin
class IRCChannelMappingAdmin(admin.ModelAdmin):
list_display = ('channel', 'irc_server_name',
'irc_channel_name', 'is_enabled')
admin.site.register(IRCChannelMapping, IRCChannelMappingAdmin)

83
src/irc/connection.py Normal file
View file

@ -0,0 +1,83 @@
"""
This connects to an IRC network/channel and launches an 'bot' onto it.
The bot then pipes what is being said between the IRC channel and one or
more Evennia channels.
"""
from twisted.words.protocols import irc
from twisted.internet import protocol
from twisted.internet import reactor
from src.irc.models import IRCChannelMapping
from src import comsys
from src import logger
#store all irc channels
IRC_CHANNELS = []
class IRC_Bot(irc.IRCClient):
def _get_nickname(self):
"required for correct nickname setting"
return self.factory.nickname
nickname = property(_get_nickname)
def signedOn(self):
global IRC_CHANNELS
self.join(self.factory.channel)
# This is the first point the protocol is instantiated.
# add this protocol instance to the global list so we
# can access it later to send data.
IRC_CHANNELS.append(self)
logger.log_infomsg("IRC: Client connecting to %s.'" % (self.factory.channel))
def joined(self, channel):
logger.log_infomsg("Joined %s/%s as '%s'." % (self.factory.network,channel,self.factory.nickname))
def privmsg(self, user, irc_channel, msg):
"Someone has written something in channel. Echo it to the evennia channel"
print "got msg: %s" % msg
try:
#find irc->evennia channel mappings
mappings = IRCChannelMapping.objects.filter(irc_channel_name=irc_channel)
if not mappings:
return
#format message:
user = user.split("!")[0]
if user:
user.strip()
msg = "%s@%s: %s" % (user,irc_channel,msg)
logger.log_infomsg("<IRC: " + msg)
for mapping in mappings:
if mapping.channel:
comsys.send_cmessage(mapping.channel, msg, from_external="IRC")
except IRCChannelMapping.DoesNotExist:
#no mappings found. Ignore.
pass
def send_msg(self,msg):
"Called by evennia when sending something to mapped IRC channel"
self.msg(self.factory.channel, msg)
logger.log_infomsg(">IRC: " + msg)
class IRC_BotFactory(protocol.ClientFactory):
protocol = IRC_Bot
def __init__(self, channel, network, nickname):
self.network = network
self.channel = channel
self.nickname = nickname
def clientConnectionLost(self, connector, reason):
logger.log_errmsg("IRC: Lost connection (%s), reconnecting." % reason)
connector.connect()
def clientConnectionFailed(self, connector, reason):
logger.log_errmsg("IRC: Could not connect: %s" % reason)
def connect_to_IRC(irc_network,irc_port,irc_channel,irc_bot_nick ):
"Create the bot instance and connect to the IRC network and channel."
connect = reactor.connectTCP(irc_network, irc_port,
IRC_BotFactory(irc_channel,irc_network,irc_bot_nick))

20
src/irc/models.py Normal file
View file

@ -0,0 +1,20 @@
from django.db import models
from src.channels.models import CommChannel
class IRCChannelMapping(models.Model):
"""
Each IRCChannelMapping object determines which in-game channel incoming
IRC messages are routed to.
"""
channel = models.ForeignKey(CommChannel)
irc_server_name = models.CharField(max_length=78)
irc_channel_name = models.CharField(max_length=78)
is_enabled = models.BooleanField(default=True)
class Meta:
verbose_name = "IRC Channel mapping"
verbose_name_plural = "IRC Channel mappings"
def __str__(self):
return "%s <-> %s (%s)" % (self.channel, self.irc_channel_name,
self.irc_server_name)