[telepathy-idle/master] Don't include "PRIVMSG ..." in Sent signals
Will Thompson
will.thompson at collabora.co.uk
Wed May 27 15:54:35 PDT 2009
The previous fix was incorrect, as it emitted the same string in Sent
that it sends over the wire.
---
src/idle-text.c | 29 ++++++++++++++++------
src/idle-text.h | 2 +-
tests/test-text-encode-and-split.c | 8 +++++-
tests/twisted/messages/split-msg-sent-signal.py | 9 +++++-
4 files changed, 36 insertions(+), 12 deletions(-)
diff --git a/src/idle-text.c b/src/idle-text.c
index 6f08bd9..e2ea796 100644
--- a/src/idle-text.c
+++ b/src/idle-text.c
@@ -51,8 +51,9 @@ gboolean idle_text_decode(const gchar *text, TpChannelTextMessageType *type, gch
return TRUE;
}
-GStrv idle_text_encode_and_split(TpChannelTextMessageType type, const gchar *recipient, const gchar *text, gsize max_msg_len, GError **error) {
+GStrv idle_text_encode_and_split(TpChannelTextMessageType type, const gchar *recipient, const gchar *text, gsize max_msg_len, GStrv *bodies_out, GError **error) {
GPtrArray *messages;
+ GPtrArray *bodies;
const gchar *remaining_text = text;
const gchar * const text_end = text + strlen(text);
gchar *header;
@@ -77,12 +78,14 @@ GStrv idle_text_encode_and_split(TpChannelTextMessageType type, const gchar *rec
}
messages = g_ptr_array_new();
+ bodies = g_ptr_array_new();
max_bytes = max_msg_len - (strlen(header) + strlen(footer));
while (remaining_text < text_end) {
char *newline = strchr(remaining_text, '\n');
const char *end_iter;
char *message;
+ int len;
if (newline != NULL && ((unsigned) (newline - remaining_text)) < max_bytes) {
/* String up to the next newline is short enough. */
@@ -98,8 +101,10 @@ GStrv idle_text_encode_and_split(TpChannelTextMessageType type, const gchar *rec
end_iter = text_end;
}
- message = g_strdup_printf("%s%.*s%s", header, (int)(end_iter - remaining_text), remaining_text, footer);
+ len = (int)(end_iter - remaining_text);
+ message = g_strdup_printf("%s%.*s%s", header, len, remaining_text, footer);
g_ptr_array_add(messages, message);
+ g_ptr_array_add(bodies, g_strndup(remaining_text, len));
remaining_text = end_iter;
if (*end_iter == '\n') {
@@ -111,15 +116,23 @@ GStrv idle_text_encode_and_split(TpChannelTextMessageType type, const gchar *rec
g_assert (remaining_text == text_end);
g_ptr_array_add(messages, NULL);
+ g_ptr_array_add(bodies, NULL);
+
+ if (bodies_out != NULL) {
+ *bodies_out = (GStrv) g_ptr_array_free(bodies, FALSE);
+ } else {
+ g_ptr_array_free(bodies, TRUE);
+ }
g_free(header);
return (GStrv) g_ptr_array_free(messages, FALSE);
}
void idle_text_send(GObject *obj, guint type, const gchar *recipient, const gchar *text, IdleConnection *conn, DBusGMethodInvocation *context) {
- time_t timestamp;
+ time_t timestamp = time(NULL);
GError *error = NULL;
GStrv messages;
+ GStrv bodies;
if ((recipient == NULL) || (strlen(recipient) == 0)) {
IDLE_DEBUG("invalid recipient");
@@ -132,18 +145,18 @@ void idle_text_send(GObject *obj, guint type, const gchar *recipient, const gcha
}
gsize msg_len = idle_connection_get_max_message_length(conn);
- messages = idle_text_encode_and_split(type, recipient, text, msg_len, &error);
+ messages = idle_text_encode_and_split(type, recipient, text, msg_len, &bodies, &error);
if (messages == NULL) {
dbus_g_method_return_error(context, error);
g_error_free(error);
return;
}
- for(GStrv m = messages; *m != NULL; m++) {
- idle_connection_send(conn, *m);
+ for(guint i = 0; messages[i] != NULL; i++) {
+ g_assert(bodies[i] != NULL);
+ idle_connection_send(conn, messages[i]);
- timestamp = time(NULL);
- tp_svc_channel_type_text_emit_sent(obj, timestamp, type, *m);
+ tp_svc_channel_type_text_emit_sent(obj, timestamp, type, bodies[i]);
}
g_strfreev(messages);
diff --git a/src/idle-text.h b/src/idle-text.h
index 10d2241..7b231b2 100644
--- a/src/idle-text.h
+++ b/src/idle-text.h
@@ -31,7 +31,7 @@
G_BEGIN_DECLS
gboolean idle_text_decode(const gchar *text, TpChannelTextMessageType *type, gchar **body);
-GStrv idle_text_encode_and_split(TpChannelTextMessageType type, const gchar *recipient, const gchar *text, gsize max_msg_len, GError **error);
+GStrv idle_text_encode_and_split(TpChannelTextMessageType type, const gchar *recipient, const gchar *text, gsize max_msg_len, GStrv *bodies_out, GError **error);
void idle_text_send(GObject *obj, guint type, const gchar *recipient, const gchar *text, IdleConnection *conn, DBusGMethodInvocation *ctx);
G_END_DECLS
diff --git a/tests/test-text-encode-and-split.c b/tests/test-text-encode-and-split.c
index 7fe791d..86cf6ac 100644
--- a/tests/test-text-encode-and-split.c
+++ b/tests/test-text-encode-and-split.c
@@ -24,7 +24,8 @@ test (TpChannelTextMessageType type,
gchar *msg)
{
gchar *recipient = "ircuser";
- gchar **output = idle_text_encode_and_split (type, recipient, msg, 510, NULL);
+ gchar **bodies;
+ gchar **output = idle_text_encode_and_split (type, recipient, msg, 510, &bodies, NULL);
GString *reconstituted_msg = g_string_sized_new (strlen (msg));
int i = -1;
char *line = NULL, *c = NULL;
@@ -88,6 +89,11 @@ test (TpChannelTextMessageType type,
g_strescape (expected_suffixes[type], ""));
}
+ if (strncmp (c, bodies[i], strlen (c) - strlen (expected_suffixes[type])))
+ {
+ fail ("body of '%s' doesn't match alleged body '%s'", c, bodies[i]);
+ }
+
g_string_append_len (reconstituted_msg, c,
strlen (c) - strlen (expected_suffixes[type]));
}
diff --git a/tests/twisted/messages/split-msg-sent-signal.py b/tests/twisted/messages/split-msg-sent-signal.py
index 217b447..cb3fe59 100644
--- a/tests/twisted/messages/split-msg-sent-signal.py
+++ b/tests/twisted/messages/split-msg-sent-signal.py
@@ -8,6 +8,8 @@ from servicetest import EventPattern, call_async
import dbus
LONG_MESSAGE='one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twenty-one twenty-two twenty-three twenty-four twenty-five twenty-six twenty-seven twenty-eight twenty-nine thirty thirty-one thirty-two thirty-three thirty-four thirty-five thirty-six thirty-seven thirty-eight thirty-nine forty forty-one forty-two forty-three forty-four forty-five forty-six forty-seven forty-eight forty-nine fifty fifty-one fifty-two fifty-three fifty-four fifty-five fifty-six fifty-seven fifty-eight fifty-nine sixty sixty-one sixty-two sixty-three sixty-four sixty-five sixty-six sixty-seven sixty-eight sixty-nine'
+MESSAGE_PART_ONE='one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twenty-one twenty-two twenty-three twenty-four twenty-five twenty-six twenty-seven twenty-eight twenty-nine thirty thirty-one thirty-two thirty-three thirty-four thirty-five thirty-six thirty-seven thirty-eight thirty-nine forty forty-one forty-two forty-thr'
+MESSAGE_PART_TWO='ee forty-four forty-five forty-six forty-seven forty-eight forty-nine fifty fifty-one fifty-two fifty-three fifty-four fifty-five fifty-six fifty-seven fifty-eight fifty-nine sixty sixty-one sixty-two sixty-three sixty-four sixty-five sixty-six sixty-seven sixty-eight sixty-nine'
def test(q, bus, conn, stream):
conn.Connect()
@@ -27,8 +29,11 @@ def test(q, bus, conn, stream):
# should emit two Sent signals since the message is long enough to be
# split into two messages
- q.expect('dbus-signal', signal='Sent')
- q.expect('dbus-signal', signal='Sent')
+ e = q.expect('dbus-signal', signal='Sent')
+ assert e.args[2] == MESSAGE_PART_ONE, e.args[2]
+
+ e = q.expect('dbus-signal', signal='Sent')
+ assert e.args[2] == MESSAGE_PART_TWO, e.args[2]
call_async(q, conn, 'Disconnect')
return True
--
1.5.6.5
More information about the telepathy-commits
mailing list