Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import re
- import os
- import random
- from twisted.internet import reactor
- from twisted.words.protocols import irc
- from twisted.internet import protocol
- from collections import defaultdict
- class RelayBot(irc.IRCClient):
- watchers = ['']
- wchans = ['#minezinemirror']
- watchlist = ['#minezinec']
- authd = []
- password = "Notchium"
- tempnames = []
- tempchannel = []
- Names = {}
- channels = []
- shutup = False
- def _get_nickname(self):
- return self.factory.nickname
- nickname = property(_get_nickname)
- def signedOn(self):
- self.msg("nickserv", "identify Char7zard")
- self.join('#minezinec')
- self.join('#minezinemirror')
- print "Signed on as %s." % self.nickname
- def joined(self, channel):
- print "Joined %s." % channel
- # self.sendLine("NAMES %s" % channel)
- self.channels.append(channel)
- def privmsg(self, user, channel, msg):
- userhost = user
- user = user.split("!")[0]
- print ("<%s:%s> %s" % (user, channel, msg))
- if channel in self.watchlist:
- for name in self.watchers:
- self.msg(name, "<4%s> 3%s" % (user, msg))
- if not self.shutup:
- for name in self.wchans:
- self.msg(name, "<4%s> 3%s" % (user, msg))
- elif msg.startswith("!"):
- command = msg.split(" ")[0].lstrip("!")
- args = msg.split(" ")[1:]
- if command == "help":
- try:
- comm = args[0]
- except IndexError:
- self.msg(channel, "%s: Commands: help wadd, wdel, login, logout, mewatch, meunwatch, radd, rdel, stfu, unstfu." % user)
- self.msg(channel, "Try !help command for more information.")
- else:
- comm = args[0]
- if comm == "help":
- self.msg(channel, "%s: You're reading it, stupid." % user)
- elif comm == "wadd":
- self.msg(channel, "%s: Adds and joins a channel to be watched and relayed from." % user)
- elif comm == "wdel":
- self.msg(channel, "%s: Parts from any channel currently being watched." % user)
- elif comm == "login":
- self.msg(channel, "%s: Logs you in as an administrator." % user)
- elif comm == "logout":
- self.msg(channel, "%s: Logs you out if you're logged in." % user)
- elif comm == "mewatch":
- self.msg(channel, "%s: Makes me query you when I relay." % user)
- elif comm == "meunwatch":
- self.msg(channel, "%s: Stops me from querying you when I relay." % user)
- elif comm == "radd":
- self.msg(channel, "%s: Adds and joins a channel to relay to." % user)
- elif comm == "rdel":
- self.msg(channel, "%s: Parts a channel I'm relaying to." % user)
- elif comm == "topic":
- self.msg(channel, "%s: Requests the topic for a channel." % user)
- elif comm == "names":
- self.msg(channel, "%s: Returns the userlist for a channel." % user)
- else:
- self.msg(channel, "%s: No help topic for %s." % (user, comm))
- elif command == "stfu":
- if userhost in self.authd:
- self.shutup = True
- self.msg(channel, "%s: Paused relaying to channels." % user)
- else:
- self.msg(channel, "%s: You are not authorized to do that." % user)
- elif command == "unstfu":
- if userhost in self.authd:
- self.shutup = False
- self.msg(channel, "%s: Continuing to relay to channels." % user)
- else:
- self.msg(channel, "%s: You are not authorized to do that." % user)
- elif command == "wadd":
- if userhost in self.authd:
- if len(args) > 0:
- chan = args[0]
- if chan.startswith("#"):
- if not chan in self.wchans:
- self.msg(channel, "%s: Joining and watching %s." % (user, chan))
- self.watchlist.append(chan)
- self.join(chan)
- else:
- self.msg(channel, "%s: I'm relaying to that channel!" % name)
- else:
- self.msg(channel, "%s: %s is not a valid channel name." % (user, chan))
- else:
- self.msg(channel, "%s: Syntax: !wadd #channel" % user)
- else:
- self.msg(channel, "%s: You are not authorized to do that." % user)
- elif command == "wdel":
- if userhost in self.authd:
- if len(args) > 0:
- chan = args[0]
- if chan in self.watchlist:
- self.msg(channel, "%s: Leaving %s." % (user, chan))
- self.watchlist.remove(chan)
- self.part(chan)
- else:
- self.msg(channel, "%s: Not watching %s." % (user, chan))
- else:
- self.msg(channel, "%s: Syntax: !wdel #channel" % user)
- else:
- self.msg(channel, "%s: You are not authorized to do that." % user)
- elif command == "login":
- pw = " ".join(args)
- if pw == self.password:
- self.msg(channel, "%s: You are now logged in." % user)
- self.authd.append(userhost)
- self.watchers.append(user)
- elif command == "logout":
- if userhost in self.authd:
- self.msg(channel, "%s: You are now logged out." % user)
- self.authd.remove(userhost)
- self.watchers.remove(user)
- else:
- self.msg(channel, "%s: You were never logged in." % user)
- elif command == "mewatch":
- if userhost in self.authd:
- if user in self.watchers:
- self.msg(channel, "%s: You are already a watcher." % user)
- else:
- self.msg(channel, "%s: You are now a watcher." % user)
- self.watchers.append(user)
- else:
- self.msg(channel, "%s: You are not authorized to do that." % user)
- elif command == "meunwatch":
- if userhost in self.authd:
- if user in self.watchers:
- self.msg(channel, "%s: You are no longer a watcher." % user)
- self.watchers.remove(user)
- else:
- self.msg(channel, "%s: You were never a watcher." % user)
- else:
- self.msg(channel, "%s: You are not authorized to do that." % user)
- elif command == "radd":
- if userhost in self.authd:
- if len(args) > 0:
- chan = args[0]
- if chan.startswith("#"):
- if not chan in self.watchlist:
- self.msg(channel, "%s: Joining and %s." % (user, chan))
- self.wchans.append(chan)
- self.join(chan)
- else:
- self.msg(channel, "%s: I'm monitoring that channel!" % user)
- else:
- self.msg(channel, "%s: %s is not a valid channel name." % (user, chan))
- else:
- self.msg(channel, "%s: Syntax: !radd #channel" % user)
- else:
- self.msg(channel, "%s: You are not authorized to do that." % user)
- elif command == "rdel":
- if userhost in self.authd:
- if len(args) > 0:
- chan = args[0]
- if chan.startswith("#"):
- self.msg(channel, "%s: Leaving %s." % (user, chan))
- self.wchans.remove(chan)
- self.part(chan)
- else:
- self.msg(channel, "%s: %s is not a valid channel name." % (user, chan))
- else:
- self.msg(channel, "%s: Syntax: !rdel #channel" % user)
- else:
- self.msg(channel, "%s: You are not authorized to do that." % user)
- elif command == "names":
- if userhost in self.authd:
- if len(args) > 0:
- chan = args[0]
- if chan.startswith("#"):
- if chan in self.Names.keys():
- self.msg(channel, "Names for %s: %s." % (chan, " ".join(self.Names[chan])))
- else:
- self.msg(channel, "%s: I'm not in that channel or there is no userlist for it!" % user)
- else:
- self.msg(channel, "%s: %s is not a valid channel name." % (user, chan))
- else:
- self.msg(channel, "%s: Syntax: !names #channel" % user)
- else:
- self.msg(channel, "%s: You are not authorized to do that." % user)
- elif command == "topic":
- if userhost in self.authd:
- if len(args) > 0:
- chan = args[0]
- if chan.startswith("#"):
- if chan in self.watchlist:
- self.topic(chan)
- else:
- self.msg(channel, "%s: I'm not watching that channel!" % user)
- else:
- self.msg(channel, "%s: %s is not a valid channel name." % (user, chan))
- else:
- self.msg(channel, "%s: Syntax: !names #channel" % user)
- else:
- self.msg(channel, "%s: You are not authorized to do that." % user)
- #TODO: !raw, !modes, !umodes
- def privnotice(self, user, channel, msg):
- user = user.split("!")[0]
- print ("-%s:%s- %s" % (user, channel, msg))
- if channel in self.watchlist:
- for name in self.watchers:
- self.msg(name, "-%s:%s- %s" % (user, channel, msg))
- for name in self.wchans:
- self.msg(name, "-%s:%s- %s" % (user, channel, msg))
- def receivedMOTD(self, motd):
- for element in motd:
- stuff = "||" + element
- print stuff
- def created(self, when):
- print "||" + when
- def yourHost(self, info):
- print "||" + info
- # def myInfo(self, servername, version, umodes, cmodes):
- # print servername
- # print version
- # print umodes
- # print cmodes
- def luserClient(self, info):
- print "||" + info
- def bounce(self, info):
- print "||" + info
- def isupport(self, options):
- pass
- def luserChannels(self, channels):
- print "||Channels: %s" % channels
- def luserOp(self, ops):
- print "||Ops: %s" % ops
- def luserMe(self, info):
- print "||" + info
- def left(self, channel):
- print "Left %s." % channel
- self.channels.remove(channel)
- def modeChanged(self, user, channel, set, modes, args):
- userhost = user
- user = user.split("!", 1)[0]
- if channel in self.watchlist:
- for name in self.watchers:
- if set:
- self.msg(name, "%s sets mode %s +%s %s" % (user, channel, modes, " ".join(args)))
- else:
- self.msg(name, "%s sets mode %s -%s %s" % (user, channel, modes, " ".join(args)))
- for name in self.wchans:
- if set:
- self.msg(name, "%s sets mode %s +%s %s" % (user, channel, modes, " ".join(args)))
- else:
- self.msg(name, "%s sets mode %s -%s %s" % (user, channel, modes, " ".join(args)))
- if set:
- print "%s sets mode %s +%s %s" % (user, channel, modes, " ".join(args))
- else:
- print "%s sets mode %s -%s %s" % (user, channel, modes, " ".join(args))
- def kickedFrom(self, channel, kicker, message):
- if channel in self.watchlist:
- for name in self.watchers:
- self.msg(name, "%s Kicked me from %s :%s" % (kicker, channel, message))
- for name in self.wchans:
- self.msg(name, "%s Kicked me from %s :%s" % (kicker, channel, message))
- print "Kicked from %s by %s: %s" % (channel, kicker, message)
- def nickChanged(self, nick):
- print "Nick changed to %s." % nick
- def userJoined(self, user, channel):
- if channel in self.watchlist:
- for name in self.watchers:
- self.msg(name, "%s joined %s." % (user, channel))
- for name in self.wchans:
- self.msg(name, "%s joined %s." % (user, channel))
- print "%s joined %s." % (user, channel)
- def userLeft(self, user, channel):
- if channel in self.watchlist:
- for name in self.watchers:
- self.msg(name, "%s left %s." % (user, channel))
- for name in self.wchans:
- self.msg(name, "%s left %s." % (user, channel))
- print "%s left %s." % (user.split("!")[0])
- def userKicked(self, kickee, channel, kicker, message):
- kickee = kickee.split("!", 1)[0]
- kicker = kicker.split("!", 1)[0]
- if channel in self.watchlist:
- for name in self.watchers:
- self.msg(name, "%s was kicked from %s by %s [%s]" % (kickee, channel, kicker, message))
- for name in self.wchans:
- self.msg(name, "%s was kicked from %s by %s [%s]" % (kickee, channel, kicker, message))
- print "%s was kicked from %s by %s [%s]" % (kickee, channel, kicker, message)
- def action(self, user, channel, data):
- userhost = user
- user = user.split("!", 1)[0]
- if channel in self.watchlist:
- for name in self.watchers:
- self.describe(name, "* %s:%s %s" % (user, channel, data))
- for name in self.wchans:
- self.describe(name, "* %s:%s %s" % (user, channel, data))
- def irc_QUIT(self, user, params):
- userhost = user
- user = user.split('!')[0]
- quitMessage = params[0]
- i = 0
- for channel in self.channels:
- users = self.Names[channel]
- if user in channel:
- if i == 0:
- i = 1
- chan = channel
- for name in self.watchers:
- self.me(name, "(%s) %s left IRC [%s]" % (chan, user, quitMessage))
- for name in self.wchans:
- self.me(name, "(%s) %s left IRC [%s]" % (chan, user, quitMessage))
- self.Names[chan].remove(user)
- print
- def topicUpdated(self, user, channel, newTopic):
- userhost = user
- user = user.split("!")[0]
- if channel in self.watchlist:
- for name in self.watchers:
- self.msg(name, "%s set topic %s to \"%s\"" % (user, channel, newTopic))
- for name in self.wchans:
- self.msg(name, "%s set topic %s to \"%s\"" % (user, channel, newTopic))
- def irc_RPL_NAMREPLY(self, prefix, params):
- channel = params[2]
- names = params[3].split()
- self.tempchannel = channel
- self.tempnames = self.tempnames + names
- self.Names[channel] = self.tempnames
- self.tempnames, self.tempchannel = [], []
- names = ", ".join(self.Names[channel])
- print "Names for %s: %s" % (channel, names)
- for name in self.watchers:
- self.msg(name, "Names for %s: %s" % (channel, names))
- for name in self.wchans:
- self.msg(name, "Names for %s: %s" % (channel, names))
- class RelayBotFactory(protocol.ClientFactory):
- protocol = RelayBot
- def __init__(self, channel, nickname='interviewBot'):
- self.channel = channel
- self.nickname = nickname
- def clientConnectionLost(self, connector, reason):
- print "Lost connection (%s), reconnecting." % (reason,)
- connector.connect()
- def clientConnectionFailed(self, connector, reason):
- print "Could not connect: %s" % (reason,)
- if __name__ == "__main__":
- reactor.connectTCP('optical.esper.net', 6667, RelayBotFactory("#artest"))
- reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement