ChatService.js 4.1 KB

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