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 free_user_info (UserInfo *info)
41 g_free (info->passwd);
47 authentication_cb (LmConnection *connection, gboolean result, gpointer ud)
51 g_print ("Auth: %d\n", result);
52 free_user_info ((UserInfo *) ud);
57 m = lm_message_new_with_sub_type (NULL,
58 LM_MESSAGE_TYPE_PRESENCE,
59 LM_MESSAGE_SUB_TYPE_AVAILABLE);
60 string = lm_message_node_to_string (m->node);
61 g_print (":: %s\n", string);
64 lm_connection_send (connection, m, NULL);
71 connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
73 g_print ("Connected callback\n");
74 lm_connection_authenticate (connection,
75 info->name, info->passwd, "TestLM",
76 authentication_cb, info, FALSE, NULL);
77 g_print ("Sent auth message\n");
81 send_reply (LmConnection *connection,
89 reply = lm_message_new (peer, LM_MESSAGE_TYPE_MESSAGE);
91 lm_message_node_add_child (reply->node, "body", message);
93 result = lm_connection_send (connection, reply, &error);
94 lm_message_unref (reply);
97 g_error ("lm_connection_send failed");
102 handle_command (LmConnection *connection,
107 msg_data_t *data = closure;
110 if (strcmp (command, "quit") == 0) {
111 g_main_loop_quit (data->main_loop);
115 error = g_strdup_printf ("Unknown command: '%s'", command);
116 send_reply (connection, peer, error);
120 static LmHandlerResult
121 handle_messages (LmMessageHandler *handler,
122 LmConnection *connection,
128 const char *body_str;
130 peer = lm_message_node_get_attribute (m->node, "from");
133 g_print ("Incoming message from: %s\n", peer);
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, closure);
142 send_reply (connection, peer, body_str);
145 return LM_HANDLER_RESULT_REMOVE_MESSAGE;
148 main (int argc, char **argv)
150 LmConnection *connection;
151 LmMessageHandler *handler;
159 g_print ("Usage: %s <server> <username> <password> <connectserver> <connectport>\n", argv[0]);
163 connection = lm_connection_new (argv[4]);
165 jid = g_strdup_printf ("%s@%s", argv[2], argv[1]);
166 lm_connection_set_jid (connection, jid);
169 lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10));
171 data.main_loop = g_main_loop_new (NULL, FALSE);
173 handler = lm_message_handler_new (handle_messages, &data, NULL);
174 lm_connection_register_message_handler (connection, handler,
175 LM_MESSAGE_TYPE_MESSAGE,
176 LM_HANDLER_PRIORITY_NORMAL);
178 lm_message_handler_unref (handler);
180 info = g_new0 (UserInfo, 1);
181 info->name = g_strdup (argv[2]);
182 info->passwd = g_strdup (argv[3]);
184 result = lm_connection_open (connection,
185 (LmResultFunction) connection_open_cb,
189 g_print ("Opening connection failed: %d\n", result);
191 g_print ("Returned from the connection_open\n");
194 g_main_loop_run (data.main_loop);
196 g_main_loop_unref (data.main_loop);
198 lm_connection_close (connection, &error);
199 lm_connection_unref (connection);