/* * stanza.c * * Copyright (C) 2012, 2013 James Booth * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Profanity is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Profanity. If not, see . * */ #include #include #include #include #include "common.h" #include "xmpp/connection.h" #include "xmpp/stanza.h" #include "xmpp/capabilities.h" static int _field_compare(FormField *f1, FormField *f2); xmpp_stanza_t * stanza_create_chat_state(xmpp_ctx_t *ctx, const char * const recipient, const char * const state) { xmpp_stanza_t *msg, *chat_state; msg = xmpp_stanza_new(ctx); xmpp_stanza_set_name(msg, STANZA_NAME_MESSAGE); xmpp_stanza_set_type(msg, STANZA_TYPE_CHAT); xmpp_stanza_set_attribute(msg, STANZA_ATTR_TO, recipient); chat_state = xmpp_stanza_new(ctx); xmpp_stanza_set_name(chat_state, state); xmpp_stanza_set_ns(chat_state, STANZA_NS_CHATSTATES); xmpp_stanza_add_child(msg, chat_state); return msg; } xmpp_stanza_t * stanza_create_message(xmpp_ctx_t *ctx, const char * const recipient, const char * const type, const char * const message, const char * const state) { char *encoded_xml = encode_xml(message); xmpp_stanza_t *msg, *body, *text; msg = xmpp_stanza_new(ctx); xmpp_stanza_set_name(msg, STANZA_NAME_MESSAGE); xmpp_stanza_set_type(msg, type); xmpp_stanza_set_attribute(msg, STANZA_ATTR_TO, recipient); body = xmpp_stanza_new(ctx); xmpp_stanza_set_name(body, STANZA_NAME_BODY); text = xmpp_stanza_new(ctx); xmpp_stanza_set_text(text, encoded_xml); xmpp_stanza_add_child(body, text); xmpp_stanza_add_child(msg, body); if (state != NULL) { xmpp_stanza_t *chat_state = xmpp_stanza_new(ctx); xmpp_stanza_set_name(chat_state, state); xmpp_stanza_set_ns(chat_state, STANZA_NS_CHATSTATES); xmpp_stanza_add_child(msg, chat_state); } g_free(encoded_xml); return msg; } xmpp_stanza_t * stanza_create_room_join_presence(xmpp_ctx_t * const ctx, const char * const full_room_jid) { xmpp_stanza_t *presence = xmpp_stanza_new(ctx); xmpp_stanza_set_name(presence, STANZA_NAME_PRESENCE); xmpp_stanza_set_attribute(presence, STANZA_ATTR_TO, full_room_jid); xmpp_stanza_t *x = xmpp_stanza_new(ctx); xmpp_stanza_set_name(x, STANZA_NAME_X); xmpp_stanza_set_ns(x, STANZA_NS_MUC); xmpp_stanza_add_child(presence, x); return presence; } xmpp_stanza_t * stanza_create_room_newnick_presence(xmpp_ctx_t *ctx, const char * const full_room_jid) { xmpp_stanza_t *presence = xmpp_stanza_new(ctx); xmpp_stanza_set_name(presence, STANZA_NAME_PRESENCE); xmpp_stanza_set_attribute(presence, STANZA_ATTR_TO, full_room_jid); return presence; } xmpp_stanza_t * stanza_create_room_leave_presence(xmpp_ctx_t *ctx, const char * const room, const char * const nick) { GString *full_jid = g_string_new(room); g_string_append(full_jid, "/"); g_string_append(full_jid, nick); xmpp_stanza_t *presence = xmpp_stanza_new(ctx); xmpp_stanza_set_name(presence, STANZA_NAME_PRESENCE); xmpp_stanza_set_type(presence, STANZA_TYPE_UNAVAILABLE); xmpp_stanza_set_attribute(presence, STANZA_ATTR_TO, full_jid->str); g_string_free(full_jid, TRUE); return presence; } xmpp_stanza_t * stanza_create_presence(xmpp_ctx_t * const ctx) { xmpp_stanza_t *presence = xmpp_stanza_new(ctx); xmpp_stanza_set_name(presence, STANZA_NAME_PRESENCE); return presence; } xmpp_stanza_t * stanza_create_software_version_iq(xmpp_ctx_t *ctx, const char * const fulljid) { xmpp_stanza_t *iq = xmpp_stanza_new(ctx); xmpp_stanza_set_name(iq, STANZA_NAME_IQ); xmpp_stanza_set_type(iq, STANZA_TYPE_GET); xmpp_stanza_set_id(iq, "sv"); xmpp_stanza_set_attribute(iq, "to", fulljid);
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: module test.test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body bgcolor="#f0f0f8">

<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="test.html"><font color="#ffffff">test</font></a>.test</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/work/ranger/test/test.py">/home/hut/work/ranger/test/test.py</a></font></td></tr></table>
    <p><tt>Workaround&nbsp;to&nbsp;allow&nbsp;running&nbsp;single&nbsp;test&nbsp;cases&nbsp;directly</tt></p>

</body></html>
(caps_gstr, FALSE); return caps_str; } DataForm * stanza_create_form(xmpp_stanza_t * const stanza) { DataForm *result = NULL; xmpp_ctx_t *ctx = connection_get_ctx(); xmpp_stanza_t *child = xmpp_stanza_get_children(stanza); if (child != NULL) { result = malloc(sizeof(struct data_form_t)); result->form_type = NULL; result->fields = NULL; } //handle fields while (child != NULL) { char *var = xmpp_stanza_get_attribute(child, "var"); // handle FORM_TYPE if (g_strcmp0(var, "FORM_TYPE") == 0) { xmpp_stanza_t *value = xmpp_stanza_get_child_by_name(child, "value"); char *value_text = xmpp_stanza_get_text(value); result->form_type = strdup(value_text); // handle regular fields } else { FormField *field = malloc(sizeof(struct form_field_t)); field->var = strdup(var); field->values = NULL; xmpp_stanza_t *value = xmpp_stanza_get_children(child); // handle values while (value != NULL) { char *text = xmpp_stanza_get_text(value); if (text != NULL) { field->values = g_slist_insert_sorted(field->values, strdup(text), (GCompareFunc)octet_compare); xmpp_free(ctx, text); } value = xmpp_stanza_get_next(value); } result->fields = g_slist_insert_sorted(result->fields, field, (GCompareFunc)_field_compare); } child = xmpp_stanza_get_next(child); } return result; } void stanza_destroy_form(DataForm *form) { if (form != NULL) { FREE_SET_NULL(form->form_type); if (form->fields != NULL) { GSList *curr_field = form->fields; while (curr_field != NULL) { FormField *field = curr_field->data; FREE_SET_NULL(field->var); if ((field->values) != NULL) { g_slist_free_full(field->values, free); } } } form = NULL; } } void stanza_attach_priority(xmpp_ctx_t * const ctx, xmpp_stanza_t * const presence, const int pri) { if (pri != 0) { xmpp_stanza_t *priority, *value; char pri_str[10]; snprintf(pri_str, sizeof(pri_str), "%d", pri); priority = xmpp_stanza_new(ctx); value = xmpp_stanza_new(ctx); xmpp_stanza_set_name(priority, STANZA_NAME_PRIORITY); xmpp_stanza_set_text(value, pri_str); xmpp_stanza_add_child(priority, value); xmpp_stanza_add_child(presence, priority); xmpp_stanza_release(priority); } } void stanza_attach_show(xmpp_ctx_t * const ctx, xmpp_stanza_t * const presence, const char * const show) { if (show != NULL) { xmpp_stanza_t *show_stanza = xmpp_stanza_new(ctx); xmpp_stanza_set_name(show_stanza, STANZA_NAME_SHOW); xmpp_stanza_t *text = xmpp_stanza_new(ctx); xmpp_stanza_set_text(text, show); xmpp_stanza_add_child(show_stanza, text); xmpp_stanza_add_child(presence, show_stanza); xmpp_stanza_release(text); xmpp_stanza_release(show_stanza); } } void stanza_attach_status(xmpp_ctx_t * const ctx, xmpp_stanza_t * const presence, const char * const status) { if (status != NULL) { xmpp_stanza_t *status_stanza = xmpp_stanza_new(ctx); xmpp_stanza_set_name(status_stanza, STANZA_NAME_STATUS); xmpp_stanza_t *text = xmpp_stanza_new(ctx); xmpp_stanza_set_text(text, status); xmpp_stanza_add_child(status_stanza, text); xmpp_stanza_add_child(presence, status_stanza); xmpp_stanza_release(text); xmpp_stanza_release(status_stanza); } } void stanza_attach_last_activity(xmpp_ctx_t * const ctx, xmpp_stanza_t * const presence, const int idle) { if (idle > 0) { xmpp_stanza_t *query = xmpp_stanza_new(ctx); xmpp_stanza_set_name(query, STANZA_NAME_QUERY); xmpp_stanza_set_ns(query, STANZA_NS_LASTACTIVITY); char idle_str[10]; snprintf(idle_str, sizeof(idle_str), "%d", idle); xmpp_stanza_set_attribute(query, STANZA_ATTR_SECONDS, idle_str); xmpp_stanza_add_child(presence, query); xmpp_stanza_release(query); } } void stanza_attach_caps(xmpp_ctx_t * const ctx, xmpp_stanza_t * const presence) { xmpp_stanza_t *caps = xmpp_stanza_new(ctx); xmpp_stanza_set_name(caps, STANZA_NAME_C); xmpp_stanza_set_ns(caps, STANZA_NS_CAPS); xmpp_stanza_t *query = caps_create_query_response_stanza(ctx); char *sha1 = caps_create_sha1_str(query); xmpp_stanza_set_attribute(caps, STANZA_ATTR_HASH, "sha-1"); xmpp_stanza_set_attribute(caps, STANZA_ATTR_NODE, "http://www.profanity.im"); xmpp_stanza_set_attribute(caps, STANZA_ATTR_VER, sha1); xmpp_stanza_add_child(presence, caps); xmpp_stanza_release(caps); xmpp_stanza_release(query); FREE_SET_NULL(sha1); } const char * stanza_get_presence_string_from_type(resource_presence_t presence_type) { switch(presence_type) { case RESOURCE_AWAY: return STANZA_TEXT_AWAY; case RESOURCE_DND: return STANZA_TEXT_DND; case RESOURCE_CHAT: return STANZA_TEXT_CHAT; case RESOURCE_XA: return STANZA_TEXT_XA; default: return NULL; } } static int _field_compare(FormField *f1, FormField *f2) { return octet_compare((unsigned char *)f1->var, (unsigned char *)f2->var); }