Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- #
- # Example program using irc.bot.
- #
- # Joel Rosdahl <joel@rosdahl.net>
- #export VERSIONER_PYTHON_PREFER_32_BIT=yes
- """A simple example bot.
- This is an example bot that uses the SingleServerIRCBot class from
- irc.bot. The bot enters a channel and listens for commands in
- private messages and channel traffic. Commands in channel messages
- are given by prefixing the text by the bot name followed by a colon.
- It also responds to DCC CHAT invitations and echos data sent in such
- sessions.
- The known commands are:
- stats -- Prints some channel information.
- disconnect -- Disconnect the bot. The bot will try to reconnect
- after 60 seconds.
- die -- Let the bot cease to exist.
- dcc -- Let the bot invite you to a DCC CHAT connection.
- """
- import irc.bot
- import irc.strings
- from irc.client import ip_numstr_to_quad, ip_quad_to_numstr
- import urllib2
- import urllib
- from time import sleep,strftime
- import threading
- import sys
- #irc.client.ServerConnection.buffer_class = irc.buffer.LenientDecodingLineBuffer
- #irc.client.ServerConnection.buffer_class = irc.client.LineBuffer
- class IgnoreErrorsBuffer(irc.buffer.DecodingLineBuffer):
- def handle_exception(self):
- pass
- irc.client.ServerConnection.buffer_class = IgnoreErrorsBuffer
- def main():
- server = 'irc.utonet.org'
- port = 6667
- channel = '#zx'
- nickname = 'Petra'
- botpass = '560092'
- channelpass=''
- sitepass = 'ss23123petra23'
- domain = 'petra'
- bot = TestBot(channel,channelpass, nickname, server, botpass,sitepass, domain, port)
- bot.start()
- class TestBot(irc.bot.SingleServerIRCBot):
- def __init__(self, channel, channelpass, nickname, server, botpass,sitepass, domain, port=6667):
- irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
- self.channel = channel
- self.botpass = botpass
- self.channelpass = channelpass
- self.sitepass = sitepass
- self.domain = domain
- self.topics = {}
- def on_nicknameinuse(self, c, e):
- c.nick(c.get_nickname() + "_")
- def on_welcome(self, c, e):
- c.privmsg('NickServ','Identify '+self.botpass)
- sleep(1)
- #c.privmsg('chanserv','invite '+self.channel)
- #sleep(1)
- #print "join",self.channel,self.channelpass
- #c.join(self.channel,self.channelpass)
- #sleep(2)
- c.mode(c.get_nickname(),'+B')
- c.mode(c.get_nickname(),'+R')
- #self.ping()
- sleep(5)
- print "run pinger!"
- threading.Thread(target=self.ping).start()
- def on_topic(self, c, e):
- print 'topic change?'
- channel = e.target
- topic = e.arguments[0].decode('latin1')
- c.privmsg(channel ,'Topic changed to:'+topic)
- self.topics[channel] = topic
- def on_privmsg(self, c, e):
- nick = e.source.nick
- match = 0
- for chname, chobj in self.channels.items():
- if nick in chobj.users():
- match = 1
- if match:
- a = e.arguments[0]
- if a[0]=="." or a[0]=="!":
- self.do_command(e, a[1:],1)
- else:
- c.privmsg(nick,'<3 snuggles')
- def on_privnotice(self, c, e):
- print e.arguments[0]
- def on_pubmsg(self, c, e):
- users = self.channels[e.target].users()
- users = ",".join(users)
- channel = e.target.replace("#","")
- user = e.source.nick
- realuser = e.source.user
- msg = e.arguments[0]
- try:
- msg = urllib.quote(msg)
- req = urllib2.Request('http://'+self.domain+'.umunk.net/ircbot/bot.php?p='+self.sitepass+'&u='+user+'&u2='+realuser+'&ch='+channel+'&c=log&msg='+msg+'&users='+users)
- urllib2.urlopen(req)
- except:
- #print 'http://'+self.domain+'.umunk.net/ircbot/bot.php?p='+self.sitepass+'&u='+user+'&u2='+realuser+'&ch='+channel+'&c=log&msg='+msg
- print "Logging failed!"
- a = e.arguments[0]
- if a[0]=="." or a[0]=="!":
- self.do_command(e, a[1:],0)
- return
- def on_join(self, c, e):
- nick = e.source.nick
- #print get_nickname(nick)
- realuser = e.source.user
- realuser = e.source.nick
- channel = e.target.replace("#","")
- try:
- req = urllib2.Request('http://'+self.domain+'.umunk.net/ircbot/bot.php?p='+self.sitepass+'&u='+realuser+'&ch='+channel+'&c=welcome')
- response = urllib2.urlopen(req)
- the_page = response.read()
- the_page = the_page.decode("utf8")
- for line in the_page.split('\n'):
- c.privmsg(nick,line)
- except:
- print "Send welcome to "+realuser+" failed!"
- def ping(self):
- self.keepping = True
- self.failcount = 0
- while self.keepping:
- try:
- req = urllib2.Request('http://'+self.domain+'.umunk.net/ircbot/bot.php?p='+self.sitepass+'&u=bot&c=ping')
- response = urllib2.urlopen(req)
- the_page = response.read()
- the_page = the_page.decode("utf8")
- c = self.connection
- for line in the_page.split('\n'):
- if line[:4] == "#PM#":
- newnick = line[4:line.find("%&%")]
- line = line[line.find("%&%")+3:]
- c.privmsg(newnick,line)
- else:
- c.privmsg(self.channel,line)
- self.failcount = 0
- except:
- print "Pinging failed!"
- self.failcount += 1
- if self.failcount>3:
- print "Failed too much -- shutting down"
- self.keepping = False
- sleep(8)
- #sleep(3)
- #self.ping()
- #threading.Event().wait(3)
- #self.ping()
- #threading.Timer(2, self.ping).start();
- #print "PING",strftime("%H:%M:%S")
- def do_command(self, e, cmd, pm):
- nick = e.source.nick
- #print get_nickname(nick)
- nick3 = e.source.user
- nick2 = e.source.nick
- target = e.target
- # print nick
- c = self.connection
- #cmd = cmd.decode("utf8")
- #print nick, nick2, pm, cmd
- #with open("logs/"+self.domain+".txt", "a") as myfile:
- # myfile.write(nick + " " + nick2 + " " + str(pm) + " " + cmd + "\n")
- if self.failcount>3:
- sleep(4)
- print "Turning on pinger"
- threading.Thread(target=self.ping).start()
- if cmd == "disconnect":
- self.disconnect()
- elif cmd == "die":
- self.die()
- elif cmd == "ping":
- print "Turning off pinger"
- self.keepping = False
- sleep(4)
- #del pinging
- print "Turning on pinger"
- threading.Thread(target=self.ping).start()
- elif cmd == "snugs2":
- print "join"
- c.join(self.channel, self.channelpass)
- elif cmd.find("join")==0:
- join = cmd[cmd.find(" "):]
- c.privmsg('chanserv','invite '+join)
- print "join"
- c.join(join)
- elif cmd.find("leave")==0:
- leave = cmd[cmd.find(" "):]
- print "leave"
- c.part(leave)
- elif cmd == "topic":
- print "trying to change", target
- c.topic(target)
- #c.topic(target,"munkbot")
- c.send_raw("TOPIC " + target)
- #print("%s" % topic)
- #topic = c.topic("#munktest")
- #c.topic("#munktest")
- #print("%s" % topic)
- if target in self.topics:
- c.privmsg(target,self.topics[target])
- else:
- c.privmsg(target, "I have no clue what topic is, sorry")
- elif cmd == "snugs":
- print "Identify"
- c.privmsg('NickServ','Identify '+self.botpass)
- print "Invite myself command: Invite "+self.channel
- c.privmsg('chanserv','invite '+self.channel)
- print "join"
- c.join(self.channel, self.channelpass)
- elif cmd == "stats":
- for chname, chobj in self.channels.items():
- c.notice(nick, "--- Channel statistics ---")
- c.notice(nick, "Channel: " + chname)
- users = chobj.users()
- users.sort()
- c.notice(nick, "Users: " + ", ".join(users))
- #opers = chobj.opers()
- #opers.sort()
- #c.notice(nick, "Opers: " + ", ".join(opers))
- #voiced = chobj.voiced()
- #voiced.sort()
- # c.notice(nick, "Voiced: " + ", ".join(voiced))
- elif cmd == "slapall":
- for chname, chobj in self.channels.items():
- if chname == target:
- users = chobj.users()
- users.sort()
- c.privmsg(target, "SLAP!!! " + ", ".join(users))
- else:
- try:
- cmd = urllib.quote(cmd)
- channel = target.replace("#","")
- req = urllib2.Request('http://'+self.domain+'.umunk.net/ircbot/bot.php?p='+self.sitepass+'&u='+nick2+'&ch='+channel+'&c='+cmd)
- print 'http://'+self.domain+'.umunk.net/ircbot/bot.php?u='+nick2+'&ch='+channel+'&c='+cmd
- response = urllib2.urlopen(req)
- the_page = response.read()
- the_page = the_page.decode("utf8")
- # print the_page
- if the_page[:4] == "#PM#":
- pm = 1
- the_page = the_page[4:]
- if (len(the_page.split('\n'))<50):
- for line in the_page.split('\n'):
- #print line
- if line[:4] == "#PM#":
- newnick = line[4:line.find("%&%")]
- line = line[line.find("%&%")+3:]
- c.privmsg(newnick,line)
- elif pm:
- c.privmsg(nick,line)
- else:
- c.privmsg(target,line)
- except:
- print "Error sending msg!"
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement