source: sandbox/jabberit_messenger/trophy_expresso/strophejs/plugins/strophe.pubsub.js @ 2397

Revision 2397, 7.5 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #986 - Importacao do modulo trophy integrado ao expresso.

  • Property svn:executable set to *
Line 
1/*
2  Copyright 2008, Stanziq  Inc.
3*/
4
5Strophe.addConnectionPlugin('pubsub', {
6/*
7  Extend connection object to have plugin name 'pubsub'. 
8*/
9    _connection: null,
10
11        //The plugin must have the init function.
12        init: function(conn) {
13
14            this._connection = conn;
15
16            /*
17              Function used to setup plugin.
18            */
19           
20            /* extend name space
21             *  NS.PUBSUB - XMPP Publish Subscribe namespace
22             *              from XEP 60. 
23             *
24             *  NS.PUBSUB_SUBSCRIBE_OPTIONS - XMPP pubsub
25             *                                options namespace from XEP 60.
26             */
27            Strophe.addNamespace('PUBSUB',"http://jabber.org/protocol/pubsub");
28            Strophe.addNamespace('PUBSUB_SUBSCRIBE_OPTIONS',
29                                 Strophe.NS.PUBSUB+"#subscribe_options");
30            Strophe.addNamespace('PUBSUB_ERRORS',Strophe.NS.PUBSUB+"#errors");
31            Strophe.addNamespace('PUBSUB_EVENT',Strophe.NS.PUBSUB+"#event");
32            Strophe.addNamespace('PUBSUB_OWNER',Strophe.NS.PUBSUB+"#owner");
33            Strophe.addNamespace('PUBSUB_AUTO_CREATE',
34                                 Strophe.NS.PUBSUB+"#auto-create");
35            Strophe.addNamespace('PUBSUB_PUBLISH_OPTIONS',
36                                 Strophe.NS.PUBSUB+"#publish-options");
37            Strophe.addNamespace('PUBSUB_NODE_CONFIG',
38                                 Strophe.NS.PUBSUB+"#node_config");
39            Strophe.addNamespace('PUBSUB_CREATE_AND_CONFIGURE',
40                                 Strophe.NS.PUBSUB+"#create-and-configure");
41            Strophe.addNamespace('PUBSUB_SUBSCRIBE_AUTHORIZATION',
42                                 Strophe.NS.PUBSUB+"#subscribe_authorization");
43            Strophe.addNamespace('PUBSUB_GET_PENDING',
44                                 Strophe.NS.PUBSUB+"#get-pending");
45            Strophe.addNamespace('PUBSUB_MANAGE_SUBSCRIPTIONS',
46                                 Strophe.NS.PUBSUB+"#manage-subscriptions");
47            Strophe.addNamespace('PUBSUB_META_DATA',
48                                 Strophe.NS.PUBSUB+"#meta-data");
49           
50        },
51        /***Function
52           
53        Create a pubsub node on the given service with the given node
54        name.
55       
56        Parameters:
57        (String) jid - The node owner's jid.
58        (String) service - The name of the pubsub service.
59        (String) node -  The name of the pubsub node.
60        (Dictionary) options -  The configuration options for the  node.
61        (Function) call_back - Used to determine if node
62        creation was sucessful.
63       
64        Returns:
65        Iq id used to send subscription.
66        */
67        createNode: function(jid,service,node,options, call_back) {
68           
69            var iqid = this._connection.getUniqueId("pubsubcreatenode");
70           
71            var iq = $iq({from:jid, to:service, type:'set', id:iqid});
72           
73            var c_options = Strophe.xmlElement("configure",[]);
74            var x = Strophe.xmlElement("x",[["xmlns","jabber:x:data"]]);
75            var form_field = Strophe.xmlElement("field",[["var","FORM_TYPE"],
76                                                         ["type","hidden"]]);
77            var value = Strophe.xmlElement("value",[]);
78            var text = Strophe.xmlTextNode(Strophe.NS.PUBSUB+"#node_config");
79            value.appendChild(text);
80            form_field.appendChild(value);
81            x.appendChild(form_field);
82           
83            for (var i in options)
84            {
85                var val = options[i];
86                x.appendChild(val);
87            }
88           
89            if(options.length && options.length != 0)
90            {
91                c_options.appendChild(x);
92            }
93           
94            iq.c('pubsub',
95                {xmlns:Strophe.NS.PUBSUB}).c('create',
96                    {node:node}).up().cnode(c_options);
97           
98            this._connection.addHandler(call_back,
99                                  null,
100                                  'iq',
101                                  null,
102                                  iqid,
103                                  null);
104            this._connection.send(iq.tree());
105            return iqid;
106        },
107        /***Function
108            Subscribe to a node in order to receive event items.
109           
110            Parameters:
111            (String) jid - The node owner's jid.
112            (String) service - The name of the pubsub service.
113            (String) node -  The name of the pubsub node.
114            (Array) options -  The configuration options for the  node.
115            (Function) event_cb - Used to recieve subscription events.
116            (Function) call_back - Used to determine if node
117            creation was sucessful.
118           
119            Returns:
120            Iq id used to send subscription.
121        */
122        subscribe: function(jid,service,node,options, event_cb, call_back) {
123           
124            var subid = this._connection.getUniqueId("subscribenode");
125           
126            //create subscription options
127            var sub_options = Strophe.xmlElement("options",[]);
128            var x = Strophe.xmlElement("x",[["xmlns","jabber:x:data"]]);
129            var form_field = Strophe.xmlElement("field",[["var","FORM_TYPE"],
130                                                         ["type","hidden"]]);
131            var value = Strophe.xmlElement("value",[]);
132            var text = Strophe.xmlTextNode(Strophe.NS.PUBSUB_SUBSCRIBE_OPTIONS);
133            value.appendChild(text);
134            form_field.appendChild(value);
135            x.appendChild(form_field);
136           
137            var sub = $iq({from:jid, to:service, type:'set', id:subid})
138
139            if(options && options.length && options.length !== 0)
140            {
141                for (var i = 0; i < options.length; i++)
142                {
143                    var val = options[i];
144                    x.appendChild(val);
145                }
146                sub_options.appendChild(x);
147               
148                sub.c('pubsub', { xmlns:Strophe.NS.PUBSUB }).c('subscribe',
149                {node:node,jid:jid}).up().cnode(sub_options);
150            }
151            else
152            {
153               
154                sub.c('pubsub', { xmlns:Strophe.NS.PUBSUB }).c('subscribe',
155                    {node:node,jid:jid});
156            }
157           
158           
159            this._connection.addHandler(call_back,
160                                  null,
161                                  'iq',
162                                  null,
163                                  subid,
164                                  null);
165           
166            //add the event handler to receive items
167            this._connection.addHandler(event_cb,
168                                  null,
169                                  'message',
170                                  null,
171                                  null,
172                                  null);
173            this._connection.send(sub.tree());
174            return subid;
175           
176        },
177        /***Function
178            Unsubscribe from a node.
179           
180            Parameters:
181            (String) jid - The node owner's jid.
182            (String) service - The name of the pubsub service.
183            (String) node -  The name of the pubsub node.
184            (Function) call_back - Used to determine if node
185            creation was sucessful.
186           
187        */   
188        unsubscribe: function(jid,service,node, call_back) {
189           
190            var subid = this._connection.getUniqueId("unsubscribenode");
191           
192           
193            var sub = $iq({from:jid, to:service, type:'set', id:subid})
194            sub.c('pubsub', { xmlns:Strophe.NS.PUBSUB }).c('unsubscribe',
195                {node:node,jid:jid});
196
197           
198           
199            this._connection.addHandler(call_back,
200                                  null,
201                                  'iq',
202                                  null,
203                                  subid,
204                                  null);
205            this._connection.send(sub.tree());
206           
207           
208            return subid;
209           
210        },
211        /***Function
212           
213        Publish and item to the given pubsub node.
214       
215        Parameters:
216        (String) jid - The node owner's jid.
217        (String) service - The name of the pubsub service.
218        (String) node -  The name of the pubsub node.
219        (Array) items -  The list of items to be published.
220        (Function) call_back - Used to determine if node
221        creation was sucessful.
222        */   
223        publish: function(jid, service, node, items, call_back) {
224            var pubid = this._connection.getUniqueId("publishnode");
225           
226           
227            var publish_elem = Strophe.xmlElement("publish",
228                                                  [["node",
229                                                    node],
230                                                   ["jid",
231                                                    jid]]);
232            for (var i in items)
233            {
234                var item = Strophe.xmlElement("item",[]);
235                var entry = Strophe.xmlElement("entry",[]);
236                var t = Strophe.xmlTextNode(items[i]);
237                entry.appendChild(t);
238                item.appendChild(entry);
239                publish_elem.appendChild(item);
240            }
241           
242            var pub = $iq({from:jid, to:service, type:'set', id:pubid})
243            pub.c('pubsub', { xmlns:Strophe.NS.PUBSUB }).cnode(publish_elem);
244           
245           
246            this._connection.addHandler(call_back,
247                                  null,
248                                  'iq',
249                                  null,
250                                  pubid,
251                                  null);
252            this._connection.send(pub.tree());
253           
254           
255            return pubid;
256        },
257        /*Function: items
258          Used to retrieve the persistent items from the pubsub node.
259         
260        */
261        items: function(jid,service,node,ok_callback,error_back) {
262            var pub = $iq({from:jid, to:service, type:'get'})
263           
264            //ask for all items
265            pub.c('pubsub',
266                { xmlns:Strophe.NS.PUBSUB }).c('items',{node:node});
267           
268            return this._connection.sendIQ(pub.tree(),ok_callback,error_back);
269        }
270});
Note: See TracBrowser for help on using the repository browser.