This repository has been archived by the owner on Apr 22, 2019. It is now read-only.
/
client.py
452 lines (427 loc) · 20.2 KB
/
client.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import tkinter as tk
from tkinter.scrolledtext import ScrolledText
import socket
import os
import sys
import random
import time
import _thread as thread
import easygui
easygui.msgbox("""XeIRC is a community-driven project by Damian Heaton (www.damianheaton.com)
Please see www.xeirc.xyz for help, information, and XeIRC's source code.
XeIRC's source code can also be found with the compiled .exe files for XeIRC:
- If this is a zipped release version, then the source will be a file called
"client.py" found where you extracted XeIRC to.
- If this is an installed version of XeIRC, navigate to the installation directory;
the source is a file called "client.py".
- If you're runnning this from source, you know exactly where the source is. :)
XeIRC IRC Client Copyright (C) 2016 Damian Heaton
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. Please see the GNU General Public License
version 3 for more information.""")
class ServerDetails(easygui.EgStore):
def __init__(self, filename):
self.nickname = "XeIRCUser"
self.ip = "chat.freenode.com"
self.port = 6667
self.channel = "#xeirc"
self.password = ""
self.filename = filename
self.restore()
connSettings = ServerDetails("conn.xe")
pre = [
connSettings.nickname,
connSettings.ip,
connSettings.port,
connSettings.channel,
connSettings.password
]
details = easygui.multpasswordbox("Please enter IRC Connection Details. \
Settings marked with an asterisk (*) are required.",
"Connect to IRC.", ["Your Nickname (*)",
"Server Address (*)",
"Server Port (*)",
"Channel (*)",
"NickServ Password"],
pre)
if details is None:
sys.exit(0)
connSettings.nickname = details[0]
connSettings.ip = details[1]
connSettings.port = int(details[2])
connSettings.channel = details[3]
connSettings.password = details[4]
connSettings.store()
botnick = connSettings.nickname
server = connSettings.ip
port = connSettings.port
password = connSettings.password
if password == "":
nickserv = False
else:
nickserv = True
global primary
primary = [connSettings.channel]
secondary = []
class CommandHelp():
"""
This class keeps records of the help information of each command and will
provide formatted responses as needed by the /help command.
"""
def __init__(self):
self.commands = {}
self.extHelp = {}
def addCommand(self, cmdName, cmdFormat, cmdShortHelp,
cmdLongHelp):
self.commands[cmdName] = [cmdFormat, cmdShortHelp]
self.extHelp[cmdName] = cmdLongHelp
def listCommands(self):
entries = []
for command in self.commands:
cmdEntry = ("* " + self.commands[command][0]
+ " :: " + self.commands[command][1])
entries.append(cmdEntry)
return entries
def cmdHelp(self, command):
try:
helpEntry = ["Help for command \"" + command + "\":",
"Format: " + self.commands[command][0],
"Description: " + self.extHelp[command]]
return helpEntry
except IndexError:
raise ValueError("The command \"" + command + "\" does not exist.")
cmdHelp = CommandHelp()
cmdHelp.addCommand("@", "@<messageid>|<response text>", "This will quote a \
specific message, allowing you to respond clearly.", """@ notation will allow \
you to respond to the message which is identified by messageid with your \
response text. Messageids are shown in brackets ([]) at the beginning of \
logged messages. Some messages (such as system/client messages) are not \
assigned messageids, and so cannot be quoted with @ notation.""")
cmdHelp.addCommand("/showchan", "/showchan", "This will display the \
current channel that you are talking on to you in chat.", """/showchan will \
display the channel that you are currently talking on in chat, similar to \
checking the command list's currently-selected channel (except this one does \
not need a channel selected to show the active channel.)""")
cmdHelp.addCommand("/join", "/join <channel> [password]", "This will join a \
channel with a password if supplied.", """/join will join a channel, using a \
password if you supply one. When you join a channel, you will be able to see \
chat within that channel and talk in the channel. However, /join will not \
automatically set the active channel to the channel that you are joining, and \
so the channel must be manually selected as the active channel in your channel \
list.""")
cmdHelp.addCommand("/quit", "/quit <reason>", "This will quit the server, \
displaying the provided reason along with it.", """/quit will send a QUIT \
request, citing your provided reason, to the connected IRC server. You will \
not be able to chat or see any new messages on the IRC server. Currently, there \
IS NO accompanying /joinserv command to join a server after quitting one.""")
cmdHelp.addCommand("/away", "/away <reason>", "This will set you as away on \
the IRC server, citing the provided reason along with it.", """/away will \
set you as away on the IRC server as a result of the reason provided, alerting \
other users that you will not be able to respond to messages at the moment. It \
will also automatically update your nickname to (your nickname)^away, to show \
that you are away to clients that do not support the AWAY syntax.""")
cmdHelp.addCommand("/back", "/back", "This will undo the effects of the /away \
command.", """/back will mark you as having returned to the IRC server. It \
will also automatically reset your nickname back to its original state, to show \
that you are back to clients that do not support the AWAY syntax.""")
cmdHelp.addCommand("/invite", "/invite <nick> <channel>", "This will invite a user \
to an IRC channel.", """/invite will invite a user to join an IRC channel, additionally \
allowing them to do so if the channel is restricted access, provided that you have \
the rights to do so. It will not necessarily force the user to join the channel.""")
cmdHelp.addCommand("/kick", "/kick <nick>", "This will kick a user from the current \
channel.", """/kick will remove the requested user from the current channel, provided \
that you have the rights to do so on the current channel. Kicks are handled by ChanServ, \
and will not work on a server if ChanServ is not enabled.""")
cmdHelp.addCommand("/names", "/names", "This will display a list of the users currently \
on the active channel.", """/names will display a list of users on the active channel, \
separated by spaces. Ops are represented by an @ next to their name, half-ops shown with &, \
owners shown by ~, and normal users will only show their name.""")
cmdHelp.addCommand("/msg", "/msg <nick> <message>", "This will send a user a private \
message.", """/msg will send the defined user a message, which only they will be able to \
see. They will NOT receive the message if they are offline, due to the volatile nature of \
IRC chat.""")
print("Logging chat to", os.getcwd())
if os.path.isdir("logs") == False:
os.mkdir("logs")
if os.path.isdir("logs/"+botnick) == False:
os.mkdir("logs/"+botnick)
if os.path.isdir("logs/"+botnick+"/"+server) == False:
os.mkdir("logs/"+botnick+"/"+server)
logdir = "logs/"+botnick+"/"+server
msgs = []
global irc
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("connecting to: "+server)
irc.connect((server,port))
irc.send(bytes("USER "+ botnick +" "+ botnick +" "+ botnick +" :"+ botnick +"\n",
"utf-8"))
irc.send(bytes("NICK "+ botnick +"\n", "utf-8"))
text = str(irc.recv(2040))
unraw=text.split("\\r\\n")
for line in unraw:
print(line)
if line.find("PING :") != -1:
irc.send(bytes('PONG :' + line.split(" :")[1].upper() + '\r\n',
"utf-8"))
elif "433" in line and "Nickname is already in use" in line:
easygui.msgbox("Nickname already in use. Trying another..."))
irc.send(bytes("NICK "+ botnick + str(random.randint(1, 100)) + "\n", "utf-8"))
for chan in primary:
irc.send(bytes("JOIN "+ chan +"\n", "utf-8"))
text = str(irc.recv(2040))
unraw=text.split("\\r\\n")
for line in unraw:
print(line)
if line.find("PING :") != -1:
irc.send(bytes('PONG :' + line.split(" :")[1].upper() + '\r\n',
"utf-8"))
if nickserv:
irc.send(bytes("PRIVMSG NickServ :IDENTIFY " + password + "\n",
"utf-8"))
text = str(irc.recv(2040))
unraw=text.split("\\r\\n")
for line in unraw:
print(line)
if line.find("PING :") != -1:
irc.send(bytes('PONG :' + line.split(" :")[1].upper() + '\r\n',
"utf-8"))
for chan in primary:
irc.send(bytes("JOIN "+ chan +"\n", "utf-8"))
print("i")
text = str(irc.recv(2040))
unraw=text.split("\\r\\n")
for line in unraw:
print(line)
if line.find("PING :") != -1:
irc.send(bytes('PONG :' + line.split(" :")[1].upper() + '\r\n',
"utf-8"))
for chan in primary:
irc.send(bytes("JOIN "+ chan +"\n", "utf-8"))
text = str(irc.recv(2040))
unraw=text.split("\\r\\n")
for line in unraw:
print(line)
if line.find("PING :") != -1:
irc.send(bytes('PONG :' + line.split(" :")[1].upper() + '\r\n',
"utf-8"))
root = tk.Tk()
root.title("XeIRC IRC Client")
root.iconbitmap("icon.ico")
frame = tk.Frame(root)
frame.pack(fill='both', expand='yes')
channels = tk.Listbox(frame,
height = 2,
bg = 'black',
fg = "#888478",
highlightthickness=0,
selectbackground="#4f4f4f",
selectforeground="#888478")
channels.pack(fill='x')
channels.insert(tk.END, primary[0])
chatLog = ScrolledText(
master = frame,
wrap = 'word', # wrap text at full words only
width = 25, # characters
height = 10, # text lines
bg = 'black', # background color of edit area
fg = '#0B62ED',
insertbackground = "#E80860",
highlightbackground="#888478",
selectbackground="#E80860",
selectforeground="#0B62ED"
)
# the padx/pady space will form a frame
chatLog.pack(fill='both', expand="yes")
chat = tk.Text(frame, padx=4, height=1, bg="black", fg="#E80860", insertbackground="#0B62ED",
selectbackground="#0B62ED", selectforeground="#E80860", highlightthickness=0)
chat.pack(fill='x', side="bottom")
chatLog.config(state="disabled")
def addchat(text):
chatLog.config(state="normal")
chatLog.insert("insert", text + "\n")
chatLog.see(tk.END)
chatLog.config(state="disabled")
def enterPressed(event):
#addchat(chat.get("1.0",'end-1c'))
sendt = chat.get("1.0",'end-2c')
try:
pars = sendt.split(" ")
except:
pars = [sendt]
try:
channel = str(channels.get(channels.curselection()[0]))
except:
channel = str(channels.get(0))
chat.delete("1.0",'end')
print(sendt)
if "@" in pars[0]:
msgid = sendt.split("|")[0].split("@")[1]
sendt = "\x1d"+msgs[int(msgid)]+"\x1d | " + sendt.split("|")[1]
if pars[0] == "/chan":
channel = sendt.split(" ")[1]
elif pars[0] == "/showchan":
addchat("Currently chatting on " + channel)
elif pars[0] == "/join":
channel = sendt.split(" ")[1]
irc.send(bytes("JOIN "+ sendt.split(" ", 1)[1] +"\n", "utf-8"))
channels.insert(tk.END, channel)
elif pars[0] == "/quit":
irc.send(bytes("QUIT :"+ sendt.split(" ", 1)[1] +"\n", "utf-8"))
elif pars[0] == "/names":
irc.send(bytes("NAMES :"+ channel +"\n", "utf-8"))
elif pars[0] == "/away":
irc.send(bytes("NICK :"+ botnick +"^away\n", "utf-8"))
irc.send(bytes("AWAY :"+ sendt.split(" ", 1)[1] +"\n", "utf-8"))
elif pars[0] == "/back":
irc.send(bytes("AWAY\n", "utf-8"))
irc.send(bytes("NICK :"+ botnick +"\n", "utf-8"))
elif pars[0] == "/invite":
irc.send(bytes("INVITE :"+ sendt.split(" ", 1)[1] + " " + sendt.split(" ", 2)[2] +"\n", "utf-8"))
elif pars[0] == "/kick":
irc.send(bytes("PRIVMSG ChanServ :kick "+ channel +" "+ sendt.split(" ", 1)[1] + " " + sendt.split(" ",
2)
[2] + "\n", "utf-8"))
elif pars[0] == "/msg":
recip = pars[1]
message = pars = sendt.split(" ", 2)[2]
irc.send(bytes("PRIVMSG " + recip + " :" + message + "\n", "utf-8"))
msg = "["+time.strftime("%Y.%m.%d %H:%M:%S")+"] " + botnick + " -> " + recip + ": " + message
msgs.append(msg)
addchat("["+str(msgs.index(msg))+"] "+msgs[msgs.index(msg)])
elif pars[0] == "/help":
if len(pars) == 1:
addchat("Help:")
addchat("Please note that <variables> are required for the command to \
work, and [variables] are optional. For example, to join #channel (which has \
no channel password), you would do /join #channel .")
addchat("To get detailed help on a specific command, do \"/help <command>\"\
, for example, \"/help @\" will provide help on @ notation, and \"/help /join\" will \
provide help on the /join command.")
cmds = cmdHelp.listCommands()
for cmd in cmds:
addchat(cmd)
else:
try:
cmd = cmdHelp.cmdHelp(pars[1])
for line in cmd:
addchat(line)
except ValueError as e:
addchat("[error] " + e)
elif sendt == "":
pass
else:
irc.send(bytes("PRIVMSG "+ channel +" :" + sendt + "\n", "utf-8"))
msg = "["+time.strftime("%Y.%m.%d %H:%M:%S")+"] ["+channel+"] <"+botnick+"> "+sendt
msgs.append(msg)
addchat("["+str(msgs.index(msg))+"] "+msgs[msgs.index(msg)])
root.bind('<Return>', enterPressed)
def getMsgs():
while True:
text = str(irc.recv(2040))
unraw=text.split("\\r\\n")
for line in unraw:
print(line)
try:
chan = line.split(" :")[0].split(" ")[2]
nick = line.split(":")[1].split("!")[0]
text = line.split(":", 2)[2]
pars = text.lower().split(" ")
except:
chan = "unknown channel"
nick = "Service"
if "." in nick:
nick = "Service"
if "401" in line:
addchat("[error] " + text)
elif "372" in line:
addchat("[motd] " + text)
elif "376" in line or "305" in line or "306" in line:
addchat("[sys] " + text)
elif "MODE" in line or "366" in line:
pass
elif "332" in line:
addchat("[channel topic] " + text)
else:
try:
if nick != "Service":
if chan != botnick:
msg = "["+time.strftime("%Y.%m.%d %H:%M:%S")+"] ["+chan+"] <"+nick+"> "+text
if '\\x0' in msg:
msg = msg.replace("\\x0f", "[normal]")
msg = msg.replace("\\x0310", "[turquoise]")
msg = msg.replace("\\x031", "[black]")
msg = msg.replace("\\x032", "[blue]")
msg = msg.replace("\\x033", "[green]")
msg = msg.replace("\\x034", "[red]")
msg = msg.replace("\\x035", "[brown]")
msg = msg.replace("\\x036", "[purple]")
msg = msg.replace("\\x037", "[orange]")
msg = msg.replace("\\x038", "[yellow]")
msg = msg.replace("\\x039", "[light green]")
msg = msg.replace("\\x030", "[white]")
msgs.append(msg)
addchat("["+str(msgs.index(msg))+"] "+msgs[msgs.index(msg)])
file = open(logdir+"/"+chan+".log", "a")
file.write("["+str(msgs.index(msg))+"] "+msgs[msgs.index(msg)] + "\n")
file.close()
if botnick in text:
print("\a", end="")
else:
msg = "["+time.strftime("%Y.%m.%d %H:%M:%S")+"] " + nick + " -> " + botnick + ": " + text
if '\\x0' in msg:
msg = msg.replace("\\x0f", "[normal]")
msg = msg.replace("\\x0310", "[turquoise]")
msg = msg.replace("\\x031", "[black]")
msg = msg.replace("\\x032", "[blue]")
msg = msg.replace("\\x033", "[green]")
msg = msg.replace("\\x034", "[red]")
msg = msg.replace("\\x035", "[brown]")
msg = msg.replace("\\x036", "[purple]")
msg = msg.replace("\\x037", "[orange]")
msg = msg.replace("\\x038", "[yellow]")
msg = msg.replace("\\x039", "[light green]")
msg = msg.replace("\\x030", "[white]")
msgs.append(msg)
addchat("["+str(msgs.index(msg))+"] "+msgs[msgs.index(msg)])
file = open(logdir+"/"+chan+".log", "a")
file.write("["+str(msgs.index(msg))+"] "+msgs[msgs.index(msg)] + "\n")
file.close()
if botnick in text:
print("\a", end="")
except Exception as e:
print(e)
if line.find("PING :") != -1 and nick == "Service":
irc.send(bytes('PONG :' + line.split(" :")[1].upper() + '\r\n', "utf-8"))
elif line.find("JOIN :") != -1:
for chan1 in primary:
irc.send(bytes("JOIN "+ chan1 +"\n", "utf-8"))
for chan1 in secondary:
irc.send(bytes("JOIN "+ chan1 +"\n", "utf-8"))
if nick != botnick:
msg = "["+time.strftime("%Y.%m.%d %H:%M:%S")+"] * "+ line.split(":")[1].split("!")[0] \
+" has joined " + line.split(":")[2] + "."
msgs.append(msg)
addchat("["+str(msgs.index(msg))+"] "+msgs[msgs.index(msg)])
elif line.find("PART :") != -1:
if nick != botnick:
msg = "["+time.strftime("%Y.%m.%d %H:%M:%S")+"] ["+chan+"] * "+ line.split(":")[1].split("!")[0] \
+" has left the channel: "+line.split(":", 2)[2]+"."
msgs.append(msg)
addchat("["+str(msgs.index(msg))+"] "+msgs[msgs.index(msg)])
elif line.find("QUIT :") != -1:
if nick != botnick:
msg = "["+time.strftime("%Y.%m.%d %H:%M:%S")+"] ["+chan+"] * "+ line.split(":")[1].split("!")[0] \
+" has left the server: "+line.split(":", 2)[2]+"."
msgs.append(msg)
addchat("["+str(msgs.index(msg))+"] "+msgs[msgs.index(msg)])
elif line.find("NICK :") != -1:
if nick != botnick:
msg = "["+time.strftime("%Y.%m.%d %H:%M:%S")+"] ["+chan+"] * "+ line.split(":")[1].split("!")[0] \
+" has changed their name: "+line.split(":", 2)[2]+"."
msgs.append(msg)
addchat("["+str(msgs.index(msg))+"] "+msgs[msgs.index(msg)])
elif line.find("353") != -1:
addchat("*** People currently on channel: "+text + "\n")
thread.start_new_thread(getMsgs, ())
root.mainloop()