1 | dojo.provide("dojox.xmpp.ChatService"); |
---|
2 | |
---|
3 | dojox.xmpp.chat = { |
---|
4 | CHAT_STATE_NS: 'http://jabber.org/protocol/chatstates', |
---|
5 | |
---|
6 | ACTIVE_STATE: 'active', |
---|
7 | COMPOSING_STATE: 'composing', |
---|
8 | INACTIVE_STATE: 'inactive', |
---|
9 | PAUSED_STATE: 'paused', |
---|
10 | GONE_STATE: 'gone' |
---|
11 | }; |
---|
12 | |
---|
13 | dojo.declare("dojox.xmpp.ChatService", null, { |
---|
14 | state: "", |
---|
15 | |
---|
16 | constructor: function(){ |
---|
17 | this.state=""; |
---|
18 | this.chatid = Math.round(Math.random() * 1000000000000000); |
---|
19 | }, |
---|
20 | |
---|
21 | recieveMessage: function(msg,initial){ |
---|
22 | if (msg&&!initial){ |
---|
23 | this.onNewMessage(msg); |
---|
24 | } |
---|
25 | }, |
---|
26 | |
---|
27 | setSession: function(session){ |
---|
28 | this.session = session; |
---|
29 | }, |
---|
30 | |
---|
31 | setState: function(state){ |
---|
32 | if (this.state != state){ |
---|
33 | this.state = state; |
---|
34 | } |
---|
35 | }, |
---|
36 | |
---|
37 | invite: function(contact){ |
---|
38 | if (this.uid){return;} |
---|
39 | |
---|
40 | |
---|
41 | if(!contact || contact==''){ |
---|
42 | throw new Error("ChatService::invite() contact is NULL"); |
---|
43 | } |
---|
44 | |
---|
45 | this.uid = contact; |
---|
46 | |
---|
47 | var req = { |
---|
48 | xmlns: "jabber:client", |
---|
49 | to: this.uid, |
---|
50 | from: this.session.jid + "/" + this.session.resource, |
---|
51 | type: "chat" |
---|
52 | } |
---|
53 | var request = new dojox.string.Builder(dojox.xmpp.util.createElement("message", req, false)); |
---|
54 | request.append(dojox.xmpp.util.createElement("thread",{},false)); |
---|
55 | request.append(this.chatid); |
---|
56 | request.append("</thread>"); |
---|
57 | request.append(dojox.xmpp.util.createElement("active",{xmlns: dojox.xmpp.chat.CHAT_STATE_NS},true)); |
---|
58 | request.append("</message>"); |
---|
59 | this.session.dispatchPacket(request.toString()); |
---|
60 | |
---|
61 | this.onInvite(contact); |
---|
62 | this.setState(dojox.xmpp.chat.CHAT_STATE_NS); |
---|
63 | }, |
---|
64 | |
---|
65 | |
---|
66 | sendMessage: function(msg){ |
---|
67 | if (!this.uid){ |
---|
68 | //console.log("ChatService::sendMessage() - Contact Id is null, need to invite to chat"); |
---|
69 | return; |
---|
70 | } |
---|
71 | |
---|
72 | if ((!msg.body || msg.body=="") && !msg.xhtml){return;} |
---|
73 | |
---|
74 | var req = { |
---|
75 | xmlns: "jabber:client", |
---|
76 | to: this.uid, |
---|
77 | from: this.session.jid + "/" + this.session.resource, |
---|
78 | type: "chat" |
---|
79 | } |
---|
80 | |
---|
81 | var message = new dojox.string.Builder(dojox.xmpp.util.createElement("message",req,false)); |
---|
82 | var html = dojox.xmpp.util.createElement("html", { "xmlns":dojox.xmpp.xmpp.XHTML_IM_NS},false) |
---|
83 | |
---|
84 | var bodyTag = dojox.xmpp.util.createElement("body", {"xml:lang":this.session.lang, "xmlns":dojox.xmpp.xmpp.XHTML_BODY_NS}, false) + msg.body + "</body>"; |
---|
85 | var bodyPlainTag = dojox.xmpp.util.createElement("body", {}, false) + dojox.xmpp.util.stripHtml(msg.body) + "</body>"; |
---|
86 | /* |
---|
87 | if (msg.xhtml){ |
---|
88 | if (msg.xhtml.getAttribute('xmlns') != dojox.xmpp.xmpp.XHTML_IM_NS){ |
---|
89 | //console.log("ChatService::sendMessage() - Cannot use this xhtml without the propper xmlns"); |
---|
90 | }else{ |
---|
91 | //FIXME do this in some portable way |
---|
92 | //console.log("ChatService::sendMessage() - FIXME Serialize XHTML to string: ", msg.xhtml.toString()); |
---|
93 | } |
---|
94 | } |
---|
95 | */ |
---|
96 | if (message.subject && message.subject != ""){ |
---|
97 | message.append(dojox.xmpp.util.createElement("subject",{},false)); |
---|
98 | message.append(message.subject); |
---|
99 | message.append("</subject>"); |
---|
100 | } |
---|
101 | message.append(bodyPlainTag); |
---|
102 | message.append(html); |
---|
103 | message.append(bodyTag); |
---|
104 | message.append("</html>"); |
---|
105 | message.append(dojox.xmpp.util.createElement("thread", {}, false)); |
---|
106 | message.append(this.chatid); |
---|
107 | message.append("</thread>"); |
---|
108 | |
---|
109 | if (this.useChatStates){ |
---|
110 | message.append(dojox.xmpp.util.createElement("active",{xmlns: dojox.xmpp.chat.CHAT_STATE_NS},true)); |
---|
111 | } |
---|
112 | message.append("</message>"); |
---|
113 | |
---|
114 | this.session.dispatchPacket(message.toString()); |
---|
115 | }, |
---|
116 | |
---|
117 | sendChatState: function(state){ |
---|
118 | if (!this.useChatState || this.firstMessage){return;} |
---|
119 | if (state==this._currentState){return;} |
---|
120 | |
---|
121 | var req={ |
---|
122 | xmlns: "jabber:client", |
---|
123 | to: this.uid, |
---|
124 | from: this.session.jid + "/" + this.session.resource, |
---|
125 | type: "chat" |
---|
126 | } |
---|
127 | |
---|
128 | var request = new dojox.string.Builder(dojox.xmpp.util.createElement("message",req,false)); |
---|
129 | request.append(dojox.xmpp.util.createElement(state, {xmlns: dojox.xmpp.chat.CHAT_STATE_NS},true)); |
---|
130 | this._currentState = state; |
---|
131 | request.append("<thread>"); |
---|
132 | request.append(this.chatid); |
---|
133 | request.append("</thread></message>"); |
---|
134 | |
---|
135 | this.session.dispatchPacket(request.toString()); |
---|
136 | }, |
---|
137 | |
---|
138 | //EVENTS |
---|
139 | onNewMessage: function(msg){}, |
---|
140 | onInvite: function(contact){} |
---|
141 | }); |
---|