Skip to content

Instantly share code, notes, and snippets.

@geekgonecrazy
Created June 4, 2011 23:31
Show Gist options
  • Save geekgonecrazy/1008491 to your computer and use it in GitHub Desktop.
Save geekgonecrazy/1008491 to your computer and use it in GitHub Desktop.
added a thread... lots of work todo
#!/usr/bin/env python
import sys, socket, string, time, os, ssl, threading
from cmd import Cmd
USING_READLINE = True
try:
# For platforms without readline support go visit ...
# http://pypi.python.org/pypi/readline/
import readline
except:
try:
# For Windows readline support go visit ...
# https://launchpad.net/pyreadline
import pyreadline
except:
USING_READLINE = False
# Global Variables
HOST="irc.secfo.org"
PORT=6667
SSLPORT=7000
USESSL=1
NICK="Corellon"
IDENT="Corellon"
REALNAME="Corellon Larethian"
CHAN="#random2"
readbuffer=""
BOTOWNER="jonas"
class CmdLine(Cmd):
"""
Here are the available commands:
1. help - This brings up this.
2. msg - This sends a message. Usage: msg message
3. exit - Exits bot
"""
def __init__(self):
Cmd.__init__(self)
if not USING_READLINE:
self.completekey = None
self.prompt = "[+]"
self.intro = "Aaron's IRCbot"
def default(self, line):
cmd, arg, line = self.parseline(line)
cmds = self.completenames(cmd)
num_cmds = len(cmds)
if num_cmds == 1:
getattr(self, 'do_'+cmds[0])(arg)
elif num_cmds > 1:
sys.stdout.write('%% Ambiguous command:\t"%s"\n' % cmd)
else:
sys.stdout.write('% Unrecognized command\n')
def emptyline(self):
pass
def do_help(self, arg):
doc_strings = [ (i[3:], getattr(self, i).__doc__)
for i in dir(self) if i.startswith('do_') ]
doc_strings = [ ' %s\t%s\n' % (i, j)
for i, j in doc_strings if j is not None ]
sys.stdout.write('Commands:\n%s\n' % ''.join(doc_strings))
def do_exit(self, args):
" Shell operations "
sys.stdout.write('Exiting\n')
def do_msg(self, args):
" Show running system information "
MessageSend(CHAN,args)
print "Sending message: %s" % args
def precmd(self, line):
if line.strip() == 'help':
sys.stdout.write('%s\n' % self.__doc__)
return ''
cmd, arg, line = self.parseline(line)
if arg == '?':
cmds = self.completenames(cmd)
if cmds:
self.columnize(cmds)
sys.stdout.write('\n')
return ''
return line
class cliinput (threading.Thread):
def run (self):
cmdLine = CmdLine()
cmdLine.cmdloop()
# Creates a socket and connects to IRC server/channel
if USESSL == 1:
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
s.connect((HOST, SSLPORT))
irc = ssl.wrap_socket(s)
else:
irc = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
irc.connect((HOST, PORT))
irc.send("NICK %s\r\n" % NICK)
irc.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
irc.send("JOIN :%s\r\n" % CHAN)
irc.send("PRIVMSG %s :%s\r\n" % ("nickserv", "id password"))
irc.send("PRIVMSG %s :%s %s\r\n" % ("chanserv", "op ", CHAN))
def Analyze (tmp): # Decide what the input type was.
if tmp.find('PING') != -1:
irc.send('PONG :%s' % HOST)
time.sleep(5)
print"PONG"
if tmp.find('PRIVMSG') != -1: # If it was of the type privmsg
# This block of code splits the incomming line into 4 parts.
# 0: Username 1: input type 2: Channel 3: Message
msgstrip = tmp.rstrip().split(' ', 3) # The input is split up into 4 sections.
sender = msgstrip[0].strip(':').split('!')[0] # Username is found by stripping away the hostname.
msg = msgstrip[3]
msgtrigger = msg.replace(':', '', 1) # The ':' is removed from the front of the message.
Commands(sender, msgtrigger) # Sent to the Commands function to find out what the command given is.
if tmp.find('JOIN') != -1: # If it was of the type join
msgstrip = tmp.rstrip().split(' ', 2)
print msgstrip
sender = msgstrip[0].strip(':').split('!')[0]
print sender
chanstrip = msgstrip[2].strip(':')
print chanstrip
Users(sender) # Sent to Users to find out what permissions this user has.
def Users (sender): # Users function
# List of users that will be automatically voiced when they enter the channel.
print '3',sender,'3'
if sender == 'jonas':
print "found"
irc.send("MODE %s %s %s\r\n" % (CHAN, "+v ", sender))
def Action (destination, msg):
irc.send("PRIVMSG "+destination+" :\x01ACTION "+msg+" \x01\r\n")
time.sleep(5)
def Commands (sender, msgtrigger): # All the commands will be here.
if msgtrigger == 'Corellon':
print "[+] %s typed a known trigger" % sender
the_msg5 = sender+' Yes?'
MessageSend(CHAN, the_msg5)
if msgtrigger == '!commands': # List 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
MessageSend(CHAN, the_msg8)
MessageSend(CHAN, msg_commands)
if msgtrigger == '!up': # Op
print "[+]%s wants me to give him op" % sender
irc.send("MODE %s %s %s\r\n" % (CHAN, "+o ", sender))
if msgtrigger == '!voice': # voice
print "[+]%s wants me to give him op" % sender
irc.send("MODE %s %s %s\r\n" % (CHAN, "+v ", sender))
if msgtrigger == '!down': # DeOp
print "[+] %s wants me to deop" % sender
irc.send("MODE %s %s %s\r\n" % (CHAN, "-o ", sender))
if msgtrigger == '!shutdown': # Shutdown the bot
print "[+] %s Issued the shutdown command" % sender
if sender == BOTOWNER:
MessageSend(CHAN, "Shutting Down...")
irc.send("PART %s : Bye!\r\n" % CHAN)
time.sleep(5)
sys.exit()
if msgtrigger == '!cycle': # Cycle - Leave then rejoin channel
print "[+]Cycling now."
irc.send("PART %s : Cylcling now..\r\n" % CHAN)
irc.send("JOIN :%s\r\n" % CHAN)
irc.send("PRIVMSG %s :%s %s\r\n" % ("chanserv", "op ", CHAN))
if msgtrigger.find('!kick') != -1: # Kick User
if sender == BOTOWNER:
message = msgtrigger.rstrip().split(' ', 1)
kickie = message[1]
irc.send("KICK %s %s\r\n" % (CHAN, kickie))
def MessageSend (destination, message): # This is the function that sends messages.
irc.send("PRIVMSG "+destination+" :"+message+"\r\n")
time.sleep(5)
def logwritter (tmp): #Our Log function
temp=string.split(tmp, "\n")
readbuffer=temp.pop( )
for line in temp:
print "[+] %s" % line # Writes line to console
test = cliinput()
test.setDaemon(True)
test.start()
while 1:
readbuffer = irc.recv(1024) # Recieves texting from IRC socket
Data = readbuffer
Analyze(Data) # This sends data to the Analyze function.
logwritter(Data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment