Skip to content

Commit

Permalink
Ajout du switch de channel
Browse files Browse the repository at this point in the history
  • Loading branch information
sl4shme committed Oct 16, 2013
1 parent f18ded3 commit 4621410
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
4 changes: 2 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#autocompletion
#verifier le user qui lance sschat
#switch channel
#color
#command url

#verifier le user qui lance sschat
#timer notif
#help dans key
1 change: 1 addition & 0 deletions help.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"/clear : Clear what is displayed on your screen.",
"/timestamp <on|off> : Enable/disable line timestamping.",
"/nickname <newNickname> : Change your nickname.",
"/channel <newChannelName> : Switch channel.",
"/pm <id> <message> : Send a private message to the user with the id <id>",
"/history [on|off|clear] : With no argument, display the last 50 message.",
"/bug <message> : Send a bug / suggestion report.",
Expand Down
13 changes: 9 additions & 4 deletions minion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ def __init__(self, minion):
threading.Thread.__init__(self)
self.minion=minion
self.afkEvent=threading.Event()
self.active = True

def run(self):
while 1:
while self.active:
if self.afkEvent.wait(3600) :
self.afkEvent.clear()
if self.minion.afk == True :
self.minion.afk = False
else:
if self.minion.afk == False :
self.minion.afk = True
return

class SocketManager(threading.Thread):
def __init__(self, minion):
Expand All @@ -24,11 +26,15 @@ def __init__(self, minion):
self.initPeers()
self.sock=socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
self.sock.bind(self.address)
self.active=True

def run(self):
while 1:
while self.active==True:
incomingMessage = self.sock.recvfrom(4096)
self.handlerGetComm(incomingMessage[0])
try:
self.handlerGetComm(incomingMessage[0])
except:
continue

def handlerGetComm(self, incMess):
if incMess[0] == "/":
Expand Down Expand Up @@ -112,4 +118,3 @@ def sendMessage(self, outMessage):
self.myAfk.afkEvent.set()
for peerPid in self.mySocket.peers:
self.sendMessageTo(outMessage, peerPid)

46 changes: 35 additions & 11 deletions sschat.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#!/usr/bin/python
import minion, screen, signal, help, re
import minion, screen, signal, help, re, time, socket, threading

class Sschat:
def __init__(self):
def __init__(self, channel="", nickname=""):
self.screen=screen.Screen()
signal.signal(signal.SIGINT, self.screen.clearInput)
signal.signal(signal.SIGHUP, self.cleanQuit)
signal.signal(signal.SIGWINCH, self.screen.handlerResize)
self.screen.printMessage("Hi, which channel would you like to connect to ?")
channel = self.screen.strictInput()
self.screen.printMessage("What's your nickname ?")
nickname = self.screen.strictInput()
if channel == "":
self.screen.printMessage("Hi, which channel would you like to connect to ?")
channel = self.screen.strictInput()
if nickname == "":
self.screen.printMessage("What's your nickname ?")
nickname = self.screen.strictInput()
self.minion=minion.Minion(channel, self.screen, nickname)
self.screen.clearConvers()
self.screen.setTitle(channel, len(self.minion.mySocket.peers))
Expand All @@ -24,7 +26,23 @@ def main(self):
self.minion.sendMessage("/msg "+chatMessage)
self.screen.printMessage(chatMessage)
else:
self.command(chatMessage[1:])
newChannel = self.command(chatMessage[1:])
if newChannel:
self.channelSwitch()
return (newChannel, self.minion.nickname)

def channelSwitch(self):
message= "/rem "+self.minion.pid+"|"+self.minion.nickname+"|ChannelSwitch"
self.minion.sendMessage(message)
self.screen.stopScreen()
self.minion.mySocket.active=False
self.minion.mySocket.sock.shutdown(socket.SHUT_RDWR)
self.minion.mySocket.sock.close()
self.screen.stopNotif()
self.minion.myAfk.active=False
self.minion.myAfk.afkEvent.set()
while threading.activeCount() != 1:
time.sleep(0.2)

def cleanQuit(self, signum="", frame="", reason=""):
try:
Expand Down Expand Up @@ -70,6 +88,12 @@ def command(self, mess):
args = mess.split(" ")[1:]
if cmd == "clear":
self.screen.clearConvers()
elif cmd == "channel":
channel = args[0]
if re.match("^[A-Za-z]*$", channel) and len(channel) <= 12:
return channel
else:
self.screen.printMessage("Bad channel name.")
elif cmd == "help":
self.screen.scrollPrinter(help.help)
elif cmd == "list":
Expand Down Expand Up @@ -97,9 +121,6 @@ def command(self, mess):
self.screen.printMessage(chatMessage)
self.minion.afk = True
else :
chatMessage = self.minion.nickname+" is back."
self.minion.sendMessage("/msg "+chatMessage)
self.screen.printMessage(chatMessage)
self.minion.afk = False
elif cmd == "nickname":
nick = args[0]
Expand Down Expand Up @@ -135,4 +156,7 @@ def command(self, mess):
self.screen.printMessage("/"+mess)

chat=Sschat()
chat.main()
while 1 :
newChannel, nickname = chat.main()
chat=None
chat=Sschat(newChannel, nickname)

0 comments on commit 4621410

Please sign in to comment.