Revision: mange@freemail.hu--2005/emacs-jabber--cvs-head--0--patch-556
[emacs-jabber.git] / jabber-search.el
blobc5a2f9ecf303fa7e890c0f63d26f592991fd4c7c
1 ;; jabber-search.el - searching by JEP-0055, with x:data support
3 ;; Copyright (C) 2002, 2003, 2004 - tom berger - object@intelectronica.net
4 ;; Copyright (C) 2003, 2004 - Magnus Henoch - mange@freemail.hu
6 ;; This file is a part of jabber.el.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program; if not, write to the Free Software
20 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 (require 'jabber-register)
24 (add-to-list 'jabber-jid-service-menu
25 (cons "Search directory" 'jabber-get-search))
26 (defun jabber-get-search (jc to)
27 "Send IQ get request in namespace \"jabber:iq:search\"."
28 (interactive (list (jabber-read-account)
29 (jabber-read-jid-completing "Search what database: ")))
30 (jabber-send-iq jc to
31 "get"
32 '(query ((xmlns . "jabber:iq:search")))
33 #'jabber-process-data #'jabber-process-register-or-search
34 #'jabber-report-success "Search field retrieval"))
36 ;; jabber-process-register-or-search logically comes here, rendering
37 ;; the search form, but since register and search are so similar,
38 ;; having two functions would be serious code duplication. See
39 ;; jabber-register.el.
41 ;; jabber-submit-search is called when the "submit" button of the
42 ;; search form is activated.
43 (defun jabber-submit-search (&rest ignore)
44 "Submit search. See `jabber-process-register-or-search'."
46 (let ((text (concat "Search at " jabber-submit-to)))
47 (jabber-send-iq jabber-buffer-connection jabber-submit-to
48 "set"
50 (cond
51 ((eq jabber-form-type 'register)
52 `(query ((xmlns . "jabber:iq:search"))
53 ,@(jabber-parse-register-form)))
54 ((eq jabber-form-type 'xdata)
55 `(query ((xmlns . "jabber:iq:search"))
56 ,(jabber-parse-xdata-form)))
58 (error "Unknown form type: %s" jabber-form-type)))
59 #'jabber-process-data #'jabber-process-search-result
60 #'jabber-report-success text))
62 (message "Search sent"))
64 (defun jabber-process-search-result (jc xml-data)
65 "Receive and display search results."
67 ;; This function assumes that all search results come in one packet,
68 ;; which is not necessarily the case.
69 (let ((query (jabber-iq-query xml-data))
70 (have-xdata nil)
71 xdata fields (jid-fields 0))
73 ;; First, check for results in jabber:x:data form.
74 (dolist (x (jabber-xml-get-children query 'x))
75 (when (string= (jabber-xml-get-attribute x 'xmlns) "jabber:x:data")
76 (setq have-xdata t)
77 (setq xdata x)))
79 (if have-xdata
80 (jabber-render-xdata-search-results xdata)
82 (insert (jabber-propertize "Search results" 'face 'jabber-title-medium) "\n")
84 (setq fields '((first . (label "First name" column 0))
85 (last . (label "Last name" column 15))
86 (nick . (label "Nickname" column 30))
87 (jid . (label "JID" column 45))
88 (email . (label "E-mail" column 65))))
89 (setq jid-fields 1)
91 (dolist (field-cons fields)
92 (indent-to (plist-get (cdr field-cons) 'column) 1)
93 (insert (jabber-propertize (plist-get (cdr field-cons) 'label) 'face 'bold)))
94 (insert "\n\n")
96 ;; Now, the items
97 (dolist (item (jabber-xml-get-children query 'item))
98 (let ((start-of-line (point))
99 jid)
101 (dolist (field-cons fields)
102 (let ((field-plist (cdr field-cons))
103 (value (if (eq (car field-cons) 'jid)
104 (setq jid (jabber-xml-get-attribute item 'jid))
105 (car (jabber-xml-node-children (car (jabber-xml-get-children item (car field-cons))))))))
106 (indent-to (plist-get field-plist 'column) 1)
107 (if value (insert value))))
109 (if jid
110 (put-text-property start-of-line (point)
111 'jabber-jid jid))
112 (insert "\n"))))))
114 (provide 'jabber-search)
116 ;;; arch-tag: c39e9241-ab6f-4ac5-b1ba-7908bbae009c