forked from fsavje/math-with-slack
-
Notifications
You must be signed in to change notification settings - Fork 28
/
math-with-slack.py
1125 lines (930 loc) · 36 KB
/
math-with-slack.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
################################################################################
# Rendered math (MathJax) with Slack's desktop client
################################################################################
#
# Slack (https://slack.com) does not display rendered math. This script
# injects MathJax (https://www.mathjax.org) into Slack's desktop client,
# which allows you to write nice-looking inline- and display-style math
# using familiar TeX/LaTeX syntax.
#
# https://github.com/fsavje/math-with-slack
#
# MIT License, Copyright 2017-2019 Fredrik Savje
#
################################################################################
# Python 2.7 and 3
from __future__ import print_function
# Load modules
import argparse
import json
import os
import stat
import errno
import platform
import glob
import plistlib
import shutil
import sys
import subprocess
import time
import textwrap
import logging
import math
import struct
import functools
import hashlib
from distutils.version import LooseVersion
try:
# Python 3
import urllib.request as urllib_request
except:
# Python 2
import urllib as urllib_request
# ssl is added for Windows and possibly Mac to avoid ssl
# certificate_verify_failed error
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
import tarfile
import tempfile
# Math with Slack version
mws_version = '0.4.3.0'
# Misc functions
def exprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
sys.exit(1)
def _windows_process_exists(process_name):
call = 'TASKLIST', '/FI', 'imagename eq %s' % process_name
# use buildin check_output right away
output = subprocess.check_output(call).decode()
# check in last line for process name
last_line = output.strip().split('\r\n')[-1]
# because Fail message could be translated
return last_line.lower().startswith(process_name.lower())
def get_platform():
_platform = sys.platform
if _platform == "win32":
return "win32"
elif _platform.startswith("linux"):
return "linux"
elif _platform.startswith("darwin"):
return "darwin"
else:
exprint("Unsupported platform {}".format(_platform))
def _diagnose_permission(ex, app_path):
err_msg = ""
platform = get_platform()
if platform == "win32":
if _windows_process_exists("slack.exe"):
err_msg = ("Possibile fix:\n"
"\tSeems like your Slack is running. "
"Please close Slack and re-run the script.")
elif platform == "linux":
app_path = os.path.normpath(app_path)
app_path_parts = os.path.split(os.sep)
if "snap" in app_path_parts:
err_msg = ("Possibile fix:\n"
"\tSeems like you have a Snap "
"install that this script might not support. "
"Please check README for more details.")
elif platform == "darwin":
if isinstance(ex, IOError) or isinstance(
ex, OSError) and (ex.errno == errno.EPERM or ex.errno == errno.EACCES):
err_msg = (
"Possibile fix:\n"
"\tSeems like you are using MacOS. "
"Perhaps your Slack is installed through App Store?\n"
"\tIf that's the case, "
"you will need to use `sudo` to give the script enough permissions. "
"Please check README for more details.")
return err_msg
def _macos_get_slack_app_root_path(app_path):
return os.path.normpath(os.path.join(app_path, "../../../"))
def check_app_ready(app_path):
plat = get_platform()
if plat == "darwin":
output = subprocess.check_output(
["xattr", _macos_get_slack_app_root_path(app_path)])
if b'com.apple.appstore.metadata' in output:
exprint("Cannot use this script on Slack downloaded from App Store."
" Please re-install from Slack website instead.")
def check_permission(file_path, write=False):
"""Test if the file is readable and/or writable."""
try:
with open(file_path, mode='rb') as check_app_fp:
app_content_bk = check_app_fp.read()
if write:
with open(file_path, mode='wb') as check_app_fp_w:
check_app_fp_w.write(app_content_bk)
except Exception as ex:
print(ex)
diagnosis = _diagnose_permission(ex, file_path)
exprint(("Cannot modify {}. "
"Make sure the script has write permissions. {}").format(
file_path, diagnosis))
def get_backup_path(orig_path):
return orig_path + ".mwsbak"
def make_backup(orig_path):
"""Creates a backup."""
backup_path = get_backup_path(orig_path)
try:
shutil.copy(orig_path, backup_path)
except Exception as ex:
print(ex)
diagnosis = _diagnose_permission(ex, orig_path)
exprint(("Cannot make backup {} -> {}. "
"Make sure the script has write permissions. "
).format(orig_path, backup_path) + diagnosis)
def restore_from_backup(orig_file):
"""Restores backup into orig_file.
Assumes write permission are set on `orig_file`.
"""
backup_path = get_backup_path(orig_file)
if not os.path.isfile(backup_path):
exprint(
"Missing backup at {}. Please re-install Slack.".format(backup_path))
try:
os.remove(orig_file)
shutil.move(backup_path, orig_file)
except Exception as ex:
print(ex)
diagnosis = _diagnose_permission(ex, orig_file)
exprint(("Cannot remove previously modified file at {}. " +
"Make sure the script has write permissions. ").format(orig_file) +
diagnosis)
def remove_backup(orig_file):
backup_path = get_backup_path(orig_file)
if os.path.isfile(backup_path):
try:
os.remove(backup_path)
except Exception as ex:
print(ex)
diagnosis = _diagnose_permission(ex, backup_path)
exprint(
("Cannot remove old backup at {}. "
"Make sure the script has write permissions.").format(backup_path) +
diagnosis)
def macos_codesign_setup(cert, workdir):
"""Setup a certificate in MacOS's keychain."""
out = subprocess.call(
[
"security", "find-certificate", "-Z", "-p", "-c", cert,
"/Library/Keychains/System.keychain"
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if out == 0:
print("Using existing certificate {}".format(cert))
return
cert_tmpl_path = os.path.join(workdir, "cert.tmpl")
with open(cert_tmpl_path, "w+") as f:
f.write(
textwrap.dedent("""\
[ req ]
default_bits = 2048 # RSA key size
encrypt_key = no # Protect private key
default_md = sha512 # MD to use
prompt = no # Prompt for DN
distinguished_name = codesign_dn # DN template
[ codesign_dn ]
commonName = "{}"
[ codesign_reqext ]
keyUsage = critical,digitalSignature
extendedKeyUsage = critical,codeSigning""".format(cert)))
print("Generating and installing {} certificate".format(cert))
cert_path = os.path.join(workdir, "{}.cer".format(cert))
key_path = os.path.join(workdir, "{}.key".format(cert))
subprocess.check_call(
[
"openssl", "req", "-new", "-newkey", "rsa:2048", "-x509", "-days",
"3650", "-nodes", "-config", cert_tmpl_path, "-extensions",
"codesign_reqext", "-batch", "-out", cert_path, "-keyout", key_path
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
subprocess.check_call(
[
"sudo", "security", "add-trusted-cert", "-d", "-r", "trustRoot", "-p",
"codeSign", "-k", "/Library/Keychains/System.keychain", cert_path
],
stdout=subprocess.DEVNULL,
)
subprocess.check_call(
[
"sudo", "security", "import", key_path, "-A", "-k",
"/Library/Keychains/System.keychain"
],
stdout=subprocess.DEVNULL,
)
subprocess.check_call(["sudo", "pkill", "-f", "/usr/libexec/taskgated"],
stdout=subprocess.DEVNULL)
print("Geneterated new certificate {}".format(cert))
def macos_codesign_app(cert, workdir, app_path):
slack_app_path = _macos_get_slack_app_root_path(app_path)
entitlements_path = os.path.join(workdir, "slack-entitlements.xml")
subprocess.check_call(
["codesign", "-d", "--entitlements", entitlements_path, slack_app_path],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
with open(entitlements_path, "rb+") as f:
is_der_file = b"[Dict]" in f.readline()
if is_der_file:
# `codesign` produced a DER file, so scratch that and regenerate with `--xml`.
f.truncate(0)
if is_der_file:
subprocess.check_call(
[
"codesign", "-d", "--entitlements", entitlements_path, "--xml",
slack_app_path
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
subprocess.check_call(
[
"codesign", "--entitlements", entitlements_path, "--deep", "--force",
"--sign", cert, slack_app_path
],
stdout=subprocess.DEVNULL,
)
# end Misc functions
# Asar
def roundup(val, divisor):
return int(math.ceil((float(val) / divisor)) * divisor)
def is_unpacked(fileinfo):
return fileinfo.get('unpacked', False)
def _walk_fileinfos(fileinfos, root=".", ignore_unpacked=False):
for name, fileinfo in fileinfos["files"].items():
sub_path = os.path.join(root, name)
if 'files' in fileinfo:
# is directory
for v in _walk_fileinfos(fileinfo, root=sub_path):
yield v
elif ignore_unpacked and is_unpacked(fileinfo):
continue
else:
yield sub_path, fileinfo
class AsarExtractor:
"""Represents a single *.asar file."""
LOGGER = logging.getLogger("AsarExtractor")
def __init__(self, filename, asarfile, files, baseoffset):
"""Initializes a new instance of the :see AsarExtractor class.
Args:
filename (str):
The path to the *.asar file to read/write from/to.
asarfile (File):
An open *.asar file object.
files (dict):
Dictionary of files contained in the archive.
(The header that was read from the file).
baseoffset (int):
Base offset, indicates where in the file the header ends.
"""
self.filename = filename
self.asarfile = asarfile
self.files = files
self.baseoffset = baseoffset
def extract(self, destination):
"""Extracts the contents of the archive to the specifed directory.
Args:
destination (str):
Path to an empty directory to extract the files to.
"""
if os.path.exists(destination):
raise OSError(20, 'Destination exists', destination)
self.__extract_directory('.', self.files['files'], destination)
def __extract_directory(self, path, files, destination):
"""Extracts a single directory to the specified directory on disk.
Args:
path (str):
Relative (to the root of the archive) path of the directory
to extract.
files (dict):
A dictionary of files from a *.asar file header.
destination (str):
The path to extract the files to.
"""
# assures the destination directory exists
destination_path = os.path.join(destination, path)
if not os.path.exists(destination_path):
os.makedirs(destination_path)
for name, contents in files.items():
item_path = os.path.join(path, name)
# objects that have a 'files' member are directories,
# recurse into them
if 'files' in contents:
self.__extract_directory(item_path, contents['files'], destination)
continue
self.__extract_file(item_path, contents, destination)
def __extract_file(self, path, fileinfo, destination):
"""Extracts the specified file to the specified destination.
Args:
path (str):
Relative (to the root of the archive) path of the
file to extract.
fileinfo (dict):
Dictionary containing the offset and size of the file
(Extracted from the header).
destination (str):
Directory to extract the archive to.
"""
if is_unpacked(fileinfo):
self.__copy_unpacked(path, destination)
return
self.asarfile.seek(self.__absolute_offset(fileinfo['offset']))
# TODO: read in chunks, ain't going to read multiple GB's in memory
contents = self.asarfile.read(fileinfo['size'])
destination_path = os.path.join(destination, path)
with open(destination_path, 'wb') as fp:
fp.write(contents)
if sys.platform != 'win32' and fileinfo.get('executable', False):
os.chmod(destination_path,
os.stat(destination_path).st_mode | stat.S_IEXEC)
self.LOGGER.debug('Extracted %s to %s', path, destination_path)
def __copy_unpacked(self, path, destination):
"""Copies a file that was already extracted to the destination directory.
Args:
path (str):
Relative (to the root of the archive) of the file to copy.
destination (str):
Directory to extract the archive to.
"""
unpacked_dir = self.filename + '.unpacked'
if not os.path.isdir(unpacked_dir):
self.LOGGER.warn('Failed to copy extracted file %s, no extracted dir',
path)
return
source_path = os.path.join(unpacked_dir, path)
if not os.path.exists(source_path):
self.LOGGER.warn('Failed to copy extracted file %s, does not exist', path)
return
destination_path = os.path.join(destination, path)
shutil.copyfile(source_path, destination_path)
def __absolute_offset(self, offset):
"""Converts the specified relative offset into an absolute offset.
Offsets specified in the header are relative to the end of the header.
Args:
offset (int):
The relative offset to convert to an absolute offset.
Returns (int):
The specified relative offset as an absolute offset.
"""
return int(offset) + self.baseoffset
def get_unpackeds(self):
"""Gets all the unpacked files."""
for path, fileinfo in _walk_fileinfos(self.files, root="."):
if is_unpacked(fileinfo):
yield os.path.relpath(path, ".")
def __enter__(self):
"""When the `with` statements opens."""
return self
def __exit__(self, type, value, traceback):
"""When the `with` statement ends."""
if not self.asarfile:
return
self.asarfile.close()
self.asarfile = None
@classmethod
def open(cls, filename):
"""Opens a *.asar file and constructs a new :see AsarArchive instance.
Args:
filename (str):
Path to the *.asar file to open for reading.
Returns (AsarArchive):
An insance of of the :AsarArchive class or None if reading failed.
"""
asarfile = open(filename, 'rb')
(header_data_size, json_binary_size, json_data_size,
json_string_size) = struct.unpack('<4I', asarfile.read(16))
assert header_data_size == 4
assert json_binary_size == json_data_size + 4
json_header_bytes = asarfile.read(json_string_size).decode('utf-8')
files = json.loads(json_header_bytes)
baseoffset = roundup(16 + json_string_size, 4)
return cls(filename, asarfile, files, baseoffset)
class AsarPacker:
BLOCK_SIZE = 4 * 1024 * 1024
def __dir_to_fileinfos(self, directory, unpackeds=tuple()):
fileinfos = {'.': {'files': {}}}
offset = 0
for root, subdirs, files in os.walk(directory, topdown=True):
parts = os.path.normpath(os.path.relpath(root,
directory)).split(os.path.sep)
if root != directory:
parts = ['.'] + parts
dirinfo = functools.reduce(lambda dirinfo, part: dirinfo[part]['files'],
parts, fileinfos)
for file in files:
file_path = os.path.join(root, file)
file_size = os.path.getsize(file_path)
if os.path.relpath(file_path, directory) in unpackeds:
fileinfo = {'size': file_size, 'unpacked': True}
else:
fileinfo = {'size': file_size, 'offset': str(offset)}
offset += file_size
if sys.platform != 'win32' and (os.stat(file_path).st_mode & 0o100):
fileinfo['executable'] = True
dirinfo[file] = fileinfo
for subdir in subdirs:
dirinfo[subdir] = {'files': {}}
fileinfos = fileinfos['.']
self.__add_file_integrities(directory, fileinfos)
return fileinfos
def pack(self, directory, out_asar, unpackeds=tuple()):
"""Pack a directory into an ASAR file.
Args:
directory (str): the directory to pack.
out_asarfile (str): the output asar file path.
unpackeds: (Tuple[str]): a list of files to ignore during packing.
Returns:
the hash of the header.
"""
with open(out_asar, "wb") as out_asarfile:
fileinfos = self.__dir_to_fileinfos(directory, unpackeds=unpackeds)
json_header = json.dumps(fileinfos, sort_keys=True, separators=(',', ':'))
json_header_bytes = json_header.encode('utf-8')
header_string_size = len(json_header_bytes)
data_size = 4
aligned_size = roundup(header_string_size, data_size)
header_size = aligned_size + 8
header_object_size = aligned_size + data_size
out_asarfile.seek(0)
out_asarfile.write(
struct.pack('<4I', data_size, header_size, header_object_size,
header_string_size))
out_asarfile.write(json_header_bytes + b'\0' *
(aligned_size - header_string_size))
baseoffset = roundup(header_string_size + 16, 4)
for path, fileinfo in _walk_fileinfos(fileinfos, ignore_unpacked=True):
with open(os.path.join(directory, path), 'rb') as fp:
out_asarfile.seek(int(fileinfo['offset']) + baseoffset)
out_asarfile.write(fp.read())
return hashlib.sha256(json_header_bytes).hexdigest()
def __add_file_integrities(self, directory, fileinfos):
for path, fileinfo in _walk_fileinfos(fileinfos, ignore_unpacked=True):
fileinfo["integrity"] = self.__get_file_integrity(
os.path.join(directory, path))
def __get_file_integrity(self, file):
"""Computes file integrity hashes of a file.
For more details, see
https://paper.dropbox.com/doc/Electron-ASAR-Integrity-QrEbSA149226qARwYOcp9.
Args:
file (str): the full path to the file.
Returns:
a Dict, respecting Asar's integrity format.
"""
hasher = hashlib.sha256()
block_hashes = []
with open(file, "rb") as f:
while True:
block = f.read(self.BLOCK_SIZE)
if not block:
break
block_hashes.append(hashlib.sha256(block).hexdigest())
hasher.update(block)
return {
"algorithm": "SHA256",
"hash": hasher.hexdigest(),
"blockSize": self.BLOCK_SIZE,
"blocks": block_hashes
}
# end Asar
def create_cmd_parser():
parser = argparse.ArgumentParser(prog='math-with-slack',
description='Inject Slack with MathJax.')
parser.add_argument('-a',
'--app-file',
help='Path to Slack\'s \'app.asar\' file.')
parser.add_argument(
'--mathjax-url',
help=("Either a remote URL to download a MathJax release or"
" a local path to a pre-downloaded MathJax release."),
default='https://registry.npmjs.org/mathjax/-/mathjax-3.1.0.tgz')
parser.add_argument(
'--mathjax-tex-options',
type=str,
help=('Path to file with TeX input processor options, '
'or an inline string to the TeX input processor options. '
'See http://docs.mathjax.org/en/latest/options/input/tex.html '
'for the options format.'),
default='default')
parser.add_argument("--macos-codesign",
action="store_true",
help="Opt-in to perform code signing on MacOS.")
parser.add_argument('-u',
'--uninstall',
action='store_true',
help='Removes injected MathJax code.')
parser.add_argument('--version',
action='version',
version='%(prog)s ' + mws_version)
return parser
# Find path to app.asar
def _find_candidate_app_files(path_globs,
filename="app*.asar",
min_file_size_kb=100):
candidates = []
for path_glob in path_globs:
candidates += glob.glob(os.path.join(path_glob, filename))
candidates = [
c for c in candidates
if os.path.isfile(c) and os.path.getsize(c) > min_file_size_kb * 1000
]
candidates = sorted(candidates,
key=lambda c: os.path.getctime(c),
reverse=True)
return candidates
def _filter_candidates_app_files_by_arch(search_paths):
output = subprocess.check_output(
["sysctl", "-n", "machdep.cpu.brand_string"])
if b'Apple' in output:
suffix = "arm64"
else:
suffix = "x64"
def cond(path):
basename = os.path.splitext(os.path.basename(path))[0]
return basename in ("app", "app-{}".format(suffix))
return list(filter(cond, search_paths))
def _display_choose_from_menu(candidates, header="", prompt=""):
print(header)
for i, candidate in enumerate(candidates):
if i == 0:
candidate += " <== (default)"
print("{}) {}".format(i, candidate))
choice = input(prompt).lower()
choice = "".join(choice.split()) # remote whitespace
choice = choice.strip(")") # remove trailing ')'
try:
if choice in ("", "y", "yes"):
choice = 0
else:
choice = int(choice)
return candidates[choice]
except:
exprint("Invalid choice. Please restart script.")
def resolve_app_path(app_path):
"""Resolves the true app_path from various sources of information."""
if app_path is not None:
if not os.path.isfile(app_path):
exprint('Cannot find Slack at ' + app_path)
else:
path_lookups = {
'darwin': ['/Applications/Slack.app/Contents/Resources/'],
'linux': [
'/usr/lib/slack/resources/',
'/usr/local/lib/slack/resources/',
'/opt/slack/resources/',
'/mnt/c/Users/*/AppData/Local/slack/*/resources/',
],
'win32': ['c:\\Users\\*\\AppData\\Local\\slack\\*\\resources\\']
}
_platform = get_platform()
search_paths = path_lookups[_platform]
candidate_app_asars = _find_candidate_app_files(search_paths)
if _platform == "darwin":
candidate_app_asars = _filter_candidates_app_files_by_arch(
candidate_app_asars)
if len(candidate_app_asars) == 0:
exprint(
("Could not find Slack's app.asar file under {}. "
"Please manually provide path (see --help)").format(search_paths))
if len(candidate_app_asars) == 1:
app_path = candidate_app_asars[0]
else:
app_path = _display_choose_from_menu(
candidate_app_asars,
header="Several versions of Slack are installed.",
prompt="Choose from above:")
return app_path
def get_files_need_modification(app_path, slack_version):
ret = [app_path]
if get_platform() == "darwin" and slack_version >= LooseVersion("4.22"):
ret += macos_get_plists(app_path)
return ret
def is_previously_modified(app_path):
with AsarExtractor.open(app_path) as asar_extractor:
asar_header = asar_extractor.files
return 'MWSINJECT' in asar_header["files"]
def extract_asar(tmp_dir, app_path):
asar_extracted_dir = os.path.join(tmp_dir, "app.asar.unpacked")
asar_extractor = AsarExtractor.open(app_path)
asar_extractor.filename = app_path # Use the non-backup asar name
asar_extractor.extract(asar_extracted_dir)
return asar_extractor, asar_extracted_dir
def read_slack_version(asar_extracted_dir):
with open(os.path.join(asar_extracted_dir, "package.json"), "r") as f:
return LooseVersion(json.load(f)["version"])
def check_slack_version(asar_extracted_dir):
slack_version = read_slack_version(asar_extracted_dir)
if slack_version <= LooseVersion('4.4'):
exprint("Unsupported Slack Version {}.".format(slack_version))
def download_mathjax(mathjax_url):
"""Downloads MathJax and returns the core source.
We currently assumes downloaded file is a tar called `package.tar`.
"""
def get_reporthook():
class report:
start_time = None
progress_size = None
def reporthook(count, block_size, total_size):
if count == 0:
report.progress_size = 0
report.start_time = time.time(
) - 1e-6 # also offset a bit so we don't run into divide by zero.
return
duration = time.time() - report.start_time
report.progress_size += block_size
if report.progress_size >= total_size:
report.progress_size = total_size
try:
speed = report.progress_size / (1024 * duration)
percent = int(report.progress_size * 100 / total_size)
except ZeroDivisionError:
speed, percent = 0, 0
sys.stdout.write(
("\rDownloading MathJax...{:3d}%, {:3.1f} MB / {:3.1f} MB,"
" {:6.1f} KB/s, {:3.1f} sec").format(
percent, report.progress_size / (1024 * 1024),
total_size / (1024 * 1024), speed, duration))
if report.progress_size >= total_size:
sys.stdout.write("\n")
sys.stdout.flush()
return reporthook
if os.path.isfile(mathjax_url):
mathjax_tar_name = mathjax_url
else:
mathjax_tar_name, headers = urllib_request.urlretrieve(
mathjax_url, [], get_reporthook())
mathjax_tmp_dir = tempfile.mkdtemp()
mathjax_tar = tarfile.open(mathjax_tar_name)
mathjax_tar.extractall(path=mathjax_tmp_dir)
mathjax_tar.close()
mathjax_dir = os.path.join(mathjax_tmp_dir, "package")
mathjax_src_path = os.path.join(mathjax_dir, "es5/tex-svg-full.js")
with open(mathjax_src_path, "r+") as mathjax_src_file:
mathjax_src = mathjax_src_file.read().encode('utf-8')
return mathjax_src
def get_injected_code(mathjax_src, mathjax_tex_options):
"""Gets the injected code."""
inject_code = ('\n\n// math-with-slack ' + mws_version + '''
// Inject MathJax 3.
// Credit to initial implementation: https://github.com/fsavje/math-with-slack
document.addEventListener('DOMContentLoaded', function() {
function typeset(elements) {
if(elements.length) {
const MathJax = window.MathJax;
MathJax.startup.promise = MathJax.startup.promise
.then(() => { return MathJax.typesetPromise(elements); })
.catch((err) => console.log('Typeset failed: ' + err.message));
return MathJax.startup.promise;
}
}
window.MathJax = {
options: {
skipHtmlTags: [
'script', 'noscript', 'style', 'textarea',
'annotation', 'annotation-xml'
],
renderActions: {
assistiveMml: [], // Disable assitiveMML since we are not using it anywhere
// Also because this will cause duplicated copied text
addCopyText: [156,
(doc) => {
if (!doc.processed.isSet('addtext')) {
for (const math of doc.math) {
MathJax.config.addCopyText(math, doc);
}
doc.processed.set('addtext');
}
},
(math, doc) => MathJax.config.addCopyText(math, doc)
]
}
},
addCopyText(math, doc) {
if (math.state() < MathJax.STATE.ADDTEXT) {
if (!math.isEscaped) {
const adaptor = doc.adaptor;
const text = adaptor.node('span', {'aria-hidden': true, 'class': 'mathjax_ignore mjx-copytext'}, [
adaptor.text(math.start.delim + math.math + math.end.delim)
]);
adaptor.append(math.typesetRoot, text);
// Insert thin space(s) if math is at begin or end of text
if (math.start.n == 0) {
adaptor.insert(adaptor.text('\u200A'), adaptor.firstChild(math.typesetRoot));
}
if (math.end.n == math.end.node.length) {
adaptor.append(math.typesetRoot, adaptor.text('\u200A'));
}
}
math.state(MathJax.STATE.ADDTEXT);
}
},
loader: {
paths: {mathjax: 'mathjax/es5'},
source: {},
require: require,
load: [
'input/tex-full',
'output/svg',
'[tex]/noerrors',
'[tex]/noundefined',
]
},
tex: $MATHJAX_TEX_OPTIONS$,
startup: {
ready: () => {
MathJax = window.MathJax;
// Allocate a state bit for mjx-copytext and
// make the copyable text hidden
const {newState, STATE} = MathJax._.core.MathItem;
const {AbstractMathDocument} = MathJax._.core.MathDocument;
const {SVG} = MathJax._.output.svg_ts;
newState('ADDTEXT', 156);
AbstractMathDocument.ProcessBits.allocate('addtext');
SVG.commonStyles['.mjx-copytext'] = {
'font-size': 0
};
MathJax.STATE = STATE;
// Invoke MathJax's default initialization
MathJax.startup.defaultReady();
// Disable some menu option that will cause us to crash
MathJax.startup.document.menu.menu.findID('Settings', 'Renderer').disable();
MathJax.startup.document.menu.menu.findID('Accessibility').disable();
// Observer for when an element needs to be typeset
var intersection_observer = new IntersectionObserver(
(entries, observer) => {
var appearedEntries = entries.filter((entry) => entry.intersectionRatio > 0);
if(appearedEntries.length) {
typeset(appearedEntries.map((entry) => entry.target));
}
},
{ root: null }
);
// observer for elements are first inserted into the DOM.
// We delay elements that require typesetting to the intersection observer
function observe_dom_change(mutations) {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((addedNode) => {
if(addedNode.nodeType != Node.TEXT_NODE) {
const messages = addedNode.querySelectorAll(
'span.c-message__body, span.c-message_kit__text, div.p-rich_text_block, span.c-message_attachment__text');
messages.forEach((msg) => {
if(!msg.ready) {
msg.ready = true;
intersection_observer.observe(msg);
}
});
}
});
})
mutations.forEach((mutation) => {
MathJax.typesetClear(mutation.removedNodes);
})
}
var mutation_observer = new MutationObserver(observe_dom_change);
mutation_observer.observe(document.body, {
childList: true,
subtree: true
});
observe_dom_change();
}
},
};
var ctxMenustyle = document.createElement('style');
ctxMenustyle.innerHTML = `
.CtxtMenu_MenuFrame {
z-index: 10000 !important;
}
.CtxtMenu_Menu {
z-index: 10000 !important;
}
.CtxtMenu_Info {
z-index: 10000 !important;
}
`;
document.body.appendChild(ctxMenustyle);
// Import mathjax
$MATHJAX_STUB$
});
''').encode('utf-8')
if mathjax_tex_options == "default":
mathjax_tex_options = """{
packages: {'[+]': ['noerrors', 'noundefined']},
inlineMath: [['$', '$']],
displayMath: [['$$', '$$']],
}"""
elif os.path.isfile(mathjax_tex_options):
with open(mathjax_tex_options, "r") as f:
mathjax_tex_options = f.read()
else:
mathjax_tex_options = mathjax_tex_options
inject_code = inject_code.replace(b"$MATHJAX_TEX_OPTIONS$",
mathjax_tex_options.encode("utf-8"))
inject_code = inject_code.replace(b"$MATHJAX_STUB$", mathjax_src)
return inject_code
def macos_get_plists(app_path):
plist_paths = [
"../../Info.plist",
# "../../FrameWorks/Electron Framework.framework/Resources/Info.plist",
]