summary refs log tree commit diff stats
path: root/lisp/+jabber.el
blob: 2382102ed28c830b21f7c0628505247e31681de5 (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
243
244
245
246
247
248
249
250
251
252
253
254
;;; +jabber.el --- Customizations for jabber.el -*- lexical-binding: t; -*-

;;; Commentary:

;; Most changes I want to PR and contribute, but a few don't make sense to
;; contribute upstream, at least not now.

;;; Code:

(require 'jabber)
(require 'tracking)

(defvar +jabber-tracking-show-p #'jabber-activity-show-p-default
  "Function that checks if the given JID should be shown in the mode line.
This does the same as `jabber-activity-show-p', but for the
`tracking-mode' mode-line.")

(defun +jabber-tracking-add (from buffer text proposed-alert)
  "ADVICE to add jabber buffers to `tracking-buffers'."
  (when (funcall +jabber-tracking-show-p from)
    (tracking-add-buffer buffer 'jabber-activity-face)))

(defun +jabber-tracking-add-muc (nick group buffer text proposed-alert)
  "ADVICE to add jabber MUC buffers to `tracking-buffers'."
  (when (funcall +jabber-tracking-show-p group)
    (tracking-add-buffer buffer 'jabber-activity-face)))

;;; Hiding presence messages:
;; https://paste.sr.ht/~hdasch/f0ad09fbcd08e940a4fda71c2f40abc1c4efd45f

;; Tame MUC presence notifications.

;; This patch hides or applies a face to MUC presence notifications in
;; the MUC chat buffer.  To control its behavior, customize
;; ’jabber-muc-decorate-presence-patterns’.  By default it does nothing.

;; ’jabber-muc-decorate-presence-patterns’ is a list of pairs consisting
;; of a regular expression and a either a face or ‘nil’.  If a the
;; regular expression matches a presence notification, then either:

;;  - the specified face is applied to the notification message
;;  - or if the second value of the pair is nil, the notification is
;;    discarded

;; If no regular expression in the list of pairs matches the notification
;; message, the message is displayed unchanged.

;; For example, the customization:

;;     '(jabber-muc-decorate-presence-patterns
;;       '(("\\( enters the room ([^)]+)\\| has left the chatroom\\)$")
;;         ("." . jabber-muc-presence-dim)))

;; hides participant enter/leave notifications.  It also diminishes other
;; presence notification messages to make it easier to distinguish
;; between conversation and notifications.

(defface jabber-muc-presence-dim
  '((t (:foreground "dark grey" :weight light :slant italic)))
  "face for diminished presence notifications.")

(defcustom jabber-muc-decorate-presence-patterns nil
  "List of regular expressions and face pairs.
When a presence notification matches a pattern, display it with
associated face.  Ignore notification if face is ‘nil’."
  :type '(repeat
          :tag "Patterns"
          (cons :format "%v"
           (regexp :tag "Regexp")
           (choice
            (const :tag "Ignore" nil)
            (face :tag "Face" :value jabber-muc-presence-dim))))
  :group 'jabber-alerts)

(defun jabber-muc-maybe-decorate-presence (node)
  "Filter presence notifications."
  (cl-destructuring-bind (key msg &key time) node
    (let* ((match (cl-find-if
                   (lambda (pair)
                     (string-match (car pair) msg))
                   jabber-muc-decorate-presence-patterns))
           (face (cdr-safe match)))
      (if match
          (when face
            (jabber-maybe-print-rare-time
             (ewoc-enter-last
              jabber-chat-ewoc
              (list key
                    (propertize msg 'face face)
                    :time time))))
        (jabber-maybe-print-rare-time
         (ewoc-enter-last jabber-chat-ewoc node))))))

(defun jabber-muc-process-presence (jc presence)
  (let* ((from (jabber-xml-get-attribute presence 'from))
         (type (jabber-xml-get-attribute presence 'type))
         (x-muc (cl-find-if
                 (lambda (x) (equal (jabber-xml-get-attribute x 'xmlns)
                               "http://jabber.org/protocol/muc#user"))
                 (jabber-xml-get-children presence 'x)))
         (group (jabber-jid-user from))
         (nickname (jabber-jid-resource from))
         (symbol (jabber-jid-symbol from))
         (our-nickname (gethash symbol jabber-pending-groupchats))
         (item (car (jabber-xml-get-children x-muc 'item)))
         (actor (jabber-xml-get-attribute (car (jabber-xml-get-children item 'actor)) 'jid))
         (reason (car (jabber-xml-node-children (car (jabber-xml-get-children item 'reason)))))
         (error-node (car (jabber-xml-get-children presence 'error)))
         (status-codes (if error-node
                           (list (jabber-xml-get-attribute error-node 'code))
                         (mapcar
                          (lambda (status-element)
                            (jabber-xml-get-attribute status-element 'code))
                          (jabber-xml-get-children x-muc 'status)))))
    ;; handle leaving a room
    (cond
     ((or (string= type "unavailable") (string= type "error"))
      ;; error from room itself? or are we leaving?
      (if (or (null nickname)
              (member "110" status-codes)
              (string= nickname our-nickname))
          ;; Assume that an error means that we were thrown out of the
          ;; room...
          (let* ((leavingp t)
                 (message (cond
                           ((string= type "error")
                            (cond
                             ;; ...except for certain cases.
                             ((or (member "406" status-codes)
                                  (member "409" status-codes))
                              (setq leavingp nil)
                              (concat "Nickname change not allowed"
                                      (when error-node
                                        (concat ": " (jabber-parse-error error-node)))))
                             (t
                              (concat "Error entering room"
                                      (when error-node
                                        (concat ": " (jabber-parse-error error-node)))))))
                           ((member "301" status-codes)
                            (concat "You have been banned"
                                    (when actor (concat " by " actor))
                                    (when reason (concat " - '" reason "'"))))
                           ((member "307" status-codes)
                            (concat "You have been kicked"
                                    (when actor (concat " by " actor))
                                    (when reason (concat " - '" reason "'"))))
                           (t
                            "You have left the chatroom"))))
            (when leavingp
              (jabber-muc-remove-groupchat group))
            ;; If there is no buffer for this groupchat, don't bother
            ;; creating one just to tell that user left the room.
            (let ((buffer (get-buffer (jabber-muc-get-buffer group))))
              (if buffer
                  (with-current-buffer buffer
                    (jabber-muc-maybe-decorate-presence
                     (list (if (string= type "error")
                               :muc-error
                             :muc-notice)
                           message
                           :time (current-time)))))
              (message "%s: %s" (jabber-jid-displayname group) message))))
      ;; or someone else?
      (let* ((plist (jabber-muc-participant-plist group nickname))
             (jid (plist-get plist 'jid))
             (name (concat nickname
                           (when jid
                             (concat " <"
                                     (jabber-jid-user jid)
                                     ">")))))
        (jabber-muc-remove-participant group nickname)
        (with-current-buffer (jabber-muc-create-buffer jc group)
          (jabber-muc-maybe-decorate-presence
           (list :muc-notice
                 (cond
                  ((member "301" status-codes)
                   (concat name " has been banned"
                           (when actor (concat " by " actor))
                           (when reason (concat " - '" reason "'"))))
                  ((member "307" status-codes)
                   (concat name " has been kicked"
                           (when actor (concat " by " actor))
                           (when reason (concat " - '" reason "'"))))
                  ((member "303" status-codes)
                   (concat name " changes nickname to "
                           (jabber-xml-get-attribute item 'nick)))
                  (t
                   (concat name " has left the chatroom")))
                 :time (current-time))))))
     (t
      ;; someone is entering

      (when (or (member "110" status-codes) (string= nickname our-nickname))
        ;; This is us.  We just succeeded in entering the room.
        ;;
        ;; The MUC server is supposed to send a 110 code whenever this
        ;; is our presence ("self-presence"), but at least one
        ;; (ejabberd's mod_irc) doesn't, so check the nickname as well.
        ;;
        ;; This check might give incorrect results if the server
        ;; changed our nickname to avoid collision with an existing
        ;; participant, but even in this case the window where we have
        ;; incorrect information should be very small, as we should be
        ;; getting our own 110+210 presence shortly.
        (let ((whichgroup (assoc group *jabber-active-groupchats*)))
          (if whichgroup
              (setcdr whichgroup nickname)
            (add-to-list '*jabber-active-groupchats* (cons group nickname))))
        ;; The server may have changed our nick.  Record the new one.
        (puthash symbol nickname jabber-pending-groupchats))

      ;; Whoever enters, we create a buffer (if it didn't already
      ;; exist), and print a notice.  This is where autojoined MUC
      ;; rooms have buffers created for them.  We also remember some
      ;; metadata.
      (let ((old-plist (jabber-muc-participant-plist group nickname))
            (new-plist (jabber-muc-parse-affiliation x-muc)))
        (jabber-muc-modify-participant group nickname new-plist)
        (let ((report (jabber-muc-report-delta nickname old-plist new-plist
                                               reason actor)))
          (when report
            (with-current-buffer (jabber-muc-create-buffer jc group)
              (jabber-muc-maybe-decorate-presence
               (list :muc-notice report
                     :time (current-time)))
              ;; Did the server change our nick?
              (when (member "210" status-codes)
                (ewoc-enter-last
                 jabber-chat-ewoc
                 (list :muc-notice
                       (concat "Your nick was changed to " nickname " by the server")
                       :time (current-time))))
              ;; Was this room just created?  If so, it's a locked
              ;; room.  Notify the user.
              (when (member "201" status-codes)
                (ewoc-enter-last
                 jabber-chat-ewoc
                 (list :muc-notice
                       (with-temp-buffer
                         (insert "This room was just created, and is locked to other participants.\n"
                                 "To unlock it, ")
                         (insert-text-button
                          "configure the room"
                          'action (apply-partially 'call-interactively 'jabber-muc-get-config))
                         (insert " or ")
                         (insert-text-button
                          "accept the default configuration"
                          'action (apply-partially 'call-interactively 'jabber-muc-instant-config))
                         (insert ".")
                         (buffer-string))
                       :time (current-time))))))))))))

(provide '+jabber)
;;; +jabber.el ends here