4 """ Des fonctions utilitaire pour savoir si on est dans tel ou tel cas. """
10 def regex_join(liste
, avant
=u
".*(?:^| )", apres
=u
"(?:$|\.| |,|;).*"):
11 """Fabrique une regexp à partir d'une liste d'éléments à matcher."""
12 return avant
+ u
"(" + u
"|".join(liste
) + u
")" + apres
14 def is_something(chain
, regexp
=None, matches
=[], avant
=u
".*(?:^| )", apres
=u
"(?:$|\.| |,|;).*",
15 case_sensitive
=False):
16 """Vérifie si chain contient un des éléments de ``matches``.
17 Si ``regexp`` est fournie, c'est simplement elle qui est testée"""
18 if not case_sensitive
:
23 regexp
= regex_join(matches
, avant
, apres
)
24 regexp
= re
.compile(regexp
)
25 o
= regexp
.match(chain
)
29 """Compilation des regexp à partir de la conf.
30 Place les résultats dans le namespace de ``config``"""
31 config
.insult_regexp
= regex_join(config
.insultes
, avant
=u
".*(?:^| |')")
32 config
.insult_regexp_compiled
= re
.compile(config
.insult_regexp
)
34 config
.not_insult_regexp
= u
".*pas %s%s" % (config
.amplifier_regexp
, config
.insult_regexp
)
35 config
.not_insult_regexp_compiled
= re
.compile(config
.not_insult_regexp
)
37 config
.compliment_regexp
= regex_join(config
.compliment_triggers
, avant
=u
".*(?:^| |')")
38 config
.compliment_regexp_compiled
= re
.compile(config
.compliment_regexp
)
40 config
.perdu_regexp
= regex_join(config
.perdu
)
41 config
.perdu_regexp_compiled
= re
.compile(config
.perdu_regexp
)
43 config
.tag_regexp
= regex_join(config
.tag_triggers
)
44 config
.tag_regexp_compiled
= re
.compile(config
.tag_regexp
)
46 config
.gros_regexp
= regex_join(config
.gros
)
47 config
.gros_regexp_compiled
= re
.compile(config
.gros_regexp
)
49 config
.tesla_regexp
= regex_join(config
.tesla_triggers
, avant
=u
"^", apres
="$")
50 config
.tesla_regexp_compiled
= re
.compile(config
.tesla_regexp
)
52 config
.merci_regexp
= regex_join(config
.merci_triggers
)
53 config
.merci_regexp_compiled
= re
.compile(config
.merci_regexp
)
55 config
.tamere_regexp
= regex_join(config
.tamere_triggers
)
56 config
.tamere_regexp_compiled
= re
.compile(config
.tamere_regexp
)
58 config
.bonjour_regexp
= regex_join(config
.bonjour_triggers
, avant
=u
"^")
59 config
.bonjour_regexp_compiled
= re
.compile(config
.bonjour_regexp
)
61 config
.bonne_nuit_regexp
= regex_join(config
.bonne_nuit_triggers
, avant
=u
"^")
62 config
.bonne_nuit_regexp_compiled
= re
.compile(config
.bonne_nuit_regexp
)
64 config
.pan_regexp
= regex_join(config
.pan_triggers
, avant
=".*", apres
=".*")
65 config
.pan_regexp_compiled
= re
.compile(config
.pan_regexp
)
69 def is_insult(chain
, debug
=True):
70 """Vérifie si ``chain`` contient une insulte."""
71 return is_something(chain
, config
.insult_regexp_compiled
)
72 def is_not_insult(chain
):
73 """Vérifie si ``chain`` contient une insulte à la forme négative."""
74 return is_something(chain
, config
.not_insult_regexp_compiled
)
75 def is_compliment(chain
, debug
=True):
76 """Vérifie si ``chain`` contient un compliment."""
77 return is_something(chain
, config
.compliment_regexp_compiled
)
79 """Vérifie si ``chain`` contient une raison de perdre."""
80 return is_something(chain
, config
.perdu_regexp_compiled
)
82 """Vérifie si ``chain`` demande de fermer sa gueule."""
83 return is_something(chain
, config
.tag_regexp_compiled
)
85 """Vérifie si ``chain`` traite de gros."""
86 return is_something(chain
, config
.gros_regexp_compiled
)
88 """Vérifie si ``chain`` est un ping."""
89 return is_something(chain
, config
.tesla_regexp_compiled
)
91 """Vérifie si ``chain`` contient un remerciement."""
92 return is_something(chain
, config
.merci_regexp_compiled
)
94 """Vérifie si ``chain`` traite ma mère."""
95 return is_something(chain
, config
.tamere_regexp_compiled
)
96 def is_bad_action_trigger(chain
,pseudo
):
97 """Vérifie si ``chain`` est une action méchante.
98 On a besoin d'une regexp dynamique à cause du pseudo qui peut changer."""
99 return is_something(chain
, matches
=config
.bad_action_triggers
, avant
=u
"^",
100 apres
="(?: [a-z]*ment)? %s($|\.| |,|;).*" % (pseudo
))
101 def is_good_action_trigger(chain
,pseudo
):
102 """Vérifie si ``chain`` est une action gentille.
103 On a besoin d'une regexp dynamique à cause du pseudo qui peut changer."""
104 return is_something(chain
, matches
=config
.good_action_triggers
, avant
=u
"^",
105 apres
="(?: [a-z]*ment)? %s($|\.| |,|;).*" % (pseudo
))
106 def is_bonjour(chain
):
107 """Vérifie si ``chain`` contient un bonjour."""
108 return is_something(chain
, config
.bonjour_regexp_compiled
)
109 def is_bonne_nuit(chain
):
110 """Vérifie si ``chain`` contient un bonne nuit."""
111 return is_something(chain
, config
.bonne_nuit_regexp_compiled
)
113 """Vérifie si ``chain`` contient un pan."""
114 return is_something(chain
, config
.pan_regexp_compiled
)
117 """Vérifie si l'heure actuelle est entre les deux heures ``conf[0]`` et ``conf[1]``"""
118 _
, _
, _
, h
, m
, s
, _
, _
, _
= time
.localtime()
119 return (conf
[0], 0, 0) < (h
, m
, s
) < (conf
[1], 0, 0)
121 """Vérifie si on est le jour."""
122 return is_time(config
.daytime
)
124 """Vérifie si on est la nuit."""
125 return is_time(config
.nighttime
)