Advertisement
Guest User

Untitled

a guest
Jun 11th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.25 KB | None | 0 0
  1. import sys
  2. import re
  3. import os
  4. import random
  5. from twisted.internet import reactor
  6. from twisted.words.protocols import irc
  7. from twisted.internet import protocol
  8. from collections import defaultdict
  9.  
  10.  
  11. class RelayBot(irc.IRCClient):
  12.     watchers = ['']
  13.     wchans = ['#minezinemirror']
  14.     watchlist = ['#minezinec']
  15.     authd = []
  16.     password = "Notchium"
  17.     tempnames = []
  18.     tempchannel = []
  19.     Names = {}
  20.     channels = []
  21.     shutup = False
  22.     def _get_nickname(self):
  23.         return self.factory.nickname
  24.     nickname = property(_get_nickname)
  25.  
  26.     def signedOn(self):
  27.         self.msg("nickserv", "identify Char7zard")
  28.         self.join('#minezinec')
  29.         self.join('#minezinemirror')
  30.         print "Signed on as %s." % self.nickname
  31.  
  32.     def joined(self, channel):
  33.         print "Joined %s." % channel
  34.         # self.sendLine("NAMES %s" % channel)
  35.         self.channels.append(channel)
  36.  
  37.     def privmsg(self, user, channel, msg):
  38.         userhost = user
  39.         user = user.split("!")[0]
  40.         print ("<%s:%s> %s" % (user, channel, msg))
  41.         if channel in self.watchlist:
  42.             for name in self.watchers:
  43.                 self.msg(name, "<4%s> 3%s" % (user, msg))
  44.             if not self.shutup:
  45.                 for name in self.wchans:
  46.                     self.msg(name, "<4%s> 3%s" % (user, msg))
  47.         elif msg.startswith("!"):
  48.             command = msg.split(" ")[0].lstrip("!")
  49.             args = msg.split(" ")[1:]
  50.             if command == "help":
  51.                 try:
  52.                     comm = args[0]
  53.                 except IndexError:
  54.                     self.msg(channel, "%s: Commands: help wadd, wdel, login, logout, mewatch, meunwatch, radd, rdel, stfu, unstfu." % user)
  55.                     self.msg(channel, "Try !help command for more information.")
  56.                 else:
  57.                     comm = args[0]
  58.                     if comm == "help":
  59.                         self.msg(channel, "%s: You're reading it, stupid." % user)
  60.                     elif comm == "wadd":
  61.                         self.msg(channel, "%s: Adds and joins a channel to be watched and relayed from." % user)
  62.                     elif comm == "wdel":
  63.                         self.msg(channel, "%s: Parts from any channel currently being watched." % user)
  64.                     elif comm == "login":
  65.                         self.msg(channel, "%s: Logs you in as an administrator." % user)
  66.                     elif comm == "logout":
  67.                         self.msg(channel, "%s: Logs you out if you're logged in." % user)
  68.                     elif comm == "mewatch":
  69.                         self.msg(channel, "%s: Makes me query you when I relay." % user)
  70.                     elif comm == "meunwatch":
  71.                         self.msg(channel, "%s: Stops me from querying you when I relay." % user)
  72.                     elif comm == "radd":
  73.                         self.msg(channel, "%s: Adds and joins a channel to relay to." % user)
  74.                     elif comm == "rdel":
  75.                         self.msg(channel, "%s: Parts a channel I'm relaying to." % user)
  76.                     elif comm == "topic":
  77.                         self.msg(channel, "%s: Requests the topic for a channel." % user)
  78.                     elif comm == "names":
  79.                         self.msg(channel, "%s: Returns the userlist for a channel." % user)
  80.                     else:
  81.                         self.msg(channel, "%s: No help topic for %s." % (user, comm))
  82.             elif command == "stfu":
  83.                 if userhost in self.authd:
  84.                     self.shutup = True
  85.                     self.msg(channel, "%s: Paused relaying to channels." % user)
  86.                 else:
  87.                     self.msg(channel, "%s: You are not authorized to do that." % user)
  88.             elif command == "unstfu":
  89.                 if userhost in self.authd:
  90.                     self.shutup = False
  91.                     self.msg(channel, "%s: Continuing to relay to channels." % user)
  92.                 else:
  93.                     self.msg(channel, "%s: You are not authorized to do that." % user)
  94.             elif command == "wadd":
  95.                 if userhost in self.authd:
  96.                     if len(args) > 0:
  97.                         chan = args[0]
  98.                         if chan.startswith("#"):
  99.                             if not chan in self.wchans:
  100.                                 self.msg(channel, "%s: Joining and watching %s." % (user, chan))
  101.                                 self.watchlist.append(chan)
  102.                                 self.join(chan)
  103.                             else:
  104.                                 self.msg(channel, "%s: I'm relaying to that channel!" % name)
  105.                         else:
  106.                             self.msg(channel, "%s: %s is not a valid channel name." % (user, chan))
  107.                     else:
  108.                         self.msg(channel, "%s: Syntax: !wadd #channel" % user)
  109.                 else:
  110.                     self.msg(channel, "%s: You are not authorized to do that." % user)
  111.             elif command == "wdel":
  112.                 if userhost in self.authd:
  113.                     if len(args) > 0:
  114.                         chan = args[0]
  115.                         if chan in self.watchlist:
  116.                             self.msg(channel, "%s: Leaving %s." % (user, chan))
  117.                             self.watchlist.remove(chan)
  118.                             self.part(chan)
  119.                         else:
  120.                             self.msg(channel, "%s: Not watching %s." % (user, chan))
  121.                     else:
  122.                         self.msg(channel, "%s: Syntax: !wdel #channel" % user)
  123.                 else:
  124.                     self.msg(channel, "%s: You are not authorized to do that." % user)
  125.             elif command == "login":
  126.                 pw = " ".join(args)
  127.                 if pw == self.password:
  128.                     self.msg(channel, "%s: You are now logged in." % user)
  129.                     self.authd.append(userhost)
  130.                     self.watchers.append(user)
  131.             elif command == "logout":
  132.                 if userhost in self.authd:
  133.                     self.msg(channel, "%s: You are now logged out." % user)
  134.                     self.authd.remove(userhost)
  135.                     self.watchers.remove(user)
  136.                 else:
  137.                     self.msg(channel, "%s: You were never logged in." % user)
  138.             elif command == "mewatch":
  139.                 if userhost in self.authd:
  140.                     if user in self.watchers:
  141.                         self.msg(channel, "%s: You are already a watcher." % user)
  142.                     else:
  143.                         self.msg(channel, "%s: You are now a watcher." % user)
  144.                         self.watchers.append(user)
  145.                 else:
  146.                     self.msg(channel, "%s: You are not authorized to do that." % user)
  147.             elif command == "meunwatch":
  148.                 if userhost in self.authd:
  149.                     if user in self.watchers:
  150.                         self.msg(channel, "%s: You are no longer a watcher." % user)
  151.                         self.watchers.remove(user)
  152.                     else:
  153.                         self.msg(channel, "%s: You were never a watcher." % user)
  154.                 else:
  155.                     self.msg(channel, "%s: You are not authorized to do that." % user)
  156.             elif command == "radd":
  157.                 if userhost in self.authd:
  158.                     if len(args) > 0:
  159.                         chan = args[0]
  160.                         if chan.startswith("#"):
  161.                             if not chan in self.watchlist:
  162.                                 self.msg(channel, "%s: Joining and  %s." % (user, chan))
  163.                                 self.wchans.append(chan)
  164.                                 self.join(chan)
  165.                             else:
  166.                                 self.msg(channel, "%s: I'm monitoring that channel!" % user)
  167.                         else:
  168.                             self.msg(channel, "%s: %s is not a valid channel name." % (user, chan))
  169.                     else:
  170.                         self.msg(channel, "%s: Syntax: !radd #channel" % user)
  171.                 else:
  172.                     self.msg(channel, "%s: You are not authorized to do that." % user)
  173.             elif command == "rdel":
  174.                 if userhost in self.authd:
  175.                     if len(args) > 0:
  176.                         chan = args[0]
  177.                         if chan.startswith("#"):
  178.                             self.msg(channel, "%s: Leaving %s." % (user, chan))
  179.                             self.wchans.remove(chan)
  180.                             self.part(chan)
  181.                         else:
  182.                             self.msg(channel, "%s: %s is not a valid channel name." % (user, chan))
  183.                     else:
  184.                         self.msg(channel, "%s: Syntax: !rdel #channel" % user)
  185.                 else:
  186.                     self.msg(channel, "%s: You are not authorized to do that." % user)
  187.             elif command == "names":
  188.                 if userhost in self.authd:
  189.                     if len(args) > 0:
  190.                         chan = args[0]
  191.                         if chan.startswith("#"):
  192.                             if chan in self.Names.keys():
  193.                                 self.msg(channel, "Names for %s:  %s." % (chan, " ".join(self.Names[chan])))
  194.                             else:
  195.                                 self.msg(channel, "%s: I'm not in that channel or there is no userlist for it!" % user)
  196.                         else:
  197.                             self.msg(channel, "%s: %s is not a valid channel name." % (user, chan))
  198.                     else:
  199.                         self.msg(channel, "%s: Syntax: !names #channel" % user)
  200.                 else:
  201.                     self.msg(channel, "%s: You are not authorized to do that." % user)
  202.             elif command == "topic":
  203.                 if userhost in self.authd:
  204.                     if len(args) > 0:
  205.                         chan = args[0]
  206.                         if chan.startswith("#"):
  207.                             if chan in self.watchlist:
  208.                                 self.topic(chan)
  209.                             else:
  210.                                 self.msg(channel, "%s: I'm not watching that channel!" % user)
  211.                         else:
  212.                             self.msg(channel, "%s: %s is not a valid channel name." % (user, chan))
  213.                     else:
  214.                         self.msg(channel, "%s: Syntax: !names #channel" % user)
  215.                 else:
  216.                     self.msg(channel, "%s: You are not authorized to do that." % user)
  217.             #TODO: !raw, !modes, !umodes
  218.            
  219.     def privnotice(self, user, channel, msg):
  220.         user = user.split("!")[0]
  221.         print ("-%s:%s- %s" % (user, channel, msg))
  222.         if channel in self.watchlist:
  223.             for name in self.watchers:
  224.                 self.msg(name, "-%s:%s- %s" % (user, channel, msg))
  225.             for name in self.wchans:
  226.                 self.msg(name, "-%s:%s- %s" % (user, channel, msg))
  227.  
  228.  
  229.     def receivedMOTD(self, motd):
  230.         for element in motd:
  231.             stuff = "||" + element
  232.             print stuff
  233.  
  234.     def created(self, when):
  235.         print "||" + when
  236.  
  237.     def yourHost(self, info):
  238.         print "||" + info
  239.    
  240.     # def myInfo(self, servername, version, umodes, cmodes):
  241.         # print servername
  242.         # print version
  243.         # print umodes
  244.         # print cmodes
  245.    
  246.     def luserClient(self, info):
  247.         print "||" + info
  248.  
  249.     def bounce(self, info):
  250.         print "||" + info
  251.                        
  252.     def isupport(self, options):
  253.         pass
  254.    
  255.     def luserChannels(self, channels):
  256.         print "||Channels: %s" % channels
  257.    
  258.     def luserOp(self, ops):
  259.         print "||Ops: %s" % ops
  260.    
  261.     def luserMe(self, info):
  262.         print "||" + info
  263.    
  264.     def left(self, channel):
  265.         print "Left %s." % channel
  266.         self.channels.remove(channel)
  267.    
  268.     def modeChanged(self, user, channel, set, modes, args):
  269.         userhost = user
  270.         user = user.split("!", 1)[0]
  271.         if channel in self.watchlist:
  272.             for name in self.watchers:
  273.                 if set:
  274.                     self.msg(name, "%s sets mode %s +%s %s" % (user, channel, modes, " ".join(args)))
  275.                 else:
  276.                     self.msg(name, "%s sets mode %s -%s %s" % (user, channel, modes, " ".join(args)))
  277.             for name in self.wchans:
  278.                 if set:
  279.                     self.msg(name, "%s sets mode %s +%s %s" % (user, channel, modes, " ".join(args)))
  280.                 else:
  281.                     self.msg(name, "%s sets mode %s -%s %s" % (user, channel, modes, " ".join(args)))
  282.         if set:
  283.             print "%s sets mode %s +%s %s" % (user, channel, modes, " ".join(args))
  284.         else:
  285.             print "%s sets mode %s -%s %s" % (user, channel, modes, " ".join(args))
  286.    
  287.     def kickedFrom(self, channel, kicker, message):
  288.         if channel in self.watchlist:
  289.             for name in self.watchers:
  290.                 self.msg(name, "%s Kicked me from %s :%s" % (kicker, channel, message))
  291.             for name in self.wchans:
  292.                 self.msg(name, "%s Kicked me from %s :%s" % (kicker, channel, message))
  293.         print "Kicked from %s by %s: %s" % (channel, kicker, message)
  294.    
  295.     def nickChanged(self, nick):
  296.         print "Nick changed to %s." % nick
  297.    
  298.     def userJoined(self, user, channel):
  299.         if channel in self.watchlist:
  300.             for name in self.watchers:
  301.                 self.msg(name, "%s joined %s." % (user, channel))
  302.             for name in self.wchans:
  303.                 self.msg(name, "%s joined %s." % (user, channel))
  304.         print "%s joined %s." % (user, channel)
  305.    
  306.     def userLeft(self, user, channel):
  307.         if channel in self.watchlist:
  308.             for name in self.watchers:
  309.                 self.msg(name, "%s left %s." % (user, channel))
  310.             for name in self.wchans:
  311.                 self.msg(name, "%s left %s." % (user, channel))
  312.         print "%s left %s." % (user.split("!")[0])
  313.    
  314.     def userKicked(self, kickee, channel, kicker, message):
  315.         kickee = kickee.split("!", 1)[0]
  316.         kicker = kicker.split("!", 1)[0]
  317.         if channel in self.watchlist:
  318.             for name in self.watchers:
  319.                 self.msg(name, "%s was kicked from %s by %s [%s]" % (kickee, channel, kicker, message))
  320.             for name in self.wchans:
  321.                 self.msg(name, "%s was kicked from %s by %s [%s]" % (kickee, channel, kicker, message))
  322.         print "%s was kicked from %s by %s [%s]" % (kickee, channel, kicker, message)
  323.  
  324.     def action(self, user, channel, data):
  325.         userhost = user
  326.         user = user.split("!", 1)[0]
  327.         if channel in self.watchlist:
  328.             for name in self.watchers:
  329.                 self.describe(name, "* %s:%s %s" % (user, channel, data))
  330.             for name in self.wchans:
  331.                 self.describe(name, "* %s:%s %s" % (user, channel, data))
  332.  
  333.     def irc_QUIT(self, user, params):
  334.         userhost = user
  335.         user = user.split('!')[0]
  336.         quitMessage = params[0]
  337.         i = 0
  338.         for channel in self.channels:
  339.             users = self.Names[channel]
  340.             if user in channel:
  341.                 if i == 0:
  342.                     i = 1
  343.                     chan = channel
  344.                     for name in self.watchers:
  345.                         self.me(name, "(%s) %s left IRC [%s]" % (chan, user, quitMessage))
  346.                     for name in self.wchans:
  347.                         self.me(name, "(%s) %s left IRC [%s]" % (chan, user, quitMessage))
  348.                 self.Names[chan].remove(user)
  349.         print
  350.    
  351.     def topicUpdated(self, user, channel, newTopic):
  352.         userhost = user
  353.         user = user.split("!")[0]
  354.         if channel in self.watchlist:
  355.             for name in self.watchers:
  356.                 self.msg(name, "%s set topic %s to \"%s\"" % (user, channel, newTopic))
  357.             for name in self.wchans:
  358.                 self.msg(name, "%s set topic %s to \"%s\"" % (user, channel, newTopic))
  359.  
  360.    
  361.     def irc_RPL_NAMREPLY(self, prefix, params):
  362.         channel = params[2]
  363.         names = params[3].split()
  364.         self.tempchannel = channel
  365.         self.tempnames = self.tempnames + names
  366.         self.Names[channel] = self.tempnames
  367.         self.tempnames, self.tempchannel = [], []
  368.         names = ", ".join(self.Names[channel])
  369.         print "Names for %s: %s" % (channel, names)
  370.         for name in self.watchers:
  371.             self.msg(name, "Names for %s: %s" % (channel, names))
  372.         for name in self.wchans:
  373.             self.msg(name, "Names for %s: %s" % (channel, names))
  374.  
  375. class RelayBotFactory(protocol.ClientFactory):
  376.     protocol = RelayBot
  377.  
  378.     def __init__(self, channel, nickname='interviewBot'):
  379.         self.channel = channel
  380.         self.nickname = nickname
  381.  
  382.     def clientConnectionLost(self, connector, reason):
  383.         print "Lost connection (%s), reconnecting." % (reason,)
  384.         connector.connect()
  385.  
  386.     def clientConnectionFailed(self, connector, reason):
  387.         print "Could not connect: %s" % (reason,)
  388.  
  389. if __name__ == "__main__":
  390.     reactor.connectTCP('optical.esper.net', 6667, RelayBotFactory("#artest"))
  391.     reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement