
Goule
   
Groupe : Membres
Messages : 980
Inscrit le : 01/08/2002 23:00
Lieu : Lyon
Membre no. 106

|
Oui mais c beau techniquement  ))
J'ai aussi ça :
CODE |
#!/usr/bin/env python
# NYCTALOPE - 07/30/2002 - 02:33:02
#
# Lorsque quelqu'un parle, le bot prend un nombre aléatoirement entre 0 et 1
# Il prend le code ASCII correspondant aux 2 premières décimales de ce nombre
# Si ce code apparaît dans le nick en majuscules de l'intervenant, une ligne
# des scripts du donjon est envoyée.
#
# Tout le monde peut dire en public ou en privé :
# nyctalope, parle
# pour recevoir une citation.
#
# Le fichier nyctalope.conf permet de configurer le bot.
#
# GNU-GPL v2, (C)2002 Romain GUY, romain.guy@jext.org
import os.path
from random import *
import re
from socket import *
import sys
################################################################################ ##########
# CONSTANTES - Définies dans le fichier nyctalope.conf
# Ce sont les valeurs par défaut si le fichier nyctalope.conf est introuvable.
################################################################################ ##########
BOT_CHANS = '#pcteam,#progworld' # canaux (séparés par des virgules)
BOT_HOST = 'ircnet.kaptech.fr' # serveur
BOT_PORT = 6667 # port sur le serveur IRC
BOT_IDENT = 'nyctalope' # nick ET ident du bot
OP = '.?gfx@.*\.ppp\.tiscali\.fr' # masque de l'administrateur (c'est une regex)
IDENT_PORT = 113 # port ident (local)
################################################################################ ##########
# CODE SOURCE
################################################################################ ##########
DEBUG = 0 # mode debug
sourceFiles = []
# EMISSION D'UNE CITATION
def quote(source):
randomizer = Random()
file = sourceFiles[randomizer.randrange(1, len(sourceFiles))]
if file[-1] == '\n':
file = file[:-1]
quotes = open(file)
lines = quotes.readlines()
line = ''
quotes.close()
while len(line) == 0:
line = lines[randomizer.randrange(1, len(lines))].strip()
if DEBUG:
print line
bot.send('NOTICE ' + source + ' :' + line + '\r\n')
# CONFIGURATION
print "Reading configuration..."
if os.path.exists('nyctalope.conf'):
conf = open('nyctalope.conf')
lines = conf.readlines()
conf.close()
for line in lines:
match = re.search('^([^=]*)=(.*)$', line)
if match is not None:
key, value = match.groups(2)
if key == 'BOT_CHANS': BOT_CHANS = value
elif key == 'BOT_HOST' : BOT_HOST = value
elif key == 'BOT_PORT' : BOT_PORT = int(value)
elif key == 'BOT_IDENT': BOT_IDENT = value
elif key == 'OP' : OP = '(\+.)?' + value.replace('.', '\.').replace('*', '.*')
# CREATION DE LA LISTE DES FICHIERS
print "Gathering data..."
naheulbeuk = re.compile('\?|/?Pen of Chaos_Le Donjon de Naheulbeuk \d\d.*.txt$')
for file in os.listdir('.'):
if naheulbeuk.search(file):
sourceFiles.append(file)
# SOCKETS
print "Creating sockets..."
ident, bot = socket(AF_INET, SOCK_STREAM), socket(AF_INET, SOCK_STREAM)
# SERVEUR IDENT
print "Running ident server on port", IDENT_PORT, "..."
try:
ident.bind(('', IDENT_PORT))
ident.listen(1)
bot.connect((BOT_HOST, BOT_PORT))
irc, irchost = ident.accept()
print "Got ident request, answering..."
irc.send(re.search('(\d+)', irc.recv(1024)).group(1) + ", " + str(BOT_PORT) + ' : USERID : UNIX : ' + BOT_IDENT)
irc.close()
ident.close()
except:
print "Ident server already running on port", IDENT_PORT, "..."
# CONNEXION DU BOT
print "Connecting bot..."
try:
bot.send('NICK ' + BOT_IDENT + '\r\n')
bot.send('USER ' + BOT_IDENT + ' ' + gethostname() + ' ' + BOT_HOST + ' :' + BOT_IDENT + '\r\n')
while (bot.recv(1024).find('376 ' + BOT_IDENT) == -1):
pass
bot.send('JOIN ' + BOT_CHANS + '\r\n')
except:
print "Could not connect, try changing bot nick..."
sys.exit(1)
# BOUCLE PRINCIPALE
while 1:
# RECEPTION D'UNE LIGNE DU SERVEUR
match = re.search('(?m)^PING.+|:([^!]+)!([^\s]+)\s+(\w+)\s+(#?\w+)?\s*([^:]+)?:?([^\r]*)', bot.recv(1024))
if match is not None:
nick, id, command, source, arg, text = match.groups(5)
# PING PONG
if match.group(0).find('PING') == 0:
bot.send('PONG ' + BOT_IDENT + '\r\n')
else:
# COMMANDES PUBLIQUES
if command == 'PRIVMSG':
# COMMANDES DE L'OP
if re.search(OP, id) is not None:
match = re.search('^' + BOT_IDENT + ', (.*)$', text)
if match is not None:
command = match.group(1)
if command == 'quit':
bot.send("QUIT :Je le savais bien que t'étais une salope !!\r\n")
bot.close()
break
# COMMANDES TIERS
match = re.search('^' + BOT_IDENT + ', (parle)\s?(.*)$', text)
if match is not None:
command, arguments = match.groups(2)
# ORDRE DE PARLER
if command == 'parle':
quote(('#' in source and source or nick))
print "I've been forced to talk by", nick, "..."
# CITATION ALEATOIRE
elif chr(int(re.search('0\.(\d\d)', str(Random().random())).group(1))) in nick.upper():
quote(('#' in source and source or nick))
print "I've randomly quoted on", nick, "talk..."
# REJOINDRE LES CHANS
elif command == 'PART':
if text == BOT_IDENT and nick == BOT_IDENT:
bot.send('JOIN ' + source + '\r\n')
print "Rejoining chan", source, "..."
# REJOINDRE APRES UN KICK
elif command == 'KICK':
if arg.strip() == BOT_IDENT:
bot.send('JOIN ' + source + '\r\n')
print "Kicked, rejoining chan", source, "..."
# end of nyctalope.py
|
--------------------
|