This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.rkt
282 lines (254 loc) · 11.2 KB
/
config.rkt
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
#lang racket/base
; config.rkt
; contains default values for variables
(require json
racket/bool
srfi/13
racket/cmdline
racket/list
racket/path)
(provide (all-defined-out))
; base tox directory
(define tox-path (cond [(eq? (system-type) 'unix)
; check XDG variable first, then default
; to ~/.config/tox
(let ([xdg (getenv "XDG_CONFIG_HOME")])
(if xdg
(build-path xdg "tox")
(build-path (find-system-path 'home-dir)
".config/tox")))]
[(eq? (system-type) 'windows)
(normal-case-path
(build-path (find-system-path 'home-dir)
"appdata/local/tox"))]
[(eq? (system-type) 'macosx)
(build-path (find-system-path 'home-dir)
"Library/Application Support/tox")]))
(define download-path (normal-case-path (build-path (find-system-path 'home-dir)
"Downloads")))
; profiling!
(define default-profile "blight")
(define profile-name (make-parameter default-profile))
(define profiles
(λ ([x null])
; populate profiles list
(let* ([ext ".tox"]
[dlst (directory-list tox-path)]
[checker (λ (f)
(let ([name (path->string f)])
(cond [(false? (string-contains-ci name ext)) #f]
[else f])))]
[filtered (filter checker dlst)])
(make-parameter (map
(λ (x)
(let [(name (path->string x))]
(substring name 0 (- (string-length name) 4))))
filtered)))))
; history db file
(define db-file
(λ ([profile (profile-name)])
(make-parameter
(build-path tox-path
(string-append profile ".sqlite")))))
; tox-specific information
(define data-file
(λ ([profile (profile-name)])
(make-parameter
(build-path tox-path
(string-append profile ".tox")))))
; blight-specific configurations
(define config-file
(λ ([profile (profile-name)])
(make-parameter
(build-path tox-path
(string-append profile ".json")))))
; location of sound directory (currently depends on running from same dir
; change to /usr/share/blight/sounds (or something) once a proper
; installer is to be had
(define sound-dir (build-path "sounds"))
; list of sound files
(define sounds (list
(build-path sound-dir "New Message.wav")
(build-path sound-dir "Contact Logs In.wav")
(build-path sound-dir "Contact Logs Out.wav")
(build-path sound-dir "Log In.wav")
(build-path sound-dir "Log Out.wav")
(build-path sound-dir "Contact Request Accepted.wav")
(build-path sound-dir "Transfer Pending.wav")
(build-path sound-dir "Transfer Complete.wav")
(build-path sound-dir "Incoming Call.wav")
(build-path sound-dir "Outgoing Call.wav")
(build-path sound-dir "Incoming Video Call.wav")
(build-path sound-dir "Outgoing Video Call.wav")
; error sound will be the last element
(build-path sound-dir "Error.wav")))
(define make-noise-default #t)
; blight icons for the buddy list
(define icon-dir (build-path "icons"))
; list of icon files
(define icons (list
(build-path icon-dir "offline.png")
(build-path icon-dir "busy.png")
(build-path icon-dir "away.png")
(build-path icon-dir "groupchat.png")
(build-path icon-dir "available.png")
(build-path icon-dir "speaker-unmuted.png")
(build-path icon-dir "speaker-muted.png")))
; max avatar size
(define BLIGHT-MAX-AVATAR-SIZE (expt 2 16))
(define avatar-dir (build-path tox-path "avatars"))
(define logo-dir (build-path "img"))
(define logo (build-path logo-dir "blight-logo-128px.png"))
; tox-path doesn't exist? create it
(unless (directory-exists? tox-path)
(make-directory tox-path))
(unless (directory-exists? avatar-dir)
(make-directory avatar-dir))
#|
command-line stuff
--profile: determine the profile to use
--list: list available profiles (by looking for .tox files)
arg: list of files to copy to tox-path as .tox files
1. default profile is "blight"
2. all other files are determined by adding to profile-name
ex: (string-append (profile-name) ".json")
(string-append (profile-name) ".tox")
(string-append (profile-name) ".sqlite")
3. file parameters are curried, which means (db-file) returns a procedure
and to change it, you must run ((db-file <profile>)), but that is
only temporary as calling ((db-file)) again will change it back to
whatever (profile-name) is currently
|#
(let ([ext ".tox"])
(command-line
#:usage-help
"Calling Blight without any arguments will start Blight with the defualt profile."
"Otherwise, please provide a valid profile for Blight to use."
"Giving Blight an optional number of files will have them be imported"
"as Tox profiles."
#:once-any
[("-p" "--profile")
pn ; takes one argument pn
"Specify the profile (by name) to use at startup. (Do not include a .tox extension.)"
"Use --list to see a list of available profiles."
; given profile has no extension
(cond [(integer? (string-contains-ci pn ext))
(displayln "Invalid profile entered! Reverting to default profile...")]
; given profile is valid (if it doesn't exist, we'll just make it)
[else (profile-name pn)
((data-file))
((config-file))
((db-file))])]
[("-l" "--list") "List available Tox profiles to load."
(for-each (λ (f) (displayln f)) ((profiles)))
(exit)]
#:args import-files
(unless (empty? import-files)
(for-each
(λ (x)
(let* ([fn (path->string (file-name-from-path x))]
[contains (string-contains-ci fn ext)]
[timestamp
(inexact->exact
(floor (current-inexact-milliseconds)))])
(if (false? contains)
(copy-file x (build-path tox-path
(string-append fn timestamp ext)))
(copy-file x (build-path tox-path
(substring fn 0 contains))))))
import-files))))
#| ###################### END COMMAND-LINE STUFF ######################### |#
; if <profile>.json does not exist, create it
(unless (file-exists? ((config-file)))
(define config-port-out
(open-output-file ((config-file))
#:mode 'text
#:exists 'can-update))
(printf "~a created...\n" ((config-file)))
(close-output-port config-port-out))
; if <profile>.tox does not exist, create it
(unless (file-exists? ((data-file)))
(define data-port-out
(open-output-file ((data-file))
#:mode 'binary
#:exists 'can-update))
(printf "~a created...~n" ((data-file)))
(close-output-port data-port-out))
; default name and status message
; if data exists, do no use these
(define my-name-default "Blight Tester")
(define my-name (make-parameter my-name-default))
(define my-status-message-default "Toxing on Blight")
(define my-status-message (make-parameter my-status-message-default))
; DHT stuff
(struct dht-node (nick address port public-key) #:transparent)
; list of structs containing DHT node information obtained from
; http://wiki.tox.im - change this to be taken from a JSON query
(define node-list
(list
(dht-node 'bunslow "76.191.23.96" 33445
"93574A3FAB7D612FEA29FD8D67D3DD10DFD07A075A5D62E8AF3DD9F5D0932E11")
(dht-node 'stal "23.226.230.47" 33445
"A09162D68618E742FFBCA1C2C70385E6679604B2D80EA6E84AD0996A1AC8A074")
(dht-node 'nurupo "192.210.149.121" 33445
"F404ABAA1C99A9D37D61AB54898F56793E1DEF8BD46B1038B9D822E8460FAB67")
(dht-node 'jfreegman "104.219.184.206" 443
"8CD087E31C67568103E8C2A28653337E90E6B8EDA0D765D57C6B5172B4F1F04C")))
; if <profile>.json exists, do not use these
(define use-ipv6?-default #t)
(define use-udp?-default #t)
(define proxy-type-default 'none) ; _TOX-PROXY-TYPE value
(define proxy-host-default "") ; ignored if proxy type is 'NONE
(define proxy-port-default 0) ; ignored if proxy type is 'NONE
(define start-port-default 0)
(define end-port-default 0)
(define tcp-port-default 345) ; tcp server port - 0 to disable server
(define encrypted?-default #f)
; if blight-config.json does not exist, initalize it to default values
(define json-default
(hasheq 'make-noise make-noise-default
'ipv6? use-ipv6?-default
'udp? use-udp?-default
; turn proxy-type-default into a string because of (jsexpr?)
'proxy-type (symbol->string proxy-type-default)
'proxy-host proxy-host-default
'proxy-port proxy-port-default
'start-port start-port-default
'end-port end-port-default
'tcp-port tcp-port-default
'encrypted? encrypted?-default))
; <profile>.json is empty, initialize with default values for variables
(when (zero? (file-size ((config-file))))
(let ([config-port-out (open-output-file ((config-file))
#:mode 'text
#:exists 'truncate/replace)])
(json-null 'null)
(write-json json-default config-port-out)
(write-json (json-null) config-port-out)
(close-output-port config-port-out)))
(define-syntax-rule (hash-ref* mhash k1 k2 ...)
(let ([to-set (λ () #f)]) ; in case the key isn't in the hash
(values (make-parameter (hash-ref mhash k1 to-set))
(make-parameter (hash-ref mhash k2 to-set)) ...)))
(define-values
(make-noise use-ipv6? use-udp? proxy-type proxy-host
proxy-port start-port end-port tcp-port encrypted?)
(let* ([config-port-in (open-input-file ((config-file)) #:mode 'text)]
[json-info (read-json config-port-in)])
(close-input-port config-port-in)
(hash-ref* json-info 'make-noise 'ipv6? 'udp? 'proxy-type 'proxy-host
'proxy-port 'start-port 'end-port 'tcp-port 'encrypted?)))
; _TOX-PROXY-TYPE is a symbol
(proxy-type (string->symbol (proxy-type)))
(define (toggle-noise) (make-noise (not (make-noise))))
; list of unicode emoticons
(define emojis (list "😁" "😂" "😃" "😄" "😅" "😇"
"😈" "😉" "😊" "😋" "😌" "😍"
"😎" "😏" "😐" "😒" "😓" "😔"
"😖" "😘" "😚" "😜" "😝" "😞"
"😠" "😡" "😢" "😣" "😥" "😨"
"😩" "😪" "😫" "😭" "😰" "😱"
"😲" "😳" "😵" "😶" "😷" "😸"
"😹" "😺" "😻" "😼" "😽" "😾"
"😿" "🙀" "☺" "☹" "⚇" "🐱"
"♥" "☔" "☀" "♫" "☕" "★"))