1 /* 2 * libjingle 3 * Copyright 2004--2005, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef TALK_XMPP_XMPPCLIENT_H_ 29 #define TALK_XMPP_XMPPCLIENT_H_ 30 31 #include <string> 32 #include "talk/base/basicdefs.h" 33 #include "talk/base/sigslot.h" 34 #include "talk/base/task.h" 35 #include "talk/xmpp/asyncsocket.h" 36 #include "talk/xmpp/xmppclientsettings.h" 37 #include "talk/xmpp/xmppengine.h" 38 #include "talk/xmpp/xmpptask.h" 39 40 namespace buzz { 41 42 class PreXmppAuth; 43 class CaptchaChallenge; 44 45 // Just some non-colliding number. Could have picked "1". 46 #define XMPP_CLIENT_TASK_CODE 0x366c1e47 47 48 ///////////////////////////////////////////////////////////////////// 49 // 50 // XMPPCLIENT 51 // 52 ///////////////////////////////////////////////////////////////////// 53 // 54 // See Task first. XmppClient is a parent task for XmppTasks. 55 // 56 // XmppClient is a task which is designed to be the parent task for 57 // all tasks that depend on a single Xmpp connection. If you want to, 58 // for example, listen for subscription requests forever, then your 59 // listener should be a task that is a child of the XmppClient that owns 60 // the connection you are using. XmppClient has all the utility methods 61 // that basically drill through to XmppEngine. 62 // 63 // XmppClient is just a wrapper for XmppEngine, and if I were writing it 64 // all over again, I would make XmppClient == XmppEngine. Why? 65 // XmppEngine needs tasks too, for example it has an XmppLoginTask which 66 // should just be the same kind of Task instead of an XmppEngine specific 67 // thing. It would help do certain things like GAIA auth cleaner. 68 // 69 ///////////////////////////////////////////////////////////////////// 70 71 class XmppClient : public XmppTaskParentInterface, 72 public XmppClientInterface, 73 public sigslot::has_slots<> 74 { 75 public: 76 explicit XmppClient(talk_base::TaskParent * parent); 77 virtual ~XmppClient(); 78 79 XmppReturnStatus Connect(const XmppClientSettings & settings, 80 const std::string & lang, 81 AsyncSocket * socket, 82 PreXmppAuth * preauth); 83 84 virtual int ProcessStart(); 85 virtual int ProcessResponse(); 86 XmppReturnStatus Disconnect(); 87 88 sigslot::signal1<XmppEngine::State> SignalStateChange; 89 XmppEngine::Error GetError(int *subcode); 90 91 // When there is a <stream:error> stanza, return the stanza 92 // so that they can be handled. 93 const XmlElement *GetStreamError(); 94 95 // When there is an authentication error, we may have captcha info 96 // that the user can use to unlock their account 97 CaptchaChallenge GetCaptchaChallenge(); 98 99 // When authentication is successful, this returns the service token 100 // (if we used GAIA authentication) 101 std::string GetAuthMechanism(); 102 std::string GetAuthToken(); 103 104 XmppReturnStatus SendRaw(const std::string & text); 105 106 XmppEngine* engine(); 107 108 sigslot::signal2<const char *, int> SignalLogInput; 109 sigslot::signal2<const char *, int> SignalLogOutput; 110 111 // As XmppTaskParentIntreface 112 virtual XmppClientInterface* GetClient() { return this; } 113 114 // As XmppClientInterface 115 virtual XmppEngine::State GetState() const; 116 virtual const Jid& jid() const; 117 virtual std::string NextId(); 118 virtual XmppReturnStatus SendStanza(const XmlElement *stanza); 119 virtual XmppReturnStatus SendStanzaError(const XmlElement * pelOriginal, 120 XmppStanzaError code, 121 const std::string & text); 122 virtual void AddXmppTask(XmppTask *, XmppEngine::HandlerLevel); 123 virtual void RemoveXmppTask(XmppTask *); 124 125 private: 126 friend class XmppTask; 127 128 void OnAuthDone(); 129 130 // Internal state management 131 enum { 132 STATE_PRE_XMPP_LOGIN = STATE_NEXT, 133 STATE_START_XMPP_LOGIN = STATE_NEXT + 1, 134 }; 135 int Process(int state) { 136 switch (state) { 137 case STATE_PRE_XMPP_LOGIN: return ProcessTokenLogin(); 138 case STATE_START_XMPP_LOGIN: return ProcessStartXmppLogin(); 139 default: return Task::Process(state); 140 } 141 } 142 143 std::string GetStateName(int state) const { 144 switch (state) { 145 case STATE_PRE_XMPP_LOGIN: return "PRE_XMPP_LOGIN"; 146 case STATE_START_XMPP_LOGIN: return "START_XMPP_LOGIN"; 147 default: return Task::GetStateName(state); 148 } 149 } 150 151 int ProcessTokenLogin(); 152 int ProcessStartXmppLogin(); 153 void EnsureClosed(); 154 155 class Private; 156 friend class Private; 157 talk_base::scoped_ptr<Private> d_; 158 159 bool delivering_signal_; 160 bool valid_; 161 }; 162 163 } 164 165 #endif // TALK_XMPP_XMPPCLIENT_H_ 166