Cooperation/CooperationClient.py

changeset 165
3302a726fd1e
parent 164
b395b006d2a8
child 168
3383117f894b
equal deleted inserted replaced
164:b395b006d2a8 165:3302a726fd1e
12 from PyQt4.QtCore import QObject, pyqtSignal, QProcess, QRegExp 12 from PyQt4.QtCore import QObject, pyqtSignal, QProcess, QRegExp
13 from PyQt4.QtNetwork import QHostInfo, QHostAddress, QAbstractSocket 13 from PyQt4.QtNetwork import QHostInfo, QHostAddress, QAbstractSocket
14 14
15 from .CooperationServer import CooperationServer 15 from .CooperationServer import CooperationServer
16 from .Connection import Connection 16 from .Connection import Connection
17
18 import Preferences
17 19
18 class CooperationClient(QObject): 20 class CooperationClient(QObject):
19 """ 21 """
20 Class implementing the client of the cooperation package. 22 Class implementing the client of the cooperation package.
21 23
132 if connectionList: 134 if connectionList:
133 return True 135 return True
134 136
135 return False 137 return False
136 138
137 def __removeConnection(self, connection): 139 def removeConnection(self, connection):
138 """ 140 """
139 Private method to remove a connection. 141 Public method to remove a connection.
140 142
141 @param connection reference to the connection to be removed (Connection) 143 @param connection reference to the connection to be removed (Connection)
142 """ 144 """
143 if connection.peerAddress() in self.__peers and \ 145 if connection.peerAddress() in self.__peers and \
144 connection in self.__peers[connection.peerAddress()]: 146 connection in self.__peers[connection.peerAddress()]:
145 self.__peers[connection.peerAddress()].remove(connection) 147 self.__peers[connection.peerAddress()].remove(connection)
146 nick = connection.name() 148 nick = connection.name()
147 if nick != "": 149 if nick != "":
148 self.participantLeft.emit(nick) 150 self.participantLeft.emit(nick)
149 151
152 if connection.isValid():
153 connection.abort()
154
150 connection.deleteLater() 155 connection.deleteLater()
151 156
152 def disconnectConnections(self): 157 def disconnectConnections(self):
153 """ 158 """
154 Public slot to disconnect from the chat network. 159 Public slot to disconnect from the chat network.
155 """ 160 """
156 for connectionList in self.__peers.values(): 161 for connectionList in self.__peers.values():
157 while connectionList: 162 while connectionList:
158 self.__removeConnection(connectionList[0]) 163 self.removeConnection(connectionList[0])
159 164
160 def __newConnection(self, connection): 165 def __newConnection(self, connection):
161 """ 166 """
162 Private slot to handle a new connection. 167 Private slot to handle a new connection.
163 168
197 else: 202 else:
198 msg = "* {0}\n".format(connection.errorString()) 203 msg = "* {0}\n".format(connection.errorString())
199 self.connectionError.emit(msg) 204 self.connectionError.emit(msg)
200 if connection == self.__initialConnection: 205 if connection == self.__initialConnection:
201 self.cannotConnect.emit() 206 self.cannotConnect.emit()
202 self.__removeConnection(connection) 207 self.removeConnection(connection)
203 208
204 def __disconnected(self): 209 def __disconnected(self):
205 """ 210 """
206 Private slot to handle the disconnection of a chat client. 211 Private slot to handle the disconnection of a chat client.
207 """ 212 """
208 connection = self.sender() 213 connection = self.sender()
209 self.__removeConnection(connection) 214 self.removeConnection(connection)
210 215
211 def __readyForUse(self): 216 def __readyForUse(self):
212 """ 217 """
213 Private slot to handle a connection getting ready for use. 218 Private slot to handle a connection getting ready for use.
214 """ 219 """
283 @param message editor command to be sent (string) 288 @param message editor command to be sent (string)
284 """ 289 """
285 for connectionList in self.__peers.values(): 290 for connectionList in self.__peers.values():
286 for connection in connectionList: 291 for connection in connectionList:
287 connection.sendEditorCommand(projectHash, filename, message) 292 connection.sendEditorCommand(projectHash, filename, message)
293
294 def __findConnections(self, nick):
295 """
296 Public method to get a list of connection given a nick name.
297
298 @param nick nick name in the format of self.nickName() (string)
299 @return list of references to the connection objects (list of Connection)
300 """
301 if "@" not in nick or ":" not in nick:
302 # nick given in wrong format
303 return []
304
305 user, host = nick.split(":")[0].split("@")
306 senderIp = QHostAddress(host)
307
308 if senderIp not in self.__peers:
309 return []
310
311 return self.__peers[senderIp][:]
312
313 def kickUser(self, nick):
314 """
315 Public method to kick a user by its nick name.
316
317 @param nick nick name in the format of self.nickName() (string)
318 """
319 for connection in self.__findConnections(nick):
320 connection.abort()
321
322 def banUser(self, nick):
323 """
324 Public method to ban a user by its nick name.
325
326 @param nick nick name in the format of self.nickName() (string)
327 """
328 Preferences.syncPreferences()
329 user = nick.split(":")[0]
330 bannedUsers = Preferences.getCooperation("BannedUsers")[:]
331 if user not in bannedUsers:
332 bannedUsers.append(user)
333 Preferences.setCooperation("BannedUsers", bannedUsers)
334
335 def banKickUser(self, nick):
336 """
337 Public method to ban and kick a user by its nick name.
338
339 @param nick nick name in the format of self.nickName() (string)
340 """
341 self.banUser(nick)
342 self.kickUser(nick)

eric ide

mercurial