| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
import types |
|---|
| 19 |
import string, random, re |
|---|
| 20 |
from ircbot import SingleServerIRCBot |
|---|
| 21 |
from irclib import nm_to_n, nm_to_h, ip_numstr_to_quad, ip_quad_to_numstr |
|---|
| 22 |
from motcle import * |
|---|
| 23 |
from dico_poilu import * |
|---|
| 24 |
from dico_poilu import unicode2term |
|---|
| 25 |
|
|---|
| 26 |
class TestBot(SingleServerIRCBot): |
|---|
| 27 |
def __init__(self, channel, utf8_channel, nickname, server, port=6667): |
|---|
| 28 |
SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) |
|---|
| 29 |
self.god = "haypo" |
|---|
| 30 |
self.channel = channel |
|---|
| 31 |
self.enmarche = 1 |
|---|
| 32 |
self.utf8_chan = utf8_channel |
|---|
| 33 |
self.dico = dico_poilu(self) |
|---|
| 34 |
self.motcle = motcle_poilu() |
|---|
| 35 |
self.taux_reponse = 100 |
|---|
| 36 |
self.welcome = u"Salut" |
|---|
| 37 |
self.reponse_motcle = True |
|---|
| 38 |
self.reponse_dico = True |
|---|
| 39 |
|
|---|
| 40 |
def on_nicknameinuse(self, c, e): |
|---|
| 41 |
c.nick(c.get_nickname() + "_") |
|---|
| 42 |
|
|---|
| 43 |
def on_welcome(self, c, e): |
|---|
| 44 |
c.join(self.channel) |
|---|
| 45 |
self.send_privmsgu(self.channel, self.welcome) |
|---|
| 46 |
|
|---|
| 47 |
def get_command(self, e): |
|---|
| 48 |
cmd = e.arguments()[0] |
|---|
| 49 |
try: |
|---|
| 50 |
cmd=unicode(cmd, "utf-8") |
|---|
| 51 |
except: |
|---|
| 52 |
cmd=unicode(cmd, "iso-8859-1") |
|---|
| 53 |
return cmd.strip() |
|---|
| 54 |
|
|---|
| 55 |
def aide(self): |
|---|
| 56 |
self.echou(u"Commandes :") |
|---|
| 57 |
self.echou(u"- liste rimes <mot> : liste des rimes pour la terminaison du mot spécifié") |
|---|
| 58 |
self.echou(u"- rime mot / derime mot : ajoute/supprime un mot du dictinnaire") |
|---|
| 59 |
self.echou(u"- terminaison mot : affiche la terminaison du mot") |
|---|
| 60 |
self.echou(u"- dit #chan ... / dit nick ... : fait parler le bot") |
|---|
| 61 |
self.echou(u"- recharge_muet : recharge muet.txt") |
|---|
| 62 |
self.echou(u"- recharge_terminaison : recharge terminaison.txt") |
|---|
| 63 |
self.echou(u"- recharge_dico : recharge dico.txt") |
|---|
| 64 |
self.echou(u"- recharge_motcle : recharge motcle_regex.txt") |
|---|
| 65 |
self.echou(u"- join #chan / leave #chan : joint/quitte le canal #<chan>") |
|---|
| 66 |
self.echou(u"- nick xxx : change de surnom") |
|---|
| 67 |
self.echou(u"- backup : sauve toutes les données sur le disque dur") |
|---|
| 68 |
self.echou(u"- utf-8 / iso : parle en UTF-8 / iso-8859-1") |
|---|
| 69 |
self.echou(u"- muet : liste des caractères muets") |
|---|
| 70 |
self.echou(u"- taux_reponse xxx : fixe le taux de réponse (en pourcent)") |
|---|
| 71 |
self.echou(u"- reponse_dico : active/désactive la réponse du dico") |
|---|
| 72 |
self.echou(u"- reponse_motcle : active/désactive la réponse des mot-clés") |
|---|
| 73 |
|
|---|
| 74 |
def on_privmsg(self, c, e): |
|---|
| 75 |
cmd = self.get_command(e) |
|---|
| 76 |
self.do_priv_command(cmd) |
|---|
| 77 |
|
|---|
| 78 |
def on_pubmsg(self, c, e): |
|---|
| 79 |
self.channel = e.target() |
|---|
| 80 |
nick = nm_to_n(e.source()) |
|---|
| 81 |
cmd = self.get_command(e) |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
regs = re.compile("^"+self.connection.get_nickname()+"[:,>]? *(.*)$", re.IGNORECASE).search(cmd) |
|---|
| 85 |
if regs != None: |
|---|
| 86 |
if nick==self.god and self.do_priv_command(regs.group(1)): |
|---|
| 87 |
return |
|---|
| 88 |
if self.do_pub_command(nick, regs.group(1)): |
|---|
| 89 |
return |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
if self.enmarche == 0: |
|---|
| 93 |
return |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
reponse = None |
|---|
| 97 |
if reponse==None and self.reponse_motcle: reponse = self.motcle.reponse(cmd) |
|---|
| 98 |
if reponse==None and self.reponse_dico: reponse = self.dico.reponse(cmd) |
|---|
| 99 |
if reponse==None: return |
|---|
| 100 |
|
|---|
| 101 |
if self.taux_reponse <= random.uniform(0,101): return |
|---|
| 102 |
self.send_privmsgu(self.channel, nick+": "+reponse) |
|---|
| 103 |
return |
|---|
| 104 |
|
|---|
| 105 |
def send_privmsg(self, nick, message): |
|---|
| 106 |
if type(nick)==types.UnicodeType: |
|---|
| 107 |
nick = nick.encode("ascii") |
|---|
| 108 |
if self.utf8_chan: |
|---|
| 109 |
self.connection.privmsg(nick, message.decode("latin-1").encode("utf-8")) |
|---|
| 110 |
else: |
|---|
| 111 |
self.connection.privmsg(nick, message) |
|---|
| 112 |
|
|---|
| 113 |
def send_privmsgu(self, nick, message): |
|---|
| 114 |
if type(nick)==types.UnicodeType: |
|---|
| 115 |
nick = nick.encode("ascii") |
|---|
| 116 |
if self.utf8_chan: |
|---|
| 117 |
self.connection.privmsg(nick, message.encode("UTF-8")) |
|---|
| 118 |
else: |
|---|
| 119 |
self.connection.privmsg(nick, message.encode("iso-8859-1")) |
|---|
| 120 |
|
|---|
| 121 |
def echo(self, message): |
|---|
| 122 |
self.echou(message.decode("iso-8859-1")) |
|---|
| 123 |
|
|---|
| 124 |
def echou(self, message): |
|---|
| 125 |
print unicode2term(message) |
|---|
| 126 |
self.send_privmsgu(self.god, message) |
|---|
| 127 |
|
|---|
| 128 |
def before_dying(self): |
|---|
| 129 |
self.dico.sauve() |
|---|
| 130 |
|
|---|
| 131 |
def do_pub_command(self, nick, cmd): |
|---|
| 132 |
c = self.connection |
|---|
| 133 |
|
|---|
| 134 |
if (re.compile("^ta gueule", re.IGNORECASE).search(cmd) != None): |
|---|
| 135 |
if self.enmarche!=0: self.send_privmsg(self.channel, "Ok, je me tais") |
|---|
| 136 |
self.enmarche = 0 |
|---|
| 137 |
return True |
|---|
| 138 |
elif self.enmarche==0: |
|---|
| 139 |
self.send_privmsg(self.channel, "re") |
|---|
| 140 |
self.enmarche = 1 |
|---|
| 141 |
return True |
|---|
| 142 |
return False |
|---|
| 143 |
|
|---|
| 144 |
def do_priv_command(self, cmd): |
|---|
| 145 |
c = self.connection |
|---|
| 146 |
|
|---|
| 147 |
if cmd == "aide": |
|---|
| 148 |
self.aide() |
|---|
| 149 |
return True |
|---|
| 150 |
|
|---|
| 151 |
if cmd == "disconnect": |
|---|
| 152 |
self.disconnect() |
|---|
| 153 |
return True |
|---|
| 154 |
|
|---|
| 155 |
if (re.compile("^casse toi", re.IGNORECASE).search(cmd) != None): |
|---|
| 156 |
self.before_dying() |
|---|
| 157 |
self.die() |
|---|
| 158 |
return True |
|---|
| 159 |
|
|---|
| 160 |
regs = re.compile("^insulte (.+) (.+)$", re.IGNORECASE).search(cmd) |
|---|
| 161 |
if regs != None: |
|---|
| 162 |
if self.dico.ajoute(regs.group(1), regs.group(2)): |
|---|
| 163 |
self.echou(u"Ajoute l'insulte %s pour %s" \ |
|---|
| 164 |
%(regs.group(2), regs.group(1))) |
|---|
| 165 |
return True |
|---|
| 166 |
|
|---|
| 167 |
regs = re.compile("^rime (.+)$", re.IGNORECASE).search(cmd) |
|---|
| 168 |
if regs != None: |
|---|
| 169 |
if self.dico.ajoute_terme(regs.group(1)): |
|---|
| 170 |
self.echou(u"Ajoute la rime %s" %(regs.group(1))) |
|---|
| 171 |
return True |
|---|
| 172 |
|
|---|
| 173 |
regs = re.compile("^dit ([^ ]+) (.+)$", re.IGNORECASE).search(cmd) |
|---|
| 174 |
if regs != None: |
|---|
| 175 |
dit=regs.group(2) |
|---|
| 176 |
self.send_privmsgu(regs.group(1), regs.group(2)) |
|---|
| 177 |
return True |
|---|
| 178 |
|
|---|
| 179 |
if cmd == "utf8": |
|---|
| 180 |
if self.utf8_chan==False: self.echo("Passe en UTF-8") |
|---|
| 181 |
self.utf8_chan = True |
|---|
| 182 |
return True |
|---|
| 183 |
|
|---|
| 184 |
if cmd == "iso": |
|---|
| 185 |
if self.utf8_chan==True: self.echo("Passe en ISO-8859-1") |
|---|
| 186 |
self.utf8_chan = False |
|---|
| 187 |
return True |
|---|
| 188 |
|
|---|
| 189 |
regs = re.compile("^derime (.*)$", re.IGNORECASE).search(cmd) |
|---|
| 190 |
if regs != None: |
|---|
| 191 |
if self.dico.supprime_terme(regs.group(1)) != None: |
|---|
| 192 |
self.echou(u"Supprime la rime %s" %(regs.group(1))) |
|---|
| 193 |
return True |
|---|
| 194 |
|
|---|
| 195 |
regs = re.compile("^liste rimes (.+)$", re.IGNORECASE).search(cmd) |
|---|
| 196 |
if regs != None: |
|---|
| 197 |
termes = self.dico.termes(regs.group(1)) |
|---|
| 198 |
if termes!=None: |
|---|
| 199 |
self.echou("Rimes en %s: %s" %(regs.group(1), ", ".join(termes))) |
|---|
| 200 |
return True |
|---|
| 201 |
|
|---|
| 202 |
regs = re.compile("^liste insultes (.+)$", re.IGNORECASE).search(cmd) |
|---|
| 203 |
if regs != None: |
|---|
| 204 |
termes = self.motcle.insultes(regs.group(1)) |
|---|
| 205 |
if termes!=None: |
|---|
| 206 |
self.echou("Insultes pour %s: %s" \ |
|---|
| 207 |
%(regs.group(1), ", ".join(termes))) |
|---|
| 208 |
return True |
|---|
| 209 |
|
|---|
| 210 |
regs = re.compile("^reponse_dico$", re.IGNORECASE).search(cmd) |
|---|
| 211 |
if regs != None: |
|---|
| 212 |
self.reponse_dico = not self.reponse_dico |
|---|
| 213 |
self.echou(u"Réponse par dico : %s" % self.reponse_dico) |
|---|
| 214 |
return True |
|---|
| 215 |
|
|---|
| 216 |
regs = re.compile("^reponse_motcle$", re.IGNORECASE).search(cmd) |
|---|
| 217 |
if regs != None: |
|---|
| 218 |
self.reponse_motcle = not self.reponse_motcle |
|---|
| 219 |
self.echou(u"Réponse par mot-clé : %s" % self.reponse_motcle) |
|---|
| 220 |
return True |
|---|
| 221 |
|
|---|
| 222 |
regs = re.compile("^taux_reponse (.*)$", re.IGNORECASE).search(cmd) |
|---|
| 223 |
if regs != None: |
|---|
| 224 |
try: |
|---|
| 225 |
taux = int(regs.group(1)) |
|---|
| 226 |
if taux<0: taux=0 |
|---|
| 227 |
if 100<taux: taux=100 |
|---|
| 228 |
self.taux_reponse = taux |
|---|
| 229 |
self.echou(u"Taux réponse = %s%%" % self.taux_reponse) |
|---|
| 230 |
return True |
|---|
| 231 |
except: |
|---|
| 232 |
self.echou(u"%s n'est pas un taux valide" %(regs.group(1))) |
|---|
| 233 |
|
|---|
| 234 |
if cmd=="muet": |
|---|
| 235 |
self.echou(self.dico.muet) |
|---|
| 236 |
return True |
|---|
| 237 |
|
|---|
| 238 |
if (cmd == "recharge_muet"): |
|---|
| 239 |
self.echo("(recharge muet.txt)") |
|---|
| 240 |
self.dico.charge_muet() |
|---|
| 241 |
return True |
|---|
| 242 |
|
|---|
| 243 |
if (cmd == "recharge_dico"): |
|---|
| 244 |
self.echo("(recharge dico.txt)") |
|---|
| 245 |
self.dico.charge_dico() |
|---|
| 246 |
return True |
|---|
| 247 |
|
|---|
| 248 |
if (cmd == "recharge_terminaison"): |
|---|
| 249 |
self.echo("(sauve dico, recharge terminaison.txt puis dico.txt)") |
|---|
| 250 |
self.dico.sauve() |
|---|
| 251 |
self.dico.charge_regex() |
|---|
| 252 |
self.dico.charge_dico() |
|---|
| 253 |
return True |
|---|
| 254 |
|
|---|
| 255 |
if (cmd == "recharge_motcle"): |
|---|
| 256 |
self.echo("(recharge motcle_regex.txt)") |
|---|
| 257 |
self.motcle.charge_regex() |
|---|
| 258 |
self.echo("(recharge insulte.txt)") |
|---|
| 259 |
self.motcle.charge() |
|---|
| 260 |
return True |
|---|
| 261 |
|
|---|
| 262 |
if (cmd == "backup"): |
|---|
| 263 |
self.dico.sauve() |
|---|
| 264 |
self.echo("backup done.") |
|---|
| 265 |
return True |
|---|
| 266 |
|
|---|
| 267 |
regs = re.compile("^leave (#.*)$", re.IGNORECASE).search(cmd) |
|---|
| 268 |
if regs != None: |
|---|
| 269 |
self.channel = regs.group(1).encode("ascii") |
|---|
| 270 |
self.connection.part(self.channel) |
|---|
| 271 |
return True |
|---|
| 272 |
|
|---|
| 273 |
regs = re.compile("^nick (.*)$", re.IGNORECASE).search(cmd) |
|---|
| 274 |
if regs != None: |
|---|
| 275 |
self.connection.nick(regs.group(1)) |
|---|
| 276 |
return True |
|---|
| 277 |
|
|---|
| 278 |
regs = re.compile("^terminaison (.*)$", re.IGNORECASE).search(cmd) |
|---|
| 279 |
if regs != None: |
|---|
| 280 |
mot = regs.group(1) |
|---|
| 281 |
term = self.dico.terminaison(mot) |
|---|
| 282 |
if term != None: |
|---|
| 283 |
self.echou(u"Terminaison de %s : %s" % (mot, term)) |
|---|
| 284 |
else: |
|---|
| 285 |
self.echou(u"Terminaison de %s : (aucune)" % mot) |
|---|
| 286 |
return True |
|---|
| 287 |
|
|---|
| 288 |
regs = re.compile("^join (#.*)$", re.IGNORECASE).search(cmd) |
|---|
| 289 |
if regs != None: |
|---|
| 290 |
self.channel = regs.group(1) |
|---|
| 291 |
self.connection.join(self.channel) |
|---|
| 292 |
return True |
|---|
| 293 |
return False |
|---|
| 294 |
|
|---|
| 295 |
def main(): |
|---|
| 296 |
import sys |
|---|
| 297 |
if len(sys.argv) != 4: |
|---|
| 298 |
print "Usage: testbot <server[:port]> <channel>[:utf8] <nickname>" |
|---|
| 299 |
sys.exit(1) |
|---|
| 300 |
|
|---|
| 301 |
s = string.split(sys.argv[1], ":", 1) |
|---|
| 302 |
server = s[0] |
|---|
| 303 |
if len(s) == 2: |
|---|
| 304 |
try: |
|---|
| 305 |
port = int(s[1]) |
|---|
| 306 |
except ValueError: |
|---|
| 307 |
print "Error: Erroneous port." |
|---|
| 308 |
sys.exit(1) |
|---|
| 309 |
else: |
|---|
| 310 |
port = 6667 |
|---|
| 311 |
|
|---|
| 312 |
channel = sys.argv[2] |
|---|
| 313 |
regs = re.compile("^(.*):utf8$").search(channel) |
|---|
| 314 |
regsiso = re.compile("^(.*):iso$").search(channel) |
|---|
| 315 |
if regs != None: |
|---|
| 316 |
utf8 = True |
|---|
| 317 |
channel = regs.group(1) |
|---|
| 318 |
elif regsiso != None: |
|---|
| 319 |
channel = regsiso.group(1) |
|---|
| 320 |
utf8 = False |
|---|
| 321 |
else: |
|---|
| 322 |
utf8 = False |
|---|
| 323 |
|
|---|
| 324 |
nickname = sys.argv[3] |
|---|
| 325 |
|
|---|
| 326 |
print "Creation de TestBot ..." |
|---|
| 327 |
bot = TestBot(channel, utf8, nickname, server, port) |
|---|
| 328 |
print "Lance le bot ... (salon %s, serveur %s, port %s)" % (channel, server, port) |
|---|
| 329 |
try: |
|---|
| 330 |
bot.start() |
|---|
| 331 |
except KeyboardInterrupt: |
|---|
| 332 |
bot.dico.sauve() |
|---|
| 333 |
print "Interrompu (CTRL+C)." |
|---|
| 334 |
|
|---|
| 335 |
if __name__ == "__main__": |
|---|
| 336 |
main() |
|---|
| 337 |
|
|---|
| 338 |
|
|---|