Debian Codegraph @

lsi-getconf

raw
1 #!/usr/bin/env python3
2 # localslackirc
3 # Copyright (C) 2024 Salvo "LtWorf" Tomaselli
4 #
5 # localslackirc is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18 # author Salvo "LtWorf" Tomaselli <[email protected]>
19
20 import os
21 import sys
22 import sqlite3
23 import argparse
24 from pathlib import Path
25
26
27 HOME = Path(os.environ.get('HOME', ''))
28 MOZILLA_CONF = HOME / '.mozilla'
29
30 def log(*args):
31 print(*args, file=sys.stderr)
32
33 def get_mozilla_localstorage_tokens(ls: Path) -> list[str]:
34 '''
35 Pass the path of a possible local storage file and get slack tokens
36 '''
37
38 db = sqlite3.connect(ls)
39 try:
40 s = db.execute('select value from data where key="localConfig_v2";')
41 except Exception:
42 return []
43
44 r = []
45 for i in s:
46 # Some magic crap, it's a readable json polluted with unreadable crap…
47 # No idea if this will work in other cases than on my machine
48
49 i = i[0]
50 i = i.split(b'"toke', 1)[1].split(b'"', 1)[0]
51 i = i[i.index(b'xox'):]
52 r.append(i.decode('ascii'))
53
54 return r
55
56
57 def get_mozilla_cookies(jar: Path) -> list[str]:
58 '''
59 Pass the path of a cookie jar and receive a list of slack cookies
60 '''
61 db = sqlite3.connect(jar)
62 return [i[0] for i in db.execute('select value from moz_cookies WHERE host= ".slack.com" and name="d";')]
63
64 def mozilla(cfgdir: Path) -> tuple[list[str], list[str]]:
65 '''
66 returns cookies, tokens
67 '''
68 log(f'Scanning {cfgdir} as a Mozilla directory')
69 cookiefiles = cfgdir.rglob('cookies.sqlite')
70 other_files = cfgdir.rglob('*.sqlite')
71
72 cookies = []
73 tokens = []
74
75 for i in cookiefiles:
76 log(f'Scanning possible cookie jar {i}')
77 try:
78 cookies += get_mozilla_cookies(i)
79 except Exception as e:
80 sys.exit(str(e))
81
82 for i in other_files:
83 log(f'Scanning possible local storage file {i}')
84 try:
85 tokens += get_mozilla_localstorage_tokens(i)
86 except Exception as e:
87 sys.exit(str(e))
88
89 return cookies, tokens
90
91 def main() -> None:
92 parser = argparse.ArgumentParser(
93 description='Read the browser database to try to find the cookie and token that are necessary to set up localslackirc.'
94 )
95 parser.add_argument('--mozilla-dir', type=Path, action='store', dest='mozilla_dir',
96 default=MOZILLA_CONF)
97 args = parser.parse_args()
98
99 cookies = []
100 tokens = []
101
102 c, t = mozilla(args.mozilla_dir)
103 cookies += c
104 tokens += t
105
106 # TODO chromium
107
108 if not cookies and not tokens:
109 sys.exit('Fail: Nothing was found')
110
111 print('# ==============')
112 if tokens:
113 print('# Tokens found:')
114 for i in tokens:
115 print(f'TOKEN={i}')
116 if cookies:
117 print('# Cookies found:')
118 for i in cookies:
119 print(f'COOKIE="d={i};"')
120
121 if len(tokens) > 1 or len(cookies) > 1:
122 print('# Try deleting all cookies from your browsers and logging into slack again')
123
124 if __name__ == '__main__':
125 main()