Re-indent with 4-space indentation
[loudgame] / lm-echo.c
1 /*
2  * Copyright (C) 2003-2004 Imendio AB
3  *
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.
8  *
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.
13  *
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.
18  */
19  
20 #include <glib.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <loudmouth/loudmouth.h>
24 #ifdef __WIN32__
25 #include <winsock2.h>
26 #endif
27  
28 typedef struct {
29     gchar *name;
30     gchar *passwd;
31 } UserInfo;
32  
33 static void
34 free_user_info (UserInfo *info)
35 {
36     g_free (info->name);
37     g_free (info->passwd);
38  
39     g_free (info);
40 }
41  
42  
43 static void
44 authentication_cb (LmConnection *connection, gboolean result, gpointer ud)
45 {
46     g_print ("Auth: %d\n", result);
47     free_user_info ((UserInfo *) ud);
48  
49     if (result == TRUE) {
50         LmMessage *m;
51                  
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));
56                  
57         lm_connection_send (connection, m, NULL);
58         lm_message_unref (m);
59     }
60
61 }
62  
63 static void
64 connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
65 {
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");
71 }
72  
73 static LmHandlerResult
74 handle_messages (LmMessageHandler *handler,
75                  LmConnection     *connection,
76                  LmMessage        *m,
77                  gpointer          user_data)
78 {
79     LmMessage *reply;
80     gboolean result;
81     GError *error = NULL;
82     LmMessageNode *body;
83
84     g_print ("Incoming message from: %s\n",
85              lm_message_node_get_attribute (m->node, "from"));
86
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));
92
93     result = lm_connection_send (connection, reply, &error);
94     lm_message_unref (reply);
95
96     if (! result) {
97         g_error ("lm_connection_send failed");
98     }
99  
100     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
101 }
102 int
103 main (int argc, char **argv)
104 {
105     GMainLoop        *main_loop;
106     LmConnection     *connection;
107     LmMessageHandler *handler;
108     gboolean          result;
109     UserInfo         *info;
110     gchar            *jid;
111                                                                                 
112     if (argc < 6) {
113         g_print ("Usage: %s <server> <username> <password> <connectserver> <connectport>\n", argv[0]);
114         return 1;
115     }
116                                                                                 
117     connection = lm_connection_new (argv[4]);
118
119     jid = g_strdup_printf ("%s@%s", argv[2], argv[1]);
120     lm_connection_set_jid (connection, jid);
121     g_free (jid);
122
123     lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10));
124
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);
129                                                                                 
130     lm_message_handler_unref (handler);
131                                                                                 
132     info = g_new0 (UserInfo, 1);
133     info->name = g_strdup (argv[2]);
134     info->passwd = g_strdup (argv[3]);
135                                                                                 
136     result = lm_connection_open (connection,
137                                  (LmResultFunction) connection_open_cb,
138                                  info, NULL, NULL);
139                                                                                 
140     if (!result) {
141         g_print ("Opening connection failed: %d\n", result);
142     } else {
143         g_print ("Returned from the connection_open\n");
144     }
145                                                                                 
146     main_loop = g_main_loop_new (NULL, FALSE);
147     g_main_loop_run (main_loop);
148                                                                                 
149     return 0;
150 }