aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWill Thompson <will.thompson@collabora.co.uk>2009-08-27 02:46:39 +0800
committerWill Thompson <will.thompson@collabora.co.uk>2009-08-28 04:16:52 +0800
commitdec9d4dcf8c7611862221be9eadb53ef52a5bc3b (patch)
tree12434fe2fc2f9d95bd6c99235ace6dbf278058bc
parentcd9d7d1ef951ab85bf87e7ff56b1180169d053c2 (diff)
downloadgsoc2013-empathy-dec9d4dcf8c7611862221be9eadb53ef52a5bc3b.tar
gsoc2013-empathy-dec9d4dcf8c7611862221be9eadb53ef52a5bc3b.tar.gz
gsoc2013-empathy-dec9d4dcf8c7611862221be9eadb53ef52a5bc3b.tar.bz2
gsoc2013-empathy-dec9d4dcf8c7611862221be9eadb53ef52a5bc3b.tar.lz
gsoc2013-empathy-dec9d4dcf8c7611862221be9eadb53ef52a5bc3b.tar.xz
gsoc2013-empathy-dec9d4dcf8c7611862221be9eadb53ef52a5bc3b.tar.zst
gsoc2013-empathy-dec9d4dcf8c7611862221be9eadb53ef52a5bc3b.zip
Add empathy_message_new_from_entry()
This will be the sole place that user input is parsed for special commands like /me; the parsing logic will be removed from EmpathyMessage and chat_send().
-rw-r--r--libempathy/empathy-message.c56
-rw-r--r--libempathy/empathy-message.h1
2 files changed, 57 insertions, 0 deletions
diff --git a/libempathy/empathy-message.c b/libempathy/empathy-message.c
index a8fe6084c..0da55112d 100644
--- a/libempathy/empathy-message.c
+++ b/libempathy/empathy-message.c
@@ -218,6 +218,62 @@ message_set_property (GObject *object,
};
}
+static gboolean
+has_prefix_case (const gchar *s,
+ const gchar *prefix)
+{
+ return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
+}
+
+/*
+ * Constructs an EmpathyMessage based on user input, which may include "/me"
+ * and friends.
+ *
+ * Returns: an #EmpathyMessage if @message could be parsed, or %NULL if
+ * @message was an unknown command.
+ */
+EmpathyMessage *
+empathy_message_new_from_entry (const gchar *message)
+{
+ TpChannelTextMessageType t = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
+
+ g_return_val_if_fail (message != NULL, NULL);
+
+ if (message[0] == '/') {
+ if (g_ascii_strcasecmp (message, "/me") == 0) {
+ message = "";
+ t = TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
+ } else if (has_prefix_case (message, "/me ")) {
+ message += strlen ("/me ");
+ t = TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
+ } else if (has_prefix_case (message, "/say ")) {
+ message += strlen ("/say ");
+ } else {
+ /* Also allow messages with two slashes before the
+ * first space, so it is possible to send a /unix/path.
+ * This heuristic is kind of crap.
+ */
+ gboolean second_slash = FALSE;
+ const gchar *m = message + 1;
+
+ while (!second_slash && *m != '\0' && *m != ' ') {
+ if (*m == '/')
+ second_slash = TRUE;
+
+ m++;
+ }
+
+ if (!second_slash)
+ return NULL;
+ }
+ }
+
+ return g_object_new (EMPATHY_TYPE_MESSAGE,
+ "type", t,
+ "body", message,
+ NULL);
+}
+
EmpathyMessage *
empathy_message_new (const gchar *body)
{
diff --git a/libempathy/empathy-message.h b/libempathy/empathy-message.h
index 00064df57..f9a488703 100644
--- a/libempathy/empathy-message.h
+++ b/libempathy/empathy-message.h
@@ -52,6 +52,7 @@ struct _EmpathyMessageClass {
};
GType empathy_message_get_type (void) G_GNUC_CONST;
+EmpathyMessage * empathy_message_new_from_entry (const gchar *message);
EmpathyMessage * empathy_message_new (const gchar *body);
TpChannelTextMessageType empathy_message_get_tptype (EmpathyMessage *message);
void empathy_message_set_tptype (EmpathyMessage *message,