-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
901 lines (780 loc) · 37.5 KB
/
init.lua
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
--- === WatchForMeeting ===
---
--- A Spoon to answer the question
--- > Are you in a meeting?
---
--- Watches to see if:
--- 1) Zoom is running
--- 2) Are you on a call
--- 3) Are you on mute, is your camera on, and/or are you screen sharing
---
--- And then lets you share that information.
---
--- # Installation & Basic Usage
--- Download the [Latest Release](https://github.com/asp55/WatchForMeeting/releases/latest) and unzip to `~/.hammerspoon/Spoons/`
---
--- To get going right out of the box, in your `~/.hammerspoon/init.lua` add these lines:
--- ```
--- hs.loadSpoon("WatchForMeeting")
--- spoon.WatchForMeeting:start()
--- ```
---
--- This will start the spoon monitoring for zoom calls, and come with the default status page, and menubar configurations.
---
--We'll store some stuff in an internal table
local _internal = {}
-- create a namespace
local WatchForMeeting={}
WatchForMeeting.__index = WatchForMeeting
-- Metadata
WatchForMeeting.name = "WatchForMeeting"
WatchForMeeting.version = "2.0.3"
WatchForMeeting.author = "Andrew Parnell <[email protected]>"
WatchForMeeting.homepage = "https://github.com/asp55/WatchForMeeting"
WatchForMeeting.license = "MIT - https://opensource.org/licenses/MIT"
-- Event callbacks
local events = {
meetingChange=true,
meetingStarted=true,
meetingStopped=true,
micChange=true,
micOn=true,
micOff=true,
videoChange=true,
videoOn=true,
videoOff=true,
screensharingChange=true,
screensharingOn=true,
screensharingOff=true,
}
_internal.events = {}
for k in pairs(events) do WatchForMeeting[k]=k end
-------------------------------------------
-- Declare Variables
-------------------------------------------
--- WatchForMeeting.logger
--- Variable
--- hs.logger object used within the Spoon. Can be accessed to set the default log level for the messages coming from the Spoon.
WatchForMeeting.logger = hs.logger.new('WatchMeeting')
-- private variable to track if spoon is already running or not. (Makes it easier to find local variables)
_internal.running = false
-------------------------------------------
-- Special Variables (stored in _internal and accessed through metamethods defined below)
-------------------------------------------
--- WatchForMeeting.sharing
--- Variable
--- A Table containing the settings that control sharing.
---
--- | Key | Description | Default |
--- | --- | ----------- | ------- |
--- | enabled | Whether or not sharing is enabled.<br/><br/>When false, the spoon will still monitor meeting status to [meetingState](#meetingState), but you will need to write your own automations for what to do with that info. | _true_ |
--- | useServer | Do you want to use an external server? (See *Configuration Options* below) | _false_ |
--- | | ↓ _required info when `useServer=false`_ | |
--- | port | What port to run the self hosted server when WatchForMeeting.sharing.useServer is false. | _8080_ |
--- | | ↓ _required info when `useServer=true`_ | |
--- | serverURL | The complete url for the external server, including port. IE: `http://localhost:8080` | _nil_ |
--- | key | UUID to identify the room. Value is provided when the room is added on the server side. | _nil_ |
--- | maxConnectionAttempts | Maximum number of connection attempts when using an external server. When less than 0, infinite retrys | _-1_ |
--- | waitBeforeRetry | Time, in seconds, between connection attempts when using an external server | _5_ |
---
--- # Configuration Options
--- ## Default
--- In order to minimize dependencies, by default this spoon uses a [hs.httpserver](https://www.hammerspoon.org/docs/hs.httpserver.html) to host the status page. This comes with a significant downside of: only the last client to load the page will receive status updates. Any previously connected clients will remain stuck at the last update they received before that client connected.
---
--- Once you are running the spoon, assuming you haven't changed the port (and nothing else is running at that location) you can reach your status page at http://localhost:8080
---
--- ## Better - MeetingStatusServer
--- For a better experience I recommend utilizing an external server to receive updates via websockets, and broadcast them to as many clients as you wish to connect.
---
--- For that purpose I've built [http://github.com/asp55/MeetingStatusServer](http://github.com/asp55/MeetingStatusServer) which runs on node.js and can either be run locally as its own thing, or hosted remotely.
---
--- If using the external server, you will to create a key to identify your "room" and then provide that information to the spoon.
--- In that case, before `spoon.WatchForMeeting:start()` add the following to your `~/.hammerspoon/init.lua`
---
--- ```
--- spoon.WatchForMeeting.sharing.useServer = true
--- spoon.WatchForMeeting.sharing.serverURL="[YOUR SERVER URL]"
--- spoon.WatchForMeeting.sharing.key="[YOUR KEY]"
--- ```
---
--- or
---
--- ```
--- spoon.WatchForMeeting.sharing = {
--- useServer = true,
--- serverURL = "[YOUR SERVER URL]",
--- key="[YOUR KEY]"
--- }
--- ```
---
--- ## Disable
--- If you don't want to broadcast your status to a webpage, simply disable sharing
--- ```
--- spoon.WatchForMeeting.sharing = {
--- enabled = false
--- }
--- ```
---
_internal.sharingDefaults = {
enabled = true,
useServer = false,
port = 8080,
serverURL = nil,
key = nil,
maxConnectionAttempts = -1, --when less than 0, infinite retrys
waitBeforeRetry = 5,
}
_internal.sharing = setmetatable({}, {__index=_internal.sharingDefaults})
--- WatchForMeeting.menubar
--- Variable
--- A Table containing the settings that control sharing.
---
--- | Key | Description | Default |
--- | --- | ----------- | ------- |
--- | enabled | Whether or not to show the menu bar. | _true_ |
--- | color | Whether or not to use color icons. | _true_ |
--- | detailed | Whether or not to use the detailed icon set. | _true_ |
--- | showFullState | Whether the menubar icon should represent the full state<br/>(IE: Mic On/Off, Video On/Off, & Screen Sharing) | _true_ |
---
---
--- ## Icons
---
--- <table>
--- <thead>
--- <tr>
--- <th>
--- <code>WatchForMeeting.menuBar = {...}</code> →
--- </th>
--- <th><code>color=true,</code><br/><code>detailed=true,</code></th>
--- <th><code>color=true,</code><br/><code>detailed=false,</code></th>
--- <th><code>color=false,</code><br/><code>detailed=true,</code></th>
--- <th><code>color=false,</code><br/><code>detailed=false,</code></th>
--- </tr>
--- <tr>
--- <th>State (See: <a href="#meetingState">WatchForMeeting.meetingState</a>) ↓
--- </th>
--- <th colspan="4"><code>showFullState=true</code> or <code>showFullState=false</code></th>
--- </tr>
--- </thead>
--- <tbody>
--- <tr>
--- <td>Available</td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Detailed/Free.png" alt="Free slash Available" height="16" /></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Minimal/Free.png" alt="Free slash Available" height="16" /></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Detailed/Free.png" alt="Free slash Available" height="16" /></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Minimal/Free.png" alt="Free slash Available" height="16" /></td>
--- </tr>
--- <tr>
--- <td>Busy</td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Detailed/Meeting.png" alt="In meeting, no additional status" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Minimal/Meeting.png" alt="In meeting, no additional status" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Detailed/Meeting.png" alt="In meeting, no additional status" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Minimal/Meeting.png" alt="In meeting, no additional status" height="16"></td>
--- </tr>
--- <tr>
--- <td></td>
--- <th colspan="4"><code>showFullState=true</code> only</th>
--- </tr>
--- <tr>
--- <td>Busy + Mic On</td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Detailed/Meeting-Mic.png" alt="In meeting, mic:on, video:off, screensharing:off" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Minimal/Meeting-Mic.png" alt="In meeting, mic:on, video:off, screensharing:off" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Detailed/Meeting-Mic.png" alt="In meeting, mic:on, video:off, screensharing:off" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Minimal/Meeting-Mic.png" alt="In meeting, mic:on, video:off, screensharing:off" height="16"></td>
--- </tr>
--- <tr>
--- <td>Busy + Video On</td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Detailed/Meeting-Vid.png" alt="In meeting, mic:off, video:on, screensharing:off" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Minimal/Meeting-Vid.png" alt="In meeting, mic:off, video:on, screensharing:off" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Detailed/Meeting-Vid.png" alt="In meeting, mic:off, video:on, screensharing:off" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Minimal/Meeting-Vid.png" alt="In meeting, mic:off, video:on, screensharing:off" height="16"></td>
--- </tr>
--- <tr>
--- <td>Busy + Screen Sharing</td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Detailed/Meeting-Screen.png" alt="In meeting, mic:off, video:off, screensharing:on" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Minimal/Meeting-Screen.png" alt="In meeting, mic:off, video:off, screensharing:on" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Detailed/Meeting-Screen.png" alt="In meeting, mic:off, video:off, screensharing:on" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Minimal/Meeting-Screen.png" alt="In meeting, mic:off, video:off, screensharing:on" height="16"></td>
--- </tr>
--- <tr>
--- <td>Busy + Mic On + Video On</td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Detailed/Meeting-Mic-Vid.png" alt="In meeting, mic:on, video:on, screensharing:off" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Minimal/Meeting-Mic-Vid.png" alt="In meeting, mic:on, video:on, screensharing:off" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Detailed/Meeting-Mic-Vid.png" alt="In meeting, mic:on, video:on, screensharing:off" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Minimal/Meeting-Mic-Vid.png" alt="In meeting, mic:on, video:on, screensharing:off" height="16"></td>
--- </tr>
--- <tr>
--- <td>Busy + Mic On + Screen Sharing</td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Detailed/Meeting-Mic-Screen.png" alt="In meeting, mic:on, video:off, screensharing:on" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Minimal/Meeting-Mic-Screen.png" alt="In meeting, mic:on, video:off, screensharing:on" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Detailed/Meeting-Mic-Screen.png" alt="In meeting, mic:on, video:off, screensharing:on" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Minimal/Meeting-Mic-Screen.png" alt="In meeting, mic:on, video:off, screensharing:on" height="16"></td>
--- </tr>
--- <tr>
--- <td>Busy + Video On + Screen Sharing</td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Detailed/Meeting-Vid-Screen.png" alt="In meeting, mic:off, video:on, screensharing:on" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Minimal/Meeting-Vid-Screen.png" alt="In meeting, mic:off, video:on, screensharing:on" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Detailed/Meeting-Vid-Screen.png" alt="In meeting, mic:off, video:on, screensharing:on" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Minimal/Meeting-Vid-Screen.png" alt="In meeting, mic:off, video:on, screensharing:on" height="16"></td>
--- </tr>
--- <tr>
--- <td>Busy + Mic On + Video On + Screen Sharing</td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Detailed/Meeting-Mic-Vid-Screen.png" alt="In meeting, mic:on, video:on, screensharing:on" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Color/Minimal/Meeting-Mic-Vid-Screen.png" alt="In meeting, mic:on, video:on, screensharing:on" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Detailed/Meeting-Mic-Vid-Screen.png" alt="In meeting, mic:on, video:on, screensharing:on" height="16"></td>
--- <td><img src="https://raw.githubusercontent.com/asp55/WatchForMeeting/main/menubar-icons/Template/Minimal/Meeting-Mic-Vid-Screen.png" alt="In meeting, mic:on, video:on, screensharing:on" height="16"></td>
--- </tr>
--- </tbody>
--- </table>
_internal.menubarDefaults = {
enabled = true,
color = true,
detailed = true,
showFullState = true
}
_internal.menubar__newIndex = function (table, key, value)
if(key=="enabled") then
if(value) then
_internal.meetingMenuBar:returnToMenuBar()
_internal.updateMenuIcon(_internal.meetingState, _internal.faking)
else
_internal.meetingMenuBar:removeFromMenuBar()
end
else
_internal.updateMenuIcon(_internal.meetingState, _internal.faking)
end
end
_internal.menubar = setmetatable({}, {__index=_internal.menubarDefaults, __newindex=_internal.menubar__newIndex})
--- WatchForMeeting.mode
--- Variable
--- Number representing which mode WatchForMeeting should be running
---
--- - *0* - Automatic (default)
--- -- Monitors Zoom and updates status accordingly
--- - *1* - Busy
--- -- Fakes a meeting. (Marks as in meeting, and signals that the mic is live, camera is on, and screen is sharing.) Useful when meeting type is not supported (Currently any platform that isn't zoom.)
_internal.mode = 0
--- WatchForMeeting.zoom
--- Variable
--- (Read-only) The hs.application for zoom if it is running, otherwise nil
_internal.zoom = nil
--- WatchForMeeting.meetingState
--- Variable
--- (Read-only) Either false (when not in a meeting) or a table (when in a meeting)
---
--- | Value | Description |
--- | ----------------------------------------------------------------------- | ----------- |
--- | `false` | Available |
--- | `{mic_open = [Boolean], video_on = [Boolean], sharing = [Boolean] }` | Busy |
_internal.meetingState = false
_internal.lastMeetingState = nil;
--- WatchForMeeting.faking
--- Variable
--- (Read-only) Boolean representing if the meeting is real or faked
-- MetaMethods
WatchForMeeting = setmetatable(WatchForMeeting, {
--GET
__index = function (table, key)
if(key=="zoom" or key=="meetingState" or key=="faking" or key=="menubar" or key=="mode" or key=="sharing") then
return _internal[key]
else
return rawget( table, key )
end
end,
--SET
__newindex = function (table, key, value)
if(key=="zoom" or key=="meetingState" or key=="faking") then
--skip writing zoom or meeting state to watchformeeting as they are read-only fields
elseif(key=="menubar") then
_internal.menubar = setmetatable(value, {__index=_internal.menubarDefaults, __newindex=_internal.menubar__newIndex})
if(_internal.menubar.enabled) then
_internal.meetingMenuBar:returnToMenuBar()
_internal.updateMenuIcon(_internal.meetingState, _internal.faking)
else
_internal.meetingMenuBar:removeFromMenuBar()
end
elseif(key=="mode") then
if(value == 1) then
table:fake()
else
table:auto()
end
elseif(key=="sharing") then
_internal.sharing = setmetatable(value, {__index=_internal.sharingDefaults})
else
return rawset(table, key, value)
end
end
})
-------------------------------------------
-- End of Declare Variables
-------------------------------------------
-------------------------------------------
-- Menu Bar
-------------------------------------------
_internal.meetingMenuBar = hs.menubar.new(false)
function _internal.updateMenuIcon(status, faking)
if(_internal.menubar.enabled) then
local iconPath = 'menubar-icons/'
if(_internal.menubar.color) then
iconPath = iconPath..'Color/'
else
iconPath = iconPath..'Template/'
end
if(_internal.menubar.detailed) then
iconPath = iconPath..'Detailed/'
else
iconPath = iconPath..'Minimal/'
end
local iconFile = ""
if(status) then
iconFile = "Meeting"
if(_internal.menubar.showFullState and (status.mic_open or status.video_on or status.sharing)) then
if(status.mic_open) then iconFile = iconFile.."-Mic" end
if(status.video_on) then iconFile = iconFile.."-Vid" end
if(status.sharing) then iconFile = iconFile.."-Screen" end
end
if(faking) then iconFile = iconFile.."-Faking" end
iconFile = iconFile..".pdf"
else
iconFile = "Free.pdf"
end
_internal.meetingMenuBar:setIcon(hs.spoons.resourcePath(iconPath..iconFile),not _internal.menubar.color)
end
end
-------------------------------------------
-- End of Menu Bar
-------------------------------------------
-------------------------------------------
-- Web Server
-------------------------------------------
_internal.server = nil
_internal.websocketStatus = "closed"
local function composeJsonUpdate(meetingState)
local message = {action="update", inMeeting=meetingState}
return hs.json.encode(message)
end
local monitorfile = io.open(hs.spoons.resourcePath("monitor.html"), "r")
local htmlContent = monitorfile:read("*a")
monitorfile:close()
local function selfhostHttpCallback()
local websocketPath = "ws://"..hs.network.interfaceDetails(hs.network.primaryInterfaces())["IPv4"]["Addresses"][1]..":"..WatchForMeeting.sharing.port.."/ws"
htmlContent = string.gsub(htmlContent,"%%websocketpath%%",websocketPath)
return htmlContent, 200, {}
end
local function selfhostWebsocketCallback(msg)
return composeJsonUpdate(_internal.meetingState)
end
-------------------------------------------
-- End Web Server
-------------------------------------------
-------------------------------------------
-- Zoom Monitor
-------------------------------------------
local function emit(event)
local fns=_internal.events[event]
if fns then
for fn in pairs(fns) do fn() end
end
end
local function updateCallbacks()
if(_internal.server and _internal.websocketStatus == "open") then _internal.server:send(composeJsonUpdate(_internal.meetingState)) end
-- Emit appropriate events
if type(_internal.meetingState)~=type(_internal.lastMeetingState) then
emit(WatchForMeeting.meetingChange)
if _internal.meetingState==false then
if type(_internal.lastMeetingState)=="table" then
emit(WatchForMeeting.micChange)
emit(WatchForMeeting.videoChange)
emit(WatchForMeeting.screensharingChange)
if _internal.lastMeetingState.mic_open then
emit(WatchForMeeting.micOff)
end
if _internal.lastMeetingState.video_on then
emit(WatchForMeeting.videoOff)
end
if _internal.lastMeetingState.sharing then
emit(WatchForMeeting.screensharingOff)
end
end
emit(WatchForMeeting.meetingStopped)
else
emit(WatchForMeeting.meetingStarted)
end
end
if type(_internal.meetingState)=="table" then
if not _internal.lastMeetingState or _internal.lastMeetingState.mic_open~=_internal.meetingState.mic_open then
emit(WatchForMeeting.micChange)
if _internal.meetingState.mic_open then
emit(WatchForMeeting.micOn)
else
emit(WatchForMeeting.micOff)
end
end
if not _internal.lastMeetingState or _internal.lastMeetingState.video_on~=_internal.meetingState.video_on then
emit(WatchForMeeting.videoChange)
if _internal.meetingState.video_on then
emit(WatchForMeeting.videoOn)
else
emit(WatchForMeeting.videoOff)
end
end
if not _internal.lastMeetingState or _internal.lastMeetingState.sharing~=_internal.meetingState.sharing then
emit(WatchForMeeting.screensharingChange)
if _internal.meetingState.sharing then
emit(WatchForMeeting.screensharingOn)
else
emit(WatchForMeeting.screensharingOff)
end
end
end
_internal.lastMeetingState = _internal.meetingState
end
local function currentlyInMeeting()
--If zoom is running and the second menu in zoom's menu bar is "Meeting" then we're in a meeting
local inMeetingState = (_internal.zoom ~= nil and _internal.zoom:getMenuItems()[2].AXTitle == "Meeting")
return inMeetingState
end
--declare startStopWatchMeeting before watchMeeting, define it after.
local startStopWatchMeeting = function() end
local watchMeeting = hs.timer.new(0.5, function()
if(currentlyInMeeting() == false) then
_internal.updateMenuIcon(false)
-- No longer in a meeting, stop watching the meeting
startStopWatchMeeting()
updateCallbacks()
return
else
_internal.updateMenuIcon(_internal.meetingState, _internal.faking)
--Watch for zoom menu items
local _mic_open = _internal.zoom:findMenuItem({"Meeting", "Unmute audio"})==nil
local _video_on = _internal.zoom:findMenuItem({"Meeting", "Start video"})==nil
local _sharing = _internal.zoom:findMenuItem({"Meeting", "Start share"})==nil
if((_internal.meetingState.mic_open ~= _mic_open) or (_internal.meetingState.video_on ~= _video_on) or (_internal.meetingState.sharing ~= _sharing)) then
_internal.meetingState = {mic_open = _mic_open, video_on = _video_on, sharing = _sharing}
WatchForMeeting.logger.d("In Meeting: ", (_internal.meetingState and true)," Open Mic: ",_internal.meetingState.mic_open," Video-ing:",_internal.meetingState.video_on," Sharing",_internal.meetingState.sharing)
updateCallbacks()
end
end
end)
startStopWatchMeeting = function()
if(not _internal.faking) then
if(_internal.meetingState == false and currentlyInMeeting() == true) then
_internal.updateMenuIcon(_internal.meetingState, _internal.faking)
WatchForMeeting.logger.d("Start Meeting")
_internal.meetingState = {}
watchMeeting:start()
watchMeeting:fire()
elseif(_internal.meetingState and currentlyInMeeting() == false) then
_internal.updateMenuIcon(false)
WatchForMeeting.logger.d("End Meeting")
watchMeeting:stop()
_internal.meetingState = false
updateCallbacks()
end
else
--If we're faking the meeting we don't need watchMeeting to be regularly checking the status of the meeting elements
watchMeeting:stop()
end
end
local function checkMeetingStatus(window, name, event)
WatchForMeeting.logger.d("Check Meeting Status",window,name,event)
_internal.zoom = window:application()
startStopWatchMeeting()
end
-- Monitor zoom for running meeting
hs.application.enableSpotlightForNameSearches(true)
_internal.zoomWindowFilter = hs.window.filter.new(false,"ZoomWindowFilterLog",0):setAppFilter('zoom.us')
_internal.zoomWindowFilter:subscribe(hs.window.filter.hasWindow,checkMeetingStatus,true)
_internal.zoomWindowFilter:subscribe(hs.window.filter.hasNoWindows,checkMeetingStatus)
_internal.zoomWindowFilter:subscribe(hs.window.filter.windowDestroyed,checkMeetingStatus)
_internal.zoomWindowFilter:subscribe(hs.window.filter.windowTitleChanged,checkMeetingStatus)
_internal.zoomWindowFilter:pause()
-------------------------------------------
-- End of Zoom Monitor
-------------------------------------------
_internal.connectionAttempts = 0
_internal.connectionError = false
--Declare function before start connection because they're circular
local function retryConnection()
end
local function stopConnection()
if(_internal.server) then
if(getmetatable(_internal.server).stop) then _internal.server:stop() end
if(getmetatable(_internal.server).close) then _internal.server:close() end
end
end
local function serverWebsocketCallback(type, message)
if(type=="open") then
_internal.websocketStatus = "open"
_internal.connectionAttempts = 0
local draft = {action="identify", key=WatchForMeeting.sharing.key, type="room", status={inMeeting=_internal.meetingState}}
_internal.server:send(hs.json.encode(draft))
elseif(type == "closed" and _internal.running) then
_internal.websocketStatus = "closed"
if(_internal.connectionError) then
WatchForMeeting.logger.d("Lost connection to websocket, will not reattempt due to error")
else
WatchForMeeting.logger.d("Lost connection to websocket, attempting to reconnect in "..WatchForMeeting.sharing.waitBeforeRetry.." seconds")
retryConnection()
end
elseif(type == "fail") then
_internal.websocketStatus = "fail"
if(WatchForMeeting.sharing.maxConnectionAttempts > 0) then
WatchForMeeting.logger.d("Could not connect to websocket server. attempting to reconnect in "..WatchForMeeting.sharing.waitBeforeRetry.." seconds. (Attempt ".._internal.connectionAttempts.."/"..WatchForMeeting.sharing.maxConnectionAttempts..")")
else
WatchForMeeting.logger.d("Could not connect to websocket server. attempting to reconnect in "..WatchForMeeting.sharing.waitBeforeRetry.." seconds. (Attempt ".._internal.connectionAttempts..")")
end
retryConnection()
elseif(type == "received") then
local parsed = hs.json.decode(message);
if(parsed.error) then
_internal.connectionError = true;
if(parsed.errorType == "badkey") then
stopConnection()
hs.showError("")
WatchForMeeting.logger.e("WatchForMeeting.sharing.key not valid. Make sure that key has been established on the server.")
end
else
WatchForMeeting.logger.d("Websocket Message received: ", hs.inspect.inspect(parsed));
end
else
WatchForMeeting.logger.d("Websocket Callback "..type, message)
end
end
local function startConnection()
if(WatchForMeeting.sharing) then
if(WatchForMeeting.sharing.useServer) then
WatchForMeeting.logger.d("Connecting to server at "..WatchForMeeting.sharing.serverURL)
_internal.connectionAttempts = _internal.connectionAttempts + 1
_internal.websocketStatus = "connecting"
_internal.server = hs.websocket.new(WatchForMeeting.sharing.serverURL, serverWebsocketCallback);
else
WatchForMeeting.logger.d("Starting Self Hosted Server on port "..WatchForMeeting.sharing.port)
_internal.server = hs.httpserver.new()
_internal.server:websocket("/ws", selfhostWebsocketCallback)
_internal.websocketStatus = "open"
_internal.server:setPort(WatchForMeeting.sharing.port)
_internal.server:setCallback(selfhostHttpCallback)
_internal.server:start()
end
end
end
--redefine retryConnection now that startConnection & stopConnection exist.
retryConnection = function()
if(WatchForMeeting.sharing.maxConnectionAttempts > 0 and _internal.connectionAttempts >= WatchForMeeting.sharing.maxConnectionAttempts) then
WatchForMeeting.logger.e("Maximum Connection Attempts failed")
stopConnection()
elseif(_internal.connectionError) then
stopConnection()
else
hs.timer.doAfter(WatchForMeeting.sharing.waitBeforeRetry, startConnection)
end
end
function validateShareSettings()
WatchForMeeting.logger.d("validateShareSettings")
if(WatchForMeeting.sharing.useServer and (WatchForMeeting.sharing.serverURL==nil or WatchForMeeting.sharing.key==nil)) then
hs.showError("")
if(WatchForMeeting.sharing.serverURL==nil) then WatchForMeeting.logger.e("WatchForMeeting.sharing.serverURL required when using a server") end
if(WatchForMeeting.sharing.key==nil) then WatchForMeeting.logger.e("WatchForMeeting.sharing.key required when using a server") end
return false
elseif(not WatchForMeeting.sharing.useServer and WatchForMeeting.sharing.port==nil) then
hs.showError("")
WatchForMeeting.logger.e("WatchForMeeting.sharing.port required when self hosting")
return false
else
return true
end
end
-------------------------------------------
-- Methods
-------------------------------------------
--- WatchForMeeting:start() -> WatchForMeeting
--- Method
--- Starts a WatchForMeeting object
---
--- Parameters:
--- * None
---
--- Returns:
--- * The spoon.WatchForMeeting object
function WatchForMeeting:start()
if(not _internal.running) then
_internal.running = true
if(self.sharing.enabled and validateShareSettings()) then
startConnection()
end
if(self.menubar.enabled) then
_internal.meetingMenuBar:returnToMenuBar()
end
if(_internal.mode == 1 ) then
self:fake()
else
self:auto()
end
end
return self
end
--- WatchForMeeting:stop()
--- Method
--- Stops a WatchForMeeting object
---
--- Parameters:
--- * None
---
--- Returns:
--- * The spoon.WatchForMeeting object
function WatchForMeeting:stop()
_internal.running = false
stopConnection()
_internal.lastMeetingState = nil
_internal.meetingMenuBar:removeFromMenuBar()
_internal.zoomWindowFilter:pause()
return self
end
--- WatchForMeeting:start()
--- Method
--- Restarts a WatchForMeeting object
---
--- Parameters:
--- * None
---
--- Returns:
--- * The spoon.WatchForMeeting object
function WatchForMeeting:restart()
self:stop()
return self:start()
end
--- WatchForMeeting:auto()
--- Method
--- Monitors Zoom and updates status accordingly
---
--- Parameters:
--- * None
---
--- Returns:
--- * The spoon.WatchForMeeting object
function WatchForMeeting:auto()
_internal.mode = 0
if(_internal.running) then
_internal.faking = false
_internal.meetingState = false
startStopWatchMeeting()
_internal.meetingMenuBar:setMenu({
{ title = "Meeting Status:", disabled = true },
{ title = "Automatic", checked = true },
{ title = "Busy", checked = false, fn=function() WatchForMeeting:fake() end }
})
--Update everything
_internal.updateMenuIcon(_internal.meetingState, _internal.faking)
updateCallbacks()
--turn on the zoom window monitor
_internal.zoomWindowFilter:resume()
end
return self
end
--- WatchForMeeting:fake(mic_open, video_on, sharing)
--- Method
--- Disables monitoring and reports as being in a meeting. Useful when meeting type is not supported (currently any platform that isn't zoom.)
---
--- Parameters:
--- * mic_open - A boolean indicating if the mic is open
--- * video_on - A boolean indicating if the video camera is on
--- * sharing - A boolean indicating if screen sharing is on
---
--- Returns:
--- * The spoon.WatchForMeeting object
function WatchForMeeting:fake(_mic_open, _video_on, _sharing)
_internal.mode = 1
if(_internal.running) then
_internal.faking = true
_internal.meetingState = {mic_open = _mic_open, video_on = _video_on, sharing = _sharing}
startStopWatchMeeting()
local meetingMenu = {
{ title = "Meeting Status:", disabled = true },
{ title = "Automatic", checked = false, fn=function() WatchForMeeting:auto() end },
{ title = "Busy", checked = true },
{ title = "-"}
}
if(not (_mic_open and _video_on and _sharing)) then
table.insert(meetingMenu, { title = "Select All", fn=function() WatchForMeeting:fake(true, true, true) end })
else
table.insert(meetingMenu, { title = "Select None", fn=function() WatchForMeeting:fake(false, false, false) end })
end
table.insert(meetingMenu, { title = "Mic On", indent=1, checked = _internal.meetingState.mic_open, fn=function() WatchForMeeting:fake(not _mic_open, _video_on, _sharing) end})
table.insert(meetingMenu, { title = "Video On", indent=1, checked = _internal.meetingState.video_on, fn=function() WatchForMeeting:fake(_mic_open, not _video_on, _sharing) end })
table.insert(meetingMenu, { title = "Sharing Screen", indent=1, checked = _internal.meetingState.sharing, fn=function() WatchForMeeting:fake(_mic_open, _video_on, not _sharing) end })
if(_mic_open or _video_on or _sharing) then
table.insert(meetingMenu, { title = "Clear", fn=function() WatchForMeeting:fake(false, false, false) end })
end
_internal.meetingMenuBar:setMenu(meetingMenu)
_internal.zoomWindowFilter:pause()
updateCallbacks()
_internal.updateMenuIcon(_internal.meetingState, _internal.faking)
end
return self
end
--- WatchForMeeting:subscribe(event, fn)
--- Method
--- Subscribe to one event with one or more functions
---
--- Parameters:
--- * event - string of the event to subscribe to (see the `spoon.WatchForMeeting` constants)
--- * fn - function or list of functions, the callback(s) to add for the event(s);
---
--- Returns:
--- * The `spoon.WatchForMeeting` object for method chaining
function WatchForMeeting:subscribe(event, fns)
if not events[event] then error('invalid event: '..event,3) end
if type(fns)=='function' then fns = {fns} end
if type(fns)~='table' then error('fn must be a function or table of functions',3) end
for _,fn in pairs(fns) do
if type(fn)~='function' then error('fn must be a function or table of functions',3) end
if not _internal.events[event] then _internal.events[event]={} end
if not _internal.events[event][fn] then
_internal.events[event][fn]=true
WatchForMeeting.logger.df('added callback for event %s',event)
end
end
return self
end
--- WatchForMeeting:unsubscribe(event, fn) -> hs.window.filter object
--- Method
--- Removes one or more event subscriptions
---
--- Parameters:
--- * event - string of the event to unsubscribe;
--- * fn - function or list of functions, the callback(s) to remove; if omitted, all callbacks will be unsubscribed from `event`(s)
---
--- Returns:
--- * The `spoon.WatchForMeeting` object for method chaining
---
function WatchForMeeting:unsubscribe(event,fn)
if _internal.events[event] and _internal.events[event][fn] then
WatchForMeeting.logger.df('removed callback for event %s',event)
_internal.events[event][fn]=nil
if not next(_internal.events[event]) then
WatchForMeeting.logger.df('no more callbacks for event %s',event)
_internal.events[event]=nil
end
end
end
--- WatchForMeeting:unsubscribeEvent(event) -> hs.window.filter object
--- Method
--- Removes all subscriptions from one event
---
--- Parameters:
--- * event - string of the event to unsubscribe; ;
---
--- Returns:
--- * The `spoon.WatchForMeeting` object for method chaining
---
function WatchForMeeting:unsubscribeEvent(event)
if not events[event] then error('invalid event: '..event,3) end
if _internal.events[event] then WatchForMeeting.logger.df('removed all callbacks for event %s',event) end
_internal.events[event]=nil
end
-------------------------------------------
-- End of Methods
-------------------------------------------
return WatchForMeeting