2012-04-12 01:06:17 +00:00
# coding=utf-8
import http . client
import time
import re
2012-05-03 14:16:38 +00:00
import _thread
2012-05-14 15:50:11 +00:00
import threading
2012-04-12 01:06:17 +00:00
from datetime import date
from datetime import datetime
2012-05-30 10:28:46 +00:00
from module_state import ModuleState
2012-06-29 17:20:29 +00:00
from . SiteSoutenances import SiteSoutenances
from . Delayed import Delayed
2012-04-12 01:06:17 +00:00
2012-06-29 17:20:29 +00:00
nemubotversion = 3.0
2012-04-12 01:06:17 +00:00
2012-05-14 15:50:11 +00:00
DELAYED = dict ( )
2012-06-29 17:20:29 +00:00
def help_tiny ( ) :
2012-04-12 01:06:17 +00:00
""" Line inserted in the response to the command !help """
2012-04-18 21:35:58 +00:00
return " EPITA ING1 defenses module "
2012-04-12 01:06:17 +00:00
2012-06-29 17:20:29 +00:00
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/ "
2012-04-12 01:06:17 +00:00
datas = None
2012-05-03 14:16:38 +00:00
THREAD = None
wait = list ( )
2012-04-12 01:06:17 +00:00
2012-06-29 17:20:29 +00:00
def parseanswer ( msg ) :
2012-05-03 14:16:38 +00:00
global THREAD , wait
2012-04-12 01:06:17 +00:00
if msg . cmd [ 0 ] == " soutenance " or msg . cmd [ 0 ] == " soutenances " :
2012-05-03 14:16:38 +00:00
if THREAD is None :
THREAD = _thread . start_new_thread ( startSoutenance , ( msg , ) )
2012-04-12 01:06:17 +00:00
else :
2012-05-03 14:16:38 +00:00
wait . append ( msg )
return True
return False
2012-04-12 01:06:17 +00:00
2012-06-29 17:20:29 +00:00
def parseask ( msg ) :
if len ( DELAYED ) > 0 and msg . sender == msg . srv . partner :
treat = False
for part in msg . content . split ( ' ; ' ) :
if part is None :
continue
for d in DELAYED . keys ( ) :
if DELAYED [ d ] . res is None and part . find ( DELAYED [ d ] . name ) > = 0 :
result = re . match ( " .* est (.*[^.]) \ .? " , part )
if result is not None :
DELAYED [ d ] . res = result . group ( 1 )
DELAYED [ d ] . evt . set ( )
return treat
return False
2012-05-03 14:16:38 +00:00
def startSoutenance ( msg ) :
global datas , THREAD , wait
#Starts by updating datas
if datas is not None :
datas = datas . update ( )
if datas is None :
datas = SiteSoutenances ( getPage ( ) . decode ( ) )
if len ( msg . cmd ) == 1 or msg . cmd [ 1 ] == " next " :
soutenance = datas . findLast ( )
if soutenance is None :
msg . send_chn ( " Il ne semble pas y avoir de soutenance pour le moment. " )
else :
if soutenance . start > soutenance . hour :
avre = " %s de *retard* " % msg . just_countdown ( soutenance . start - soutenance . hour , 4 )
else :
avre = " %s *d ' avance* " % msg . just_countdown ( soutenance . hour - soutenance . start , 4 )
msg . send_chn ( " Actuellement à la soutenance numéro %d , commencée il y a %s avec %s . " % ( soutenance . rank , msg . just_countdown ( datetime . now ( ) - soutenance . start , 4 ) , avre ) )
elif msg . cmd [ 1 ] == " assistants " or msg . cmd [ 1 ] == " assistant " or msg . cmd [ 1 ] == " yaka " or msg . cmd [ 1 ] == " yakas " or msg . cmd [ 1 ] == " acu " or msg . cmd [ 1 ] == " acus " :
assistants = datas . findAssistants ( )
if len ( assistants ) > 0 :
msg . send_chn ( " Les %d assistants faisant passer les soutenances sont : %s . " % ( len ( assistants ) , ' , ' . join ( assistants . keys ( ) ) ) )
else :
msg . send_chn ( " Il ne semble pas y avoir de soutenance pour le moment. " )
else :
name = msg . cmd [ 1 ]
if msg . cmd [ 0 ] == " soutenance " :
soutenance = datas . findClose ( name )
2012-05-14 15:50:11 +00:00
if soutenance is None :
global DELAYED
DELAYED [ msg ] = Delayed ( name )
msg . srv . send_msg_prtn ( " ~whois %s " % name )
DELAYED [ msg ] . wait ( 5 )
if DELAYED [ msg ] . res is not None :
name = DELAYED [ msg ] . res
soutenance = datas . findClose ( name )
2012-05-03 14:16:38 +00:00
if soutenance is None :
msg . send_chn ( " Pas d ' horaire de soutenance pour %s . " % name )
else :
if soutenance . state == " En cours " :
msg . send_chn ( " %s est actuellement en soutenance avec %s . Elle était prévue à %s , position %d . " % ( name , soutenance . assistant , soutenance . hour , soutenance . rank ) )
elif soutenance . state == " Effectue " :
msg . send_chn ( " %s a passé sa soutenance avec %s . Elle a duré %s . " % ( name , soutenance . assistant , msg . just_countdown ( soutenance . end - soutenance . start , 4 ) ) )
elif soutenance . state == " Retard " :
msg . send_chn ( " %s était en retard à sa soutenance de %s . " % ( name , soutenance . hour ) )
2012-04-12 01:06:17 +00:00
else :
2012-05-03 14:16:38 +00:00
last = datas . findLast ( )
if last is not None :
if soutenance . hour + ( last . start - last . hour ) > datetime . now ( ) :
msg . send_chn ( " Soutenance de %s : %s , position %d ; estimation du passage : dans %s . " % ( name , soutenance . hour , soutenance . rank , msg . just_countdown ( ( soutenance . hour - datetime . now ( ) ) + ( last . start - last . hour ) ) ) )
2012-04-12 01:06:17 +00:00
else :
2012-05-03 14:16:38 +00:00
msg . send_chn ( " Soutenance de %s : %s , position %d ; passage imminent. " % ( name , soutenance . hour , soutenance . rank ) )
else :
msg . send_chn ( " Soutenance de %s : %s , position %d . " % ( name , soutenance . hour , soutenance . rank ) )
elif msg . cmd [ 0 ] == " soutenances " :
souts = datas . findAll ( name )
if souts is None :
msg . send_snd ( " Pas de soutenance prévues pour %s . " % name )
else :
first = True
for s in souts :
if first :
msg . send_snd ( " Soutenance(s) de %s : - %s (position %d ) ; " % ( name , s . hour , s . rank ) )
first = False
else :
msg . send_snd ( " %s - %s (position %d ) ; " % ( len ( name ) * ' ' , s . hour , s . rank ) )
THREAD = None
if len ( wait ) > 0 :
startSoutenance ( wait . pop ( ) )
2012-04-12 01:06:17 +00:00
2012-06-29 17:20:29 +00:00
def getPage ( ) :
conn = http . client . HTTPConnection ( CONF . getNode ( " server " ) [ " ip " ] )
try :
conn . request ( " GET " , CONF . getNode ( " server " ) [ " url " ] )
res = conn . getresponse ( )
data = res . read ( )
except :
print ( " [ %s ] impossible de récupérer la page %s . " % ( s , p ) )
return " "
conn . close ( )
return data