1 ;; jabber-roster.el - displaying the roster -*- coding: utf-8; -*-
3 ;; Copyright (C) 2009 - Kirill A. Korinskiy - catap@catap.ru
4 ;; Copyright (C) 2003, 2004, 2007, 2008 - Magnus Henoch - mange@freemail.hu
5 ;; Copyright (C) 2002, 2003, 2004 - tom berger - object@intelectronica.net
7 ;; This file is a part of jabber.el.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program; if not, write to the Free Software
21 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 (require 'jabber-presence)
24 (require 'jabber-util)
25 (require 'jabber-alert)
26 (require 'jabber-keymap)
27 (require 'format-spec)
28 (require 'cl-lib) ;for `find'
29 (require 'jabber-private)
31 (defgroup jabber-roster nil "roster display options"
34 (defcustom jabber-roster-line-format " %a %c %-25n %u %-8s %S"
35 "The format specification of the lines in the roster display.
37 These fields are available:
40 %c \"*\" if the contact is connected, or \" \" if not
41 %u sUbscription state - see below
42 %n Nickname of contact, or JID if no nickname
43 %j Bare JID of contact (without resource)
44 %r Highest-priority resource of contact
45 %s Availability of contact as string (\"Online\", \"Away\" etc)
46 %S Status string specified by contact
48 %u is replaced by one of the strings given by
49 `jabber-roster-subscription-display'."
51 :group 'jabber-roster)
53 (defcustom jabber-roster-subscription-display '(("none" . " ")
57 "Strings used for indicating subscription status of contacts.
58 \"none\" means that there is no subscription between you and the
60 \"from\" means that the contact has a subscription to you, but you
61 have no subscription to the contact.
62 \"to\" means that you have a subscription to the contact, but the
63 contact has no subscription to you.
64 \"both\" means a mutual subscription.
66 Having a \"presence subscription\" means being able to see the
67 other person's presence.
69 Some fancy arrows you might want to use, if your system can
70 display them: ← → ⇄ ↔"
71 :type '(list (cons :format "%v" (const :format "" "none") (string :tag "None"))
72 (cons :format "%v" (const :format "" "from") (string :tag "From"))
73 (cons :format "%v" (const :format "" "to") (string :tag "To"))
74 (cons :format "%v" (const :format "" "both") (string :tag "Both")))
75 :group 'jabber-roster)
77 (defcustom jabber-resource-line-format " %r - %s (%S), priority %p"
78 "The format specification of resource lines in the roster display.
79 These are displayed when `jabber-show-resources' permits it.
81 These fields are available:
83 %c \"*\" if the contact is connected, or \" \" if not
84 %n Nickname of contact, or JID if no nickname
85 %j Bare JID of contact (without resource)
86 %p Priority of this resource
87 %r Name of this resource
88 %s Availability of resource as string (\"Online\", \"Away\" etc)
89 %S Status string specified by resource."
91 :group 'jabber-roster)
93 (defcustom jabber-roster-sort-functions
94 '(jabber-roster-sort-by-status jabber-roster-sort-by-displayname)
95 "Sort roster according to these criteria.
97 These functions should take two roster items A and B, and return:
102 :options '(jabber-roster-sort-by-status
103 jabber-roster-sort-by-displayname
104 jabber-roster-sort-by-group)
105 :group 'jabber-roster)
107 (defcustom jabber-sort-order '("chat" "" "away" "dnd" "xa")
108 "Sort by status in this order. Anything not in list goes last.
109 Offline is represented as nil."
110 :type '(repeat (restricted-sexp :match-alternatives (stringp nil)))
111 :group 'jabber-roster)
113 (defcustom jabber-show-resources 'sometimes
114 "Show contacts' resources in roster?
115 This can be one of the following symbols:
117 nil Never show resources
118 sometimes Show resources when there are more than one
119 always Always show resources."
120 :type '(radio (const :tag "Never" nil)
121 (const :tag "When more than one connected resource" sometimes)
122 (const :tag "Always" always))
123 :group 'jabber-roster)
125 (defcustom jabber-show-offline-contacts t
126 "Show offline contacts in roster when non-nil."
128 :group 'jabber-roster)
130 (defcustom jabber-remove-newlines t
131 "Remove newlines in status messages?
132 Newlines in status messages mess up the roster display. However,
133 they are essential to status message poets. Therefore, you get to
134 choose the behaviour.
136 Trailing newlines are always removed, regardless of this variable."
138 :group 'jabber-roster)
140 (defcustom jabber-roster-show-bindings t
141 "Show keybindings in roster buffer?."
143 :group 'jabber-roster)
145 (defcustom jabber-roster-show-title t
146 "Show title in roster buffer?."
148 :group 'jabber-roster)
150 (defcustom jabber-roster-mode-hook nil
151 "Hook run when entering Roster mode."
152 :group 'jabber-roster
155 (defcustom jabber-roster-default-group-name "other"
156 "Default group name for buddies without groups."
157 :group 'jabber-roster
160 (let ((val (symbol-value var)))
162 (set-text-properties 0 (length val) nil val))
164 :set '(lambda (var val)
166 (set-text-properties 0 (length val) nil val))
167 (custom-set-default var val)))
169 (defcustom jabber-roster-show-empty-group nil
170 "Show empty groups in roster?."
171 :group 'jabber-roster
174 (defcustom jabber-roster-roll-up-group nil
175 "Show empty groups in roster?."
176 :group 'jabber-roster
179 (defface jabber-roster-user-online
180 '((t (:foreground "blue" :weight bold :slant normal)))
181 "Face for displaying online users."
182 :group 'jabber-roster)
184 (defface jabber-roster-user-xa
185 '((((background dark)) (:foreground "magenta" :weight normal :slant italic))
186 (t (:foreground "black" :weight normal :slant italic)))
187 "Face for displaying extended away users."
188 :group 'jabber-roster)
190 (defface jabber-roster-user-dnd
191 '((t (:foreground "red" :weight normal :slant italic)))
192 "Face for displaying do not disturb users."
193 :group 'jabber-roster)
195 (defface jabber-roster-user-away
196 '((t (:foreground "dark green" :weight normal :slant italic)))
197 "Face for displaying away users."
198 :group 'jabber-roster)
200 (defface jabber-roster-user-chatty
201 '((t (:foreground "dark orange" :weight bold :slant normal)))
202 "Face for displaying chatty users."
203 :group 'jabber-roster)
205 (defface jabber-roster-user-error
206 '((t (:foreground "red" :weight light :slant italic)))
207 "Face for displaying users sending presence errors."
208 :group 'jabber-roster)
210 (defface jabber-roster-user-offline
211 '((t (:foreground "dark grey" :weight light :slant italic)))
212 "Face for displaying offline users."
213 :group 'jabber-roster)
215 (defvar jabber-roster-debug nil
216 "Debug roster draw.")
218 (defvar jabber-roster-mode-map
219 (let ((map (make-sparse-keymap)))
220 (suppress-keymap map)
221 (set-keymap-parent map jabber-common-keymap)
222 (define-key map [mouse-2] 'jabber-roster-mouse-2-action-at-point)
223 (define-key map (kbd "TAB") 'jabber-go-to-next-roster-item)
224 (define-key map (kbd "S-TAB") 'jabber-go-to-previous-roster-item)
225 (define-key map (kbd "M-TAB") 'jabber-go-to-previous-roster-item)
226 (define-key map (kbd "<backtab>") 'jabber-go-to-previous-roster-item)
227 (define-key map (kbd "RET") 'jabber-roster-ret-action-at-point)
228 (define-key map (kbd "C-k") 'jabber-roster-delete-at-point)
230 (define-key map "e" 'jabber-roster-edit-action-at-point)
231 (define-key map "s" 'jabber-send-subscription-request)
232 (define-key map "q" 'bury-buffer)
233 (define-key map "i" 'jabber-get-disco-items)
234 (define-key map "j" 'jabber-muc-join)
235 (define-key map "I" 'jabber-get-disco-info)
236 (define-key map "b" 'jabber-get-browse)
237 (define-key map "v" 'jabber-get-version)
238 (define-key map "a" 'jabber-send-presence)
239 (define-key map "g" 'jabber-display-roster)
240 (define-key map "S" 'jabber-ft-send)
241 (define-key map "o" 'jabber-roster-toggle-offline-display)
242 (define-key map "H" 'jabber-roster-toggle-binding-display)
243 ;;(define-key map "D" 'jabber-disconnect)
246 (defun jabber-roster-ret-action-at-point ()
248 Before try to roll up/down group. Eval `chat-with-jid-at-point' is no group at
251 (let ((group-at-point (get-text-property (point)
253 (account-at-point (get-text-property (point)
255 (jid-at-point (get-text-property (point)
257 (if (and group-at-point account-at-point)
258 (jabber-roster-roll-group account-at-point group-at-point)
259 ;; Is this a normal contact, or a groupchat? Let's ask it.
260 (jabber-disco-get-info
261 account-at-point (jabber-jid-user jid-at-point) nil
262 #'jabber-roster-ret-action-at-point-1
265 (defun jabber-roster-ret-action-at-point-1 (jc jid result)
266 ;; If we get an error, assume it's a normal contact.
267 (if (eq (car result) 'error)
268 (jabber-chat-with jc jid)
269 ;; Otherwise, let's check whether it has a groupchat identity.
270 (let ((identities (car result)))
271 (if (cl-find "conference" (if (sequencep identities) identities nil)
272 :key (lambda (i) (aref i 1))
274 ;; Yes! Let's join it.
275 (jabber-muc-join jc jid
276 (jabber-muc-read-my-nickname jc jid t)
278 ;; No. Let's open a normal chat buffer.
279 (jabber-chat-with jc jid)))))
281 (defun jabber-roster-mouse-2-action-at-point (e)
283 Before try to roll up/down group. Eval `chat-with-jid-at-point' is no group
287 (let ((group-at-point (get-text-property (point)
289 (account-at-point (get-text-property (point)
291 (if (and group-at-point account-at-point)
292 (jabber-roster-roll-group account-at-point group-at-point)
293 (jabber-popup-combined-menu))))
295 (defun jabber-roster-delete-at-point ()
296 "Delete at point from roster.
297 Try to delete the group from all contaacs.
298 Delete a jid if there is no group at point."
300 (let ((group-at-point (get-text-property (point)
302 (account-at-point (get-text-property (point)
304 (if (and group-at-point account-at-point)
305 (let ((jids-with-group
306 (gethash group-at-point
308 (fsm-get-state-data account-at-point)
310 (jabber-roster-delete-group-from-jids account-at-point
313 (jabber-roster-delete-jid-at-point))))
315 (defun jabber-roster-edit-action-at-point ()
316 "Action for e. Before try to edit group name.
317 Eval `jabber-roster-change' is no group at point."
319 (let ((group-at-point (get-text-property (point)
321 (account-at-point (get-text-property (point)
323 (if (and group-at-point account-at-point)
324 (let ((jids-with-group
325 (gethash group-at-point
327 (fsm-get-state-data account-at-point)
329 (jabber-roster-edit-group-from-jids account-at-point
332 (call-interactively 'jabber-roster-change))))
334 (defun jabber-roster-roll-group (jc group-name &optional set)
335 "Roll up/down group in roster.
336 If optional SET is t, roll up group.
337 If SET is nor t or nil, roll down group."
338 (let* ((state-data (fsm-get-state-data jc))
339 (roll-groups (plist-get state-data :roster-roll-groups))
340 (new-roll-groups (if (cl-find group-name roll-groups :test 'string=)
341 ;; group is rolled up, roll it down if needed
342 (if (or (not set) (and set (not (eq set t))))
343 (cl-remove-if-not (lambda (group-name-in-list)
344 (not (string= group-name
345 group-name-in-list)))
348 ;; group is rolled down, roll it up if needed
349 (if (or (not set) (and set (eq set t)))
350 (append roll-groups (list group-name))
352 (unless (equal roll-groups new-roll-groups)
354 state-data :roster-roll-groups
356 (jabber-display-roster))))
358 (defun jabber-roster-mode ()
359 "Major mode for Jabber roster display.
360 Use the keybindings (mnemonic as Chat, Roster, Info, MUC, Service) to
361 bring up menus of actions.
362 \\{jabber-roster-mode-map}"
363 (kill-all-local-variables)
364 (setq major-mode 'jabber-roster-mode
365 mode-name "jabber-roster")
366 (use-local-map jabber-roster-mode-map)
367 (setq buffer-read-only t)
368 (if (fboundp 'run-mode-hooks)
369 (run-mode-hooks 'jabber-roster-mode-hook)
370 (run-hooks 'jabber-roster-mode-hook)))
372 (put 'jabber-roster-mode 'mode-class 'special)
375 (defun jabber-switch-to-roster-buffer (&optional _jc)
376 "Switch to roster buffer.
377 Optional JC argument is ignored; it's there so this function can
378 be used in `jabber-post-connection-hooks'."
380 (if (not (get-buffer jabber-roster-buffer))
381 (jabber-display-roster)
382 (switch-to-buffer jabber-roster-buffer)))
384 (defun jabber-sort-roster (jc)
385 "Sort roster according to online status.
386 JC is the Jabber connection."
387 (let ((state-data (fsm-get-state-data jc)))
388 (dolist (group (plist-get state-data :roster-groups))
389 (let ((group-name (car group)))
393 (plist-get state-data :roster-hash))
394 #'jabber-roster-sort-items)
395 (plist-get state-data :roster-hash))))))
397 (defun jabber-roster-prepare-roster (jc)
398 "Make a hash based roster.
399 JC is the Jabber connection."
400 (let* ((state-data (fsm-get-state-data jc))
401 (hash (make-hash-table :test 'equal))
402 (buddies (plist-get state-data :roster))
404 (dolist (buddy buddies)
405 (let ((groups (get buddy 'groups)))
408 (dolist (group groups)
410 (setq all-groups (append all-groups (list group)))
412 (append (gethash group hash)
416 (setq all-groups (append all-groups
417 (list jabber-roster-default-group-name)))
418 (puthash jabber-roster-default-group-name
419 (append (gethash jabber-roster-default-group-name hash)
423 ;; remove duplicates name of group
424 (setq all-groups (sort
425 (cl-remove-duplicates all-groups
429 ;; put to state-data all-groups as list of list
430 (plist-put state-data :roster-groups
431 (mapcar #'list all-groups))
433 ;; put to state-data hash-roster
434 (plist-put state-data :roster-hash
437 (defun jabber-roster-sort-items (a b)
438 "Sort roster items A and B according to `jabber-roster-sort-functions'.
439 Return t if A is less than B."
441 (seq-find (lambda (fn)
442 (setq result (funcall fn a b))
444 jabber-roster-sort-functions)
447 (defun jabber-roster-sort-by-status (a b)
448 "Sort roster items by online status.
449 See `jabber-sort-order' for order used."
450 (cl-flet ((order (item) (length (member (get item 'show) jabber-sort-order))))
451 (let ((a-order (order a))
453 ;; Note reversed test. Items with longer X-order go first.
462 (defun jabber-roster-sort-by-displayname (a b)
463 "Sort roster items by displayed name."
464 (let ((a-name (jabber-jid-displayname a))
465 (b-name (jabber-jid-displayname b)))
467 ((string-lessp a-name b-name) -1)
468 ((string= a-name b-name) 0)
471 (defun jabber-roster-sort-by-group (a b)
472 "Sort roster items by group membership."
473 (cl-flet ((first-group (item) (or (car (get item 'groups)) "")))
474 (let ((a-group (first-group a))
475 (b-group (first-group b)))
477 ((string-lessp a-group b-group) -1)
478 ((string= a-group b-group) 0)
481 (defun jabber-fix-status (status)
482 "Make status strings more readable."
484 (when (string-match "\n+$" status)
485 (setq status (replace-match "" t t status)))
486 (when jabber-remove-newlines
487 (while (string-match "\n" status)
488 (setq status (replace-match " " t t status))))
491 (defvar jabber-roster-ewoc nil
492 "Ewoc displaying the roster.
493 There is only one; we don't rely on buffer-local variables or
496 (defun jabber-roster-filter-display (buddies)
497 "Filter BUDDIES for items to be displayed in the roster."
498 (cl-remove-if-not (lambda (buddy) (or jabber-show-offline-contacts
499 (get buddy 'connected)))
502 (defun jabber-roster-toggle-offline-display ()
503 "Toggle display of offline contacts.
504 To change this permanently, customize the `jabber-show-offline-contacts'."
506 (setq jabber-show-offline-contacts
507 (not jabber-show-offline-contacts))
508 (jabber-display-roster))
510 (defun jabber-roster-toggle-binding-display ()
511 "Toggle display of the roster binding text."
513 (setq jabber-roster-show-bindings
514 (not jabber-roster-show-bindings))
515 (jabber-display-roster))
517 (defun jabber-display-roster ()
518 "Switch to the main jabber buffer and refresh it.
519 Switch to the roster display and refresh it to reflect the current
522 (with-current-buffer (get-buffer-create jabber-roster-buffer)
523 (if (not (eq major-mode 'jabber-roster-mode))
524 (jabber-roster-mode))
525 (setq buffer-read-only nil)
526 ;; line-number-at-pos is in Emacs >= 21.4. Only used to avoid
527 ;; excessive scrolling when updating roster, so not absolutely
529 (let ((current-line (and (fboundp 'line-number-at-pos) (line-number-at-pos)))
530 (current-column (current-column)))
532 (setq jabber-roster-ewoc nil)
533 (when jabber-roster-show-title
534 (insert (jabber-propertize "Jabber roster" 'face 'jabber-title-large) "\n"))
535 (when jabber-roster-show-bindings
536 (insert "RET Open chat buffer C-k Delete roster item
537 e Edit item s Send subscription request
538 q Bury buffer i Get disco items
539 I Get disco info b Browse
540 j Join groupchat (MUC) v Get client version
541 a Send presence o Show offline contacts on/off
542 C-c C-c Chat menu C-c C-m Multi-User Chat menu
543 C-c C-i Info menu C-c C-r Roster menu
546 H Toggle displaying this text
548 (insert "__________________________________\n\n")
549 (if (null jabber-connections)
550 (insert "Not connected\n")
551 (let ((map (make-sparse-keymap)))
552 (define-key map [mouse-2] #'jabber-send-presence)
553 (insert (jabber-propertize (concat (format " - %s"
554 (cdr (assoc *jabber-current-show* jabber-presence-strings)))
555 (if (not (zerop (length *jabber-current-status*)))
557 (jabber-fix-status *jabber-current-status*)))
559 'face (or (cdr (assoc *jabber-current-show* jabber-presence-faces))
560 'jabber-roster-user-online)
561 ;;'mouse-face (cons 'background-color "light grey")
565 (dolist (jc jabber-connections)
566 ;; use a hash-based roster
567 (when (not (plist-get (fsm-get-state-data jc) :roster-hash))
568 (jabber-roster-prepare-roster jc))
569 ;; We sort everything before putting it in the ewoc
570 (jabber-sort-roster jc)
571 (let ((before-ewoc (point))
575 (let* ((group (car data))
576 (group-name (car group))
577 (buddy (car (cdr data))))
578 (jabber-display-roster-entry jc group-name buddy))))
580 (jabber-propertize (concat
581 (plist-get (fsm-get-state-data jc) :username)
583 (plist-get (fsm-get-state-data jc) :server))
584 'face 'jabber-title-medium)
585 "\n__________________________________\n")
586 "__________________________________"))
588 (plist-put(fsm-get-state-data jc) :roster-ewoc ewoc)
589 (dolist (group (plist-get (fsm-get-state-data jc) :roster-groups))
590 (let* ((group-name (car group))
591 (buddies (jabber-roster-filter-display
593 (plist-get (fsm-get-state-data jc) :roster-hash)))))
594 (when (or jabber-roster-show-empty-group
595 (> (length buddies) 0))
596 (let ((group-node (ewoc-enter-last ewoc (list group nil))))
599 (plist-get (fsm-get-state-data jc) :roster-roll-groups)
601 (dolist (buddy (reverse buddies))
602 (ewoc-enter-after ewoc group-node (list group buddy))))))))
603 (goto-char (point-max))
605 (put-text-property before-ewoc (point)
606 'jabber-account jc)))
608 (goto-char (point-min))
609 (setq buffer-read-only t)
610 (if (called-interactively-p 'interactive)
611 (dolist (hook '(jabber-info-message-hooks jabber-alert-info-message-hooks))
612 (run-hook-with-args hook 'roster (current-buffer) (funcall jabber-alert-info-message-function 'roster (current-buffer)))))
614 ;; Go back to previous line - don't use goto-line, since it
616 (goto-char (point-min))
617 (forward-line (1- current-line))
618 ;; ...and go back to previous column
619 (move-to-column current-column)))))
621 (defun jabber-display-roster-entry (jc group-name buddy)
622 "Format and insert a roster entry for BUDDY at point.
623 BUDDY is a JID symbol. JC is the Jabber connection."
625 (let ((buddy-str (format-spec
626 jabber-roster-line-format
628 (cons ?a (jabber-propertize " " 'display (get buddy 'avatar)))
629 (cons ?c (if (get buddy 'connected) "*" " "))
632 (get buddy 'subscription) "none")
633 jabber-roster-subscription-display)))
634 (cons ?n (if (> (length (get buddy 'name)) 0)
636 (symbol-name buddy)))
637 (cons ?j (symbol-name buddy))
638 (cons ?r (or (get buddy 'resource) ""))
639 (cons ?s (or (cdr (assoc (get buddy 'show)
640 jabber-presence-strings))
642 (cons ?S (if (get buddy 'status)
643 (jabber-fix-status (get buddy 'status))
645 (add-text-properties 0
649 (or (cdr (assoc (get buddy 'show) jabber-presence-faces))
650 'jabber-roster-user-online)
652 ;;(cons 'background-color "light grey")
662 (when (or (eq jabber-show-resources 'always)
663 (and (eq jabber-show-resources 'sometimes)
664 (> (jabber-count-connected-resources buddy) 1)))
665 (dolist (resource (get buddy 'resources))
666 (when (plist-get (cdr resource) 'connected)
667 (let ((resource-str (format-spec jabber-resource-line-format
672 (get buddy 'name)) 0)
674 (symbol-name buddy)))
675 (cons ?j (symbol-name buddy))
684 (cdr resource) 'show)
685 jabber-presence-strings))
687 (cdr resource) 'show)))
688 (cons ?S (if (plist-get
689 (cdr resource) 'status)
691 (plist-get (cdr resource)
694 (cons ?p (number-to-string
695 (plist-get (cdr resource)
697 (add-text-properties 0
698 (length resource-str)
701 (or (cdr (assoc (plist-get
704 jabber-presence-faces))
705 'jabber-roster-user-online)
707 (format "%s/%s" (symbol-name buddy) (car resource))
711 (insert "\n" resource-str))))))
712 (let ((group-name (or group-name
713 jabber-roster-default-group-name)))
714 (add-text-properties 0
717 'face 'jabber-title-small
718 'jabber-group group-name
721 (insert group-name))))
724 (defun jabber-roster-update (jc new-items changed-items deleted-items)
725 "Update roster, in memory and on display.
726 Add NEW-ITEMS, update CHANGED-ITEMS and remove DELETED-ITEMS, all
727 three being lists of JID symbols.
728 JC is the Jabber connection."
729 (let* ((roster (plist-get (fsm-get-state-data jc) :roster))
730 (hash (plist-get (fsm-get-state-data jc) :roster-hash))
731 (ewoc (plist-get (fsm-get-state-data jc) :roster-ewoc))
732 (all-groups (plist-get (fsm-get-state-data jc) :roster-groups))
734 (lambda (deleted-items)
735 (dolist (delete-this deleted-items)
736 (let ((groups (get delete-this 'groups))
740 ((group (or g jabber-roster-default-group-name))
741 (buddies (gethash group hash)))
743 (setq new-groups (append new-groups (list group))))
745 (delq delete-this buddies)
748 (dolist (group groups)
750 (terminator groups)))))))
753 (dolist (delete-this deleted-items)
754 (setq roster (delq delete-this roster)))
755 (setq roster (append new-items roster))
756 (plist-put (fsm-get-state-data jc) :roster roster)
758 ;; update a hash-roster
760 (jabber-roster-prepare-roster jc)
762 (when jabber-roster-debug
763 (message "update hash-based roster"))
766 (dolist (delete-this (append deleted-items changed-items))
767 (let ((jid (symbol-name delete-this)))
768 (when jabber-roster-debug
769 (message (concat "delete jid: " jid)))
770 (dolist (group (mapcar (lambda (g) (car g)) all-groups))
771 (when jabber-roster-debug
772 (message (concat "try to delete jid: " jid " from group " group)))
774 (delq delete-this (gethash group hash))
777 ;; insert changed-items
778 (dolist (insert-this (append changed-items new-items))
779 (let ((jid (symbol-name insert-this)))
780 (when jabber-roster-debug
781 (message (concat "insert jid: " jid)))
782 (dolist (group (or (get insert-this 'groups)
783 (list jabber-roster-default-group-name)))
784 (when jabber-roster-debug
785 (message (concat "insert jid: " jid " to group " group)))
787 (append (gethash group hash)
790 (setq all-groups (append all-groups (list (list group)))))))
792 (when jabber-roster-debug
793 (message "remove duplicates from new group"))
794 (setq all-groups (sort
795 (cl-remove-duplicates all-groups
796 :test (lambda (g1 g2)
797 (let ((g1-name (car g1))
802 (let ((g1-name (car g1))
807 (plist-put (fsm-get-state-data jc) :roster-groups all-groups))
809 (when jabber-roster-debug
810 (message "re display roster"))
812 ;; recreate roster buffer
813 (jabber-display-roster)))
815 (defalias 'jabber-presence-update-roster 'ignore)
816 ;;jabber-presence-update-roster is not needed anymore.
817 ;;Its work is done in `jabber-process-presence'."
818 (make-obsolete 'jabber-presence-update-roster 'ignore "27.2")
820 (defun jabber-next-property (&optional prev)
821 "Return position of next property appearence or nil if there is none.
822 If optional PREV is non-nil, return position of previous property appearence."
825 (nextprev (if prev 'previous-single-property-change
826 'next-single-property-change)))
829 (let ((jid (funcall nextprev pos 'jabber-jid))
830 (group (funcall nextprev pos 'jabber-group)))
834 (t (funcall (if prev 'max 'min) jid group)))))
837 (setq found (or (get-text-property pos 'jabber-jid)
838 (get-text-property pos 'jabber-group)))))
841 (defun jabber-go-to-next-roster-item ()
842 "Move the cursor to the next jid/group in the buffer."
844 (let* ((next (jabber-next-property))
846 (progn (goto-char (point-min))
847 (jabber-next-property)) next)))
848 (if next (goto-char next)
849 (goto-char (point-min)))))
851 (defun jabber-go-to-previous-roster-item ()
852 "Move the cursor to the previous jid/group in the buffer."
854 (let* ((previous (jabber-next-property 'prev))
855 (previous (if (not previous)
856 (progn (goto-char (point-max))
857 (jabber-next-property 'prev)) previous)))
858 (if previous (goto-char previous)
859 (goto-char (point-max)))))
861 (defun jabber-roster-restore-groups (jc)
862 "Restore roster's groups rolling state from private storage.
863 JC is the Jabber connection."
864 (interactive (list (jabber-read-account)))
865 (jabber-private-get jc 'roster "emacs-jabber"
866 'jabber-roster-restore-groups-1 'ignore))
868 (defun jabber-roster-restore-groups-1 (jc xml-data)
869 "Parse roster groups and restore rolling state.
871 JC is the Jabber connection.
872 XML-DATA is the parsed tree data from the stream (stanzas)
873 obtained from `xml-parse-region'."
874 (when (string= (jabber-xml-get-xmlns xml-data) "emacs-jabber")
875 (let* ((data (car (last xml-data)))
876 (groups (if (stringp data) (split-string data "\n") nil)))
877 (dolist (group groups)
878 (jabber-roster-roll-group jc group t)))))
880 (defun jabber-roster-save-groups ()
881 "Save roster's groups rolling state in private storage."
883 (dolist (jc jabber-connections)
884 (let* ((groups (plist-get (fsm-get-state-data jc) :roster-roll-groups))
887 (mapconcat (lambda (a) (substring-no-properties a)) groups "\n")
889 (jabber-private-set jc
890 `(roster ((xmlns . "emacs-jabber"))
892 'jabber-report-success "Roster groups saved"
893 'jabber-report-success "Failed to save roster groups"))))
895 (provide 'jabber-roster)
897 ;;; arch-tag: 096af063-0526-4dd2-90fd-bc6b5ba07d32