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