From ce469cbad1be7074adbcb346c4aa1acdbfd17a37 Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Wed, 9 Dec 2020 14:11:22 +0900 Subject: Renaming of files in preparation for code style tools. Signed-off-by: Michele Calgaro (cherry picked from commit a06e2c2f225d76b67b0058a9880222f75d5495c3) --- .../libjingle/talk/examples/call/Makefile.am | 4 +- .../jingle/libjingle/talk/examples/call/call.pro | 18 +- .../libjingle/talk/examples/call/call_main.cc | 62 ---- .../libjingle/talk/examples/call/call_main.cpp | 62 ++++ .../libjingle/talk/examples/call/callclient.cc | 390 --------------------- .../libjingle/talk/examples/call/callclient.cpp | 390 +++++++++++++++++++++ .../jingle/libjingle/talk/examples/call/console.cc | 196 ----------- .../libjingle/talk/examples/call/console.cpp | 196 +++++++++++ .../talk/examples/call/presenceouttask.cc | 148 -------- .../talk/examples/call/presenceouttask.cpp | 148 ++++++++ .../talk/examples/call/presencepushtask.cc | 172 --------- .../talk/examples/call/presencepushtask.cpp | 172 +++++++++ .../libjingle/talk/examples/login/Makefile.am | 10 +- .../libjingle/talk/examples/login/login_main.cc | 63 ---- .../libjingle/talk/examples/login/login_main.cpp | 63 ++++ .../libjingle/talk/examples/login/xmppauth.cc | 93 ----- .../libjingle/talk/examples/login/xmppauth.cpp | 93 +++++ .../libjingle/talk/examples/login/xmpppump.cc | 73 ---- .../libjingle/talk/examples/login/xmpppump.cpp | 73 ++++ .../libjingle/talk/examples/login/xmppsocket.cc | 144 -------- .../libjingle/talk/examples/login/xmppsocket.cpp | 144 ++++++++ .../libjingle/talk/examples/login/xmppthread.cc | 80 ----- .../libjingle/talk/examples/login/xmppthread.cpp | 80 +++++ 23 files changed, 1437 insertions(+), 1437 deletions(-) delete mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/call/call_main.cc create mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/call/call_main.cpp delete mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/call/callclient.cc create mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/call/callclient.cpp delete mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/call/console.cc create mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/call/console.cpp delete mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presenceouttask.cc create mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presenceouttask.cpp delete mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presencepushtask.cc create mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presencepushtask.cpp delete mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/login/login_main.cc create mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/login/login_main.cpp delete mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppauth.cc create mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppauth.cpp delete mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmpppump.cc create mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmpppump.cpp delete mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppsocket.cc create mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppsocket.cpp delete mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppthread.cc create mode 100644 kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppthread.cpp (limited to 'kopete/protocols/jabber/jingle/libjingle/talk/examples') diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/Makefile.am b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/Makefile.am index 64e09526..9464e252 100644 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/Makefile.am +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/Makefile.am @@ -1,6 +1,6 @@ bin_PROGRAMS = call call_CXXFLAGS = $(AM_CXXFLAGS) -call_SOURCES = call_main.cc callclient.cc console.cc presencepushtask.cc presenceouttask.cc +call_SOURCES = call_main.cpp callclient.cpp console.cpp presencepushtask.cpp presenceouttask.cpp noinst_HEADERS = callclient.h console.h presenceouttask.h presencepushtask.h status.h call_LDADD = \ $(srcdir)/../../../talk/examples/login/libcricketexampleslogin.la \ @@ -14,4 +14,4 @@ call_LDADD = \ $(srcdir)/../../../talk/third_party/ortp/libortp.la \ $(EXPAT_LIBS) -lpthread $(ILBC_LIBS) $(SPEEX_LIBS) $(GLIB_LIBS) -lasound AM_CPPFLAGS = -DPOSIX -DEFAULT_INCLUDES = -I$(srcdir)/../../.. \ No newline at end of file +DEFAULT_INCLUDES = -I$(srcdir)/../../.. diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/call.pro b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/call.pro index ccf0638b..ee1538e0 100644 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/call.pro +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/call.pro @@ -6,14 +6,14 @@ include(../../../../../conf.pri) # Input SOURCES += \ - call_main.cc \ - callclient.cc \ - console.cc \ - presenceouttask.cc \ - presencepushtask.cc \ - ../login/xmppauth.cc \ - ../login/xmpppump.cc \ - ../login/xmppsocket.cc \ - ../login/xmppthread.cc + call_main.cpp \ + callclient.cpp \ + console.cpp \ + presenceouttask.cpp \ + presencepushtask.cpp \ + ../login/xmppauth.cpp \ + ../login/xmpppump.cpp \ + ../login/xmppsocket.cpp \ + ../login/xmppthread.cpp LIBS += ../../../liblibjingle.a diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/call_main.cc b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/call_main.cc deleted file mode 100644 index 661f2849..00000000 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/call_main.cc +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Jingle call example - * Copyright 2004--2005, Google Inc. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "talk/xmpp/xmppclientsettings.h" -#include "talk/examples/login/xmppthread.h" -#include "talk/examples/login/xmppauth.h" -#include "talk/examples/call/callclient.h" -#include "talk/examples/call/console.h" - -void GetString(const char* desc, char* out) { - printf("%s: ", desc); - fflush(stdout); - scanf("%s", out); -} - -int main(int argc, char **argv) { - // TODO: Make this into a console task - char username[256], auth_cookie[256]; - GetString("Username", username); - GetString("Auth Cookie", auth_cookie); - - printf("Logging in as %s@gmail.com\n", username); - - // We will run the console and the XMPP client on the main thread. The - // CallClient maintains a separate worker thread for voice. - - cricket::PhysicalSocketServer ss; - cricket::Thread main_thread(&ss); - cricket::ThreadManager::SetCurrent(&main_thread); - - InitConsole(&ss); - XmppPump pump; - CallClient client(pump.client()); - - buzz::XmppClientSettings xcs; - xcs.set_user(username); - xcs.set_host("gmail.com"); - xcs.set_use_tls(false); - xcs.set_auth_cookie(auth_cookie); - xcs.set_server(cricket::SocketAddress("talk.google.com", 5222)); - pump.DoLogin(xcs, new XmppSocket(false), new XmppAuth()); - - main_thread.Loop(); - - return 0; -} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/call_main.cpp b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/call_main.cpp new file mode 100644 index 00000000..661f2849 --- /dev/null +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/call_main.cpp @@ -0,0 +1,62 @@ +/* + * Jingle call example + * Copyright 2004--2005, Google Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "talk/xmpp/xmppclientsettings.h" +#include "talk/examples/login/xmppthread.h" +#include "talk/examples/login/xmppauth.h" +#include "talk/examples/call/callclient.h" +#include "talk/examples/call/console.h" + +void GetString(const char* desc, char* out) { + printf("%s: ", desc); + fflush(stdout); + scanf("%s", out); +} + +int main(int argc, char **argv) { + // TODO: Make this into a console task + char username[256], auth_cookie[256]; + GetString("Username", username); + GetString("Auth Cookie", auth_cookie); + + printf("Logging in as %s@gmail.com\n", username); + + // We will run the console and the XMPP client on the main thread. The + // CallClient maintains a separate worker thread for voice. + + cricket::PhysicalSocketServer ss; + cricket::Thread main_thread(&ss); + cricket::ThreadManager::SetCurrent(&main_thread); + + InitConsole(&ss); + XmppPump pump; + CallClient client(pump.client()); + + buzz::XmppClientSettings xcs; + xcs.set_user(username); + xcs.set_host("gmail.com"); + xcs.set_use_tls(false); + xcs.set_auth_cookie(auth_cookie); + xcs.set_server(cricket::SocketAddress("talk.google.com", 5222)); + pump.DoLogin(xcs, new XmppSocket(false), new XmppAuth()); + + main_thread.Loop(); + + return 0; +} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/callclient.cc b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/callclient.cc deleted file mode 100644 index 6d818932..00000000 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/callclient.cc +++ /dev/null @@ -1,390 +0,0 @@ -/* - * Jingle call example - * Copyright 2004--2005, Google Inc. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include -#include - -#include "talk/xmpp/constants.h" -#include "talk/base/thread.h" -#include "talk/base/network.h" -#include "talk/base/socketaddress.h" -#include "talk/p2p/base/sessionmanager.h" -#include "talk/p2p/base/helpers.h" -#include "talk/p2p/client/basicportallocator.h" -#include "talk/session/receiver.h" -#include "talk/session/sessionsendtask.h" -#include "talk/session/phone/phonesessionclient.h" -#include "talk/examples/call/callclient.h" -#include "talk/examples/call/console.h" -#include "talk/examples/call/presencepushtask.h" -#include "talk/examples/call/presenceouttask.h" - -namespace { - -const char* CALL_COMMANDS = -"Available commands:\n" -"\n" -" hangup Ends the call.\n" -" mute Stops sending voice.\n" -" unmute Re-starts sending voice.\n" -""; - -class CallTask: public ConsoleTask, public sigslot::has_slots<> { -public: - CallTask(CallClient* call_client, const buzz::Jid& jid, cricket::Call* call) - : call_client_(call_client), jid_(jid), call_(call) { - } - - virtual ~CallTask() {} - - virtual void Start() { - call_client_->phone_client()->SignalCallDestroy.connect( - this, &CallTask::OnCallDestroy); - if (!call_) { - call_ = call_client_->phone_client()->CreateCall(); - call_->SignalSessionState.connect(this, &CallTask::OnSessionState); - session_ = call_->InitiateSession(jid_); - } - call_client_->phone_client()->SetFocus(call_); - } - - virtual std::string GetPrompt() { return jid_.node(); } - - virtual void ProcessLine(const std::string& line) { - std::vector words; - ParseLine(line, &words); - - if ((words.size() == 1) && (words[0] == "hangup")) { - call_->Terminate(); - SignalDone(this); - } else if ((words.size() == 1) && (words[0] == "mute")) { - call_->Mute(true); - } else if ((words.size() == 1) && (words[0] == "unmute")) { - call_->Mute(false); - } else { - console()->Print(CALL_COMMANDS); - } - } - -private: - CallClient* call_client_; - buzz::Jid jid_; - cricket::Call* call_; - cricket::Session* session_; - - void OnCallDestroy(cricket::Call* call) { - if (call == call_) { - console()->Print("call destroyed"); - SignalDone(this); - } - } - - void OnSessionState(cricket::Call* call, - cricket::Session* session, - cricket::Session::State state) { - if (state == cricket::Session::STATE_SENTINITIATE) { - console()->Print("calling..."); - } else if (state == cricket::Session::STATE_RECEIVEDACCEPT) { - console()->Print("call answered"); - } else if (state == cricket::Session::STATE_RECEIVEDREJECT) { - console()->Print("call not answered"); - SignalDone(this); - } else if (state == cricket::Session::STATE_INPROGRESS) { - console()->Print("call in progress"); - } else if (state == cricket::Session::STATE_RECEIVEDTERMINATE) { - console()->Print("other side hung up"); - SignalDone(this); - } - } -}; - -const char* RECEIVE_COMMANDS = -"Available commands:\n" -"\n" -" accept Accepts the incoming call and switches to it.\n" -" reject Rejects the incoming call and stays with the current call.\n" -""; - -class ReceiveTask: public ConsoleTask { -public: - ReceiveTask(CallClient* call_client, - const buzz::Jid& jid, - cricket::Call* call) - : call_client_(call_client), jid_(jid), call_(call) { - } - - virtual std::string GetPrompt() { return jid_.node(); } - - virtual void ProcessLine(const std::string& line) { - std::vector words; - ParseLine(line, &words); - - if ((words.size() == 1) && (words[0] == "accept")) { - assert(call_->sessions().size() == 1); - call_->AcceptSession(call_->sessions()[0]); - Console()->Push(new CallTask(call_client_, jid_, call_)); - SignalDone(this); - } else if ((words.size() == 1) && (words[0] == "reject")) { - call_->RejectSession(call_->sessions()[0]); - SignalDone(this); - } else { - console()->Print(RECEIVE_COMMANDS); - } - } - -private: - CallClient* call_client_; - buzz::Jid jid_; - cricket::Call* call_; -}; - -const char* CONSOLE_COMMANDS = -"Available commands:\n" -"\n" -" roster Prints the online friends from your roster.\n" -" call Initiates a call to the friend with the given name.\n" -" quit Quits the application.\n" -""; - -class CallConsoleTask: public ConsoleTask { -public: - CallConsoleTask(CallClient* call_client) : call_client_(call_client) {} - virtual ~CallConsoleTask() {} - - virtual std::string GetPrompt() { return "console"; } - - virtual void ProcessLine(const std::string& line) { - std::vector words; - ParseLine(line, &words); - - if ((words.size() == 1) && (words[0] == "quit")) { - SignalDone(this); - } else if ((words.size() == 1) && (words[0] == "roster")) { - call_client_->PrintRoster(); - } else if ((words.size() == 2) && (words[0] == "call")) { - call_client_->MakeCallTo(words[1]); - } else { - console()->Print(CONSOLE_COMMANDS); - } - } - -private: - CallClient* call_client_; -}; - -const char* DescribeStatus(buzz::Status::Show show, const std::string& desc) { - switch (show) { - case buzz::Status::SHOW_XA: return desc.c_str(); - case buzz::Status::SHOW_ONLINE: return "online"; - case buzz::Status::SHOW_AWAY: return "away"; - case buzz::Status::SHOW_DND: return "do not disturb"; - case buzz::Status::SHOW_CHAT: return "ready to chat"; - delault: return "offline"; - } -} - -} // namespace - -CallClient::CallClient(buzz::XmppClient* xmpp_client) - : xmpp_client_(xmpp_client), roster_(new RosterMap) { - xmpp_client_->SignalStateChange.connect(this, &CallClient::OnStateChange); - Console()->Push(new CallConsoleTask(this)); -} - -CallClient::~CallClient() { - delete roster_; -} - -const std::string CallClient::strerror(buzz::XmppEngine::Error err) { - switch (err) { - case buzz::XmppEngine::ERROR_NONE: - return ""; - case buzz::XmppEngine::ERROR_XML: - return "Malformed XML or encoding error"; - case buzz::XmppEngine::ERROR_STREAM: - return "XMPP stream error"; - case buzz::XmppEngine::ERROR_VERSION: - return "XMPP version error"; - case buzz::XmppEngine::ERROR_UNAUTHORIZED: - return "User is not authorized (Confirm your GX cookie at mail.google.com)"; - case buzz::XmppEngine::ERROR_TLS: - return "TLS could not be negotiated"; - case buzz::XmppEngine::ERROR_AUTH: - return "Authentication could not be negotiated"; - case buzz::XmppEngine::ERROR_BIND: - return "Resource or session binding could not be negotiated"; - case buzz::XmppEngine::ERROR_CONNECTION_CLOSED: - return "Connection closed by output handler."; - case buzz::XmppEngine::ERROR_DOCUMENT_CLOSED: - return "Closed by "; - case buzz::XmppEngine::ERROR_SOCKET: - return "Socket error"; - } -} - -void CallClient::OnStateChange(buzz::XmppEngine::State state) { - switch (state) { - case buzz::XmppEngine::STATE_START: - Console()->Print("connecting..."); - break; - - case buzz::XmppEngine::STATE_OPENING: - Console()->Print("logging in..."); - break; - - case buzz::XmppEngine::STATE_OPEN: - Console()->Print("logged in..."); - InitPhone(); - InitPresence(); - break; - - case buzz::XmppEngine::STATE_CLOSED: - buzz::XmppEngine::Error error = xmpp_client_->GetError(); - Console()->Print("logged out..." + strerror(error)); - exit(0); - } -} - -void CallClient::InitPhone() { - std::string client_unique = xmpp_client_->jid().Str(); - cricket::InitRandom(client_unique.c_str(), client_unique.size()); - - worker_thread_ = new cricket::Thread(); - - network_manager_ = new cricket::NetworkManager(); - - cricket::SocketAddress *stun_addr = new cricket::SocketAddress("64.233.167.126", 19302); - port_allocator_ = new cricket::BasicPortAllocator(network_manager_, stun_addr, NULL); - - session_manager_ = new cricket::SessionManager( - port_allocator_, worker_thread_); - session_manager_->SignalRequestSignaling.connect( - this, &CallClient::OnRequestSignaling); - session_manager_->OnSignalingReady(); - - phone_client_ = new cricket::PhoneSessionClient( - xmpp_client_->jid(),session_manager_); - phone_client_->SignalCallCreate.connect(this, &CallClient::OnCallCreate); - phone_client_->SignalSendStanza.connect(this, &CallClient::OnSendStanza); - - receiver_ = new cricket::Receiver(xmpp_client_, phone_client_); - receiver_->Start(); - - worker_thread_->Start(); -} - -void CallClient::OnRequestSignaling() { - session_manager_->OnSignalingReady(); -} - -void CallClient::OnCallCreate(cricket::Call* call) { - call->SignalSessionState.connect(this, &CallClient::OnSessionState); -} - -void CallClient::OnSessionState(cricket::Call* call, - cricket::Session* session, - cricket::Session::State state) { - if (state == cricket::Session::STATE_RECEIVEDINITIATE) { - buzz::Jid jid(session->remote_address()); - Console()->Printf("Incoming call from '%s'", jid.Str().c_str()); - Console()->Push(new ReceiveTask(this, jid, call)); - } -} - -void CallClient::OnSendStanza(cricket::SessionClient *client, const buzz::XmlElement* stanza) { - cricket::SessionSendTask* sender = - new cricket::SessionSendTask(xmpp_client_, phone_client_); - sender->Send(stanza); - sender->Start(); -} - -void CallClient::InitPresence() { - presence_push_ = new buzz::PresencePushTask(xmpp_client_); - presence_push_->SignalStatusUpdate.connect( - this, &CallClient::OnStatusUpdate); - presence_push_->Start(); - - buzz::Status my_status; - my_status.set_jid(xmpp_client_->jid()); - my_status.set_available(true); - my_status.set_invisible(false); - my_status.set_show(buzz::Status::SHOW_ONLINE); - my_status.set_priority(0); - my_status.set_know_capabilities(true); - my_status.set_phone_capability(true); - my_status.set_is_google_client(true); - my_status.set_version("1.0.0.66"); - - buzz::PresenceOutTask* presence_out_ = - new buzz::PresenceOutTask(xmpp_client_); - presence_out_->Send(my_status); - presence_out_->Start(); -} - -void CallClient::OnStatusUpdate(const buzz::Status& status) { - RosterItem item; - item.jid = status.jid(); - item.show = status.show(); - item.status = status.status(); - - std::string key = item.jid.Str(); - - if (status.available() && status.phone_capability()) { - Console()->Printf("Adding to roster: %s", key.c_str()); - (*roster_)[key] = item; - } else { - Console()->Printf("Removing from roster: %s", key.c_str()); - RosterMap::iterator iter = roster_->find(key); - if (iter != roster_->end()) - roster_->erase(iter); - } -} - -void CallClient::PrintRoster() { - Console()->Printf("Roster contains %d callable", roster_->size()); - RosterMap::iterator iter = roster_->begin(); - while (iter != roster_->end()) { - Console()->Printf("%s - %s", - iter->second.jid.BareJid().Str().c_str(), - DescribeStatus(iter->second.show, iter->second.status)); - iter++; - } -} - -void CallClient::MakeCallTo(const std::string& name) { - bool found = false; - buzz::Jid found_jid; - - RosterMap::iterator iter = roster_->begin(); - while (iter != roster_->end()) { - if (iter->second.jid.node() == name) { - found = true; - found_jid = iter->second.jid; - break; - } - ++iter; - } - - if (found) { - Console()->Printf("Found online friend '%s'", found_jid.Str().c_str()); - Console()->Push(new CallTask(this, found_jid, NULL)); - } else { - Console()->Printf("Could not find online friend '%s'", name.c_str()); - } -} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/callclient.cpp b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/callclient.cpp new file mode 100644 index 00000000..6d818932 --- /dev/null +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/callclient.cpp @@ -0,0 +1,390 @@ +/* + * Jingle call example + * Copyright 2004--2005, Google Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "talk/xmpp/constants.h" +#include "talk/base/thread.h" +#include "talk/base/network.h" +#include "talk/base/socketaddress.h" +#include "talk/p2p/base/sessionmanager.h" +#include "talk/p2p/base/helpers.h" +#include "talk/p2p/client/basicportallocator.h" +#include "talk/session/receiver.h" +#include "talk/session/sessionsendtask.h" +#include "talk/session/phone/phonesessionclient.h" +#include "talk/examples/call/callclient.h" +#include "talk/examples/call/console.h" +#include "talk/examples/call/presencepushtask.h" +#include "talk/examples/call/presenceouttask.h" + +namespace { + +const char* CALL_COMMANDS = +"Available commands:\n" +"\n" +" hangup Ends the call.\n" +" mute Stops sending voice.\n" +" unmute Re-starts sending voice.\n" +""; + +class CallTask: public ConsoleTask, public sigslot::has_slots<> { +public: + CallTask(CallClient* call_client, const buzz::Jid& jid, cricket::Call* call) + : call_client_(call_client), jid_(jid), call_(call) { + } + + virtual ~CallTask() {} + + virtual void Start() { + call_client_->phone_client()->SignalCallDestroy.connect( + this, &CallTask::OnCallDestroy); + if (!call_) { + call_ = call_client_->phone_client()->CreateCall(); + call_->SignalSessionState.connect(this, &CallTask::OnSessionState); + session_ = call_->InitiateSession(jid_); + } + call_client_->phone_client()->SetFocus(call_); + } + + virtual std::string GetPrompt() { return jid_.node(); } + + virtual void ProcessLine(const std::string& line) { + std::vector words; + ParseLine(line, &words); + + if ((words.size() == 1) && (words[0] == "hangup")) { + call_->Terminate(); + SignalDone(this); + } else if ((words.size() == 1) && (words[0] == "mute")) { + call_->Mute(true); + } else if ((words.size() == 1) && (words[0] == "unmute")) { + call_->Mute(false); + } else { + console()->Print(CALL_COMMANDS); + } + } + +private: + CallClient* call_client_; + buzz::Jid jid_; + cricket::Call* call_; + cricket::Session* session_; + + void OnCallDestroy(cricket::Call* call) { + if (call == call_) { + console()->Print("call destroyed"); + SignalDone(this); + } + } + + void OnSessionState(cricket::Call* call, + cricket::Session* session, + cricket::Session::State state) { + if (state == cricket::Session::STATE_SENTINITIATE) { + console()->Print("calling..."); + } else if (state == cricket::Session::STATE_RECEIVEDACCEPT) { + console()->Print("call answered"); + } else if (state == cricket::Session::STATE_RECEIVEDREJECT) { + console()->Print("call not answered"); + SignalDone(this); + } else if (state == cricket::Session::STATE_INPROGRESS) { + console()->Print("call in progress"); + } else if (state == cricket::Session::STATE_RECEIVEDTERMINATE) { + console()->Print("other side hung up"); + SignalDone(this); + } + } +}; + +const char* RECEIVE_COMMANDS = +"Available commands:\n" +"\n" +" accept Accepts the incoming call and switches to it.\n" +" reject Rejects the incoming call and stays with the current call.\n" +""; + +class ReceiveTask: public ConsoleTask { +public: + ReceiveTask(CallClient* call_client, + const buzz::Jid& jid, + cricket::Call* call) + : call_client_(call_client), jid_(jid), call_(call) { + } + + virtual std::string GetPrompt() { return jid_.node(); } + + virtual void ProcessLine(const std::string& line) { + std::vector words; + ParseLine(line, &words); + + if ((words.size() == 1) && (words[0] == "accept")) { + assert(call_->sessions().size() == 1); + call_->AcceptSession(call_->sessions()[0]); + Console()->Push(new CallTask(call_client_, jid_, call_)); + SignalDone(this); + } else if ((words.size() == 1) && (words[0] == "reject")) { + call_->RejectSession(call_->sessions()[0]); + SignalDone(this); + } else { + console()->Print(RECEIVE_COMMANDS); + } + } + +private: + CallClient* call_client_; + buzz::Jid jid_; + cricket::Call* call_; +}; + +const char* CONSOLE_COMMANDS = +"Available commands:\n" +"\n" +" roster Prints the online friends from your roster.\n" +" call Initiates a call to the friend with the given name.\n" +" quit Quits the application.\n" +""; + +class CallConsoleTask: public ConsoleTask { +public: + CallConsoleTask(CallClient* call_client) : call_client_(call_client) {} + virtual ~CallConsoleTask() {} + + virtual std::string GetPrompt() { return "console"; } + + virtual void ProcessLine(const std::string& line) { + std::vector words; + ParseLine(line, &words); + + if ((words.size() == 1) && (words[0] == "quit")) { + SignalDone(this); + } else if ((words.size() == 1) && (words[0] == "roster")) { + call_client_->PrintRoster(); + } else if ((words.size() == 2) && (words[0] == "call")) { + call_client_->MakeCallTo(words[1]); + } else { + console()->Print(CONSOLE_COMMANDS); + } + } + +private: + CallClient* call_client_; +}; + +const char* DescribeStatus(buzz::Status::Show show, const std::string& desc) { + switch (show) { + case buzz::Status::SHOW_XA: return desc.c_str(); + case buzz::Status::SHOW_ONLINE: return "online"; + case buzz::Status::SHOW_AWAY: return "away"; + case buzz::Status::SHOW_DND: return "do not disturb"; + case buzz::Status::SHOW_CHAT: return "ready to chat"; + delault: return "offline"; + } +} + +} // namespace + +CallClient::CallClient(buzz::XmppClient* xmpp_client) + : xmpp_client_(xmpp_client), roster_(new RosterMap) { + xmpp_client_->SignalStateChange.connect(this, &CallClient::OnStateChange); + Console()->Push(new CallConsoleTask(this)); +} + +CallClient::~CallClient() { + delete roster_; +} + +const std::string CallClient::strerror(buzz::XmppEngine::Error err) { + switch (err) { + case buzz::XmppEngine::ERROR_NONE: + return ""; + case buzz::XmppEngine::ERROR_XML: + return "Malformed XML or encoding error"; + case buzz::XmppEngine::ERROR_STREAM: + return "XMPP stream error"; + case buzz::XmppEngine::ERROR_VERSION: + return "XMPP version error"; + case buzz::XmppEngine::ERROR_UNAUTHORIZED: + return "User is not authorized (Confirm your GX cookie at mail.google.com)"; + case buzz::XmppEngine::ERROR_TLS: + return "TLS could not be negotiated"; + case buzz::XmppEngine::ERROR_AUTH: + return "Authentication could not be negotiated"; + case buzz::XmppEngine::ERROR_BIND: + return "Resource or session binding could not be negotiated"; + case buzz::XmppEngine::ERROR_CONNECTION_CLOSED: + return "Connection closed by output handler."; + case buzz::XmppEngine::ERROR_DOCUMENT_CLOSED: + return "Closed by "; + case buzz::XmppEngine::ERROR_SOCKET: + return "Socket error"; + } +} + +void CallClient::OnStateChange(buzz::XmppEngine::State state) { + switch (state) { + case buzz::XmppEngine::STATE_START: + Console()->Print("connecting..."); + break; + + case buzz::XmppEngine::STATE_OPENING: + Console()->Print("logging in..."); + break; + + case buzz::XmppEngine::STATE_OPEN: + Console()->Print("logged in..."); + InitPhone(); + InitPresence(); + break; + + case buzz::XmppEngine::STATE_CLOSED: + buzz::XmppEngine::Error error = xmpp_client_->GetError(); + Console()->Print("logged out..." + strerror(error)); + exit(0); + } +} + +void CallClient::InitPhone() { + std::string client_unique = xmpp_client_->jid().Str(); + cricket::InitRandom(client_unique.c_str(), client_unique.size()); + + worker_thread_ = new cricket::Thread(); + + network_manager_ = new cricket::NetworkManager(); + + cricket::SocketAddress *stun_addr = new cricket::SocketAddress("64.233.167.126", 19302); + port_allocator_ = new cricket::BasicPortAllocator(network_manager_, stun_addr, NULL); + + session_manager_ = new cricket::SessionManager( + port_allocator_, worker_thread_); + session_manager_->SignalRequestSignaling.connect( + this, &CallClient::OnRequestSignaling); + session_manager_->OnSignalingReady(); + + phone_client_ = new cricket::PhoneSessionClient( + xmpp_client_->jid(),session_manager_); + phone_client_->SignalCallCreate.connect(this, &CallClient::OnCallCreate); + phone_client_->SignalSendStanza.connect(this, &CallClient::OnSendStanza); + + receiver_ = new cricket::Receiver(xmpp_client_, phone_client_); + receiver_->Start(); + + worker_thread_->Start(); +} + +void CallClient::OnRequestSignaling() { + session_manager_->OnSignalingReady(); +} + +void CallClient::OnCallCreate(cricket::Call* call) { + call->SignalSessionState.connect(this, &CallClient::OnSessionState); +} + +void CallClient::OnSessionState(cricket::Call* call, + cricket::Session* session, + cricket::Session::State state) { + if (state == cricket::Session::STATE_RECEIVEDINITIATE) { + buzz::Jid jid(session->remote_address()); + Console()->Printf("Incoming call from '%s'", jid.Str().c_str()); + Console()->Push(new ReceiveTask(this, jid, call)); + } +} + +void CallClient::OnSendStanza(cricket::SessionClient *client, const buzz::XmlElement* stanza) { + cricket::SessionSendTask* sender = + new cricket::SessionSendTask(xmpp_client_, phone_client_); + sender->Send(stanza); + sender->Start(); +} + +void CallClient::InitPresence() { + presence_push_ = new buzz::PresencePushTask(xmpp_client_); + presence_push_->SignalStatusUpdate.connect( + this, &CallClient::OnStatusUpdate); + presence_push_->Start(); + + buzz::Status my_status; + my_status.set_jid(xmpp_client_->jid()); + my_status.set_available(true); + my_status.set_invisible(false); + my_status.set_show(buzz::Status::SHOW_ONLINE); + my_status.set_priority(0); + my_status.set_know_capabilities(true); + my_status.set_phone_capability(true); + my_status.set_is_google_client(true); + my_status.set_version("1.0.0.66"); + + buzz::PresenceOutTask* presence_out_ = + new buzz::PresenceOutTask(xmpp_client_); + presence_out_->Send(my_status); + presence_out_->Start(); +} + +void CallClient::OnStatusUpdate(const buzz::Status& status) { + RosterItem item; + item.jid = status.jid(); + item.show = status.show(); + item.status = status.status(); + + std::string key = item.jid.Str(); + + if (status.available() && status.phone_capability()) { + Console()->Printf("Adding to roster: %s", key.c_str()); + (*roster_)[key] = item; + } else { + Console()->Printf("Removing from roster: %s", key.c_str()); + RosterMap::iterator iter = roster_->find(key); + if (iter != roster_->end()) + roster_->erase(iter); + } +} + +void CallClient::PrintRoster() { + Console()->Printf("Roster contains %d callable", roster_->size()); + RosterMap::iterator iter = roster_->begin(); + while (iter != roster_->end()) { + Console()->Printf("%s - %s", + iter->second.jid.BareJid().Str().c_str(), + DescribeStatus(iter->second.show, iter->second.status)); + iter++; + } +} + +void CallClient::MakeCallTo(const std::string& name) { + bool found = false; + buzz::Jid found_jid; + + RosterMap::iterator iter = roster_->begin(); + while (iter != roster_->end()) { + if (iter->second.jid.node() == name) { + found = true; + found_jid = iter->second.jid; + break; + } + ++iter; + } + + if (found) { + Console()->Printf("Found online friend '%s'", found_jid.Str().c_str()); + Console()->Push(new CallTask(this, found_jid, NULL)); + } else { + Console()->Printf("Could not find online friend '%s'", name.c_str()); + } +} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/console.cc b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/console.cc deleted file mode 100644 index 96f5dc96..00000000 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/console.cc +++ /dev/null @@ -1,196 +0,0 @@ -/* - * Jingle call example - * Copyright 2004--2005, Google Inc. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -extern "C" { -#include -#include -#include -#include -#include -} -#include -#include -#include -#include - -#include "talk/examples/call/console.h" - -namespace { - -void PError(const char* desc) { - perror(desc); - exit(1); -} - -CConsole* gConsole = NULL; - -const uint32 MSG_UPDATE = 1; - -} // namespace - -void InitConsole(cricket::PhysicalSocketServer* ss) { - assert(gConsole == NULL); - assert(ss); - gConsole = new CConsole(ss); -} - -CConsole* Console() { - assert(gConsole); - return gConsole; -} - -CConsole::CConsole(cricket::PhysicalSocketServer* ss) - : prompting_(false), prompt_dirty_(false) { - stdin_ = ss->CreateFile(0); - stdin_->SignalReadEvent.connect(this, &CConsole::OnReadInput); - - tasks_ = new std::vector; -} - -CConsole::~CConsole() { - delete stdin_; - delete tasks_; -} - -void CConsole::Push(ConsoleTask* task) { - task->set_console(this); - task->SignalDone.connect(this, &CConsole::OnTaskDone); - tasks_->push_back(task); - task->Start(); - UpdatePrompt(); -} - -void CConsole::Remove(ConsoleTask* task) { - int index = -1; - for (size_t i = 0; i < tasks_->size(); ++i) { - if ((*tasks_)[i] == task) - index = i; - } - - assert(index >= 0); - tasks_->erase(tasks_->begin() + index); - if (static_cast(tasks_->size()) == index) - UpdatePrompt(); - - delete task; - - if (tasks_->size() == 0) - exit(0); -} - -void CConsole::Print(const char* str) { - if (prompting_) - printf("\r"); - printf("%s\n", str); - prompting_ = false; - UpdatePrompt(); -} - -void CConsole::Print(const std::string& str) { - Print(str.c_str()); -} - -void CConsole::Printf(const char* format, ...) { - va_list ap; - va_start(ap, format); - - char buf[4096]; - int size = vsnprintf(buf, sizeof(buf), format, ap); - assert(size >= 0); - assert(size < static_cast(sizeof(buf))); - buf[size] = '\0'; - Print(buf); - - va_end(ap); -} - -void CConsole::OnTaskDone(ConsoleTask* task) { - Remove(task); -} - -void CConsole::OnReadInput(cricket::AsyncFile* file) { - assert(file == stdin_); - - char buf[4096]; - int size = read(0, buf, sizeof(buf)); - if (size < 0) - PError("read"); - - prompting_ = (buf[size-1] != '\n'); - - int start = 0; - for (int i = 0; i < size; ++i) { - if (buf[i] == '\n') { - std::string line = input_; - line.append(buf + start, i + 1 - start); - input_.clear(); - - assert(tasks_->size() > 0); - tasks_->back()->ProcessLine(line); - - start = i + 1; - } - } - - input_.append(buf + start, size - start); -} - -void CConsole::OnMessage(cricket::Message* pmsg) { - assert(pmsg->message_id == MSG_UPDATE); - assert(tasks_->size() > 0); - if (prompting_) - printf("\n"); - printf("%s: %s", tasks_->back()->GetPrompt().c_str(), input_.c_str()); - fflush(stdout); - prompting_ = true; - prompt_dirty_ = false; -} - -void CConsole::UpdatePrompt() { - if (!prompt_dirty_) { - prompt_dirty_ = true; - cricket::Thread::Current()->Post(this, MSG_UPDATE); - } -} - -void ConsoleTask::ParseLine(const std::string& line, - std::vector* words) { - assert(line.size() > 0); - assert(line[line.size() - 1] == '\n'); - - int start = -1; - int state = 0; - for (int index = 0; index <= static_cast(line.size()); ++index) { - if (state == 0) { - if (!isspace(line[index])) { - start = index; - state = 1; - } - } else { - assert(state == 1); - assert(start >= 0); - if (isspace(line[index])) { - std::string word(line, start, index - start); - words->push_back(word); - start = -1; - state = 0; - } - } - } -} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/console.cpp b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/console.cpp new file mode 100644 index 00000000..96f5dc96 --- /dev/null +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/console.cpp @@ -0,0 +1,196 @@ +/* + * Jingle call example + * Copyright 2004--2005, Google Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +extern "C" { +#include +#include +#include +#include +#include +} +#include +#include +#include +#include + +#include "talk/examples/call/console.h" + +namespace { + +void PError(const char* desc) { + perror(desc); + exit(1); +} + +CConsole* gConsole = NULL; + +const uint32 MSG_UPDATE = 1; + +} // namespace + +void InitConsole(cricket::PhysicalSocketServer* ss) { + assert(gConsole == NULL); + assert(ss); + gConsole = new CConsole(ss); +} + +CConsole* Console() { + assert(gConsole); + return gConsole; +} + +CConsole::CConsole(cricket::PhysicalSocketServer* ss) + : prompting_(false), prompt_dirty_(false) { + stdin_ = ss->CreateFile(0); + stdin_->SignalReadEvent.connect(this, &CConsole::OnReadInput); + + tasks_ = new std::vector; +} + +CConsole::~CConsole() { + delete stdin_; + delete tasks_; +} + +void CConsole::Push(ConsoleTask* task) { + task->set_console(this); + task->SignalDone.connect(this, &CConsole::OnTaskDone); + tasks_->push_back(task); + task->Start(); + UpdatePrompt(); +} + +void CConsole::Remove(ConsoleTask* task) { + int index = -1; + for (size_t i = 0; i < tasks_->size(); ++i) { + if ((*tasks_)[i] == task) + index = i; + } + + assert(index >= 0); + tasks_->erase(tasks_->begin() + index); + if (static_cast(tasks_->size()) == index) + UpdatePrompt(); + + delete task; + + if (tasks_->size() == 0) + exit(0); +} + +void CConsole::Print(const char* str) { + if (prompting_) + printf("\r"); + printf("%s\n", str); + prompting_ = false; + UpdatePrompt(); +} + +void CConsole::Print(const std::string& str) { + Print(str.c_str()); +} + +void CConsole::Printf(const char* format, ...) { + va_list ap; + va_start(ap, format); + + char buf[4096]; + int size = vsnprintf(buf, sizeof(buf), format, ap); + assert(size >= 0); + assert(size < static_cast(sizeof(buf))); + buf[size] = '\0'; + Print(buf); + + va_end(ap); +} + +void CConsole::OnTaskDone(ConsoleTask* task) { + Remove(task); +} + +void CConsole::OnReadInput(cricket::AsyncFile* file) { + assert(file == stdin_); + + char buf[4096]; + int size = read(0, buf, sizeof(buf)); + if (size < 0) + PError("read"); + + prompting_ = (buf[size-1] != '\n'); + + int start = 0; + for (int i = 0; i < size; ++i) { + if (buf[i] == '\n') { + std::string line = input_; + line.append(buf + start, i + 1 - start); + input_.clear(); + + assert(tasks_->size() > 0); + tasks_->back()->ProcessLine(line); + + start = i + 1; + } + } + + input_.append(buf + start, size - start); +} + +void CConsole::OnMessage(cricket::Message* pmsg) { + assert(pmsg->message_id == MSG_UPDATE); + assert(tasks_->size() > 0); + if (prompting_) + printf("\n"); + printf("%s: %s", tasks_->back()->GetPrompt().c_str(), input_.c_str()); + fflush(stdout); + prompting_ = true; + prompt_dirty_ = false; +} + +void CConsole::UpdatePrompt() { + if (!prompt_dirty_) { + prompt_dirty_ = true; + cricket::Thread::Current()->Post(this, MSG_UPDATE); + } +} + +void ConsoleTask::ParseLine(const std::string& line, + std::vector* words) { + assert(line.size() > 0); + assert(line[line.size() - 1] == '\n'); + + int start = -1; + int state = 0; + for (int index = 0; index <= static_cast(line.size()); ++index) { + if (state == 0) { + if (!isspace(line[index])) { + start = index; + state = 1; + } + } else { + assert(state == 1); + assert(start >= 0); + if (isspace(line[index])) { + std::string word(line, start, index - start); + words->push_back(word); + start = -1; + state = 0; + } + } + } +} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presenceouttask.cc b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presenceouttask.cc deleted file mode 100644 index a15a6c83..00000000 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presenceouttask.cc +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Jingle call example - * Copyright 2004--2005, Google Inc. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include -#include -#include "talk/xmpp/constants.h" -#include "talk/xmpp/xmppclient.h" -#include "talk/examples/call/presenceouttask.h" - -namespace buzz { - -// string helper functions ----------------------------------------------------- -template static -bool FromString(const std::string& s, - T * t) { - std::istringstream iss(s); - return !(iss>>*t).fail(); -} - -template static -bool ToString(const T &t, - std::string* s) { - std::ostringstream oss; - oss << t; - *s = oss.str(); - return !oss.fail(); -} - -XmppReturnStatus -PresenceOutTask::Send(const Status & s) { - if (GetState() != STATE_INIT) - return XMPP_RETURN_BADSTATE; - - stanza_.reset(TranslateStatus(s)); - return XMPP_RETURN_OK; -} - -XmppReturnStatus -PresenceOutTask::SendDirected(const Jid & j, const Status & s) { - if (GetState() != STATE_INIT) - return XMPP_RETURN_BADSTATE; - - XmlElement * presence = TranslateStatus(s); - presence->AddAttr(TQN_TO, j.Str()); - stanza_.reset(presence); - return XMPP_RETURN_OK; -} - -XmppReturnStatus PresenceOutTask::SendProbe(const Jid & jid) { - if (GetState() != STATE_INIT) - return XMPP_RETURN_BADSTATE; - - XmlElement * presence = new XmlElement(TQN_PRESENCE); - presence->AddAttr(TQN_TO, jid.Str()); - presence->AddAttr(TQN_TYPE, "probe"); - - stanza_.reset(presence); - return XMPP_RETURN_OK; -} - -int -PresenceOutTask::ProcessStart() { - if (SendStanza(stanza_.get()) != XMPP_RETURN_OK) - return STATE_ERROR; - return STATE_DONE; -} - -XmlElement * -PresenceOutTask::TranslateStatus(const Status & s) { - XmlElement * result = new XmlElement(TQN_PRESENCE); - if (!s.available()) { - result->AddAttr(TQN_TYPE, STR_UNAVAILABLE); - } - else { - if (s.invisible()) { - result->AddAttr(TQN_TYPE, STR_INVISIBLE); - } - - if (s.show() != Status::SHOW_ONLINE && s.show() != Status::SHOW_OFFLINE) { - result->AddElement(new XmlElement(TQN_SHOW)); - switch (s.show()) { - default: - result->AddText(STR_SHOW_AWAY, 1); - break; - case Status::SHOW_XA: - result->AddText(STR_SHOW_XA, 1); - break; - case Status::SHOW_DND: - result->AddText(STR_SHOW_DND, 1); - break; - case Status::SHOW_CHAT: - result->AddText(STR_SHOW_CHAT, 1); - break; - } - } - - result->AddElement(new XmlElement(TQN_STATUS)); - result->AddText(s.status(), 1); - - std::string pri; - ToString(s.priority(), &pri); - - result->AddElement(new XmlElement(TQN_PRIORITY)); - result->AddText(pri, 1); - - if (s.know_capabilities() && s.is_google_client()) { - result->AddElement(new XmlElement(TQN_CAPS_C, true)); - result->AddAttr(TQN_NODE, GOOGLE_CLIENT_NODE, 1); - result->AddAttr(TQN_VER, s.version(), 1); - result->AddAttr(TQN_EXT, s.phone_capability() ? "voice-v1" : "", 1); - } - - // Put the delay mark on the presence according to JEP-0091 - { - result->AddElement(new XmlElement(kQnDelayX, true)); - - // This here is why we *love* the C runtime - time_t current_time_seconds; - time(¤t_time_seconds); - struct tm* current_time = gmtime(¤t_time_seconds); - char output[256]; - strftime(output, ARRAY_SIZE(output), "%Y%m%dT%H:%M:%S", current_time); - result->AddAttr(kQnStamp, output, 1); - } - - } - - return result; -} - - -} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presenceouttask.cpp b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presenceouttask.cpp new file mode 100644 index 00000000..a15a6c83 --- /dev/null +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presenceouttask.cpp @@ -0,0 +1,148 @@ +/* + * Jingle call example + * Copyright 2004--2005, Google Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include "talk/xmpp/constants.h" +#include "talk/xmpp/xmppclient.h" +#include "talk/examples/call/presenceouttask.h" + +namespace buzz { + +// string helper functions ----------------------------------------------------- +template static +bool FromString(const std::string& s, + T * t) { + std::istringstream iss(s); + return !(iss>>*t).fail(); +} + +template static +bool ToString(const T &t, + std::string* s) { + std::ostringstream oss; + oss << t; + *s = oss.str(); + return !oss.fail(); +} + +XmppReturnStatus +PresenceOutTask::Send(const Status & s) { + if (GetState() != STATE_INIT) + return XMPP_RETURN_BADSTATE; + + stanza_.reset(TranslateStatus(s)); + return XMPP_RETURN_OK; +} + +XmppReturnStatus +PresenceOutTask::SendDirected(const Jid & j, const Status & s) { + if (GetState() != STATE_INIT) + return XMPP_RETURN_BADSTATE; + + XmlElement * presence = TranslateStatus(s); + presence->AddAttr(TQN_TO, j.Str()); + stanza_.reset(presence); + return XMPP_RETURN_OK; +} + +XmppReturnStatus PresenceOutTask::SendProbe(const Jid & jid) { + if (GetState() != STATE_INIT) + return XMPP_RETURN_BADSTATE; + + XmlElement * presence = new XmlElement(TQN_PRESENCE); + presence->AddAttr(TQN_TO, jid.Str()); + presence->AddAttr(TQN_TYPE, "probe"); + + stanza_.reset(presence); + return XMPP_RETURN_OK; +} + +int +PresenceOutTask::ProcessStart() { + if (SendStanza(stanza_.get()) != XMPP_RETURN_OK) + return STATE_ERROR; + return STATE_DONE; +} + +XmlElement * +PresenceOutTask::TranslateStatus(const Status & s) { + XmlElement * result = new XmlElement(TQN_PRESENCE); + if (!s.available()) { + result->AddAttr(TQN_TYPE, STR_UNAVAILABLE); + } + else { + if (s.invisible()) { + result->AddAttr(TQN_TYPE, STR_INVISIBLE); + } + + if (s.show() != Status::SHOW_ONLINE && s.show() != Status::SHOW_OFFLINE) { + result->AddElement(new XmlElement(TQN_SHOW)); + switch (s.show()) { + default: + result->AddText(STR_SHOW_AWAY, 1); + break; + case Status::SHOW_XA: + result->AddText(STR_SHOW_XA, 1); + break; + case Status::SHOW_DND: + result->AddText(STR_SHOW_DND, 1); + break; + case Status::SHOW_CHAT: + result->AddText(STR_SHOW_CHAT, 1); + break; + } + } + + result->AddElement(new XmlElement(TQN_STATUS)); + result->AddText(s.status(), 1); + + std::string pri; + ToString(s.priority(), &pri); + + result->AddElement(new XmlElement(TQN_PRIORITY)); + result->AddText(pri, 1); + + if (s.know_capabilities() && s.is_google_client()) { + result->AddElement(new XmlElement(TQN_CAPS_C, true)); + result->AddAttr(TQN_NODE, GOOGLE_CLIENT_NODE, 1); + result->AddAttr(TQN_VER, s.version(), 1); + result->AddAttr(TQN_EXT, s.phone_capability() ? "voice-v1" : "", 1); + } + + // Put the delay mark on the presence according to JEP-0091 + { + result->AddElement(new XmlElement(kQnDelayX, true)); + + // This here is why we *love* the C runtime + time_t current_time_seconds; + time(¤t_time_seconds); + struct tm* current_time = gmtime(¤t_time_seconds); + char output[256]; + strftime(output, ARRAY_SIZE(output), "%Y%m%dT%H:%M:%S", current_time); + result->AddAttr(kQnStamp, output, 1); + } + + } + + return result; +} + + +} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presencepushtask.cc b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presencepushtask.cc deleted file mode 100644 index 31d1ae77..00000000 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presencepushtask.cc +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Jingle call example - * Copyright 2004--2005, Google Inc. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "talk/examples/call/presencepushtask.h" -#include "talk/xmpp/constants.h" -#include - - -namespace buzz { - -// string helper functions ----------------------------------------------------- -template static -bool FromString(const std::string& s, - T * t) { - std::istringstream iss(s); - return !(iss>>*t).fail(); -} - -template static -bool ToString(const T &t, - std::string* s) { - std::ostringstream oss; - oss << t; - *s = oss.str(); - return !oss.fail(); -} - -static bool -IsXmlSpace(int ch) { - return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; -} - -static bool -ListContainsToken(const std::string & list, const std::string & token) { - size_t i = list.find(token); - if (i == std::string::npos || token.empty()) - return false; - bool boundary_before = (i == 0 || IsXmlSpace(list[i - 1])); - bool boundary_after = (i == list.length() - token.length() || IsXmlSpace(list[i + token.length()])); - return boundary_before && boundary_after; -} - - -bool -PresencePushTask::HandleStanza(const XmlElement * stanza) { - if (stanza->Name() != TQN_PRESENCE) - return false; - if (stanza->HasAttr(TQN_TYPE) && stanza->Attr(TQN_TYPE) != STR_UNAVAILABLE) - return false; - QueueStanza(stanza); - return true; -} - -static bool IsUtf8FirstByte(int c) { - return (((c)&0x80)==0) || // is single byte - ((unsigned char)((c)-0xc0)<0x3e); // or is lead byte -} - -int -PresencePushTask::ProcessStart() { - const XmlElement * stanza = NextStanza(); - if (stanza == NULL) - return STATE_BLOCKED; - Status s; - - s.set_jid(Jid(stanza->Attr(TQN_FROM))); - - if (stanza->Attr(TQN_TYPE) == STR_UNAVAILABLE) { - s.set_available(false); - SignalStatusUpdate(s); - } - else { - s.set_available(true); - const XmlElement * status = stanza->FirstNamed(TQN_STATUS); - if (status != NULL) { - s.set_status(status->BodyText()); - - // Truncate status messages longer than 300 bytes - if (s.status().length() > 300) { - size_t len = 300; - - // Be careful not to split legal utf-8 chars in half - while (!IsUtf8FirstByte(s.status()[len]) && len > 0) { - len -= 1; - } - std::string truncated(s.status(), 0, len); - s.set_status(truncated); - } - } - - const XmlElement * priority = stanza->FirstNamed(TQN_PRIORITY); - if (priority != NULL) { - int pri; - if (FromString(priority->BodyText(), &pri)) { - s.set_priority(pri); - } - } - - const XmlElement * show = stanza->FirstNamed(TQN_SHOW); - if (show == NULL || show->FirstChild() == NULL) { - s.set_show(Status::SHOW_ONLINE); - } - else { - if (show->BodyText() == "away") { - s.set_show(Status::SHOW_AWAY); - } - else if (show->BodyText() == "xa") { - s.set_show(Status::SHOW_XA); - } - else if (show->BodyText() == "dnd") { - s.set_show(Status::SHOW_DND); - } - else if (show->BodyText() == "chat") { - s.set_show(Status::SHOW_CHAT); - } - else { - s.set_show(Status::SHOW_ONLINE); - } - } - - const XmlElement * caps = stanza->FirstNamed(TQN_CAPS_C); - if (caps != NULL) { - std::string node = caps->Attr(TQN_NODE); - std::string ver = caps->Attr(TQN_VER); - std::string exts = caps->Attr(TQN_EXT); - - s.set_know_capabilities(true); - - if (node == GOOGLE_CLIENT_NODE) { - s.set_is_google_client(true); - s.set_version(ver); - if (ListContainsToken(exts, "voice-v1")) { - s.set_phone_capability(true); - } - } - } - - const XmlElement* delay = stanza->FirstNamed(kQnDelayX); - if (delay != NULL) { - // Ideally we would parse this according to the Psuedo ISO-8601 rules - // that are laid out in JEP-0082: - // http://www.jabber.org/jeps/jep-0082.html - std::string stamp = delay->Attr(kQnStamp); - s.set_sent_time(stamp); - } - - SignalStatusUpdate(s); - } - - return STATE_START; -} - - -} - - diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presencepushtask.cpp b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presencepushtask.cpp new file mode 100644 index 00000000..31d1ae77 --- /dev/null +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/call/presencepushtask.cpp @@ -0,0 +1,172 @@ +/* + * Jingle call example + * Copyright 2004--2005, Google Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "talk/examples/call/presencepushtask.h" +#include "talk/xmpp/constants.h" +#include + + +namespace buzz { + +// string helper functions ----------------------------------------------------- +template static +bool FromString(const std::string& s, + T * t) { + std::istringstream iss(s); + return !(iss>>*t).fail(); +} + +template static +bool ToString(const T &t, + std::string* s) { + std::ostringstream oss; + oss << t; + *s = oss.str(); + return !oss.fail(); +} + +static bool +IsXmlSpace(int ch) { + return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; +} + +static bool +ListContainsToken(const std::string & list, const std::string & token) { + size_t i = list.find(token); + if (i == std::string::npos || token.empty()) + return false; + bool boundary_before = (i == 0 || IsXmlSpace(list[i - 1])); + bool boundary_after = (i == list.length() - token.length() || IsXmlSpace(list[i + token.length()])); + return boundary_before && boundary_after; +} + + +bool +PresencePushTask::HandleStanza(const XmlElement * stanza) { + if (stanza->Name() != TQN_PRESENCE) + return false; + if (stanza->HasAttr(TQN_TYPE) && stanza->Attr(TQN_TYPE) != STR_UNAVAILABLE) + return false; + QueueStanza(stanza); + return true; +} + +static bool IsUtf8FirstByte(int c) { + return (((c)&0x80)==0) || // is single byte + ((unsigned char)((c)-0xc0)<0x3e); // or is lead byte +} + +int +PresencePushTask::ProcessStart() { + const XmlElement * stanza = NextStanza(); + if (stanza == NULL) + return STATE_BLOCKED; + Status s; + + s.set_jid(Jid(stanza->Attr(TQN_FROM))); + + if (stanza->Attr(TQN_TYPE) == STR_UNAVAILABLE) { + s.set_available(false); + SignalStatusUpdate(s); + } + else { + s.set_available(true); + const XmlElement * status = stanza->FirstNamed(TQN_STATUS); + if (status != NULL) { + s.set_status(status->BodyText()); + + // Truncate status messages longer than 300 bytes + if (s.status().length() > 300) { + size_t len = 300; + + // Be careful not to split legal utf-8 chars in half + while (!IsUtf8FirstByte(s.status()[len]) && len > 0) { + len -= 1; + } + std::string truncated(s.status(), 0, len); + s.set_status(truncated); + } + } + + const XmlElement * priority = stanza->FirstNamed(TQN_PRIORITY); + if (priority != NULL) { + int pri; + if (FromString(priority->BodyText(), &pri)) { + s.set_priority(pri); + } + } + + const XmlElement * show = stanza->FirstNamed(TQN_SHOW); + if (show == NULL || show->FirstChild() == NULL) { + s.set_show(Status::SHOW_ONLINE); + } + else { + if (show->BodyText() == "away") { + s.set_show(Status::SHOW_AWAY); + } + else if (show->BodyText() == "xa") { + s.set_show(Status::SHOW_XA); + } + else if (show->BodyText() == "dnd") { + s.set_show(Status::SHOW_DND); + } + else if (show->BodyText() == "chat") { + s.set_show(Status::SHOW_CHAT); + } + else { + s.set_show(Status::SHOW_ONLINE); + } + } + + const XmlElement * caps = stanza->FirstNamed(TQN_CAPS_C); + if (caps != NULL) { + std::string node = caps->Attr(TQN_NODE); + std::string ver = caps->Attr(TQN_VER); + std::string exts = caps->Attr(TQN_EXT); + + s.set_know_capabilities(true); + + if (node == GOOGLE_CLIENT_NODE) { + s.set_is_google_client(true); + s.set_version(ver); + if (ListContainsToken(exts, "voice-v1")) { + s.set_phone_capability(true); + } + } + } + + const XmlElement* delay = stanza->FirstNamed(kQnDelayX); + if (delay != NULL) { + // Ideally we would parse this according to the Psuedo ISO-8601 rules + // that are laid out in JEP-0082: + // http://www.jabber.org/jeps/jep-0082.html + std::string stamp = delay->Attr(kQnStamp); + s.set_sent_time(stamp); + } + + SignalStatusUpdate(s); + } + + return STATE_START; +} + + +} + + diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/Makefile.am b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/Makefile.am index 16164fb7..78851923 100644 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/Makefile.am +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/Makefile.am @@ -1,12 +1,12 @@ noinst_LTLIBRARIES= libcricketexampleslogin.la -libcricketexampleslogin_la_SOURCES = xmppsocket.cc \ - xmppauth.cc \ - xmppthread.cc \ - xmpppump.cc +libcricketexampleslogin_la_SOURCES = xmppsocket.cpp \ + xmppauth.cpp \ + xmppthread.cpp \ + xmpppump.cpp noinst_HEADERS = xmppauth.h xmpppump.h xmppsocket.h xmppthread.h bin_PROGRAMS = login login_CXXFLAGS = $(AM_CXXFLAGS) -login_SOURCES = login_main.cc xmppsocket.cc xmppthread.cc xmpppump.cc xmppauth.cc +login_SOURCES = login_main.cpp xmppsocket.cpp xmppthread.cpp xmpppump.cpp xmppauth.cpp login_LDADD = $(srcdir)/../../../talk/xmpp/libcricketxmpp.la \ $(srcdir)/../../../talk/xmllite/libcricketxmllite.la \ $(srcdir)/../../../talk/base/libcricketbase.la \ diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/login_main.cc b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/login_main.cc deleted file mode 100644 index 2939c79f..00000000 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/login_main.cc +++ /dev/null @@ -1,63 +0,0 @@ -/* - * libjingle - * Copyright 2004--2005, Google Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "talk/base/thread.h" -#include "talk/xmpp/xmppclientsettings.h" -#include "talk/examples/login/xmppthread.h" -#include - -int main(int argc, char **argv) { - printf("Auth Cookie: "); - fflush(stdout); - - char auth_cookie[256]; - scanf("%s", auth_cookie); - - char username[256]; - scanf("%s", username); - - // Start xmpp on a different thread - XmppThread thread; - thread.Start(); - - buzz::XmppClientSettings xcs; - xcs.set_user(username); - xcs.set_host("gmail.com"); - xcs.set_use_tls(false); - xcs.set_auth_cookie(auth_cookie); - xcs.set_server(cricket::SocketAddress("talk.google.com", 5222)); - thread.Login(xcs); - - // Use main thread for console input - std::string line; - while (std::getline(std::cin, line)) { - if (line == "quit") - break; - } - return 0; -} - diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/login_main.cpp b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/login_main.cpp new file mode 100644 index 00000000..2939c79f --- /dev/null +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/login_main.cpp @@ -0,0 +1,63 @@ +/* + * libjingle + * Copyright 2004--2005, Google Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "talk/base/thread.h" +#include "talk/xmpp/xmppclientsettings.h" +#include "talk/examples/login/xmppthread.h" +#include + +int main(int argc, char **argv) { + printf("Auth Cookie: "); + fflush(stdout); + + char auth_cookie[256]; + scanf("%s", auth_cookie); + + char username[256]; + scanf("%s", username); + + // Start xmpp on a different thread + XmppThread thread; + thread.Start(); + + buzz::XmppClientSettings xcs; + xcs.set_user(username); + xcs.set_host("gmail.com"); + xcs.set_use_tls(false); + xcs.set_auth_cookie(auth_cookie); + xcs.set_server(cricket::SocketAddress("talk.google.com", 5222)); + thread.Login(xcs); + + // Use main thread for console input + std::string line; + while (std::getline(std::cin, line)) { + if (line == "quit") + break; + } + return 0; +} + diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppauth.cc b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppauth.cc deleted file mode 100644 index 66191f12..00000000 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppauth.cc +++ /dev/null @@ -1,93 +0,0 @@ -/* - * libjingle - * Copyright 2004--2005, Google Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include "talk/examples/login/xmppauth.h" -#include "talk/xmpp/saslcookiemechanism.h" -#include "talk/xmpp/saslplainmechanism.h" - -XmppAuth::XmppAuth() : done_(false), error_(false) { -} - -XmppAuth::~XmppAuth() { -} - -void XmppAuth::StartPreXmppAuth(const buzz::Jid & jid, - const cricket::SocketAddress & server, - const buzz::XmppPassword & pass, - const std::string & auth_cookie) { - jid_ = jid; - passwd_ = pass; - auth_cookie_ = auth_cookie; - error_ = auth_cookie.empty(); - done_ = true; - - SignalAuthDone(); -} - -std::string XmppAuth::ChooseBestSaslMechanism( - const std::vector & mechanisms, - bool encrypted) { - std::vector::const_iterator it; - - // a token is the weakest auth - 15s, service-limited, so prefer it. - it = std::find(mechanisms.begin(), mechanisms.end(), "X-GOOGLE-TOKEN"); - if (it != mechanisms.end()) - return "X-GOOGLE-TOKEN"; - - // a cookie is the next weakest - 14 days - it = std::find(mechanisms.begin(), mechanisms.end(), "X-GOOGLE-COOKIE"); - if (it != mechanisms.end()) - return "X-GOOGLE-COOKIE"; - - // never pass @google.com passwords without encryption!! - if (!encrypted && (jid_.domain() == "google.com")) - return ""; - - // as a last resort, use plain authentication - if (jid_.domain() != "google.com") { - it = std::find(mechanisms.begin(), mechanisms.end(), "PLAIN"); - if (it != mechanisms.end()) - return "PLAIN"; - } - - // No good mechanism found - return ""; -} - -buzz::SaslMechanism* XmppAuth::CreateSaslMechanism( - const std::string & mechanism) { - if (mechanism == "X-GOOGLE-TOKEN") { - return new buzz::SaslCookieMechanism(mechanism, jid_.Str(), auth_cookie_); - //} else if (mechanism == "X-GOOGLE-COOKIE") { - // return new buzz::SaslCookieMechanism(mechanism, jid.Str(), sid_); - } else if (mechanism == "PLAIN") { - return new buzz::SaslPlainMechanism(jid_, passwd_); - } else { - return NULL; - } -} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppauth.cpp b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppauth.cpp new file mode 100644 index 00000000..66191f12 --- /dev/null +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppauth.cpp @@ -0,0 +1,93 @@ +/* + * libjingle + * Copyright 2004--2005, Google Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "talk/examples/login/xmppauth.h" +#include "talk/xmpp/saslcookiemechanism.h" +#include "talk/xmpp/saslplainmechanism.h" + +XmppAuth::XmppAuth() : done_(false), error_(false) { +} + +XmppAuth::~XmppAuth() { +} + +void XmppAuth::StartPreXmppAuth(const buzz::Jid & jid, + const cricket::SocketAddress & server, + const buzz::XmppPassword & pass, + const std::string & auth_cookie) { + jid_ = jid; + passwd_ = pass; + auth_cookie_ = auth_cookie; + error_ = auth_cookie.empty(); + done_ = true; + + SignalAuthDone(); +} + +std::string XmppAuth::ChooseBestSaslMechanism( + const std::vector & mechanisms, + bool encrypted) { + std::vector::const_iterator it; + + // a token is the weakest auth - 15s, service-limited, so prefer it. + it = std::find(mechanisms.begin(), mechanisms.end(), "X-GOOGLE-TOKEN"); + if (it != mechanisms.end()) + return "X-GOOGLE-TOKEN"; + + // a cookie is the next weakest - 14 days + it = std::find(mechanisms.begin(), mechanisms.end(), "X-GOOGLE-COOKIE"); + if (it != mechanisms.end()) + return "X-GOOGLE-COOKIE"; + + // never pass @google.com passwords without encryption!! + if (!encrypted && (jid_.domain() == "google.com")) + return ""; + + // as a last resort, use plain authentication + if (jid_.domain() != "google.com") { + it = std::find(mechanisms.begin(), mechanisms.end(), "PLAIN"); + if (it != mechanisms.end()) + return "PLAIN"; + } + + // No good mechanism found + return ""; +} + +buzz::SaslMechanism* XmppAuth::CreateSaslMechanism( + const std::string & mechanism) { + if (mechanism == "X-GOOGLE-TOKEN") { + return new buzz::SaslCookieMechanism(mechanism, jid_.Str(), auth_cookie_); + //} else if (mechanism == "X-GOOGLE-COOKIE") { + // return new buzz::SaslCookieMechanism(mechanism, jid.Str(), sid_); + } else if (mechanism == "PLAIN") { + return new buzz::SaslPlainMechanism(jid_, passwd_); + } else { + return NULL; + } +} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmpppump.cc b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmpppump.cc deleted file mode 100644 index 7d966fb3..00000000 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmpppump.cc +++ /dev/null @@ -1,73 +0,0 @@ -/* - * libjingle - * Copyright 2004--2005, Google Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "talk/examples/login/xmpppump.h" -#include "talk/examples/login/xmppauth.h" - -XmppPump::XmppPump(XmppPumpNotify * notify) { - state_ = buzz::XmppEngine::STATE_NONE; - notify_ = notify; - client_ = new buzz::XmppClient(this); // NOTE: deleted by TaskRunner -} - -void XmppPump::DoLogin(const buzz::XmppClientSettings & xcs, - buzz::AsyncSocket* socket, - buzz::PreXmppAuth* auth) { - OnStateChange(buzz::XmppEngine::STATE_START); - client_->SignalStateChange.connect(this, &XmppPump::OnStateChange); - client_->Connect(xcs, socket, auth); - client_->Start(); -} - -void XmppPump::DoDisconnect() { - client_->Disconnect(); - OnStateChange(buzz::XmppEngine::STATE_CLOSED); -} - -void XmppPump::OnStateChange(buzz::XmppEngine::State state) { - if (state_ == state) - return; - state_ = state; - if (notify_ != NULL) - notify_->OnStateChange(state); -} - -void XmppPump::WakeTasks() { - cricket::Thread::Current()->Post(this); -} - -unsigned long long XmppPump::CurrentTime() { - return cricket::Time(); -} - -void XmppPump::OnMessage(cricket::Message *pmsg) { - RunTasks(); -} - -buzz::XmppReturnStatus XmppPump::SendStanza(const buzz::XmlElement *stanza) { - return client_->SendStanza(stanza); -} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmpppump.cpp b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmpppump.cpp new file mode 100644 index 00000000..7d966fb3 --- /dev/null +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmpppump.cpp @@ -0,0 +1,73 @@ +/* + * libjingle + * Copyright 2004--2005, Google Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "talk/examples/login/xmpppump.h" +#include "talk/examples/login/xmppauth.h" + +XmppPump::XmppPump(XmppPumpNotify * notify) { + state_ = buzz::XmppEngine::STATE_NONE; + notify_ = notify; + client_ = new buzz::XmppClient(this); // NOTE: deleted by TaskRunner +} + +void XmppPump::DoLogin(const buzz::XmppClientSettings & xcs, + buzz::AsyncSocket* socket, + buzz::PreXmppAuth* auth) { + OnStateChange(buzz::XmppEngine::STATE_START); + client_->SignalStateChange.connect(this, &XmppPump::OnStateChange); + client_->Connect(xcs, socket, auth); + client_->Start(); +} + +void XmppPump::DoDisconnect() { + client_->Disconnect(); + OnStateChange(buzz::XmppEngine::STATE_CLOSED); +} + +void XmppPump::OnStateChange(buzz::XmppEngine::State state) { + if (state_ == state) + return; + state_ = state; + if (notify_ != NULL) + notify_->OnStateChange(state); +} + +void XmppPump::WakeTasks() { + cricket::Thread::Current()->Post(this); +} + +unsigned long long XmppPump::CurrentTime() { + return cricket::Time(); +} + +void XmppPump::OnMessage(cricket::Message *pmsg) { + RunTasks(); +} + +buzz::XmppReturnStatus XmppPump::SendStanza(const buzz::XmlElement *stanza) { + return client_->SendStanza(stanza); +} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppsocket.cc b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppsocket.cc deleted file mode 100644 index 33aabf3e..00000000 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppsocket.cc +++ /dev/null @@ -1,144 +0,0 @@ -/* - * libjingle - * Copyright 2004--2005, Google Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include "talk/base/basicdefs.h" -#include "talk/base/logging.h" -#include "talk/base/thread.h" -#ifdef FEATURE_ENABLE_SSL -#include "talk/base/schanneladapter.h" -#endif -#include "xmppsocket.h" - -XmppSocket::XmppSocket(bool tls) : tls_(tls) { - cricket::Thread* pth = cricket::Thread::Current(); - cricket::AsyncSocket* socket = - pth->socketserver()->CreateAsyncSocket(SOCK_STREAM); -#ifdef FEATURE_ENABLE_SSL - if (tls_) - socket = new cricket::SChannelAdapter(socket); -#endif - cricket_socket_ = socket; - cricket_socket_->SignalReadEvent.connect(this, &XmppSocket::OnReadEvent); - cricket_socket_->SignalWriteEvent.connect(this, &XmppSocket::OnWriteEvent); - cricket_socket_->SignalConnectEvent.connect(this, - &XmppSocket::OnConnectEvent); - state_ = buzz::AsyncSocket::STATE_CLOSED; -} - -XmppSocket::~XmppSocket() { - Close(); - delete cricket_socket_; -} - -void XmppSocket::OnReadEvent(cricket::AsyncSocket * socket) { - SignalRead(); -} - -void XmppSocket::OnWriteEvent(cricket::AsyncSocket * socket) { - // Write bytes if there are any - while (buffer_.Length() != 0) { - int written = cricket_socket_->Send(buffer_.Data(), buffer_.Length()); - if (written > 0) { - buffer_.Shift(written); - continue; - } - if (!cricket_socket_->IsBlocking()) - LOG(LS_ERROR) << "Send error: " << cricket_socket_->GetError(); - return; - } -} - -void XmppSocket::OnConnectEvent(cricket::AsyncSocket * socket) { -#if defined(FEATURE_ENABLE_SSL) - if (state_ == buzz::AsyncSocket::STATE_TLS_CONNECTING) { - state_ = buzz::AsyncSocket::STATE_TLS_OPEN; - SignalSSLConnected(); - OnWriteEvent(cricket_socket_); - return; - } -#endif // !defined(FEATURE_ENABLE_SSL) - state_ = buzz::AsyncSocket::STATE_OPEN; - SignalConnected(); -} - -buzz::AsyncSocket::State XmppSocket::state() { - return state_; -} - -buzz::AsyncSocket::Error XmppSocket::error() { - return buzz::AsyncSocket::ERROR_NONE; -} - -bool XmppSocket::Connect(const cricket::SocketAddress& addr) { - if (cricket_socket_->Connect(addr) < 0) { - return cricket_socket_->IsBlocking(); - } - return true; -} - -bool XmppSocket::Read(char * data, size_t len, size_t* len_read) { - int read = cricket_socket_->Recv(data, len); - if (read > 0) { - *len_read = (size_t)read; - return true; - } - return false; -} - -bool XmppSocket::Write(const char * data, size_t len) { - buffer_.WriteBytes(data, len); - OnWriteEvent(cricket_socket_); - return true; -} - -bool XmppSocket::Close() { - if (state_ != buzz::AsyncSocket::STATE_OPEN) - return false; - if (cricket_socket_->Close() == 0) { - state_ = buzz::AsyncSocket::STATE_CLOSED; - SignalClosed(); - return true; - } - return false; -} - -bool XmppSocket::StartTls(const std::string & domainname) { -#if defined(FEATURE_ENABLE_SSL) - if (!tls_) - return false; - cricket::SChannelAdapter * ssl = - static_cast(cricket_socket_); - ssl->set_ignore_bad_cert(true); - if (ssl->StartSSL(domainname.c_str(), false) != 0) - return false; - state_ = buzz::AsyncSocket::STATE_TLS_CONNECTING; - return true; -#else // !defined(FEATURE_ENABLE_SSL) - return false; -#endif // !defined(FEATURE_ENABLE_SSL) -} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppsocket.cpp b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppsocket.cpp new file mode 100644 index 00000000..33aabf3e --- /dev/null +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppsocket.cpp @@ -0,0 +1,144 @@ +/* + * libjingle + * Copyright 2004--2005, Google Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "talk/base/basicdefs.h" +#include "talk/base/logging.h" +#include "talk/base/thread.h" +#ifdef FEATURE_ENABLE_SSL +#include "talk/base/schanneladapter.h" +#endif +#include "xmppsocket.h" + +XmppSocket::XmppSocket(bool tls) : tls_(tls) { + cricket::Thread* pth = cricket::Thread::Current(); + cricket::AsyncSocket* socket = + pth->socketserver()->CreateAsyncSocket(SOCK_STREAM); +#ifdef FEATURE_ENABLE_SSL + if (tls_) + socket = new cricket::SChannelAdapter(socket); +#endif + cricket_socket_ = socket; + cricket_socket_->SignalReadEvent.connect(this, &XmppSocket::OnReadEvent); + cricket_socket_->SignalWriteEvent.connect(this, &XmppSocket::OnWriteEvent); + cricket_socket_->SignalConnectEvent.connect(this, + &XmppSocket::OnConnectEvent); + state_ = buzz::AsyncSocket::STATE_CLOSED; +} + +XmppSocket::~XmppSocket() { + Close(); + delete cricket_socket_; +} + +void XmppSocket::OnReadEvent(cricket::AsyncSocket * socket) { + SignalRead(); +} + +void XmppSocket::OnWriteEvent(cricket::AsyncSocket * socket) { + // Write bytes if there are any + while (buffer_.Length() != 0) { + int written = cricket_socket_->Send(buffer_.Data(), buffer_.Length()); + if (written > 0) { + buffer_.Shift(written); + continue; + } + if (!cricket_socket_->IsBlocking()) + LOG(LS_ERROR) << "Send error: " << cricket_socket_->GetError(); + return; + } +} + +void XmppSocket::OnConnectEvent(cricket::AsyncSocket * socket) { +#if defined(FEATURE_ENABLE_SSL) + if (state_ == buzz::AsyncSocket::STATE_TLS_CONNECTING) { + state_ = buzz::AsyncSocket::STATE_TLS_OPEN; + SignalSSLConnected(); + OnWriteEvent(cricket_socket_); + return; + } +#endif // !defined(FEATURE_ENABLE_SSL) + state_ = buzz::AsyncSocket::STATE_OPEN; + SignalConnected(); +} + +buzz::AsyncSocket::State XmppSocket::state() { + return state_; +} + +buzz::AsyncSocket::Error XmppSocket::error() { + return buzz::AsyncSocket::ERROR_NONE; +} + +bool XmppSocket::Connect(const cricket::SocketAddress& addr) { + if (cricket_socket_->Connect(addr) < 0) { + return cricket_socket_->IsBlocking(); + } + return true; +} + +bool XmppSocket::Read(char * data, size_t len, size_t* len_read) { + int read = cricket_socket_->Recv(data, len); + if (read > 0) { + *len_read = (size_t)read; + return true; + } + return false; +} + +bool XmppSocket::Write(const char * data, size_t len) { + buffer_.WriteBytes(data, len); + OnWriteEvent(cricket_socket_); + return true; +} + +bool XmppSocket::Close() { + if (state_ != buzz::AsyncSocket::STATE_OPEN) + return false; + if (cricket_socket_->Close() == 0) { + state_ = buzz::AsyncSocket::STATE_CLOSED; + SignalClosed(); + return true; + } + return false; +} + +bool XmppSocket::StartTls(const std::string & domainname) { +#if defined(FEATURE_ENABLE_SSL) + if (!tls_) + return false; + cricket::SChannelAdapter * ssl = + static_cast(cricket_socket_); + ssl->set_ignore_bad_cert(true); + if (ssl->StartSSL(domainname.c_str(), false) != 0) + return false; + state_ = buzz::AsyncSocket::STATE_TLS_CONNECTING; + return true; +#else // !defined(FEATURE_ENABLE_SSL) + return false; +#endif // !defined(FEATURE_ENABLE_SSL) +} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppthread.cc b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppthread.cc deleted file mode 100644 index 7a1b2a13..00000000 --- a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppthread.cc +++ /dev/null @@ -1,80 +0,0 @@ -/* - * libjingle - * Copyright 2004--2005, Google Inc. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "talk/xmpp/xmppclientsettings.h" -#include "talk/examples/login/xmppthread.h" -#include "talk/examples/login/xmppauth.h" - -namespace { - -const uint32 MSG_LOGIN = 1; -const uint32 MSG_DISCONNECT = 2; - -struct LoginData: public cricket::MessageData { - LoginData(const buzz::XmppClientSettings& s) : xcs(s) {} - virtual ~LoginData() {} - - buzz::XmppClientSettings xcs; -}; - -} // namespace - -XmppThread::XmppThread() { - pump_ = new XmppPump(this); -} - -XmppThread::~XmppThread() { - delete pump_; -} - -void XmppThread::Loop(int cms) { - Thread::Loop(cms); -} - -void XmppThread::Login(const buzz::XmppClientSettings& xcs) { - Post(this, MSG_LOGIN, new LoginData(xcs)); -} - -void XmppThread::Disconnect() { - Post(this, MSG_DISCONNECT); -} - -void XmppThread::OnStateChange(buzz::XmppEngine::State state) { -} - -void XmppThread::OnMessage(cricket::Message* pmsg) { - if (pmsg->message_id == MSG_LOGIN) { - assert(pmsg->pdata); - LoginData* data = reinterpret_cast(pmsg->pdata); - pump_->DoLogin(data->xcs, new XmppSocket(false), new XmppAuth()); - delete data; - } else if (pmsg->message_id == MSG_DISCONNECT) { - pump_->DoDisconnect(); - } else { - assert(false); - } -} diff --git a/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppthread.cpp b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppthread.cpp new file mode 100644 index 00000000..7a1b2a13 --- /dev/null +++ b/kopete/protocols/jabber/jingle/libjingle/talk/examples/login/xmppthread.cpp @@ -0,0 +1,80 @@ +/* + * libjingle + * Copyright 2004--2005, Google Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "talk/xmpp/xmppclientsettings.h" +#include "talk/examples/login/xmppthread.h" +#include "talk/examples/login/xmppauth.h" + +namespace { + +const uint32 MSG_LOGIN = 1; +const uint32 MSG_DISCONNECT = 2; + +struct LoginData: public cricket::MessageData { + LoginData(const buzz::XmppClientSettings& s) : xcs(s) {} + virtual ~LoginData() {} + + buzz::XmppClientSettings xcs; +}; + +} // namespace + +XmppThread::XmppThread() { + pump_ = new XmppPump(this); +} + +XmppThread::~XmppThread() { + delete pump_; +} + +void XmppThread::Loop(int cms) { + Thread::Loop(cms); +} + +void XmppThread::Login(const buzz::XmppClientSettings& xcs) { + Post(this, MSG_LOGIN, new LoginData(xcs)); +} + +void XmppThread::Disconnect() { + Post(this, MSG_DISCONNECT); +} + +void XmppThread::OnStateChange(buzz::XmppEngine::State state) { +} + +void XmppThread::OnMessage(cricket::Message* pmsg) { + if (pmsg->message_id == MSG_LOGIN) { + assert(pmsg->pdata); + LoginData* data = reinterpret_cast(pmsg->pdata); + pump_->DoLogin(data->xcs, new XmppSocket(false), new XmppAuth()); + delete data; + } else if (pmsg->message_id == MSG_DISCONNECT) { + pump_->DoDisconnect(); + } else { + assert(false); + } +} -- cgit v1.2.1