diff options
Diffstat (limited to 'examples/message.c')
-rw-r--r-- | examples/message.c | 135 |
1 files changed, 135 insertions, 0 deletions
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; +} |