Keep the status and status messages when we're disconnected
Improve the automatic reconnection after a network failure.
- AutoConnection is only set to false when the user disconnects
explicitly (i.e. in do_disconnect()).
- AutoConnection is set to TRUE after the 1st successful connection.
- In jb_setstatus(), update the status message and the "wanted status"
even in offline mode. It helps with auto-away.
--- a/mcabber/src/commands.c Sun Nov 12 15:55:04 2006 +0100
+++ b/mcabber/src/commands.c Sun Nov 12 22:25:14 2006 +0100
@@ -2371,6 +2371,7 @@
static void do_disconnect(char *arg)
{
jb_disconnect();
+ AutoConnection = false;
}
static void do_help(char *arg)
--- a/mcabber/src/jab_iq.c Sun Nov 12 15:55:04 2006 +0100
+++ b/mcabber/src/jab_iq.c Sun Nov 12 22:25:14 2006 +0100
@@ -679,7 +679,7 @@
// Post-login stuff
// Usually we request the roster only at connection time
// so we should be there only once. (That's ugly, however)
- jb_setstatus(available, NULL, NULL);
+ jb_setprevstatus();
}
}
--- a/mcabber/src/jabglue.c Sun Nov 12 15:55:04 2006 +0100
+++ b/mcabber/src/jabglue.c Sun Nov 12 22:25:14 2006 +0100
@@ -39,16 +39,17 @@
#define RECONNECTION_TIMEOUT 60L
jconn jc;
+guint AutoConnection;
enum enum_jstate jstate;
char imstatus2char[imstatus_size+1] = {
'_', 'o', 'i', 'f', 'd', 'n', 'a', '\0'
};
-static bool AutoConnection;
static time_t LastPingTime;
static unsigned int KeepaliveDelay;
static enum imstatus mystatus = offline;
+static enum imstatus mywantedstatus = available;
static gchar *mystatusmsg;
static unsigned char online;
@@ -125,9 +126,6 @@
jab_start(jc);
}
- if (jc)
- AutoConnection = true;
-
return jc;
}
@@ -140,6 +138,11 @@
jb_setstatus(offline, NULL, "");
// End the XML flow
jb_send_raw("</stream:stream>");
+ /*
+ // Free status message
+ g_free(mystatusmsg);
+ mystatusmsg = NULL;
+ */
}
// Announce it to the user
@@ -147,7 +150,6 @@
jab_delete(jc);
jc = NULL;
- AutoConnection = false;
}
inline void jb_reset_keepalive()
@@ -181,16 +183,16 @@
static time_t disconnection_timestamp = 0L;
time_t now;
+ // Maybe we're voluntarily offline...
+ if (!AutoConnection)
+ return;
+
// Are we totally disconnected?
if (jc && jc->state != JCONN_STATE_OFF) {
disconnection_timestamp = 0L;
return;
}
- // Maybe we're voluntarily offline...
- if (!AutoConnection)
- return;
-
time(&now);
if (!disconnection_timestamp) {
disconnection_timestamp = now;
@@ -404,8 +406,6 @@
{
xmlnode x;
- if (!online) return;
-
if (msg) {
// The status message has been specified. We'll use it, unless it is
// "-" which is a special case (option meaning "no status message").
@@ -425,28 +425,39 @@
}
}
- x = presnew(st, recipient, (st != invisible ? msg : NULL));
- jab_send(jc, x);
- xmlnode_free(x);
+ // Only send the packet if we're online.
+ // (But we want to update internal status even when disconnected,
+ // in order to avoid some problems during network failures)
+ if (online) {
+ x = presnew(st, recipient, (st != invisible ? msg : NULL));
+ jab_send(jc, x);
+ xmlnode_free(x);
+ }
// If we didn't change our _global_ status, we are done
if (recipient) return;
- // Send presence to chatrooms
- if (st != invisible) {
- struct T_presence room_presence;
- room_presence.st = st;
- room_presence.msg = msg;
- foreach_buddy(ROSTER_TYPE_ROOM, &roompresence, &room_presence);
+ if (online) {
+ // Send presence to chatrooms
+ if (st != invisible) {
+ struct T_presence room_presence;
+ room_presence.st = st;
+ room_presence.msg = msg;
+ foreach_buddy(ROSTER_TYPE_ROOM, &roompresence, &room_presence);
+ }
}
- // We'll need to update the roster if we switch to/from offline because
- // we don't know the presences of buddies when offline...
- if (mystatus == offline || st == offline)
- update_roster = TRUE;
+ if (online) {
+ // We'll need to update the roster if we switch to/from offline because
+ // we don't know the presences of buddies when offline...
+ if (mystatus == offline || st == offline)
+ update_roster = TRUE;
- hk_mystatuschange(0, mystatus, st, (st != invisible ? msg : ""));
- mystatus = st;
+ hk_mystatuschange(0, mystatus, st, (st != invisible ? msg : ""));
+ mystatus = st;
+ }
+ if (st)
+ mywantedstatus = st;
if (msg != mystatusmsg) {
g_free(mystatusmsg);
if (*msg)
@@ -459,6 +470,13 @@
scr_UpdateMainStatus(TRUE);
}
+// jb_setprevstatus()
+// Set previous status. This wrapper function is used after a disconnection.
+inline void jb_setprevstatus(void)
+{
+ jb_setstatus(mywantedstatus, NULL, mystatusmsg);
+}
+
// new_msgid()
// Generate a new id string. The caller should free it.
static char *new_msgid(void)
@@ -1372,11 +1390,11 @@
if (previous_state != JCONN_STATE_OFF)
scr_LogPrint(LPRINT_LOGNORM, "[Jabber] Not connected to the server");
+ // Sometimes the state isn't correctly updated
+ if (jc)
+ jc->state = JCONN_STATE_OFF;
online = FALSE;
mystatus = offline;
- // Free status message
- g_free(mystatusmsg);
- mystatusmsg = NULL;
// Free bookmarks
xmlnode_free(bookmarks);
bookmarks = NULL;
@@ -1399,6 +1417,8 @@
scr_LogPrint(LPRINT_LOGNORM, "[Jabber] Communication with the server "
"established");
online = TRUE;
+ // We set AutoConnection to true after the 1st successful connection
+ AutoConnection = true;
break;
case JCONN_STATE_CONNECTING:
--- a/mcabber/src/jabglue.h Sun Nov 12 15:55:04 2006 +0100
+++ b/mcabber/src/jabglue.h Sun Nov 12 22:25:14 2006 +0100
@@ -15,6 +15,7 @@
#endif
extern jconn jc;
+extern guint AutoConnection;
extern char imstatus2char[];
// Status chars: '_', 'o', 'i', 'f', 'd', 'n', 'a'
@@ -50,6 +51,7 @@
inline enum imstatus jb_getstatus(void);
inline const char *jb_getstatusmsg(void);
void jb_setstatus(enum imstatus st, const char *recipient, const char *msg);
+inline void jb_setprevstatus(void);
void jb_send_msg(const char *jid, const char *text, int type,
const char *subject, const char *id);
void jb_send_raw(const char *str);