QAD Minecraft IRC bridge

While waiting for bukkit to be released for minecraft 1.3 we moved out server to the pre-release version. We were happy to roll without plugins especially as we move towards a more hardcore server. What we did miss, however, was irc integration and the interaction with Dobby.

This is a quick and very dirty python irc bridge I wrote up that connects to IRC with a socket which it parses and sends the say command to the screen minecraft is running in. It reads lines from the server.log file and passes those back to the IRC channel.

It is known to work with the foonetic network. It will need a little bit of tweaking to work on other networks.


#!/usr/bin/env python
import re
import time
import sys
import socket
import string
import threading
import os #not necassary but later on I am going to use a few features from this 

HOST=’irc.foonetic.net’ #The server we want to connect to
PORT=6667 #The connection port which is usually 6667
NICK=’MinecraftBot’ #The bot’s nickname
IDENT=’pybot’
REALNAME=’MinecraftBot’
OWNER=’owner’ #The bot owner’s nick
CHANNELINIT=’#channel’ #The default channel for the bot
readbuffer=” #Here we store all the messages from server
minecraftdir=’/pile/mc/mserv/current’
screenname=’minecraft’

s=socket.socket( ) #Create the socket
lastline = []
reg = False

def send(m):
global s
print m
s.send(m)

def parsemsg(msg):
global screen
complete=msg[1:].split(‘:’,1) #Parse the message into useful data
info=complete[0].split(‘ ‘)
msgpart=complete[1]
sender=info[0].split(‘!’)
com = ‘screen -X -S ‘+screenname+’ -p 0 stuff \”say IRC(‘+sender[0]+’): ‘+msgpart+’\r\n\”‘
print com
os.popen(com)

if msgpart[0]==’-‘ and sender[0]==OWNER : #Treat msgs with – as explicit command to send to server
cmd=msgpart[1:]
send(cmd+’\r\n’)
print ‘cmd=’+cmd

class ircloop(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def get_result(self):
return self.result

def run(self):
global s,HOST,PORT,NICK,IDENT,REALNAME,OWNER,CHANNELINIT,readbuffer,reg
try:
s.connect((HOST, PORT)) #Connect to server
s.send(‘NICK ‘+NICK+’\r\n’) #Send the nick to server
s.send(‘USER ‘+IDENT+’ ‘+HOST+’ ‘+NICK+’ :’+REALNAME+’\r\n’) #Identify to server
readbuffer=””
reg = False
while True:
time.sleep(0.1)
print “——————————–”
try:
readbuffer=readbuffer+s.recv(64)
temp=readbuffer.split(“\n”)
readbuffer=temp.pop( )
except:
temp = []
for line in temp:
line = str(line)
print line

if line.find(‘ound your hostname’)!=-1 or line.find(‘have not registered’)!= -1: #This is Crap(I wasn’t sure about it but it works)
s.send(‘NICK ‘+NICK+’\r\n’) #Send the nick to server
s.send(‘USER ‘+IDENT+’ ‘+NICK+’ ‘+NICK+’ :’+REALNAME+’\r\n’) #Identify to server
if line.find(‘Welcome to Foonetic’) != -1:
reg = True
s.send(‘MODE ‘+NICK+’ +B\r\n’)
s.send(‘JOIN ‘+CHANNELINIT+’\r\n’) #Join a channel
if line.find(‘PRIVMSG’)!=-1: #Call a parsing function
parsemsg(line)
if line.find(‘PING’) != -1: #If server pings then pong
s.send(‘PONG ‘+line.split()[1]+’\r\n’)
except IOError:
print “fail”

class logloop(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def get_result(self):
return self.result

def run(self):
global s,l,lastline,reg,minecraftdir
try:
while True:
time.sleep(0.1)
c = os.popen(“tail –lines=1 “+minecraftdir+”/server.log”)
l = c.readlines()
for e in l:
e = re.sub(“^.*?\[INFO\] “,””,e)
if re.match(“^(<|.*?lost connection|.*? logged in).*”,e):
e = re.sub(“<(\w*?)>”,”(\g)”,e)
if e not in lastline and reg:
send(‘PRIVMSG #machinespirit :’+e+’\r\n’)
if len(lastline) > 10:
lastline = lastline[:-10]
lastline.append(e)
c.close()
except IOError:
print “fail”

thread = logloop()
thread.start()
thread = ircloop()
thread.start()


Leave a Reply

Your email address will not be published. Required fields are marked *