Skip to content

Instantly share code, notes, and snippets.

@Malex
Created July 8, 2009 19:40
Show Gist options
  • Save Malex/143121 to your computer and use it in GitHub Desktop.
Save Malex/143121 to your computer and use it in GitHub Desktop.
Il mio bot
# -*- coding: utf-8 -*-
#!/urs/bin/env python
#########################
# MalexBot v 0.1 Beta 2
# as known as
# MyIRCBot
# Author: Malex
# Malexprojects.com Software
# License: GNU/GPL 3
#########################
# The My(c)Softwares are an
# exclusive trademark of
# Malexprojects.com (website
# http://malexprojects.altervista.org)
# You cannot use that name for your
# software without the approvation of
# Malex, e-mailing him at alexmarcat@hotmail.it
# You have to get the approvation of Malex
# to use the name, and besides you always
# have to put in your source the original author
# and the original name.
#########################
# 29/06/2009 21:49 Official Release: Beta 2
import socket
from time import sleep
import re
try:
import plugins
except:
pass
class Bot:
server=""
port=6667
channel=""
chanpass=""
name=""
passnick=""
logfile=""
admin=""
coadmin=[]
serverEncode="utf-8" #the encode of messages sent by server
#Initializating the standard variable, they are almost all private, do not modify it manually,
#use the public vars instead, and their specified methods
def __init__(self,plugs={},igns=[]):
self.r=re.compile("(\w+)\!.+\@\S+\s([A-Z]+)\s(\#?\w+)\s(\:.+|\w+\s\:.+|\+[a-zA-Z]\s?.*)\s\s")
self.check=re.compile("(\w+)\s\:(.+)")
self.check2=re.compile("\+([a-zA-Z])+\s?(.*)")
for i in igns:
self.ign=i,False
self.__plugins={"PRIVMSG" : {}, "KICK" : {}, "MODE" : {}, "INVITE" : {}, "QUIT" : {}, \
"JOIN" : {}, "PART" : {}, "NOTICE" : {}, "NICK" : {} }
self._classlist=[]
if not "plugins" in dir():
raise SystemError("The plugins package was not found. Please check your bot class")
for k,v in plugs:
self.plugins=v,k,True
def connect(self):
self.s=socket.socket()
self.s.connect((self.server,self.port))
print("Connected to server...")
if self.logfile:
self.f=open(self.logfile,'a')
else:
self.f=False
self.iready()
self.listen()
def iready(self):
self.send("USER "+self.name+" 0 * :"+self.name)
self.send("NICK "+self.name)
def listen(self):
tmp=True
tmp2=False
pi=re.compile("PING :(.+)")
while True:
recv=self.s.recv(4096)
if tmp2:
t=recv.split(" ")[6:]
t[6]=t[6][1:]
r=re.compile(".?(\w+)\!.+")
for i in t:
self.ign=r.match(i).groups()[0],True
if pi.match(recv):
self.send("PONG :"+str(pi.match(recv).groups()[0]))
elif tmp and re.match(".+00.+",recv):
tmp=False
self.send("PRIVMSG NickServ :identify "+self.passnick)
sleep(1)
self.send("JOIN "+self.channel+" "+self.chanpass)
sleep(0.5)
tmp2=True
self.send("NAMES "+self.channel)
else:
self.risp(recv)
#methods used to interact with ingored list
def getIgn(self):
ret=[]
for i,j in self.__ign:
if j==False:
ret.append(i)
return ret
def setIgn(self,who,value):
if who in list(self.__ign.keys()):
if value==self.__ign[who]:
return 0
self.__ign[who]=value
return 1
def delIgn(self):
self.__ign={}
#the public version of ignored list, with the specification of its interactive methods
ign=property(getIgn,setIgn,delIgn,"the list of ignored users")
#methods and public var for plugins
def getPlug(self):
act=[]
nonact=[]
for k,v in self.__plugins:
for i,j in v:
if j:
act.append(i)
else:
nonact.append(i)
return "Activated Plugins: "+", ".join(act)+"\n Not Activated Plugins: "+", ".join(nonact)
def setPlug(self,tipe,what,yn):
if yn!=True and yn!=False:
raise TypeError("Incorrect value for yn. yn must be a bool istance")
if yn:
if hasattr(plugins,tipe+"."+what):
self.__plugins[tipe][what]=True
self._classlist.append(getattr(plugins,tipe+"."+what))
return "Done"
else:
return "That plugin does not exists"
else:
self._classlist.remove(getattr(plugins,tipe+"."+what))
return "Done"
def delPlug(self):
pass
plugins=property(getPlug,setPlug,delPlug,"the plugins var")
#wrapper of sending messages (it adds "\r\n" on itself)
def send(self,mex):
if mex[-2:]!="\r\n":
mex+="\r\n"
mex=bytes(mex,self.serverEncode)
self.s.send(mex)
mex=mex.decode(self.serverEncode)
wp=self.parse(mex,"OUT")
if self.f:
self.f.write(mex)
print(wp)
#parser for messages
comp={"PRIVMSG" : "a Message", "NICK" : "a Nick string", "USER" : "a User string", \
"MODE" : "a Mode", "KICK" : "a Kick", "NOTICE" : "a notice", "QUIT" : "a Quit string",\
"INVITE" : "an invite", "JOIN" : "a Join string", "PART" : "a Part string", "PING" : "a Ping", "PONG" : "a Pong" }
_parse=re.compile("([A-Z]+)\s(\#?\w+)\s(\:.+|\w+\s\:.+|\+[a-zA-Z]\s?.*)\s\s")
def parse(self,s,inout):
if not self._parse.match(s):
return ""
else:
tipe,chan,mex=self._parse.match(s).groups()
if inout=="OUT":
ret+="[+] Sending {1} to {2}.\n [.] {3} Sended.\n"
elif inout=="IN":
ret+="[-] {1} was received.\n [.] {2}:{3} Received."
return ret.format(self.comp[tipe],chan,mex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment