about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-10-28 18:51:13 +0000
committerJames Booth <boothj5@gmail.com>2012-10-28 18:51:13 +0000
commit15cdc69f310886a213c104595d83109744ca2f65 (patch)
tree4f106efb8858f3fc376eb57db4462f96157d06a4 /src
parentcd56134ebb8f0789c776477a1a1b6e337e6d0e70 (diff)
downloadprofani-tty-15cdc69f310886a213c104595d83109744ca2f65.tar.gz
Added basic subscriptions
Diffstat (limited to 'src')
-rw-r--r--src/command.c36
-rw-r--r--src/jabber.c27
-rw-r--r--src/jabber.h1
3 files changed, 64 insertions, 0 deletions
diff --git a/src/command.c b/src/command.c
index e9f40409..7892e78a 100644
--- a/src/command.c
+++ b/src/command.c
@@ -82,6 +82,7 @@ static gboolean _cmd_prefs(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_who(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_connect(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_disconnect(const char * const inp, struct cmd_help_t help);
+static gboolean _cmd_sub(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_msg(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_tiny(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_close(const char * const inp, struct cmd_help_t help);
@@ -178,6 +179,16 @@ static struct cmd_t main_commands[] =
           "Example : /msg boothj5@gmail.com Hey, here's a message!",
           NULL } } },
 
+    { "/sub",
+        _cmd_sub,
+        { "/sub user@host", "Subscribe to presence notifications of user.",
+        { "/sub user@host",
+          "------------------",
+          "Send a subscription request to the user to be informed of their presence.",
+          "",
+          "Example: /sub myfriend@jabber.org",
+          NULL  } } },
+
     { "/tiny",
         _cmd_tiny,
         { "/tiny url", "Send url as tinyurl in current chat.",
@@ -692,6 +703,31 @@ _cmd_connect(const char * const inp, struct cmd_help_t help)
 }
 
 static gboolean
+_cmd_sub(const char * const inp, struct cmd_help_t help)
+{
+    gboolean result = FALSE;
+    jabber_conn_status_t conn_status = jabber_get_connection_status();
+
+    if (conn_status != JABBER_CONNECTED) {
+        cons_show("You are currently not connected.");
+        result = TRUE;
+    } else if (strlen(inp) < 6) {
+        cons_show("Usage: %s", help.usage);
+        result = TRUE;
+    } else {
+        char *user, *lower;
+        user = strndup(inp+5, strlen(inp)-5);
+        lower = g_utf8_strdown(user, -1);
+    
+        jabber_subscribe(lower);
+
+        result = TRUE;
+    }
+
+    return result;
+}
+
+static gboolean
 _cmd_disconnect(const char * const inp, struct cmd_help_t help)
 {
     if (jabber_get_connection_status() == JABBER_CONNECTED) {
diff --git a/src/jabber.c b/src/jabber.c
index 4913618e..f3e3fa27 100644
--- a/src/jabber.c
+++ b/src/jabber.c
@@ -185,6 +185,19 @@ jabber_roster_request(void)
 }
 
 void
+jabber_subscribe(const char * const recipient)
+{
+    xmpp_stanza_t *presence;
+
+    presence = xmpp_stanza_new(jabber_conn.ctx);
+    xmpp_stanza_set_name(presence, "presence");
+    xmpp_stanza_set_type(presence, "subscribe");
+    xmpp_stanza_set_attribute(presence, "to", recipient); 
+    xmpp_send(jabber_conn.conn, presence);
+    xmpp_stanza_release(presence);
+}
+
+void
 jabber_update_presence(jabber_presence_t status, const char * const msg)
 {
     jabber_conn.presence = status;
@@ -435,6 +448,20 @@ _presence_handler(xmpp_conn_t * const conn,
     char *short_from = strtok(from, "/");
     char *type = xmpp_stanza_get_attribute(stanza, "type");
 
+    if (type != NULL) {
+        if (strcmp(type, "subscribe") == 0) {
+            xmpp_stanza_t *presence;
+
+            presence = xmpp_stanza_new(jabber_conn.ctx);
+            xmpp_stanza_set_name(presence, "presence");
+            xmpp_stanza_set_type(presence, "subscribed");
+            xmpp_stanza_set_attribute(presence, "to", short_from); 
+            xmpp_send(jabber_conn.conn, presence);
+            xmpp_stanza_release(presence);
+            return 1;
+        }
+    }
+
     char *show_str, *status_str;
 
     xmpp_stanza_t *show = xmpp_stanza_get_child_by_name(stanza, "show");
diff --git a/src/jabber.h b/src/jabber.h
index d12d00b3..f1ca99e5 100644
--- a/src/jabber.h
+++ b/src/jabber.h
@@ -46,6 +46,7 @@ jabber_conn_status_t jabber_connect(const char * const user,
 void jabber_disconnect(void);
 void jabber_roster_request(void);
 void jabber_process_events(void);
+void jabber_subscribe(const char * const recipient);
 void jabber_send(const char * const msg, const char * const recipient);
 void jabber_update_presence(jabber_presence_t status, const char * const msg);
 const char * jabber_get_jid(void);