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