Underc0de

Programación Scripting => Python => Mensaje iniciado por: @ed33x en Enero 30, 2011, 10:11:18 AM

Título: IRC Bot
Publicado por: @ed33x en Enero 30, 2011, 10:11:18 AM
Código (python) [Seleccionar]
#!/usr/bin/env python

# This is a just for fun experiment bot to learn how IRC works
# My name is Andre and I go by the handle Tr1pX
# Feel free to contact me floridait[--at--]gmail[---dot---]com
# This bot was tested on irc.bluehell.org

import sys
import socket
import string
import time
import os # when I use this import in linux it works but you might have to change it for winblows

# here are our connection variables
HOST="irc.bluehell.org"
PORT=6667
REALNAME="Ceasar"
# the readbuffer stores all the data the server send to us
readbuffer=""
# here we give the bot a name
try:
nickname = sys.argv[1]

print "[+] Joining with name "+nickname
except IndexError:
print '-This bot is designed for BlueHell-'
print '-----Please use xbot.py <nick>-----'
sys.exit()

CHAN='#bhf'
NICK=nickname
IDENT=NICK
# here we connect to the irc server
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))
# here are some other variables that we need to set
BOTOWNER = 'Tr1pX'
things_to_say = ['Here\'s 20 cents. Call all your friends and bring back some change! ',
'Any similarity between you and a human is purely coincidental! ',
'Calling you stupid would be an insult to stupid people. ',
'Do you ever wonder what life would be like if you\'d had enough oxygen at birth? ',
'Do you want people to accept you as you are or do you want them to like you? ',
'Do you still love nature, despite what it did to you? ',
'Don\'t you need a license to be that ugly? ',
'Every person has the right to be ugly, but you abused the privilege! ',
'You are the kind of a man that you would use as a blueprint to build an idiot. ']
#here goes our functioning part


def ai (tmp):
msgstrip = tmp.rstrip().split(' ', 3) # seperate the message text from the rest
msgtrigger = msgstrip[3]
sender = msgstrip[0].strip(':').split('!')[0] #here we get the command givers name

# now we define what happens if we get certain words
if msgstrip[1] == "PRIVMSG" and msgstrip[2] == CHAN or NICK:
if msgtrigger == ':'+NICK+':':
print "[+] %s mentioned your name!" % sender #this is for output to the console the bot is running in
the_msg = ' I am busy right now! I\'ll brb '+sender
msg_sender(CHAN, the_msg) # remeber to put CHAN in there for the destination

if msgtrigger == ':lol':
print "[+] %s typed a known trigger" % sender
the_msg1 = sender+' is laughing out loud!'
msg_sender(CHAN, the_msg1) # remeber to put CHAN in there for the destination

if msgtrigger == ':hmm':
print "[+] %s typed a known trigger" % sender
the_msg2 = 'What are you thinking about so hard '+sender+'?'
msg_sender(CHAN, the_msg2) # remeber to put CHAN in there for the destination

if msgtrigger == ':hello':
print "[+] %s typed a known trigger" % sender
the_msg3 = 'Hello '+sender+'!'
msg_sender(CHAN, the_msg3) # remeber to put CHAN in there for the destination

if msgtrigger == ':lmao':
print "[+] %s typed a known trigger" % sender
the_msg5 = sender+' is laughing his a$$ off!'
msg_sender(CHAN, the_msg5)

# here we are going to write more functional commands
if msgtrigger == ':!change.name':
print "[+] %s gave the command to change the name" % sender
the_msg6 = 'OK! '+sender+', I will change my name now.'
msg_sender(CHAN, the_msg6)
recon_newnick(CHAN, sender)

if msgtrigger == ':!exit':
print "[+] %s gave me the command to exit" % sender
the_msg7 = 'OK! '+sender+', let me check if you have permission to turn me off.'
msg_sender(CHAN, the_msg7)
permission(CHAN, sender)

if msgtrigger == ':!commands':
print "[+] %s has requested a list of commands" % sender
the_msg8 = 'OK! '+sender+', here come my commands:'
msg_commands = '[1] !change.name [2] !commands [3] !exit [4] !op.me [5] !diss.me [#] More to come: '+sender
msg_sender(CHAN, the_msg8)
msg_sender(CHAN, msg_commands)

if msgtrigger == ':!op.me':
print "[+] %s wants me to give him op" % sender
the_msg9 = 'OK! '+sender+', let me see if I can give you Op status.'
msg_sender(CHAN, the_msg9)
give_op(CHAN, sender)

if msgtrigger == ':!diss.me':
print "[+] %s typed a known trigger" % sender
the_msg10 = 'Ok! '+sender+' the following apply to you!'
msg_sender(CHAN, the_msg10)
x = 0
for text in things_to_say:
x = x + 1
text = str(x) + '. ' + text
time.sleep(3)
msg_sender(CHAN, text)

# if msgtrigger == ':!create.army':
# print "[+] %s is trying to load more bots" % sender
# the_msg11 = 'OK! '+sender+', I am getting back-up now, are you sure? ~(!create.army!yes / !create.army!no)~'
# msg_sender(CHAN, the_msg11)
#
# if msgtrigger == ':!create.army!yes':
# print "[+] %s has confirmed that he wants more bots" % sender
# msg_yes = 'Ok! '+sender+', as you wish!'
# msg_sender(CHAN, msg_yes)
# more_bots()
#
# if msgtrigger == ':!create.army!no':
# print "[+] %s has canceled and wants no more bots"
# msg_no = 'OK! '+sender+', I have cancelled your request'
# msg_sender(CHAN, msg_no)

#def more_bots (): # this function does work but most irc servers have a host session limit
# b = 0 # so after 3 connects from one host they will be created but the old ones disconnect
# while b < 5: # here you can set the number of bots to enter
# time.sleep(5)
# b = b + 1
# os.popen('python soldier.py soldier'+str(cool.gif) # i dont know if this works in windows but it does in linux



def give_op (channel, commander): # this function allows the bot to give the botowner op only if
if commander == BOTOWNER: # the bot has op status though obviously
print "[+] %s is recordnized as a authorized user" % commander
s.send("MODE "+channel+' +o '+commander+'\r\n')
op_msg = commander+', if I had the power to OP you, then you should be OP now!'
msg_sender(channel, op_msg)
time.sleep(5) # most of these sleep commands just keep the bot from flooding
commander = ''

elif commander != BOTOWNER:
print "[+] %s has no authority for OP" % commander
denied_op_msg = 'I am sorry '+commander+', but you don\'t have permission to be OPed'
msg_sender(channel, denied_op_msg)
time.sleep(5)
commander = ''



def permission (channel, commander): # this function is to shut the bot down with !exit, it checks your nick
if commander == BOTOWNER : # or botowner variable to see if the commander is authorized
print "[+] Shutting down connection"
shutdown_msg = '[+]Shutting Down[+] Good Bye '+commander+'!'
msg_sender(channel, shutdown_msg)
s.send("PART \r\n")
time.sleep(5)
sys.exit()

elif commander != BOTOWNER:
print "[+] %s has no proper authority!" % commander
denied_msg = '[+]Shut down denied[+] '+commander+' has no authority!'
msg_sender(channel, denied_msg)
time.sleep(5)
commander = ''


def recon_newnick (channel, commander): # this function lets you command the bot to change its name to yournicks_p3t
s.send("PART \r\n")
s.send("NICK "+commander+'s_p3t \r\n')
s.send("JOIN "+channel+'\r\n')

time.sleep(10)
confirm_msg = 'I changed my name '+commander+', like you wanted me to.'
msg_sender(channel, confirm_msg)

commander = '' # we have to clear commander to avoid errors like wrong sender or multiple


def msg_sender (destination, message): # this is the communicator, it sends all messages to irc for processing
s.send("PRIVMSG "+destination+" :"+message+"\r\n")
time.sleep(5)
message = '' # we have to make sure that message is empty before getting another trigger

def connect_to_channel (check): # I wrote this function to make the bot run smoother by not always running the same
try: # loop over and over trying to connect to the same channel
connected = check.rstrip().split(' ', 3)
are_we_con = connected[2]
if are_we_con != CHAN: # check to see if we already joined the channel
s.send('JOIN '+CHAN+'\r\n') #here we connect to the channel
except IndexError:
print "[+] Ideling"
#here ends our function

while 1: # this is our connection loop
readbuffer=readbuffer+s.recv(1024) # here we get data no more than 1024 bytes
chan_check = readbuffer # here i set up a test variable to search for data
connect_to_channel(chan_check) # here i call my connection function
if readbuffer.find('PRIVMSG') != -1: # here we get the data ready for processing for the ai() function
ai(chan_check) # this sends all data to the ai() function so it recorgnizes the triggers
temp=string.split(readbuffer, "\n")
readbuffer=temp.pop( ) # remove our last line to get room for the next
for line in temp:
line=string.rstrip(line)
line=string.split(line)
print "[+] %s" % line # this line helps me right my other function to see some output

if(line[0]=="PING"): # every bot needs to tell IRC that they are alive here
s.send("PONG %s\r\n" % line[1])