]>
gitweb.pimeys.fr Git - bots/hung.git/blob - hung.py
4 # Codé par 20-100 le 23/04/12
6 # Un bot IRC qui joue au pendu
11 import socket
, ssl
, json
17 from commands
import getstatusoutput
as ex
19 # Oui, j'ai recodé ma version d'irclib pour pouvoir rattrapper les SIGHUP
20 sys
. path
. insert ( 0 , "/home/vincent/scripts/python-myirclib" )
29 def get_config_logfile ( serveur
):
30 serveurs
={ "acoeur.crans.org" : "acoeur" , "irc.crans.org" : "crans" , "localhost" : "localhost" }
31 return config
. logfile_template
%( serveurs
[ serveur
])
33 def log ( serveur
, channel
, auteur
= None , message
= None ):
34 f
= open ( get_config_logfile ( serveur
), "a" )
35 if auteur
== message
== None :
36 # alors c'est que c'est pas un channel mais juste une ligne de log
37 chain
= " %s %s " %( time
. strftime ( " %F %T" ), channel
)
39 chain
= " %s [ %s : %s ] %s " %( time
. strftime ( " %F %T" ), channel
, auteur
, message
)
41 if config
. debug_stdout
:
46 class UnicodeBotError ( Exception ):
48 def bot_unicode ( chain
):
51 except UnicodeDecodeError as exc
:
54 def remplace_accents ( chaine
):
56 remplacements
= { u
"á" : u
"a" , u
"à" : u
"a" , u
"â" : u
"a" , u
"ä" : u
"a" , u
"é" : u
"e" , u
"è" : u
"e" , u
"ê" : u
"e" , u
"ë" : u
"e" , u
"í" : u
"i" , u
"ì" : u
"i" , u
"î" : u
"i" , u
"ï" : u
"i" , u
"ó" : u
"o" , u
"ò" : u
"o" , u
"ô" : u
"o" , u
"ö" : u
"o" , u
"ú" : u
"u" , u
"ù" : u
"u" , u
"û" : u
"u" , u
"ü" : u
"u" , u
"ý" : u
"y" , u
"ỳ" : u
"y" , u
"ŷ" : u
"y" , u
"ÿ" : u
"y" , u
"œ" : u
"oe" , u
"æ" : u
"ae" }
57 for avant
, apres
in remplacements
. items ():
58 chaine
= chaine
. replace ( avant
, apres
)
61 def is_something ( chain
, matches
, avant
= u
".*(?:^| )" , apres
= u
"(?:$|\.| |,|;).*" , case_sensitive
= False , debug
= False ):
63 chain
= unicode ( chain
, "utf8" )
65 chain
= unicode ( chain
, "utf8" ). lower ()
66 allmatches
= "(" + "|" . join ( matches
)+ ")"
67 reg
=( avant
+ allmatches
+ apres
). lower ()
72 return is_something ( chain
, config
. tag_triggers
)
74 def is_mot ( mot
, liste
):
75 real_word
= "" . join ([ lettre
[ 0 ] for lettre
in liste
])
76 real_word
= real_word
. decode ( "utf8" ). lower ()
77 mot
= remplace_accents ( mot
. decode ( "utf8" ))
80 class Hung ( ircbot
. SingleServerIRCBot
):
81 def __init__ ( self
, serveur
, debug
= False ):
82 temporary_pseudo
= config
. irc_pseudo
+ str ( random
. randrange ( 10000 , 100000 ))
83 ircbot
. SingleServerIRCBot
.__ init
__ ( self
, [( serveur
, 6667 )],
84 temporary_pseudo
, "Bot irc pour jouer au pendu" , 10 )
87 self
. overops
= config
. overops
88 self
. ops
= self
. overops
+ config
. ops
89 self
. report_bugs_to
= config
. report_bugs_to
90 self
. chanlist
= config
. chanlist
91 self
. stay_channels
= config
. stay_channels
92 self
. play_channels
= config
. play_channels
93 self
. play_status
={ i
:[ None , None , None ] for i
in self
. play_channels
}
94 self
. quiet_channels
= config
. quiet_channels
97 def give_me_my_pseudo ( self
, serv
):
98 serv
. privmsg ( "NickServ" , "RECOVER %s %s " %( config
. irc_pseudo
, config
. irc_password
))
99 serv
. privmsg ( "NickServ" , "RELEASE %s %s " %( config
. irc_pseudo
, config
. irc_password
))
101 serv
. nick ( config
. irc_pseudo
)
103 def on_welcome ( self
, serv
, ev
):
104 self
. serv
= serv
# ça serv ira :)
105 self
. give_me_my_pseudo ( serv
)
106 serv
. privmsg ( "NickServ" , "IDENTIFY %s " %( config
. irc_password
))
107 log ( self
. serveur
, "Connected" )
109 self
. chanlist
=[ "#bot" ]
110 self
. play_channels
=[ "#bot" ]
111 for c
in self
. chanlist
:
112 log ( self
. serveur
, "JOIN %s " %(c))
115 def pourmoi ( self
, serv
, message
):
116 """renvoie (False,lemessage) ou (True, le message amputé de "pseudo: ")"""
117 pseudo
= serv
. get_nickname ()
119 if message
[: size
]== pseudo
and len ( message
)> size
and message
[ size
]== ":" :
120 return ( True , message
[ size
+ 1 :]. lstrip ( " " ))
122 return ( False , message
)
124 def on_privmsg ( self
, serv
, ev
):
125 message
= ev
. arguments ()[ 0 ]
126 auteur
= irclib
. nm_to_n ( ev
. source ())
128 test
= bot_unicode ( message
)
129 except UnicodeBotError
:
130 if config
. utf8_trigger
:
131 serv
. privmsg ( auteur
, config
. utf8_fail_answers
)
133 message
= message
. split ()
134 cmd
= message
[ 0 ]. lower ()
137 helpmsg_default
= """Liste des commandes :
138 HELP Affiche ce message d'aide
139 SCORE Affiche ton score
140 SCORES Affiche les scores"""
142 JOIN Faire rejoindre un channel (sans paramètres, donne la liste des chans actuels)
143 LEAVE Faire quitter un channel
144 PLAY Passe un channel en mode "jouer"
145 NOPLAY Passe un channel en mode "ne pas jouer"
146 QUIET Se taire sur un channel
147 NOQUIET Opposé de QUIET
148 RELOAD Recharge la config"""
150 SAY Fais envoyer un message sur un chan ou à une personne
151 DO Me fait faire une action sur un chan
152 STAY Ignorera les prochains LEAVE pour un chan
153 NOSTAY Opposé de STAY
154 STATUS Montre l'état courant
156 helpmsg
= helpmsg_default
157 if auteur
in self
. ops
:
159 if auteur
in self
. overops
:
160 helpmsg
+= helpmsg_overops
161 for ligne
in helpmsg
. split ( " \n " ):
162 serv
. privmsg ( auteur
, ligne
)
164 if auteur
in self
. ops
:
166 if message
[ 1 ] in self
. chanlist
:
167 serv
. privmsg ( auteur
, "Je suis déjà sur %s " %( message
[ 1 ]))
169 serv
. join ( message
[ 1 ])
170 self
. chanlist
. append ( message
[ 1 ])
171 serv
. privmsg ( auteur
, "Channels : " + " " . join ( self
. chanlist
))
172 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
))
174 serv
. privmsg ( auteur
, "Channels : " + " " . join ( self
. chanlist
))
178 if auteur
in self
. ops
and len ( message
)> 1 :
179 if message
[ 1 ] in self
. chanlist
:
180 if not ( message
[ 1 ] in self
. stay_channels
) or auteur
in self
. overops
:
181 self
. quitter ( message
[ 1 ], " " . join ( message
[ 2 :]))
182 self
. chanlist
. remove ( message
[ 1 ])
183 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[successful]" )
185 serv
. privmsg ( auteur
, "Non, je reste !" )
186 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[failed]" )
188 serv
. privmsg ( auteur
, "Je ne suis pas sur %s " %( message
[ 1 ]))
192 if auteur
in self
. ops
:
194 if message
[ 1 ] in self
. play_channels
:
195 serv
. privmsg ( auteur
, "Je play déjà sur %s ." %( message
[ 1 ]))
196 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[failed]" )
198 self
. play_channels
. append ( message
[ 1 ])
199 self
. play_status
[ message
[ 1 ]]=[ None , None , None ]
200 serv
. privmsg ( auteur
, "Play channels : " + " " . join ( self
. play_channels
))
201 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[successful]" )
203 serv
. privmsg ( auteur
, "Play channels : " + " " . join ( self
. play_channels
))
207 if auteur
in self
. ops
:
209 if message
[ 1 ] in self
. play_channels
:
210 self
. play_channels
. remove ( message
[ 1 ])
211 serv
. privmsg ( auteur
, "Play channels : " + " " . join ( self
. play_channels
))
212 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[successful]" )
214 serv
. privmsg ( auteur
, "Je ne play pas sur %s ." %( message
[ 1 ]))
215 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[failed]" )
219 if auteur
in self
. overops
:
221 if message
[ 1 ] in self
. stay_channels
:
222 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[failed]" )
223 serv
. privmsg ( auteur
, "Je stay déjà sur %s ." %( message
[ 1 ]))
225 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[successful]" )
226 self
. stay_channels
. append ( message
[ 1 ])
227 serv
. privmsg ( auteur
, "Stay channels : " + " " . join ( self
. stay_channels
))
229 serv
. privmsg ( auteur
, "Stay channels : " + " " . join ( self
. stay_channels
))
233 if auteur
in self
. overops
:
235 if message
[ 1 ] in self
. stay_channels
:
236 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[successful]" )
237 self
. stay_channels
. remove ( message
[ 1 ])
238 serv
. privmsg ( auteur
, "Stay channels : " + " " . join ( self
. stay_channels
))
240 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[failed]" )
241 serv
. privmsg ( auteur
, "Je ne stay pas sur %s ." %( message
[ 1 ]))
245 elif cmd
in [ "states" , "status" ]:
246 if auteur
in self
. overops
:
247 for k
in self
. play_status
. keys ():
248 if self
. play_status
[ k
]==[ None , None , None ]:
249 serv
. privmsg ( auteur
, "None" )
251 serv
. privmsg ( auteur
, " %s : %s ( %s ) [ %s ]" %( k
, "" . join ([ str ( i
[ 0 ]) for i
in self
. play_status
[ k
][ 0 ]])
252 , self
. play_status
[ k
][ 1 ], self
. play_status
[ k
][ 2 ]))
254 if auteur
in self
. overops
:
255 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[successful]" )
260 if auteur
in self
. ops
:
262 if message
[ 1 ] in self
. quiet_channels
:
263 serv
. privmsg ( auteur
, "Je me la ferme déjà sur %s " %( message
[ 1 ]))
264 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[failed]" )
266 self
. quiet_channels
. append ( message
[ 1 ])
267 serv
. privmsg ( auteur
, "Quiet channels : " + " " . join ( self
. quiet_channels
))
268 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[successful]" )
270 serv
. privmsg ( auteur
, "Quiet channels : " + " " . join ( self
. quiet_channels
))
274 if auteur
in self
. ops
:
276 if message
[ 1 ] in self
. quiet_channels
:
277 self
. quiet_channels
. remove ( message
[ 1 ])
278 serv
. privmsg ( auteur
, "Quiet channels : " + " " . join ( self
. quiet_channels
))
279 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[successful]" )
281 serv
. privmsg ( auteur
, "Je ne me la ferme pas sur %s ." %( message
[ 1 ]))
282 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[failed]" )
286 if auteur
in self
. ops
:
288 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
)+ "[successful]" )
292 if auteur
in self
. overops
and len ( message
)> 2 :
293 serv
. privmsg ( message
[ 1 ], " " . join ( message
[ 2 :]))
294 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
))
295 elif len ( message
)<= 2 :
296 serv
. privmsg ( auteur
, "Syntaxe : SAY <channel> <message>" )
300 if auteur
in self
. overops
and len ( message
)> 2 :
301 serv
. action ( message
[ 1 ], " " . join ( message
[ 2 :]))
302 log ( self
. serveur
, "priv" , auteur
, " " . join ( message
))
303 elif len ( message
)<= 2 :
304 serv
. privmsg ( auteur
, "Syntaxe : DO <channel> <action>" )
307 elif cmd
in [ "score" , "scores" ]:
308 self
. send_scores ( serv
, auteur
)
312 serv
. privmsg ( auteur
, "Je n'ai pas compris. Essaye HELP…" )
314 def affiche_mot ( self
, serv
, canal
, begin
= "Mot courant" ):
315 if self
. play_status
. has_key ( canal
):
316 mot
= self
. play_status
[ canal
][ 0 ]
317 obfuskated
= " " . join ([ lettre
[ 0 ] if lettre
[ 1 ] else "_" for lettre
in mot
])
318 serv
. privmsg ( canal
, " %s : %s " %( begin
, obfuskated
))
320 def start_partie ( self
, serv
, canal
):
321 mots
=[ mot
. strip () for mot
in open ( config
. dico_mots
). readlines ()]
322 defs
=[ defi
. strip () for defi
in open ( config
. dico_defs
). readlines ()]
323 indice
= random
. randrange ( 0 , len ( mots
))
324 mot
, definition
= mots
[ indice
], defs
[ indice
]
325 # ' et - sont considérés comme déjà devinés
326 mot
= [( lettre
, lettre
in "'-()" ) for lettre
in list ( mot
)]
327 self
. play_status
[ canal
]=[ mot
, definition
,{}]
328 self
. affiche_mot ( serv
, canal
, begin
= "Devinez" )
330 def on_pubmsg ( self
, serv
, ev
):
331 auteur
= irclib
. nm_to_n ( ev
. source ())
333 message
= ev
. arguments ()[ 0 ]
335 test
= bot_unicode ( message
)
336 except UnicodeBotError
:
337 if not canal
in self
. quiet_channels
and config
. utf8_trigger
:
338 serv
. privmsg ( canal
, " %s : %s " %( auteur
, config
. utf8_fail_answers
))
340 pour_moi
, message
= self
. pourmoi ( serv
, message
)
341 if pour_moi
and message
. split ()!=[]:
342 cmd
= message
. split ()[ 0 ]. lower ()
344 args
= " " . join ( message
. split ()[ 1 :])
347 if cmd
in [ "meurs" , "die" , "crève" ]:
348 if auteur
in self
. overops
:
349 log ( self
. serveur
, canal
, auteur
, message
+ "[successful]" )
352 serv
. privmsg ( canal
, " %s : crève !" %(auteur))
353 log ( self
. serveur
, canal
, auteur
, message
+ "[failed]" )
354 elif cmd
== "reload" :
355 if auteur
in self
. ops
:
356 log ( self
. serveur
, canal
, auteur
, message
+ "[successful]" )
358 elif cmd
in [ "part" , "leave" , "dégage" ]:
359 if auteur
in self
. ops
and ( not ( canal
in self
. stay_channels
)
360 or auteur
in self
. overops
):
362 log ( self
. serveur
, canal
, auteur
, message
+ "[successful]" )
363 if canal
in self
. chanlist
:
364 self
. chanlist
. remove ( canal
)
366 serv
. privmsg ( canal
, " %s : Non, je reste !" %(auteur))
367 log ( self
. serveur
, canal
, auteur
, message
+ "[failed]" )
368 elif cmd
in [ "play" , "jeu" , "encore" , "again" , "partie" , "pendu" , "game" , "mot" , "go" , "allez" ]:
369 if not canal
in self
. quiet_channels
and canal
in self
. play_channels
:
370 if self
. play_status
. has_key ( canal
):
371 if self
. play_status
[ canal
]==[ None , None , None ]:
372 self
. start_partie ( serv
, canal
)
374 self
. affiche_mot ( serv
, canal
, begin
= " %s : Rappel" %(auteur))
376 self
. play_status
[ canal
]=[ None , None , None ]
377 self
. start_partie ( serv
, canal
)
378 elif not canal
in self
. play_channels
:
379 serv
. privmsg ( canal
, " %s : pas ici…" %(auteur))
380 elif ( cmd
in list ( "azertyuiopqsdfghjklmwxcvbn" ) and canal
in self
. play_channels
381 and self
. play_status
. has_key ( canal
) and self
. play_status
[ canal
]!=[ None , None , None ]):
383 liste
= self
. play_status
[ canal
][ 0 ]
384 listeapres
=[( lettre
[ 0 ], lettre
[ 1 ] or lettre
[ 0 ]== giv_let
) for lettre
in liste
]
385 if liste
!= listeapres
:
386 nbtrouvees
=( sum ([ lettre
[ 1 ] for lettre
in listeapres
if not lettre
[ 0 ] in "'-()" ])
387 - sum ([ lettre
[ 1 ] for lettre
in liste
if not lettre
[ 0 ] in "'-()" ]))
388 if self
. play_status
[ canal
][ 2 ]. has_key ( auteur
):
389 self
. play_status
[ canal
][ 2 ][ auteur
]+= nbtrouvees
391 self
. play_status
[ canal
][ 2 ][ auteur
] = nbtrouvees
392 self
. play_status
[ canal
][ 0 ]= listeapres
393 self
. affiche_mot ( serv
, canal
, begin
= " %s placé" %(giv_let))
394 if all ([ lettre
[ 1 ] for lettre
in listeapres
]):
395 self
. gagne ( serv
, canal
)
397 elif cmd
in [ "score" , "scores" , "!score" , "!scores" ]:
398 self
. send_scores ( serv
, auteur
)
399 if cmd
in [ "meur" , "meurt" , "meurre" , "meurres" ] and not canal
in self
. quiet_channels
:
400 serv
. privmsg ( canal
, ' %s : Mourir, impératif, 2ème personne du singulier : "meurs" (de rien)' %(auteur))
401 if is_tag ( message
) and not canal
in self
. quiet_channels
:
402 if auteur
in self
. ops
:
403 action
= random
. choice ( config
. tag_actions
)
404 serv
. action ( canal
, action
. encode ( "utf8" ))
405 self
. quiet_channels
. append ( canal
)
407 answer
= random
. choice ( config
. tag_answers
)
408 for ligne
in answer
. split ( " \n " ):
409 serv
. privmsg ( canal
, " %s : %s " %( auteur
, ligne
. encode ( "utf8" )))
410 # on essaye de voir si le mot fourni matche la partie en cours
412 if self
. play_status
[ canal
][ 0 ]!= None and is_mot ( mot
, self
. play_status
[ canal
][ 0 ]):
414 # on regarde combien de lettre il manquait
415 manquait
= sum ([ not lettre
[ 1 ] for lettre
in self
. play_status
[ canal
][ 0 ]])
416 self
. add_score ({ auteur
: manquait
})
417 if self
. play_status
[ canal
][ 2 ]. has_key ( auteur
):
418 self
. play_status
[ canal
][ 2 ][ auteur
]+= manquait
420 self
. play_status
[ canal
][ 2 ][ auteur
]= manquait
421 self
. gagne ( serv
, canal
, bonus
= auteur
, bonusvalue
= manquait
)
426 def on_action ( self
, serv
, ev
):
427 action
= ev
. arguments ()[ 0 ]
428 auteur
= irclib
. nm_to_n ( ev
. source ())
429 channel
= ev
. target ()
432 def on_kick ( self
, serv
, ev
):
433 auteur
= irclib
. nm_to_n ( ev
. source ())
434 channel
= ev
. target ()
435 victime
= ev
. arguments ()[ 0 ]
436 raison
= ev
. arguments ()[ 1 ]
437 if victime
== self
. nick
:
438 log ( self
. serveur
, " %s kické de %s par %s (raison : %s )" %( victime
, channel
, auteur
, raison
))
441 # on ne dit rien au rejoin
442 #l1,l2=config.kick_answers,config.kick_actions
443 #n1,n2=len(l1),len(l2)
444 #i=random.randrange(n1+n2)
446 # serv.action(channel,l2[i-n1].format(auteur).encode("utf8"))
448 # serv.privmsg(channel,l1[i].format(auteur).encode("utf8"))
451 return self
. serv
. get_nickname ()
452 nick
= property ( _getnick
)
454 def quitter ( self
, chan
, leave_message
= None ):
455 if leave_message
== None :
456 leave_message
= random
. choice ( config
. leave_messages
)
457 self
. serv
. part ( chan
, message
= leave_message
. encode ( "utf8" ))
460 quit_message
= random
. choice ( config
. quit_messages
)
461 self
. die ( msg
= quit_message
. encode ( "utf8" ))
463 def get_scores ( self
):
464 f
= open ( config
. scores_file
)
465 scores
= pickle
. load ( f
)
468 def save_scores ( self
, scores
):
469 f
= open ( config
. scores_file
, 'w' )
470 pickle
. dump ( scores
, f
)
472 def add_score ( self
, dico
):
473 scores
= self
. get_scores ()
474 for k
, v
in dico
. items ():
475 if scores
. has_key ( k
):
479 self
. save_scores ( scores
)
480 def send_scores ( self
, serv
, destinataire
):
481 scores
= self
. get_scores ()
482 scores
= scores
. items ()
483 scores
. sort ( lambda x
, y
: cmp ( x
[ 1 ], y
[ 1 ]))
485 serv
. privmsg ( destinataire
, "Scores by score : " + " ; " . join ([ " %s %s " %( k
, v
) for ( k
, v
) in scores
]) )
486 scores
. sort ( lambda x
, y
: cmp ( x
[ 0 ]. lower (), y
[ 0 ]. lower ()))
487 serv
. privmsg ( destinataire
, "Scores by pseudo : " + " ; " . join ([ " %s %s " %( k
, v
) for ( k
, v
) in scores
]) )
489 def gagne ( self
, serv
, canal
, bonus
= None , bonusvalue
= 2 ):
490 realword
= "" . join ([ lettre
[ 0 ] for lettre
in self
. play_status
[ canal
][ 0 ]])
491 definition
= self
. play_status
[ canal
][ 1 ]
492 serv
. privmsg ( canal
, "Bravo ! C'était %s " %(realword))
493 serv
. privmsg ( canal
, definition
)
494 nlettre
= float ( len ( realword
. replace ( "'" , "" ). replace ( "-" , "" )))
495 contribs
=[ " %s : %s%%%s " %( pseudo
, str ( int ( 100 * contrib
/ nlettre
)),( "+bonus( %s )" %(bonusvalue))*( bonus
== pseudo
)) for pseudo
, contrib
in self
. play_status
[ canal
][ 2 ]. items ()]
496 contribs_score
={ pseudo
: int ( 10 * contrib
/ nlettre
) for pseudo
, contrib
in self
. play_status
[ canal
][ 2 ]. items ()}
497 self
. add_score ( contribs_score
)
498 serv
. privmsg ( canal
, "Contributions : %s " %( " " . join ( contribs
)) )
499 self
. play_status
[ canal
]=[ None , None , None ]
501 def reload ( self
, auteur
= None ):
503 if auteur
in [ None , "SIGHUP" ]:
504 towrite
= "Config reloaded" + " (SIGHUP received)" *( auteur
== "SIGHUP" )
505 for to
in config
. report_bugs_to
:
506 self
. serv
. privmsg ( to
, towrite
)
507 log ( self
. serveur
, towrite
)
509 self
. serv
. privmsg ( auteur
, "Config reloaded" )
511 def start_as_daemon ( self
, outfile
):
512 sys
. stderr
= Logger ( outfile
)
516 class Logger ( object ):
517 """Pour écrire ailleurs que sur stdout"""
518 def __init__ ( self
, filename
= "hung.full.log" ):
519 self
. filename
= filename
521 def write ( self
, message
):
522 f
= open ( self
. filename
, "a" )
527 if __name__
== "__main__" :
530 print "Usage : hung.py <serveur> [--debug] [--no-output] [--daemon [--pidfile]] [--outfile]"
531 print " --outfile sans --no-output ni --daemon n'a aucun effet"
534 if "--daemon" in sys
. argv
:
535 thisfile
= os
. path
. realpath ( __file__
)
536 thisdirectory
= thisfile
. rsplit ( "/" , 1 )[ 0 ]
537 os
. chdir ( thisdirectory
)
541 if "debug" in sys
. argv
or "--debug" in sys
. argv
:
545 if "--no-output" in sys
. argv
or "--daemon" in sys
. argv
:
546 outfile
= "/var/log/bots/hung.full.log"
549 if arg
[ 0 ]. strip ( '-' ) in [ "out" , "outfile" , "logfile" ]:
551 sys
. stdout
= Logger ( outfile
)
552 serveurs
={ "a♡" : "acoeur.crans.org" , "acoeur" : "acoeur.crans.org" , "acoeur.crans.org" : "acoeur.crans.org" ,
553 "irc" : "irc.crans.org" , "crans" : "irc.crans.org" , "irc.crans.org" : "irc.crans.org" ,
554 "localhost" : "localhost" }
556 serveur
= serveurs
[ serveur
]
558 print "Server Unknown : %s " %(serveur)
560 hung
= Hung ( serveur
, debug
)
561 # Si on reçoit un SIGHUP, on reload la config
562 def sighup_handler ( signum
, frame
):
563 hung
. reload ( "SIGHUP" )
564 signal
. signal ( signal
. SIGHUP
, sighup_handler
)
566 child_pid
= os
. fork ()
569 hung
. start_as_daemon ( outfile
)
571 # on enregistre le pid de hung
572 pidfile
= "/var/run/bots/hung.pid"
575 if arg
[ 0 ]. strip ( '-' ) in [ "pidfile" ]:
577 f
= open ( pidfile
, "w" )
578 f
. write ( " %s \n " % child_pid
)