import sys
import socket
import string
import os
#import irclib
from constants import *
# twisted imports
#from twisted.protocols import irc
#from twisted.internet import reactor, protocol
#from twisted.python import log
# Generic echo handler ( space added )
# This is used to output some initial information from the server
#def handleEcho ( connection, event ):
# print
# print ' '.join ( event.arguments() )
#irc = irclib.IRC()
#irc.add_global_handler ( 'welcome', handleEcho )
def send(self, to, msg):
"""Sends a message "msg" to "to" """
s.send('PRIVMSG %s :%s\r\n' % (to, msg))
def sendresponse(self, to, msg, nick):
"""Personalized version of send."""
msg = msg.replace('[nick]', nick)
s.send('PRIVMSG %s :%s\r\n' % (to, msg))
def parsemsg(msg):
complete=msg[1:].split(':',1)
info=complete[0].split(' ')
msgpart=complete[1]
sender=info[0].split('!')
ask=msgpart.split(' ')
if msgpart[0]=='`' and (sender[0] == OWNER or sender[0] == ADMIN or sender[0] == ADMIN2):
cmd=msgpart[1:].split(' ')
if cmd[0]=='op':
s.send('MODE %s +o %s \n' % (info[2], cmd[1]))
s.send('PRIVMSG %s : %s, I hope you know what you are doing.\n' % (info[2], sender[0]))
if cmd[0]=='deop':
s.send('MODE %s -o %s \n' % (info[2], cmd[1]))
reply = 'muhahahaha'
if cmd[0]=='voice':
s.send('MODE %s +v %s \n' % (info[2], cmd[1]))
reply = 'But %s is so much better quiet' % (receiver)
if cmd[0]=='devoice':
s.send('MODE %s -o %s \n' % (info[2], cmd[1]))
reply = 'Much better'
if cmd[0]=='invite':
# target = cmd.split(' ')
s.send('invite %s %s\n' % (cmd[1], info[2]))
if cmd[0]=='sys':
syscmd(msgpart[1:],info[2])
if msgpart[0]=='-' and (sender[0] == OWNER or sender[0] == ADMIN or sender[0] == ADMIN2):
cmd=msgpart[1:]
s.send('%s \n' % (cmd))
print 'cmd= %s' % (cmd)
if msgpart[0]=='@':
cmd=msgpart[1:].strip()
receiver = None
if ask[0]=='%s' % (NICK) or ask[0] == '%s,' % (NICK):
cmd=msgpart[4:].strip()
receiver = None
if cmd.find('|')!=-1:
cmd = cmd.split('|')
receiver = cmd[1].strip()
cmd = cmd[0].strip()
cmd = cmd.lower()
if (msgpart.find('fuck')!=-1 or msgpart.find('fucking')!=-1): #and fuck == 0:
s.send('PRIVMSG %s : %s, Please do not swear in here. This is a family friendly channel.\n' % (info[2], sender[0]))
channel=info[2]
factoids(cmd,channel,sender,receiver)
def factoids(cmd,channel,sender,receiver):
print cmd
reply = "%s I have no idea what you're talking about" % (sender[0])
if cmd == 'ping':
reply = 'pong'
if cmd == 'info':
reply = "I am ajmorris' python bot"
if cmd == 'cbf':
reply = 'TOO BAD! DO IT!!'
if cmd == 'bhb':
reply = "I am ajmorris' bot. Beware me for i am god in this channel"
if cmd == 'pacman':
reply = 'OM NOM NOM NOM! I EAT YOU!'
if cmd == 'help':
reply = 'ajmorris, Joeb454, bodhi_zazen, attention please!'
if cmd == 'help!':
reply = 'ajmorris, Joeb454, bodhi_zazen, attention please!'
if cmd == 'run':
reply = '%s, im coming to eat you!' % (sender[0])
if cmd == 'you suck':
reply = 'no you do %s\r\n' % (sender[0])
if cmd == 'ohmy':
reply = 'Please watch your language and topic to help keep this channel family friendly.'
if cmd == 'microsoft':
reply = 'Microsoft is bad, VERY BAD. END OF DISCUSSION!'
if cmd == 'pastebin':
reply = 'pastebin is a service to post multiple-lined texts so you do not flood the channel. One of the many is located at http://www.pastebin.ca'
if cmd == 'insult':
reply = 'You are nothing but a weedy ooze of penguin-molesting bladders.'
if cmd == 'bodhi_zazen':
reply = 'bodhi is an op in this channel, do what he says or incur his wrath ;)'
if cmd == 'ajmorris':
reply = 'ajmorris is an op in this channel, and my creator, do what he says or incur his wrath ;)'
if cmd == 'joeb454':
reply = 'Joe is an op in this channel, do what he says or incur his wrath ;)'
if cmd.find('join')!=-1 and (sender[0] == OWNER or sender[0] == ADMIN or sender[0] == ADMIN2):
chan = cmd.split(' ')
print cmd
s.send('JOIN %s\r\n' % (chan[1]))
reply = 'done'
if cmd.find('kick')!=-1 and (sender[0] == OWNER or sender[0] == ADMIN or sender[0] == ADMIN2):
target = cmd.split(' ')
print cmd
s.send('KICK %s %s\r\n' % (CHAN, target[1]))
reply = 'with pleasure'
if cmd.find('cs')!=-1 and (sender[0] == OWNER or sender[0] == ADMIN or sender[0] == ADMIN2):
#chan = cmd.split(' ')
print cmd
s.send('PRIVMSG ChanServ :OP %s\r\n' % (channel))
reply = 'I feel the power!'
#if cmd.find('op')!=-1 and cmd.find('op')!=2 and (sender[0] == OWNER or sender[0] == ADMIN):
# target = cmd.split(' ')
# s.send('ChanServ :OP %s %s\r\n' % (channel, target[1]))
#reply = 'with pleasure'
#if cmd.find('deop')!=-1 and (sender[0] == OWNER or sender[0] == ADMIN):
# target = cmd.split(' ')
# s.send('deop %s\r\n' % (target[1]))
if cmd == 'leave' and (sender[0] == OWNER or sender[0] == ADMIN or sender[0] == ADMIN2):
reply = 'some cock head made me leave'
s.send('QUIT \r\n')
if cmd == 'part' and (sender[0] == OWNER):
reply = 'bye'
s.send('PART %s\r\n' % (channel))
if cmd.find('identify')!=-1 and (sender [0] == OWNER or sender[0] == ADMIN or sender[0] == ADMIN2):
#cmd = cmd.split(' ')
s.send('PRIVMSG NickServ: IDENTIFY %s\r\n' % (PASS))
#if cmd.find('invite')!=-1 and (sender [0] == OWNER or sender[0] == ADMIN or sender[0] == ADMIN2):
# target = cmd.split(' ')
# print cmd
# s.send('invite %s %s\r\n' % (target[0], channel))
if receiver != None:
s.send('PRIVMSG %s :%s, %s\n' % (channel, receiver, reply))
elif receiver == None:
s.send('PRIVMSG %s :%s\n' % (channel, reply))
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 %s :%s\n' % (channel, ot))
return 0
#Connection
s=socket.socket( )
s.connect((HOST, PORT))
s.send('NICK %s \n' % (NICK)) #Tells nick server nickname
s.send('USER %s %s bla : %s \n' % (IDENT, HOST, REALNAME)) #Tells user Identity, current host, and actual name
s.send('PRIVMSG NickServ :IDENTIFY %s\r\n' % (PASS))
s.send('PRIVMSG ChanServ :OP %s\r\n' % (CHAN))
while 1: #While True
line=s.recv(500)
print line # I believe this is for the bot owners information. It doesn't send anything to chatroom
log = file('log.txt', 'a')
log.write(line)
log.close()
if line.find('Welcome to the freenode IRC Network')!=-1: #If any line says this it will join the channel(s) put in CHAN
s.send('JOIN %s %s %s %s\n' % (CHAN, CHAN2, CHAN3, CHAN4))
if line.find('PRIVMSG')!=-1: # For private messaging, I do not know how this works
parsemsg(line)