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>
38 authentication_cb (LmConnection *connection, gboolean result, gpointer ud)
40 g_print ("Auth: %d\n", result);
45 m = lm_message_new_with_sub_type (NULL,
46 LM_MESSAGE_TYPE_PRESENCE,
47 LM_MESSAGE_SUB_TYPE_AVAILABLE);
49 lm_connection_send (connection, m, NULL);
56 connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
58 lm_connection_authenticate (connection,
59 info->name, info->passwd, "TestLM",
60 authentication_cb, info, FALSE, NULL);
64 send_reply (LmConnection *connection,
72 reply = lm_message_new (peer, LM_MESSAGE_TYPE_MESSAGE);
74 lm_message_node_add_child (reply->node, "body", message);
76 result = lm_connection_send (connection, reply, &error);
77 lm_message_unref (reply);
80 g_error ("lm_connection_send failed");
85 handle_command (LmConnection *connection,
90 msg_data_t *data = closure;
93 if (strcmp (command, "quit") == 0) {
94 g_main_loop_quit (data->main_loop);
98 error = g_strdup_printf ("Unknown command: '%s'", command);
99 send_reply (connection, peer, error);
103 static LmHandlerResult
104 handle_messages (LmMessageHandler *handler,
105 LmConnection *connection,
111 const char *body_str;
113 peer = lm_message_node_get_attribute (m->node, "from");
115 body = lm_message_node_get_child (m->node, "body");
117 body_str = lm_message_node_get_value (body);
119 if (body_str && body_str[0] == '%')
120 handle_command (connection, peer, body_str + 1, closure);
122 send_reply (connection, peer, body_str);
125 return LM_HANDLER_RESULT_REMOVE_MESSAGE;
128 main (int argc, char **argv)
130 LmConnection *connection;
131 LmMessageHandler *handler;
139 g_print ("Usage: %s <server> <username> <password>\n", argv[0]);
143 connection = lm_connection_new (argv[1]);
145 jid = g_strdup_printf ("%s@%s", argv[2], argv[1]);
146 lm_connection_set_jid (connection, jid);
149 data.main_loop = g_main_loop_new (NULL, FALSE);
151 handler = lm_message_handler_new (handle_messages, &data, NULL);
152 lm_connection_register_message_handler (connection, handler,
153 LM_MESSAGE_TYPE_MESSAGE,
154 LM_HANDLER_PRIORITY_NORMAL);
156 lm_message_handler_unref (handler);
159 info.passwd = argv[3];
161 result = lm_connection_open (connection,
162 (LmResultFunction) connection_open_cb,
165 g_print ("Opening connection failed: %d\n", result);
169 g_main_loop_run (data.main_loop);
171 g_main_loop_unref (data.main_loop);
173 lm_connection_close (connection, &error);
174 lm_connection_unref (connection);