From d9d567cd7241c633c5ce5ef7665399c9c71d257a Mon Sep 17 00:00:00 2001 From: Juhani Haverinen Date: Sun, 30 Jun 2013 01:01:33 +0300 Subject: [PATCH] first commit --- botcmd.py | 16 +++++++ botcmd.pyc | Bin 0 -> 1143 bytes ircbot.py | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 botcmd.py create mode 100644 botcmd.pyc create mode 100644 ircbot.py diff --git a/botcmd.py b/botcmd.py new file mode 100644 index 0000000..ce70dda --- /dev/null +++ b/botcmd.py @@ -0,0 +1,16 @@ +opnicks=['nortti','nortti_','shikhin','shikhin`','`shikhin','^[]','sortiecat','martinFTW','graphitemaster','XgF'] +opchans=['#osdev-offtopic'] +oprights={} +for i in opnicks: + oprights[i]=opchans + +def parse((line,irc)): + line=line.split(' ') + if line[1]=='PRIVMSG' and line[3]==':#echo': + irc.send('PRIVMSG %s :%s'%(line[2],' '.join(line[4:]))) + elif line[1]=='PRIVMSG' and line[3]==':#op': + irc.send('PRIVMSG NickServ :ACC '+line[0].split('!')[0][1:]) + elif line[1]=='NOTICE' and line[0].split('!')[0]==':NickServ' and line[4]=='ACC': + if line[3][1:] in oprights and int(line[5])==3: + for chan in oprights[line[3][1:]]: + irc.send('MODE %s +o %s'%(chan,line[3][1:])) diff --git a/botcmd.pyc b/botcmd.pyc new file mode 100644 index 0000000000000000000000000000000000000000..08f22cc2faee35b8d553a6a8081fc4d75c743e50 GIT binary patch literal 1143 zcmbVMT~8B16uq-8r7aZDDB34#kfcbY#>WPYrhwsrqClW2A5wRx-D&C0vNI4&ATiND z z!59S@3dSkOQjnugo|*vUsUxTO4<_gfFyNV_ zopJ&EHUkss#9C#?Zoy!AFfZFb27@LkU2g$vN=Y8L`lD8o!4){i`IAd z?G$pIq!owC%6`jOnONS|=&YM*%=6j=^5Us(x2=wq?+e7z7!w#Z40DLc715zb*MhD+ zx)RiNqI`xc*o8Vp$kX#EX*)j+e{AG?)CDBt65~iubD(H@G>b_@KmMZnc0-kO zRPYB>d6$@=B9}2q*bG^VC}Qq`Bl>znRB#&`f@}N-KXoga#s2AAK0g5XAXG0yNkiDQ zz*AtIDdXoI^@xo%nal49%b z{_Xb0-sa95#aCsxN2>GE54GXj9Og<3rc_-p%0o!;l^HhGc^#YCTd2D7dpk0@C{aHA z(bX!l+^{-MR425?ks(%QnB`iRpE=~IisT@f_%7gwt*DGVIb#;Fs)V{PE8TNsR3%Py zSNhgew2nS#yfDuznEZaQ9Dj7>#I3|geu>OIm=xv2v?z%sj79N6JP}1vAJonm9Y+wx vII(riXPa$)ADlL7`#zSxi}|05vS3*0>)`xUzGS@(e^(xhoXCiRnDTxB0uA1W literal 0 HcmV?d00001 diff --git a/ircbot.py b/ircbot.py new file mode 100644 index 0000000..241f22b --- /dev/null +++ b/ircbot.py @@ -0,0 +1,127 @@ +#!/usr/bin/python +import threading +import time +import socket +import sys + +import botcmd + +class Channel: + def __init__(self): + self.lock=threading.Lock() + self.msg=[] + def send(self,msg): + self.lock.acquire() + self.msg.append(msg) + self.lock.release() + def recv(self,wait=True): + while True: + self.lock.acquire() + if len(self.msg)>0: + msg=self.msg.pop(0) + self.lock.release() + return msg + if not wait: + self.lock.release() + return None + self.lock.release() + time.sleep(0.1) + +class Connhandler(threading.Thread): + def __init__(self,server,port,chan,nick,botname,inpc,logc): + threading.Thread.__init__(self) + self.server=server + self.port=port + self.nick=nick + self.name=botname + self.chan=chan + self.inpc=inpc + self.logc=logc + def send(self,s): + self.sock.send(s+'\r\n') + if s.split(' ')[0]!='PONG': + self.logc.send(s+'\n') + def check(self,line): + if line.split(' ')[0]=='PING': + self.send('PONG :hjdicks') + else: + self.logc.send(line+'\n') + Threadwrapper(botcmd.parse,(line,self.inpc)).start() + def run(self): + self.sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sock.connect((self.server,self.port)) + self.sock.settimeout(0.1) + + self.send('NICK %s'%self.nick) + self.send('USER %s a a :%s'%(self.nick,self.name)) + self.send('JOIN %s'%(self.chan)) + + buf='' + while True: + while True: + try: + data=self.sock.recv(4096) + break + except: + pass + cmd=self.inpc.recv(wait=False) + if cmd=='QUIT': + data=None + self.logc.send('QUIT') + break + elif cmd: + self.send(cmd) + time.sleep(0.1) + if not data: break + buf+=data + buf=buf.split('\n') + for line in buf[:-1]: + if line[-1]=='\r': line=line[:-1] + self.check(line) + buf=buf[-1] + self.sock.close() + +class Keyhandler(threading.Thread): + def __init__(self,outc): + self.outc=outc + threading.Thread.__init__(self) + def run(self): + while True: + line=raw_input() + c=line.split(' ') + if c[0]=='/j' and len(c)==2: + self.outc.send('JOIN '+c[1]) + elif c[0]=='/m' and len(c)==3: + self.outc.send('PRIVMSG %s :%s'%(c[1],' '.join(c[2:]))) + elif c[0]=='/q' and len(c)==1: + self.outc.send('QUIT') + break + elif c[0][0]=='/': + self.outc.send(c[0][1:].upper()+' '.join(c[1:])) + +class Loghandler(threading.Thread): + def __init__(self,inpc): + self.inpc=inpc + threading.Thread.__init__(self) + def run(self): + while True: + s=self.inpc.recv() + if s=='QUIT': break + sys.stdout.write(s) + +class Threadwrapper(threading.Thread): + def __init__(self,func,arg): + self.func=func + self.arg=arg + threading.Thread.__init__(self) + def run(self): + self.func(self.arg) + +if len(sys.argv)!=5: + print 'Usage: '+sys.argv[0]+' server port channel nick' +else: + keych=Channel() + logch=Channel() + Keyhandler(keych).start() + Loghandler(logch).start() + Connhandler(sys.argv[1],int(sys.argv[2]),sys.argv[3],sys.argv[4],sys.argv[4],keych,logch).start()