Add thread when doing long tasks
This commit is contained in:
parent
f676ce11c6
commit
69c5d06ed7
@ -62,8 +62,8 @@ class Credits:
|
|||||||
now = datetime.now() + self.randsec
|
now = datetime.now() + self.randsec
|
||||||
if self.lastmessage.minute == now.minute and (self.lastmessage.second == now.second or self.lastmessage.second == now.second - 1):
|
if self.lastmessage.minute == now.minute and (self.lastmessage.second == now.second or self.lastmessage.second == now.second - 1):
|
||||||
print("AUTOBAN %s: too low time between messages" % self.name)
|
print("AUTOBAN %s: too low time between messages" % self.name)
|
||||||
BANLIST.append(self.name)
|
#BANLIST.append(self.name)
|
||||||
#self.credits -= self.credits / 2 #Une alternative
|
self.credits -= self.credits / 2 #Une alternative
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.iask = True
|
self.iask = True
|
||||||
@ -169,6 +169,10 @@ class Message:
|
|||||||
self.cmd = "UNKNOWN"
|
self.cmd = "UNKNOWN"
|
||||||
print (line)
|
print (line)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_owner(self):
|
||||||
|
return self.sender == self.srv.owner
|
||||||
|
|
||||||
|
|
||||||
def send_msg (self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
|
def send_msg (self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
|
||||||
if CREDITS[self.realname].speak():
|
if CREDITS[self.realname].speak():
|
||||||
|
32
nemuspeak.py
32
nemuspeak.py
@ -164,6 +164,7 @@ class Server:
|
|||||||
return msg.sender != OWNER and (msg.channel == OWNER or msg.channel in self.channels)
|
return msg.sender != OWNER and (msg.channel == OWNER or msg.channel in self.channels)
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
|
global stopSpk, talkEC, g_queue
|
||||||
readbuffer = "" #Here we store all the messages from server
|
readbuffer = "" #Here we store all the messages from server
|
||||||
while 1:
|
while 1:
|
||||||
try:
|
try:
|
||||||
@ -178,7 +179,36 @@ class Server:
|
|||||||
msg = message.Message(self, line)
|
msg = message.Message(self, line)
|
||||||
if msg.cmd == "PING":
|
if msg.cmd == "PING":
|
||||||
self.s.send(("PONG %s\r\n" % msg.content).encode ())
|
self.s.send(("PONG %s\r\n" % msg.content).encode ())
|
||||||
elif msg.cmd == "PRIVMSG" and self.authorize(msg):
|
elif msg.cmd == "PRIVMSG" and (self.authorize(msg) or msg.content[0] == '`'):
|
||||||
|
if msg.content[0] == '`' and msg.sender == OWNER:
|
||||||
|
cmd = msg.content[1:].split(' ')
|
||||||
|
if cmd[0] == "speak":
|
||||||
|
_thread.start_new_thread(speak, (0,))
|
||||||
|
elif cmd[0] == 'reset':
|
||||||
|
while len(g_queue) > 0:
|
||||||
|
g_queue.pop()
|
||||||
|
elif cmd[0] == 'save':
|
||||||
|
if talkEC == 0:
|
||||||
|
talkEC = 1
|
||||||
|
stopSpk = 1
|
||||||
|
elif cmd[0] == 'test':
|
||||||
|
g_queue.append(message.Message(self, ":Quelqun!someone@p0m.fr PRIVMSG %s :Ceci est un message de test ;)"%(self.channels)))
|
||||||
|
elif cmd[0] == 'list':
|
||||||
|
print ("Currently listened channels:")
|
||||||
|
for chan in self.channels:
|
||||||
|
print (chan)
|
||||||
|
print ("-- ")
|
||||||
|
elif cmd[0] == 'add' and len(cmd) > 1:
|
||||||
|
self.channels.append(cmd[1])
|
||||||
|
print (cmd[1] + " added to listened channels")
|
||||||
|
elif cmd[0] == 'del' and len(cmd) > 1:
|
||||||
|
if self.channels.count(cmd[1]) > 0:
|
||||||
|
self.channels.remove(cmd[1])
|
||||||
|
print (cmd[1] + " removed from listened channels")
|
||||||
|
else:
|
||||||
|
print (cmd[1] + " not in listened channels")
|
||||||
|
|
||||||
|
else:
|
||||||
g_queue.append(msg)
|
g_queue.append(msg)
|
||||||
if talkEC == 0:
|
if talkEC == 0:
|
||||||
_thread.start_new_thread(speak, (0,))
|
_thread.start_new_thread(speak, (0,))
|
||||||
|
28
qd.py
28
qd.py
@ -41,6 +41,16 @@ class Score:
|
|||||||
self.great = int(item.getAttribute("great"))
|
self.great = int(item.getAttribute("great"))
|
||||||
self.bad = int(item.getAttribute("bad"))
|
self.bad = int(item.getAttribute("bad"))
|
||||||
|
|
||||||
|
def merge(self, other):
|
||||||
|
self.ftt += other.ftt
|
||||||
|
self.twt += other.twt
|
||||||
|
self.pi += other.pi
|
||||||
|
self.notfound += other.notfound
|
||||||
|
self.tententen += other.tententen
|
||||||
|
self.leet += other.leet
|
||||||
|
self.great += other.great
|
||||||
|
self.bad += other.bad
|
||||||
|
|
||||||
def newWinner(self):
|
def newWinner(self):
|
||||||
self.ftt = 0
|
self.ftt = 0
|
||||||
self.twt = 0
|
self.twt = 0
|
||||||
@ -164,7 +174,17 @@ def rev (tupl):
|
|||||||
def parseanswer (msg):
|
def parseanswer (msg):
|
||||||
if msg.cmd[0] == "42" or msg.cmd[0] == "score" or msg.cmd[0] == "scores":
|
if msg.cmd[0] == "42" or msg.cmd[0] == "score" or msg.cmd[0] == "scores":
|
||||||
global SCORES, MANCHE
|
global SCORES, MANCHE
|
||||||
if len(msg.cmd) > 1 and (msg.cmd[1] == "help" or msg.cmd[1] == "aide"):
|
if len(msg.cmd) > 3 and msg.is_owner and (msg.cmd[1] == "merge"):
|
||||||
|
if msg.cmd[2] in SCORES and msg.cmd[3] in SCORES:
|
||||||
|
SCORES[msg.cmd[2]].merge (SCORES[msg.cmd[3]])
|
||||||
|
del SCORES[msg.cmd[3]]
|
||||||
|
msg.send_chn ("%s a été correctement fusionné avec %s."%(msg.cmd[3], msg.cmd[2]))
|
||||||
|
else:
|
||||||
|
if msg.cmd[2] not in SCORES:
|
||||||
|
msg.send_chn ("%s n'est pas un joueur connu."%msg.cmd[2])
|
||||||
|
elif msg.cmd[3] not in SCORES:
|
||||||
|
msg.send_chn ("%s n'est pas un joueur connu."%msg.cmd[3])
|
||||||
|
elif len(msg.cmd) > 1 and (msg.cmd[1] == "help" or msg.cmd[1] == "aide"):
|
||||||
msg.send_chn ("Formule : normal * 2 - bad * 10 + great * 5 + leet * 3 + pi * 3.1415 + dt + not_found * 4.04")
|
msg.send_chn ("Formule : normal * 2 - bad * 10 + great * 5 + leet * 3 + pi * 3.1415 + dt + not_found * 4.04")
|
||||||
elif len(msg.cmd) > 1 and (msg.cmd[1] == "manche" or msg.cmd[1] == "round"):
|
elif len(msg.cmd) > 1 and (msg.cmd[1] == "manche" or msg.cmd[1] == "round"):
|
||||||
msg.send_chn ("Nous sommes dans la %de manche, gagnée par %s avec %d points et commencée par %s le %s"%MANCHE)
|
msg.send_chn ("Nous sommes dans la %de manche, gagnée par %s avec %d points et commencée par %s le %s"%MANCHE)
|
||||||
@ -173,8 +193,8 @@ def parseanswer (msg):
|
|||||||
phrase = ""
|
phrase = ""
|
||||||
|
|
||||||
if len(msg.cmd) > 1:
|
if len(msg.cmd) > 1:
|
||||||
if msg.cmd[1].lower() in SCORES:
|
if msg.cmd[1] in SCORES:
|
||||||
phrase += " " + msg.cmd[1] + ": " + SCORES[msg.cmd[1].lower()].details()
|
phrase += " " + msg.cmd[1] + ": " + SCORES[msg.cmd[1]].details()
|
||||||
else:
|
else:
|
||||||
phrase = " %s n'a encore jamais joué,"%(msg.cmd[1])
|
phrase = " %s n'a encore jamais joué,"%(msg.cmd[1])
|
||||||
else:
|
else:
|
||||||
@ -194,7 +214,7 @@ def parseanswer (msg):
|
|||||||
|
|
||||||
def win(msg):
|
def win(msg):
|
||||||
global SCORES, MANCHE
|
global SCORES, MANCHE
|
||||||
who = msg.sender.lower()
|
who = msg.sender
|
||||||
|
|
||||||
(num_manche, winner, nb_points, whosef, dayte) = MANCHE
|
(num_manche, winner, nb_points, whosef, dayte) = MANCHE
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
import http.client
|
import http.client
|
||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
|
import _thread
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
@ -124,10 +125,23 @@ def help_full ():
|
|||||||
return "!soutenance: gives information about current defenses state\n!soutenance /who/: gives the date of the next defense of /who/.\n!soutenances /who/: gives all defense dates of /who/"
|
return "!soutenance: gives information about current defenses state\n!soutenance /who/: gives the date of the next defense of /who/.\n!soutenances /who/: gives all defense dates of /who/"
|
||||||
|
|
||||||
datas = None
|
datas = None
|
||||||
|
THREAD = None
|
||||||
|
wait = list()
|
||||||
|
|
||||||
def parseanswer (msg):
|
def parseanswer (msg):
|
||||||
global datas
|
global THREAD, wait
|
||||||
if msg.cmd[0] == "soutenance" or msg.cmd[0] == "soutenances":
|
if msg.cmd[0] == "soutenance" or msg.cmd[0] == "soutenances":
|
||||||
|
if THREAD is None:
|
||||||
|
THREAD = _thread.start_new_thread (startSoutenance, (msg,))
|
||||||
|
else:
|
||||||
|
wait.append(msg)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def startSoutenance (msg):
|
||||||
|
global datas, THREAD, wait
|
||||||
|
|
||||||
#Starts by updating datas
|
#Starts by updating datas
|
||||||
if datas is not None:
|
if datas is not None:
|
||||||
datas = datas.update ()
|
datas = datas.update ()
|
||||||
@ -187,9 +201,10 @@ def parseanswer (msg):
|
|||||||
first = False
|
first = False
|
||||||
else:
|
else:
|
||||||
msg.send_snd (" %s - %s (position %d) ;"%(len(name)*' ', s.hour, s.rank))
|
msg.send_snd (" %s - %s (position %d) ;"%(len(name)*' ', s.hour, s.rank))
|
||||||
|
THREAD = None
|
||||||
|
if len(wait) > 0:
|
||||||
|
startSoutenance(wait.pop())
|
||||||
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def parseask (msg):
|
def parseask (msg):
|
||||||
return False
|
return False
|
||||||
|
56
whereis.py
56
whereis.py
@ -4,6 +4,7 @@ import re
|
|||||||
import sys
|
import sys
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
import _thread
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
@ -15,10 +16,14 @@ from xml.dom.minidom import getDOMImplementation
|
|||||||
NS_SERVER = 'ns-server.epita.fr'
|
NS_SERVER = 'ns-server.epita.fr'
|
||||||
NS_PORT = 4242
|
NS_PORT = 4242
|
||||||
|
|
||||||
|
THREAD = None
|
||||||
|
search = list()
|
||||||
|
|
||||||
class UpdatedStorage:
|
class UpdatedStorage:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
sock = connect_to_ns(NS_SERVER, NS_PORT)
|
sock = connect_to_ns(NS_SERVER, NS_PORT)
|
||||||
self.users = dict()
|
self.users = dict()
|
||||||
|
if sock != None:
|
||||||
for l in list_users(sock):
|
for l in list_users(sock):
|
||||||
u = User(l)
|
u = User(l)
|
||||||
self.users[u.login] = u
|
self.users[u.login] = u
|
||||||
@ -52,6 +57,8 @@ class User(object):
|
|||||||
return 'en midlab'
|
return 'en midlab'
|
||||||
elif self.ip.startswith('10.250'):
|
elif self.ip.startswith('10.250'):
|
||||||
return 'en cisco'
|
return 'en cisco'
|
||||||
|
elif self.ip.startswith('10.41'):
|
||||||
|
return 'sur le wifi'
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -62,6 +69,9 @@ class User(object):
|
|||||||
return 'quelque part sur le PIE (%s)'%self.ip
|
return 'quelque part sur le PIE (%s)'%self.ip
|
||||||
else:
|
else:
|
||||||
return "chez lui"
|
return "chez lui"
|
||||||
|
else:
|
||||||
|
if self.ip.startswith('10.200'):
|
||||||
|
return self.sm + " poste " + self.ip.split('.')[3]
|
||||||
else:
|
else:
|
||||||
return self.sm + " rangée " + self.ip.split('.')[2] + " poste " + self.ip.split('.')[3]
|
return self.sm + " rangée " + self.ip.split('.')[2] + " poste " + self.ip.split('.')[3]
|
||||||
|
|
||||||
@ -72,8 +82,12 @@ class User(object):
|
|||||||
return hash(self.login)
|
return hash(self.login)
|
||||||
|
|
||||||
def connect_to_ns(server, port):
|
def connect_to_ns(server, port):
|
||||||
|
try:
|
||||||
s = socket.socket()
|
s = socket.socket()
|
||||||
|
s.settimeout(1)
|
||||||
s.connect((server, port))
|
s.connect((server, port))
|
||||||
|
except socket.error:
|
||||||
|
return None
|
||||||
s.recv(8192) # salut ...
|
s.recv(8192) # salut ...
|
||||||
return s
|
return s
|
||||||
|
|
||||||
@ -94,6 +108,8 @@ def load_module(datas_path):
|
|||||||
|
|
||||||
def save_module():
|
def save_module():
|
||||||
"""Save the module state"""
|
"""Save the module state"""
|
||||||
|
if THREAD is not None:
|
||||||
|
THREAD.exit()
|
||||||
return
|
return
|
||||||
|
|
||||||
def help_tiny ():
|
def help_tiny ():
|
||||||
@ -105,28 +121,29 @@ def help_full ():
|
|||||||
|
|
||||||
datas = None
|
datas = None
|
||||||
|
|
||||||
def parseanswer (msg):
|
def startWhereis(msg):
|
||||||
global datas
|
global datas, THREAD, search
|
||||||
if msg.cmd[0] == "whereis" or msg.cmd[0] == "whereare" or msg.cmd[0] == "ouest" or msg.cmd[0] == "ousont":
|
|
||||||
if datas is not None:
|
if datas is not None:
|
||||||
datas = datas.update ()
|
datas = datas.update ()
|
||||||
if datas is None:
|
if datas is None:
|
||||||
datas = UpdatedStorage()
|
datas = UpdatedStorage()
|
||||||
|
if datas is None:
|
||||||
if len(msg.cmd) > 10:
|
msg.send_chn("Hmm c'est embarassant, serait-ce la fin du monde ou juste netsoul qui est mort ?")
|
||||||
msg.send_snd ("Demande moi moins de personnes à la fois dans ton !%s" % msg.cmd[0])
|
return
|
||||||
return True
|
|
||||||
|
|
||||||
pasla = list()
|
pasla = list()
|
||||||
|
|
||||||
for name in msg.cmd:
|
for name in msg.cmd:
|
||||||
if name == "whereis" or name == "whereare" or name == "ouest" or name == "ousont":
|
if name == "whereis" or name == "whereare" or name == "ouest" or name == "ousont" or name == "ip":
|
||||||
if len(msg.cmd) >= 2:
|
if len(msg.cmd) >= 2:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
name = msg.sender
|
name = msg.sender
|
||||||
|
|
||||||
if name in datas.users:
|
if name in datas.users:
|
||||||
|
if msg.cmd[0] == "ip":
|
||||||
|
msg.send_chn ("L'ip de %s est %s." %(name, datas.users[name].ip))
|
||||||
|
else:
|
||||||
msg.send_chn ("%s est %s (%s)." %(name, datas.users[name].poste, unquote(datas.users[name].location)))
|
msg.send_chn ("%s est %s (%s)." %(name, datas.users[name].poste, unquote(datas.users[name].location)))
|
||||||
else:
|
else:
|
||||||
pasla.append(name)
|
pasla.append(name)
|
||||||
@ -136,17 +153,22 @@ def parseanswer (msg):
|
|||||||
elif len(pasla) > 1:
|
elif len(pasla) > 1:
|
||||||
msg.send_chn ("%s ne sont pas connectés sur le PIE." % ", ".join(pasla))
|
msg.send_chn ("%s ne sont pas connectés sur le PIE." % ", ".join(pasla))
|
||||||
|
|
||||||
return True
|
THREAD = None
|
||||||
elif msg.cmd[0] == "ip":
|
if len(search) > 0:
|
||||||
if len(msg.cmd) >= 2:
|
startWhereis(search.pop())
|
||||||
name = msg.cmd[1]
|
|
||||||
else:
|
|
||||||
name = msg.sender
|
|
||||||
|
|
||||||
if name in datas.users:
|
|
||||||
msg.send_chn ("L'ip de %s est %s." %(name, datas.users[name].ip))
|
def parseanswer (msg):
|
||||||
|
global datas, THREAD, search
|
||||||
|
if msg.cmd[0] == "whereis" or msg.cmd[0] == "whereare" or msg.cmd[0] == "ouest" or msg.cmd[0] == "ousont" or msg.cmd[0] == "ip":
|
||||||
|
if len(msg.cmd) > 10:
|
||||||
|
msg.send_snd ("Demande moi moins de personnes à la fois dans ton !%s" % msg.cmd[0])
|
||||||
|
return True
|
||||||
|
|
||||||
|
if THREAD is None:
|
||||||
|
THREAD = _thread.start_new_thread (startWhereis, (msg,))
|
||||||
else:
|
else:
|
||||||
msg.send_chn ("%s n'est pas connecté à netsoul." % name)
|
search.append(msg)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user