diff options
author | Will Thompson <will.thompson@collabora.co.uk> | 2011-02-15 15:51:46 +0000 |
---|---|---|
committer | Will Thompson <will.thompson@collabora.co.uk> | 2011-03-17 14:25:13 +0000 |
commit | 5f37fa362ee2dcf3995f8a9a7ba8506b2c9777a6 (patch) | |
tree | 54d7ef58893ebe62bccd2f1c64bb5aa0cb0a7579 | |
parent | de8d20d065f554f6ae7fd1945649dbf0322b2971 (diff) |
Add a simple example of sending a message.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | examples/Makefile.am | 5 | ||||
-rw-r--r-- | examples/send-message.c | 138 |
3 files changed, 144 insertions, 0 deletions
@@ -108,6 +108,7 @@ tests/tardis examples/wocky-connect examples/wocky-register +examples/wocky-send-message examples/wocky-unregister coverage/ diff --git a/examples/Makefile.am b/examples/Makefile.am index a771a61..4ea6beb 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,6 +1,7 @@ EXAMPLES = EXAMPLES += wocky-connect +EXAMPLES += wocky-send-message EXAMPLES += wocky-register EXAMPLES += wocky-unregister @@ -9,6 +10,9 @@ INCLUDES := -I$(top_builddir)/wocky wocky_connect_SOURCES = connect.c wocky_connect_DEPENDENCIES = $(top_builddir)/wocky/libwocky.la +wocky_send_message_SOURCES = send-message.c +wocky_send_message_DEPENDENCIES = $(top_builddir)/wocky/libwocky.la + wocky_register_SOURCES = register.c wocky_register_DEPENDENCIES = $(top_builddir)/wocky/libwocky.la @@ -26,6 +30,7 @@ AM_CFLAGS = \ @GLIB_CFLAGS@ check_c_sources = $(wocky_connect_SOURCES) \ + $(wocky_send_message_SOURCES) \ $(wocky_register_SOURCES) \ $(wocky_unregister_SOURCES) diff --git a/examples/send-message.c b/examples/send-message.c new file mode 100644 index 0000000..de1c234 --- /dev/null +++ b/examples/send-message.c @@ -0,0 +1,138 @@ +#include <stdio.h> +#include <stdlib.h> + +#include <string.h> + +#include <glib.h> + +#include <gio/gio.h> +#include <wocky/wocky-connector.h> +#include <wocky/wocky-xmpp-connection.h> +#include <wocky/wocky.h> +#include <wocky/wocky-porter.h> +#include <wocky/wocky-session.h> + +GMainLoop *mainloop; +char *recipient; +char *message; + +static void +closed_cb ( + GObject *source, + GAsyncResult *res, + gpointer user_data) +{ + WockyPorter *porter = WOCKY_PORTER (source); + WockySession *session = WOCKY_SESSION (user_data); + GError *error = NULL; + + if (wocky_porter_close_finish (porter, res, &error)) + { + g_print ("Signed out\n"); + } + else + { + g_warning ("Couldn't sign out cleanly: %s\n", error->message); + g_clear_error (&error); + } + + /* Either way, we're done. */ + g_object_unref (session); + g_main_loop_quit (mainloop); +} + +static void +message_sent_cb ( + GObject *source, + GAsyncResult *res, + gpointer user_data) +{ + WockyPorter *porter = WOCKY_PORTER (source); + WockySession *session = WOCKY_SESSION (user_data); + GError *error = NULL; + + if (wocky_porter_send_finish (porter, res, &error)) + { + g_print ("Sent '%s' to %s\n", message, recipient); + } + else + { + g_warning ("Couldn't send message: %s\n", error->message); + g_clear_error (&error); + } + + /* Sign out. */ + wocky_porter_close_async (porter, NULL, closed_cb, session); +} + +static void +connected_cb ( + GObject *source, + GAsyncResult *res, + gpointer user_data) +{ + WockyConnector *connector = WOCKY_CONNECTOR (source); + WockyXmppConnection *connection; + gchar *jid = NULL; + GError *error = NULL; + + connection = wocky_connector_connect_finish (connector, res, &jid, NULL, + &error); + + if (connection == NULL) + { + g_warning ("Couldn't connect: %s", error->message); + g_clear_error (&error); + g_main_loop_quit (mainloop); + } + else + { + WockySession *session = wocky_session_new (connection, jid); + WockyPorter *porter = wocky_session_get_porter (session); + WockyStanza *stanza = wocky_stanza_build (WOCKY_STANZA_TYPE_MESSAGE, + WOCKY_STANZA_SUB_TYPE_NONE, NULL, recipient, + '(', "body", + '$', message, + ')', NULL); + + g_print ("Connected as %s\n", jid); + + wocky_porter_start (porter); + wocky_porter_send_async (porter, stanza, NULL, message_sent_cb, session); + + g_object_unref (stanza); + g_free (jid); + } +} + +int +main (int argc, + char **argv) +{ + char *jid, *password; + WockyConnector *connector; + + g_type_init (); + wocky_init (); + + if (argc != 5) + { + printf ("Usage: %s <jid> <password> <recipient> <message>\n", argv[0]); + return -1; + } + + jid = argv[1]; + password = argv[2]; + recipient = argv[3]; + message = argv[4]; + + mainloop = g_main_loop_new (NULL, FALSE); + connector = wocky_connector_new (jid, password, NULL, NULL, NULL); + wocky_connector_connect_async (connector, NULL, connected_cb, NULL); + + g_main_loop_run (mainloop); + + g_object_unref (connector); + g_main_loop_unref (mainloop); + return 0; +} |