Simple Python IRC bot.

By berend on Nov 15, 2012

EDIT: Changed te code to a better version

import socket
import sys
from random import randint
import re

#----------------------------------- Settings --------------------------------------#
network = 'irc.onlinegamesnet.net'
port = 6667
homechan = '#berend'
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
print irc.recv ( 4096 )
irc.send ( 'PASS *********\r\n')
irc.send ( 'NICK bython\r\n' )
irc.send ( 'USER bython bython bython :Python IRC\r\n' )
#----------------------------------------------------------------------------------#

#---------------------------------- Functions -------------------------------------#
def readAdmin(host):                        # Return status 0/1
    bestand = open('admins.txt', 'r')
    for line in bestand:
        if host in line:
            status = 1
            return status
        else:
            status = 0
            return status

def GetHost(host):                          # Return Host
    host = host.split('@')[1]
    host = host.split(' ')[0]
    return host

def GetChannel(data):                       # Return Channel
    channel = data.split('#')[1]
    channel = channel.split(':')[0]
    channel = '#' + channel
    channel = channel.strip(' \t\n\r')
    return channel

def GetNick(data):                          # Return Nickname
    nick = data.split('!')[0]
    nick = nick.replace(':', ' ')
    nick = nick.replace(' ', '')
    nick = nick.strip(' \t\n\r')
    return nick

def Send(msg):
    irc.send('PRIVMSG ' + homechan + ' : ' + msg +  '\r\n')

def Join(chan):
    irc.send ( 'JOIN ' + chan + '\r\n' )

def Part(chan):
    irc.send ( 'PART ' + chan + '\r\n' )

def Op(to_op, chan):
    irc.send( 'MODE ' + chan + ' +o: ' + to_op + '\r\n')

def DeOp(to_deop, chan):
    irc.send( 'MODE ' + chan + ' -o: ' + to_deop + '\r\n')

def Voice(to_v, chan):
    irc.send( 'MODE ' + chan + ' +v: ' + to_v + '\r\n')

def DeVoice(to_dv, chan):
    irc.send( 'MODE ' + chan + ' -v: ' + to_dv + '\r\n')
#------------------------------------------------------------------------------#

while True:
    action = 'none'
    data = irc.recv ( 4096 ) 
    print data

    if data.find ( 'Welcome to the OnlineGamesNet IRC Network' ) != -1:
            Join(homechan)

    if data.find ( 'PING' ) != -1:
            irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )

    #--------------------------- Action check --------------------------------#
    if data.find('#') != -1:
        action = data.split('#')[0]
        action = action.split(' ')[1]

    if data.find('NICK') != -1:
        if data.find('#') == -1:
            action = 'NICK'

    #----------------------------- Actions -----------------------------------#
    if action != 'none':

        if action == 'PRIVMSG':
            if data.find('$') != -1:
                x = data.split('#')[1]
                x = x.split('$')[1]
                info = x.split(' ')
                info[0] = info[0].strip(' \t\n\r')

                if info[0] == 'op':
                    host = GetHost(data)
                    status = readAdmin(host)
                    if status == 1:
                        chan = GetChannel(data)
                        Op(info[1], chan) 
                if info[0] == 'deop':
                    host = GetHost(data)
                    status = readAdmin(host)
                    if status == 1:
                        chan = GetChannel(data)
                        DeOp(info[1], chan)
                if info[0] == 'voice':
                    host = GetHost(data)
                    status = readAdmin(host)
                    if status == 1:
                        chan = GetChannel(data)
                        Voice(info[1], chan) 
                if info[0] == 'devoice':
                    host = GetHost(data)
                    status = readAdmin(host)
                    if status == 1:
                        chan = GetChannel(data)
                        DeVoice(info[1], chan) 
                if info[0] == 'join':
                    Join('#' + info[1])
                if info[0] == 'part':
                    Part('#' + info[1])
                if info[0] == 'version':
                    Send('Im a Python IRC bot coded by berend')

        if action == 'MODE':
            Host = GetHost(data)
            status = readAdmin(Host)
            if status == 0:
                if data.find('-o') != -1:
                    to_op = data.split('-o')[1]
                    chan = GetChannel(data)
                    chan = chan.split('-o')[0]
                    Op(to_op, chan)

                if data.find('+o') != -1:
                    to_deop = data.split('+o')[1]
                    chan = GetChannel(data)
                    chan = chan.split('+o')[0]
                    DeOp(to_deop, chan)

        if action == 'JOIN':
            Host = GetHost(data)
            status = readAdmin(Host)
            if status == 1:
                chan = GetChannel(data)
                nick = GetNick(data)
                Op(nick, chan)

Comments

Sign in to comment.
brightkit   -  Dec 10, 2013

Nice one!

I would like to know how to use an IPv6 address using the script above.

 Respond  
berend   -  Nov 16, 2012

Code changed better SunnyD?

 Respond  
Hawkee   -  Nov 15, 2012

@berend I'm with @Sorasyn. You should definitely isolate any configurable data so users can modify it easily.

 Respond  
Sorasyn   -  Nov 15, 2012

So, you're gonna litter the whole snippet with personal code so the user has to spend their time cleaning it up just so they can use it? For shame..

 Respond  
berend   -  Nov 15, 2012

Nah, if you gonna use a python bot and know how to run this on a server you should have at least some experience and see that you need to change that. Anyway when I found a better way to check actions I will put channels and server on vars so you can change them easy, thanks for the tip :-)

 Respond  
Sorasyn   -  Nov 15, 2012

Also, if you're going to share a snippet with the public, make sure to twice over your code and make sure that it's flexible enough to work for everyone not just yourself.

    def Op(to_op):
        irc.send( 'MODE #berend +o: ' + to_op + '\r\n')

    def DeOp(to_deop):
        irc.send( 'MODE #berend -o: ' + to_deop + '\r\n')

    def Voice(to_v):
        irc.send( 'MODE #berend +v: ' + to_v + '\r\n')

    def DeVoice(to_dv):
        irc.send( 'MODE #berend -v: ' + to_dv + '\r\n')
 Respond  
berend   -  Nov 15, 2012

I was thinking about strip the part which concludes "PRIVMSG/JOIN/PART/QUIT" and check from there what Is happening. So typing JOIN wont let the bot think someone joines the channel. Like I said, I'll keep you posted :D

 Respond  
berend   -  Nov 15, 2012

I agree 100% with you Aha2Y, I need to find out some better ways to check what is happening on IRC, join, part, quit, text etc. And I still need to create functions for every IRC action. I will keep you posted

 Respond  
Jordyk19   -  Nov 15, 2012

Nice, except for the data.find.
When you type JOIN the bot will think someone is joining or the bot self is.
Same with MODE.

 Respond  
Sorasyn   -  Nov 15, 2012

Nice to see some Python, but IRC frames seem to be abundant in this section. Good work though, every time I see Python I'd like to learn it, but never get around to doing so. Lol.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.