2015-05-26 05:26:13 +00:00
# coding=utf-8
import re
from nemubot import context
2015-10-30 20:57:45 +00:00
from nemubot . exception import IMException
2015-05-26 05:26:13 +00:00
from nemubot . hooks import hook
from nemubot . tools . xmlparser . node import ModuleState
nemubotversion = 3.4
from more import Response
2015-05-28 05:18:07 +00:00
from networking . page import headers
2015-05-26 05:26:13 +00:00
PASSWD_FILE = None
def load ( context ) :
global PASSWD_FILE
2015-10-27 17:03:28 +00:00
if not context . config or " passwd " not in context . config :
2015-05-26 05:26:13 +00:00
print ( " No passwd file given " )
return None
PASSWD_FILE = context . config [ " passwd " ]
if not context . data . hasNode ( " aliases " ) :
context . data . addChild ( ModuleState ( " aliases " ) )
context . data . getNode ( " aliases " ) . setIndex ( " from " , " alias " )
if not context . data . hasNode ( " pics " ) :
context . data . addChild ( ModuleState ( " pics " ) )
context . data . getNode ( " pics " ) . setIndex ( " login " , " pict " )
import nemubot . hooks
2017-07-05 20:54:07 +00:00
context . add_hook ( nemubot . hooks . Command ( cmd_whois , " whois " , keywords = { " lookup " : " Perform a lookup of the begining of the login instead of an exact search. " } ) ,
2015-11-16 06:19:09 +00:00
" in " , " Command " )
2015-05-26 05:26:13 +00:00
class Login :
def __init__ ( self , line ) :
s = line . split ( " : " )
self . login = s [ 0 ]
self . uid = s [ 2 ]
self . gid = s [ 3 ]
self . cn = s [ 4 ]
self . home = s [ 5 ]
def get_promo ( self ) :
return self . home . split ( " / " ) [ 2 ] . replace ( " _ " , " " )
def get_photo ( self ) :
if self . login in context . data . getNode ( " pics " ) . index :
return context . data . getNode ( " pics " ) . index [ self . login ] [ " url " ]
2016-09-13 17:16:37 +00:00
for url in [ " https://photos.cri.epita.net/ %s " , " https://static.acu.epita.fr/photos/ %s " , " https://static.acu.epita.fr/photos/ %s / %% s " % self . gid , " https://intra-bocal.epitech.eu/trombi/ %s .jpg " , " http://whois.23.tf/p/ %s / %% s.jpg " % self . gid ] :
2015-05-28 05:18:07 +00:00
url = url % self . login
2015-09-24 04:09:59 +00:00
try :
_ , status , _ , _ = headers ( url )
if status == 200 :
return url
except :
logger . exception ( " On URL %s " , url )
2015-05-28 05:18:07 +00:00
return None
2015-05-26 05:26:13 +00:00
2017-07-05 20:54:07 +00:00
def found_login ( login , search = False ) :
2015-05-26 05:26:13 +00:00
if login in context . data . getNode ( " aliases " ) . index :
login = context . data . getNode ( " aliases " ) . index [ login ] [ " to " ]
2017-07-05 20:54:07 +00:00
login_ = login + ( " : " if not search else " " )
2015-05-26 05:26:13 +00:00
lsize = len ( login_ )
with open ( PASSWD_FILE , encoding = " iso-8859-15 " ) as f :
for l in f . readlines ( ) :
if l [ : lsize ] == login_ :
2017-07-05 20:54:07 +00:00
yield Login ( l . strip ( ) )
2015-05-26 05:26:13 +00:00
def cmd_whois ( msg ) :
if len ( msg . args ) < 1 :
2015-10-30 20:57:45 +00:00
raise IMException ( " Provide a name " )
2015-05-26 05:26:13 +00:00
2017-07-05 20:54:07 +00:00
def format_response ( t ) :
srch , l = t
if type ( l ) is Login :
2015-05-26 05:26:13 +00:00
pic = l . get_photo ( )
2017-07-05 20:54:07 +00:00
return " %s is %s ( %s %s ): %s %s " % ( srch , l . cn . capitalize ( ) , l . login , l . uid , l . get_promo ( ) , " and looks like %s " % pic if pic is not None else " " )
2015-05-26 05:26:13 +00:00
else :
2017-07-05 20:54:07 +00:00
return l % srch
res = Response ( channel = msg . channel , count = " ( %d more logins) " , line_treat = format_response )
for srch in msg . args :
found = False
for l in found_login ( srch , " lookup " in msg . kwargs ) :
found = True
res . append_message ( ( srch , l ) )
if not found :
res . append_message ( ( srch , " Unknown %s :( " ) )
2015-05-26 05:26:13 +00:00
return res
2015-11-02 19:19:12 +00:00
@hook.command ( " nicks " )
2015-05-29 17:28:07 +00:00
def cmd_nicks ( msg ) :
if len ( msg . args ) < 1 :
2015-10-30 20:57:45 +00:00
raise IMException ( " Provide a login " )
2015-05-29 17:28:07 +00:00
nick = found_login ( msg . args [ 0 ] )
if nick is None :
nick = msg . args [ 0 ]
else :
nick = nick . login
nicks = [ ]
for alias in context . data . getNode ( " aliases " ) . getChilds ( ) :
if alias [ " to " ] == nick :
nicks . append ( alias [ " from " ] )
if len ( nicks ) > = 1 :
return Response ( " %s is also known as %s . " % ( nick , " , " . join ( nicks ) ) , channel = msg . channel )
else :
return Response ( " %s has no known alias. " % nick , channel = msg . channel )
2015-11-02 19:19:12 +00:00
@hook.ask ( )
2015-05-26 05:26:13 +00:00
def parseask ( msg ) :
res = re . match ( r " ^( \ S+) \ s*( ' s|suis|est|is|was|were) \ s+([a-zA-Z0-9_-] { 3,8})$ " , msg . text , re . I )
if res is not None :
nick = res . group ( 1 )
login = res . group ( 3 )
if nick == " my " or nick == " I " or nick == " i " or nick == " je " or nick == " mon " or nick == " ma " :
nick = msg . nick
if nick in context . data . getNode ( " aliases " ) . index :
context . data . getNode ( " aliases " ) . index [ nick ] [ " to " ] = login
else :
ms = ModuleState ( " alias " )
ms . setAttribute ( " from " , nick )
ms . setAttribute ( " to " , login )
context . data . getNode ( " aliases " ) . addChild ( ms )
context . save ( )
return Response ( " ok, c ' est noté, %s est %s "
% ( nick , login ) ,
channel = msg . channel ,
nick = msg . nick )