]> git.cworth.org Git - loudgame/blob - lg-echo.c
d10b20f73d845c4da799af649594e5d970a00d05
[loudgame] / lg-echo.c
1 /*
2  * Copyright (C) 2003-2004 Imendio AB
3  * Copyright (C) 2008 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Authors: Imendio AB
19  *          Carl Worth <cworth@cworth.org>
20  */
21  
22 #include <glib.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <loudmouth/loudmouth.h>
26 #ifdef __WIN32__
27 #include <winsock2.h>
28 #endif
29  
30 typedef struct _loudgame {
31     gchar               *server;
32     gchar               *name;
33     gchar               *passwd;
34     LmConnection        *connection;
35     GMainLoop           *main_loop;
36     int                  return_value;
37 } loudgame_t;
38
39 static void
40 loudgame_quit (loudgame_t *lg, int return_value)
41 {
42     GError *error;
43
44     lg->return_value = return_value;
45
46     if (! lm_connection_close (lg->connection, &error))
47         g_print ("An error occurred during lm_connection_close: %s\n",
48                  error->message);
49
50     lm_connection_unref (lg->connection);
51
52     g_main_loop_quit (lg->main_loop);
53 }
54  
55 static void
56 authentication_cb (LmConnection *connection, gboolean result, gpointer closure)
57 {
58     LmMessage *m;
59     loudgame_t *lg = closure;
60
61     if (! result) {
62         g_print ("Authentication for %s failed\n", lg->name);
63         loudgame_quit (lg, 1);
64         return;
65     }
66                  
67     m = lm_message_new_with_sub_type (NULL,
68                                       LM_MESSAGE_TYPE_PRESENCE,
69                                       LM_MESSAGE_SUB_TYPE_AVAILABLE);
70
71     lm_connection_send (connection, m, NULL);
72     lm_message_unref (m);
73 }
74  
75 static void
76 connection_open_cb (LmConnection *connection, gboolean result, loudgame_t *lg)
77 {
78     lm_connection_authenticate (connection,
79                                 lg->name, lg->passwd, "TestLM",
80                                 authentication_cb, lg, FALSE,  NULL);
81 }
82
83 static void
84 send_reply (LmConnection        *connection,
85             const char          *peer,
86             const char          *message,
87             loudgame_t          *lg)
88 {
89     LmMessage *reply;
90     gboolean result;
91     GError *error = NULL;
92
93     reply = lm_message_new (peer, LM_MESSAGE_TYPE_MESSAGE);
94
95     lm_message_node_add_child (reply->node, "body", message);
96
97     result = lm_connection_send (connection, reply, &error);
98     lm_message_unref (reply);
99
100     if (! result) {
101         g_error ("lm_connection_send failed: error->message");
102         loudgame_quit (lg, 1);
103     }
104 }
105
106 static void
107 handle_command (LmConnection    *connection,
108                 const char      *peer,
109                 const char      *command,
110                 loudgame_t      *lg)
111 {
112     char *error;
113
114     if (strcmp (command, "quit") == 0) {
115         loudgame_quit (lg, 0);
116         return;
117     }
118
119     error = g_strdup_printf ("Unknown command: '%s'", command);
120     send_reply (connection, peer, error, lg);
121     free (error);
122 }
123  
124 static LmHandlerResult
125 handle_messages (LmMessageHandler *handler,
126                  LmConnection     *connection,
127                  LmMessage        *m,
128                  gpointer          closure)
129 {
130     loudgame_t *lg = closure;
131     LmMessageNode *body;
132     const char *peer;
133     const char *body_str;
134
135     peer = lm_message_node_get_attribute (m->node, "from");
136
137     body = lm_message_node_get_child (m->node, "body");
138     if (body) {
139         body_str = lm_message_node_get_value (body);
140
141         if (body_str && body_str[0] == '%')
142             handle_command (connection, peer, body_str + 1, lg);
143         else
144             send_reply (connection, peer, body_str, lg);
145     }
146         
147     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
148 }
149
150 static gboolean
151 make_connection (gpointer closure)
152 {
153     loudgame_t       *lg;
154     LmMessageHandler *handler;
155     gchar            *jid;
156     GError           *error;
157
158     lg = closure;
159                                                                                 
160     lg->connection = lm_connection_new (lg->server);
161
162     jid = g_strdup_printf ("%s@%s", lg->name, lg->server);
163     lm_connection_set_jid (lg->connection, jid);
164     g_free (jid);
165
166     handler = lm_message_handler_new (handle_messages, lg, NULL);
167     lm_connection_register_message_handler (lg->connection,
168                                             handler,
169                                             LM_MESSAGE_TYPE_MESSAGE,
170                                             LM_HANDLER_PRIORITY_NORMAL);
171                                                                                 
172     lm_message_handler_unref (handler);
173                                                                                 
174     if (! lm_connection_open (lg->connection,
175                               (LmResultFunction) connection_open_cb,
176                               lg, NULL, &error))
177     {
178         g_print ("Opening connection failed: %s\n", error->message);
179         loudgame_quit (lg, 1);
180     }
181
182     /* Return false to not schedule another call. */
183     return 0;
184 }
185
186 int
187 main (int argc, char **argv)
188 {
189     loudgame_t lg;
190
191     if (argc != 4) {
192         g_print ("Usage: %s <server> <username> <password>\n", argv[0]);
193         return 1;
194     }
195
196     lg.server = argv[1];
197     lg.name = argv[2];
198     lg.passwd = argv[3];
199
200     lg.connection = NULL;
201
202     lg.main_loop = g_main_loop_new (NULL, FALSE);
203
204     g_idle_add (make_connection, &lg);
205
206     g_main_loop_run (lg.main_loop);
207
208     g_main_loop_unref (lg.main_loop);
209                                                                                 
210     return lg.return_value;
211 }