Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #VERSION 0.2.2
- import socket
- import threading
- import time
- import random
- import sys
- import md5
- HOSTIP = "130.111.39.241"
- HOSTPORT = 25565
- SPAMTIMER = .85
- SPAMLENGTH = 81
- MD5PASS = "8226a96f304d1c92f5d1a8cb7f0fcbdc"
- class Queue:
- def __init__(self, items):
- self.items = items
- self.modifying = False
- def pop(self):
- while self.modifying:
- time.sleep(random.randint(1,5)*.1)
- self.modifying = True
- #Will raise IndexError if queue is empty
- myitem = self.items[0]
- self.items.remove(myitem)
- self.modifying = False
- return myitem
- def push(self, item):
- while self.modifying:
- time.sleep(random.randint(1,5)*.1)
- self.modifying = True
- self.items.append(item)
- self.modifying = False
- def isEmpty(self):
- if len(self.items) > 0:
- return False
- return True
- class Server:
- def __init__(self):
- self.toRemove = []
- self.connections = []
- self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- self.removing = False
- self.q = Queue([])
- self.online = True
- def start(self):
- print "Starting chat server on "+HOSTIP+":"+str(HOSTPORT)
- try:
- threading.Thread(target=self.sendLoop).start()
- self.s.bind((HOSTIP,HOSTPORT))
- self.s.listen(10)
- while self.online:
- (c,addr) = self.s.accept()
- username = c.recv(1024).strip()
- if (" " not in username) and username != "" and len(self.findByUsername(username)) == 0 and len(username) <= 15:
- while self.removing:
- time.sleep(.1)
- client = [username,addr,c]
- self.connections.append(client)
- threading.Thread(target=self.getMessage,args=(client,)).start()
- threading.Thread(target=self.serverSend,args=("["+username+":"+addr[0]+"] has joined.",)).start()
- else:
- c.close()
- except KeyboardInterrupt:
- self.online = False
- sys.exit(0)
- def serverSend(self, message):
- self.q.push([["Server","127.0.0.1",""],message])
- def getMessage(self, client):
- while self.online:
- before = time.time()
- #quip is a list [fullClientData,actualMessage]
- quip = [client,""]
- quip[1] = client[2].recv(1024)
- after=time.time()
- if quip[1].strip() != "":
- if after-before > SPAMTIMER and len(self.findByUsername(client[0])) > 0 and len(quip[1]) < SPAMLENGTH:
- print "got message:"+client[0]+":"+quip[1]
- self.q.push(quip)
- else:
- while self.removing:
- time.sleep(1)
- self.toRemove.append(client)
- def isWhisper(self, message):
- if message.startswith("/msg") or message.startswith("/tell") or message.startswith("/whisper") or message.startswith("/message"):
- return True
- def pushMessage(self):
- quip = self.q.pop()
- print "pushing new message!"
- if quip[1].startswith("/"):
- if quip[1].startswith("/help"):
- threading.Thread(target=self.cmdHelp,args=(quip,)).start()
- elif quip[1].startswith("/online"):
- threading.Thread(target=self.cmdOnline,args=(quip,)).start()
- elif self.isWhisper(quip[1]):
- threading.Thread(target=self.cmdWhisper,args=(quip,)).start()
- elif quip[1].startswith("/quit"):
- quip[0][2].close()
- self.toRemove.append(quip[0])
- elif quip[1].startswith("/kick"):
- threading.Thread(target=self.cmdKick,args=(quip,)).start()
- else:
- quip[1] = quip[0][0]+": "+quip[1]
- for client in self.connections:
- threading.Thread(target=self.sendMessage,args=(quip, client)).start()
- def sendLoop(self):
- while self.online:
- try:
- self.pushMessage()
- except IndexError:
- self.q.modifying = False
- if len(self.toRemove) > 0:
- while self.removing:
- time.sleep(.1)
- self.removeOld()
- time.sleep(.05)
- def removeOld(self):
- self.removing = True
- for client in self.toRemove:
- try:
- client[2].close()
- self.connections.remove(client)
- threading.Thread(target=self.serverSend,args=("["+client[0]+":"+client[1][0]+"] has disconnected.",)).start()
- except:
- pass
- self.removing = False
- def sendMessage(self, quip, client):
- try:
- fromuser = quip[0][0]
- client[2].send(quip[1])
- except:
- while self.removing:
- time.sleep(1)
- self.toRemove.append(client)
- def findByUsername(self,username):
- c = []
- for client in self.connections:
- if client[0] == username:
- return client
- return c
- def cmdOnline(self, quip):
- fromclient = quip[0]
- users = []
- for client in self.connections:
- users.append(client[0])
- quip[1] = "\nOnline:\n -"+"\n -".join(users)
- self.sendMessage(quip,quip[0])
- def cmdWhisper(self, quip):
- fromclient = quip[0]
- args = quip[1].split(" ")
- if len(args) < 3:
- return
- else:
- toclient = self.findByUsername(args[1])
- if len(toclient) > 0:
- quip[1] = "[Whisper from "+fromclient[0]+"]: " + " ".join(args[2:])
- self.sendMessage(quip,toclient)
- def cmdKick(self,quip):
- args = quip[1].split(" ")
- if len(args) == 3:
- password = args[1]
- tokick = self.findByUsername(args[2])
- hasher = md5.new()
- hasher.update(password)
- hex = hasher.hexdigest()
- if hex == MD5PASS:
- self.serverSend("["+args[2]+"] has been kicked.")
- quip[1] = "/quit"
- self.sendMessage(quip,tokick)
- while self.removing:
- time.sleep(1)
- self.toRemove.append(tokick)
- def cmdHelp(self,quip):
- tosend = quip[0]
- f = open("help.txt","r")
- data = f.readline()
- while data != "":
- quip[1] = data
- self.sendMessage(quip,tosend)
- data = f.readline()
- f.close()
- serv = Server()
- serv.start()
- #VERSION 0.1.3
- import socket
- import time
- import threading
- class Client:
- def __init__(self, username, ip, port):
- self.username = username.strip()
- if username == "" or len(username) > 15:
- return
- self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- try:
- self.socket.connect((ip,port))
- self.socket.send(username)
- self.online = True
- except:
- print "couldn't connect to "+ip+":"+str(port)
- self.online = False
- def send(self):
- while self.online:
- try:
- mymsg = raw_input(self.username+": ")
- if not mymsg.startswith("/quit"):
- if mymsg.strip() != "":
- self.socket.send(mymsg)
- else:
- self.socket.send(mymsg)
- self.online = False
- self.socket.close()
- except:
- self.online = False
- self.socket.close()
- def recv(self):
- while self.online:
- try:
- mymsg = self.socket.recv(1024)
- if mymsg == "/quit":
- self.socket.close()
- self.online = False
- elif not mymsg.startswith(self.username) and not mymsg.strip() == "":
- print mymsg
- except:
- self.online = False
- self.socket.close()
- username = raw_input("Enter username (max 15 chars): ")
- ip = raw_input("Enter ip: ")
- port = input("Enter port: ")
- connection = Client(username,ip,port)
- threading.Thread(target=connection.send).start()
- threading.Thread(target=connection.recv).start()
- ####HELP#####
- {} Signifies a collection of aliases
- [] signifies a placeholder variable, to be replaced with actual info
- /help - show this help menu
- /online - shows online users
- /{msg|tell|whisper|message} [username] [message] - sends a message that only [username] will see
- /quit - is supposed to disconnect, but only works if you do Ctrl-C,/quit,enter
- /kick [password] [username] - kicks a user, but only if you know the special password!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement