Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # BridgeBot version 0.1
- # Attemps to create a seemless interface between multiple chat protocols
- # Written and designed by MegaLoler (megaloler9000@gmail.com)
- # Written for Python version 2.7
- # Todo list:
- # Make bridged rooms list dynamic
- # Deal with already existing nicks
- # Prevent connecting to the same place multiple times
- # Add colors
- # Add quit reasons to IRC
- # Find goodies to add yay
- # Fix bugs
- # Due to a bug, I accidentally spammed Tinychat and got banned. So I was unable to finish tinychat's functionality. It was working rather well, but I am sure there is still an unacceptable lack of functionality.
- # I'm stuck on chatango because I can't figure out how to make the library connect as temporary users.
- # Stopped working on IRC because I got tired
- # So basically nothing works except a little bit of Tinychat, which I can't even access :P
- # Import connection libraries and declare some constants.
- # MegaLoler's IRC library (http://pastebin.com/d33RgTWY)
- import irc
- # Lumirayz's Chatango library (https://github.com/theholder/ch.py)
- import chatango
- # MegaLoler's Tinychat library (http://pastebin.com/aVJuiLK4)
- import tinychat
- import thread
- IRC = 0
- CHATANGO = 1
- TINYCHAT = 2
- # Configuration
- botName_IRC = "BridgeBotObserver"
- botPass_IRC = None
- botName_Chatango = "BridgeBotObserver"
- botPass_Chatango = None
- botName_Tinychat = "BridgeBotObserver"
- # A list of the rooms to bridge. The first entry is the service. The second entry is the room name. The third entry is only for IRC connections and is the server name.
- bridgedRooms = ()
- # A User's job is to mock the real user that it represents by relaying their actions to all other connections.
- class User():
- def __init__(self, nativeConnection, nick, password=None):
- self.nativeConnection = nativeConnection
- self.nick = nick
- self.password = password
- self.connections = []
- self.connect()
- def _connect(self, room):
- if room.service == IRC:
- self.connections.append(IRCWrapper(self.nick, self.password, room.room, room.server))
- elif room.service == CHATANGO:
- self.connections.append(ChatangoWrapper(self.nick, self.password, room.room))
- elif room.service == TINYCHAT:
- self.connections.append(TinychatWrapper(self.nick, room.room))
- # Actions
- def connect(self):
- users.append(self)
- for room in rooms:
- if room != self.nativeConnection: self._connect(room)
- def quit(self):
- users.remove(self)
- for connection in self.connections: connection.quit()
- def say(self, message, color):
- for connection in self.connections: connection.say(message, color)
- def setNick(self, nick):
- self.nick = nick
- for connection in self.connections: connection.setNick(nick)
- # These wrappers are to provide an identical interface for any type of connection.
- class IRCWrapper():
- def __init__(self, nick, password, room, server, responder=None):
- self.nick = nick
- self.password = password
- self.room = room
- self.server = server
- self.responder = responder
- self.connect()
- def _listen(self):
- while self.connection.connected:
- self.connection._buffer += self.connection.sock.recv(1024).decode()
- if(self.connection._buffer.find("\r\n")):
- for msg in self.connection._buffer.split("\r\n")[0:-1]:
- self.connection.verboseOut("SERVER: " + msg)
- cmd = self.connection._parse(msg)
- self.connection.logPretty(cmd)
- self.connection._handle(cmd)
- self.handle(cmd)
- self.connection._buffer = self.connection._buffer.split("\r\n")[-1]
- def handle(self, msg):
- if cmd.command == "PRIVMSG":
- if msg.nick.lower() != self.nick.lower() and self.responder == self.responder.getUserByNick(msg.nick).nativeConnection: self.responder.onSay(msg.nick, msg.trailing, None)
- # Actions
- def connect(self):
- self.connection = irc.IRCServer(self.server)
- if not self.password: self.password = ""
- self.connection.connect(self.nick, self.password, listen=False)
- thread.start_new_thread(self._listen, ())
- def quit(self):
- self.connection.disconnect()
- def say(self, message, color):
- self.connection.say(message, self.room)
- def setNick(self, nick):
- self.nick = nick
- self.connection._sendCommand("nick", [nick])
- self.connection.nick = nick
- class ChatangoWrapper():
- def __init__(self, nick, password, room, responder=None):
- self.nick = nick
- self.password = password
- self.room = room
- self.responder = responder
- self.connect()
- class RoomManager(chatango.RoomManager):
- def onConnect(self, room):
- self.theRoom = room
- def onJoin(self, room, user):
- self.responder.onConnect(user.name)
- def onLeave(self, room, user):
- self.responder.onQuit(user.name)
- def onMessage(self, room, user, message):
- if user.name.lower() != self.nick.lower() and self.responder == self.responder.getUserByNick(user.name).nativeConnection: self.responder.onSay(user.name, message.body, message.fontColor)
- # Actions
- def connect(self):
- self.connection = self.RoomManager(self.nick, self.password, False)
- self.connection.joinRoom(self.room)
- self.connection.nick = self.nick
- self.connection.theRoom = None
- self.connection.responder = self.responder
- thread.start_new_thread(self.connection.main, ())
- def quit(self):
- self.connection.stop()
- self.connection.theRoom.disconnect()
- def say(self, message, color):
- self.connection.message(message)
- def setNick(self, nick):
- self.nick = nick
- self.quit()
- self.connect()
- class TinychatWrapper():
- def __init__(self, nick, room, responder=None):
- self.nick = nick
- self.room = room
- self.responder = responder
- self.connect()
- class TinychatRoom(tinychat.TinychatRoom):
- def onJoin(self, user):
- if user.nick.find("-") == -1: self.responder.onConnect(user.nick)
- def onQuit(self, user):
- self.responder.onQuit(user.nick)
- def onMessage(self, user, message):
- if self.responder == self.responder.getUserByNick(user.nick).nativeConnection: self.responder.onSay(user.nick, message.msg, message.color)
- def onNickChange(self, new, old, user):
- self.responder.onSetNick(old, new)
- # Actions
- def connect(self):
- self.connection = self.TinychatRoom(self.room, self.nick)
- self.connection.responder = self.responder
- thread.start_new_thread(self.connection.connect, ())
- def quit(self):
- self.connection.disconnect()
- def say(self, message, color):
- self.connection.say(message)
- def setNick(self, nick):
- self.nick = nick
- self.connection.setNick(nick)
- # This represents a bridged room. Specifically, this is the observing connection. It's job is to create users representing connections and control them based on observations.
- class Room():
- def __init__(self, service, room, server=None):
- self.service = service
- self.room = room
- self.server = server
- rooms.append(self)
- if service == IRC:
- self.connection = IRCWrapper(botName_IRC, botPass_IRC, room, server, self)
- elif service == CHATANGO:
- self.connection = ChatangoWrapper(botName_Chatango, botPass_Chatango, room, self)
- elif service == TINYCHAT:
- self.connection = TinychatWrapper(botName_Tinychat, room, self)
- def getUserByNick(self, nick, create=True):
- for user in users:
- if user.nick.lower() == nick.lower(): return user
- if create: return User(self, nick)
- # Events
- def onConnect(self, nick):
- self.getUserByNick(nick)
- print(nick + " has joined.")
- def onQuit(self, nick):
- user = self.getUserByNick(nick, False)
- if user:
- user.quit()
- print(nick + " has quit.")
- def onSay(self, nick, message, color):
- self.getUserByNick(nick).say(message, color)
- print(nick + ": " + message)
- def onSetNick(self, oldNick, newNick):
- user = self.getUserByNick(oldNick, False)
- if user:
- user.setNick(newNick)
- print(oldNick + " is now known as " + newNick + ".")
- # Initialization
- rooms = []
- users = []
- # Connect to the listed rooms to bridge.
- for bridgedRoom in bridgedRooms:
- service = bridgedRoom[0]
- room = bridgedRoom[1]
- if service == IRC:
- server = bridgedRoom[2]
- else:
- server = None
- Room(service, room, server)
- # Welcome the console user and wait.
- print("")
- print("Welcome to BridgeBot!")
- print("Attemps to create a seemless interface between multiple chat protocols")
- print("Written and designed by MegaLoler (megaloler9000@gmail.com)")
- print("Written for Python version 2.7")
- print("")
- print("IRC library by MegaLoler (http://pastebin.com/d33RgTWY)")
- print("Chatango library by Lumirayz (https://github.com/theholder/ch.py)")
- print("Tinychat library by MegaLoler (http://pastebin.com/aVJuiLK4)")
- print("")
- print("Press enter at anytime to quit.")
- print("")
- raw_input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement