]> git.cworth.org Git - loudgame/blob - lm-echo.c
Add lm-echo program, (from test-tunnel.c demo from loudmouth docs.)
[loudgame] / lm-echo.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2003-2004 Imendio AB
4  *
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.
9  *
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.
14  *
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.
19  */
20  
21 #include <glib.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <loudmouth/loudmouth.h>
25 #ifdef __WIN32__
26 #include <winsock2.h>
27 #endif
28  
29 typedef struct {
30         gchar *name;
31         gchar *passwd;
32 } UserInfo;
33  
34 static void
35 free_user_info (UserInfo *info)
36 {
37         g_free (info->name);
38         g_free (info->passwd);
39  
40         g_free (info);
41 }
42  
43  
44 static void
45 authentication_cb (LmConnection *connection, gboolean result, gpointer ud)
46 {
47         g_print ("Auth: %d\n", result);
48         free_user_info ((UserInfo *) ud);
49  
50         if (result == TRUE) {
51                 LmMessage *m;
52                  
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));
57                  
58                 lm_connection_send (connection, m, NULL);
59                 lm_message_unref (m);
60         }
61
62 }
63  
64 static void
65 connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
66 {
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");
72 }
73  
74 static LmHandlerResult
75 handle_messages (LmMessageHandler *handler,
76                  LmConnection     *connection,
77                  LmMessage        *m,
78                  gpointer          user_data)
79 {
80         g_print ("Incoming message from: %s\n",
81                  lm_message_node_get_attribute (m->node, "from"));
82  
83         return LM_HANDLER_RESULT_REMOVE_MESSAGE;
84 }
85 int
86 main (int argc, char **argv)
87 {
88         GMainLoop        *main_loop;
89         LmConnection     *connection;
90         LmMessageHandler *handler;
91         gboolean          result;
92         UserInfo         *info;
93         gchar            *jid;
94                                                                                 
95         if (argc < 6) {
96                 g_print ("Usage: %s <server> <username> <password> <connectserver> <connectport>\n", argv[0]);
97                 return 1;
98         }
99                                                                                 
100         connection = lm_connection_new (argv[4]);
101
102         jid = g_strdup_printf ("%s@%s", argv[2], argv[1]);
103         lm_connection_set_jid (connection, jid);
104         g_free (jid);
105
106         lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10));
107
108         handler = lm_message_handler_new (handle_messages, NULL, NULL);
109         lm_connection_register_message_handler (connection, handler,
110                                                 LM_MESSAGE_TYPE_MESSAGE,
111                                                 LM_HANDLER_PRIORITY_NORMAL);
112                                                                                 
113         lm_message_handler_unref (handler);
114                                                                                 
115         info = g_new0 (UserInfo, 1);
116         info->name = g_strdup (argv[2]);
117         info->passwd = g_strdup (argv[3]);
118                                                                                 
119         result = lm_connection_open (connection,
120                                      (LmResultFunction) connection_open_cb,
121                                      info, NULL, NULL);
122                                                                                 
123         if (!result) {
124                 g_print ("Opening connection failed: %d\n", result);
125         } else {
126                 g_print ("Returned from the connection_open\n");
127         }
128                                                                                 
129         main_loop = g_main_loop_new (NULL, FALSE);
130         g_main_loop_run (main_loop);
131                                                                                 
132         return 0;
133 }
134