1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003-2004 Imendio AB
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
24 #include <loudmouth/loudmouth.h>
35 free_user_info (UserInfo *info)
38 g_free (info->passwd);
45 authentication_cb (LmConnection *connection, gboolean result, gpointer ud)
47 g_print ("Auth: %d\n", result);
48 free_user_info ((UserInfo *) ud);
53 m = lm_message_new_with_sub_type (NULL,
54 LM_MESSAGE_TYPE_PRESENCE,
55 LM_MESSAGE_SUB_TYPE_AVAILABLE);
56 g_print (":: %s\n", lm_message_node_to_string (m->node));
58 lm_connection_send (connection, m, NULL);
65 connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
67 g_print ("Connected callback\n");
68 lm_connection_authenticate (connection,
69 info->name, info->passwd, "TestLM",
70 authentication_cb, info, FALSE, NULL);
71 g_print ("Sent auth message\n");
74 static LmHandlerResult
75 handle_messages (LmMessageHandler *handler,
76 LmConnection *connection,
85 g_print ("Incoming message from: %s\n",
86 lm_message_node_get_attribute (m->node, "from"));
88 reply = lm_message_new (lm_message_node_get_attribute (m->node, "from"),
89 LM_MESSAGE_TYPE_MESSAGE);
90 body = lm_message_node_get_child (m->node, "body");
91 lm_message_node_add_child (reply->node, "body",
92 lm_message_node_get_value (body));
94 result = lm_connection_send (connection, reply, &error);
95 lm_message_unref (reply);
98 g_error ("lm_connection_send failed");
101 return LM_HANDLER_RESULT_REMOVE_MESSAGE;
104 main (int argc, char **argv)
106 GMainLoop *main_loop;
107 LmConnection *connection;
108 LmMessageHandler *handler;
114 g_print ("Usage: %s <server> <username> <password> <connectserver> <connectport>\n", argv[0]);
118 connection = lm_connection_new (argv[4]);
120 jid = g_strdup_printf ("%s@%s", argv[2], argv[1]);
121 lm_connection_set_jid (connection, jid);
124 lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10));
126 handler = lm_message_handler_new (handle_messages, NULL, NULL);
127 lm_connection_register_message_handler (connection, handler,
128 LM_MESSAGE_TYPE_MESSAGE,
129 LM_HANDLER_PRIORITY_NORMAL);
131 lm_message_handler_unref (handler);
133 info = g_new0 (UserInfo, 1);
134 info->name = g_strdup (argv[2]);
135 info->passwd = g_strdup (argv[3]);
137 result = lm_connection_open (connection,
138 (LmResultFunction) connection_open_cb,
142 g_print ("Opening connection failed: %d\n", result);
144 g_print ("Returned from the connection_open\n");
147 main_loop = g_main_loop_new (NULL, FALSE);
148 g_main_loop_run (main_loop);