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>
30 loudgame_quit (loudgame_t *lg, int return_value)
34 lg->return_value = return_value;
36 if (! lm_connection_close (lg->connection, &error))
37 g_print ("An error occurred during lm_connection_close: %s\n",
40 lm_connection_unref (lg->connection);
42 g_main_loop_quit (lg->main_loop);
46 authentication_cb (LmConnection *connection, gboolean result, gpointer closure)
49 loudgame_t *lg = closure;
52 g_print ("Authentication for %s failed\n", lg->name);
53 loudgame_quit (lg, 1);
57 m = lm_message_new_with_sub_type (NULL,
58 LM_MESSAGE_TYPE_PRESENCE,
59 LM_MESSAGE_SUB_TYPE_AVAILABLE);
61 lm_connection_send (connection, m, NULL);
66 connection_open_cb (LmConnection *connection, gboolean result, loudgame_t *lg)
68 lm_connection_authenticate (connection,
69 lg->name, lg->passwd, "TestLM",
70 authentication_cb, lg, FALSE, NULL);
74 send_reply (LmConnection *connection,
83 reply = lm_message_new (peer, LM_MESSAGE_TYPE_MESSAGE);
85 lm_message_node_add_child (reply->node, "body", message);
87 result = lm_connection_send (connection, reply, &error);
88 lm_message_unref (reply);
91 g_error ("lm_connection_send failed: error->message");
92 loudgame_quit (lg, 1);
97 handle_command (LmConnection *connection,
104 if (strcmp (command, "quit") == 0) {
105 loudgame_quit (lg, 0);
109 error = g_strdup_printf ("Unknown command: '%s'", command);
110 send_reply (connection, peer, error, lg);
114 static LmHandlerResult
115 handle_messages (LmMessageHandler *handler,
116 LmConnection *connection,
120 loudgame_t *lg = closure;
123 const char *body_str;
125 peer = lm_message_node_get_attribute (m->node, "from");
127 body = lm_message_node_get_child (m->node, "body");
129 body_str = lm_message_node_get_value (body);
131 if (body_str && body_str[0] == '%')
132 handle_command (connection, peer, body_str + 1, lg);
133 else if (lg->handle_message)
134 (lg->handle_message) (lg, peer, body_str);
137 return LM_HANDLER_RESULT_REMOVE_MESSAGE;
141 make_connection (gpointer closure)
144 LmMessageHandler *handler;
146 GError *error = NULL;
150 lg->connection = lm_connection_new (lg->server);
152 jid = g_strdup_printf ("%s@%s", lg->name, lg->server);
153 lm_connection_set_jid (lg->connection, jid);
156 handler = lm_message_handler_new (handle_messages, lg, NULL);
157 lm_connection_register_message_handler (lg->connection,
159 LM_MESSAGE_TYPE_MESSAGE,
160 LM_HANDLER_PRIORITY_NORMAL);
162 lm_message_handler_unref (handler);
164 if (! lm_connection_open (lg->connection,
165 (LmResultFunction) connection_open_cb,
168 g_print ("Failed to open connection to %s: %s.\n",
169 lg->server, error->message);
170 loudgame_quit (lg, 1);
173 /* Return false to not schedule another call. */
178 loudgame_init (loudgame_t *lg, int argc, char **argv)
181 g_print ("Usage: %s <server> <username> <password>\n", argv[0]);
185 lg->server = argv[1];
187 lg->passwd = argv[3];
189 lg->connection = NULL;
191 lg->main_loop = g_main_loop_new (NULL, FALSE);
193 lg->return_value = 0;
195 lg->handle_message = NULL;
201 loudgame_run (loudgame_t *lg)
203 g_idle_add (make_connection, lg);
205 g_main_loop_run (lg->main_loop);
207 g_main_loop_unref (lg->main_loop);
209 return lg->return_value;