diff options
author | dequis <dx@dxzone.com.ar> | 2015-11-23 14:49:09 -0300 |
---|---|---|
committer | dequis <dx@dxzone.com.ar> | 2015-11-23 14:49:09 -0300 |
commit | 9c8dbc75d416c8867be20ccf3732303163e620ce (patch) | |
tree | a714f5af6ca4a17db97981036c0f79ae5ffda7fd /protocols/jabber/hipchat.c | |
parent | c34247d0b3111f16dae1a52d831df0d61c03ee35 (diff) |
hipchat: 'chat add hipchat "channel name"' now tries to guess the JID
It's basically prepending the organization id, appending the default MUC
host from the success packet, and generating a slug based on the name
for the middle part, which is replacing a few characters with
underscores and doing a unicode aware lowercasing.
Includes tests, which are useless other than validating the initial
implementation with the test vectors that i already tested manually.
Guaranteed to detect zero breakages in the future. Good test code.
Diffstat (limited to 'protocols/jabber/hipchat.c')
-rw-r--r-- | protocols/jabber/hipchat.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/protocols/jabber/hipchat.c b/protocols/jabber/hipchat.c index 66675885..49e7c895 100644 --- a/protocols/jabber/hipchat.c +++ b/protocols/jabber/hipchat.c @@ -42,6 +42,8 @@ xt_status hipchat_handle_success(struct im_connection *ic, struct xt_node *node) *sep = '/'; } + jd->muc_host = g_strdup(xt_find_attr(node, "muc_host")); + /* Hipchat's auth doesn't expect a restart here */ jd->flags &= ~JFLAG_STREAM_RESTART; @@ -91,3 +93,51 @@ xt_status jabber_parse_hipchat_profile(struct im_connection *ic, struct xt_node return XT_HANDLED; } + +/* Returns a newly allocated string that tries to match the "slug" part of the JID using an + * approximation of the method used by the server. This might fail in some rare conditions + * (old JIDs generated a different way, locale settings unicode, etc) */ +char *hipchat_make_channel_slug(const char *name) +{ + char *lower; + char *new = g_malloc(strlen(name) + 1); + int i = 0; + + do { + if (*name == ' ') { + new[i++] = '_'; + } else if (*name && !strchr("\"&'/:<>@", *name)) { + new[i++] = *name; + } + } while (*(name++)); + + new[i] = '\0'; + + lower = g_utf8_strdown(new, -1); + g_free(new); + + return lower; +} + +char *hipchat_guess_channel_name(struct im_connection *ic, const char *name) +{ + struct jabber_data *jd = ic->proto_data; + char *slug, *retval, *underscore; + + if (!(underscore = strchr(jd->username, '_')) || !jd->muc_host) { + return NULL; + } + + slug = hipchat_make_channel_slug(name); + + /* Get the organization ID from the username, before the underscore */ + *underscore = '\0'; + + retval = g_strdup_printf("%s_%s@%s", jd->username, slug, jd->muc_host); + + *underscore = '_'; + + g_free(slug); + + return retval; +} |