ChatService.js 4.3 KB

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