2 * Copyright (C) 2003-2004 Imendio AB
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include <loudmouth/loudmouth.h>
28 typedef struct _loudgame {
32 LmConnection *connection;
38 loudgame_quit (loudgame_t *lg, int return_value)
42 lg->return_value = return_value;
44 if (! lm_connection_close (lg->connection, &error))
45 g_print ("An error occurred during lm_connection_close: %s\n",
48 lm_connection_unref (lg->connection);
50 g_main_loop_quit (lg->main_loop);
54 authentication_cb (LmConnection *connection, gboolean result, gpointer closure)
57 loudgame_t *lg = closure;
60 g_print ("Authentication for %s failed\n", lg->name);
61 loudgame_quit (lg, 1);
65 m = lm_message_new_with_sub_type (NULL,
66 LM_MESSAGE_TYPE_PRESENCE,
67 LM_MESSAGE_SUB_TYPE_AVAILABLE);
69 lm_connection_send (connection, m, NULL);
74 connection_open_cb (LmConnection *connection, gboolean result, loudgame_t *lg)
76 lm_connection_authenticate (connection,
77 lg->name, lg->passwd, "TestLM",
78 authentication_cb, lg, FALSE, NULL);
82 send_reply (LmConnection *connection,
91 reply = lm_message_new (peer, LM_MESSAGE_TYPE_MESSAGE);
93 lm_message_node_add_child (reply->node, "body", message);
95 result = lm_connection_send (connection, reply, &error);
96 lm_message_unref (reply);
99 g_error ("lm_connection_send failed: error->message");
100 loudgame_quit (lg, 1);
105 handle_command (LmConnection *connection,
112 if (strcmp (command, "quit") == 0) {
113 loudgame_quit (lg, 0);
117 error = g_strdup_printf ("Unknown command: '%s'", command);
118 send_reply (connection, peer, error, lg);
122 static LmHandlerResult
123 handle_messages (LmMessageHandler *handler,
124 LmConnection *connection,
128 loudgame_t *lg = closure;
131 const char *body_str;
133 peer = lm_message_node_get_attribute (m->node, "from");
135 body = lm_message_node_get_child (m->node, "body");
137 body_str = lm_message_node_get_value (body);
139 if (body_str && body_str[0] == '%')
140 handle_command (connection, peer, body_str + 1, lg);
142 send_reply (connection, peer, body_str, lg);
145 return LM_HANDLER_RESULT_REMOVE_MESSAGE;
149 make_connection (gpointer closure)
152 LmMessageHandler *handler;
158 lg->connection = lm_connection_new (lg->server);
160 jid = g_strdup_printf ("%s@%s", lg->name, lg->server);
161 lm_connection_set_jid (lg->connection, jid);
164 handler = lm_message_handler_new (handle_messages, lg, NULL);
165 lm_connection_register_message_handler (lg->connection,
167 LM_MESSAGE_TYPE_MESSAGE,
168 LM_HANDLER_PRIORITY_NORMAL);
170 lm_message_handler_unref (handler);
172 if (! lm_connection_open (lg->connection,
173 (LmResultFunction) connection_open_cb,
176 g_print ("Opening connection failed: %s\n", error->message);
177 loudgame_quit (lg, 1);
180 /* Return false to not schedule another call. */
185 main (int argc, char **argv)
190 g_print ("Usage: %s <server> <username> <password>\n", argv[0]);
198 lg.connection = NULL;
200 lg.main_loop = g_main_loop_new (NULL, FALSE);
202 g_idle_add (make_connection, &lg);
204 g_main_loop_run (lg.main_loop);
206 g_main_loop_unref (lg.main_loop);
208 return lg.return_value;