summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>2013-10-15 13:51:39 -0400
committerGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>2013-10-15 13:51:39 -0400
commit5d9b814f90e8db92d13874d276187213d3feabfd (patch)
tree04acb862545d9db37dd59761b64610907599c53e
parent4c5405c481b24787830136c239fcc93d01b0b35e (diff)
downloadtelepathy-idle-5d9b814f90e8db92d13874d276187213d3feabfd.tar.gz
IRC_Command: prevent user of sending commands for which we have proper API
-rw-r--r--extensions/Connection_Interface_IRC_Command1.xml6
-rw-r--r--src/idle-connection.c53
-rw-r--r--tests/twisted/irc-command.py5
3 files changed, 64 insertions, 0 deletions
diff --git a/extensions/Connection_Interface_IRC_Command1.xml b/extensions/Connection_Interface_IRC_Command1.xml
index 0d75a4d..2166fc9 100644
--- a/extensions/Connection_Interface_IRC_Command1.xml
+++ b/extensions/Connection_Interface_IRC_Command1.xml
@@ -39,6 +39,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.</
<tp:possible-errors>
<tp:error name="org.freedesktop.Telepathy.Error.Disconnected"/>
<tp:error name="org.freedesktop.Telepathy.Error.NetworkError"/>
+ <tp:error name="org.freedesktop.Telepathy.Error.InvalidArgument">
+ <tp:docstring>
+ The connection manager MAY raise this error for commands that
+ have a more appropriate D-Bus API.
+ </tp:docstring>
+ </tp:error>
</tp:possible-errors>
</method>
<tp:docstring>
diff --git a/src/idle-connection.c b/src/idle-connection.c
index 46fb3d6..d99ed91 100644
--- a/src/idle-connection.c
+++ b/src/idle-connection.c
@@ -1571,12 +1571,65 @@ static void _renaming_iface_init(gpointer g_iface, gpointer iface_data) {
#undef IMPLEMENT
}
+typedef struct
+{
+ const gchar *command;
+ const gchar *error_msg;
+} IrcCommandCheck;
+
+static const IrcCommandCheck commands[] = {
+ { "INVITE", "Use the Group API on room channels" },
+ { "JOIN", "Use the Group API on room channels" },
+ { "KICK", "Use the Group API on room channels" },
+ { "PART", "Use the Group API on room channels" },
+ { "PRIVMSG", "Use text channels" },
+ { "QUIT", "Disconnect the connection" },
+ { "TOPIC", "Use the Subject API on room channels" },
+ { NULL, NULL }
+};
+
+/* Return FALSE and set @error if @command is not meant to be used with
+ * IRC_Command.Send() as we have proper Telepathy API for it. */
+static gboolean
+check_irc_command (IdleConnection *self,
+ const gchar *full_command,
+ GError **error)
+{
+ gchar **splitted;
+ guint i;
+
+ splitted = g_strsplit (full_command, " ", 0);
+
+ for (i = 0; commands[i].command != NULL; i++)
+ {
+ if (g_ascii_strcasecmp (splitted[0], commands[i].command) == 0)
+ {
+ g_set_error_literal (error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
+ commands[i].error_msg);
+
+ g_strfreev (splitted);
+ return FALSE;
+ }
+ }
+
+ g_strfreev (splitted);
+ return TRUE;
+}
+
static void
idle_connection_irc_command_send (IdleSvcConnectionInterfaceIRCCommand1 *iface,
const gchar *command,
DBusGMethodInvocation *context)
{
IdleConnection *self = IDLE_CONNECTION(iface);
+ GError *error = NULL;
+
+ if (!check_irc_command (self, command, &error))
+ {
+ dbus_g_method_return_error (context, error);
+ g_error_free (error);
+ return;
+ }
_send_with_priority (self, command, SERVER_CMD_NORMAL_PRIORITY);
diff --git a/tests/twisted/irc-command.py b/tests/twisted/irc-command.py
index f27dbb1..6ec27da 100644
--- a/tests/twisted/irc-command.py
+++ b/tests/twisted/irc-command.py
@@ -20,6 +20,11 @@ def test(q, bus, conn, stream):
q.expect('dbus-return', method='Send')
+ # We are not supposed to use this API to send messages
+ call_async(q, irc_cmd, 'Send', 'PRIVMSG badger :oh hi')
+
+ q.expect('dbus-error', method='Send', name=cs.INVALID_ARGUMENT)
+
call_async(q, conn, 'Disconnect')
if __name__ == '__main__':