summaryrefslogtreecommitdiffhomepage
path: root/custom/jao-custom-chats.el
blob: c2105c677048b4d23e69949c3ad6483628975185 (plain)
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
;; -*- lexical-binding: t -*-


;;;; circe
(defvar jao-libera-channels ())
(defvar jao-oftc-channels ())
(defvar jao-bitlbee-channels ())
(defvar jao-slack-channels ())

(use-package circe
  :ensure t
  :bind (:map circe-channel-mode-map
              (("C-c C-a" . lui-track-jump-to-indicator)))
  :init
  (setq circe-chat-buffer-name "{target} - {network}"
        circe-default-realname "https://jao.io"
        circe-default-part-message ""
        circe-default-quit-message ""
        circe-ignore-list nil
        circe-server-coding-system '(undecided . undecided)
        circe-server-killed-confirmation 'ask-and-kill-all
        circe-server-auto-join-default-type :after-auth
        circe-server-buffer-name "{network} - {host}:{port}"
        circe-format-say "({nick}) {body}"
        circe-format-self-say "(jao) {body}"
        circe-new-buffer-behavior 'ignore
        circe-new-buffer-behavior-ignore-auto-joins t
        circe-nickserv-ghost-style 'after-auth
        circe-prompt-string ": "
        circe-completion-suffix ", "
        circe-reduce-lurker-spam t

        circe-lagmon-mode-line-format-string "" ;;  "%.0f "
        circe-lagmon-mode-line-unknown-lag-string "" ;; "? "
        circe-lagmon-timer-tick 120
        circe-lagmon-reconnect-interval 180

        lui-max-buffer-size 30000
        lui-fill-column 80
        lui-time-stamp-position 'right
        lui-time-stamp-format "%H:%M"
        lui-flyspell-p nil

        lui-track-indicator (if window-system 'fringe 'bar)
        lui-track-behavior 'before-tracking-next-buffer)
  :config

  (defsubst jao-circe-nick-no ()
    (if (derived-mode-p 'circe-query-mode)
        2
      (length (circe-channel-nicks))))

  (defsubst jao-circe-netowrk ()
    (or (plist-get lui-logging-format-arguments :network) ""))

  (define-minor-mode jao-circe-user-number-mode ""
    :lighter (:eval (format " [%s]" (jao-circe-nick-no))))

  (defun jao-circe-channel-hook ()
    (when jao-mode-line-in-minibuffer
      (setq header-line-format
            '(" %b" (:eval (format "@%s - %s nicks"
                                   (jao-circe-netowrk)
                                   (jao-circe-nick-no))))))
    (jao-circe-user-number-mode 1))

  (add-hook 'circe-channel-mode-hook #'jao-circe-channel-hook)
  (add-hook 'circe-query-mode-hook #'jao-circe-channel-hook)

  (defun circe-command-RECOVER (&rest _ignore)
    "Recover nick"
    (jao-with-auth "freenode" u p
      (circe-command-MSG "nickserv" (format "IDENTIFY %s %s" u p))
      (circe-command-MSG "nickserv" (format "GHOST %s" u))
      (circe-command-MSG "nickserv" (format "RELEASE %s" u))
      (circe-command-NICK u)))

  (defun circe-command-NNICKS (&rest _)
    "Echo number of nicks"
    (circe-display-server-message
     (format "%d nicks in this channel" (jao-circe-nick-no))))

  (defun circe-command-SENDFILE (line)
    "/sendfile for localslackirc"
    (circe-command-QUOTE (format "sendfile %s" line)))

  (advice-add 'circe-command-NAMES :after #'circe-command-NNICKS)

  (defun jao-circe-reconnect-unmonitored ()
    (interactive)
    (dolist (b (buffer-list))
      (with-current-buffer b
        (when (and (derived-mode-p 'circe-server-mode)
                   circe-lagmon-disabled)
          (circe-reconnect)))))

  (add-to-list 'jao-sleep-awake-functions #'jao-circe-reconnect-unmonitored)

  (setq circe-network-options
        (list (jao-with-auth "libera" u p
                (list "Libera Chat" :nick u :channels jao-libera-channels
                      :tls t :sasl-username u :sasl-password p))
              (jao-with-auth "oftc" u p
                (list "OFTC"
                      :nick u :channels jao-oftc-channels :nickserv-password p
                      :tls t :sasl-username u :sasl-password p))
              (jao-with-auth "bitlbee" u p
                (list "Bitlbee" :host "127.0.0.1" :nick u
                      :channels jao-bitlbee-channels :lagmon-disabled t
                      :nickserv-password u :user p))))

  (jao-shorten-modes 'circe-channel-mode
                     'circe-server-mode
                     'circe-query-mode)

  (enable-circe-display-images)
  (enable-lui-track)
  (circe-lagmon-mode))

;;;; telegram
(use-package telega
  :ensure t
  :custom
  (telega-use-tracking-for '(unmuted) ;; '(or unmuted mention)
                           telega-rainbow-color-custom-for nil
                           telega-msg-rainbow-title nil
                           telega-sticker-set-download t
                           telega-symbol-checkmark "·"
                           telega-symbol-heavy-checkmark "×"
                           telega-symbol-verified "*")
  :config
  (define-key global-map (kbd "C-c C-t") telega-prefix-map)
  (setq telega-chat-show-avatars nil
        telega-chat-prompt-insexp '(telega-ins "> ")
        telega-completing-read-function #'completing-read
        telega-root-show-avatars nil
        telega-emoji-use-images nil
        telega-temp-dir "/tmp/telega"
        telega-symbol-horizontal-bar
        (propertize "-" 'face 'jao-themes-f00)
        telega-symbol-vertical-bar
        (propertize "| " 'face 'jao-themes-dimm)
        telega-mode-line-string-format
        '(:eval (telega-mode-line-unread-unmuted))
        telega-use-images (display-graphic-p)
        telega-open-file-function #'jao--see
        telega-open-message-as-file
        (unless (display-graphic-p) '(photo video animation)))
  (with-eval-after-load "tracking"
    (jao-shorten-modes 'telega-chat-mode)
    (jao-tracking-faces 'telega-tracking))
  (telega-mode-line-mode 1))

(defun jao-telega ()
  (interactive)
  (jao-tracking-go-to-chats)
  (if (get-buffer telega-root-buffer-name)
      (pop-to-buffer telega-root-buffer-name)
    (telega)))

;;;; ement
(use-package ement
  :disabled t
  :ensure t
  :init (setq ement-save-sessions t
              ement-sessions-file (locate-user-emacs-file "cache/ement.el")
              ement-room-avatars nil
              ement-notify-dbus-p nil
              ement-room-left-margin-width 0
              ement-room-right-margin-width 11
              ement-room-timestamp-format "%H:%M"
              ement-room-timestamp-header-format "­­--------")

  :custom ((ement-room-message-format-spec "(%S) %B%r%R %t"))

  :config
  (defun jao-ement-track (event room session)
    (when (ement-notify--room-unread-p event room session)
      (when-let ((n (ement-room--buffer-name room))
                 (b (get-buffer n)))
        (tracking-add-buffer b))))

  (add-hook 'ement-event-hook #'jao-ement-track)
  (jao-shorten-modes 'ement-room-mode)
  (jao-tracking-cleaner "^\\*Ement Room: \\(.+\\)\\*" "@\\1"))

;;;; startup
(defun jao-chats (&optional p)
  (interactive "P")
  (when (or p (y-or-n-p "Connect to telegram? "))
    (telega))
  (when (and (fboundp 'ement-connect) (or p (y-or-n-p "Connect to matrix? ")))
    (unless (get-buffer "*Ement Rooms*")
      (jao-with-auth "matrix.org" u p (ement-connect :user-id u :password p))))
  ;; (when (and (fboundp 'mastodon) (or p (y-or-n-p "Connect to mastodon? ")))
  ;;   (mastodon))
  (dolist (c '(("Libera Chat" . "irc.libera.chat:6697")))
    (unless (get-buffer (cdr c))
      (when (or p (y-or-n-p (format "Connect to %s? " (car c))))
        (circe (car c))))))

(defun jao-all-chats ()
  (interactive)
  (when jao-tracking-use-scratch
    (jao-afio-goto-chats)
    (delete-other-windows))
  (jao-chats t))

(defun jao-chats-telega ()
  (interactive)
  (jao-buffer-same-mode '(telega-root-mode telega-chat-mode)))

(defun jao-chats-slack ()
  (interactive)
  (jao-buffer-same-mode 'slack-message-buffer-mode))

(defun jao-chats-irc ()
  (interactive)
  (jao-buffer-same-mode '(circe-channel-mode circe-query-mode erc-mode)))

;;;; consult narrowing
(defvar jao-chat-buffer-source
  (list :name "chats"
        :category 'buffer
        :action (lambda (b) (jao-afio-pop-to-buffer 0 b))
        :hidden t
        :narrow (cons ?c "chats")
        :items (jao-consult--mode-buffers 'erc-mode
                                          'circe-channel-mode
                                          'circe-query-mode
                                          'signel-chat-mode
                                          'slack-message-buffer-mode
                                          'slack-thread-message-buffer-mode
                                          'telega-root-mode
                                          'telega-chat-mode
                                          'ement-room-mode
                                          'ement-room-list-mode)))
(with-eval-after-load "consult"
  (jao-consult-add-buffer-source 'jao-chat-buffer-source))


(provide 'jao-custom-chats)