comparison mcabber/mcabber/xmpp.c @ 1682:d1e8fb14ce2d

Destroy lm connection object on disconnect
author Myhailo Danylenko <isbear@ukrpost.net>
date Tue, 19 Jan 2010 09:53:12 +0200
parents 41c26b7d2890
children b09f82f61745
comparison
equal deleted inserted replaced
1681:687f6b515d87 1682:d1e8fb14ce2d
41 #include "utils.h" 41 #include "utils.h"
42 #include "main.h" 42 #include "main.h"
43 43
44 #define RECONNECTION_TIMEOUT 60L 44 #define RECONNECTION_TIMEOUT 60L
45 45
46 LmConnection* lconnection; 46 LmConnection* lconnection = NULL;
47 static guint AutoConnection; 47 static guint AutoConnection;
48 48
49 inline void update_last_use(void); 49 inline void update_last_use(void);
50 inline gboolean xmpp_reconnect(); 50 inline gboolean xmpp_reconnect();
51 51
98 { 98 {
99 LmMessageNode *query, *y; 99 LmMessageNode *query, *y;
100 LmMessage *iq; 100 LmMessage *iq;
101 char *cleanjid; 101 char *cleanjid;
102 102
103 if (!lm_connection_is_authenticated(lconnection)) return; 103 if (!lconnection || !lm_connection_is_authenticated(lconnection))
104 return;
104 105
105 cleanjid = jidtodisp(bjid); // Stripping resource, just in case... 106 cleanjid = jidtodisp(bjid); // Stripping resource, just in case...
106 107
107 // We don't check if the jabber user already exists in the roster, 108 // We don't check if the jabber user already exists in the roster,
108 // because it allows to re-ask for notification. 109 // because it allows to re-ask for notification.
136 { 137 {
137 LmMessage *iq; 138 LmMessage *iq;
138 LmMessageNode *x; 139 LmMessageNode *x;
139 char *cleanjid; 140 char *cleanjid;
140 141
141 if (!lm_connection_is_authenticated(lconnection)) return; 142 if (!lconnection || !lm_connection_is_authenticated(lconnection))
143 return;
142 144
143 // XXX We should check name's and group's correctness 145 // XXX We should check name's and group's correctness
144 146
145 cleanjid = jidtodisp(bjid); // Stripping resource, just in case... 147 cleanjid = jidtodisp(bjid); // Stripping resource, just in case...
146 148
166 { 168 {
167 LmMessageNode *y, *z; 169 LmMessageNode *y, *z;
168 LmMessage *iq; 170 LmMessage *iq;
169 char *cleanjid; 171 char *cleanjid;
170 172
171 if (!lm_connection_is_authenticated(lconnection)) return; 173 if (!lconnection || !lm_connection_is_authenticated(lconnection))
174 return;
172 175
173 cleanjid = jidtodisp(bjid); // Stripping resource, just in case... 176 cleanjid = jidtodisp(bjid); // Stripping resource, just in case...
174 177
175 // If the current buddy is an agent, unsubscribe from it 178 // If the current buddy is an agent, unsubscribe from it
176 if (roster_gettype(cleanjid) == ROSTER_TYPE_AGENT) { 179 if (roster_gettype(cleanjid) == ROSTER_TYPE_AGENT) {
301 gchar *enc = NULL; 304 gchar *enc = NULL;
302 305
303 if (encrypted) 306 if (encrypted)
304 *encrypted = 0; 307 *encrypted = 0;
305 308
306 if (!lm_connection_is_authenticated(lconnection)) 309 if (!lconnection || !lm_connection_is_authenticated(lconnection))
307 return; 310 return;
308 311
309 if (!text && type == ROSTER_TYPE_USER) 312 if (!text && type == ROSTER_TYPE_USER)
310 return; 313 return;
311 314
482 GSList *sl_buddy; 485 GSList *sl_buddy;
483 const char *chattag; 486 const char *chattag;
484 char *rjid, *fjid = NULL; 487 char *rjid, *fjid = NULL;
485 struct jep0085 *jep85 = NULL; 488 struct jep0085 *jep85 = NULL;
486 489
487 if (!lm_connection_is_authenticated(lconnection)) return; 490 if (!lconnection || !lm_connection_is_authenticated(lconnection))
491 return;
488 492
489 sl_buddy = roster_find(bjid, jidsearch, ROSTER_TYPE_USER); 493 sl_buddy = roster_find(bjid, jidsearch, ROSTER_TYPE_USER);
490 494
491 // If we have a resource name, we use it. Else we use NULL, 495 // If we have a resource name, we use it. Else we use NULL,
492 // which hopefully will give us the most likely resource. 496 // which hopefully will give us the most likely resource.
540 char *rname, *barejid; 544 char *rname, *barejid;
541 GSList *sl_buddy; 545 GSList *sl_buddy;
542 struct jep0022 *jep22 = NULL; 546 struct jep0022 *jep22 = NULL;
543 guint jep22_state; 547 guint jep22_state;
544 548
545 if (!lm_connection_is_authenticated(lconnection)) return; 549 if (!lconnection || !lm_connection_is_authenticated(lconnection))
550 return;
546 551
547 rname = strchr(fjid, JID_RESOURCE_SEPARATOR); 552 rname = strchr(fjid, JID_RESOURCE_SEPARATOR);
548 barejid = jidtodisp(fjid); 553 barejid = jidtodisp(fjid);
549 sl_buddy = roster_find(barejid, jidsearch, ROSTER_TYPE_USER); 554 sl_buddy = roster_find(barejid, jidsearch, ROSTER_TYPE_USER);
550 g_free(barejid); 555 g_free(barejid);
838 scr_LogPrint(LPRINT_LOGNORM, "Authentication failed"); 843 scr_LogPrint(LPRINT_LOGNORM, "Authentication failed");
839 } 844 }
840 845
841 gboolean xmpp_reconnect() 846 gboolean xmpp_reconnect()
842 { 847 {
843 if (!lm_connection_is_authenticated(lconnection)) 848 if (!lconnection)
844 xmpp_connect(); 849 xmpp_connect();
845 return FALSE; 850 return FALSE;
846 } 851 }
847 852
848 static void _try_to_reconnect(void) 853 static void _try_to_reconnect(void)
849 { 854 {
855 xmpp_disconnect();
850 if (AutoConnection) 856 if (AutoConnection)
851 g_timeout_add_seconds(RECONNECTION_TIMEOUT, xmpp_reconnect, NULL); 857 g_timeout_add_seconds(RECONNECTION_TIMEOUT, xmpp_reconnect, NULL);
852 } 858 }
853 859
854 static void connection_open_cb(LmConnection *connection, gboolean success, 860 static void connection_open_cb(LmConnection *connection, gboolean success,
1593 unsigned int port; 1599 unsigned int port;
1594 unsigned int ping; 1600 unsigned int ping;
1595 LmMessageHandler *handler; 1601 LmMessageHandler *handler;
1596 GError *error = NULL; 1602 GError *error = NULL;
1597 1603
1598 if (lconnection && lm_connection_is_open(lconnection)) 1604 xmpp_disconnect();
1599 xmpp_disconnect();
1600 1605
1601 servername = settings_opt_get("server"); 1606 servername = settings_opt_get("server");
1602 userjid = settings_opt_get("jid"); 1607 userjid = settings_opt_get("jid");
1603 password = settings_opt_get("password"); 1608 password = settings_opt_get("password");
1604 resource = settings_opt_get("resource"); 1609 resource = settings_opt_get("resource");
1766 lm_message_node_set_attribute(y, "ver", ver); 1771 lm_message_node_set_attribute(y, "ver", ver);
1767 } 1772 }
1768 1773
1769 void xmpp_disconnect(void) 1774 void xmpp_disconnect(void)
1770 { 1775 {
1771 if (!lconnection || !lm_connection_is_authenticated(lconnection)) 1776 if (!lconnection)
1772 return; 1777 return;
1773 1778 if (lm_connection_is_authenticated(lconnection)) {
1774 // Launch pre-disconnect internal hook 1779 // Launch pre-disconnect internal hook
1775 hook_execute_internal("hook-pre-disconnect"); 1780 hook_execute_internal("hook-pre-disconnect");
1776 // Announce it to everyone else 1781 // Announce it to everyone else
1777 xmpp_setstatus(offline, NULL, "", FALSE); 1782 xmpp_setstatus(offline, NULL, "", FALSE);
1778 lm_connection_close(lconnection, NULL); 1783 }
1784 if (lm_connection_is_open(lconnection))
1785 lm_connection_close(lconnection, NULL);
1786 lm_connection_unref(lconnection);
1787 lconnection = NULL;
1779 } 1788 }
1780 1789
1781 void xmpp_setstatus(enum imstatus st, const char *recipient, const char *msg, 1790 void xmpp_setstatus(enum imstatus st, const char *recipient, const char *msg,
1782 int do_not_sign) 1791 int do_not_sign)
1783 { 1792 {
1803 } 1812 }
1804 1813
1805 // Only send the packet if we're online. 1814 // Only send the packet if we're online.
1806 // (But we want to update internal status even when disconnected, 1815 // (But we want to update internal status even when disconnected,
1807 // in order to avoid some problems during network failures) 1816 // in order to avoid some problems during network failures)
1808 if (lm_connection_is_authenticated(lconnection)) { 1817 if (lconnection && lm_connection_is_authenticated(lconnection)) {
1809 const char *s_msg = (st != invisible ? msg : NULL); 1818 const char *s_msg = (st != invisible ? msg : NULL);
1810 m = lm_message_new_presence(st, recipient, s_msg); 1819 m = lm_message_new_presence(st, recipient, s_msg);
1811 insert_entity_capabilities(m->node, st); // Entity Capabilities (XEP-0115) 1820 insert_entity_capabilities(m->node, st); // Entity Capabilities (XEP-0115)
1812 #ifdef HAVE_GPGME 1821 #ifdef HAVE_GPGME
1813 if (!do_not_sign && gpg_enabled()) { 1822 if (!do_not_sign && gpg_enabled()) {
1826 } 1835 }
1827 1836
1828 // If we didn't change our _global_ status, we are done 1837 // If we didn't change our _global_ status, we are done
1829 if (recipient) return; 1838 if (recipient) return;
1830 1839
1831 if (lm_connection_is_authenticated(lconnection)) { 1840 if (lconnection && lm_connection_is_authenticated(lconnection)) {
1832 // Send presence to chatrooms 1841 // Send presence to chatrooms
1833 if (st != invisible) { 1842 if (st != invisible) {
1834 struct T_presence room_presence; 1843 struct T_presence room_presence;
1835 room_presence.st = st; 1844 room_presence.st = st;
1836 room_presence.msg = msg; 1845 room_presence.msg = msg;
2047 } 2056 }
2048 2057
2049 if (!changed) 2058 if (!changed)
2050 return; 2059 return;
2051 2060
2052 if (lm_connection_is_authenticated(lconnection)) 2061 if (lconnection && lm_connection_is_authenticated(lconnection))
2053 send_storage(bookmarks); 2062 send_storage(bookmarks);
2054 else 2063 else
2055 scr_LogPrint(LPRINT_LOGNORM, 2064 scr_LogPrint(LPRINT_LOGNORM,
2056 "Warning: you're not connected to the server."); 2065 "Warning: you're not connected to the server.");
2057 } 2066 }
2188 } 2197 }
2189 2198
2190 if (!changed) 2199 if (!changed)
2191 return; 2200 return;
2192 2201
2193 if (lm_connection_is_authenticated(lconnection)) 2202 if (lconnection && lm_connection_is_authenticated(lconnection))
2194 send_storage(rosternotes); 2203 send_storage(rosternotes);
2195 else 2204 else
2196 scr_LogPrint(LPRINT_LOGNORM, 2205 scr_LogPrint(LPRINT_LOGNORM,
2197 "Warning: you're not connected to the server."); 2206 "Warning: you're not connected to the server.");
2198 } 2207 }