/* -*- C++ -*- * Copyright (C) 2001-2003, Christof Meerwald * http://JabXPCOM.sunsite.dk */ /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License * as published by the Free Software Foundation; version 2 dated June, * 1991. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ #include "jabxpcom-session.h" #include "jabxpcom-presence.h" #include "jabxpcom-message.h" #include "jabxpcom-iq.h" #include "judoxpcom-wrapper.h" #include #include #include #include #include #include #include NS_IMPL_ISUPPORTS2(Jabber_Session_Wrapper, jabISession, jabIDataStream) Jabber_Session_Wrapper::Jabber_Session_Wrapper() { NS_INIT_ISUPPORTS(); /* member initializers and constructor code */ #ifdef JABXPCOM_DEBUG printf("constructing Jabber_Session_Wrapper()\n"); #endif session_.evtTransmitXML.connect(SigC::slot(*this, &Jabber_Session_Wrapper::transmit_xml)); } Jabber_Session_Wrapper::~Jabber_Session_Wrapper() { /* destructor code */ #ifdef JABXPCOM_DEBUG printf("deleting Jabber_Session_Wrapper()\n"); #endif } void Jabber_Session_Wrapper::transmit_xml(const char *data) { if (output_stream_) { output_stream_->Write(reinterpret_cast(const_cast(data)), strlen(data)); } } /* void write ([array, size_is (length)] in octet data, in unsigned long length); */ NS_IMETHODIMP Jabber_Session_Wrapper::Write(PRUint8 *data, PRUint32 length) { session_.push(reinterpret_cast(data), length); return NS_OK; } /* void close (in long err_code); */ NS_IMETHODIMP Jabber_Session_Wrapper::Close(PRInt32 err_code) { static_cast(session_).onDocumentEnd(); return NS_OK; } /* readonly attribute jabIDataStream inputStream; */ NS_IMETHODIMP Jabber_Session_Wrapper::GetInputStream(jabIDataStream * *aInputStream) { QueryInterface(NS_GET_IID(jabIDataStream), (void **) aInputStream); return NS_OK; } /* attribute jabIDataStream outputStream; */ NS_IMETHODIMP Jabber_Session_Wrapper::GetOutputStream(jabIDataStream * *aOutputStream) { *aOutputStream = output_stream_; NS_IF_ADDREF(*aOutputStream); return NS_OK; } NS_IMETHODIMP Jabber_Session_Wrapper::SetOutputStream(jabIDataStream * aOutputStream) { output_stream_ = aOutputStream; return NS_OK; } /* readonly attribute ConnectionState connState; */ NS_IMETHODIMP Jabber_Session_Wrapper::GetConnState(ConnectionState *aConnState) { *aConnState = session_.getConnState(); return NS_OK; } /* readonly attribute string userName; */ NS_IMETHODIMP Jabber_Session_Wrapper::GetUserName(nsACString &aUserName) { const std::string &username = session_.getUserName(); aUserName.Assign(username.data(), username.length()); return NS_OK; } IMPLEMENT_WRAP_EVENT(Jabber_Session_Wrapper, Connected) void Jabber_Session_Wrapper::WrapEvtConnected::OnConnected(const judo::Element &element) { Judo_ConstElement_Wrapper *element_wrapper = new Judo_ConstElement_Wrapper(&element); NS_ADDREF(element_wrapper); slot_->OnConnected(element_wrapper); if (element_wrapper->Release()) { // the wrapper is still referenced, so we have to detach from the // contained element (and make a private copy) element_wrapper->detach(); } } /* unsigned long connectEvtConnected (in jabISessionEvtConnected slot); */ NS_IMETHODIMP Jabber_Session_Wrapper::ConnectEvtConnected(jabISessionEvtConnected *slot, PRUint32 *_retval) { CONNECT_WRAPPED_EVENT(Connected, session_.evtConnected, slot); return NS_OK; } /* void disconnectEvtConnected (in unsigned long id); */ NS_IMETHODIMP Jabber_Session_Wrapper::DisconnectEvtConnected(PRUint32 id) { return NS_ERROR_NOT_IMPLEMENTED; } IMPLEMENT_WRAP_EVENT(Jabber_Session_Wrapper, Disconnected) void Jabber_Session_Wrapper::WrapEvtDisconnected::OnDisconnected() { slot_->OnDisconnected(); } /* unsigned long connectEvtDisconnected (in jabISessionEvtDisconnected slot); */ NS_IMETHODIMP Jabber_Session_Wrapper::ConnectEvtDisconnected(jabISessionEvtDisconnected *slot, PRUint32 *_retval) { CONNECT_WRAPPED_EVENT(Disconnected, session_.evtDisconnected, slot); return NS_OK; } /* void disconnectEvtDisconnected (in unsigned long id); */ NS_IMETHODIMP Jabber_Session_Wrapper::DisconnectEvtDisconnected(PRUint32 id) { return NS_ERROR_NOT_IMPLEMENTED; } IMPLEMENT_WRAP_EVENT(Jabber_Session_Wrapper, AuthError) void Jabber_Session_Wrapper::WrapEvtAuthError::OnAuthError(int code, const char *data) { const nsCString data_str(data); slot_->OnAuthError(code, data_str); } /* unsigned long connectEvtAuthError (in jabISessionEvtAuthError slot); */ NS_IMETHODIMP Jabber_Session_Wrapper::ConnectEvtAuthError(jabISessionEvtAuthError *slot, PRUint32 *_retval) { CONNECT_WRAPPED_EVENT(AuthError, session_.evtAuthError, slot); return NS_OK; } /* void disconnectEvtAuthError (in unsigned long id); */ NS_IMETHODIMP Jabber_Session_Wrapper::DisconnectEvtAuthError(PRUint32 id) { return NS_ERROR_NOT_IMPLEMENTED; } IMPLEMENT_WRAP_EVENT(Jabber_Session_Wrapper, Iq) void Jabber_Session_Wrapper::WrapEvtIq::OnIq(const judo::Element &element) { Judo_ConstElement_Wrapper *element_wrapper = new Judo_ConstElement_Wrapper(&element); NS_ADDREF(element_wrapper); slot_->OnIq(element_wrapper); if (element_wrapper->Release()) { // the wrapper is still referenced, so we have to detach from the // contained element (and make a private copy) element_wrapper->detach(); } } /* unsigned long connectEvtIq (in jabISessionEvtIq slot); */ NS_IMETHODIMP Jabber_Session_Wrapper::ConnectEvtIq(jabISessionEvtIq *slot, PRUint32 *_retval) { CONNECT_WRAPPED_EVENT(Iq, session_.evtIQ, slot); return NS_OK; } /* void disconnectEvtIq (in unsigned long id); */ NS_IMETHODIMP Jabber_Session_Wrapper::DisconnectEvtIq(PRUint32 id) { return NS_ERROR_NOT_IMPLEMENTED; } IMPLEMENT_WRAP_EVENT(Jabber_Session_Wrapper, Message) void Jabber_Session_Wrapper::WrapEvtMessage::OnMessage(const jabberoo::Message &message) { Jabber_ConstMessage_Wrapper *message_wrapper = new Jabber_ConstMessage_Wrapper(&message); NS_ADDREF(message_wrapper); slot_->OnMessage(message_wrapper); if (message_wrapper->Release()) { // the wrapper is still referenced, so we have to detach from the // contained element (and make a private copy) message_wrapper->detach(); } } /* unsigned long connectEvtMessage (in jabISessionEvtMessage slot); */ NS_IMETHODIMP Jabber_Session_Wrapper::ConnectEvtMessage(jabISessionEvtMessage *slot, PRUint32 *_retval) { CONNECT_WRAPPED_EVENT(Message, session_.evtMessage, slot); return NS_OK; } /* void disconnectEvtMessage (in unsigned long id); */ NS_IMETHODIMP Jabber_Session_Wrapper::DisconnectEvtMessage(PRUint32 id) { return NS_ERROR_NOT_IMPLEMENTED; } IMPLEMENT_WRAP_EVENT(Jabber_Session_Wrapper, UnknownPacket) void Jabber_Session_Wrapper::WrapEvtUnknownPacket::OnUnknownPacket(const judo::Element &element) { Judo_ConstElement_Wrapper *element_wrapper = new Judo_ConstElement_Wrapper(&element); NS_ADDREF(element_wrapper); slot_->OnUnknownPacket(element_wrapper); if (element_wrapper->Release()) { // the wrapper is still referenced, so we have to detach from the // contained element (and make a private copy) element_wrapper->detach(); } } /* unsigned long connectEvtUnknownPacket (in jabISessionEvtUnknownPacket slot); */ NS_IMETHODIMP Jabber_Session_Wrapper::ConnectEvtUnknownPacket(jabISessionEvtUnknownPacket *slot, PRUint32 *_retval) { CONNECT_WRAPPED_EVENT(UnknownPacket, session_.evtUnknownPacket, slot); return NS_OK; } /* void disconnectEvtUnknownPacket (in unsigned long id); */ NS_IMETHODIMP Jabber_Session_Wrapper::DisconnectEvtUnknownPacket(PRUint32 id) { return NS_ERROR_NOT_IMPLEMENTED; } IMPLEMENT_WRAP_EVENT(Jabber_Session_Wrapper, IqVersion) void Jabber_Session_Wrapper::WrapEvtIqVersion::OnIqVersion(std::string &name, std::string &version, std::string &os) { nsCString name_str, version_str, os_str; slot_->OnIqVersion(name_str, version_str, os_str); name.assign(PromiseFlatCString(name_str).get(), name_str.Length()); version.assign(PromiseFlatCString(version_str).get(), version_str.Length()); os.assign(PromiseFlatCString(os_str).get(), os_str.Length()); } /* unsigned long connectEvtIqVersion (in jabISessionEvtIqVersion slot); */ NS_IMETHODIMP Jabber_Session_Wrapper::ConnectEvtIqVersion(jabISessionEvtIqVersion *slot, PRUint32 *_retval) { CONNECT_WRAPPED_EVENT(IqVersion, session_.evtOnVersion, slot); return NS_OK; } /* void disconnectEvtIqVersion (in unsigned long id); */ NS_IMETHODIMP Jabber_Session_Wrapper::DisconnectEvtIqVersion(PRUint32 id) { return NS_ERROR_NOT_IMPLEMENTED; } IMPLEMENT_WRAP_EVENT(Jabber_Session_Wrapper, IqLast) void Jabber_Session_Wrapper::WrapEvtIqLast::OnIqLast(std::string &seconds) { nsCString seconds_str; slot_->OnIqLast(seconds_str); seconds.assign(PromiseFlatCString(seconds_str).get(), seconds_str.Length()); } /* unsigned long connectEvtIqLast (in jabISessionEvtIqLast slot); */ NS_IMETHODIMP Jabber_Session_Wrapper::ConnectEvtIqLast(jabISessionEvtIqLast *slot, PRUint32 *_retval) { CONNECT_WRAPPED_EVENT(IqLast, session_.evtOnLast, slot); return NS_OK; } /* void disconnectEvtIqLast (in unsigned long id); */ NS_IMETHODIMP Jabber_Session_Wrapper::DisconnectEvtIqLast(PRUint32 id) { return NS_ERROR_NOT_IMPLEMENTED; } IMPLEMENT_WRAP_EVENT(Jabber_Session_Wrapper, Presence) void Jabber_Session_Wrapper::WrapEvtPresence::OnPresence(const jabberoo::Presence &presence, jabberoo::Presence::Type prev_type) { Jabber_Presence_Wrapper *presence_wrapper = new Jabber_Presence_Wrapper(&presence); NS_ADDREF(presence_wrapper); slot_->OnPresence(presence_wrapper, prev_type); if (presence_wrapper->Release()) { // the wrapper is still referenced, so we have to detach from the // contained element (and make a private copy) presence_wrapper->detach(); } } /* unsigned long connectEvtPresence (in jabISessionEvtPresence slot); */ NS_IMETHODIMP Jabber_Session_Wrapper::ConnectEvtPresence(jabISessionEvtPresence *slot, PRUint32 *_retval) { CONNECT_WRAPPED_EVENT(Presence, session_.evtPresence, slot); return NS_OK; } /* void disconnectEvtPresence (in unsigned long id); */ NS_IMETHODIMP Jabber_Session_Wrapper::DisconnectEvtPresence(PRUint32 id) { return NS_ERROR_NOT_IMPLEMENTED; } IMPLEMENT_WRAP_EVENT(Jabber_Session_Wrapper, PresenceRequest) void Jabber_Session_Wrapper::WrapEvtPresenceRequest::OnPresenceRequest(const jabberoo::Presence &presence) { Jabber_Presence_Wrapper *presence_wrapper = new Jabber_Presence_Wrapper(&presence); NS_ADDREF(presence_wrapper); slot_->OnPresenceRequest(presence_wrapper); if (presence_wrapper->Release()) { // the wrapper is still referenced, so we have to detach from the // contained element (and make a private copy) presence_wrapper->detach(); } } /* unsigned long connectEvtPresenceRequest (in jabISessionEvtPresenceRequest slot); */ NS_IMETHODIMP Jabber_Session_Wrapper::ConnectEvtPresenceRequest(jabISessionEvtPresenceRequest *slot, PRUint32 *_retval) { CONNECT_WRAPPED_EVENT(PresenceRequest, session_.evtPresenceRequest, slot); return NS_OK; } /* void disconnectEvtPresenceRequest (in unsigned long id); */ NS_IMETHODIMP Jabber_Session_Wrapper::DisconnectEvtPresenceRequest(PRUint32 id) { return NS_ERROR_NOT_IMPLEMENTED; } /* void connect (in string server, in AuthType atype, in string username, in string resource, in string password, in boolean createuser); */ NS_IMETHODIMP Jabber_Session_Wrapper::Connect(const nsACString &server, AuthType atype, const nsACString &username, const nsACString &resource, const nsACString &password, PRBool createuser) { session_.connect(PromiseFlatCString(server).get(), jabberoo::Session::AuthType(atype), PromiseFlatCString(username).get(), PromiseFlatCString(resource).get(), PromiseFlatCString(password).get(), bool(createuser)); return NS_OK; } /* boolean disconnect (); */ NS_IMETHODIMP Jabber_Session_Wrapper::Disconnect(PRBool *_retval) { *_retval = session_.disconnect(); return NS_OK; } /* string getNextID (); */ NS_IMETHODIMP Jabber_Session_Wrapper::GetNextID(nsACString &_retval) { const std::string &id = session_.getNextID(); _retval.Assign(id.data(), id.length()); return NS_OK; } /* jabIMessage createMessage (in string jid, in string body, in MessageType mtype); */ NS_IMETHODIMP Jabber_Session_Wrapper::CreateMessage(const nsACString &jid, const nsACString &body, MessageType mtype, jabIMessage **_retval) { *_retval = new Jabber_Message_Wrapper(PromiseFlatCString(jid).get(), PromiseFlatCString(body).get(), mtype); NS_ADDREF(*_retval); return NS_OK; } /* jabIInfoQuery createInfoQuery (in string jid, in InfoQueryType iqtype); */ NS_IMETHODIMP Jabber_Session_Wrapper::CreateInfoQuery(const nsACString &jid, InfoQueryType iqtype, jabIInfoQuery **_retval) { *_retval = new Jabber_InfoQuery_Wrapper(PromiseFlatCString(jid).get(), iqtype); NS_ADDREF(*_retval); return NS_OK; } /* jabIPresence createPresence (in string jid, in PresenceType ptype, in ShowType stype, in string status, in long priority); */ NS_IMETHODIMP Jabber_Session_Wrapper::CreatePresence(const nsACString &jid, PresenceType ptype, ShowType stype, const nsACString &status, PRInt32 priority, jabIPresence **_retval) { *_retval = new Jabber_Presence_Wrapper(PromiseFlatCString(jid).get(), ptype, stype, PromiseFlatCString(status).get(), priority); NS_ADDREF(*_retval); return NS_OK; } /* jabIPresence createPresenceRequest (in string jid, in PresenceType ptype); */ NS_IMETHODIMP Jabber_Session_Wrapper::CreatePresenceRequest(const nsACString &jid, PresenceType ptype, jabIPresence **_retval) { *_retval = new Jabber_Presence_Wrapper(PromiseFlatCString(jid).get(), ptype); NS_ADDREF(*_retval); return NS_OK; } /* jabIPresence createMyPresence (in PresenceType ptype, in ShowType stype, in string status, in long priority); */ NS_IMETHODIMP Jabber_Session_Wrapper::CreateMyPresence(PresenceType ptype, ShowType stype, const nsACString &status, PRInt32 priority, jabIPresence **_retval) { *_retval = new Jabber_Presence_Wrapper("", ptype, stype, PromiseFlatCString(status).get(), priority); NS_ADDREF(*_retval); return NS_OK; } /* void sendPacket (in jabIConstPacket packet); */ NS_IMETHODIMP Jabber_Session_Wrapper::SendPacket(jabIConstPacket *packet) { nsCString xml; nsresult rc = packet->ToXML(xml); if (NS_SUCCEEDED(rc)) { session_ << PromiseFlatCString(xml).get(); } return rc; }