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
6 * modify it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 * Carl Worth <cworth@cworth.org>
27 #include <loudmouth/loudmouth.h>
32 typedef struct _loudgame {
36 LmConnection *connection;
42 loudgame_quit (loudgame_t *lg, int return_value)
46 lg->return_value = return_value;
48 if (! lm_connection_close (lg->connection, &error))
49 g_print ("An error occurred during lm_connection_close: %s\n",
52 lm_connection_unref (lg->connection);
54 g_main_loop_quit (lg->main_loop);
58 authentication_cb (LmConnection *connection, gboolean result, gpointer closure)
61 loudgame_t *lg = closure;
64 g_print ("Authentication for %s failed\n", lg->name);
65 loudgame_quit (lg, 1);
69 m = lm_message_new_with_sub_type (NULL,
70 LM_MESSAGE_TYPE_PRESENCE,
71 LM_MESSAGE_SUB_TYPE_AVAILABLE);
73 lm_connection_send (connection, m, NULL);
78 connection_open_cb (LmConnection *connection, gboolean result, loudgame_t *lg)
80 lm_connection_authenticate (connection,
81 lg->name, lg->passwd, "TestLM",
82 authentication_cb, lg, FALSE, NULL);
86 send_reply (LmConnection *connection,
95 reply = lm_message_new (peer, LM_MESSAGE_TYPE_MESSAGE);
97 lm_message_node_add_child (reply->node, "body", message);
99 result = lm_connection_send (connection, reply, &error);
100 lm_message_unref (reply);
103 g_error ("lm_connection_send failed: error->message");
104 loudgame_quit (lg, 1);
109 handle_command (LmConnection *connection,
116 if (strcmp (command, "quit") == 0) {
117 loudgame_quit (lg, 0);
121 error = g_strdup_printf ("Unknown command: '%s'", command);
122 send_reply (connection, peer, error, lg);
126 static LmHandlerResult
127 handle_messages (LmMessageHandler *handler,
128 LmConnection *connection,
132 loudgame_t *lg = closure;
135 const char *body_str;
137 peer = lm_message_node_get_attribute (m->node, "from");
139 body = lm_message_node_get_child (m->node, "body");
141 body_str = lm_message_node_get_value (body);
143 if (body_str && body_str[0] == '%')
144 handle_command (connection, peer, body_str + 1, lg);
146 send_reply (connection, peer, body_str, lg);
149 return LM_HANDLER_RESULT_REMOVE_MESSAGE;
153 make_connection (gpointer closure)
156 LmMessageHandler *handler;
162 lg->connection = lm_connection_new (lg->server);
164 jid = g_strdup_printf ("%s@%s", lg->name, lg->server);
165 lm_connection_set_jid (lg->connection, jid);
168 handler = lm_message_handler_new (handle_messages, lg, NULL);
169 lm_connection_register_message_handler (lg->connection,
171 LM_MESSAGE_TYPE_MESSAGE,
172 LM_HANDLER_PRIORITY_NORMAL);
174 lm_message_handler_unref (handler);
176 if (! lm_connection_open (lg->connection,
177 (LmResultFunction) connection_open_cb,
180 g_print ("Opening connection failed: %s\n", error->message);
181 loudgame_quit (lg, 1);
184 /* Return false to not schedule another call. */
189 main (int argc, char **argv)
194 g_print ("Usage: %s <server> <username> <password>\n", argv[0]);
202 lg.connection = NULL;
204 lg.main_loop = g_main_loop_new (NULL, FALSE);
206 g_idle_add (make_connection, &lg);
208 g_main_loop_run (lg.main_loop);
210 g_main_loop_unref (lg.main_loop);
212 return lg.return_value;