Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket, ssl, base64, sys
- # Fill in the values needed. Start with: bad-robot:~ Dennis$ python bot.py
- server = "irc.atlassian.com" # Server
- port = 6669 # Port
- usessl = True # SSL
- channel = "#verbose" # Channel
- botnick = "Bad-Robot" # Bot nick
- username = "yourusername" # IRC Server Auth Username
- password = "yourpassword" # IRC Server Auth Password, base64
- quitmsg = "GRRRRR FINE, I'LL LEAVE!" # IRC Leave message
- prefix = "." # Prefix for commands, using a "." makes ".help", using "bot " makes "bot help"
- owner = "Dennis" # Used in some commands to describe the bot owner
- botowner = "dmeer" # This is the username it responds to on admin commands
- def joinchan(chan): # This function is used to join channels.
- print "Connecting to " + chan
- ircsock.send("JOIN %s\n" % (chan))
- ircsock.send("PRIVMSG %s :%s reporting!\n" % (channel,botnick))
- print "Connected to "+channel+"!"
- # Bot commands
- def commands(nick,user,channel,message):
- # Says that it"s awesome
- if message.find(prefix+"awesome")!=-1:
- ircsock.send("PRIVMSG %s :%s: this is awesome!\r\n" % (channel,nick))
- # Help command, you can use it to list other commands
- elif message.find(prefix+"help")!=-1:
- ircsock.send("PRIVMSG %s :%s: My other command is awesome.\r\n" % (channel,nick))
- # Check if user is considered an admin
- elif message.find(prefix+"admin")!=-1:
- if user==botowner:
- ircsock.send("PRIVMSG %s :hey %s, you're my owner!\r\n" % (channel,nick))
- else:
- ircsock.send("PRIVMSG %s :%s: you're not my daddy!\r\n" % (channel,nick))
- # When someone mentions bad robot, it will respond
- elif message.find("bad robot")!=-1:
- ircsock.send("PRIVMSG %s :%s: I'm %s, an irc bot. My owner is %s\r\n" % (channel,nick,botnick,owner))
- # Responds to hey BOTNICK, example: hey Bad-Robot
- elif message.find(":hey "+ botnick)!=-1:
- ircsock.send("PRIVMSG %s :hey %s, what's up?\r\n" % (channel,nick))
- # Create connection
- if usessl:
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create socket
- s.connect((server, port)) # Connect to the server
- ircsock = ssl.wrap_socket(s) # Wrap existing socket in ssl
- else:
- ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create socket
- ircsock.connect((server, port)) # Connect to the server
- # Start Login Process
- passwordDecrypted = base64.decodestring(password) # Decode password from base64
- ircsock.send("PASS %s\n" % (passwordDecrypted)) # Supply password
- ircsock.send("NICK %s\n" % (botnick)) # Here we actually assign the nick to the bot
- ircsock.send("USER %s %s %s :%s\n" % (username,username,server,botnick)) # User authentication
- while 1: # Loop loop loop!
- ircmsg = ircsock.recv(2048) # Receive data from server
- ircmsg = ircmsg.strip("\n\r") # Removing unnecessary linebreaks.
- print(ircmsg) # Print what"s coming from the server
- # Create command handler
- if ircmsg.find(" PRIVMSG ")!=-1: # This needs to be done more secure with a split that looks at the full msg
- nick=ircmsg.split("!")[0][1:]
- channel=ircmsg.split(" PRIVMSG ")[-1].split(" :")[0]
- user=ircmsg.split("!")[-1].split("@")[0]
- commands(nick,user,channel,ircmsg)
- if ircmsg.find(prefix+"leave") != -1: # Look for leave and exit bot
- user=ircmsg.split("!")[-1].split("@")[0] # Grab username (not nick) of user asking to exit bot
- if user==botowner: # See if user has enough rights to quit bot
- ircsock.send("QUIT :%s\n" % (quitmsg)) # Disconnect from IRC
- ircsock.close() # Close irc socket
- if usessl:
- s.close() # Close unencrypted irc socket
- break # Exit loop, thus exiting the program
- else:
- ircsock.send("PRIVMSG %s :%s: you can't shut me down!\r\n" % (channel,nick))
- if ircmsg.find("001 "+botnick) != -1: # Join channel when welcome message prints
- joinchan(channel)
- if ircmsg.find("PING :") != -1: # If the server pings us then we"ve got to respond!
- ircsock.send("PONG :pingis\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement