1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Added basic subscriptions

This commit is contained in:
James Booth 2012-10-28 18:51:13 +00:00
parent cd56134ebb
commit 15cdc69f31
3 changed files with 64 additions and 0 deletions

View File

@ -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.",
@ -691,6 +702,31 @@ _cmd_connect(const char * const inp, struct cmd_help_t help)
return result;
}
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)
{

View File

@ -184,6 +184,19 @@ jabber_roster_request(void)
xmpp_stanza_release(iq);
}
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)
{
@ -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");

View File

@ -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);