-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherk.py
563 lines (484 loc) · 16.7 KB
/
erk.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#
# Erk IRC Client
# Copyright (C) 2019 Daniel Hetrick
# _ _ _
# | | (_) | |
# _ __ _ _| |_ _ ___ | |__
# | '_ \| | | | __| |/ _ \| '_ \
# | | | | |_| | |_| | (_) | |_) |
# |_| |_|\__,_|\__| |\___/|_.__/ _
# | | | | _/ | | |
# | | __ _| |__ |__/_ _ __ __ _| |_ ___ _ __ _ _
# | |/ _` | '_ \ / _ \| '__/ _` | __/ _ \| '__| | | |
# | | (_| | |_) | (_) | | | (_| | || (_) | | | |_| |
# |_|\__,_|_.__/ \___/|_| \__,_|\__\___/|_| \__, |
# __/ |
# |___/
# https://github.com/nutjob-laboratories
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse
import string
import shutil
import sys
import os
from zipfile import ZipFile
import urllib.parse
import posixpath
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtCore
app = QApplication([])
import qt5reactor
qt5reactor.install()
from twisted.internet import reactor
from erk.dialogs import ComboDialogBanner,ScriptEditor
from erk.main import Erk
from erk.files import *
from erk.objects import *
from erk.strings import *
import erk.config
from erk.common import *
#from erk.plugins import PLUGIN_DIRECTORY
# Handle commandline arguments
parser = argparse.ArgumentParser(
prog=f"python {PROGRAM_FILENAME}",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=f''' ___ _
|__ \ _ _| |__ |==============
/ _ | '_| / / | {APPLICATION_NAME} {APPLICATION_VERSION}
\___/|_| |_\_\\ |==============
An open source, cross-platform IRC client
https://github.com/nutjob-laboratories/erk
''',
)
congroup = parser.add_argument_group('Connection')
congroup.add_argument("server", type=str,help="Server to connect to", metavar="SERVER", nargs='?')
congroup.add_argument("port", type=int,help="Server port to connect to (6667)", default=6667, nargs='?', metavar="PORT")
congroup.add_argument( "--ssl", help=f"Use SSL to connect to IRC", action="store_true")
congroup.add_argument( "--reconnect", help=f"Reconnect to servers on disconnection", action="store_true")
congroup.add_argument("-p","--password", type=str,help="Use server password to connect", metavar="PASSWORD", default='')
congroup.add_argument("-c","--channel", type=str,help="Join channel on connection", metavar="CHANNEL[:KEY]", action='append')
congroup.add_argument("-l","--last", help=f"Automatically connect to the last server connected to", action="store_true")
congroup.add_argument("-u","--url", type=str,help="Use an IRC URL to connect", metavar="URL", default='')
congroup.add_argument("-a","--autoscript", help=f"Execute server script on connection (if one exists)", action="store_true")
congroup.add_argument("-s","--script", type=str,help="Execute a custom server script on connection", metavar="FILENAME", action='append')
displaygroup = parser.add_argument_group('Display')
displaygroup.add_argument("-f","--fullscreen", help="Open in fullscreen mode", action="store_true")
displaygroup.add_argument("-o","--ontop", help="Application window is always on top", action="store_true")
displaygroup.add_argument("-W","--width", type=int,help="Set initial window width", default=None, metavar="WIDTH")
displaygroup.add_argument("-H","--height", type=int,help="Set initial window height", default=None, metavar="HEIGHT")
miscgroup = parser.add_argument_group('Configuration')
miscgroup.add_argument("-C","--config", type=str,help="Use an alternate configuration file", metavar="FILE", default=SETTINGS_FILE)
miscgroup.add_argument("-U","--user", type=str,help="Use an alternate user file", metavar="FILE", default=USER_FILE)
miscgroup.add_argument("-Y","--style", type=str,help="Use an alternate text style file", metavar="FILE", default=STYLE_FILE)
miscgroup.add_argument("-L","--logs", type=str,help="Use an alternate log storage location", metavar="DIRECTORY", default=LOG_DIRECTORY)
miscgroup.add_argument("-S","--scripts", type=str,help="Use an alternate script storage location", metavar="DIRECTORY", default=SCRIPTS_DIRECTORY)
miscgroup.add_argument("-T","--styles", type=str,help="Use an alternate style storage location", metavar="DIRECTORY", default=STYLES_DIRECTORY)
miscgroup.add_argument("-M","--macros", type=str,help="Use an alternate macro save file", metavar="FILE", default=MACRO_SAVE_FILE)
miscgroup.add_argument("-X","--export-settings", type=str,help="Export settings to a zip file", metavar="ZIP")
miscgroup.add_argument("-A","--export-all", type=str,help="Export settings and logs to a zip file", metavar="ZIP")
miscgroup.add_argument("-I","--import-settings", type=str,help="Import settings (and logs) from a zip file", metavar="ZIP")
devgroup = parser.add_argument_group('Tools')
devgroup.add_argument("--scripter", help="Open the script editor", action="store_true")
devgroup.add_argument("--scripter-edit", dest="scripted",type=str,help="Open a file in the script editor", metavar="FILE", default='')
disgroup = parser.add_argument_group('Disable functionality')
disgroup.add_argument( "--noask", help=f"Don't ask for a server to connect to on start", action="store_true")
disgroup.add_argument( "--nosettings", help=f"Disable settings menu(s)", action="store_true")
disgroup.add_argument( "--nomenus", help=f"Disable all menus", action="store_true")
disgroup.add_argument( "--noconnect", help=f"Disable connection commands", action="store_true")
disgroup.add_argument( "--noscripts", help=f"Disable scripting", action="store_true")
disgroup.add_argument( "--nodisplay", help=f"Disable connection display", action="store_true")
disgroup.add_argument( "--nostyles", help=f"Disables style loading and editing", action="store_true")
disgroup.add_argument( "--noedit", help=f"Disables the script editor", action="store_true")
disgroup.add_argument( "--qt5menu", help=f"Disable menu toolbar, and use normal menus", action="store_true")
args = parser.parse_args()
loaded_config_file = False
if __name__ == '__main__':
app = QApplication([])
# If the user has passed an alternate configuration file,
# and the file doesn't exist, create a new config file
# where the user indicated, with default values
if args.config:
if not os.path.isfile(args.config):
erk.config.load_settings(args.config)
loaded_config_file = True
print("\""+args.config+"\" created!")
if args.style:
if not os.path.isfile(args.style):
f = get_text_format_settings(None)
write_style_file(f,args.style)
print("\""+args.style+"\" created!")
if args.user:
if not os.path.isfile(args.user):
u = get_user(args.user)
save_user(u,args.user)
print("\""+args.user+"\" created!")
if args.logs:
if not os.path.isdir(args.logs):
os.mkdir(args.logs)
print("\""+args.logs+"\" directory created!")
if args.scripts:
if not os.path.isdir(args.scripts):
os.mkdir(args.scripts)
print("\""+args.scripts+"\" directory created!")
if args.styles:
if not os.path.isdir(args.styles):
os.mkdir(args.styles)
print("\""+args.styles+"\" directory created!")
# Handle exporting settings
if args.export_settings:
outfile = args.export_settings
efl = len("zip")+1
if outfile[-efl:].lower()!=f".zip": outfile = outfile+f".zip"
print("Exporting settings to \""+outfile+"\"...")
zf = ZipFile(outfile, "w")
for dirname, subdirs, files in os.walk(SETTINGS_DIRECTORY):
for fname in files:
zf.write(os.path.join(dirname, fname), os.path.relpath(os.path.join(dirname, fname), os.path.join(SETTINGS_DIRECTORY, '..')))
zf.close()
print("Done!")
sys.exit(0)
# Handle exporting settings and logs
if args.export_all:
outfile = args.export_all
efl = len("zip")+1
if outfile[-efl:].lower()!=f".zip": outfile = outfile+f".zip"
print("Exporting settings to \""+outfile+"\"...")
zf = ZipFile(outfile, "w")
for dirname, subdirs, files in os.walk(SETTINGS_DIRECTORY):
for fname in files:
zf.write(os.path.join(dirname, fname), os.path.relpath(os.path.join(dirname, fname), os.path.join(SETTINGS_DIRECTORY, '..')))
print("Exporting logs to \""+outfile+"\"...")
for dirname, subdirs, files in os.walk(LOG_DIRECTORY):
for fname in files:
zf.write(os.path.join(dirname, fname), os.path.relpath(os.path.join(dirname, fname), os.path.join(LOG_DIRECTORY, '..')))
zf.close()
print("Done!")
sys.exit(0)
# Handle importing settings
if args.import_settings:
file = args.import_settings
if not os.path.isfile(file):
print("\""+file+"\" doesn't exist.")
sys.exit(1)
print("Importing settings from \""+file+"\"...")
with ZipFile(file,'r') as zipObj:
zipObj.extractall(INSTALL_DIRECTORY)
print("Done!")
sys.exit(0)
# Handle launching the script editor
elif args.scripter:
erk.config.load_settings(args.config)
if erk.config.DISPLAY_FONT=='':
id = QFontDatabase.addApplicationFont(DEFAULT_FONT)
_fontstr = QFontDatabase.applicationFontFamilies(id)[0]
font = QFont(_fontstr,9)
else:
f = QFont()
f.fromString(erk.config.DISPLAY_FONT)
font = f
app.setFont(font)
EDITOR = ScriptEditor(None,None,args.config,args.scripts,app)
EDITOR.resize(int(erk.config.DEFAULT_APP_WIDTH),int(erk.config.DEFAULT_APP_HEIGHT))
EDITOR.show()
elif args.scripted:
file = args.scripted
if not os.path.isfile(file):
print("\""+file+"\" doesn't exist. Please use --scripter to create a new file.")
sys.exit(1)
erk.config.load_settings(args.config)
if erk.config.DISPLAY_FONT=='':
id = QFontDatabase.addApplicationFont(DEFAULT_FONT)
_fontstr = QFontDatabase.applicationFontFamilies(id)[0]
font = QFont(_fontstr,9)
else:
f = QFont()
f.fromString(erk.config.DISPLAY_FONT)
font = f
app.setFont(font)
EDITOR = ScriptEditor(file,None,args.config,args.scripts,app)
EDITOR.resize(int(erk.config.DEFAULT_APP_WIDTH),int(erk.config.DEFAULT_APP_HEIGHT))
EDITOR.show()
else:
if not loaded_config_file:
erk.config.load_settings(args.config)
if erk.config.EDITOR_FONT=='':
id = QFontDatabase.addApplicationFont(DEFAULT_FONT)
_fontstr = QFontDatabase.applicationFontFamilies(id)[0]
font = QFont(_fontstr,9)
else:
f = QFont()
f.fromString(erk.config.EDITOR_FONT)
font = f
app.setFont(font)
# Handle disabling connect commands
if args.noconnect: erk.config.DISABLE_CONNECT_COMMANDS = True
# Disable scripting, if it's disabled in the config file
is_scripting_enabled = args.noscripts
if not erk.config.ENABLE_SCRIPTS:
is_scripting_enabled = True
# Handle IRC URLs
if args.url!='':
u = urllib.parse.urlparse(args.url)
if u.scheme=='irc':
if u.password:
args.password = u.password
if u.hostname:
args.server = u.hostname
if u.port:
args.port = u.port
if u.path!='':
p = urllib.parse.unquote(u.path)
p = posixpath.normpath(p)
l = posixpath.split(p)
if len(l)>0:
if l[0]=='/':
if l[1]!='':
c = str(l[1])
if ',' in c:
channel = c.split(',')
if len(channel)==2:
if channel[0][:1]!='#': channel[0] = '#'+channel[0]
if args.channel:
args.channel.append([channel[0],channel[1]])
else:
args.channel = []
args.channel.append([channel[0],channel[1]])
else:
if c[1:]!='#': c = '#'+c
if args.channel:
args.channel.append([c,''])
else:
args.channel = []
args.channel.append([c,''])
# Handle connecting to a server if one has been provided
if args.server:
if args.noscripts:
if args.autoscript:
print("Server script cannot execute: scripting has been disabled")
sys.exit(1)
if args.script!=None:
print("Server script cannot execute: scripting has been disabled")
sys.exit(1)
autoscript = None
if args.autoscript:
autoscript = load_auto_script(args.server,args.port,args.scripts)
if args.script:
for s in args.script:
sfile = find_script_file(s,args.scripts)
if sfile==None:
print("Script not found: "+s)
sys.exit(1)
if os.path.isfile(sfile):
f=open(sfile, "r")
cscript = f.read()
f.close()
if len(cscript)>0:
if cscript[-1]!="\n": cscript = cscript + "\n"
if autoscript!=None:
autoscript = autoscript + cscript
else:
autoscript = cscript
if args.password=='':
pword = None
else:
pword = args.password
chans = []
if args.channel:
for c in args.channel:
if type(c)==list:
chans.append(c)
else:
p = c.split(':')
if len(p)==2:
chans.append(p)
else:
chans.append( [c,''] )
u = get_user(args.user)
i = ConnectInfo(
args.server,
args.port,
pword,
args.ssl,
u["nickname"],
u["alternate"],
u["username"],
u["realname"],
args.reconnect,
chans,
u["failreconnect"],
False,
autoscript,
)
GUI = Erk(
app,
i,
args.nosettings,
args.nomenus,
args.config,
args.style,
args.user,
args.fullscreen,
args.width,
args.height,
args.logs,
is_scripting_enabled,
args.scripts,
args.nodisplay,
args.ontop,
args.qt5menu,
args.styles,
args.nostyles,
args.noedit,
args.macros,
)
GUI.show()
else:
# Handle launching without the connection dialog
if args.noask:
GUI = Erk(
app,
None,
args.nosettings,
args.nomenus,
args.config,
args.style,
args.user,
args.fullscreen,
args.width,
args.height,
args.logs,
is_scripting_enabled,
args.scripts,
args.nodisplay,
args.ontop,
args.qt5menu,
args.styles,
args.nostyles,
args.noedit,
args.macros,
)
GUI.show()
# Handle connecting to the last server
elif args.last:
u = get_user(args.user)
if args.noscripts:
if args.autoscript:
print("Server script cannot execute: scripting has been disabled")
sys.exit(1)
if args.script!=None:
print("Server script cannot execute: scripting has been disabled")
sys.exit(1)
autoscript = None
if args.autoscript:
autoscript = load_auto_script(u["last_server"],u["last_port"],args.scripts)
if args.script:
for s in args.script:
sfile = find_script_file(s,args.scripts)
if sfile==None:
print("Script not found: "+s)
sys.exit(1)
if os.path.isfile(sfile):
f=open(sfile, "r")
cscript = f.read()
f.close()
if len(cscript)>0:
if cscript[-1]!="\n": cscript = cscript + "\n"
if autoscript!=None:
autoscript = autoscript + cscript
else:
autoscript = cscript
if u["last_password"] == '':
pword = None
else:
pword = u["last_password"]
if u["autojoin"]:
c = u["channels"]
else:
c = []
if args.channel:
for ch in args.channel:
p = ch.split(':')
if len(p)==2:
c.append(p)
else:
c.append( [ch,''] )
i = ConnectInfo(
u["last_server"],
int(u["last_port"]),
pword,
u["ssl"],
u["nickname"],
u["alternate"],
u["username"],
u["realname"],
u["reconnect"],
c,
u["failreconnect"],
False,
autoscript,
)
GUI = Erk(
app,
i,
args.nosettings,
args.nomenus,
args.config,
args.style,
args.user,
args.fullscreen,
args.width,
args.height,
args.logs,
is_scripting_enabled,
args.scripts,
args.nodisplay,
args.ontop,
args.qt5menu,
args.styles,
args.nostyles,
args.noedit,
args.macros,
)
GUI.show()
else:
# Launch normally, showing the connection dialog first
info = ComboDialogBanner(args.user,is_scripting_enabled,args.scripts,args.config)
if info!=None:
GUI = Erk(
app,
info,
args.nosettings,
args.nomenus,
args.config,
args.style,
args.user,
args.fullscreen,
args.width,
args.height,
args.logs,
is_scripting_enabled,
args.scripts,
args.nodisplay,
args.ontop,
args.qt5menu,
args.styles,
args.nostyles,
args.noedit,
args.macros,
)
GUI.show()
else:
app.quit()
reactor.run()