-
Notifications
You must be signed in to change notification settings - Fork 2
/
minion.py
executable file
·92 lines (81 loc) · 3.32 KB
/
minion.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
92
import socket, os, re, hashlib, threading
class SocketManager(threading.Thread):
def __init__(self, minion):
threading.Thread.__init__(self)
self.minion=minion
self.address='\0'+self.minion.pid+'|'+self.minion.channelHash
self.initPeers()
self.sock=socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
self.sock.bind(self.address)
def run(self):
while 1:
incomingMessage = self.sock.recvfrom(4096)
self.handlerGetComm(incomingMessage[0])
def handlerGetComm(self, incMess):
if incMess[0] == "/":
if incMess[1:4] == "get":
self.handlerGetNick(incMess[5:])
if incMess[1:4] == "add":
self.handlerAddPeer(incMess[5:])
if incMess[1:4] == "rem":
self.handlerRemPeer(incMess[5:])
if incMess[1:4] == "msg":
self.handlerGetMess(incMess[5:])
def handlerAddPeer(self, mess):
pid, name = mess.split("|")
self.peers.append(pid)
self.minion.screen.setTitle(self.minion.channel, len(self.peers))
self.minion.screen.printMessage(name+" joined.")
def handlerRemPeer(self, mess):
pid, name, reason = mess.split("|")
try:
self.peers.remove(pid)
self.minion.screen.setTitle(self.minion.channel, len(self.peers))
self.minion.screen.printMessage(name+" leaved. (Reason:"+reason+")")
except:
pass
def handlerGetMess(self, message):
self.minion.screen.printMessage(str(message))
def handlerGetNick(self, pid):
mess="/msg "+self.minion.nickname+" "+self.minion.pid
if self.minion.afk == True :
mess = mess+" <AFK>"
self.minion.sendMessageTo(mess, pid)
def initPeers(self):
self.peers=[]
expr = re.compile(r'.*%s.*' % self.minion.channelHash)
for line in open("/proc/net/unix"):
match = expr.search(line)
if match:
matchLine = match.group()
matchLine = matchLine.split("@", 1)
matchLine = matchLine[1]
matchLine = matchLine.split("|", 3)
self.peers.append(matchLine[0])
class Minion:
def __init__(self, channel, scr, nickname):
self.pid=str(os.getpid())
self.screen=scr
self.channel=channel
self.channelHash=hashlib.sha256(self.channel).hexdigest()
self.nickname=nickname
self.afk=False
self.mySocket=SocketManager(self)
self.mySocket.setDaemon(True)
self.mySocket.start()
message="/add "+self.pid+"|"+nickname
self.sendMessage(message)
def sendMessageTo(self, outMessage, peerPid, failed=0):
peerSock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
peerAddress = '\0'+str(peerPid)+'|'+self.channelHash
try :
peerSock.sendto(outMessage, peerAddress)
except :
if failed >= 5:
self.mySocket.handlerRemPeer(peerPid+"|Somebody("+peerPid+")|DeliveryError")
return
failed += 1
self.sendMessageTo(outMessage, peerPid, failed)
def sendMessage(self, outMessage):
for peerPid in self.mySocket.peers:
self.sendMessageTo(outMessage, peerPid)