from client2 import * import re, string, socket, signal, time, sys, WConio, pythonica2 identd = "testing" fingerresponse = "Nothing to see here" versionresponse = "UnityBot in Python version 0.01 by inhahe, using a modified version of IRCClient by Adam Sampson" users = eval(open("bot.users.txt","r").read()) logins = {} def getwho(source): if source.find("!")>-1: return source.split("!",1)[1] else: return None def ctrlc(a, b): global breakout for sc in serverconnections: sc.quit(" the properties he struggles with are exactly the same, he thinks, as powers stars in their cores") breakout=True help = {} def cmdcommands(sc, source, account, to, param, data): sc.say(sc, source, """ +==============================================+ | unityBot v1.0 commands: | |----------------------------------------------| | !commands = display this page. | | !help [command] = display help on a command. | |----------------------------------------------| | !acronym !encrypt !library !seen | | !bible !essay !memo !sum | | !calc !google !more !translate | | !comp !glossary !organelle !webster | | !decrypt !hitchcock !ping !wordnet | | !easton !jargon !prime | | !element !koran !rss | |----------------------------------------------| | !login !logout !profile !register | |----------------------------------------------| | !ascii !borgism !chess !ching | +==============================================+ """) help["commands"] = """This command displays a list of unityBot commands in a private message. Usage: !commands""" def cmdcalc(sc, source, account, to, param, data): if to[0] in "#&": source=to if param!=None: data.insert(0, "-"+param) try: a=pythonica2.processline(string.join(data," "))[2] except: a="Huh?" sc.say(source, a) help["calc"] = """This command solves a mathematical expression. Valid symbols are: /. ReplaceAll -> Rule == Equal = Set =. Unset + Plus - Minus * Times / Divide ^ Power () grouping [a-zA-Z][a-zA-Z0-9]* Any arbitrary variable j Coefficient for imaginary component of vector (i.e. complex number)  PyBot does not like very large numbers and does not divide whole numbers accurately.""" def cmdhelp(sc, source, account, to, param, data): if len(data)==1 and data[0] in help: sc.say(source, help[data[0]]) elif len(data)==0: cmdcommands(sc, source, account, to, param, data) else: sc.say(source, string.join(data, " ") + ": Unknown command") help["help"] = "Yes." def cmdregister(sc, source, account, to, param, data): if to[0] in "#&": sc.say(source, "Account registration must take place in a private message.") elif len(data)<2: sc.say(source, "Please specify a username and password.") elif len(data)>2: sc.say(source, "Username and password must contain no spaces.") elif data[0] in [x[0] for x in users]: sc.say(source, "The username " + data[0] + " is already registsred.") else: users[data[0]] = {"password": data[1]} open("bot.users.txt", "w").write(repr(users)) sc.say(source, "Your account has been created. Type !help profile for information on creating your public profile.") help["register"] = "Usage: !register [username] [password] where [username] and [password] are your chosen account username and password respectively. This must be done via private message." def cmdlogin(sc, source, account, to, param, data): if to[0] in "#&": sc.say(source, "Login must take place in a private message.") elif account!=None: sc.say(source, "Confucius say: You must log off before you log on.") elif getwho(source) not in [getwho(x) for y in self.channels for x in y[1]]: sc.say(source, "You must be in a channel that I am in to login.") elif len(data)<2: sc.say(source, "Please specify a username and password.") elif data[0] not in users: sc.say(source, "N'existe pas") elif users[data[0]]["password"] != data[1]: sc.say(source, "That password is invalid.") else: logins[getwho(source)] = data[0] sc.say(source, "You are now logged into PyBot.") help["login"] = "Usage: !login [username] [password] where [username] and [password] are your chosen account username and password respectively. This must be done via private message." class servercomm(LogAllMixin,IRCClient): #class servercomm(IRCClient): def __init__(self, network, server, port): self.server = server IRCClient.__init__(self, server, port) self.network=network self.channels = {} def handle_invite(self, source, to, channel): pass def handle_join(self, fullnick, channel): self.channels[channel.lower()][1].append(fullnick) def handle_part(self, fullnick, channel, reason): for nick in self.channels[channel.lower()][1]: if nick == getnick(fullnick): self.channels[channel.lower()][1].remove(nick) if getnick(fullnick) not in [getnick(x) for y in self.channels for x in y[1]]: if getwho(fullnick) in logins: del logins[getwho(fullnick)] def handle_quit(self, fullnick, reason): for channel in self.channels: self.channels[channel][1].remove(fullnick) if getwho(fullnick) in logins: del logins[getwho(fullnick)] def handle_nick(self, oldfull, newshort): for channel in self.channels: for nick in self.channels[channel.lower()][1]: if nick == getnick(oldfull): self.channels[channel.lower()][1].remove(nick) self.channels[channel][1].append(newshort + "!" + getwho(oldfull)) # may have inconsistency problems with channel cases def handle_say(self, source, to, message): if message[0]=="!": m = message.split() param=None data = m[1:] if len(m)>1: if m[1][0]=="-": param=m[1][1:].lower() data = m[2:] func = "cmd"+m[0][1:].lower() account=None if getwho(source) in logins: account = getwho(source) if func in globals(): globals()["cmd"+m[0][1:].lower()](self, source, account, to, param, data) else: self.say(source, chr(199)+"a ne me dit rien") elif message=="FINGER": self.notice(source, "FINGER "+ fingerresponse + "") elif message=="TIME": self.notice(source, "TIME " + time.asctime(time.localtime()) + "") elif message=="VERSION": self.notice(source, "VERSION "+ versionresponse + "") elif message[:5]=="PING": self.notice(source, message) def handle_reply(self, prefix, code, params): if code==1: #nick registered for channel in networks[self.network][nchannels]: self.join(channel) self.channels[channel]=[channel,[]] elif code==379: #forwarding to another channel self.channels[params.split(' ')[2].lower()]=[parames.split()[2],[]] del self.channels[params.split(' ')[1]] elif code==353: #names list channel = params.split(' ')[2] self.channels[channel.lower()][0]=channel for name in params.split(':')[1].split(): self.channels[channel.lower()][1].append(name) elif code==433: #nickname is already in use self.nick("blah123") def handle_command(self, prefix, command, params, line=None): pass for n in [22, 21, 8, 4, 2, 11, 15]: signal.signal(n, ctrlc) breakout =False flooddelay=.2 networks = {"dalnet":[[["irc.dal.net",6667]],["#freethinkers", "#unitybottest"],["PyBot"], "PyBot", "PyBot"],"undernet":[[["irc.undernet.org",6667]],["#bottest", "#bottest2"],["UnityBotPy"], "UnityBotPy", "UnityBotPy"]} nservers, nchannels, nnicks, nrealname = 0, 1, 2, 3 nnserver, nnport = 0, 1 serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.bind((socket.gethostname(), 113)) serversocket.listen(5) serversocket.settimeout(.001) serverconnections = [] for (network, config) in networks.iteritems(): serverconnections.append(servercomm(network, config[nservers][0][nnserver], config[nservers][0][nnport])) serverconnections[-1:][0].connect(config[nnicks][0], config[nrealname]) while breakout==False: for sc in serverconnections: try: sc.processonce() except socket.error: pass try: (clientsocket, address) = serversocket.accept() r = clientsocket.recv(1000) clientsocket.send(r.strip() + " : USERID : UNIX : " + identd + "\r\n" ) except socket.error, socket.timeout: pass if WConio.kbhit(): k = WConio.getkey() if k=="r": print "Enter raw command to send, and hurry up because operations are being blocked." print "Command: ", m = raw_input().split(" ",1) for sc in serverconnections: if sc.network==m[0].lower(): sc.send(m[1]) elif k=="n": for serverc in serverconnections: print serverc.channels #todo: # try alternate servers # use xpaths for config, read from UnityBot.conf # use alternate nicks # detect ERROR : Closing Link # ctcp # version # time # TIME # VERSION # /me = ACTION text # self.my_nick in client2.py must respond to server nick notification # otherwise remove interlacing and make additifo automatic in ircclient # finger: # uptime, online time for that network, number of channels, number of # users logged in, number of accounts, number of profiles, # number of memos, number of chess games, cpu, ram & os, age of bot, total # time online, number of lines logged, number of commands issued