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-02-15 15:51:46 +0000 |
commit | 0dd3a10f6dbfa6f580eed5ae0f492eaaac2e540f (patch) | |
tree | bffccf0bd76f478190c3b6c770d25de52383cb5f | |
parent | 913dc10f79a5717ee2995056e1a2c07425e29ebc (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/message.c | 135 |
3 files changed, 141 insertions, 0 deletions
@@ -107,6 +107,7 @@ tests/tardis # tests/certs/crl/* examples/wocky-connect +examples/wocky-message examples/wocky-register examples/wocky-unregister diff --git a/examples/Makefile.am b/examples/Makefile.am index a771a61..bb05126 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,6 +1,7 @@ EXAMPLES = EXAMPLES += wocky-connect +EXAMPLES += wocky-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_message_SOURCES = message.c +wocky_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_message_SOURCES) \ $(wocky_register_SOURCES) \ $(wocky_unregister_SOURCES) diff --git a/examples/message.c b/examples/message.c new file mode 100644 index 0000000..82be292 --- /dev/null +++ b/examples/message.c @@ -0,0 +1,135 @@ +#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); + GError *error = NULL; + + if (wocky_porter_close_finish (porter, res, &error)) + { + g_print ("Signed out\n"); + } + else + { + /* Doesn't work! “Another send operation is pending”. */ + g_warning ("Couldn't sign out: %s\n", error->message); + g_clear_error (&error); + } + + /* Either way, we're done. */ + g_main_loop_quit (mainloop); +} + +static void +message_sent_cb ( + GObject *source, + GAsyncResult *res, + gpointer user_data) +{ + WockyPorter *porter = WOCKY_PORTER (source); + 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, NULL); +} + +static void +connected_cb ( + GObject *source, + GAsyncResult *res, + gpointer user_data) +{ + WockyConnector *connector = WOCKY_CONNECTOR (source); + WockyXmppConnection *connection; + gchar *jid = NULL; + gchar *sid = NULL; + GError *error = NULL; + + connection = wocky_connector_connect_finish (connector, res, &jid, &sid, + &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); + + wocky_porter_start (porter); + wocky_porter_send_async (porter, stanza, NULL, message_sent_cb, NULL); + + g_object_unref (stanza); + /* We leak the session and porter. */ + } +} + +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; +} |