Exemplo n.º 1
0
 def __init__(self, parent = None):
     QTcpSocket.__init__(self, parent)
     
     # The 'readyRead' signal is from QTcpSocket, call 'readyReadId' 
     # whenever is emitted.
     self.readyRead.connect(self.onReadyRead)
     self.disconnected.connect(self.onDisconnected)
Exemplo n.º 2
0
    def __init__(self, parent=None):
        QTcpSocket.__init__(self, parent)

        # The 'readyRead' signal is from QTcpSocket, call 'readyReadId'
        # whenever is emitted.
        self.readyRead.connect(self.onReadyRead)
        self.disconnected.connect(self.onDisconnected)
Exemplo n.º 3
0
	def __init__(self, host, port=6667, ssl=False):
		super(IRCServer, self).__init__()
		if ssl:
			self.socket = QSslSocket()
		else:
			self.socket = QTcpSocket()
		
		self.__host = host
		self.__port = port
		self.__linebuf = ""
		self.__channels = {}
		self.socket.readyRead.connect(self.__handleRead)
		self.receivedPing.connect(self.pong)
Exemplo n.º 4
0
class clientTCP(clientAbstract.clientAbstract):
    """ OSC-client running on GOD.
    sends OSC-messages to SMi.
    receives OSC-messages from SMi (and emits signals with the data)
    """

    def __init__(self, host, port, oscprefix='', verbose=False):
        super(clientTCP, self).__init__(oscprefix=oscprefix, verbose=verbose)
        self.remote = (host, port) ## FIXXME: 'host' is not canonicalized
        self.keepListening=True

        self.slip = SLIP()
        
        self.socket = QTcpSocket()
        self.socket.readyRead.connect(self._callback)
        self.socket.connectToHost(host, port);
        self.sender=(host, port)

    def __del__(self):
        self.shutdown()

    def shutdown(self):
        self.keepListening=False
        if self.socket is not None:
            try:
                self.socket.shutdown()
                self.socket.close()
            except:
                pass
        if self.addressManager is not None:
            del self.addressManager
            self.addressManager = None
        self.remote = None
        self.socket = None

    def _callback(self):
        '''Asynchronous connection listener. Starts a handler for each connection.'''
##        while self.socket.hasPendingDatagrams():
##            datagram, sender, senderPort = self.socket.readDatagram(self.socket.pendingDatagramSize())
##            self.addressManager.handle(datagram.data(), (sender.toString(), senderPort))
        data=str(self.socket.readAll())
        self.slip.append(data)
        for d in self.slip.get():
            self.addressManager.handle(d, self.sender)
        
    def _send(self, data):
        if self.socket is not None and self.remote is not None:
            logging.log(1, "sending '%s' to %s" % (str(data), str(self.remote)))
            slip = SLIP();
            slip+=data;
            sdata=str(slip.getData())
            self.socket.writeData(sdata, len(sdata))
Exemplo n.º 5
0
 def _createSocket(self):
     from PySide.QtNetwork import QTcpSocket
     q = self.q
     ret = QTcpSocket(q)
     socketio.initsocket(ret)
     ret.error.connect(q.socketError)
     ret.connected.connect(q.connected)
     ret.disconnected.connect(q.disconnected)
     ret.readyRead.connect(self.readSocket)
     return ret
Exemplo n.º 6
0
    def __init__(self, host, port, oscprefix='', verbose=False):
        super(clientTCP, self).__init__(oscprefix=oscprefix, verbose=verbose)
        self.remote = (host, port) ## FIXXME: 'host' is not canonicalized
        self.keepListening=True

        self.slip = SLIP()
        
        self.socket = QTcpSocket()
        self.socket.readyRead.connect(self._callback)
        self.socket.connectToHost(host, port);
        self.sender=(host, port)
Exemplo n.º 7
0
 def _connect(self):
     # connect to a server
     if not self.chat_widget.connected:
         self.chat_widget.connection = QTcpSocket()
         # upon connection, disable the connect action, and enable the
         # quit server action
         self.chat_widget.connection.connected.connect(
             partial(self.quit_server_action.setEnabled, True))
         self.chat_widget.connection.connected.connect(
             partial(self.connect_action.setDisabled, True))
         # to the reverse thing upon disconnection
         self.chat_widget.connection.disconnected.connect(
             partial(self.connect_action.setEnabled, True))
         self.chat_widget.connection.disconnected.connect(
             partial(self.quit_server_action.setDisabled, True))
         # connect to the chat server
         self.chat_widget.connection.connectToHost(HOST_ADDRESS, PORT)
Exemplo n.º 8
0
class IRCServer(QObject):
	"""
	A socket to an IRC server.
	Initialize it with the host and port (default 6667)
	Connect with server.connectAs(nick), and connect to the online() signal.
	"""
	
	joinedChannel = Signal(IRCChannel) # fired when the client joins a channel
	kicked = Signal(str, IRCChannel, str) # fired when the client gets kicked from a channel
	online = Signal() # fired when a connection to the IRC server has been successfully established.
	packetRead = Signal(str) # fired when a full packet is read from the server
	packetWritten = Signal(str) # fired when a full packet is written to the server
	receivedPing = Signal(str) # fired when the client receives a Ping packet
	receivedNotice = Signal(str, str) # fired when the client receives a Notice packet
	receivedPrivateMessage = Signal(str, str) # fired when the client receives a personal privmsg packet
	
	def __init__(self, host, port=6667, ssl=False):
		super(IRCServer, self).__init__()
		if ssl:
			self.socket = QSslSocket()
		else:
			self.socket = QTcpSocket()
		
		self.__host = host
		self.__port = port
		self.__linebuf = ""
		self.__channels = {}
		self.socket.readyRead.connect(self.__handleRead)
		self.receivedPing.connect(self.pong)
	
	def __handleRead(self):
		while True:
			line = str(self.socket.readLine())
			if not line:
				break
			
			if self.__linebuf:
				line = self.__linebuf + line
				self.__linebuf = ""
			
			if not line.endswith("\r\n"):
				self.__linebuf = line
				continue
			
			sender, opcode, recipient, msg = self._parse(line.strip())
			
			if opcode == opcodes.RPL_WELCOME:
				self.online.emit()
			
			elif opcode == opcodes.RPL_TOPIC:
				channel, _, topic = msg.partition(" ")
				topic = stripcolon(topic)
				channel = self.channel(channel)
				channel.topicUpdated.emit(None, topic)
				channel.receivedReply.emit(opcode, stripcolon(topic))
			
			elif opcode == opcodes.RPL_CREATIONTIME:
				channel, timestamp = msg.split(" ")
				self.channel(channel).receivedReply.emit(opcode, timestamp)
			
			elif opcode == opcodes.RPL_CHANNELMODEIS:
				channel, mode = msg.partition(" ")
				self.channel(channel).receivedReply.emit(opcode, mode)
			
			elif opcode == "JOIN":
				user = IRCUser(sender, self)
				channel = stripcolon(recipient)
				if user.nick() == self.nick():
					self.__channels[channel] = IRCChannel(channel, self)
					self.joinedChannel.emit(self.__channels[channel])
				else:
					self.channel(channel).userJoined.emit(user)
			
			elif opcode == "KICK":
				channel = self.channel(recipient)
				user, _, reason = msg.partition(" ")
				reason = stripcolon(reason)
				
				if user == self.nick():
					# If we get kicked, emit IRCServer.kicked(sender, channel, reason)
					# TODO also emit IRCChannel.kicked(sender, reason)
					self.kicked.emit(sender, channel, reason)
					del self.__channels[recipient] # remove from channel list
				
				else:
					# Otherwise, emit IRCChannel.userKicked(sender, user, reason)
					self.channel(channel).userKicked.emit(sender, user, reason)
			
			elif opcode == "NOTICE":
				self.receivedNotice.emit(sender, msg)
			
			elif opcode == "PING":
				self.receivedPing.emit(msg)
			
			elif opcode == "PRIVMSG":
				sender = IRCUser(sender, self)
				if recipient == self.nick():
					self.receivedPrivateMessage.emit(sender, msg)
				else:
					self.channel(recipient).receivedMessage.emit(sender, msg)
			
			elif opcode == "TOPIC":
				sender = IRCUser(sender, self)
				IRCChannel(recipient).topicUpdated.emit(sender, msg)
			
			self.packetRead.emit(line)
	
	def _parse(self, line):
		if line.startswith(":"):
			# XXX We need to use partition() and check arg numbers
			line = line[1:] # strip the first colon already
			sender, opcode, recipient = line.split(" ")[:3]
			idx = len(" ".join((sender, opcode, recipient)))
			msg = line[idx:]
			if opcode.isdigit():
				opcode = int(opcode)
			return sender, opcode, recipient, stripcolon(msg.strip())
		
		elif line.startswith("PING"):
			server = ""
			recipient = ""
			opcode, msg = line.split()
			return "", opcode, "", stripcolon(msg)
	
	def channel(self, channel):
		"""
		Returns the channel \a channel.
		"""
		return self.__channels[channel]
	
	def connectAs(self, nick, user="", host="", serverName="", realName=""):
		"""
		Opens a connection to the server and instantly returns.
		If the connection is successful, the user's nickname will automatically be
		set to \a nick. \a user, \a host, \a serverName and \a realName al default
		to the user's nickname.
		"""
		if self.isSsl():
			self.socket.connectToHostEncrypted(self.__host, self.__port)
		else:
			self.socket.connectToHost(self.__host, self.__port)
		self.__nick = nick
		
		def onConnect():
			self.send("NICK %s" % (nick))
			self.send("USER %s %s %s :%s" % (user or nick, host or nick, serverName or nick, realName or nick))
		
		self.socket.connected.connect(onConnect)
	
	def isSsl(self):
		return isinstance(self.socket, QSslSocket)
	
	def join(self, channel):
		"""
		Joins the channel \a channel.
		"""
		self.send("JOIN %s" % (channel))
	
	def nick(self):
		"""
		Returns the user's nickname.
		"""
		return self.__nick
	
	def pong(self, msg):
		"""
		Sends a PONG packet to the server with the message \a msg.
		\sa receivedPing()
		"""
		self.send("PONG :%s" % (msg))
	
	def quit(self, reason=""):
		"""
		Quits the server and closes the TCP socket.
		"""
		if reason:
			self.send("QUIT :%s" % (reason))
		else:
			self.send("QUIT")
		self.close()
	
	def send(self, message):
		"""
		Sends \a message to the server, terminating it with CRLF if necessary.
		\sa write()
		"""
		if not message.endswith("\r\n"):
			message += "\r\n"
		self.write(message)
	
	def setAway(self, away, reason="Away"):
		"""
		Sets the user's away status to \a away, with reason \a reason.
		\a reason is ignored if \a away is False. Default reason is "Away".
		Note that you cannot set yourself away without a reason.
		\sa away()
		"""
		if not away:
			self.send("AWAY")
		else:
			self.send("AWAY :%s" % (reason))
	
	def write(self, data):
		"""
		Writes \a data to the server. The data is not modified, and must be properly
		terminated with CRLF.
		\sa send() packetWritten()
		"""
		data = str(data)
		self.socket.write(data)
		self.socket.waitForBytesWritten()
		self.packetWritten.emit(data)