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>
34 free_user_info (UserInfo *info)
37 g_free (info->passwd);
44 authentication_cb (LmConnection *connection, gboolean result, gpointer ud)
46 g_print ("Auth: %d\n", result);
47 free_user_info ((UserInfo *) ud);
52 m = lm_message_new_with_sub_type (NULL,
53 LM_MESSAGE_TYPE_PRESENCE,
54 LM_MESSAGE_SUB_TYPE_AVAILABLE);
55 g_print (":: %s\n", lm_message_node_to_string (m->node));
57 lm_connection_send (connection, m, NULL);
64 connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
66 g_print ("Connected callback\n");
67 lm_connection_authenticate (connection,
68 info->name, info->passwd, "TestLM",
69 authentication_cb, info, FALSE, NULL);
70 g_print ("Sent auth message\n");
73 static LmHandlerResult
74 handle_messages (LmMessageHandler *handler,
75 LmConnection *connection,
84 g_print ("Incoming message from: %s\n",
85 lm_message_node_get_attribute (m->node, "from"));
87 reply = lm_message_new (lm_message_node_get_attribute (m->node, "from"),
88 LM_MESSAGE_TYPE_MESSAGE);
89 body = lm_message_node_get_child (m->node, "body");
90 lm_message_node_add_child (reply->node, "body",
91 lm_message_node_get_value (body));
93 result = lm_connection_send (connection, reply, &error);
94 lm_message_unref (reply);
97 g_error ("lm_connection_send failed");
100 return LM_HANDLER_RESULT_REMOVE_MESSAGE;
103 main (int argc, char **argv)
105 GMainLoop *main_loop;
106 LmConnection *connection;
107 LmMessageHandler *handler;
113 g_print ("Usage: %s <server> <username> <password> <connectserver> <connectport>\n", argv[0]);
117 connection = lm_connection_new (argv[4]);
119 jid = g_strdup_printf ("%s@%s", argv[2], argv[1]);
120 lm_connection_set_jid (connection, jid);
123 lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10));
125 handler = lm_message_handler_new (handle_messages, NULL, NULL);
126 lm_connection_register_message_handler (connection, handler,
127 LM_MESSAGE_TYPE_MESSAGE,
128 LM_HANDLER_PRIORITY_NORMAL);
130 lm_message_handler_unref (handler);
132 info = g_new0 (UserInfo, 1);
133 info->name = g_strdup (argv[2]);
134 info->passwd = g_strdup (argv[3]);
136 result = lm_connection_open (connection,
137 (LmResultFunction) connection_open_cb,
141 g_print ("Opening connection failed: %d\n", result);
143 g_print ("Returned from the connection_open\n");
146 main_loop = g_main_loop_new (NULL, FALSE);
147 g_main_loop_run (main_loop);