]> git.cworth.org Git - loudgame/blob - lm-echo.c
312e6fee7bf2728f2abcd5b2c452b68528b7114a
[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         LmMessage *reply;
81         gboolean result;
82         GError *error = NULL;
83         LmMessageNode *body;
84
85         g_print ("Incoming message from: %s\n",
86                  lm_message_node_get_attribute (m->node, "from"));
87
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));
93
94         result = lm_connection_send (connection, reply, &error);
95         lm_message_unref (reply);
96
97         if (! result) {
98                 g_error ("lm_connection_send failed");
99         }
100  
101         return LM_HANDLER_RESULT_REMOVE_MESSAGE;
102 }
103 int
104 main (int argc, char **argv)
105 {
106         GMainLoop        *main_loop;
107         LmConnection     *connection;
108         LmMessageHandler *handler;
109         gboolean          result;
110         UserInfo         *info;
111         gchar            *jid;
112                                                                                 
113         if (argc < 6) {
114                 g_print ("Usage: %s <server> <username> <password> <connectserver> <connectport>\n", argv[0]);
115                 return 1;
116         }
117                                                                                 
118         connection = lm_connection_new (argv[4]);
119
120         jid = g_strdup_printf ("%s@%s", argv[2], argv[1]);
121         lm_connection_set_jid (connection, jid);
122         g_free (jid);
123
124         lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10));
125
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);
130                                                                                 
131         lm_message_handler_unref (handler);
132                                                                                 
133         info = g_new0 (UserInfo, 1);
134         info->name = g_strdup (argv[2]);
135         info->passwd = g_strdup (argv[3]);
136                                                                                 
137         result = lm_connection_open (connection,
138                                      (LmResultFunction) connection_open_cb,
139                                      info, NULL, NULL);
140                                                                                 
141         if (!result) {
142                 g_print ("Opening connection failed: %d\n", result);
143         } else {
144                 g_print ("Returned from the connection_open\n");
145         }
146                                                                                 
147         main_loop = g_main_loop_new (NULL, FALSE);
148         g_main_loop_run (main_loop);
149                                                                                 
150         return 0;
151 }
152