2 * Copyright (C) 2003-2004 Imendio AB
3 * Copyright (C) 2008 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
19 * Carl Worth <cworth@cworth.org>
25 #include <loudmouth/loudmouth.h>
30 typedef struct _loudgame {
34 LmConnection *connection;
40 loudgame_quit (loudgame_t *lg, int return_value)
44 lg->return_value = return_value;
46 if (! lm_connection_close (lg->connection, &error))
47 g_print ("An error occurred during lm_connection_close: %s\n",
50 lm_connection_unref (lg->connection);
52 g_main_loop_quit (lg->main_loop);
56 authentication_cb (LmConnection *connection, gboolean result, gpointer closure)
59 loudgame_t *lg = closure;
62 g_print ("Authentication for %s failed\n", lg->name);
63 loudgame_quit (lg, 1);
67 m = lm_message_new_with_sub_type (NULL,
68 LM_MESSAGE_TYPE_PRESENCE,
69 LM_MESSAGE_SUB_TYPE_AVAILABLE);
71 lm_connection_send (connection, m, NULL);
76 connection_open_cb (LmConnection *connection, gboolean result, loudgame_t *lg)
78 lm_connection_authenticate (connection,
79 lg->name, lg->passwd, "TestLM",
80 authentication_cb, lg, FALSE, NULL);
84 send_reply (LmConnection *connection,
93 reply = lm_message_new (peer, LM_MESSAGE_TYPE_MESSAGE);
95 lm_message_node_add_child (reply->node, "body", message);
97 result = lm_connection_send (connection, reply, &error);
98 lm_message_unref (reply);
101 g_error ("lm_connection_send failed: error->message");
102 loudgame_quit (lg, 1);
107 handle_command (LmConnection *connection,
114 if (strcmp (command, "quit") == 0) {
115 loudgame_quit (lg, 0);
119 error = g_strdup_printf ("Unknown command: '%s'", command);
120 send_reply (connection, peer, error, lg);
124 static LmHandlerResult
125 handle_messages (LmMessageHandler *handler,
126 LmConnection *connection,
130 loudgame_t *lg = closure;
133 const char *body_str;
135 peer = lm_message_node_get_attribute (m->node, "from");
137 body = lm_message_node_get_child (m->node, "body");
139 body_str = lm_message_node_get_value (body);
141 if (body_str && body_str[0] == '%')
142 handle_command (connection, peer, body_str + 1, lg);
144 send_reply (connection, peer, body_str, lg);
147 return LM_HANDLER_RESULT_REMOVE_MESSAGE;
151 make_connection (gpointer closure)
154 LmMessageHandler *handler;
160 lg->connection = lm_connection_new (lg->server);
162 jid = g_strdup_printf ("%s@%s", lg->name, lg->server);
163 lm_connection_set_jid (lg->connection, jid);
166 handler = lm_message_handler_new (handle_messages, lg, NULL);
167 lm_connection_register_message_handler (lg->connection,
169 LM_MESSAGE_TYPE_MESSAGE,
170 LM_HANDLER_PRIORITY_NORMAL);
172 lm_message_handler_unref (handler);
174 if (! lm_connection_open (lg->connection,
175 (LmResultFunction) connection_open_cb,
178 g_print ("Opening connection failed: %s\n", error->message);
179 loudgame_quit (lg, 1);
182 /* Return false to not schedule another call. */
187 main (int argc, char **argv)
192 g_print ("Usage: %s <server> <username> <password>\n", argv[0]);
200 lg.connection = NULL;
202 lg.main_loop = g_main_loop_new (NULL, FALSE);
204 g_idle_add (make_connection, &lg);
206 g_main_loop_run (lg.main_loop);
208 g_main_loop_unref (lg.main_loop);
210 return lg.return_value;