Advertisement
Guest User

bot.py

a guest
Dec 11th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.63 KB | None | 0 0
  1. #! /usr/bin/env python
  2. #
  3. # Example program using irc.bot.
  4. #
  5. # Joel Rosdahl <joel@rosdahl.net>
  6.  
  7. #export VERSIONER_PYTHON_PREFER_32_BIT=yes
  8.  
  9. """A simple example bot.
  10.  
  11. This is an example bot that uses the SingleServerIRCBot class from
  12. irc.bot. The bot enters a channel and listens for commands in
  13. private messages and channel traffic. Commands in channel messages
  14. are given by prefixing the text by the bot name followed by a colon.
  15. It also responds to DCC CHAT invitations and echos data sent in such
  16. sessions.
  17.  
  18. The known commands are:
  19.  
  20. stats -- Prints some channel information.
  21.  
  22. disconnect -- Disconnect the bot. The bot will try to reconnect
  23. after 60 seconds.
  24.  
  25. die -- Let the bot cease to exist.
  26.  
  27. dcc -- Let the bot invite you to a DCC CHAT connection.
  28. """
  29.  
  30. import irc.bot
  31. import irc.strings
  32. from irc.client import ip_numstr_to_quad, ip_quad_to_numstr
  33. import urllib2
  34. import urllib
  35. from time import sleep,strftime
  36. import threading
  37.  
  38.  
  39. import sys
  40. #irc.client.ServerConnection.buffer_class = irc.buffer.LenientDecodingLineBuffer
  41. #irc.client.ServerConnection.buffer_class = irc.client.LineBuffer
  42. class IgnoreErrorsBuffer(irc.buffer.DecodingLineBuffer):
  43. def handle_exception(self):
  44. pass
  45. irc.client.ServerConnection.buffer_class = IgnoreErrorsBuffer
  46. def main():
  47. server = 'irc.utonet.org'
  48. port = 6667
  49.  
  50.  
  51.  
  52. channel = '#zx'
  53. nickname = 'Petra'
  54. botpass = '560092'
  55. channelpass=''
  56. sitepass = 'ss23123petra23'
  57. domain = 'petra'
  58.  
  59. bot = TestBot(channel,channelpass, nickname, server, botpass,sitepass, domain, port)
  60. bot.start()
  61.  
  62.  
  63.  
  64.  
  65. class TestBot(irc.bot.SingleServerIRCBot):
  66. def __init__(self, channel, channelpass, nickname, server, botpass,sitepass, domain, port=6667):
  67.  
  68. irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
  69. self.channel = channel
  70. self.botpass = botpass
  71. self.channelpass = channelpass
  72. self.sitepass = sitepass
  73. self.domain = domain
  74. self.topics = {}
  75.  
  76.  
  77. def on_nicknameinuse(self, c, e):
  78. c.nick(c.get_nickname() + "_")
  79.  
  80. def on_welcome(self, c, e):
  81. c.privmsg('NickServ','Identify '+self.botpass)
  82. sleep(1)
  83. #c.privmsg('chanserv','invite '+self.channel)
  84. #sleep(1)
  85. #print "join",self.channel,self.channelpass
  86. #c.join(self.channel,self.channelpass)
  87. #sleep(2)
  88. c.mode(c.get_nickname(),'+B')
  89. c.mode(c.get_nickname(),'+R')
  90. #self.ping()
  91. sleep(5)
  92. print "run pinger!"
  93. threading.Thread(target=self.ping).start()
  94.  
  95. def on_topic(self, c, e):
  96. print 'topic change?'
  97. channel = e.target
  98. topic = e.arguments[0].decode('latin1')
  99. c.privmsg(channel ,'Topic changed to:'+topic)
  100. self.topics[channel] = topic
  101.  
  102.  
  103.  
  104. def on_privmsg(self, c, e):
  105. nick = e.source.nick
  106. match = 0
  107. for chname, chobj in self.channels.items():
  108. if nick in chobj.users():
  109. match = 1
  110.  
  111. if match:
  112. a = e.arguments[0]
  113. if a[0]=="." or a[0]=="!":
  114. self.do_command(e, a[1:],1)
  115. else:
  116. c.privmsg(nick,'<3 snuggles')
  117.  
  118. def on_privnotice(self, c, e):
  119. print e.arguments[0]
  120.  
  121. def on_pubmsg(self, c, e):
  122.  
  123. users = self.channels[e.target].users()
  124. users = ",".join(users)
  125. channel = e.target.replace("#","")
  126. user = e.source.nick
  127. realuser = e.source.user
  128. msg = e.arguments[0]
  129.  
  130. try:
  131. msg = urllib.quote(msg)
  132. 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)
  133. urllib2.urlopen(req)
  134. except:
  135. #print 'http://'+self.domain+'.umunk.net/ircbot/bot.php?p='+self.sitepass+'&u='+user+'&u2='+realuser+'&ch='+channel+'&c=log&msg='+msg
  136. print "Logging failed!"
  137.  
  138.  
  139. a = e.arguments[0]
  140. if a[0]=="." or a[0]=="!":
  141. self.do_command(e, a[1:],0)
  142. return
  143.  
  144. def on_join(self, c, e):
  145. nick = e.source.nick
  146. #print get_nickname(nick)
  147. realuser = e.source.user
  148. realuser = e.source.nick
  149. channel = e.target.replace("#","")
  150. try:
  151. req = urllib2.Request('http://'+self.domain+'.umunk.net/ircbot/bot.php?p='+self.sitepass+'&u='+realuser+'&ch='+channel+'&c=welcome')
  152. response = urllib2.urlopen(req)
  153. the_page = response.read()
  154. the_page = the_page.decode("utf8")
  155.  
  156. for line in the_page.split('\n'):
  157. c.privmsg(nick,line)
  158. except:
  159. print "Send welcome to "+realuser+" failed!"
  160.  
  161. def ping(self):
  162. self.keepping = True
  163. self.failcount = 0
  164. while self.keepping:
  165. try:
  166. req = urllib2.Request('http://'+self.domain+'.umunk.net/ircbot/bot.php?p='+self.sitepass+'&u=bot&c=ping')
  167. response = urllib2.urlopen(req)
  168. the_page = response.read()
  169. the_page = the_page.decode("utf8")
  170. c = self.connection
  171. for line in the_page.split('\n'):
  172. if line[:4] == "#PM#":
  173. newnick = line[4:line.find("%&%")]
  174. line = line[line.find("%&%")+3:]
  175. c.privmsg(newnick,line)
  176. else:
  177. c.privmsg(self.channel,line)
  178.  
  179. self.failcount = 0
  180. except:
  181. print "Pinging failed!"
  182.  
  183. self.failcount += 1
  184. if self.failcount>3:
  185. print "Failed too much -- shutting down"
  186. self.keepping = False
  187. sleep(8)
  188. #sleep(3)
  189. #self.ping()
  190. #threading.Event().wait(3)
  191. #self.ping()
  192. #threading.Timer(2, self.ping).start();
  193. #print "PING",strftime("%H:%M:%S")
  194.  
  195.  
  196. def do_command(self, e, cmd, pm):
  197. nick = e.source.nick
  198. #print get_nickname(nick)
  199. nick3 = e.source.user
  200.  
  201. nick2 = e.source.nick
  202. target = e.target
  203. # print nick
  204. c = self.connection
  205. #cmd = cmd.decode("utf8")
  206. #print nick, nick2, pm, cmd
  207. #with open("logs/"+self.domain+".txt", "a") as myfile:
  208. # myfile.write(nick + " " + nick2 + " " + str(pm) + " " + cmd + "\n")
  209.  
  210. if self.failcount>3:
  211. sleep(4)
  212. print "Turning on pinger"
  213. threading.Thread(target=self.ping).start()
  214.  
  215. if cmd == "disconnect":
  216. self.disconnect()
  217. elif cmd == "die":
  218. self.die()
  219. elif cmd == "ping":
  220. print "Turning off pinger"
  221. self.keepping = False
  222. sleep(4)
  223. #del pinging
  224. print "Turning on pinger"
  225. threading.Thread(target=self.ping).start()
  226. elif cmd == "snugs2":
  227. print "join"
  228. c.join(self.channel, self.channelpass)
  229. elif cmd.find("join")==0:
  230. join = cmd[cmd.find(" "):]
  231. c.privmsg('chanserv','invite '+join)
  232. print "join"
  233. c.join(join)
  234. elif cmd.find("leave")==0:
  235. leave = cmd[cmd.find(" "):]
  236. print "leave"
  237. c.part(leave)
  238. elif cmd == "topic":
  239. print "trying to change", target
  240. c.topic(target)
  241. #c.topic(target,"munkbot")
  242. c.send_raw("TOPIC " + target)
  243. #print("%s" % topic)
  244. #topic = c.topic("#munktest")
  245. #c.topic("#munktest")
  246. #print("%s" % topic)
  247. if target in self.topics:
  248. c.privmsg(target,self.topics[target])
  249. else:
  250. c.privmsg(target, "I have no clue what topic is, sorry")
  251. elif cmd == "snugs":
  252. print "Identify"
  253. c.privmsg('NickServ','Identify '+self.botpass)
  254. print "Invite myself command: Invite "+self.channel
  255. c.privmsg('chanserv','invite '+self.channel)
  256. print "join"
  257. c.join(self.channel, self.channelpass)
  258. elif cmd == "stats":
  259. for chname, chobj in self.channels.items():
  260. c.notice(nick, "--- Channel statistics ---")
  261. c.notice(nick, "Channel: " + chname)
  262. users = chobj.users()
  263. users.sort()
  264. c.notice(nick, "Users: " + ", ".join(users))
  265. #opers = chobj.opers()
  266. #opers.sort()
  267. #c.notice(nick, "Opers: " + ", ".join(opers))
  268. #voiced = chobj.voiced()
  269. #voiced.sort()
  270. # c.notice(nick, "Voiced: " + ", ".join(voiced))
  271. elif cmd == "slapall":
  272. for chname, chobj in self.channels.items():
  273. if chname == target:
  274. users = chobj.users()
  275. users.sort()
  276. c.privmsg(target, "SLAP!!! " + ", ".join(users))
  277. else:
  278. try:
  279. cmd = urllib.quote(cmd)
  280. channel = target.replace("#","")
  281. req = urllib2.Request('http://'+self.domain+'.umunk.net/ircbot/bot.php?p='+self.sitepass+'&u='+nick2+'&ch='+channel+'&c='+cmd)
  282. print 'http://'+self.domain+'.umunk.net/ircbot/bot.php?u='+nick2+'&ch='+channel+'&c='+cmd
  283. response = urllib2.urlopen(req)
  284. the_page = response.read()
  285. the_page = the_page.decode("utf8")
  286.  
  287. # print the_page
  288.  
  289. if the_page[:4] == "#PM#":
  290. pm = 1
  291. the_page = the_page[4:]
  292.  
  293. if (len(the_page.split('\n'))<50):
  294. for line in the_page.split('\n'):
  295. #print line
  296.  
  297. if line[:4] == "#PM#":
  298. newnick = line[4:line.find("%&%")]
  299. line = line[line.find("%&%")+3:]
  300. c.privmsg(newnick,line)
  301. elif pm:
  302. c.privmsg(nick,line)
  303. else:
  304. c.privmsg(target,line)
  305. except:
  306. print "Error sending msg!"
  307.  
  308.  
  309.  
  310. if __name__ == "__main__":
  311. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement