2015-04-15 17:52:28 +02:00
from view . main import Ui_MainWindow
2015-03-30 14:21:11 +02:00
from PyQt4 import QtGui , QtCore
2015-04-02 16:36:24 +02:00
from PySide . QtCore import *
from PySide . QtGui import *
2015-04-03 20:07:29 +02:00
import time , threading , datetime , time , random , re
2015-03-31 11:52:01 +02:00
from socket import *
2015-04-15 17:52:28 +02:00
from view . pmWindow import Ui_Dialog2
from view . pmFile import Ui_Dialog3
2015-04-08 15:23:48 +02:00
import configparser
2015-04-11 20:23:04 +02:00
from threading import *
2015-04-14 22:36:37 +02:00
#########################################################
##
# this class can receive files sent by p2p
# to run this class, we need to do :
# s = StreamHandler(portFile, fileName)
# s.start()
#
class StreamHandler ( Thread ) :
"""
this class can receive files sent by p2p
to run this class , we need to do :
s = StreamHandler ( portFile , fileName )
s . start ( )
"""
def __init__ ( self , port , filename ) :
"""
: param port : the port of the new connection
: param filename : the name of the file you wish to receive
: return :
"""
Thread . __init__ ( self )
self . port = port
self . filename = filename
def run ( self ) :
"""
Execute the process function
: return :
"""
self . process ( )
def bindmsock ( self ) :
"""
creation of a new socket for the p2p transfert file
: return :
"""
self . msock = socket ( AF_INET , SOCK_STREAM )
self . msock . bind ( ( ' ' , int ( self . port ) ) )
self . msock . listen ( 1 )
print ( ' [Media] Listening on port ' + self . port )
def acceptmsock ( self ) :
"""
Get the address of the connection
: return :
"""
self . mconn , self . maddr = self . msock . accept ( )
print ( ' [Media] Got connection from ' , self . maddr )
def acceptcsock ( self ) :
"""
accept to receive the file
: return :
"""
self . cconn , self . maddr = self . csock . accept ( )
print ( ' [Control] Got connection from ' + self . maddr )
2015-04-11 20:23:04 +02:00
while 1 :
2015-04-14 22:36:37 +02:00
data = self . cconn . recv ( 1024 )
2015-04-11 20:23:04 +02:00
if not data : break
2015-04-14 22:36:37 +02:00
if data [ 0 : 4 ] == " SEND " : self . filename = data [ 5 : ]
print ( ' [Control] Getting ready to receive ' + self . filename )
2015-04-11 20:23:04 +02:00
break
2015-04-14 22:36:37 +02:00
def transfer ( self ) :
"""
Starting the transfert of the file
: return :
"""
print ( ' [Media] Starting media transfer for ' + self . filename )
2015-04-11 20:23:04 +02:00
2015-04-15 17:52:28 +02:00
f = open ( " download/ " + self . filename , " wb " )
2015-04-11 20:23:04 +02:00
while 1 :
2015-04-14 22:36:37 +02:00
data = self . mconn . recv ( 1024 )
2015-04-11 20:23:04 +02:00
if not data : break
f . write ( data )
f . close ( )
2015-04-14 22:36:37 +02:00
print ( ' [Media] Got ' + self . filename )
print ( ' [Media] Closing media transfer for ' + self . filename )
2015-04-11 20:23:04 +02:00
2015-04-14 22:36:37 +02:00
def close ( self ) :
"""
We close the connection
: return :
"""
self . mconn . close ( )
self . msock . close ( )
def process ( self ) :
"""
function who start all the function
: return :
"""
self . bindmsock ( )
self . acceptmsock ( )
self . transfer ( )
self . close ( )
#########################################################
2015-04-11 20:23:04 +02:00
2015-04-02 16:36:24 +02:00
class MySignal ( QObject ) :
sig = Signal ( str )
2015-04-09 22:35:08 +02:00
2015-04-02 16:36:24 +02:00
class MyLongThread ( QThread ) :
def __init__ ( self , parent = None ) :
2015-04-14 22:36:37 +02:00
2015-04-02 16:36:24 +02:00
QThread . __init__ ( self , parent )
self . exiting = False
self . signal = MySignal ( )
2015-04-09 22:35:08 +02:00
2015-04-02 16:36:24 +02:00
def run ( self ) :
end = time . time ( ) + 10
2015-04-14 22:36:37 +02:00
while not self . exiting :
2015-04-02 16:36:24 +02:00
sys . stdout . write ( ' * ' )
sys . stdout . flush ( )
time . sleep ( 1 )
now = time . time ( )
2015-04-14 22:36:37 +02:00
if now > = end :
2015-04-02 16:36:24 +02:00
self . exiting = True
self . signal . sig . emit ( ' OK ' )
2015-04-09 22:35:08 +02:00
2015-04-02 16:36:24 +02:00
class MyThread ( QThread ) :
def __init__ ( self , parent = None ) :
QThread . __init__ ( self , parent )
2015-04-09 22:35:08 +02:00
2015-04-02 16:36:24 +02:00
self . exiting = False
2015-04-09 22:35:08 +02:00
2015-04-02 16:36:24 +02:00
def run ( self ) :
self . s . settimeout ( None )
data = self . s . recv ( 4096 )
messgServeur = ( data . decode ( ) )
self . gui . setNewMsg ( messgServeur )
2015-04-09 22:35:08 +02:00
2015-04-02 16:36:24 +02:00
def setConfig ( self , s , gui ) :
self . s = s
self . gui = gui
2015-04-11 20:23:04 +02:00
2015-04-12 00:03:38 +02:00
2015-04-14 22:36:37 +02:00
#########################################################
2015-04-12 00:03:38 +02:00
2015-04-11 20:23:04 +02:00
2015-04-14 22:36:37 +02:00
class privateFile ( ) :
def __init__ ( self , main , s , pseudoFile ) :
"""
new windows for the p2p
: param main :
: param s :
: param pseudoFile : pseudo of the people who need to send the file
: return :
"""
2015-04-10 18:18:58 +02:00
self . main = main
self . s = s
2015-04-11 16:55:45 +02:00
self . pseudoFile = pseudoFile
2015-04-10 18:18:58 +02:00
self . g = QtGui . QWidget ( )
self . ui = Ui_Dialog3 ( )
self . ui . setupUi ( self . g )
self . g . show ( )
2015-04-11 16:55:45 +02:00
self . ui . label_2 . setText ( self . pseudoFile )
self . ui . pushButton_2 . clicked . connect ( self . selectFile )
self . ui . pushButton . clicked . connect ( self . sendFile )
def sendFile ( self ) :
2015-04-14 22:36:37 +02:00
"""
Send to the sever the command : / pmfile + pseudo to send + name of the file
: return :
"""
if self . ui . lineEdit . text ( ) != " " :
2015-04-11 16:55:45 +02:00
self . ui . lineEdit . setText ( " " )
try :
print ( self . cmd1 . encode ( ) )
self . s . send ( self . cmd1 . encode ( ) )
2015-04-11 20:23:04 +02:00
self . g . close ( )
2015-04-11 16:55:45 +02:00
except timeout :
self . ShowMessageErreur ( " Erreur : Timeout. Le serveur ne repond pas " )
def selectFile ( self ) :
2015-04-14 22:36:37 +02:00
"""
buttton to open and chose the file to send and create the name of the command
: return :
"""
2015-04-11 16:55:45 +02:00
nomFile = ' ' . join ( QFileDialog . getOpenFileName ( ) )
self . ui . lineEdit . setText ( ' /pmfile ' + self . pseudoFile + " " + nomFile )
self . cmd1 = self . ui . lineEdit . text ( )
self . bob = ' ' . join ( nomFile . split ( " / " ) [ - 1 : ] )
2015-04-11 20:23:04 +02:00
2015-04-14 22:36:37 +02:00
#########################################################
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
class privateMessage ( ) :
def __init__ ( self , main , s , pmPerson , pmPerso ) :
"""
new windows for a private conversation
: param main :
: param s :
: param pmPerson :
: param pmPerso :
: return :
"""
2015-04-04 23:56:10 +02:00
self . main = main
2015-04-05 13:44:36 +02:00
self . s = s
2015-04-07 19:27:33 +02:00
self . pmPerso = pmPerso
2015-04-06 17:11:47 +02:00
self . pmPerson = pmPerson
2015-04-04 23:56:10 +02:00
self . g = QtGui . QWidget ( )
2015-04-06 17:11:47 +02:00
self . ui = Ui_Dialog2 ( )
2015-04-04 23:56:10 +02:00
self . ui . setupUi ( self . g )
self . g . show ( )
2015-04-06 17:11:47 +02:00
self . message_buffer2 = " "
2015-04-05 13:44:36 +02:00
2015-04-09 00:32:38 +02:00
self . g . setWindowState ( self . g . windowState ( ) & ~ QtCore . Qt . WindowMinimized | QtCore . Qt . WindowActive )
self . g . activateWindow ( )
2015-04-09 22:35:08 +02:00
2015-04-09 00:32:38 +02:00
Qt . WindowStaysOnTopHint
2015-04-14 22:36:37 +02:00
self . queueMsg2 = [ ]
2015-04-05 13:44:36 +02:00
self . thread = MyThread ( )
2015-04-06 17:11:47 +02:00
self . thread . finished . connect ( self . UpdateChatP )
2015-04-05 13:44:36 +02:00
self . ui . pushButton . clicked . connect ( self . send )
2015-04-09 12:01:45 +02:00
self . ui . lineEdit . returnPressed . connect ( self . send )
2015-04-09 22:35:08 +02:00
2015-04-09 00:32:38 +02:00
self . ui . pushButton_4 . clicked . connect ( self . reject )
2015-04-07 19:27:33 +02:00
self . ui . pushButton_3 . clicked . connect ( self . accept )
2015-04-11 16:55:45 +02:00
2015-04-06 17:11:47 +02:00
self . ui . label_2 . setText ( pmPerson )
2015-04-05 13:44:36 +02:00
2015-04-14 22:36:37 +02:00
def codeNb ( self , txt ) :
"""
converted a return code ( info and succes ) in the message
: param txt : code from the server
: return : info , String return from server
"""
2015-04-13 18:04:59 +02:00
if txt == " 300 " : info = " USERLIST "
elif txt == " 301 " : info = " USERAWAY "
elif txt == " 302 " : info = " HAS_JOIN "
elif txt == " 303 " : info = " HAS_LEFT "
elif txt == " 304 " : info = " NEW_MSG "
elif txt == " 305 " : info = " NAME_CHANGED "
elif txt == " 306 " : info = " NEW_PM "
elif txt == " 307 " : info = " ASKING_FOR_PM "
2015-04-14 22:36:37 +02:00
elif txt == " 308 " : info = " PRIVATE_DISCU_ACCEPTED_FROM "
elif txt == " 309 " : info = " PRIVATE_DISCU_REFUSED_FROM "
2015-04-13 18:04:59 +02:00
elif txt == " 310 " : info = " IS_NOW_ENABLE "
elif txt == " 311 " : info = " IS_NOW_DISABLE "
elif txt == " 312 " : info = " HAS_ASKED_FILE "
elif txt == " 313 " : info = " CAN_SEND_FILE "
elif txt == " 314 " : info = " HAS_REJECT_FILE "
2015-04-14 22:36:37 +02:00
elif txt == " 200 " or txt == " 200300 " : info = " SUCC_CHANNEL_JOINED "
elif txt == " 200300 " : info = " SUCC_CHANNEL_JOINED USERLIST "
2015-04-13 18:04:59 +02:00
elif txt == " 201 " : info = " SUCC_CHANNEL_QUIT "
elif txt == " 202 " : info = " SUCC_MESSAGE_SENDED "
elif txt == " 203 " : info = " SUCC_NICKNAME_CHANGED "
elif txt == " 204 " : info = " SUCC_VALID_NICKNAME "
elif txt == " 205 " : info = " SUCC_PM_SENDED "
elif txt == " 206 " : info = " SUCCESSFUL_ASKED_CONV "
elif txt == " 207 " : info = " SUCCESSFUL_ACCEPTED_CONV "
elif txt == " 208 " : info = " SUCCESSFUL_REFUSED_CONV "
elif txt == " 209 " : info = " SUCC_ENABLED "
elif txt == " 210 " : info = " SUCC_DISABLED "
2015-04-14 13:38:55 +02:00
elif txt == " 211 " : info = " SUCC_PMFILE "
2015-04-14 22:36:37 +02:00
elif txt == " 212 " : info = " SUCC_ACCEPTED_FILE "
2015-04-13 18:04:59 +02:00
elif txt == " 213 " : info = " SUCC_REFUSED_FILE "
2015-04-14 22:36:37 +02:00
else : info = txt
2015-04-13 18:04:59 +02:00
return info
2015-04-09 00:32:38 +02:00
def reject ( self ) :
2015-04-14 22:36:37 +02:00
"""
if a user reject a pm conversation
: return :
"""
2015-04-09 00:32:38 +02:00
self . cmRej = " /rejectpm " + self . pmPerson
try :
2015-04-09 12:01:45 +02:00
self . s . send ( self . cmRej . encode ( ) )
2015-04-09 00:32:38 +02:00
except timeout :
self . ShowMessageErreur ( " Erreur : Timeout. Le serveur ne repond pas " )
self . ui . txtOutput . setText ( self . message_buffer2 )
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
2015-04-09 22:35:08 +02:00
2015-04-07 19:27:33 +02:00
def accept ( self ) :
2015-04-14 22:36:37 +02:00
"""
if a user accept a pm with another user
: return :
"""
2015-04-07 19:27:33 +02:00
self . cmAcc = " /acceptpm " + self . pmPerson
try :
self . s . send ( self . cmAcc . encode ( ) )
except timeout :
self . ShowMessageErreur ( " Erreur : Timeout. Le serveur ne repond pas " )
self . ui . txtOutput . setText ( self . message_buffer2 )
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
2015-04-09 22:35:08 +02:00
2015-04-07 19:27:33 +02:00
def htmlToText ( self , html ) :
2015-04-14 22:36:37 +02:00
"""
converted some characters written by the user ( html tag , smiley )
: param html : message written by an user
: return : html , message converted
"""
2015-04-15 17:23:26 +02:00
html = html . replace ( ' & ' , ' & ' )
2015-04-07 19:27:33 +02:00
html = html . replace ( ' > ' , ' > ' )
2015-04-15 17:23:26 +02:00
html = html . replace ( ' < ' , ' " ' )
2015-04-07 19:27:33 +02:00
html = html . replace ( ' :-) ' , ' <img src= " img/happy.png " alt= " Smiley face " > ' )
html = html . replace ( ' :-( ' , ' <img src= " img/sad.png " alt= " sad face " > ' )
html = html . replace ( ' :-p ' , ' <img src= " img/langue.png " alt= " langue face " > ' )
html = html . replace ( ' ;-) ' , ' <img src= " img/oeil.png " alt= " oeil face " > ' )
html = html . replace ( ' :-D ' , ' <img src= " img/veryHappy.png " alt= " very happy face " > ' )
html = html . replace ( ' :-o ' , ' <img src= " img/etonne.png " alt= " etonne face " > ' )
html = html . replace ( ' : \' ( ' , ' <img src= " img/cry.png " alt= " cry face " > ' )
html = html . replace ( ' (y) ' , ' <img src= " img/like.png " alt= " like face " > ' )
html = html . replace ( ' 8| ' , ' <img src= " img/lunette.png " alt= " lunette face " > ' )
html = html . replace ( ' 3:) ' , ' <img src= " img/hell.png " alt= " hell face " > ' )
html = html . replace ( ' :pedobear ' , ' <img src= " img/pedo.gif " alt= " hell face " > ' )
html = html . replace ( ' :homer ' , ' <img src= " img/homer.gif " alt= " homer face " > ' )
2015-04-15 17:23:26 +02:00
html = html . replace ( ' :dalek ' , ' <img src= " img/Dalek.gif " alt= " homer face " > ' )
html = html . replace ( ' :tardis ' , ' <img src= " img/tardis.png " alt= " homer face " > ' )
2015-04-05 13:44:36 +02:00
2015-04-07 19:27:33 +02:00
return html
def ShowMessageErreur ( self , txt ) :
2015-04-14 22:36:37 +02:00
"""
Show with color message from the server with error
: param txt : message from server
: return : message with color
"""
self . message_buffer2 + = ' <br> <span style= " color : red; font-weight: bold; " > ' + self . htmlToText ( txt ) + ' </span> '
2015-04-09 00:32:38 +02:00
2015-04-05 13:44:36 +02:00
def send ( self ) :
2015-04-14 22:36:37 +02:00
"""
send the message written in the pm conversation
: return :
"""
2015-04-07 19:27:33 +02:00
self . cmdP = self . ui . lineEdit . text ( )
if self . cmdP != " " :
2015-04-05 13:44:36 +02:00
self . ui . lineEdit . setText ( ' ' )
self . s . settimeout ( 5.0 )
2015-04-07 19:27:33 +02:00
self . cmd = " /pm " + self . pmPerson + " " + self . cmdP
2015-04-05 13:44:36 +02:00
try :
self . s . send ( self . cmd . encode ( ) )
2015-04-04 23:56:10 +02:00
2015-04-05 13:44:36 +02:00
except timeout :
self . ShowMessageErreur ( " Erreur : Timeout. Le serveur ne repond pas " )
2015-04-07 19:27:33 +02:00
self . ui . txtOutput . setText ( self . message_buffer2 )
2015-04-05 13:44:36 +02:00
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
def UpdateChatP ( self ) :
"""
update the chat box
: return :
"""
2015-04-06 17:11:47 +02:00
if self . queueMsg2 :
m = self . queueMsg2 . pop ( 0 )
if m :
self . thread . start ( )
self . ShowMessageAsTextPm ( m )
2015-04-11 16:55:45 +02:00
2015-04-07 19:27:33 +02:00
def getTimeStamp ( self ) :
2015-04-14 22:36:37 +02:00
"""
the time format : H : M
: return : time
"""
2015-04-07 19:27:33 +02:00
return ( ' [ %s ] ' % str ( datetime . datetime . fromtimestamp ( int ( time . time ( ) ) ) . strftime ( ' % H: % M ' ) ) )
2015-04-14 22:36:37 +02:00
def ShowMessageAsTextPm ( self , txt ) :
"""
add txt to the buffer with the time and color
: param txt : message from sever
: return :
"""
2015-04-15 17:23:26 +02:00
#self.message_buffer2 += '<br><span style="color : grey">'+self.codeNb(txt)+'</span>'
2015-04-07 19:27:33 +02:00
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_PRIVATE_DISCUSSION_REFUSED " :
self . g . close ( )
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_PRIVATE_DISCUSSION_REJECTED " :
self . g . close ( )
2015-04-09 12:01:45 +02:00
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_PM_SENDED " :
self . message_buffer2 + = ' <br><span style= " color : grey " > ' + self . getTimeStamp ( ) + ' </span> <span style= " color : red " > < ' + self . pmPerso + ' > </span> <span style= " color : black " > ' + self . htmlToText ( self . cmdP ) + ' </span> '
2015-04-07 19:27:33 +02:00
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_PRIVATE_DISCUSSION_ACCEPTED " :
self . message_buffer2 + = ' <br> <span style= " color : green " > Chalange Accepted ! </span> '
self . ui . pushButton_4 . setDisabled ( True )
self . ui . pushButton_3 . setDisabled ( True )
2015-04-09 12:01:45 +02:00
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " PRIVATE_DISCU_ACCEPTED_FROM " :
self . message_buffer2 + = ' <br> <span style= " color : green " > Private discussion with ' + txt . split ( " " ) [ 1 ] + ' accepted ! </span> '
self . ui . pushButton_4 . setDisabled ( True )
self . ui . pushButton_3 . setDisabled ( True )
2015-04-09 00:32:38 +02:00
2015-04-07 19:27:33 +02:00
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " NEW_PM " :
2015-04-15 13:50:06 +02:00
self . message_buffer2 + = ' <br><span style= " color : grey " > ' + self . getTimeStamp ( ) + ' </span> <span style= " color : red " > < ' + txt . split ( " " ) [ 1 ] + ' > </span> ' + self . htmlToText ( ' ' . join ( txt . split ( " " ) [ 2 : ] ) ) + ' '
2015-04-06 17:11:47 +02:00
2015-04-02 16:36:24 +02:00
2015-04-14 22:36:37 +02:00
self . ui . txtOutput . setText ( self . message_buffer2 )
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
2015-04-12 00:03:38 +02:00
2015-04-14 22:36:37 +02:00
######################################################################################################################
2015-04-12 00:03:38 +02:00
2015-04-09 00:32:38 +02:00
class start ( QtGui . QMainWindow ) :
2015-03-30 14:21:11 +02:00
def __init__ ( self ) :
2015-04-14 22:36:37 +02:00
"""
Main Windows with the main conversation
: return :
"""
2015-03-30 14:21:11 +02:00
super ( start , self ) . __init__ ( )
2015-04-02 16:36:24 +02:00
self . queueMsg = [ ]
self . thread = MyThread ( )
self . thread . finished . connect ( self . UpdateChat )
2015-03-30 14:21:11 +02:00
self . createWidgets ( )
2015-04-09 22:35:08 +02:00
2015-04-02 16:36:24 +02:00
def setNewMsg ( self , msg ) :
2015-04-14 22:36:37 +02:00
"""
add msg to the queueMsg
: param msg :
: return :
"""
2015-04-02 16:36:24 +02:00
self . queueMsg . append ( msg )
2015-04-09 22:35:08 +02:00
2015-03-30 21:39:02 +02:00
def getTimeStamp ( self ) :
2015-04-14 22:36:37 +02:00
"""
the time format : H : M
: return : time
"""
return ( ' [ %s ] ' % str ( datetime . datetime . fromtimestamp ( int ( time . time ( ) ) ) . strftime ( ' % H: % M ' ) ) )
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
def htmlToText ( self , html ) :
"""
converted some characters written by the user ( html tag , smiley )
: param html : message written by an user
: return : html , message converted
"""
2015-03-30 21:39:02 +02:00
2015-04-14 22:36:37 +02:00
html = html . replace ( ' & ' , ' & ' )
2015-04-02 11:49:43 +02:00
html = html . replace ( ' > ' , ' > ' )
2015-04-14 22:36:37 +02:00
html = html . replace ( ' < ' , ' " ' )
2015-04-06 19:08:09 +02:00
html = html . replace ( ' :-) ' , ' <img src= " img/happy.png " alt= " Smiley face " > ' )
html = html . replace ( ' :-( ' , ' <img src= " img/sad.png " alt= " sad face " > ' )
html = html . replace ( ' :-p ' , ' <img src= " img/langue.png " alt= " langue face " > ' )
html = html . replace ( ' ;-) ' , ' <img src= " img/oeil.png " alt= " oeil face " > ' )
html = html . replace ( ' :-D ' , ' <img src= " img/veryHappy.png " alt= " very happy face " > ' )
html = html . replace ( ' :-o ' , ' <img src= " img/etonne.png " alt= " etonne face " > ' )
html = html . replace ( ' : \' ( ' , ' <img src= " img/cry.png " alt= " cry face " > ' )
html = html . replace ( ' (y) ' , ' <img src= " img/like.png " alt= " like face " > ' )
html = html . replace ( ' 8| ' , ' <img src= " img/lunette.png " alt= " lunette face " > ' )
html = html . replace ( ' 3:) ' , ' <img src= " img/hell.png " alt= " hell face " > ' )
2015-04-06 20:09:58 +02:00
html = html . replace ( ' :pedobear ' , ' <img src= " img/pedo.gif " alt= " hell face " > ' )
html = html . replace ( ' :homer ' , ' <img src= " img/homer.gif " alt= " homer face " > ' )
2015-04-14 22:36:37 +02:00
html = html . replace ( ' :dalek ' , ' <img src= " img/Dalek.gif " alt= " homer face " > ' )
html = html . replace ( ' :tardis ' , ' <img src= " img/tardis.png " alt= " homer face " > ' )
2015-03-30 21:39:02 +02:00
2015-04-02 11:49:43 +02:00
return html
2015-03-31 11:52:01 +02:00
2015-04-02 16:36:24 +02:00
def ShowMessageErreur ( self , txt ) :
2015-04-14 22:36:37 +02:00
"""
add to the buffer an erreur message with color
: param txt : message from the server
: return :
"""
2015-04-02 16:36:24 +02:00
self . message_buffer + = ' <br> <span style= " color : red; font-weight: bold; " > ' + self . htmlToText ( txt ) + ' </span> '
2015-04-09 22:35:08 +02:00
2015-04-03 14:51:22 +02:00
def ShowMessageOK ( self , txt ) :
2015-04-14 22:36:37 +02:00
"""
add to the buffer a good message with style
: param txt : message from the server
: return :
"""
2015-04-03 14:51:22 +02:00
self . message_buffer + = ' <br> <span style= " color : green; font-weight: bold; " > ' + self . htmlToText ( txt ) + ' </span> '
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
def ShowMessageInfo ( self , txt ) :
"""
add to the buffer an info message with style
: param txt : message from the server
: return :
"""
2015-04-04 20:28:02 +02:00
self . message_buffer + = ' <br> <span style= " color : #FF00FF; font-weight: bold; " > ' + self . htmlToText ( txt ) + ' </span> '
2015-04-09 22:35:08 +02:00
2015-04-02 11:49:43 +02:00
def ShowMessageAsText ( self , txt ) :
2015-04-14 22:36:37 +02:00
"""
add to the buffer message from the server with style and run some function
: param txt : message from the server
: return :
"""
#self.message_buffer += '<br> <span style="color : #E6E6E6"> '+ self.codeNb(str(txt)) +' </span>'
2015-04-12 21:30:23 +02:00
if re . match ( " ^4 " , txt ) :
self . ShowMessageErreur ( " Erreur ! : " + self . errNb ( txt ) )
2015-04-09 22:35:08 +02:00
2015-04-13 18:04:59 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " IS_NOW_DISABLE " :
2015-04-04 20:28:02 +02:00
self . ShowMessageInfo ( txt . split ( " " ) [ 1 ] + " is Away From Keyboard " )
self . ui . listNames . clear ( )
self . ui . listNames_2 . clear ( )
self . s . send ( " /userlist " . encode ( ) )
self . s . send ( " /userlistaway " . encode ( ) )
2015-04-09 22:35:08 +02:00
2015-04-13 18:04:59 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " IS_NOW_ENABLE " :
2015-04-04 20:28:02 +02:00
self . ShowMessageInfo ( txt . split ( " " ) [ 1 ] + " is Back !! " )
self . ui . listNames . clear ( )
self . ui . listNames_2 . clear ( )
self . s . send ( " /userlist " . encode ( ) )
self . s . send ( " /userlistaway " . encode ( ) )
2015-04-09 22:35:08 +02:00
2015-04-13 18:04:59 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " HAS_ASKED_FILE " :
2015-04-11 16:55:45 +02:00
self . ShowMessageOK ( txt . split ( " " ) [ 1 ] + " share a file with you, do you want download " + ' ' . join ( txt . split ( " " ) [ 2 ] . split ( " / " ) [ - 1 : ] ) + " ? " )
self . questionMessage ( txt . split ( " " ) [ 1 ] , txt . split ( " " ) [ 2 ] )
2015-04-11 20:23:04 +02:00
self . fileNom = ' ' . join ( txt . split ( " " ) [ 2 ] . split ( " / " ) [ - 1 : ] )
2015-04-11 16:55:45 +02:00
2015-04-13 18:04:59 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_ASKED_FILE " :
2015-04-11 16:55:45 +02:00
self . ShowMessageOK ( " Succes asked file " )
2015-04-13 20:50:40 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_ACCEPTED_FILE " :
2015-04-11 16:55:45 +02:00
self . ShowMessageOK ( " accepted file on ip " + txt . split ( " " ) [ 1 ] )
2015-04-11 20:23:04 +02:00
s = StreamHandler ( self . portFile , self . fileNom )
s . start ( )
2015-04-14 22:36:37 +02:00
2015-04-13 18:04:59 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " CAN_SEND_FILE " :
2015-04-11 16:55:45 +02:00
self . ShowMessageOK ( " file can be send " )
2015-04-11 20:23:04 +02:00
ms = socket ( AF_INET , SOCK_STREAM )
2015-04-11 20:38:03 +02:00
print ( txt . split ( " " ) [ 2 ] + " " + txt . split ( " " ) [ 3 ] )
2015-04-11 20:23:04 +02:00
ms . connect ( ( str ( txt . split ( " " ) [ 2 ] ) , int ( txt . split ( " " ) [ 3 ] ) ) )
2015-04-11 16:55:45 +02:00
f = open ( txt . split ( " " ) [ 4 ] , " rb " )
data = f . read ( )
f . close ( )
ms . send ( data )
2015-04-11 20:23:04 +02:00
ms . close ( )
2015-04-11 16:55:45 +02:00
2015-04-13 18:04:59 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCCESSFUL_ACCEPTED_CONV " :
2015-04-09 12:01:45 +02:00
self . message_buffer + = ' <br> <span style= " color : green " > PRIVATE DISCUSSION ? challenge accepted ! '
self . private2 . ShowMessageAsTextPm ( " SUCC_PRIVATE_DISCUSSION_ACCEPTED " )
2015-04-09 00:32:38 +02:00
2015-04-13 20:50:40 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " PRIVATE_DISCU_ACCEPTED_FROM " :
2015-04-14 22:36:37 +02:00
self . message_buffer + = ' <br> <span style= " color : green " > PRIVATE DISCUSSION WITH ' + txt . split ( " " ) [ 1 ] + ' ? challenge accepted ! '
2015-04-09 12:01:45 +02:00
self . private2 . ShowMessageAsTextPm ( txt )
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCCESSFUL_ASKED_CONV " :
2015-04-04 23:56:10 +02:00
self . ShowMessageOK ( " invitation requested " )
2015-04-14 22:36:37 +02:00
self . private2 = privateMessage ( self , self . s , self . demande , self . pseudo )
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " ASKING_FOR_PM " :
self . ShowMessageOK ( " private discution from " + txt . split ( " " ) [ 1 ] )
self . private2 = privateMessage ( self , self . s , txt . split ( " " ) [ 1 ] , self . pseudo )
2015-04-07 19:27:33 +02:00
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_PM_SENDED " :
2015-04-07 19:27:33 +02:00
self . private2 . ShowMessageAsTextPm ( txt . split ( " " ) [ 0 ] )
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " NEW_PM " :
2015-04-07 19:27:33 +02:00
self . private2 . ShowMessageAsTextPm ( txt )
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_PRIVATE_DISCUSSION_REFUSED " :
2015-04-09 12:01:45 +02:00
self . private2 . ShowMessageAsTextPm ( txt )
self . ShowMessageOK ( " Private discussion refused !! " )
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_PRIVATE_DISCUSSION_REJECTED " :
2015-04-09 12:01:45 +02:00
self . private2 . ShowMessageAsTextPm ( txt )
self . ShowMessageOK ( txt . split ( " " ) [ 1 ] + " Rejected your Private discussion !! " )
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_CHANNEL_QUIT " :
2015-04-09 00:32:38 +02:00
self . ShowMessageOK ( " You have logged out of the DNC ! " )
2015-04-08 16:44:40 +02:00
self . ui . listNames . clear ( )
self . ui . listNames_2 . clear ( )
2015-04-15 17:23:26 +02:00
self . ui . lineEdit . setDisabled ( True )
self . ui . pushButton . setDisabled ( True )
self . ui . pushButton_2 . setDisabled ( False )
self . ui . pushButton_3 . setDisabled ( True )
self . ui . pushButton_6 . setDisabled ( True )
self . ui . lineEdit_4 . setDisabled ( False )
self . ui . lineEdit_3 . setDisabled ( False )
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_DISABLED " :
2015-04-04 00:15:52 +02:00
self . ShowMessageOK ( " You are AFK ! " )
self . ui . listNames . clear ( )
2015-04-04 20:28:02 +02:00
self . ui . listNames_2 . clear ( )
self . s . send ( " /userlist " . encode ( ) )
2015-04-04 00:15:52 +02:00
self . s . send ( " /userlistaway " . encode ( ) )
2015-04-03 20:07:29 +02:00
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_ENABLED " :
2015-04-04 00:15:52 +02:00
self . ShowMessageOK ( " You are back ! " )
self . ui . listNames . clear ( )
self . ui . listNames_2 . clear ( )
self . s . send ( " /userlist " . encode ( ) )
self . s . send ( " /userlistaway " . encode ( ) )
2015-04-03 12:19:17 +02:00
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_NICKNAME_CHANGED " :
2015-04-15 17:23:26 +02:00
self . ShowMessageOK ( " Sucessful nickname change ! " )
self . ui . listNames . clear ( )
self . s . send ( " /userlist " . encode ( ) )
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
if self . errNb ( txt . split ( " " ) [ 0 ] ) == " ERR_INVALID_NICKNAME " :
2015-04-06 17:11:47 +02:00
self . pseudo = " INVALID_NICKNAME "
2015-04-07 19:27:33 +02:00
2015-04-14 22:36:37 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " NAME_CHANGED " :
2015-04-03 23:28:55 +02:00
self . ShowMessageNameChange ( txt . split ( " " ) [ 1 ] , txt . split ( " " ) [ 2 ] )
self . ui . listNames . clear ( )
self . s . send ( " /userlist " . encode ( ) )
2015-04-09 22:35:08 +02:00
2015-04-13 18:04:59 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " HAS_JOIN " :
2015-04-03 12:12:01 +02:00
self . ShowMessageHasJoin ( txt . split ( " " ) [ 1 ] )
2015-04-03 23:28:55 +02:00
self . ui . listNames . addItem ( txt . split ( " " ) [ 1 ] )
2015-04-09 22:35:08 +02:00
2015-04-13 18:04:59 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " HAS_LEFT " :
2015-04-03 23:28:55 +02:00
self . ShowMessageHasLeft ( txt . split ( " " ) [ 1 ] )
self . ui . listNames . clear ( )
self . s . send ( " /userlist " . encode ( ) )
2015-04-09 22:35:08 +02:00
2015-04-13 18:04:59 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " SUCC_CHANNEL_JOINED " or txt . split ( " " ) [ 0 ] == " 200 " :
2015-04-08 16:44:40 +02:00
self . ShowMessageHasJoin ( self . pseudo )
self . ui . listNames . clear ( )
self . ui . listNames_2 . clear ( )
2015-04-13 20:50:40 +02:00
self . s . send ( " /userlist " . encode ( ) )
self . s . send ( " /userlistaway " . encode ( ) )
2015-04-09 00:32:38 +02:00
2015-04-14 22:36:37 +02:00
if self . errNb ( txt . split ( " " ) [ 0 ] ) == " ERR_NICKNAME_ALREADY_USED " :
2015-04-09 00:32:38 +02:00
self . deco ( )
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
if re . compile ( ' USERLIST ' ) . search ( self . codeNb ( txt . split ( " " ) [ 0 ] ) ) :
2015-04-13 20:50:40 +02:00
self . ui . listNames . clear ( )
2015-04-14 22:36:37 +02:00
n = len ( txt . split ( " " ) [ 1 : ] ) + 1
for i in range ( 1 , n ) :
self . ui . listNames . addItem ( str ( txt . split ( " " ) [ i ] ) . replace ( " 301 " , " " ) )
#print(str(txt.split(" ")[1:]))
2015-04-03 23:28:55 +02:00
2015-04-14 22:36:37 +02:00
if re . compile ( ' USERAWAY ' ) . search ( self . codeNb ( txt . split ( " " ) [ 0 ] ) ) :
2015-04-13 20:50:40 +02:00
self . ui . listNames_2 . clear ( )
2015-04-14 22:36:37 +02:00
n = len ( txt . split ( " " ) [ 1 : ] ) + 1
for i in range ( 1 , n ) :
2015-04-04 00:15:52 +02:00
self . ui . listNames_2 . addItem ( str ( txt . split ( " " ) [ i ] ) )
2015-04-14 22:36:37 +02:00
#print(str(txt.split(" ")[1:]))
2015-04-09 22:35:08 +02:00
2015-04-13 18:04:59 +02:00
if self . codeNb ( txt . split ( " " ) [ 0 ] ) == " NEW_MSG " :
2015-04-09 00:32:38 +02:00
self . message_buffer + = ' <br><span style= " color : grey " > ' + self . getTimeStamp ( ) + ' </span> <span style= " color : red " > < ' + txt . split ( " " ) [ 1 ] + ' > </span> <span style= " color : black " > ' + self . htmlToText ( ' ' . join ( txt . split ( " " ) [ 2 : ] ) ) + ' </span> '
2015-04-03 11:18:59 +02:00
2015-04-13 18:04:59 +02:00
if self . codeNb ( txt ) == " SUCC_MESSAGE_SENDED " :
2015-04-09 00:32:38 +02:00
self . message_buffer + = ' <br><span style= " color : grey " > ' + self . getTimeStamp ( ) + ' </span> <span style= " color : red " > < ' + self . pseudo + ' > </span><span style= " color : black " > ' + self . htmlToText ( self . cmd ) + ' </span> '
2015-04-09 22:35:08 +02:00
2015-04-12 21:30:23 +02:00
def errNb ( self , txt ) :
2015-04-14 22:36:37 +02:00
"""
converted a return error code in the message
: param txt : code from the server
: return : String message
"""
if txt == " 400 " :
2015-04-12 21:30:23 +02:00
info = " ERR_NICKNAME_ALREADY_USED "
2015-04-14 22:36:37 +02:00
elif txt == " 401 " :
2015-04-12 21:30:23 +02:00
info = " ERR_NO_NICKNAME "
2015-04-14 22:36:37 +02:00
elif txt == " 402 " :
2015-04-12 21:30:23 +02:00
info = " ERR_CONV_NOT_ALLOWED "
2015-04-14 22:36:37 +02:00
elif txt == " 403 " :
2015-04-12 21:30:23 +02:00
info = " DEST_NOT_FOUND "
2015-04-14 22:36:37 +02:00
elif txt == " 404 " :
2015-04-12 21:30:23 +02:00
info = " ERR_ALREADY_ASKED_FOR_PM "
2015-04-14 22:36:37 +02:00
elif txt == " 405 " :
2015-04-12 21:30:23 +02:00
info = " ERR_NO_INVIT_TO_CONV_FOUND "
2015-04-14 22:36:37 +02:00
elif txt == " 406 " :
2015-04-12 21:30:23 +02:00
info = " ERR_UNKNOWN_ACCEPTED_FILE "
2015-04-14 22:36:37 +02:00
elif txt == " 407 " :
2015-04-12 21:30:23 +02:00
info = " COMMAND_NOT_FOUND "
2015-04-14 22:36:37 +02:00
elif txt == " 408 " :
2015-04-12 21:30:23 +02:00
info = " ERR_INVALID_NICKNAME "
2015-04-14 22:36:37 +02:00
else :
info = " ERREUR " + txt
2015-04-12 21:30:23 +02:00
return info
def codeNb ( self , txt ) :
2015-04-14 22:36:37 +02:00
"""
converted a return code in the message
: param txt : code from the server
: return : info , String return from server
"""
2015-04-13 18:04:59 +02:00
if txt == " 300 " : info = " USERLIST "
elif txt == " 301 " : info = " USERAWAY "
elif txt == " 302 " : info = " HAS_JOIN "
elif txt == " 303 " : info = " HAS_LEFT "
elif txt == " 304 " : info = " NEW_MSG "
elif txt == " 305 " : info = " NAME_CHANGED "
elif txt == " 306 " : info = " NEW_PM "
elif txt == " 307 " : info = " ASKING_FOR_PM "
2015-04-14 22:36:37 +02:00
elif txt == " 308 " : info = " PRIVATE_DISCU_ACCEPTED_FROM "
elif txt == " 309 " : info = " PRIVATE_DISCU_REFUSED_FROM "
2015-04-13 18:04:59 +02:00
elif txt == " 310 " : info = " IS_NOW_ENABLE "
elif txt == " 311 " : info = " IS_NOW_DISABLE "
elif txt == " 312 " : info = " HAS_ASKED_FILE "
elif txt == " 313 " : info = " CAN_SEND_FILE "
elif txt == " 314 " : info = " HAS_REJECT_FILE "
elif txt == " 200 " or txt == " 200300 " : info = " SUCC_CHANNEL_JOINED "
2015-04-14 22:36:37 +02:00
elif txt == " 200300 " : info = " SUCC_CHANNEL_JOINED USERLIST "
2015-04-13 18:04:59 +02:00
elif txt == " 201 " : info = " SUCC_CHANNEL_QUIT "
elif txt == " 202 " : info = " SUCC_MESSAGE_SENDED "
elif txt == " 203 " : info = " SUCC_NICKNAME_CHANGED "
elif txt == " 204 " : info = " SUCC_VALID_NICKNAME "
elif txt == " 205 " : info = " SUCC_PM_SENDED "
elif txt == " 206 " : info = " SUCCESSFUL_ASKED_CONV "
elif txt == " 207 " : info = " SUCCESSFUL_ACCEPTED_CONV "
elif txt == " 208 " : info = " SUCCESSFUL_REFUSED_CONV "
elif txt == " 209 " : info = " SUCC_ENABLED "
elif txt == " 210 " : info = " SUCC_DISABLED "
2015-04-14 22:36:37 +02:00
elif txt == " 211 " : info = " SUCC_PMFILE "
elif txt == " 212 " : info = " SUCC_ACCEPTED_FILE "
2015-04-13 18:04:59 +02:00
elif txt == " 213 " : info = " SUCC_REFUSED_FILE "
2015-04-14 22:36:37 +02:00
else : info = txt
2015-04-12 21:30:23 +02:00
return info
2015-04-14 22:36:37 +02:00
def ShowMessageHasJoin ( self , txt ) :
"""
adds a message to the buffer to alert users that a person has joined the dnc
: param txt : user name
: return :
"""
2015-04-03 11:18:59 +02:00
self . message_buffer + = ' <br> <span style= " color : #FF00FF; font-weight: bold; " > ' + self . htmlToText ( txt ) + ' has joined DNC </span> '
2015-04-03 23:28:55 +02:00
2015-04-14 22:36:37 +02:00
def ShowMessageHasLeft ( self , txt ) :
"""
adds a message to the buffer to alert users that a person has left the dnc
: param txt : user name
: return :
"""
2015-04-09 22:35:08 +02:00
self . message_buffer + = ' <br> <span style= " color : #FF00FF; font-weight: bold; " > ' + self . htmlToText ( txt ) + ' has left DNC </span> '
2015-04-14 22:36:37 +02:00
def ShowMessageNameChange ( self , txt , txt2 ) :
"""
adds a message to the buffer to alert users that a person change his name
: param txt : old name
: param txt2 : new name
: return :
"""
2015-04-03 12:12:01 +02:00
self . message_buffer + = ' <br> <span style= " color : #FF00FF; font-weight: bold; " > ' + self . htmlToText ( txt ) + ' is now : ' + self . htmlToText ( txt2 ) + ' </span> '
2015-04-03 11:18:59 +02:00
2015-04-14 22:36:37 +02:00
def UpdateChat ( self ) :
"""
update the chat with the buffer
: return :
"""
if self . queueMsg :
2015-04-02 16:36:24 +02:00
m = self . queueMsg . pop ( 0 )
2015-04-14 22:36:37 +02:00
if m :
2015-04-02 16:36:24 +02:00
self . thread . start ( )
self . ShowMessageAsText ( m )
self . ui . txtOutput . setText ( self . message_buffer )
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
def questionMessage ( self , name , fileN ) :
"""
open un QMessageBox to ask the user whether to download a file from another user
: param name : user name who send file
: param fileN : file name
: return :
"""
reply = QtGui . QMessageBox . question ( self , " send file " , " do you want to download the file : " + ' ' . join ( fileN . split ( " / " ) [ - 1 : ] ) + " from " + name + " ? " , QtGui . QMessageBox . Yes | QtGui . QMessageBox . No )
2015-04-11 16:55:45 +02:00
if reply == QtGui . QMessageBox . Yes :
2015-04-12 00:03:38 +02:00
text = ' ' . join ( str ( random . randint ( 1 , 9 ) ) for _ in range ( 4 ) )
2015-04-12 16:21:25 +02:00
while text == self . portCo :
text = ' ' . join ( str ( random . randint ( 1 , 9 ) ) for _ in range ( 4 ) )
2015-04-12 00:03:38 +02:00
cmdAccF = " /acceptfile " + name + " " + text + " " + fileN
try :
self . s . send ( cmdAccF . encode ( ) )
print ( cmdAccF )
self . portFile = text
except timeout :
self . ShowMessageErreur ( " Erreur : Timeout. Le serveur ne repond pas " )
self . ui . txtOutput . setText ( self . message_buffer )
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
2015-04-14 22:36:37 +02:00
2015-04-11 16:55:45 +02:00
elif reply == QtGui . QMessageBox . No :
2015-04-12 00:03:38 +02:00
try :
2015-04-14 22:36:37 +02:00
cmdRej = " /rejectfile " + name + " " + fileN
2015-04-12 00:03:38 +02:00
print ( cmdRej )
self . s . send ( cmdRej . encode ( ) )
except timeout :
self . ShowMessageErreur ( " Erreur : Timeout. Le serveur ne repond pas " )
self . ui . txtOutput . setText ( self . message_buffer )
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
2015-04-11 16:55:45 +02:00
2015-03-30 14:21:11 +02:00
def connectActions ( self ) :
2015-04-14 22:36:37 +02:00
"""
add an action to buttons
: return :
"""
2015-03-31 11:52:01 +02:00
self . ui . pushButton_2 . clicked . connect ( self . connecter )
self . ui . pushButton_3 . clicked . connect ( self . deco )
2015-03-30 14:21:11 +02:00
self . ui . pushButton . clicked . connect ( self . client )
2015-04-03 14:51:22 +02:00
self . ui . pushButton_6 . clicked . connect ( self . changeN )
2015-04-03 20:07:29 +02:00
self . ui . pushButton_5 . clicked . connect ( self . away )
2015-04-09 12:01:45 +02:00
self . ui . lineEdit . returnPressed . connect ( self . client )
2015-04-09 22:35:08 +02:00
self . ui . listNames . setContextMenuPolicy ( QtCore . Qt . CustomContextMenu )
self . ui . listNames . customContextMenuRequested . connect ( self . buttonAMenu )
@QtCore.pyqtSlot ( )
def on_buttonA_released ( self ) :
print ( ' Doing Stuff when clicking on Button A ' )
def buttonAMenu ( self , pos ) :
2015-04-14 22:36:37 +02:00
"""
menu with the right click on the user list
: param pos :
: return :
"""
2015-04-09 22:35:08 +02:00
menu = QtGui . QMenu ( )
2015-04-11 00:05:25 +02:00
menu . addAction ( ' Private discussion ' , lambda : self . FirstActionButtonA ( ) )
menu . addAction ( ' Send file ' , lambda : self . SecondActionButtonA ( ) )
2015-04-09 22:35:08 +02:00
menu . exec_ ( QtGui . QCursor . pos ( ) )
2015-04-11 00:05:25 +02:00
def FirstActionButtonA ( self ) :
2015-04-14 22:36:37 +02:00
"""
first action ( ask a pm ) on the menu of the right click
: return :
"""
2015-04-11 00:05:25 +02:00
test1 = self . ui . listNames . currentItem ( ) . text ( )
print ( " 1e fonction : " + str ( test1 ) )
self . someMethod ( str ( test1 ) )
2015-04-09 22:35:08 +02:00
2015-04-11 00:05:25 +02:00
def SecondActionButtonA ( self ) :
2015-04-14 22:36:37 +02:00
"""
second action ( send file ) on the menu of the right click
: return :
"""
2015-04-11 00:05:25 +02:00
test1 = self . ui . listNames . currentItem ( ) . text ( )
print ( " 2sd fonction : " + str ( test1 ) )
2015-04-14 22:36:37 +02:00
self . privateFile = privateFile ( self , self . s , str ( test1 ) )
2015-04-09 22:35:08 +02:00
2015-04-06 17:11:47 +02:00
def someMethod ( self , item ) :
2015-04-14 22:36:37 +02:00
"""
ask a pm to another user of the list
: param item : user name from the list
: return :
"""
2015-04-13 18:04:59 +02:00
nom = item . replace ( " SUCCESSFUL_ASKED_CONV " , " " )
2015-04-06 17:11:47 +02:00
cmdPM = " /askpm " + nom
try :
self . s . send ( cmdPM . encode ( ) )
self . demande = nom
except timeout :
self . ShowMessageErreur ( " Erreur : Timeout. Le serveur ne repond pas " )
self . ui . txtOutput . setText ( self . message_buffer )
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
2015-04-03 20:07:29 +02:00
def away ( self ) :
2015-04-14 22:36:37 +02:00
"""
action on the button afk and back
: return :
"""
2015-04-09 22:35:08 +02:00
if self . bouton == " disable " :
2015-04-03 20:07:29 +02:00
cmdAway = " /disable "
try :
self . s . send ( cmdAway . encode ( ) )
self . ui . pushButton_5 . setText ( " Back " )
except timeout :
self . ShowMessageErreur ( " Erreur : Timeout. Le serveur ne repond pas " )
self . ui . txtOutput . setText ( self . message_buffer )
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
self . bouton = " enable "
2015-04-09 22:35:08 +02:00
2015-04-03 20:07:29 +02:00
elif self . bouton == " enable " :
self . bouton = " disable "
cmdAway = " /enable "
try :
self . s . send ( cmdAway . encode ( ) )
self . ui . pushButton_5 . setText ( " Away From Keyboard " )
except timeout :
self . ShowMessageErreur ( " Erreur : Timeout. Le serveur ne repond pas " )
self . ui . txtOutput . setText ( self . message_buffer )
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
2015-04-03 14:51:22 +02:00
def changeN ( self ) :
2015-04-14 22:36:37 +02:00
"""
action to change name
: return :
"""
2015-04-03 14:51:22 +02:00
changePseudo = self . ui . lineEdit_2 . text ( )
cmdChange = " /name " + changePseudo
try :
self . s . send ( cmdChange . encode ( ) )
self . pseudo = changePseudo
2015-04-03 23:28:55 +02:00
self . ui . listNames . clear ( )
self . s . send ( " /userlist " . encode ( ) )
2015-04-03 14:51:22 +02:00
2015-03-30 14:21:11 +02:00
2015-04-03 14:51:22 +02:00
except timeout :
self . ShowMessageErreur ( " Erreur : Timeout. Le serveur ne repond pas " )
self . ui . txtOutput . setText ( self . message_buffer )
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
2015-03-31 11:52:01 +02:00
def connecter ( self ) :
2015-04-14 22:36:37 +02:00
"""
create a connection , chose name and start thread to update the chat
: return :
"""
ip = self . ui . lineEdit_4 . text ( )
2015-04-03 14:51:22 +02:00
port = int ( self . ui . lineEdit_3 . text ( ) )
2015-04-12 16:21:25 +02:00
self . portCo = port
2015-04-09 22:35:08 +02:00
Addr = ( ip , port )
2015-03-31 11:52:01 +02:00
self . s = socket ( AF_INET , SOCK_STREAM )
self . s . connect ( Addr )
2015-04-14 22:36:37 +02:00
self . thread . setConfig ( self . s , self )
2015-03-31 17:05:45 +02:00
self . ui . lineEdit . setDisabled ( False )
self . ui . pushButton . setDisabled ( False )
self . ui . pushButton_2 . setDisabled ( True )
self . ui . pushButton_3 . setDisabled ( False )
2015-04-03 20:07:29 +02:00
self . ui . lineEdit_4 . setDisabled ( True )
self . ui . lineEdit_3 . setDisabled ( True )
2015-04-02 16:36:24 +02:00
self . thread . start ( )
2015-04-09 22:35:08 +02:00
2015-04-03 14:51:22 +02:00
cmd2 = self . ui . lineEdit_2 . text ( )
if cmd2 != " " :
self . s . settimeout ( 5.0 )
cmdPseudo = " /newname " + cmd2
try :
self . s . send ( cmdPseudo . encode ( ) )
self . pseudo = cmd2
self . ui . pushButton_6 . setDisabled ( False )
2015-04-09 22:35:08 +02:00
2015-04-03 14:51:22 +02:00
except timeout :
self . ShowMessageErreur ( " Erreur : Timeout. Le serveur ne repond pas " )
self . ui . txtOutput . setText ( self . message_buffer )
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
2015-04-02 16:36:24 +02:00
2015-04-02 11:49:43 +02:00
def deco ( self ) :
2015-04-14 22:36:37 +02:00
"""
action on the button to disconnect from dnc
: return :
"""
2015-04-03 14:51:22 +02:00
quitter = " /quit "
self . s . send ( quitter . encode ( ) )
#self.s.close()
2015-04-15 17:23:26 +02:00
2015-04-09 22:35:08 +02:00
2015-04-02 11:49:43 +02:00
def createWidgets ( self ) :
2015-04-14 22:36:37 +02:00
"""
run the main windows , with config
: return :
"""
2015-04-09 00:32:38 +02:00
self . ui = Ui_MainWindow ( )
2015-04-02 11:49:43 +02:00
self . ui . setupUi ( self )
2015-04-09 22:35:08 +02:00
2015-04-03 14:51:22 +02:00
ano = " anonymous " + ' ' . join ( str ( random . randint ( 1 , 9 ) ) for _ in range ( 2 ) )
2015-04-09 22:35:08 +02:00
2015-04-08 15:23:48 +02:00
config = configparser . ConfigParser ( )
config . read ( " dncClient.conf " )
port = config . get ( " NETWORK " , " port " )
2015-04-14 22:36:37 +02:00
ip = config . get ( " NETWORK " , " ip " )
2015-04-08 15:23:48 +02:00
name = config . get ( " PSEUDO " , " name " )
2015-04-14 22:36:37 +02:00
if name is not None :
2015-04-08 15:23:48 +02:00
self . ui . lineEdit_2 . setText ( name )
2015-04-14 22:36:37 +02:00
else :
2015-04-08 15:23:48 +02:00
self . ui . lineEdit_2 . setText ( ano )
2015-04-14 22:36:37 +02:00
if ip is not None :
2015-04-08 15:23:48 +02:00
self . ui . lineEdit_4 . setText ( ip )
2015-04-14 22:36:37 +02:00
if port is not None :
2015-04-08 15:23:48 +02:00
self . ui . lineEdit_3 . setText ( port )
2015-04-02 11:49:43 +02:00
self . ui . lineEdit . setDisabled ( True )
self . ui . pushButton . setDisabled ( True )
self . ui . pushButton_3 . setDisabled ( True )
2015-04-03 14:51:22 +02:00
self . ui . pushButton_6 . setDisabled ( True )
2015-04-02 11:49:43 +02:00
self . message_buffer = " "
2015-04-03 20:07:29 +02:00
self . bouton = " disable "
2015-04-02 11:49:43 +02:00
self . connectActions ( )
2015-03-31 11:52:01 +02:00
def client ( self ) :
2015-04-14 22:36:37 +02:00
"""
send a message to the sever
: return :
"""
2015-04-03 11:18:59 +02:00
self . cmd = self . ui . lineEdit . text ( )
if self . cmd != " " :
2015-03-31 18:49:29 +02:00
self . ui . lineEdit . setText ( ' ' )
2015-04-02 16:36:24 +02:00
self . s . settimeout ( 5.0 )
2015-03-31 18:49:29 +02:00
try :
2015-04-03 11:18:59 +02:00
self . s . send ( self . cmd . encode ( ) )
2015-04-09 22:35:08 +02:00
2015-04-03 12:12:01 +02:00
if self . cmd . split ( " " ) [ 0 ] == " /newname " :
self . pseudo = self . cmd . split ( " " ) [ 1 ]
2015-04-09 22:35:08 +02:00
2015-04-03 12:12:01 +02:00
if self . cmd . split ( " " ) [ 0 ] == " /name " :
self . pseudo = self . cmd . split ( " " ) [ 1 ]
2015-04-09 22:35:08 +02:00
2015-04-14 22:36:37 +02:00
if self . cmd . split ( " " ) [ 0 ] == " /askpm " :
2015-04-06 17:11:47 +02:00
self . demande = self . cmd . split ( " " ) [ 1 ]
2015-04-02 11:49:43 +02:00
2015-03-31 18:49:29 +02:00
except timeout :
2015-04-02 16:36:24 +02:00
self . ShowMessageErreur ( " Erreur : Timeout. Le serveur ne repond pas " )
self . ui . txtOutput . setText ( self . message_buffer )
sb = self . ui . txtOutput . verticalScrollBar ( )
sb . setValue ( sb . maximum ( ) )
2015-03-30 14:21:11 +02:00
if __name__ == " __main__ " :
import sys
app = QtGui . QApplication ( sys . argv )
myapp = start ( )
myapp . show ( )
myapp . focusWidget ( )
2015-04-02 16:36:24 +02:00
for t in threading . enumerate ( ) :
if t != threading . main_thread ( ) : t . join ( )
2015-04-14 22:36:37 +02:00
sys . exit ( app . exec_ ( ) )