]> git.cworth.org Git - loudgame/blob - lm-echo.c
a93fb545e0ef464eda579b566555875308822d3f
[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 typedef struct {
34     GMainLoop   *main_loop;
35 } msg_data_t;
36  
37 static void
38 free_user_info (UserInfo *info)
39 {
40     g_free (info->name);
41     g_free (info->passwd);
42  
43     g_free (info);
44 }
45  
46 static void
47 authentication_cb (LmConnection *connection, gboolean result, gpointer ud)
48 {
49     g_print ("Auth: %d\n", result);
50     free_user_info ((UserInfo *) ud);
51  
52     if (result == TRUE) {
53         LmMessage *m;
54                  
55         m = lm_message_new_with_sub_type (NULL,
56                                           LM_MESSAGE_TYPE_PRESENCE,
57                                           LM_MESSAGE_SUB_TYPE_AVAILABLE);
58                  
59         lm_connection_send (connection, m, NULL);
60         lm_message_unref (m);
61     }
62
63 }
64  
65 static void
66 connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
67 {
68     lm_connection_authenticate (connection,
69                                 info->name, info->passwd, "TestLM",
70                                 authentication_cb, info, FALSE,  NULL);
71 }
72
73 static void
74 send_reply (LmConnection        *connection,
75             const char          *peer,
76             const char          *message)
77 {
78     LmMessage *reply;
79     gboolean result;
80     GError *error = NULL;
81
82     reply = lm_message_new (peer, LM_MESSAGE_TYPE_MESSAGE);
83
84     lm_message_node_add_child (reply->node, "body", message);
85
86     result = lm_connection_send (connection, reply, &error);
87     lm_message_unref (reply);
88
89     if (! result) {
90         g_error ("lm_connection_send failed");
91     }
92 }
93
94 static void
95 handle_command (LmConnection    *connection,
96                 const char      *peer,
97                 const char      *command,
98                 gpointer         closure)
99 {
100     msg_data_t *data = closure;
101     char *error;
102
103     if (strcmp (command, "quit") == 0) {
104         g_main_loop_quit (data->main_loop);
105         return;
106     }
107
108     error = g_strdup_printf ("Unknown command: '%s'", command);
109     send_reply (connection, peer, error);
110     free (error);
111 }
112  
113 static LmHandlerResult
114 handle_messages (LmMessageHandler *handler,
115                  LmConnection     *connection,
116                  LmMessage        *m,
117                  gpointer          closure)
118 {
119     LmMessageNode *body;
120     const char *peer;
121     const char *body_str;
122
123     peer = lm_message_node_get_attribute (m->node, "from");
124
125     body = lm_message_node_get_child (m->node, "body");
126     if (body) {
127         body_str = lm_message_node_get_value (body);
128
129         if (body_str && body_str[0] == '%')
130             handle_command (connection, peer, body_str + 1, closure);
131         else
132             send_reply (connection, peer, body_str);
133     }
134         
135     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
136 }
137 int
138 main (int argc, char **argv)
139 {
140     LmConnection     *connection;
141     LmMessageHandler *handler;
142     gboolean          result;
143     UserInfo         *info;
144     gchar            *jid;
145     msg_data_t        data;
146     GError           *error;
147
148     if (argc < 6) {
149         g_print ("Usage: %s <server> <username> <password> <connectserver> <connectport>\n", argv[0]);
150         return 1;
151     }
152                                                                                 
153     connection = lm_connection_new (argv[4]);
154
155     jid = g_strdup_printf ("%s@%s", argv[2], argv[1]);
156     lm_connection_set_jid (connection, jid);
157     g_free (jid);
158
159     lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10));
160
161     data.main_loop = g_main_loop_new (NULL, FALSE);
162
163     handler = lm_message_handler_new (handle_messages, &data, NULL);
164     lm_connection_register_message_handler (connection, handler,
165                                             LM_MESSAGE_TYPE_MESSAGE,
166                                             LM_HANDLER_PRIORITY_NORMAL);
167                                                                                 
168     lm_message_handler_unref (handler);
169                                                                                 
170     info = g_new0 (UserInfo, 1);
171     info->name = g_strdup (argv[2]);
172     info->passwd = g_strdup (argv[3]);
173                                                                                 
174     result = lm_connection_open (connection,
175                                  (LmResultFunction) connection_open_cb,
176                                  info, NULL, NULL);
177     if (!result) {
178         g_print ("Opening connection failed: %d\n", result);
179         exit (1);
180     }
181                                                                                 
182     g_main_loop_run (data.main_loop);
183
184     g_main_loop_unref (data.main_loop);
185
186     lm_connection_close (connection, &error);
187     lm_connection_unref (connection);
188                                                                                 
189     return 0;
190 }