Code

Newer files
authorishmal <ishmal@users.sourceforge.net>
Thu, 4 May 2006 04:42:03 +0000 (04:42 +0000)
committerishmal <ishmal@users.sourceforge.net>
Thu, 4 May 2006 04:42:03 +0000 (04:42 +0000)
src/jabber_whiteboard/pedrodom.cpp
src/jabber_whiteboard/pedrodom.h
src/jabber_whiteboard/pedroxmpp.cpp
src/jabber_whiteboard/pedroxmpp.h

index 2ed770a1af9d0c04037751171ba267f7c82ef518..5ac4a61d02e56765a5701c29d086107adc6e066b 100644 (file)
@@ -26,9 +26,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
-#ifdef HAVE_MALLOC_H
 #include <malloc.h>
-#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 
index cd10f80c058adf9b8e2fafa1c0ef82c597e27fb3..9c6651259bdd288991ba7cf1f719f788dfd999dc 100644 (file)
@@ -6,7 +6,7 @@
  * Authors:
  *   Bob Jamison
  *
- * Copyright (C) 2005 Bob Jamison
+ * Copyright (C) 2005-2006 Bob Jamison
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
@@ -29,6 +29,7 @@
 
 namespace Pedro
 {
+
 typedef std::string DOMString;
 typedef unsigned int XMLCh;
 
@@ -47,8 +48,13 @@ public:
 
     Namespace(const Namespace &other)
         {
-        prefix       = other.prefix;
-        namespaceURI = other.namespaceURI;
+        assign(other);
+        }
+
+    Namespace &operator=(const Namespace &other)
+        {
+        assign(other);
+        return *this;
         }
 
     virtual ~Namespace()
@@ -62,6 +68,12 @@ public:
 
 protected:
 
+    void assign(const Namespace &other)
+        {
+        prefix       = other.prefix;
+        namespaceURI = other.namespaceURI;
+        }
+
     DOMString prefix;
     DOMString namespaceURI;
 
@@ -81,8 +93,13 @@ public:
 
     Attribute(const Attribute &other)
         {
-        name  = other.name;
-        value = other.value;
+        assign(other);
+        }
+
+    Attribute &operator=(const Attribute &other)
+        {
+        assign(other);
+        return *this;
         }
 
     virtual ~Attribute()
@@ -96,6 +113,12 @@ public:
 
 protected:
 
+    void assign(const Attribute &other)
+        {
+        name  = other.name;
+        value = other.value;
+        }
+
     DOMString name;
     DOMString value;
 
@@ -121,18 +144,19 @@ public:
     Element(const DOMString &nameArg, const DOMString &valueArg)
         {
         parent = NULL;
-        name  = nameArg;
-        value = valueArg;
+        name   = nameArg;
+        value  = valueArg;
         }
 
     Element(const Element &other)
         {
-        parent     = other.parent;
-        children   = other.children;
-        attributes = other.attributes;
-        namespaces = other.namespaces;
-        name       = other.name;
-        value      = other.value;
+        assign(other);
+        }
+
+    Element &operator=(const Element &other)
+        {
+        assign(other);
+        return *this;
         }
 
     virtual Element *clone();
@@ -187,6 +211,15 @@ public:
 
 protected:
 
+    void assign(const Element &other)
+        {
+        parent     = other.parent;
+        children   = other.children;
+        attributes = other.attributes;
+        namespaces = other.namespaces;
+        name       = other.name;
+        value      = other.value;
+        }
 
     void findElementsRecursive(std::vector<Element *>&res, const DOMString &name);
 
@@ -211,6 +244,9 @@ protected:
 class Parser
 {
 public:
+    /**
+     * Constructor
+     */
     Parser()
         { init(); }
 
@@ -284,14 +320,13 @@ private:
 
     Element *parse(XMLCh *buf,int pos,int len);
 
-    bool         keepGoing;
-    Element      *currentNode;
-    long         parselen;
-    XMLCh        *parsebuf;
-    DOMString    cdatabuf;
-    long         currentPosition;
-    int          colNr;
-
+    bool       keepGoing;
+    Element    *currentNode;
+    long       parselen;
+    XMLCh      *parsebuf;
+    DOMString  cdatabuf;
+    long       currentPosition;
+    int        colNr;
 
 };
 
index 0429a172f5f3019d38b41e1503c4164c81e077e3..f5ce69e0b04a7438c7db3e93790e3dce373e717c 100644 (file)
@@ -47,7 +47,7 @@
 
 #endif
 
-#ifdef WITH_SSL
+#ifdef HAVE_SSL
 #include <openssl/ssl.h>
 #include <openssl/err.h>
 #endif
@@ -614,7 +614,7 @@ public:
     static void hash(unsigned char *dataIn,
                      unsigned long len, unsigned char *digest);
 
-    static DOMString hashHex(unsigned char *dataIn, unsigned long len);
+    static DOMString Md5::hashHex(unsigned char *dataIn, unsigned long len);
 
     /**
      *  Initialize the context (also zeroizes contents)
@@ -681,7 +681,7 @@ static void byteReverse(unsigned char *buf, unsigned long longs)
 {
     do
         {
-        unsigned long t = (unsigned long) 
+        unsigned long t = (unsigned long)
             ((unsigned) buf[3] << 8 | buf[2]) << 16 |
             ((unsigned) buf[1] << 8 | buf[0]);
         *(unsigned long *) buf = t;
@@ -969,7 +969,7 @@ public:
         {}
     virtual ~Runnable()
         {}
-        
+
     /**
      * The method of a delegate class which can
      * be run by a Thread.  Thread is completed when this
@@ -1059,6 +1059,7 @@ static DWORD WINAPI WinThreadFunction(LPVOID context)
 {
     Thread *thread = (Thread *)context;
     thread->execute();
+    return 0;
 }
 
 
@@ -1179,7 +1180,7 @@ private:
 
     unsigned long receiveTimeout;
 
-#ifdef WITH_SSL
+#ifdef HAVE_SSL
     SSL_CTX *sslContext;
     SSL *sslStream;
 #endif
@@ -1240,7 +1241,7 @@ TcpSocket::TcpSocket(const std::string &hostnameArg, int port)
 }
 
 
-#ifdef WITH_SSL
+#ifdef HAVE_SSL
 
 static void cryptoLockCallback(int mode, int type, const char *file, int line)
 {
@@ -1336,9 +1337,9 @@ void TcpSocket::init()
 #ifdef __WIN32__
         WORD wVersionRequested = MAKEWORD( 2, 2 );
         WSADATA wsaData;
-        int err = WSAStartup( wVersionRequested, &wsaData );
+        WSAStartup( wVersionRequested, &wsaData );
 #endif
-#ifdef WITH_SSL
+#ifdef HAVE_SSL
         sslStream  = NULL;
         sslContext = NULL;
            CRYPTO_set_locking_callback(cryptoLockCallback);
@@ -1390,7 +1391,7 @@ bool TcpSocket::connect(const std::string &hostnameArg, int portnoArg)
 
 
 
-#ifdef WITH_SSL
+#ifdef HAVE_SSL
 /*
 static int password_cb(char *buf, int bufLen, int rwflag, void *userdata)
 {
@@ -1425,7 +1426,7 @@ static void infoCallback(const SSL *ssl, int where, int ret)
 
 bool TcpSocket::startTls()
 {
-#ifdef WITH_SSL
+#ifdef HAVE_SSL
     sslStream  = NULL;
     sslContext = NULL;
 
@@ -1476,7 +1477,7 @@ bool TcpSocket::startTls()
         }
 
     sslEnabled = true;
-#endif /*WITH_SSL*/
+#endif /*HAVE_SSL*/
     return true;
 }
 
@@ -1537,7 +1538,7 @@ bool TcpSocket::disconnect()
 {
     bool ret  = true;
     connected = false;
-#ifdef WITH_SSL
+#ifdef HAVE_SSL
     if (sslEnabled)
         {
         if (sslStream)
@@ -1560,7 +1561,7 @@ bool TcpSocket::disconnect()
         }
     sslStream  = NULL;
     sslContext = NULL;
-#endif /*WITH_SSL*/
+#endif /*HAVE_SSL*/
 
 #ifdef __WIN32__
     closesocket(sock);
@@ -1600,7 +1601,7 @@ long TcpSocket::available()
 #endif
     if (count<=0 && sslEnabled)
         {
-#ifdef WITH_SSL
+#ifdef HAVE_SSL
         return SSL_pending(sslStream);
 #endif
         }
@@ -1620,7 +1621,7 @@ bool TcpSocket::write(int ch)
 
     if (sslEnabled)
         {
-#ifdef WITH_SSL
+#ifdef HAVE_SSL
         int r = SSL_write(sslStream, &c, 1);
         if (r<=0)
             {
@@ -1652,13 +1653,12 @@ bool TcpSocket::write(char *str)
         printf("write(str): socket closed\n");
         return false;
         }
-    unsigned char *s = (unsigned char *)str;
     int len = strlen(str);
 
     if (sslEnabled)
         {
-#ifdef WITH_SSL
-        int r = SSL_write(sslStream, s, len);
+#ifdef HAVE_SSL
+        int r = SSL_write(sslStream, (unsigned char *)str, len);
         if (r<=0)
             {
             switch(SSL_get_error(sslStream, r))
@@ -1672,7 +1672,7 @@ bool TcpSocket::write(char *str)
         }
     else
         {
-        if (send(sock, s, len, 0) < 0)
+        if (send(sock, str, len, 0) < 0)
         //if (send(sock, &c, 1, 0) < 0)
             {
             printf("write: could not send data\n");
@@ -1716,7 +1716,7 @@ int TcpSocket::read()
     unsigned char ch;
     if (sslEnabled)
         {
-#ifdef WITH_SSL
+#ifdef HAVE_SSL
         if (!sslStream)
             return -1;
         int r = SSL_read(sslStream, &ch, 1);
@@ -2459,7 +2459,7 @@ static int strIndex(const DOMString &str, char *key)
 }
 
 
-static DOMString toXml(const DOMString &str)
+DOMString XmppClient::toXml(const DOMString &str)
 {
     return Parser::encode(str);
 }
@@ -2640,18 +2640,18 @@ static bool isGroupChat(Element *root)
 
 
 
-static bool getGroupIds(const DOMString &jid,
-             DOMString &groupJid, DOMString &userNick)
+static bool parseJid(const DOMString &fullJid,
+             DOMString &jid, DOMString &resource)
 {
-    int p = strIndex(jid, "/");
+    int p = strIndex(fullJid, "/");
     if (p < 0)
         {
-        groupJid = jid;
-        userNick = "";
+        jid = fullJid;
+        resource = "";
         return true;
         }
-    groupJid = jid.substr(0, p);
-    userNick = jid.substr(p+1, jid.size()-p-1);
+    jid = fullJid.substr(0, p);
+    resource = fullJid.substr(p+1, fullJid.size()-p-1);
     return true;
 }
 
@@ -2710,12 +2710,12 @@ bool XmppClient::processMessage(Element *root)
         {
         DOMString fromGid;
         DOMString fromNick;
-        getGroupIds(from, fromGid, fromNick);
+        parseJid(from, fromGid, fromNick);
         //printf("fromGid:%s  fromNick:%s\n",
         //        fromGid.c_str(), fromNick.c_str());
         DOMString toGid;
         DOMString toNick;
-        getGroupIds(to, toGid, toNick);
+        parseJid(to, toGid, toNick);
         //printf("toGid:%s  toNick:%s\n",
         //        toGid.c_str(), toNick.c_str());
 
@@ -2762,7 +2762,7 @@ bool XmppClient::processMessage(Element *root)
 bool XmppClient::processPresence(Element *root)
 {
 
-    DOMString from        = root->getTagAttribute("presence", "from");
+    DOMString fullJid     = root->getTagAttribute("presence", "from");
     DOMString to          = root->getTagAttribute("presence", "to");
     DOMString presenceStr = root->getTagAttribute("presence", "type");
     bool presence = true;
@@ -2775,7 +2775,7 @@ bool XmppClient::processPresence(Element *root)
         {
         DOMString fromGid;
         DOMString fromNick;
-        getGroupIds(from, fromGid, fromNick);
+        parseJid(fullJid, fromGid, fromNick);
         //printf("fromGid:%s  fromNick:%s\n",
         //        fromGid.c_str(), fromNick.c_str());
         DOMString item_jid = root->getTagAttribute("item", "jid");
@@ -2784,7 +2784,8 @@ bool XmppClient::processPresence(Element *root)
             if (presence)
                 {
                 groupChatCreate(fromGid);
-                groupChatUserAdd(fromGid, fromNick);
+                groupChatUserAdd(fromGid, fromNick, "");
+                groupChatUserShow(fromGid, fromNick, "available");
 
                 XmppEvent event(XmppEvent::EVENT_MUC_JOIN);
                 event.setGroup(fromGid);
@@ -2808,12 +2809,15 @@ bool XmppClient::processPresence(Element *root)
                 dispatchXmppEvent(event);
                 }
             }
-        else
+        else // someone else
             {
             if (presence)
-                groupChatUserAdd(fromGid, fromNick);
+                {
+                groupChatUserAdd(fromGid, fromNick, "");
+                }
             else
-                groupChatUserDelete(fromGid, fromNick);
+                groupChatUserDelete(fromGid, fromNick);               
+            groupChatUserShow(fromGid, fromNick, show);
             XmppEvent event(XmppEvent::EVENT_MUC_PRESENCE);
             event.setGroup(fromGid);
             event.setFrom(fromNick);
@@ -2825,8 +2829,13 @@ bool XmppClient::processPresence(Element *root)
         }
     else
         {
+        DOMString shortJid;
+        DOMString dummy;
+        parseJid(fullJid, shortJid, dummy);
+        rosterShow(shortJid, show); //users in roster do not have resource
+        
         XmppEvent event(XmppEvent::EVENT_PRESENCE);
-        event.setFrom(from);
+        event.setFrom(fullJid);
         event.setPresence(presence);
         event.setShow(show);
         event.setStatus(status);
@@ -3619,11 +3628,6 @@ bool XmppClient::createSession()
     //We are now done with the synchronous handshaking.  Let's go into
     //async mode
 
-    fmt =
-     "<presence/>\n";
-    if (!write(fmt))
-        return false;
-
     fmt =
      "<iq type='get' id='roster%d'><query xmlns='jabber:iq:roster'/></iq>\n";
     if (!write(fmt, msgId++))
@@ -3641,6 +3645,11 @@ bool XmppClient::createSession()
     if (!write(fmt, msgId++, realm.c_str()))
         return false;
 
+    fmt =
+     "<presence/>\n";
+    if (!write(fmt))
+        return false;
+
     /*
     recbuf = readStanza();
     status("stream received: '%s'", recbuf.c_str());
@@ -3780,6 +3789,20 @@ std::vector<XmppUser> XmppClient::getRoster()
     return ros;
 }
 
+void XmppClient::rosterShow(const DOMString &jid, const DOMString &show)
+{
+    DOMString theShow = show;
+    if (theShow == "")
+        theShow = "available";
+    
+    std::vector<XmppUser>::iterator iter;
+    for (iter=roster.begin() ; iter != roster.end() ; iter++)
+        {
+        if (iter->jid == jid)
+            iter->show = theShow;
+        }
+}
+
 //#######################
 //# CHAT (individual)
 //#######################
@@ -3903,14 +3926,32 @@ void XmppClient::groupChatsClear()
  *
  */
 void XmppClient::groupChatUserAdd(const DOMString &groupJid,
-                                  const DOMString &nick)
+                                  const DOMString &nick,
+                                  const DOMString &jid)
+{
+    std::vector<XmppGroupChat *>::iterator iter;
+    for (iter=groupChats.begin() ; iter!=groupChats.end() ; iter++)
+        {
+        if ((*iter)->getGroupJid() == groupJid)
+            {
+            (*iter)->userAdd(nick, jid);
+            }
+        }
+}
+
+/**
+ *
+ */
+void XmppClient::groupChatUserShow(const DOMString &groupJid,
+                                   const DOMString &nick,
+                                   const DOMString &show)
 {
     std::vector<XmppGroupChat *>::iterator iter;
     for (iter=groupChats.begin() ; iter!=groupChats.end() ; iter++)
         {
         if ((*iter)->getGroupJid() == groupJid)
             {
-            (*iter)->userAdd("", nick);
+            (*iter)->userShow(nick, show);
             }
         }
 }
@@ -3926,7 +3967,7 @@ void XmppClient::groupChatUserDelete(const DOMString &groupJid,
         {
         if ((*iter)->getGroupJid() == groupJid)
             {
-            (*iter)->userDelete("", nick);
+            (*iter)->userDelete(nick);
             }
         }
 }
@@ -4731,7 +4772,8 @@ DOMString XmppGroupChat::getGroupJid()
 }
 
 
-void XmppGroupChat::userAdd(const DOMString &jid, const DOMString &nick)
+void XmppGroupChat::userAdd(const DOMString &nick, 
+                            const DOMString &jid)
 {
     std::vector<XmppUser>::iterator iter;
     for (iter= userList.begin() ; iter!=userList.end() ; iter++)
@@ -4743,7 +4785,21 @@ void XmppGroupChat::userAdd(const DOMString &jid, const DOMString &nick)
     userList.push_back(user);
 }
 
-void XmppGroupChat::userDelete(const DOMString &jid, const DOMString &nick)
+void XmppGroupChat::userShow(const DOMString &nick, 
+                             const DOMString &show)
+{
+    DOMString theShow = show;
+    if (theShow == "")
+        theShow = "available"; // a join message will now have a show
+    std::vector<XmppUser>::iterator iter;
+    for (iter= userList.begin() ; iter!=userList.end() ; iter++)
+        {
+        if (iter->nick == nick)
+            iter->show = theShow;
+        }
+}
+
+void XmppGroupChat::userDelete(const DOMString &nick)
 {
     std::vector<XmppUser>::iterator iter;
     for (iter= userList.begin() ; iter!=userList.end() ; )
index 574371bf721564a32d933a1997489b96428a4fe1..d938a25fd76dfc6830b2e6d8c3dc21284f16ab69 100644 (file)
@@ -357,11 +357,6 @@ public:
     XmppEventListener(const XmppEventListener &other)
         {}
 
-    /**
-     *
-     */
-    virtual void operator=(const XmppEventListener &other)
-        {}
 
     /**
      *
@@ -524,6 +519,11 @@ public:
      */
     virtual bool pause(unsigned long millis);
 
+    /**
+     *
+     */
+    DOMString toXml(const DOMString &str);
+
     //###########################
     //# CONNECTION
     //###########################
@@ -685,7 +685,11 @@ public:
      */
     std::vector<XmppUser> getRoster();
 
-
+    /**
+     *
+     */
+    virtual void rosterShow(const DOMString &jid, const DOMString &show);
+    
     //#######################
     //# CHAT (individual)
     //#######################
@@ -734,7 +738,14 @@ public:
      *
      */
     virtual void groupChatUserAdd(const DOMString &groupJid,
-                                  const DOMString &nick);
+                                  const DOMString &nick,
+                                  const DOMString &jid);
+    /**
+     *
+     */
+    virtual void groupChatUserShow(const DOMString &groupJid,
+                                   const DOMString &nick,
+                                   const DOMString &show);
 
     /**
      *
@@ -746,7 +757,7 @@ public:
      *
      */
     virtual std::vector<XmppUser>
-          groupChatGetUserList(const DOMString &groupJid);
+          XmppClient::groupChatGetUserList(const DOMString &groupJid);
 
     /**
      *
@@ -983,12 +994,18 @@ public:
     /**
      *
      */
-    virtual void userAdd(const DOMString &jid, const DOMString &nick);
+    virtual void userAdd(const DOMString &nick, 
+                         const DOMString &jid);
+    /**
+     *
+     */
+    virtual void userShow(const DOMString &nick,
+                          const DOMString &show);
 
     /**
      *
      */
-    virtual void userDelete(const DOMString &jid, const DOMString &nick);
+    virtual void userDelete(const DOMString &nick);
 
     /**
      *