This repository has been archived by the owner on Apr 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
chat.py
91 lines (66 loc) · 2.61 KB
/
chat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from __future__ import print_function
from random import randint
import asyncore
import collections
import socket
MAX_MESSAGE_LENGTH = 4096
class RemoteClient(asyncore.dispatcher):
"""Wraps a remote client socket."""
def __init__(self, host, socket, address):
asyncore.dispatcher.__init__(self, socket)
self.host = host
self.outbox = collections.deque()
def say(self, message):
self.outbox.append(message)
def handle_read(self):
client_message = self.recv(MAX_MESSAGE_LENGTH)
self.host.broadcast(client_message)
def handle_write(self):
if not self.outbox:
return
message = self.outbox.popleft()
if len(message) > MAX_MESSAGE_LENGTH:
raise ValueError('Message too long')
self.send(message)
class Client(asyncore.dispatcher):
def __init__(self, host_address, name):
asyncore.dispatcher.__init__(self)
print ('definition your user : (%7s)' % name)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.name = name
print('Connecting to host at server...')
self.connect(host_address)
self.outbox = collections.deque()
def handle_error(self):
print("Server is down.")
exit()
def handle_write(self):
if not self.outbox:
return
message = self.outbox.popleft()
if len(message) > MAX_MESSAGE_LENGTH:
raise ValueError('Message too long')
self.send(message)
def handle_read(self):
message = self.recv(MAX_MESSAGE_LENGTH)
if ["MODE", "JOIN", "QUIT", ".IP", ":v[", ":vHackXTGuard",
"ip.vhack.biz", "#vHackXT", "PING"] in message:
message = message.split(" ")
correcte = ['MODE', "JOIN", "QUIT", ".IP", ":v[",
"admin.vhack.biz", "mod.vhack.biz", ":vHackXTGuard",
"#vHackXT", ":Ping", "PING", ":chat.vhackxt.com",
":chat.vhackxt.com\r\n", "timeout:", "seconds\r\n",
"vip.vhack.biz", "ip.vhack.biz", ":Read", "error"]
for i, list_word in enumerate(message):
for list_match in correcte:
if list_match in list_word:
message.remove(message[i])
if "## Cluster" in message:
message = message.replace("##", "\r\n ##")
print(' '.join(message))
else:
print(message)
if __name__ == '__main__':
print('Creating clients')
client = Client(("164.132.9.247", 15000), 'User_' + str(randint(0, 500)))
asyncore.loop()