summaryrefslogtreecommitdiffstats
path: root/emacsconf.el
blob: 60f9ac2a564a2a73d46dd7d8a2e0464195bdbe07 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
;;; emacsconf.el --- Core functions and variables for EmacsConf  -*- lexical-binding: t; -*-

;; Copyright (C) 2021  Sacha Chua

;; Author: Sacha Chua <sacha@sachachua.com>
;; Keywords: multimedia

;; 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/>.

;;; Commentary:

;; 

;;; Code:

(defgroup emacsconf nil "EmacsConf" :group 'multimedia)

(defcustom emacsconf-id "emacsconf"
  "ID of conference"
  :group 'emacsconf
  :type 'string)
(defcustom emacsconf-name "EmacsConf"
  "Name of conference"
  :group 'emacsconf
  :type 'string)
(defcustom emacsconf-year "2022"
  "Conference year. String for easy inclusion."
  :group 'emacsconf
  :type 'string)
(defcustom emacsconf-directory "~/vendor/emacsconf-wiki"
  "Directory where the wiki files are."
  :group 'emacsconf
  :type 'directory)
(defcustom emacsconf-ansible-directory nil
  "Directory where the Ansible repository is."
  :group 'emacsconf
  :type 'directory)

(defcustom emacsconf-timezone "US/Eastern" "Main timezone."
  :group 'emacsconf
  :type 'string)

(defcustom emacsconf-timezones '("US/Eastern" "US/Central" "US/Mountain" "US/Pacific" "UTC" "Europe/Paris" "Europe/Athens" "Asia/Kolkata" "Asia/Singapore" "Asia/Tokyo") "List of timezones."
  :group 'emacsconf
  :type '(repeat string))

(defcustom emacsconf-base-url "https://emacsconf.org/" "Includes trailing slash"
  :group 'emacsconf
  :type 'string)
(defcustom emacsconf-publishing-phase 'program
  "Controls what information to include.
'program - don't include times
'schedule - include times; use this leading up to the emacsconference
'resources - after EmacsConf, don't need status"
  :group 'emacsconf
  :type '(choice
          (const :tag "Program: Don't include times" program)
          (const :tag "Schedule: Include detailed times" schedule)
          (const :tag "Resources: Don't include status" resources)))

(defcustom emacsconf-org-file nil
  "Path to the Org file with emacsconference information."
  :type 'file
  :group 'emacsconf)

(defcustom emacsconf-upcoming-file nil
  "Path to the Org file with upcoming talks."
  :type 'file
  :group 'emacsconf)

(defvar emacsconf-stream-base "https://live0.emacsconf.org/emacsconf/")
(defvar emacsconf-chat-base "https://chat.emacsconf.org/")
(defvar emacsconf-backstage-dir "/ssh:orga@media.emacsconf.org:/var/www/media.emacsconf.org/2022/backstage")
(defvar emacsconf-upload-dir "/ssh:orga@media.emacsconf.org:/srv/upload")
(defvar emacsconf-res-dir (format "/ssh:orga@res.emacsconf.org:/data/emacsconf/%s" emacsconf-year))
(defvar emacsconf-media-extensions '("webm" "mkv" "mp4" "webm" "avi" "ts" "ogv" "wav" "ogg" "mp3"))

(defun emacsconf-upload-dired ()
  (interactive)
  (dired emacsconf-upload-dir "-tl"))
(defun emacsconf-backstage-dired ()
  (interactive)
  (dired emacsconf-backstage-dir "-tl"))
(defun emacsconf-res-dired ()
  (interactive)
  (dired emacsconf-res-dir "-tl"))
(defun emacsconf-cache-dired ()
  (interactive)
  (dired emacsconf-cache-dir "-tl"))
(defun emacsconf-slugify (s)
  (replace-regexp-in-string " +" "-" (replace-regexp-in-string "[^a-z0-9 ]" "" (downcase s))))

(defun emacsconf-video-slug (talk)
  (concat "emacsconf-" emacsconf-year "-" (plist-get talk :slug) "--"
          (emacsconf-slugify (plist-get talk :title))
          (if (plist-get talk :speakers)
              (concat"--"
                     (emacsconf-slugify (plist-get talk :speakers)))
            "")))



(defun emacsconf-set-video-slug-if-needed (o)
  (interactive (list (emacsconf-complete-talk-info)))
  (unless (plist-get o :video-slug)
    (let ((video-slug
           (read-string "Set video slug: " (emacsconf-video-slug o))))
      (save-window-excursion
        (emacsconf-with-talk-heading (plist-get o :slug)
          (org-entry-put (point) "VIDEO_SLUG" video-slug)))
      (plist-put o :video-slug video-slug)))
  (plist-get o :video-slug))

(defun emacsconf-set-video-slugs ()
  (interactive)
  (org-map-entries
   (lambda ()
     (org-entry-put (point) "VIDEO_SLUG" (emacsconf-video-slug (emacsconf-get-talk-info-for-subtree))))
   "SLUG={.}-VIDEO_SLUG={.}"))

(defun emacsconf-upload-to-backstage ()
  (interactive)
  (copy-file (buffer-file-name) (expand-file-name (file-name-nondirectory (buffer-file-name))
                                                  emacsconf-backstage-dir)
             t))
(defun emacsconf-get-srv2-and-upload-to-backstage (talk)
  (interactive (list (emacsconf-complete-talk-info (seq-filter (lambda (o) (plist-get o :youtube-url)) (emacsconf-get-talk-info)))))
  (let ((filename (make-temp-file nil nil "srv2"))
        (buf (get-buffer-create "*test*")))
    (when (plist-get talk :youtube-url)
      (call-process "yt-dlp" nil buf t "--write-sub" "--write-auto-sub" "--no-warnings" "--sub-lang" "en" "--skip-download" "--sub-format" "srv2" "-o" filename
                    (plist-get talk :youtube-url))
      (with-current-buffer (find-file-noselect (concat filename ".en.srv2"))
        (emacsconf-upload-to-backstage-and-rename talk "main--srt")))))

(defun emacsconf-upload-to-backstage-and-rename (talk filename)
  (interactive (let ((talk (emacsconf-complete-talk-info))
                     (base (file-name-base (buffer-file-name))))
                 (list
                  talk
                  (if (and (not current-prefix-arg)
                           (string-match (concat "^" (regexp-quote (plist-get talk :video-slug)) "--\\([a-z]+\\)")
                                         base))
                      (match-string 1 base)
                    (read-string (format "Filename (%s): " base)
                                 nil nil
                                 base)))))
  (copy-file (buffer-file-name)
             (expand-file-name (concat (plist-get talk :video-slug)
                                       "--"
                                       filename
                                       "."
                                       (file-name-extension (buffer-file-name)))
                               emacsconf-backstage-dir)
             t))

(defun emacsconf-upload-copy-from-json (talk key filename)
  (interactive (let-alist (json-parse-string (buffer-string) :object-type 'alist)
                 (list (emacsconf-complete-talk-info)
                       .metadata.key
                       (read-string (format "Filename: ")))))
  (let ((new-filename (concat (plist-get talk :video-slug)
                              (if (string= filename "")
                                  filename
                                (concat "--" filename))
                              "."
                              (let-alist (json-parse-string (buffer-string) :object-type 'alist)
                                (file-name-extension .metadata.name)))))
    (copy-file key
               (expand-file-name new-filename
                                 emacsconf-backstage-dir) t)
    ;; (copy-file key (expand-file-name new-filename emacsconf-cache-dir))
    ;; (unless (file-directory-p (expand-file-name (plist-get talk :slug) emacsconf-res-dir))
    ;;   (make-directory (expand-file-name (plist-get talk :slug) emacsconf-res-dir)))
    ;; (copy-file (expand-file-name new-filename emacsconf-cache-dir)
    ;;            (expand-file-name new-filename (expand-file-name (plist-get talk :slug) emacsconf-res-dir)))
    ))

(defcustom emacsconf-download-directory "~/Downloads"
  "Directory to check for downloaded files."
  :type 'directory
  :group 'emacsconf)

(defun emacsconf-latest-file (path &optional filter)
  "Return the newest file in PATH. Optionally filter by FILTER."
  (car (sort (seq-remove #'file-directory-p (directory-files path 'full filter t)) #'file-newer-than-file-p)))

(defun emacsconf-find-captions-from-slug (search)
  (interactive (list (emacsconf-complete-talk)))
  (emacsconf-with-talk-heading search (emacsconf-subed-find-captions)))

(defun emacsconf-edit-wiki-page (search)
  (interactive (list (emacsconf-complete-talk)))
  (setq search (emacsconf-get-slug-from-string search))
  (find-file (expand-file-name (concat search ".md")
                               (expand-file-name "talks" (expand-file-name emacsconf-year emacsconf-directory)))))

(defun emacsconf-find-caption-directives-from-slug (search)
  (interactive (list (emacsconf-complete-talk)))
  (setq search (emacsconf-get-slug-from-string search))
  (find-file (expand-file-name (concat search ".md")
                               (expand-file-name "captions" (expand-file-name emacsconf-year emacsconf-directory)))))


(defun emacsconf-browse-wiki-page (search)
  (interactive (list (emacsconf-complete-talk)))
  (setq search (emacsconf-get-slug-from-string search))
  (browse-url (concat emacsconf-base-url emacsconf-year "/talks/" search "/")))

(defun emacsconf-set-property-from-slug (search prop value)
  (interactive (list (emacsconf-complete-talk) nil nil))
  (save-window-excursion
    (emacsconf-with-talk-heading search
      (setq prop (or prop (org-read-property-name)))
      (setq value (or value (org-read-property-value prop)))
      (org-entry-put (point) prop value))))


(defun emacsconf-complete-slug ()
  (emacsconf-get-slug-from-string (emacsconf-complete-talk)))

(defun emacsconf-export-slug (link description format _)
  (let ((path (format "https://emacsconf.org/%s/talks/%s" emacsconf-year link))
        (desc (or description link)))
    (pcase format
      (`html
       (format "<a href=\"#%s\">%s</a>" link desc))
      (`ascii (format "%s (%s)" desc path))
      (`markdown
       (format "[[%s|%s/talks/%s]]" desc emacsconf-year link))
      (_ path))))

(with-eval-after-load 'org
  (org-link-set-parameters
   "emacsconf"
   :follow #'emacsconf-go-to-talk
   :complete (lambda () (concat "emacsconf:" (emacsconf-complete-slug)))
   :export #'emacsconf-export-slug))


(defun emacsconf-complete-talk (&optional info)
  (let ((choices
         (mapcar (lambda (o)
                   (string-join
                    (delq nil
                          (mapcar (lambda (f) (plist-get o f)) '(:slug :title :speakers :irc)))
                    " - "))
                 (or info (emacsconf-get-talk-info)))))
    (completing-read
     "Talk: " 
     (lambda (string predicate action)
       (if (eq action 'metadata)
           '(metadata (category . emacsconf))
         (complete-with-action action choices string predicate))))))

(defun emacsconf-complete-talk-info (&optional info)
  (emacsconf-search-talk-info (emacsconf-complete-talk info) info))

(defun emacsconf-get-slug-from-string (search)
  (if (listp search) (setq search (car search)))
  (if (and search (string-match "\\(.*?\\) - " search))
      (match-string 1 search)
    search))

(defun emacsconf-go-to-talk (search)
  (interactive (list (emacsconf-complete-talk)))
  (pop-to-buffer (find-file-noselect emacsconf-org-file))
  (widen)
  (cond
   ((plist-get search :slug)
    (goto-char (org-find-property "SLUG" (plist-get search :slug))))
   ((emacsconf-get-slug-from-string search)
    (goto-char (org-find-property "SLUG" (emacsconf-get-slug-from-string search))))
   (t
    (goto-char
     (catch 'found
       (org-map-entries
        (lambda ()
          (when (string-match search
                              (cons
                               (concat (org-entry-get (point) "SLUG") " - "
                                       (org-entry-get (point) "ITEM") " - "
                                       (org-entry-get (point) "NAME") " - "
                                       (org-entry-get (point) "EMAIL"))
                               (point)))
            (throw 'found (point))))
        "SLUG={.}")))))
  (org-reveal))

(defmacro emacsconf-for-each-talk (&rest body)
  (declare (indent 0) (debug t))
  `(org-map-entries (lambda () ,@body) "SLUG={.}"))

(defmacro emacsconf-with-talk-heading (search &rest body)
  (declare (indent 1) (debug t))
  `(progn
     (emacsconf-go-to-talk ,search)
     ,@body))

(defvar emacsconf-status-types
  '(("WAITING_FOR_PREREC" . "Waiting for video from speaker")
    ("TO_PROCESS" . "Processing uploaded video")
    ("TO_AUTOCAP" . "Processing uploaded video")
    ("TO_ASSIGN" . "Waiting for a caption volunteer")
    ("TO_CAPTION" . "Processing uploaded video")
    ("TO_STREAM" . "Talk captioned")
    ("PLAYING" . "Now playing on the conference livestream")
    ("CLOSED_Q" . "Q&A starting (not yet open for joining)")
    ("OPEN_Q" . "Q&A open for participation")
    ("UNSTREAMED_Q" . "Q&A continues off the stream")
    ("TO_ARCHIVE" . "Q&A finished, IRC and pad will be archived on this page")
    ("TO_EXTRACT" . "Q&A to be extracted from the room recordings")
    ("DONE" . "All done")
    ("CANCELLED" . "Talk cancelled")))

(defun emacsconf-get-talk-categories (o)
  (org-narrow-to-subtree)
  (let (list)
    (while (re-search-forward "Category[^ \t\n]+" nil t)
      (setq list (cons (match-string-no-properties 0) list)))
    (plist-put o :categories (reverse list))))

(defun emacsconf-get-talk-info-from-properties (o)
  (let ((heading (org-heading-components))
        (field-props '(                 
                       ;; Initial creation
                       (:title "ITEM")
                       (:track "TRACK")
                       (:slug "SLUG")
                       (:speakers "NAME")                       
                       (:speakers-short "NAME_SHORT")
                       (:email "EMAIL")
                       (:public-email "PUBLIC_EMAIL")
                       (:emergency "EMERGENCY")
                       (:buffer "BUFFER")
                       (:duration "TIME")
                       (:min-time "MIN_TIME")
                       (:max-time "MAX_TIME")
                       (:availability "AVAILABILITY")
                       (:q-and-a "Q_AND_A")
                       (:timezone "TIMEZONE")
                       (:irc "IRC")
                       (:pronunciation "PRONUNCIATION")                       
                       (:pronouns "PRONOUNS") 
                       ;; Scheduling
                       (:scheduled "SCHEDULED")
                       (:time "TIME")
                       (:fixed-time "FIXED_TIME")
                       ;; Coordination
                       (:prerec-info "PREREC_INFO")
                       ;; Prep
                       (:bbb-room "ROOM")                       
                       ;; Processing
                       (:video-slug "VIDEO_SLUG")
                       (:video-file "VIDEO_FILE")                       
                       (:video-time "VIDEO_TIME")                       
                       (:video-file-size "VIDEO_FILE_SIZE")                       
                       (:video-duration "VIDEO_DURATION")
                       (:youtube-url "YOUTUBE_URL")                       
                       (:toobnix-url "TOOBNIX_URL")
                       ;; Captioning
                       (:captioner "CAPTIONER")
                       (:caption-note "CAPTION_NOTE")
                       (:intro-note "INTRO_NOTE")
                       ;; Conference
                       (:check-in "CHECK_IN")
                       (:public "PUBLIC")
                       ;; Extraction
                       (:qa-youtube "QA_YOUTUBE")
                       (:qa-toobnix "QA_TOOBNIX")
                       ;; Old
                       (:alternate-apac "ALTERNATE_APAC")                       
                       (:extra-live-time "EXTRA_LIVE_TIME")
                       (:present "PRESENT")
                       (:talk-id "TALK_ID") ; use slug instead
                       (:qa-public "QA_PUBLIC") ; now tracked by the OPEN_Q and UNSTREAMED_Q status
                       (:uuid "UUID")           ; Pentabarf export
                       )))
    (apply
     'append
     o
     (list
      :point (point)
      :year emacsconf-year
      :conf-year emacsconf-year
      :type (if (org-entry-get (point) "SLUG") 'talk 'headline)
      :status (elt heading 2)
      :level (car heading)
      :url (concat emacsconf-year "/talks/" (org-entry-get (point) "SLUG"))
      :schedule-group (org-entry-get-with-inheritance "SCHEDULE_GROUP")
      :wiki-file-path
      (expand-file-name 
       (concat (org-entry-get (point) "SLUG") ".md")
       (expand-file-name "captions" (expand-file-name emacsconf-year emacsconf-directory)))
      :start-time
      (when (org-entry-get (point) "SCHEDULED")
		    (date-to-time
		     (concat
		      (format-time-string "%Y-%m-%dT%H:%M:%S"
					                    (org-timestamp-to-time
					                     (org-timestamp-split-range
					                      (org-timestamp-from-string
					                       (org-entry-get (point) "SCHEDULED")))))
		      emacsconf-timezone-offset)))
      :end-time (when (org-entry-get (point) "SCHEDULED")
		              (date-to-time
		               (concat
		                (format-time-string "%Y-%m-%dT%H:%M:%S"
					                              (org-timestamp-to-time
					                               (org-timestamp-split-range
					                                (org-timestamp-from-string
					                                 (org-entry-get (point) "SCHEDULED"))
					                                t)))
		                emacsconf-timezone-offset))))
     (let* ((entry-props (org-entry-properties)))
       (mapcar 
        (lambda (o) (list (car o) (assoc-default (cadr o) entry-props)))
        field-props)))))

(defvar emacsconf-abstract-heading-regexp "abstract" "Regexp matching heading for talk abstract.")

(defun emacsconf-get-subtree-entry (heading-regexp)
  (car
   (delq nil
         (org-map-entries
          (lambda ()
            (when (string-match heading-regexp (org-entry-get (point) "ITEM"))
              (org-get-entry)))
          nil 'tree))))

(defun emacsconf-get-talk-abstract-from-subtree (o)
  "Add the abstract from a subheading with a title matching Abstract."
  (plist-put o :abstract (substring-no-properties (or (emacsconf-get-subtree-entry "abstract") ""))))


(defun emacsconf-get-talk-comments-from-subtree (o)
  (setq o (plist-put o :comments
                     (apply 'append
                            (org-map-entries
                             (lambda ()
                               (org-end-of-meta-data)
                               (mapcar (lambda (item)
                                         (string-trim
                                          (replace-regexp-in-string
                                           " *\n *"
                                           " "
                                           (buffer-substring-no-properties (+ (car item) (length (elt item 2)))
                                                                           (min (point-max) (elt item 6))))))
                                       (org-element-property  :structure (org-element-at-point)))
                               )
                             "ITEM={comments}" 'tree))))
  (plist-put o :acceptance-comment
             (car (delq nil (mapcar
                             (lambda (o)
                               (when (string-match "For the [^ ]+ speakers?: " o)
                                 (replace-match "" t t o)))
                             (plist-get o :comments))))))

(defun emacsconf-convert-talk-abstract-to-markdown (o)
  (plist-put o :abstract-md (org-export-string-as (or (plist-get o :abstract) "") 'md t)))

(defun emacsconf-summarize-times (time timezones)
  (let (prev-day)
    (mapconcat
     (lambda (tz)
       (let ((cur-day (format-time-string "%a %b %-e" time tz))
             (cur-time (format-time-string "%H%MH %Z" time tz)))
         (if (equal prev-day cur-day)
             cur-time
           (setq prev-day cur-day)
           (concat cur-day " " cur-time))))
     timezones
     " / ")))

(defun emacsconf-add-timezone-conversions (o)
  (plist-put o :scheduled-tzs
             (concat (org-timestamp-format (plist-get o :start-time) "%a %b %e %l:%M%p Toronto time (")
                     (emacsconf-summarize-times (plist-get o :start-time) emacsconf-timezones)
                     ")")))

(defun emacsconf-get-abstract-from-wiki (o)
  (plist-put o :markdown (emacsconf-talk-markdown-from-wiki (plist-get o :slug))))


(defun emacsconf-add-talk-status (o)
  (plist-put o :status-label
             (or (assoc-default (plist-get o :status) 
                                emacsconf-status-types 'string= "")
                 (plist-get o :status)))
  (if (member (plist-get o :status)
              (split-string "PLAYING CLOSED_Q OPEN_Q UNSTREAMED_Q TO_ARCHIVE TO_EXTRACT TO_FOLLOW_UP DONE"))
      (plist-put o :public t))
  o)

(defun emacsconf-test-public-states ()
  (let ((states (split-string (replace-regexp-in-string "(.*?)" "" "TODO(t) TO_REVIEW TO_ACCEPT TO_CONFIRM WAITING_FOR_PREREC(w) TO_PROCESS(p) TO_AUTOCAP(y) TO_ASSIGN(a) TO_CAPTION(c) TO_STREAM(s) PLAYING(m) CLOSED_Q(q) OPEN_Q(o) UNSTREAMED_Q(u) TO_ARCHIVE TO_EXTRACT TO_FOLLOW_UP"))))
    (mapc (lambda (state) (assert (null (plist-get (emacsconf-add-talk-status
                                                    (list :status state))
                                                   :public))))
          (subseq states
                  0
                  (seq-position
                   states "PLAYING")))
    (mapc (lambda (state) (assert (plist-get (emacsconf-add-talk-status (list
                                                                         :status
                                                                         state))
                                             :public)))
          (subseq states (seq-position
                          states "PLAYING")))))

(defvar emacsconf-talk-info-functions
  '(emacsconf-get-talk-info-from-properties
    emacsconf-get-talk-categories
    emacsconf-get-talk-abstract-from-subtree
    emacsconf-add-talk-status
    emacsconf-add-timezone-conversions
    emacsconf-add-speakers-with-pronouns
    emacsconf-add-live-info))
(defun emacsconf-add-speakers-with-pronouns (o)
  (plist-put o :speakers-with-pronouns
             (cond
              ((null (plist-get o :pronouns)) (plist-get o :speakers))
              ((string= (plist-get o :pronouns) "nil") (plist-get o :speakers))
              ((string-match "(" (plist-get o :pronouns)) (plist-get o :pronouns))
              (t (format "%s (%s)" (plist-get o :speakers) (plist-get o :pronouns)))))
  o)

(defun emacsconf-add-live-info (o)
  (let ((track (emacsconf-get-track (plist-get o :track))))
    (when track
      (plist-put o :watch-url (concat emacsconf-base-url emacsconf-year "/watch/" (plist-get track :id))))
    (plist-put o :channel (plist-get track :channel))
    (plist-put o :webchat-url (concat emacsconf-chat-base "?join=emacsconf," (plist-get track :channel)))
    (cond
     ((string-match "live" (or (plist-get o :q-and-a) ""))
      (plist-put o :bbb-redirect (format "https://emacsconf.org/current/%s/room/" (plist-get o :slug)))
      (plist-put o :qa-info (plist-get o :bbb-redirect))
      (plist-put o :qa-link (format "<a href=\"%s\">live</a>" (plist-get o :bbb-redirect))))
     ((string-match "IRC" (or (plist-get o :q-and-a) ""))
      (plist-put o :qa-info (concat (emacsconf-surround "nick: " (plist-get o :irc) ", " "")
                                    (plist-get o :channel)))
      (plist-put o :qa-link (format "<a href=\"%s\">%s</a>" (plist-get o :webchat-url) (plist-get o :qa-info))))
     (t (plist-put o :qa-info "none")
        (plist-put o :qa-link "none")))
    (plist-put o :pad-url (format "https://pad.emacsconf.org/%s-%s" emacsconf-year (plist-get o :slug)))
    o))

(defun emacsconf-search-talk-info (search &optional info)
  (setq info (or info (emacsconf-get-talk-info)))
  (or
   (seq-find (lambda (o) (string= (plist-get o :slug)
                                  (emacsconf-get-slug-from-string search)))
             info)
   (seq-find (lambda (o)
               (string-match
                search
                (format "%s - %s - %s - %s"
                        (plist-get o :slug)
                        (plist-get o :title)
                        (plist-get o :speakers)
                        (plist-get o :email))))
             info)))

(defun emacsconf-get-talk-info-for-subtree ()
  (seq-reduce (lambda (prev val) (save-excursion (save-restriction (funcall val prev))))
              emacsconf-talk-info-functions
              nil))

(defun emacsconf-sort-by-scheduled (a b)
  (let ((time-a (plist-get a :start-time))
        (time-b (plist-get b :start-time)))
    (cond
     ((null time-b) t)
     ((null time-a) nil)
     ((time-less-p time-a time-b) t)
     ((time-less-p time-b time-a) nil)
     (t (< (or (plist-get a :point) 0) (or (plist-get b :point) 0))))))

(defun emacsconf-get-talk-info ()
  (with-current-buffer (find-file-noselect emacsconf-org-file)
    (save-excursion
      (save-restriction
        (widen)
        (let (results)
          (org-map-entries
           (lambda ()
             (when (or (org-entry-get (point) "TIME")
                       (org-entry-get (point) "SLUG")
                       (org-entry-get (point) "INCLUDE_IN_INFO"))
               (setq results
                     (cons (emacsconf-get-talk-info-for-subtree)
                           results)))))
          (nreverse results))))))

(defun emacsconf-filter-talks (list)
  "Return only talk info in LIST."
  (seq-filter
   (lambda (talk) (eq (plist-get talk :type) 'talk))
   list))

(defun emacsconf-collect-field-for-status (status field &optional info)
  (seq-map
   (lambda (o)
     (plist-get o field))
   (seq-filter
    (lambda (o)
      (if (listp status)
          (member (plist-get o :status) status)
        (string= status (plist-get o :status))))
    (emacsconf-filter-talks (or info (emacsconf-get-talk-info))))))


(defun emacsconf-get-talk-info-from-file (&optional filename)
  (with-temp-buffer
    (insert-file-contents (or filename "conf.org"))
    (org-mode)
    (org-show-all)
    (goto-char (point-min))
    (goto-char (org-find-property "ID" "talks"))
    (emacsconf-get-talk-info 'wiki)))

(defun emacsconf-include-next-talks (info number)
  (let* ((info (sort (emacsconf-active-talks (emacsconf-filter-talks (or info (emacsconf-get-talk-info))))
                     #'emacsconf-sort-by-scheduled))
         (cur-list info))
    ;; add links to the next talks
    (while cur-list
      (plist-put (pop cur-list) :next-talks (seq-take cur-list number)))
    info))

(defun emacsconf-find-talk-info (filter &optional info)
  (setq info (or info (emacsconf-filter-talks (emacsconf-get-talk-info))))
  (when (stringp filter) (setq filter (list filter)))
  (or (seq-find (lambda (o) (string= (plist-get o :slug) (car filter))) info)
      (seq-find (lambda (o)
                  (let ((case-fold-search t)
                        (all (mapconcat (lambda (f) (plist-get o f)) '(:title :speakers :slug) " ")))
                    (null (seq-contains-p
                           (mapcar (lambda (condition) (string-match condition all)) filter)
                           nil))))
                info)))

(defun emacsconf-combine-plist (list-of-talks separator)
  (let (result entry)
    (while list-of-talks
      (setq entry (car list-of-talks))
      (while entry
        (unless (equal (plist-get result (car entry))
                         (cadr entry))
          (setq result
                (plist-put result
                           (car entry)
                           (cons (cadr entry)
                                 (or (plist-get result (car entry)))))))
        (setq entry (cddr entry)))
      (setq list-of-talks (cdr list-of-talks)))
    result))

(defun emacsconf-goto-talk-id (id)
  (goto-char (org-find-property "TALK_ID" id)))

(defun emacsconf-goto-slug (slug)
  (goto-char (org-find-property "SLUG" id)))

(defun emacsconf-talk-markdown-from-wiki (slug)
  "Return the markdown from SLUG."
  (when (file-exists-p (expand-file-name (format "%s/talks/%s.md" emacsconf-year slug) emacsconf-directory))
    (with-temp-buffer
      (insert-file-contents (expand-file-name (format "%s/talks/%s.md" emacsconf-year slug) emacsconf-directory))
      (goto-char (point-min))
      (while (re-search-forward "<!--" nil t)
        (let ((start (match-beginning 0)))
          (when (re-search-forward "-->" nil t)
            (delete-region start (match-end 0)))))
      (goto-char (point-min))
      (while (re-search-forward "\\[\\[![^]]+\\]\\]" nil t)
        (replace-match ""))
      (string-trim (buffer-string)))))

(defun emacsconf-replace-plist-in-string (attrs string)
  "Replace ${keyword} from ATTRS in STRING."
  (let ((a attrs) name val)
    (while a
      (setq name (pop a) val (pop a))
      (when (stringp val)
        (setq string
              (replace-regexp-in-string (regexp-quote (concat "${" (substring (symbol-name name) 1) "}"))
                                        (or val "")
                                        string t t))))
    string))

(defun emacsconf-public-talks (info)
  (seq-filter (lambda (f) (plist-get f :public)) info))

(defun emacsconf-format-short-time (string &optional omit-end-time)
  (if (stringp string) (setq string (org-timestamp-from-string string)))
  (downcase
   (concat (format-time-string "~%l:%M%p"
                               (org-timestamp-to-time
                                (org-timestamp-split-range
                                 string)))
           (if omit-end-time ""
             (format-time-string "-%l:%M%p"
                                 (org-timestamp-to-time
                                  (org-timestamp-split-range
                                   string t)))))))

(defvar emacsconf-focus 'time "'time or 'status")

;;; Embark
(defun emacsconf-embark-finder ()
  (when (and (derived-mode-p 'org-mode)
             (org-entry-get-with-inheritance "SLUG"))
    (cons 'emacsconf (org-entry-get-with-inheritance "SLUG"))))

(defun emacsconf-insert-talk-title (search)
  (interactive (list (emacsconf-complete-talk)))
  (insert (plist-get (emacsconf-search-talk-info search) :title)))

(with-eval-after-load 'embark
  (add-to-list 'embark-target-finders 'emacsconf-embark-finder)
  (embark-define-keymap embark-emacsconf-actions
    "Keymap for emacsconference-related things"
    ("a" emacsconf-announce)
    ("c" emacsconf-find-captions-from-slug)
    ("d" emacsconf-find-caption-directives-from-slug)
    ("p" emacsconf-set-property-from-slug)
    ("w" emacsconf-edit-wiki-page)
    ("s" emacsconf-set-start-time-for-slug)
    ("W" emacsconf-browse-wiki-page)
    ("u" emacsconf-update-talk)
    ("it" emacsconf-insert-talk-title)
    ("m" emacsconf-mail-speaker-from-slug)
    ("n" emacsconf-notmuch-search-mail-from-entry)
    ("f" org-forward-heading-same-level)
    ("b" org-backward-heading-same-level)
    ("RET" emacsconf-go-to-talk))
  (add-to-list 'embark-keymap-alist '(emacsconf . embark-emacsconf-actions)))

;;; Status updates

(defun emacsconf-status-update ()
  (interactive)
  (let ((emacsconf-info (emacsconf-get-talk-info)))
    (kill-new
     (format "%d captioned (%d minutes), %d received and waiting to be captioned (%d minutes)"
             (length (emacsconf-collect-field-for-status "CAPTIONED" :title))
             (apply '+ (seq-map 'string-to-number (conf-collect-field-for-status "CAPTIONED" :duration)))
             (length (emacsconf-collect-field-for-status "PREREC_RECEIVED" :title))
             (apply '+ (seq-map 'string-to-number (conf-collect-field-for-status "PREREC_RECEIVED" :duration)))))))

;; Timezones
(defvar emacsconf-date "2022-12-03" "Starting date of EmacsConf.")
(defvar emacsconf-timezone-offset
  (format-time-string "%z" (date-to-time emacsconf-date) emacsconf-timezone)
  "Timezone offset for `emacsconf-timezone' on `emacsconf-date'.")

(defun emacsconf-timezone-string (o tz)
  (let* ((timestamp (org-timestamp-from-string (plist-get o :scheduled)))
         (start (org-timestamp-to-time (org-timestamp-split-range timestamp)))
         (end (org-timestamp-to-time (org-timestamp-split-range timestamp t))))
    (if (string= tz "UTC")
        (format "%s - %s "
                (format-time-string "%A, %b %-e %Y, ~%-l:%M %p"
                                    start tz)
                (format-time-string "%-l:%M %p %Z"
                                    end tz))
      (format "%s - %s (%s)"
              (format-time-string "%A, %b %-e %Y, ~%-l:%M %p"
                                  start tz)
              (format-time-string "%-l:%M %p %Z"
                                  end tz)
              tz))))
(defun emacsconf-timezone-strings (o &optional timezones)
  (mapcar (lambda (tz) (emacsconf-timezone-string o tz)) (or timezones emacsconf-timezones)))

(defun emacsconf-convert-from-timezone (timezone time)
  (interactive (list (completing-read "From zone: " tzc-time-zones)
                     (read-string "Time: ")))
  (let* ((from-offset (format-time-string "%z" (date-to-time emacsconf-date) timezone))
         (time
          (date-to-time
           (concat emacsconf-date "T" (string-pad time 5 ?0 t)  ":00.000"
                   from-offset))))
    (message "%s = %s"
             (format-time-string
              "%b %d %H:%M %z"
              time
              timezone)
             (format-time-string
              "%b %d %H:%M %z"
              time
              emacsconf-timezone))))

;;; Etherpad
(defvar emacsconf-review-comments-heading "Comments")
(defun emacsconf-import-comments-from-etherpad-text (filename)
  (interactive "FEtherpad text export: ")
  (with-temp-buffer
    (insert-file-contents filename)
    (goto-char (point-min))
    (while (re-search-forward "^[\t ]+Comments for \\([^:]+\\)" nil t)
      (let ((slug (match-string 1))
            comments)
        (forward-line 1)
        (setq comments
              (split-string
                (replace-regexp-in-string
                "\t\t\\*[ \t]*"
                ""
                (buffer-substring-no-properties
                 (point)
                 (if (re-search-forward "^[^\t]" nil t)
                     (match-beginning 0)
                   (point-max))))
               "\n"))
        (save-window-excursion
          (emacsconf-with-talk-heading slug
            ;; Do we already have a heading for comments?
            (if (re-search-forward (concat "^\\(\\*+\\) +" emacsconf-review-comments-heading)
                                   (save-excursion (org-end-of-subtree)) t)
                (org-end-of-meta-data)
              (org-end-of-subtree)
              (org-insert-heading-after-current)
              (insert emacsconf-review-comments-heading "\n"))
            ;; Are these comments already included?
            (save-restriction
              (org-narrow-to-subtree)
              (mapc (lambda (o)
                      (goto-char (point-min))
                      (unless (re-search-forward (regexp-quote o) nil t)
                        (goto-char (point-max))
                        (unless (bolp) (insert "\n"))
                        (insert "- " o "\n")))
                    comments))))))))

;;; Validation

(defun emacsconf-validate-all-talks-have-comments-for-speakers ()
  (interactive)
  (emacsconf-for-each-talk
    (unless (re-search-forward "^\\(- \\)?For \\(the \\)?[^ ]+ speaker" (save-excursion (org-end-of-subtree) (point)) t)
      (error "Could not find comment for %s" (org-entry-get (point) "SLUG"))))
  nil)

(defun emacsconf-validate-all-talks-have-field (field)
  (emacsconf-for-each-talk
    (when (string= (or (org-entry-get (point) field) "") "")
      (error "%s is missing %s" (org-entry-get (point) "SLUG") field)))
  nil)



(defvar emacsconf-time-constraints
  '(("saturday morning break" "10:00" "11:30")
    ("saturday lunch" "11:30" "13:30")
    ("saturday closing remarks" "16:30" "17:30")
    ("sunday morning break" "10:00" "11:30")
    ("sunday lunch" "11:30" "13:30")
    ("sunday closing remarks" "16:30" "17:30")))

(defun emacsconf-validate-no-overlaps (&optional info)
  (let (results))
  (while (cdr info)
    (when (and (plist-get (car info) :slug)
               (time-less-p (plist-get (cadr info) :start-time) (plist-get (car info) :end-time)))
      (setq results (cons "%s overlaps with %s (ends %s, starts %s)"
                          (or (plist-get (car info) :slug)
                              (plist-get (car info) :title))
                          (or (plist-get (cadr info) :slug)
                              (plist-get (cadr info) :title))
                          (format-time-string "%H:%M" (plist-get (car info) :end-time))
                          (format-time-string "%H:%M" (plist-get (cadr info) :start-time)))))
    (setq info (cdr info))))

(defun emacsconf-active-talks (list)
  "Remove CANCELLED talks from the list."
  (seq-remove (lambda (o) (string= (plist-get o :status) "CANCELLED")) list))

(defun emacsconf-validate-talk-subtree ()
  "Report an error if required properties are missing."
  (interactive)
  (let* ((props (org-entry-properties))
         (missing (seq-remove
                  (lambda (o) (assoc-default (symbol-name o) props))
                  '(CUSTOM_ID SLUG NAME NAME_SHORT EMAIL AVAILABILITY Q_AND_A TRACK MAX_TIME))))
    (when missing
      (if (called-interactively-p 'any)
          (message "Missing %s"  (mapconcat #'symbol-name missing ", "))
        (format "Missing %s"  (mapconcat #'symbol-name missing ", "))))))

;;; Ansible support
(defun emacsconf-ansible-export-talks ()
  (interactive)
  (when emacsconf-ansible-directory
    (with-temp-file (expand-file-name "talks.json" emacsconf-ansible-directory)
      (insert (json-encode (list :talks
                                 (mapcar (lambda (o)
                                           (apply 'list
                                                  (cons :start-time (format-time-string "%FT%T%z" (plist-get o :start-time) t))
                                                  (cons :end-time (format-time-string "%FT%T%z" (plist-get o :end-time) t))
                                                  (mapcar (lambda (field)
                                                            (cons field (plist-get o field)))
                                                          '(:slug :title :speakers :pronouns :pronunciation :url :track :video-slug)))
                                           )
                                         (emacsconf-filter-talks (emacsconf-get-talk-info)))))))))

(defun emacsconf-ansible-load-vars (file)
  (interactive (list (read-file-name "File: " emacsconf-ansible-directory)))
  (with-temp-buffer
    (insert-file-contents file)
    (let ((vars
           (yaml-parse-string (buffer-string))))
      (mapc (lambda (var)
              (set (car var) (gethash (cdr var) vars))
              )
            '((emacsconf-pad-api-key . etherpad_api_key)
              (emacsconf-pad-base . etherpad_url)
              (emacsconf-backstage-password . emacsconf_backstage_password))))))
;; (emacsconf-ansible-load-vars (expand-file-name "prod-vars.yml" emacsconf-ansible-directory))
;;; Tracks
(defvar emacsconf-tracks '((:name "General" :color "peachpuff" :id "gen" :channel "emacsconf-gen"
				  :tramp "/ssh:emacsconf-gen@localhost#46668:")
                           (:name "Development" :color "skyblue" :id "dev" :channel "emacsconf-dev"
				  :tramp "/ssh:emacsconf-dev@localhost#46668:")))

(defun emacsconf-get-track (name)
  (when (listp name) (setq name (plist-get name :track)))
  (seq-find (lambda (track) (or (string= name (plist-get track :name))
                                (string= name (plist-get track :id))))
            emacsconf-tracks))

(defun emacsconf-by-track (info)
  (mapcar (lambda (track)
            (seq-filter
             (lambda (talk)
               (string= (plist-get talk :track) (plist-get track :name)))
             info))
          emacsconf-tracks))

(defun emacsconf-complete-track ()
  (emacsconf-get-track (completing-read "Track: " (mapcar (lambda (o) (plist-get o :name)) emacsconf-tracks))))

(defun emacsconf-by-day (info)
  (seq-group-by (lambda (o)
                  (format-time-string "%Y-%m-%d" (plist-get o :start-time) emacsconf-timezone))
                (sort (seq-filter (lambda (o)
                                    (or (plist-get o :slug)
                                        (plist-get o :include-in-info)))
                                  info)
                      #'emacsconf-sort-by-scheduled)))

(defun emacsconf-filter-talks-by-track (track info)
  (when (stringp track) (setq track (emacsconf-get-track track)))
  (seq-filter (lambda (o) (string= (plist-get o :track) (plist-get track :name))) info))

(defvar emacsconf-shifts
  `((:id "sat-am"
         :label "Sat Dec 3 morning"
         :start ,(concat "2022-12-03T09:00:00" emacsconf-timezone-offset)
         :end ,(concat "2022-12-03T12:00:00" emacsconf-timezone-offset))
    (:id "sat-pm"
         :label "Sat Dec 3 afternoon"
         :start ,(concat "2022-12-03T13:00:00" emacsconf-timezone-offset)
         :end ,(concat "2022-12-03T17:30:00" emacsconf-timezone-offset))
    (:id "sun-am"
         :label "Sun Dec 4 morning"
         :start ,(concat "2022-12-04T09:00:00" emacsconf-timezone-offset)
         :end ,(concat "2022-12-04T12:00:00" emacsconf-timezone-offset))
    (:id "sun-pm"
         :label "Sun Dec 4 afternoon"
         :start ,(concat "2022-12-04T13:00:00" emacsconf-timezone-offset)
         :end ,(concat "2022-12-04T17:30:00" emacsconf-timezone-offset))))

(defun emacsconf-filter-talks-by-time (start-time end-time info)
  "Return talks that are between START-TIME and END-TIME (inclusive) in INFO."
  (when (stringp start-time) (setq start-time (date-to-time start-time)))
  (when (stringp end-time) (setq end-time (date-to-time end-time)))
  (seq-filter (lambda (o)
                (and (not (time-less-p (plist-get o :start-time) end-time)) 
                     (not (time-less-p start-time (plist-get o :end-time)))))
              info))

(defun emacsconf-get-shift (time)
  "Return the shift that TIME is in."
  (unless (stringp time)
    (setq time (format-time-string "%Y-%m-%dT%H:%M:%S%z" time emacsconf-timezone)))
  (seq-find (lambda (shift)
              (and (not (string> time (plist-get shift :end)))
                   (not (string> (plist-get shift :start) time))))
            emacsconf-shifts))

(defun emacsconf-filter-talks-by-shift (time track info)
  "Return a list of talks that are in the shift specified by TIME.
Filter by TRACK if given.  Use INFO as the list of talks."
  (let* ((shift (emacsconf-get-shift time))
         (list (emacsconf-filter-talks-by-time (plist-get shift :start) (plist-get shift :end) info)))
    (if track
        (emacsconf-filter-talks-by-track track info)
      list)))

(defun emacsconf-talk-all-done-p (talk)
  (member (plist-get talk :status) (split-string "TO_ARCHIVE TO_EXTRACT TO_FOLLOW_UP DONE")))

(defun emacsconf-bbb-status (talk)
  (let ((states
         '((open . "OPEN_Q UNSTREAMED_Q")
           (before . "TODO TO_REVIEW TO_ACCEPT WAITING_FOR_PREREC TO_PROCESS TO_AUTOCAP TO_ASSIGN TO_CAPTION TO_STREAM PLAYING CLOSED_Q")
           (after . "TO_ARCHIVE TO_EXTRACT TO_FOLLOW_UP DONE"))))
    (if (string-match "live" (or (plist-get talk :q-and-a) ""))
        (or (car (seq-find (lambda (state)
                             (member (plist-get talk :status) (split-string (cdr state))))
                           states))
            (throw 'error "Unknown talk BBB state"))
      'irc)))

(defvar emacsconf-bbb-base-url "https://bbb.emacsverse.org/" "Include trailing slash.")
(defun emacsconf-bbb-room-title-list (&optional info)
  (delq nil
        (mapcar
         (lambda (o)
           (when (car o)
             (concat "ec" (substring emacsconf-year 2)
                     "-" (plist-get (emacsconf-get-shift (plist-get (cadr o) :start-time)) :id)
                     "-" (plist-get (emacsconf-get-track (plist-get (cadr o) :track)) :id)
                     " " (car o)
                     " ("
                     (mapconcat (lambda (talk) (plist-get talk :slug)) (cdr o) ", ")
                     ")")))
         (seq-group-by (lambda (o) (plist-get o :speakers))
                       (or info (emacsconf-active-talks (emacsconf-filter-talks (emacsconf-get-talk-info))))))))

(defun emacsconf-surround (before text after &optional alternative)
  "Concat BEFORE, TEXT, and AFTER if TEXT is specified, or return ALTERNATIVE."
  (if (and text (not (string= text "")))
      (concat (or before "") text (or after ""))
    alternative))

;;; Volunteer management
(defun emacsconf-get-volunteer-info ()
  (with-current-buffer (find-file-noselect emacsconf-org-file)
    (org-map-entries (lambda () (org-entry-properties))
                     "volunteer+EMAIL={.}")))

(defun emacsconf-complete-volunteer (&optional info)
  (setq info (or info (emacsconf-get-volunteer-info)))
  (let* ((choices
          (mapcar (lambda (o)
                    (string-join
                     (delq nil
                           (mapcar (lambda (f) (assoc-default f o 'string=)) '("ITEM" "EMAIL")))
                     " - "))
                  info))
         (choice (completing-read
                  "Volunteer: " 
                  (lambda (string predicate action)
                    (if (eq action 'metadata)
                        '(metadata (category . volunteer))
                      (complete-with-action action choices string predicate))))))
    (elt info (seq-position choices choice))))

(defun emacsconf-reflow ()
  "Help reflow text files."
  (interactive)
  (let (input last-input)
    (while (not (string= "" (setq input (read-string "Word: "))))
      (when (string= input "!")
        (delete-backward-char 1)
        (insert " ")
        (end-of-line)
        (re-search-forward (regexp-quote last-input) nil t)
        (setq input last-input))
      (if (string= input "'")
          (progn
            (end-of-line)          
            (unless (looking-back " ")
              (insert " "))          
            (delete-char 1))
        (forward-word)
        (cond
         ((string= input ",")
          (re-search-forward ", " nil t)
          (goto-char (match-end 0)))
         ((string= input ".")
          (re-search-forward "\\. " nil t)
          (goto-char (match-end 0)))
         (t
          (re-search-forward (concat "\\<" (regexp-quote input)) nil t)
          (goto-char (match-beginning 0))))
        (insert "\n")
        (setq last-input input)
        (recenter)
        (undo-boundary)))))

(defun emacsconf-add-org-after-todo-state-change-hook ()
  "Add FUNC to `org-after-todo-stage-change-hook'."
  (interactive)
  (with-current-buffer (find-buffer-visiting emacsconf-org-file)
    (add-hook 'org-after-todo-state-change-hook #'emacsconf-org-after-todo-state-change nil t)))

(defun emacsconf-remove-org-after-todo-state-change-hook ()
  "Remove FUNC from `org-after-todo-stage-change-hook'."
  (interactive)
  (with-current-buffer (find-buffer-visiting emacsconf-org-file)
    (remove-hook 'org-after-todo-state-change-hook
                 #'emacsconf-org-after-todo-state-change  t)))

(defvar emacsconf-todo-hooks
  '(emacsconf-stream-play-talk-on-change ;; play the talk
    emacsconf-stream-open-qa-windows-on-change
    ;; emacsconf-erc-org-after-todo-state-change ;; announce via ERC
    emacsconf-publish-media-files-on-change
    emacsconf-publish-bbb-redirect
    emacsconf-publish-backstage-org-on-state-change ;; update the backstage index
    emacsconf-stream-update-talk-info-on-change ;; write to the talk text
    )
  "Functions to run when the todo state changes.
They will be called with TALK.")

(defun emacsconf-org-after-todo-state-change ()
  "Run all the hooks in `emacsconf-todo-hooks'."
  (let ((talk (emacsconf-get-talk-info-for-subtree)))
    (run-hook-with-args 'emacsconf-todo-hooks talk)))

(defun emacsconf-broadcast (message)
  (interactive "MMessage: ")
  (when (not (string= (or message "") ""))
    (erc-cmd-BROADCAST message))
  (emacsconf-stream-broadcast message))

(defun emacsconf-agenda ()
  (interactive)
  (let ((org-agenda-files (list emacsconf-org-file)))
    (org-agenda-list nil emacsconf-date 2)))

(defun emacsconf-track-agenda (track)
  (interactive (list (emacsconf-complete-track)))
  (when (stringp track) (setq track (emacsconf-get-track track)))
  (let ((org-agenda-files (list emacsconf-org-file))
        (org-agenda-category-filter-preset (list (concat "+" (plist-get track :id)))))
    (org-agenda-list nil emacsconf-date 2)))

(defun emacsconf-update-talk-status (slug from-states to-state)
  (interactive (list (emacsconf-complete-talk) (read-string "From: ") (read-string "To: ")))
  (emacsconf-with-talk-heading slug
    (when (string-match from-states (org-entry-get (point) "TODO"))
      (org-todo to-state))))

(provide 'emacsconf)
;;; emacsconf.el ends here