2012-05-30 09:58:27 +00:00
# coding=utf-8
import re
import sys
from datetime import datetime
from datetime import date
2012-08-23 01:09:42 +00:00
from xmlparser . node import ModuleState
2012-05-30 09:58:27 +00:00
2012-08-23 01:09:42 +00:00
nemubotversion = 3.2
2012-05-30 09:58:27 +00:00
2012-08-23 01:09:42 +00:00
def load ( context ) :
2012-05-30 09:58:27 +00:00
global DATAS
DATAS . setIndex ( " name " , " birthday " )
def help_tiny ( ) :
""" Line inserted in the response to the command !help """
return " People birthdays and ages "
def help_full ( ) :
return " !anniv /who/: gives the remaining time before the anniversary of /who/ \n !age /who/: gives the age of /who/ \n If /who/ is not given, gives the remaining time before your anniversary. \n \n To set yout birthday, say it to nemubot :) "
2012-07-23 10:07:26 +00:00
def findName ( msg ) :
2012-05-30 09:58:27 +00:00
if len ( msg . cmd ) < 2 or msg . cmd [ 1 ] . lower ( ) == " moi " or msg . cmd [ 1 ] . lower ( ) == " me " :
2012-07-23 10:07:26 +00:00
name = msg . nick . lower ( )
2012-05-30 09:58:27 +00:00
else :
name = msg . cmd [ 1 ] . lower ( )
2012-07-23 10:07:26 +00:00
2012-05-30 09:58:27 +00:00
matches = [ ]
if name in DATAS . index :
matches . append ( name )
else :
for k in DATAS . index . keys ( ) :
if k . find ( name ) == 0 :
matches . append ( k )
return ( matches , name )
2012-07-23 10:07:26 +00:00
2012-05-30 09:58:27 +00:00
2012-08-23 01:09:42 +00:00
def cmd_anniv ( msg ) :
2012-05-30 09:58:27 +00:00
( matches , name ) = findName ( msg )
if len ( matches ) == 1 :
2012-08-23 01:09:42 +00:00
name = matches [ 0 ]
tyd = DATAS . index [ name ] . getDate ( " born " )
tyd = datetime ( date . today ( ) . year , tyd . month , tyd . day )
2012-05-30 09:58:27 +00:00
2012-08-30 23:54:57 +00:00
if ( tyd . day == datetime . today ( ) . day and
tyd . month == datetime . today ( ) . month ) :
return Response ( msg . sender , msg . countdown_format (
DATAS . index [ name ] . getDate ( " born " ) , " " ,
" C ' est aujourd ' hui l ' anniversaire de %s ! "
" Il a %s . Joyeux anniversaire :) " % ( name , " %s " ) ) ,
msg . channel )
2012-08-23 01:09:42 +00:00
else :
if tyd < datetime . today ( ) :
tyd = datetime ( date . today ( ) . year + 1 , tyd . month , tyd . day )
2012-05-30 09:58:27 +00:00
2012-08-30 23:54:57 +00:00
return Response ( msg . sender , msg . countdown_format ( tyd ,
" Il reste %s avant l ' anniversaire de %s ! " % ( " %s " ,
name ) , " " ) ,
msg . channel )
2012-05-30 09:58:27 +00:00
else :
2012-08-30 23:54:57 +00:00
return Response ( msg . sender , " désolé, je ne connais pas la date d ' anniversaire "
" de %s . Quand est-il né ? " % name ,
msg . channel , msg . nick )
2012-08-23 01:09:42 +00:00
def cmd_age ( msg ) :
2012-05-30 09:58:27 +00:00
( matches , name ) = findName ( msg )
if len ( matches ) == 1 :
2012-08-23 01:09:42 +00:00
name = matches [ 0 ]
d = DATAS . index [ name ] . getDate ( " born " )
2012-05-30 09:58:27 +00:00
2012-08-30 23:54:57 +00:00
return Response ( msg . sender , msg . countdown_format ( d ,
" %s va naître dans %s . " % ( name , " %s " ) ,
" %s a %s . " % ( name , " %s " ) ) ,
msg . channel )
2012-05-30 09:58:27 +00:00
else :
2012-08-30 23:54:57 +00:00
return Response ( msg . sender , " désolé, je ne connais pas l ' âge de %s . "
" Quand est-il né ? " % name , msg . channel , msg . nick )
2012-05-30 09:58:27 +00:00
return True
def parseask ( msg ) :
msgl = msg . content . lower ( )
if re . match ( " ^.*(date de naissance|birthday|geburtstag|née? |nee? le|born on).*$ " , msgl ) is not None :
try :
2012-08-30 23:54:57 +00:00
extDate = msg . extractDate ( )
if extDate is None or extDate . year > datetime . now ( ) . year :
return Response ( msg . sender ,
" ta date de naissance ne paraît pas valide... " ,
msg . channel ,
msg . nick )
2012-05-30 09:58:27 +00:00
else :
2012-07-23 10:07:26 +00:00
if msg . nick . lower ( ) in DATAS . index :
2012-08-23 01:09:42 +00:00
DATAS . index [ msg . nick . lower ( ) ] [ " born " ] = extDate
2012-05-30 09:58:27 +00:00
else :
ms = ModuleState ( " birthday " )
2012-07-23 10:07:26 +00:00
ms . setAttribute ( " name " , msg . nick . lower ( ) )
2012-05-30 09:58:27 +00:00
ms . setAttribute ( " born " , extDate )
DATAS . addChild ( ms )
save ( )
2012-08-30 23:54:57 +00:00
return Response ( msg . sender ,
" ok, c ' est noté, ta date de naissance est le %s "
% extDate . strftime ( " % A %d % B % Y à % H: % M " ) ,
msg . channel ,
msg . nick )
2012-05-30 09:58:27 +00:00
except :
2012-08-30 23:54:57 +00:00
return Response ( msg . sender , " ta date de naissance ne paraît pas valide... " ,
msg . channel , msg . nick )