Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- #Script basé sur les logs
- import time
- import ConfigParser
- import socket
- import hashlib
- import threading
- import encodings
- from sys import exit
- import tail as tailer
- import re
- ####
- def __unicode__(self): #Permet les caractéres spéciaux type FR // précéder le message par u // print u"xxx"
- return unicode(self.name)
- def strReplace(str, array): #Permet de remplacer des caractéres par d'autres
- for search, replace in array:
- str = str.replace(search, replace)
- return str
- def send2peers(name, message):
- PEERS_COUNT = int(0)
- while PEERS_COUNT != len(LIST_SERVER.split(',')): # Envoi à tous les peers
- PEER = LIST_SERVER.split(',')[PEERS_COUNT]
- PORT_PEER = PEER.split(':')[1]
- IP_PEER = PEER.split(':')[0]
- sock.sendto(USERNAME + "|-|" + hashlib.sha256(PASSWORD).hexdigest() + "|-|" + TAG + "|-|" + str(name) + "|-|" + str(message), (IP_PEER, int(PORT_PEER)))
- PEERS_COUNT = PEERS_COUNT + 1
- def getplayerlist():
- global getinfomarkeur
- PEERS_COUNT = int(0)
- while PEERS_COUNT != len(LIST_SERVER.split(',')): # Envoi à tous les peers
- time.sleep(0.5)
- getinfomarkeur = 1
- PEER = LIST_SERVER.split(',')[PEERS_COUNT]
- PORT_PEER = PEER.split(':')[1]
- IP_PEER = PEER.split(':')[0]
- sock.sendto("\xFF\xFF\xFF\xFFgetstatus", (IP_PEER, int(PORT_PEER))) # commande
- PEERS_COUNT = PEERS_COUNT + 1
- ###
- #LECTURE LOCAL LOG
- class thread_send(threading.Thread):
- def __init__(self, nom=''):
- threading.Thread.__init__(self)
- self.nom = nom
- self.Terminated = False
- def run(self):
- new_back = "none"
- new_player = int(0)
- player_tab = [-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,
- -1,-1,-1,-1] #INIT tableau 32places de correspondance payers id / names
- for new in tailer.follow(open(JKA_LOG_FILE)):
- if ((new != new_back) and (new != "")):
- try:
- """
- if((players_flag[4]) in new): #Détection du !playerlist pour la liste des joueurs
- getplayerlist()
- """
- if ((message_flag[0]) in new): #Détection d'un message
- #Traitement de la chaine new
- message = strReplace(new, replace_flag_chat).split('||')[2] #Remplacement des flag puis recup message
- #message = strReplace(message, replace_color_flag) #on vire les color_flag
- name = strReplace(new, replace_flag_chat).split('||')[1] #Remplacement des flag puis recup id + player
- name = name[3:] # on dégage l'id inutile
- #name = strReplace(name, replace_color_flag) #on vire les color_flag
- if (message[:5] != "_team"): #Detection du say team (continuer si non)
- send2peers(name, message)
- elif((players_flag[1]) in new): #Détection d'une connexion
- id_newplayer = new[-13:-10] #On chope juste l'id
- id_newplayer = int(strReplace(id_newplayer,replace_flag_players)) #convertion str en int + dégage espace autour
- #print "newplayerID=",id_newplayer
- new_player = 1 #On signale la présence d'une nouvelle connexion pour la ligne suivante !!
- elif (new_player == 1): #Détection nom nouveau player via la précédante lecture
- new_player = 0 #On RAZ la variable
- name_newplayer = new[8:] #On chope juste le nom
- name_newplayer = name_newplayer[:-1] #On jecte le dernier " à la con
- player_tab[id_newplayer] = name_newplayer #Ajout dans liste
- #print player_tab
- message = PLAYER_CO +" "+ USERNAME + "!"
- send2peers(name_newplayer, message) #On indique à tous sa présence !!!
- elif((players_flag[0]) in new): #Détection d'une déconnexion
- id_newplayer = new[-15:-12] #On chope juste l'id
- #print id_newplayer
- player_tab[int(id_newplayer)] = -1 #Zoouup!
- #print player_tab
- message = PLAYER_DISCO +" "+ USERNAME + "!"
- send2peers(name_newplayer, message) #On indique à tous sa présence !!!
- except:
- print "An error has occurred while processing!"
- new_back = new
- def stop(self):
- self.Terminated = True
- #RECEPTION
- class thread_recept(threading.Thread):
- def __init__(self, nom=''):
- threading.Thread.__init__(self)
- self.nom = nom
- self.Terminated = False
- def run(self):
- global getinfomarkeur
- getinfomarkeur = 0
- while True:
- #try:
- data, addr = sock_recu.recvfrom(20000)
- if addr[0] in AUTORISE_IPS_LIST:
- if(getinfomarkeur == 1): # PROBL7ME DE MARKEUR
- players = re.findall(r'"(.*?)(?<!\\)"', strReplace(data, replace_color_flag))
- print data
- sock.sendto("\xFF\xFF\xFF\xFFrcon " + JKA_RCON + " say ^3List of players on " + addr[0] + "(" + str(addr[1]) + ")" + ":^7" + ', '.join(players), (JKA_IP, int(JKA_PORT)))
- getinfomarkeur = 0
- else:
- DATE = int(time.time())
- SERVERNAME_recv = data.split('|-|')[0]
- PASSWORD_recv = data.split('|-|')[1]
- TAG_recv = data.split('|-|')[2]
- NAME_recv = data.split('|-|')[3]
- MESSAGE_recv = data.split('|-|')[4]
- if (hashlib.sha256(PASSWORD).hexdigest() == PASSWORD_recv):
- print u"Received data[",SERVERNAME_recv,"]",NAME_recv,":",MESSAGE_recv
- sock.sendto("\xFF\xFF\xFF\xFFrcon " + JKA_RCON + " svsay ^2[^7" + SERVERNAME_recv + "^2]" + "^2[^7" + NAME_recv + "^2]:" + MESSAGE_recv, (JKA_IP, int(JKA_PORT)))
- else:
- print u"Incorrect password[",SERVERNAME_recv,"]"
- else:
- print u"Unauthorized IP!"
- #except:
- # print u"An error occurred while receiving or processing packet"
- def stop(self):
- self.Terminated = True
- print "------------------------"
- print "-- Programme de Tlams"
- print "-- Version 1.0"
- print "-- Date: 28/09/13"
- print "-- Langage: Python V2.7"
- print u"-- Email: tlams[at]free.fr"
- print u"-- Fonction: Script de chat inter-serveur JKA / JASS MOD"
- print "------------------------"
- print ""
- time.sleep(1)
- #LECTURE DU FICHIER DE CONFIGURATION
- print "Opening configurations file...",
- try:
- config = ConfigParser.RawConfigParser()
- config.read('config.ini')
- print "[OK]"
- except:
- print "[FAILED]"
- print u"Opening the configuration file (config.ini) failed!"
- print "File not available."
- print "Closing..."
- time.sleep(5)
- exit(0)
- time.sleep(1)
- #SCRIPT INFO
- TAG = "JASS_JKA"
- #TRAITEMENT DU FICHIER DE CONFIG
- print u"Reading the configuration file..."
- try:
- JKA_LOG_FILE = config.get('JKA', 'logfile')
- print u"[Info-config]LogFile:",JKA_LOG_FILE
- JKA_RCON = config.get('JKA', 'RCON')
- JKA_IP = config.get('JKA', 'IP_server')
- print u"[Info-config]JKA-ServeurIP:",JKA_IP
- JKA_PORT = config.get('JKA', 'PORT_server')
- print u"[Info-config]JKA-ServeurPORT:",JKA_PORT
- LOCAL_IP = config.get('LOCAL', 'IP')
- print u"[Info-config]Script IP:",LOCAL_IP
- LOCAL_PORT = config.get('LOCAL', 'PORT')
- print u"[Info-config]Script PORT:",LOCAL_PORT
- USERNAME = config.get('LOCAL', 'Username')
- print u"[Info-config]Serveur nom:",USERNAME
- PASSWORD = config.get('LOCAL', 'Password')
- LIST_SERVER = config.get('PEERS', 'List_servers')
- print u"[Info-config]Liste PEERS:",LIST_SERVER
- AUTORISE_IPS = config.get('PEERS', 'IP_Autorise')
- print u"[Info-config]IP autorisés:",AUTORISE_IPS
- PLAYER_CO = config.get('LANG', 'player_co')
- PLAYER_DISCO = config.get('LANG', 'player_disco')
- except:
- print "[FAILED]"
- print "File is corrupt or data are invalid !"
- print "Closing..."
- time.sleep(5)
- exit(0)
- print ""
- time.sleep(1)
- # CREATION DU TABLEAU IP AUTORISES
- AUTORISE_IPS_LIST = []
- nb_ips = int(0)
- while nb_ips != len(AUTORISE_IPS.split(',')):
- AUTORISE_IPS_LIST.append(AUTORISE_IPS.split(',')[nb_ips])
- nb_ips = nb_ips + 1
- #PREPARATION SOCKET UDP
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- sock_recu = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- #CREATION DU SOCK LOCAL
- print "Opening a local sock...",
- try:
- sock_recu.bind((LOCAL_IP, int(LOCAL_PORT)))
- print "[OK]"
- except:
- print "[FAILED]"
- print u"A socket is already open on this port!"
- print "Closing..."
- time.sleep(5)
- exit(0)
- #JKA FLAG
- message_flag = [': say']
- players_flag = ['disconnected','connected:',' Name: ','Client','!playerlist']
- replace_flag_players = [(' ', '')]
- replace_flag_chat = [(': Cmd', '||'), (': say', '||')]
- replace_color_flag = [('^0', ''), ('^1', ''), ('^2', ''), ('^3', ''), ('^4', ''), ('^5', ''), ('^6', ''), ('^7', ''), ('^8', '')]
- #START THREAD
- a = thread_send('envoi_donne')
- a.start()
- b = thread_recept('recept_donne')
- b.start()
- print u"Loading completed !"
- print ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement