Jump to content

Python IRC Bot Help


dylanwinn

Recommended Posts

This bot is intended to say "Darren is Awesome!" every so often!

CODE:

import sys
import socket
import string
import os #not necassary but later on I am going to use a few features from this 

HOST='chat1.ustream.tv' #The server we want to connect to
PORT=6667 #The connection port which is usually 6667
NICK='darrenbot' #The bot's nickname
IDENT='awesomebot'
REALNAME='Darrren Is Awesome'
OWNER='dylanwinn' #The bot owner's nick
CHANNELINIT='#hak5editcam' #The default channel for the bot
readbuffer='' #Here we store all the messages from server

def syscmd(commandline,channel):
    cmd=commandline.replace('sys ','')
    cmd=cmd.rstrip()
    os.system(cmd+' >temp.txt')
    a=open('temp.txt')
    ot=a.read()
    ot.replace('n','|')
    a.close()
    s.send('PRIVMSG '+channel+' :'+ot+'n')
    return 0

s=socket.socket( ) #Create the socket
s.connect((HOST, PORT)) #Connect to server
s.send('NICK '+NICK+'n') #Send the nick to server
s.send('USER '+IDENT+' '+HOST+' bla :'+REALNAME+'n') #Identify to server
s.send('JOIN '+CHANNELINIT+'n') #Join a channel

while 1:
    if(line[0]=='PING'): #If server pings then pong
            s.send('PONG '+line[1]+'n') 
    time.sleep(60)
    s.send('Darren is awesome')

ERROR:

IDLE 3.0      
>>> ================================ RESTART ================================
>>> 
Traceback (most recent call last):
  File "C:/Python30/bot", line 28, in <module>
    s.send('NICK '+NICK+'n') #Send the nick to server
TypeError: send() argument 1 must be string or buffer, not str
>>>

The error doesn't seem to make sense! Isn't a string the same a STR?

Link to comment
Share on other sites

you are using python 3000, there are a lot of chages and old code is probably not compatible

i've tested it on python 2.5 and it worked.

I don't have any experience with python 3000 but it may be worth to try this:

s.send(str('NICK '+NICK+'n'))

Link to comment
Share on other sites

This bot is intended to say "Darren is Awesome!" every so often!

CODE:

import sys
import socket
import string
import os #not necassary but later on I am going to use a few features from this 

HOST='chat1.ustream.tv' #The server we want to connect to
PORT=6667 #The connection port which is usually 6667
NICK='darrenbot' #The bot's nickname
IDENT='awesomebot'
REALNAME='Darrren Is Awesome'
OWNER='dylanwinn' #The bot owner's nick
CHANNELINIT='#hak5editcam' #The default channel for the bot
readbuffer='' #Here we store all the messages from server

def syscmd(commandline,channel):
    cmd=commandline.replace('sys ','')
    cmd=cmd.rstrip()
    os.system(cmd+' >temp.txt')
    a=open('temp.txt')
    ot=a.read()
    ot.replace('n','|')
    a.close()
    s.send('PRIVMSG '+channel+' :'+ot+'n')
    return 0

s=socket.socket( ) #Create the socket
s.connect((HOST, PORT)) #Connect to server
s.send('NICK '+NICK+'n') #Send the nick to server
s.send('USER '+IDENT+' '+HOST+' bla :'+REALNAME+'n') #Identify to server
s.send('JOIN '+CHANNELINIT+'n') #Join a channel

while 1:
    if(line[0]=='PING'): #If server pings then pong
            s.send('PONG '+line[1]+'n') 
    time.sleep(60)
    s.send('Darren is awesome')

ERROR:

IDLE 3.0      
>>> ================================ RESTART ================================
>>> 
Traceback (most recent call last):
  File "C:/Python30/bot", line 28, in <module>
    s.send('NICK '+NICK+'n') #Send the nick to server
TypeError: send() argument 1 must be string or buffer, not str
>>>

The error doesn't seem to make sense! Isn't a string the same a STR?

I haven't used Py3k either, but Google tells me it's a bug in that version and you should be using a byte array instead of the str type (http://bugs.python.org/issue4275). Initially I was curious if just wrapping the argument in str() would fix it, but I guess not.

Also, where does your program call recv? From this code, it looks like you're never going to PONG and you'll get disconnected. And should those 'n' be '\n' Or is that forum magic that removed them?

Link to comment
Share on other sites

Thanks! But, I now have another issue:

import sys
import socket
import string
import time

HOST='chat1.ustream.tv' #The server we want to connect to
PORT=6667 #The connection port which is usually 6667
NICK='darrenbot' #The bot's nickname
IDENT='awesomebot'
REALNAME='Darrren Is Awesome'
OWNER='dylanwinn' #The bot owner's nick
CHANNELINIT='#hak5editcam' #The default channel for the bot
readbuffer='' #Here we store all the messages from server

def syscmd(commandline,channel):
    cmd=commandline.replace('sys ','')
    cmd=cmd.rstrip()
    os.system(cmd+' >temp.txt')
    a=open('temp.txt')
    ot=a.read()
    ot.replace('n','|')
    a.close()
    s.send('PRIVMSG '+channel+' :'+ot+'n')
    return 0

s=socket.socket( ) #Create the socket
s.connect((HOST, PORT)) #Connect to server
s.send('NICK '+NICK+'n') #Send the nick to server
s.send('USER '+IDENT+' '+HOST+' bla :'+REALNAME+'n') #Identify to server
print('Connecting to server')
time.sleep(5)
s.send('JOIN '+CHANNELINIT+'n') #Join a channel
print('Joining channel')
line=s.recv(500) #recieve server messages
if(line.find('PING') #If server pings then pongs
   s.send('PONG '+line[1]+' :'+ot+'n')
    print('PINGED!')

print('Connected!')

while 1:
    line=s.recv(500) #recieve server messages
    print line #server message is output
    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')
            print('PINGED!')

while 1:
    s.send('Darren is awesome')
    print('Darren is awsome')
    time.sleep(60)

It says there is a syntax error on this line:

s.send('PONG '+line[1]+' :'+ot+'n')

Help?

Link to comment
Share on other sites

Well, in this line:

if(line.find('PING') #If server pings then pongs

You are missing a : at the end.

So fixed up is should look like:

if(line.find('PING'): #If server pings then pongs
    s.send('PONG '+line[1]+' :'+ot+'n')
    print('PINGED!')

I still have no idea where you are getting the ot variable from, I only see it in the syscmd function (which returns 0)?

That should fix it, you got it right on the second if in the while loop though.

Link to comment
Share on other sites

Thanks! I figured that out myself, though! (Probably should have told someone)

NEW CODE:

import sys
import socket
import string
import time

HOST='chat1.ustream.tv' #The server we want to connect to
PORT=6667 #The connection port which is usually 6667
NICK='darrenbot' #The bot's nickname
IDENT='awesomebot'
REALNAME='This is a Bot'
OWNER='dylanwinn' #The bot owner's nick
CHANNELINIT='#hak5editcam' #The default channel for the bot
readbuffer='' #Here we store all the messages from server

def syscmd(commandline,channel):
    cmd=commandline.replace('sys ','')
    cmd=cmd.rstrip()
    os.system(cmd+' >temp.txt')
    a=open('temp.txt')
    ot=a.read()
    ot.replace('n','|')
    a.close()
    s.send('PRIVMSG '+channel+' :'+ot+'n')
    return 0

s=socket.socket( ) #Create the socket
s.connect((HOST, PORT)) #Connect to server
s.send('NICK '+NICK+'n') #Send the nick to server
s.send('USER '+IDENT+' '+HOST+' bla :'+REALNAME+'n') #Identify to server
print('Connecting to IRC Server...')

CONNECTED = 0

while CONNECTED ==0 :
    line=s.recv(500) #recieve server messages
    print line #prints server backtalk
    if line.find('PING'): #If server pings then pongs
       print('FOUND Server!')
       print('Ping!')
       s.send('PONG '+line[1]+'n')
       if line.find('ERROR*'):
          print('FATAL ERROR: SERVER REFUSED CONNECTION!!!')
          CONNECTED = 0
          sys.exit()
       s.send('JOIN '+CHANNELINIT+'n') #Join a channel
       print('Joining channel!')
       CONNECTED = 1
       print('Connected!')
    else:
        CONNECTED = 0
        Print('Retrying...')

while CONNECTED == 1:
    RANDOM=rand(1,5) # Choose something to say
    if RANDOM == 1:
       s.send('Darren is AWESOME!')
    elif RANDOM == 2:
       s.send('Darren is more BADASS then Evil Server!')
    elif RANDOM == 3:
       s.send('Darren is almost as HOT a Shannon!')
    elif RANDOM == 4:
       s.send('Darren is 1/10th as EPIC as Chuck Norris!')
    elif RANDOM == 5:
       s.send('You Just LOST THE GAME! Darren CANT LOOSE any game!')
    print('I have satisfied myself by saying how awesome Darren is in a witty way!')
    time.sleep(900) #Wait 15 minnutes, then repeat

NEW ERROR:

>>> ================================ RESTART ================================
>>> 
Connecting to IRC Server...
ERROR :Closing Link: [70.181.152.203] (Ping timeout)


FOUND Server!
Ping!
FATAL ERROR: SERVER REFUSED CONNECTION!!!

Traceback (most recent call last):
  File "C:\Python24\DarenIsAwesomeIRCbot", line 44, in -toplevel-
    sys.exit()
SystemExit
>>>

Ignore the SysExit not working, the real error is the Ping Timeout! I have found that I am doing the ping response incorrectly...

ANYONE THAT KNOWS HOW IRC WORKS, HOW DO I RESPOND TO THE PING?

Link to comment
Share on other sites

Here is my basic bot, hope it helps.

import sys
import socket
import string
import threading

HOST="c.ustream.tv"
PORT=6667
NICK="JzBot3"
IDENT="JzBot3"
REALNAME="JzBot3"
readbuffer=""

print "Starting..."
s=socket.socket( )
s.connect((HOST, PORT))
s.send("NICK %s\r\n" % NICK)
s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
s.send("JOIN #hak5editcam \r\n")
print "Started"
while 1:
    readbuffer=readbuffer+s.recv(1024)
    temp=string.split(readbuffer, "\n")
    readbuffer=temp.pop( )
    
    for line in temp:
        line=string.rstrip(line)
        line=string.split(line)
        if(line[0]=="PING"):
            s.send("PONG %s\r\n" % line[1])
        print line[1]

Link to comment
Share on other sites

Thanks! New Error!

CODE:

import sys
import socket
import string
import time
import threading

HOST='c.ustream.tv' #The server we want to connect to
PORT=6667 #The connection port which is usually 6667
NICK='DarrenFanBot' #The bot's nickname
IDENT='awesomebot' #The thing used in place of an IP
REALNAME='This is a Bot' #Obvious
OWNER='dylanwinn' #The bot owner's nick
CHANNELINIT='#hak5editcam' #The default channel for the bot
readbuffer='' #Here we store all the messages from server

def syscmd(commandline,channel):
    cmd=commandline.replace('sys ','')
    cmd=cmd.rstrip()
    os.system(cmd+' >temp.txt')
    a=open('temp.txt')
    ot=a.read()
    ot.replace('n','|')
    a.close()
    s.send('PRIVMSG '+channel+' :'+ot+'n')
    return 0

s=socket.socket( ) #Create the socket
s.connect((HOST, PORT)) #Connect to server
s.send('USER '+IDENT+' '+HOST+' bla :'+REALNAME+'n') #Identify to server
print('Connecting to IRC Server...')

CONNECTED = 0

while 1:
    line=s.recv(500) #recieve server messages
    print line #prints server backtalk
    readbuffer=readbuffer+s.recv(1024)
    temp=string.split(readbuffer, "\n")
    readbuffer=temp.pop( )
    if(line[0]=="PING"):
       print('FOUND Server!')
       s.send("PONG %s\r\n" % line[1])
       print('Ping!')
       if line.find('ERROR*'):
          print('FATAL ERROR: SERVER REFUSED CONNECTION!!!')
          CONNECTED = 0
          sys.exit()
       s.send('JOIN '+CHANNELINIT+'\n') #Join a channel
       s.send('NICK '+NICK+'\n') #Send the nick to server
       print('Joining channel!')
       CONNECTED = 1
       print('Connected!')
    else:
        CONNECTED = 0
        print('Retrying...')

while CONNECTED == 1:
    RANDOM=rand(1,5) # Choose something to say
    if RANDOM == 1:
       s.send('Darren is AWESOME!')
    elif RANDOM == 2:
       s.send('Darren is more BADASS then Evil Server!')
    elif RANDOM == 3:
       s.send('Darren is almost as HOT a Shannon!')
    elif RANDOM == 4:
       s.send('Darren is 1/10th as EPIC as Chuck Norris!')
    elif RANDOM == 5:
       s.send('You Just LOST THE GAME! Darren CANT LOOSE any game!')
    print('I have satisfied myself by saying how awesome Darren is in a witty way!')
    time.sleep(900) #Wait 15 minnutes, then repeat

ERROR:

>>> ================================ RESTART ================================
>>> 
Connecting to IRC Server...
ERROR :Closing Link: [70.181.152.203] (Ping timeout)


Retrying...


Traceback (most recent call last):
  File "C:\Python24\DarenIsAwesomeIRCbot", line 40, in -toplevel-
    if(line[0]=="PING"):
IndexError: string index out of range
>>>

Link to comment
Share on other sites

That last code was hopeless, so I rewrote it:

import sys
import socket
import string

HOST='c.ustream.tv' #The server we want to connect to
PORT=6667 #The connection port which is usually 6667
NICK='DarrenFanBot' #The bot's nickname
IDENT='DarrenFanBot' #The thing used in place of an IP
REALNAME='Owned By DylanWinn' #Obvious
readbuffer='' #Here we store all the messages from server

print ("Starting UP!")
s=socket.socket( ) #Initilize Socket
s.connect((HOST, PORT)) #Connect to Server
print('Connecting...')
s.send("NICK %s\r\n" % NICK) #Set Nick
print('Verifing NICK...')
s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME)) # Set other User Info
print('Verifing IDENT..')
s.send("JOIN #hak5editcam \r\n") # Join Channel
print('Joining Channel..')
print ('Started')
s.send("/me IS IN THE HOUSE! Type !praisedarren to activate me! \r\n")# Announce Presence
TIME = 0 # Setup Timer
RANDOM = 0 # Setup up Randomizer

while 1:
    readbuffer=readbuffer+s.recv(1024)
    temp=string.split(readbuffer, "\n")
    readbuffer=temp.pop( )
    
    for line in temp:
        line=string.rstrip(line)
        line=string.split(line)
        if line[0] == "PING":
           s.send("PONG %s\r\n" % line[1])
        if line == '!praisedarren': #Check for a command
            print('I have been commanded to praise Darren! \n')
            RANDOM = rand(1,5) # Randomly choose Something to say 
            if RANDOM == 1:
                s.send('Darren is AWESOME! \r\n')
            elif RANDOM == 2:
              s.send('Darren is more BADASS then Evil Server! \r\n')
            elif RANDOM == 3:
              s.send('Darren is almost as HOT a Shannon! \r\n')
            elif RANDOM == 4:
              s.send('Darren is 1/10th as EPIC as Chuck Norris! \r\n')
            print('I have satisfied myself by saying how awesome Darren is in a witty way!') # Praise Self

Unfortunately, it does noting! no errors, nothing!

Link to comment
Share on other sites

I rewrote the code again, this time using a library called PyIRClib. Now, it works! But, I need suggestions for more phrases!

import pyirclib
import string
import sys
import time
import random

def praise(): # Randomly Choose Something to Say
  RANDOM = random.randint(1,27)
  print('Praising Darren...')
  if RANDOM == 1:
     irc.privmsg('#hak5editcam', 'Darren is AWESOME!')
  elif RANDOM == 2:
     irc.privmsg('#hak5editcam', 'Darren is more BADASS then Evil Server!')
  elif RANDOM == 3:
     irc.privmsg('#hak5editcam', 'Darren is almost as HOT as Shannon!')
  elif RANDOM == 4:
     irc.privmsg('#hak5editcam', 'Darren is 1/10th as EPIC as Chuck Norris!')
  elif RANDOM == 5:
     irc.privmsg('#hak5editcam', 'Darren RULZ!!!11!!')
  elif RANDOM == 6:
     irc.privmsg('#hak5editcam', 'Darren is so 1337 he can ask Shannon to the Prom in person!')
  elif RANDOM == 7:
     irc.privmsg('#hak5editcam', 'If Darren were a flavor of linux he would be Ubuntu because he is very user FRIENDLY!')
  elif RANDOM == 8:
     irc.privmsg('#hak5editcam', 'When Darren does penetration testing, its more like BOMB-testing.')
  elif RANDOM == 9:
     irc.privmsg('#hak5editcam', 'Darrens pet Unicorn eats NAILS for breakfast.')
  elif RANDOM == 10:
     irc.privmsg('#hak5editcam', 'Al Gore didnt INVENT the interwebs, Darren Kitchen did.')
  elif RANDOM == 11:
     irc.privmsg('#hak5editcam', 'Hak5.org is a security-related site that MAY contain hacking tools or examples of malicious code written by Darren Kitchen.')
  elif RANDOM == 12:
     irc.privmsg('#hak5editcam', 'The NSA is looking for Darren because he BROKE their uncrackable encryption with a Nintendo DS.')
  elif RANDOM == 13:
     irc.privmsg('#hak5editcam', 'Evil Server once located and destroyed a Russian Supercomputer by itself. Darren taught him how.')
  elif RANDOM == 14:
     irc.privmsg('#hak5editcam', 'Darren totally PWNS at <insert name of game here>!')
  elif RANDOM == 15:
     irc.privmsg('#hak5editcam', 'Darrens cat Kerby is secretly a Ninja. *So THATS how all those darts got cut in half!*')
  elif RANDOM == 16:
     irc.privmsg('#hak5editcam', 'Darren can hack into a WPA-encrypted WIFI network with one hand tied behind his back.')
  elif RANDOM == 17:
     irc.privmsg('#hak5editcam', 'Darren has hex-edited iTunes to PAY him 99 cents every time he buys a song.')
  elif RANDOM == 18:
     irc.privmsg('#hak5editcam', 'Darren once made $40 in ONE second!')
  elif RANDOM == 19:
     irc.privmsg('#hak5editcam', 'Darren fell in LOVE with an Arduino Micro-Controller.')
  elif RANDOM == 20:
     irc.privmsg('#hak5editcam', 'Darren owns SIX more netbooks than the average American')
  elif RANDOM == 21:
     irc.privmsg('#hak5editcam', 'Darrens USB lockpick can pick Windows passwords, car doors, and COMBINATION LOCKS.')
  elif RANDOM == 22:
     irc.privmsg('#hak5editcam', 'Darren can make a whole sentance with the word BLEEP and a beer.')
  elif RANDOM == 23:
     irc.privmsg('#hak5editcam', 'Darren once stole the source code for Windows, but gave it back because he was in a good mood.')
  elif RANDOM == 24:
     irc.privmsg('#hak5editcam', 'Darren is the only person in the world that doesnt use Antivirus AND has a PC thats good as new')
  elif RANDOM == 25:
     irc.privmsg('#hak5editcam', 'Darren breaks into firewalls for fun.')
  elif RANDOM == 26:
     irc.privmsg('#hak5editcam', 'When Darren cracks a box, it doesnt fall apart, it explodes.')
  elif RANDOM == 27:
     irc.privmsg('#hak5editcam', 'Darren Kitchen: System Admin by Day; 1337 Hakzor by Night.')
                 
print('Connecting To IRC Server...')
irc = pyirclib.Irclib('c.ustream.tv',6667) # Connect to Server
irc.setDebug = 1 # For Debugging 1 = ON , 0 = OFF
print('Logging In...')
irc.login('DarrenFanBot',username = 'DarrenFanBot',realname = 'Made By dylanwinn') # Log In
print('Joining Channel...')
irc.join("#hak5editcam") # Join Channel
print('Connected!')

irc.privmsg("#hak5editcam", "IS IN THE HOUSE!")
praise() # Praise Darren

TIME = time.time() # Note Time

while 1:
  if time.time() - TIME >= 3600: # Check Time
     TIME = time.time() # Reset Time
     print('Time to Praise Darren!')
     print('Connecting To IRC Server...')
     irc = pyirclib.Irclib('c.ustream.tv',6667) # Connect to Server
     print('Logging In...')
     irc.login('DarrenFanBot',username = 'DarrenFanBot',realname = 'Made By dylanwinn') # Log In
     print('Joining Channel...')
     irc.join("#hak5editcam") # Join Channel
     print('Connected!')
     praise() # Praise Darren

Link to comment
Share on other sites

  • 2 years later...

This bot could be evil!! I see already some potentials in it.

Edited by Infiltrator
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...