diff --git a/src/XMPP/Component.c b/src/XMPP/Component.c index 26114ba..6d186f3 100644 --- a/src/XMPP/Component.c +++ b/src/XMPP/Component.c @@ -76,6 +76,7 @@ XMPPInitialiseCompStream(char *host, int port) comp = Malloc(sizeof(*comp)); comp->host = StrDuplicate(host); comp->stream = stream; + pthread_mutex_init(&comp->write_lock, NULL); return comp; } @@ -112,6 +113,8 @@ XMPPAuthenticateCompStream(XMPPComponent *comp, char *shared) return false; } + pthread_mutex_lock(&comp->write_lock); + stream = comp->stream; as = comp->host; StreamPrintf(stream, @@ -178,6 +181,7 @@ XMPPAuthenticateCompStream(XMPPComponent *comp, char *shared) Free(handshake); end: XMLFreeLexer(sax); + pthread_mutex_unlock(&comp->write_lock); return ret; } @@ -188,6 +192,7 @@ XMPPEndCompStream(XMPPComponent *comp) { return; } + pthread_mutex_destroy(&comp->write_lock); StreamClose(comp->stream); Free(comp->host); Free(comp); diff --git a/src/XMPP/MUC.c b/src/XMPP/MUC.c index 299a0fb..196714a 100644 --- a/src/XMPP/MUC.c +++ b/src/XMPP/MUC.c @@ -17,6 +17,9 @@ XMPPQueryMUC(XMPPComponent *jabber, char *muc, MUCInfo *out) { return false; } + + pthread_mutex_lock(&jabber->write_lock); + iq_query = XMLCreateTag("iq"); query = XMLCreateTag("query"); @@ -47,6 +50,7 @@ XMPPQueryMUC(XMPPComponent *jabber, char *muc, MUCInfo *out) { XMLFreeElement(iq_query); ParseeWakeupThread(); + pthread_mutex_unlock(&jabber->write_lock); return false; } query = XMLookForUnique(iq_query, "query"); @@ -58,6 +62,7 @@ XMPPQueryMUC(XMPPComponent *jabber, char *muc, MUCInfo *out) { XMLFreeElement(iq_query); ParseeWakeupThread(); + pthread_mutex_unlock(&jabber->write_lock); return false; } @@ -74,6 +79,7 @@ XMPPQueryMUC(XMPPComponent *jabber, char *muc, MUCInfo *out) } } /* Wake it up once we're done */ + pthread_mutex_unlock(&jabber->write_lock); ParseeWakeupThread(); return true; } diff --git a/src/XMPP/Stanza.c b/src/XMPP/Stanza.c index b0d6cb2..fdf6949 100644 --- a/src/XMPP/Stanza.c +++ b/src/XMPP/Stanza.c @@ -15,6 +15,8 @@ XMPPSendPlain(XMPPComponent *comp, char *fr, char *to, char *msg, char *type) { return; } + + pthread_mutex_lock(&comp->write_lock); message = XMLCreateTag("message"); XMLAddAttr(message, "from", (from = StrConcat(3, fr, "@", comp->host))); @@ -56,6 +58,8 @@ XMPPSendPlain(XMPPComponent *comp, char *fr, char *to, char *msg, char *type) StreamFlush(comp->stream); XMLFreeElement(message); Free(from); + + pthread_mutex_unlock(&comp->write_lock); } void XMPPSendMUC(XMPPComponent *comp, char *fr, char *as, char *to, char *msg, char *type) @@ -67,6 +71,8 @@ XMPPSendMUC(XMPPComponent *comp, char *fr, char *as, char *to, char *msg, char * return; } + pthread_mutex_lock(&comp->write_lock); + message = XMLCreateTag("message"); XMLAddAttr(message, "from", (from = StrConcat(5, fr, "@", comp->host, "/", as))); @@ -86,6 +92,8 @@ XMPPSendMUC(XMPPComponent *comp, char *fr, char *as, char *to, char *msg, char * XMLFreeElement(message); Free(from); Free(id); + + pthread_mutex_unlock(&comp->write_lock); } void XMPPJoinMUC(XMPPComponent *comp, char *fr, char *muc) @@ -97,6 +105,8 @@ XMPPJoinMUC(XMPPComponent *comp, char *fr, char *muc) return; } + pthread_mutex_lock(&comp->write_lock); + presence = XMLCreateTag("presence"); x = XMLCreateTag("x"); XMLAddAttr(presence, "from", (from = StrConcat(3, fr, "@", comp->host))); @@ -112,6 +122,8 @@ XMPPJoinMUC(XMPPComponent *comp, char *fr, char *muc) XMLFreeElement(presence); Free(from); Free(id); + + pthread_mutex_unlock(&comp->write_lock); } void XMPPKillThread(XMPPComponent *jabber, char *killer) @@ -124,6 +136,8 @@ XMPPKillThread(XMPPComponent *jabber, char *killer) return; } + pthread_mutex_lock(&jabber->write_lock); + from = StrConcat(3, killer, "@", jabber->host); message = XMLCreateTag("message"); XMLAddAttr(message, "from", from); @@ -136,6 +150,8 @@ XMPPKillThread(XMPPComponent *jabber, char *killer) StreamFlush(jabber->stream); XMLFreeElement(message); Free(from); + + pthread_mutex_unlock(&jabber->write_lock); } bool XMPPIsKiller(XMLElement *stanza) diff --git a/src/include/XMPP.h b/src/include/XMPP.h index 8ef17f3..22b1111 100644 --- a/src/include/XMPP.h +++ b/src/include/XMPP.h @@ -3,9 +3,14 @@ #include +#include + #include typedef struct XMPPComponent { + /* A lock for all write operations */ + pthread_mutex_t write_lock; + char *host; Stream *stream; } XMPPComponent;