summaryrefslogtreecommitdiff
path: root/python_client.py
blob: 39411c55819bc052fa65972be082b44b23deb5c7 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# Yes, this source code is terrible.
# It's just something I've put together to test the server
# before I write a *real* client.

import sys, socket, ssl, threading, struct
from PyQt5 import QtCore, QtGui, QtWidgets

protocolVer = 1
sock = None
authed = False
sessionKey = b'\0'*16
nextID = 1
lastReceivedPacketID = 0
packetCache = []
packetLock = threading.Lock()

u32 = struct.Struct('<I')

class Packet:
	def __init__(self, type, data):
		global nextID

		self.type = type
		self.data = data
		if (type & 0x8000) == 0:
			self.id = nextID
			nextID = nextID + 1

	def sendOverWire(self, sock):
		header = struct.pack('<HHI', self.type, 0, len(self.data))
		if (self.type & 0x8000) == 0:
			extHeader = struct.pack('<II', self.id, lastReceivedPacketID)
		else:
			extHeader = b''

		sock.sendall(header)
		if extHeader:
			sock.sendall(extHeader)
		sock.sendall(self.data)


def clearCachedPackets(pid):
	for packet in packetCache[:]:
		if packet.id <= pid:
			packetCache.remove(packet)

def reader():
	global lastReceivedPacketID, authed, sessionKey
	readbuf = b''

	sockCopy = sock

	print('(Connected)')
	while True:
		data = sockCopy.recv(1024)
		if not data:
			print('(Disconnected)')
			break

		readbuf += data

		pos = 0
		bufsize = len(readbuf)
		print('[bufsize: %d]' % bufsize)
		while True:
			if (pos + 8) > bufsize:
				break

			type, reserved, size = struct.unpack_from('<HHI', readbuf, pos)

			extHeaderSize = 8 if ((type & 0x8000) == 0) else 0
			if (pos + 8 + extHeaderSize + size) > bufsize:
				break

			pos += 8
			with packetLock:
				if ((type & 0x8000) == 0):
					pid, lastReceivedByServer = struct.unpack_from('<II', readbuf, pos)
					pos += 8

					lastReceivedPacketID = pid
					clearCachedPackets(lastReceivedByServer)

				packetdata = readbuf[pos:pos+size]
				print('0x%x : %d bytes : %s' % (type, size, packetdata))

				if type == 0x8001:
					sessionKey = packetdata
					authed = True
				elif type == 0x8002:
					print('FAILED!')
				elif type == 0x8003:
					authed = True
					pid = u32.unpack(packetdata)[0]
					clearCachedPackets(pid)
					try:
						for packet in packetCache:
							packet.sendOverWire(sockCopy)
					except:
						pass
				else:
					# Horrible kludge. I'm sorry.
					# I didn't feel like rewriting this to use
					# QObject and QThread. :(
					packetEvent = PacketEvent(type, packetdata)
					app.postEvent(mainwin, packetEvent)

			pos += size

		print('[processed %d bytes]' % pos)
		readbuf = readbuf[pos:]

def writePacket(type, data, allowUnauthed=False):
	packet = Packet(type, data)
	if (type & 0x8000) == 0:
		packetCache.append(packet)
	try:
		if authed or allowUnauthed:
			packet.sendOverWire(sock)
	except:
		pass


class PacketEvent(QtCore.QEvent):
	def __init__(self, ptype, pdata):
		QtCore.QEvent.__init__(self, QtCore.QEvent.User)
		self.packetType = ptype
		self.packetData = pdata


class WindowTab(QtWidgets.QWidget):
	def __init__(self, parent=None):
		QtWidgets.QWidget.__init__(self, parent)

		self.output = QtWidgets.QTextEdit(self)
		self.output.setReadOnly(True)
		self.input = QtWidgets.QLineEdit(self)
		self.input.returnPressed.connect(self.handleLineEntered)

	def makeLayout(self):
		layout = QtWidgets.QVBoxLayout(self)
		layout.addWidget(self.output)
		layout.addWidget(self.input)

	enteredMessage = QtCore.pyqtSignal(str)
	def handleLineEntered(self):
		line = self.input.text()
		self.input.setText('')

		self.enteredMessage.emit(line)

	def pushMessage(self, msg):
		cursor = self.output.textCursor()

		isAtEnd = cursor.atEnd()
		cursor.movePosition(QtGui.QTextCursor.End)
		cursor.clearSelection()
		cursor.insertText(msg)
		cursor.insertText('\n')

		if isAtEnd:
			self.output.setTextCursor(cursor)

class ChannelTab(WindowTab):
	def __init__(self, parent=None):
		WindowTab.__init__(self, parent)

		self.userList = QtWidgets.QListWidget(self)

	def makeLayout(self):
		sublayout = QtWidgets.QVBoxLayout()
		sublayout.addWidget(self.output)
		sublayout.addWidget(self.input)

		layout = QtWidgets.QHBoxLayout(self)
		layout.addLayout(sublayout)
		layout.addWidget(self.userList)

	def readJunk(self, pdata, pos):
		userCount = u32.unpack_from(pdata, pos)[0]
		pos += 4

		users = []
		for i in range(userCount):
			#prefix = pdata[pos]
			#pos += 1
			nicklen = u32.unpack_from(pdata, pos)[0]
			pos += 4
			nick = pdata[pos:pos+nicklen].decode('utf-8', 'replace')
			pos += nicklen
			modes = u32.unpack_from(pdata, pos)[0]
			pos += 4
			users.append(nick)
			#self.userList.addItem(chr(prefix)+nick)
			self.userList.addItem(nick)

		self.users = users

		topiclen = u32.unpack_from(pdata, pos)[0]
		pos += 4
		self.topic = pdata[pos:pos+topiclen].decode('utf-8', 'replace')
		pos += topiclen

		return pos

	def addUsers(self, pdata):
		userCount = u32.unpack_from(pdata, 4)[0]
		pos = 8

		for i in range(userCount):
			nicklen = u32.unpack_from(pdata, pos)[0]
			pos += 4
			nick = pdata[pos:pos+nicklen].decode('utf-8', 'replace')
			pos += nicklen
			modes = u32.unpack_from(pdata, pos)[0]
			pos += 4
			self.users.append(nick)
			self.userList.addItem(nick)

	def removeUsers(self, pdata):
		userCount = u32.unpack_from(pdata, 4)[0]
		pos = 8
		if userCount == 0:
			self.users = []
			self.userList.clear()
		else:
			for i in range(userCount):
				nicklen = u32.unpack_from(pdata, pos)[0]
				pos += 4
				nick = pdata[pos:pos+nicklen].decode('utf-8', 'replace')
				pos += nicklen
				print('Removing [%s]' % repr(nick))

				self.users.remove(nick)
				items = self.userList.findItems(nick, QtCore.Qt.MatchExactly)
				self.userList.takeItem(self.userList.row(items[0]))
	
	def renameUser(self, pdata):
		pos = 4
		nicklen = u32.unpack_from(pdata, pos)[0]
		pos += 4
		fromnick = pdata[pos:pos+nicklen].decode('utf-8', 'replace')
		pos += nicklen
		nicklen = u32.unpack_from(pdata, pos)[0]
		pos += 4
		tonick = pdata[pos:pos+nicklen].decode('utf-8', 'replace')

		try:
			idx = self.users.index(fromnick)
		except ValueError:
			self.pushMessage('Crap, [%s] was not found in the users list!' % fromnick)
			return

		self.users[idx] = tonick

		items = self.userList.findItems(fromnick, QtCore.Qt.MatchExactly)
		items[0].setText(tonick)

	def changeUserMode(self, pdata):
		# boop
		pass


class MainWindow(QtWidgets.QMainWindow):
	def __init__(self, parent=None):
		QtWidgets.QMainWindow.__init__(self, parent)

		self.setWindowTitle('Ninjifox\'s IRC Client Test')

		tb = self.addToolBar('Main')
		tb.addAction('Connect', self.handleConnect)
		tb.addAction('Disconnect', self.handleDisconnect)
		tb.addAction('Login', self.handleLogin)

		self.tabs = QtWidgets.QTabWidget(self)
		self.tabLookup = {}
		self.setCentralWidget(self.tabs)

		self.debugTab = WindowTab(self)
		self.debugTab.makeLayout()
		self.debugTab.enteredMessage.connect(self.handleDebug)
		self.tabs.addTab(self.debugTab, 'Debug')

	def event(self, event):
		if event.type() == QtCore.QEvent.User:
			event.accept()

			ptype = event.packetType
			pdata = event.packetData

			if ptype == 1:
				strlen = u32.unpack_from(pdata, 0)[0]
				msg = pdata[4:4+strlen].decode('utf-8', 'replace')
				self.debugTab.pushMessage(msg)
			elif ptype == 0x100:
				# ADD WINDOWS
				wndCount = u32.unpack_from(pdata, 0)[0]
				pos = 4

				for i in range(wndCount):
					wtype, wid, wtlen = struct.unpack_from('<III', pdata, pos)
					pos += 12
					wtitle = pdata[pos:pos+wtlen].decode('utf-8', 'replace')
					pos += wtlen
					msgCount = u32.unpack_from(pdata, pos)[0]
					pos += 4
					msgs = []
					for j in range(msgCount):
						msglen = u32.unpack_from(pdata, pos)[0]
						pos += 4
						msg = pdata[pos:pos+msglen].decode('utf-8', 'replace')
						pos += msglen
						msgs.append(msg)

					if wtype == 1:
						tab = WindowTab(self)
					elif wtype == 2:
						tab = ChannelTab(self)
						pos = tab.readJunk(pdata, pos)

					tab.makeLayout()
					tab.winID = wid
					tab.enteredMessage.connect(self.handleWindowInput)
					self.tabs.addTab(tab, wtitle)
					self.tabLookup[wid] = tab
					tab.pushMessage('\n'.join(msgs))
			elif ptype == 0x102:
				# WINDOW MESSAGES
				wndID, msglen = struct.unpack_from('<II', pdata, 0)
				msg = pdata[8:8+msglen].decode('utf-8', 'replace')
				self.tabLookup[wndID].pushMessage(msg)

			elif ptype == 0x120:
				# Add users to channel
				wndID = u32.unpack_from(pdata, 0)[0]
				self.tabLookup[wndID].addUsers(pdata)
			elif ptype == 0x121:
				# Remove users from channel
				wndID = u32.unpack_from(pdata, 0)[0]
				self.tabLookup[wndID].removeUsers(pdata)
			elif ptype == 0x122:
				# Rename user in channel
				wndID = u32.unpack_from(pdata, 0)[0]
				self.tabLookup[wndID].renameUser(pdata)
			elif ptype == 0x123:
				# Change user modes in channel
				wndID = u32.unpack_from(pdata, 0)[0]
				self.tabLookup[wndID].changeUserMode(pdata)

			return True
		else:
			return QtWidgets.QMainWindow.event(self, event)

	def handleConnect(self):
		global sock
		try:
			basesock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
			basesock.connect(('localhost', 5454))
			#sock = ssl.wrap_socket(basesock)
			sock = basesock
			thd = threading.Thread(None, reader)
			thd.daemon = True
			thd.start()
		except Exception as e:
			print(e)

	def handleDisconnect(self):
		global sock, authed
		sock.shutdown(socket.SHUT_RDWR)
		sock.close()
		sock = None
		authed = False

	def handleLogin(self):
		writePacket(0x8001, struct.pack('<II 16s', protocolVer, lastReceivedPacketID, sessionKey), True)

	def handleDebug(self, text):
		with packetLock:
			data = str(text).encode('utf-8')
			writePacket(1, struct.pack('<I', len(data)) + data)

	def handleWindowInput(self, text):
		wid = self.sender().winID
		with packetLock:
			data = str(text).encode('utf-8')
			writePacket(0x102, struct.pack('<II', wid, len(data)) + data)


app = QtWidgets.QApplication(sys.argv)

mainwin = MainWindow()
mainwin.show()

app.exec_()