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>
32 LmConnection *connection;
38 loudgame_quit (UserInfo *info, int return_value)
42 info->return_value = return_value;
44 if (! lm_connection_close (info->connection, &error))
45 g_print ("An error occurred during lm_connection_close: %s\n",
48 lm_connection_unref (info->connection);
50 g_main_loop_quit (info->main_loop);
54 authentication_cb (LmConnection *connection, gboolean result, gpointer closure)
57 UserInfo *info = closure;
60 g_print ("Authentication for %s failed\n", info->name);
61 loudgame_quit (info, 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, UserInfo *info)
76 lm_connection_authenticate (connection,
77 info->name, info->passwd, "TestLM",
78 authentication_cb, info, 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 (info, 1);
105 handle_command (LmConnection *connection,
112 if (strcmp (command, "quit") == 0) {
113 loudgame_quit (info, 0);
117 error = g_strdup_printf ("Unknown command: '%s'", command);
118 send_reply (connection, peer, error, info);
122 static LmHandlerResult
123 handle_messages (LmMessageHandler *handler,
124 LmConnection *connection,
128 UserInfo *info = 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, info);
142 send_reply (connection, peer, body_str, info);
145 return LM_HANDLER_RESULT_REMOVE_MESSAGE;
149 make_connection (gpointer closure)
152 LmMessageHandler *handler;
158 info->connection = lm_connection_new (info->server);
160 jid = g_strdup_printf ("%s@%s", info->name, info->server);
161 lm_connection_set_jid (info->connection, jid);
164 handler = lm_message_handler_new (handle_messages, info, NULL);
165 lm_connection_register_message_handler (info->connection,
167 LM_MESSAGE_TYPE_MESSAGE,
168 LM_HANDLER_PRIORITY_NORMAL);
170 lm_message_handler_unref (handler);
172 if (! lm_connection_open (info->connection,
173 (LmResultFunction) connection_open_cb,
176 g_print ("Opening connection failed: %s\n", error->message);
177 loudgame_quit (info, 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]);
194 info.server = argv[1];
196 info.passwd = argv[3];
198 info.connection = NULL;
200 info.main_loop = g_main_loop_new (NULL, FALSE);
202 g_idle_add (make_connection, &info);
204 g_main_loop_run (info.main_loop);
206 g_main_loop_unref (info.main_loop);
208 return info.return_value;