User Tools

Site Tools


user:abrunda1:python_irc_bot

Python

core.py (problem is this doesn't actually connect to the irc server it runs but it does nothing)

#!/usr/bin/python2.5
import sys
import socket
import string
import os
 
HOST='irc.offbyone.lan'
PORT=6667
NICK='Thorbot'
IDENT='Thorbot'
REALNAME='Thorbot'
OWNER='Thor'
CHANNELINIT='#botchan'
readbuffer=''
 
s=socket.socket( )
s.connect((HOST, PORT))
s.send('NICK '+NICK+'n')
s.send('USER '+IDENT+' '+HOST+' bla:'+REALNAME+'n')
 
while 1:
 
    line=s.recv(500) #recieve server messages
    print line #server message is output
    s.send('JOIN '+CHANNELINIT+'n') #Join a channel
    if line.find('PRIVMSG')!=-1: #Call a parsing function
        parsemsg(line)
        line=line.rstrip() #remove trailing 'rn'
        line=line.split()
        if(line[0]=='PING'): #If server pings then pong
            s.send('PONG '+line[1]+'n')
 
def parsemsg(msg):
    complete=msg[1:].split(':',1) #Parse the message into useful data
    info=complete[0].split(' ')
    msgpart=complete[1]
    sender=info[0].split('!')
    if msgpart[0]=='`' and sender[0]==OWNER: #Treat all messages starting with '`' as command
        cmd=msgpart[1:].split(' ')
        if cmd[0]=='op':
            s.send('MODE '+info[2]+' +o '+cmd[1]+'n')
        if cmd[0]=='deop':
            s.send('MODE '+info[2]+' -o '+cmd[1]+'n')
        if cmd[0]=='voice':
            s.send('MODE '+info[2]+' +v '+cmd[1]+'n')
        if cmd[0]=='devoice':
            s.send('MODE '+info[2]+' -v '+cmd[1]+'n')
        if cmd[0]=='sys':
            syscmd(msgpart[1:],info[2])
 
    if msgpart[0]=='-' and sender[0]==OWNER : #Treat msgs with - as explicit command to send to server
        cmd=msgpart[1:]
        s.send(cmd+'n')
        print 'cmd='+cmd
user/abrunda1/python_irc_bot.txt · Last modified: 2010/02/18 14:51 by abrunda1