1 |
|
/* |
2 |
|
* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
3 |
|
/* |
4 |
|
* Copyright (C) 2003 Mikael Hallendal <micke@codefactory.se> |
5 |
|
* Copyright (C) 2003 CodeFactory AB. |
6 |
|
* Copyright (C) 2003 Gopal . V <gopalv82@symonds.net> |
7 |
|
* |
8 |
|
* This program is free software; you can redistribute it and/or |
9 |
|
* modify it under the terms of the GNU Lesser General Public License as |
10 |
|
* published by the Free Software Foundation; either version 2 of the |
11 |
|
* License, or (at your option) any later version. |
12 |
|
* |
13 |
|
* This program is distributed in the hope that it will be useful, |
14 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 |
|
* Lesser General Public License for more details. |
17 |
|
* |
18 |
|
* You should have received a copy of the GNU Lesser General Public |
19 |
|
* License along with this program; if not, write to the |
20 |
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
21 |
|
* Boston, MA 02111-1307, USA. |
22 |
|
*/ |
23 |
|
#include <glib.h> |
24 |
|
#include <string.h> |
25 |
|
#include <loudmouth/loudmouth.h> |
26 |
|
|
27 |
|
#include <gwreq.h> |
28 |
|
#include <gwlog.h> |
29 |
|
#include <gwclient.h> |
30 |
|
#include <gwapi.h> |
31 |
|
#include <gwmsgchain.h> |
32 |
|
#include <gwreadconf.h> |
33 |
|
#include <gwgetenv.h> |
34 |
|
#include "dgfid.h" |
35 |
|
|
36 |
|
#define USAGE "Usage: dgee-client <server> <username> <password>\n" |
37 |
|
|
38 |
|
static int |
39 |
|
dummy_call_service(LmConnection *conn, LmMessage *incoming, char * xmlcall) |
40 |
|
{ |
41 |
|
LmMessage *outgoing = NULL; |
42 |
|
LmMessageNode *query = NULL; |
43 |
|
outgoing=lm_message_new_with_sub_type(lm_message_node_get_attribute ( |
44 |
|
incoming->node, "from"), |
45 |
|
LM_MESSAGE_TYPE_IQ, |
46 |
|
LM_MESSAGE_SUB_TYPE_RESULT); |
47 |
|
query = lm_message_node_add_child (outgoing->node, |
48 |
|
"query", |
49 |
|
"haha !"); |
50 |
|
lm_message_node_set_attribute(query, "xmlns", "jabber:iq:rpc"); |
51 |
|
lm_connection_send (conn, outgoing, NULL); |
52 |
|
return 0; |
53 |
|
} |
54 |
|
|
55 |
|
static int |
56 |
|
dgee_call_service(LmConnection *conn, LmMessage *incoming, char * xmlcall) |
57 |
|
{ |
58 |
|
LmMessage *outgoing = NULL; |
59 |
|
LmMessageNode *query = NULL; |
60 |
|
GWMessage msg = {0}; |
61 |
|
|
62 |
|
MsgChain *request = NULL; |
63 |
|
MsgChain *reply = NULL; |
64 |
|
|
65 |
|
void *stream = NULL; |
66 |
|
char *ctype = NULL; |
67 |
|
char *xml = NULL; |
68 |
|
char *URI = NULL; |
69 |
|
char *ARG = NULL; |
70 |
|
|
71 |
|
long clen = 0; |
72 |
|
long len = 0; |
73 |
|
int ret = 0; |
74 |
|
|
75 |
|
char lenstr[20] = {0}; |
76 |
|
|
77 |
|
#define CLEANUP_RESOURCES { if( request ) gwmc_destroy( &request ); if( reply ) gwmc_destroy( &reply ); } |
78 |
|
|
79 |
|
/* Doing gw_init() repeatedly is okay as it'll try and keep track of |
80 |
|
* when it's connected or not |
81 |
|
*/ |
82 |
|
if( gw_init() < 0 ) { |
83 |
|
return 0; |
84 |
|
} |
85 |
|
|
86 |
|
gw_logf( LOG_INTDBG, "Apache mod_dgee client Starting" ); |
87 |
|
|
88 |
|
/* Create a request buffer and populate it... |
89 |
|
*/ |
90 |
|
if( (request = gwmc_create() ) == NULL ) { |
91 |
|
return 0; |
92 |
|
} |
93 |
|
|
94 |
|
URI = "/wstest.dgmx"; |
95 |
|
|
96 |
|
/* Add URI and request XML to buffer |
97 |
|
*/ |
98 |
|
gwmc_add_data( request, DGF_REQ_URI, MIME_TEXT_PLAIN, URI, strlen(URI) ); |
99 |
|
gwmc_add_data( request, DGF_REQ_DATA, MIME_TEXT_PLAIN, xmlcall, strlen(xmlcall) ); |
100 |
|
|
101 |
|
/* Get data stream to send |
102 |
|
*/ |
103 |
|
if( (stream = gwmc_get_message_ptr( request, &len )) == NULL ) |
104 |
|
{ |
105 |
|
CLEANUP_RESOURCES; |
106 |
|
return 0; |
107 |
|
} |
108 |
|
|
109 |
|
printf("[%s]\n",xmlcall); |
110 |
|
|
111 |
|
if( ( ret = gw_svc_call( "/DGEE/SM/execXmlRpc", stream, len, &msg, 0 )) < 0 ) |
112 |
|
{ |
113 |
|
|
114 |
|
CLEANUP_RESOURCES; |
115 |
|
|
116 |
|
switch( gwerror ) { |
117 |
|
case GWE_APPERR: /* Act on application errors */ |
118 |
|
/* Should have XML Fault Reply! */ |
119 |
|
break; |
120 |
|
|
121 |
|
case GWE_NOSVC: |
122 |
|
case GWE_SBLOCKD: return 0; |
123 |
|
|
124 |
|
default: return 0; |
125 |
|
} |
126 |
|
} |
127 |
|
|
128 |
|
/* Instantiate a 'message buffer' from input data |
129 |
|
*/ |
130 |
|
if( (reply = gwmc_importGWMessage( &msg )) == NULL ) { |
131 |
|
gw_logf( LOG_ERROR, "Could not instantiate request message buffer" ); |
132 |
|
CLEANUP_RESOURCES; |
133 |
|
return 0; |
134 |
|
} |
135 |
|
|
136 |
|
ctype = MIME_TEXT_XML; |
137 |
|
xml = (char*)gwmc_read_data( reply, DGF_XMLRPC_REPLY, &ctype, 1, &len ); |
138 |
|
|
139 |
|
if( !xml || len == 0 ) { |
140 |
|
CLEANUP_RESOURCES; |
141 |
|
return 0; |
142 |
|
} |
143 |
|
|
144 |
|
outgoing=lm_message_new_with_sub_type(lm_message_node_get_attribute ( |
145 |
|
incoming->node, "from"), |
146 |
|
LM_MESSAGE_TYPE_IQ, |
147 |
|
LM_MESSAGE_SUB_TYPE_RESULT); |
148 |
|
query = lm_message_node_add_child (outgoing->node, |
149 |
|
"query", |
150 |
|
xml); |
151 |
|
lm_message_node_set_raw_mode(query, TRUE); |
152 |
|
lm_message_node_set_attribute(query, "xmlns", "jabber:iq:rpc"); |
153 |
|
lm_connection_send (conn, outgoing, NULL); |
154 |
|
|
155 |
|
CLEANUP_RESOURCES; |
156 |
|
|
157 |
|
gw_logf( LOG_INTDBG, "Apache mod_dgee client Exiting" ); |
158 |
|
|
159 |
|
return 1; |
160 |
|
} |
161 |
|
|
162 |
|
typedef struct { |
163 |
|
gchar *name; |
164 |
|
gchar *passwd; |
165 |
|
} UserInfo; |
166 |
|
|
167 |
|
static void |
168 |
|
authentication_cb (LmConnection *connection, gboolean result, gpointer ud) |
169 |
|
{ |
170 |
|
g_print ("Auth: %d\n", result); |
171 |
|
|
172 |
|
if (result == TRUE) { |
173 |
|
LmMessage *m; |
174 |
|
|
175 |
|
m = lm_message_new_with_sub_type (NULL, |
176 |
|
LM_MESSAGE_TYPE_PRESENCE, |
177 |
|
LM_MESSAGE_SUB_TYPE_AVAILABLE); |
178 |
|
g_print (":: %s\n", lm_message_node_to_string (m->node)); |
179 |
|
|
180 |
|
lm_connection_send (connection, m, NULL); |
181 |
|
lm_message_unref (m); |
182 |
|
} |
183 |
|
} |
184 |
|
|
185 |
|
static void |
186 |
|
connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info) |
187 |
|
{ |
188 |
|
g_print ("Connected callback\n"); |
189 |
|
lm_connection_authenticate (connection, |
190 |
|
info->name, info->passwd, "DGEE", |
191 |
|
FALSE, authentication_cb, NULL, NULL); |
192 |
|
g_print ("Sent auth message\n"); |
193 |
|
} |
194 |
|
|
195 |
|
static LmHandlerResult |
196 |
|
handle_messages (LmMessageHandler *handler, |
197 |
|
LmConnection *connection, |
198 |
|
LmMessage *m, |
199 |
|
gpointer user_data) |
200 |
|
{ |
201 |
|
g_print ("Incoming message from: %s\n", |
202 |
|
lm_message_node_get_attribute (m->node, "from")); |
203 |
|
|
204 |
|
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
205 |
|
} |
206 |
|
|
207 |
|
static LmHandlerResult |
208 |
|
handle_rpc (LmMessageHandler *handler, |
209 |
|
LmConnection *connection, |
210 |
|
LmMessage *m, |
211 |
|
gpointer user_data) |
212 |
|
{ |
213 |
|
LmMessageNode *query; |
214 |
|
LmMessageNode *methodcall; |
215 |
|
g_print ("Incoming RPC from: %s\n", |
216 |
|
lm_message_node_get_attribute (m->node, "from")); |
217 |
|
|
218 |
|
query= lm_message_node_get_child (m->node,"query"); |
219 |
|
if(query==NULL || |
220 |
|
(strcmp(lm_message_node_get_attribute(query, "xmlns"),"jabber:iq:rpc")!=0)) |
221 |
|
{ |
222 |
|
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
223 |
|
} |
224 |
|
|
225 |
|
methodcall = lm_message_node_get_child(query, "methodcall"); |
226 |
|
|
227 |
|
dgee_call_service(connection, m, lm_message_node_to_string(methodcall)); |
228 |
|
/* dummy_call_service(connection, m, lm_message_node_to_string(methodcall)); */ |
229 |
|
|
230 |
|
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
231 |
|
} |
232 |
|
|
233 |
|
int |
234 |
|
main (int argc, char **argv) |
235 |
|
{ |
236 |
|
GMainLoop *main_loop; |
237 |
|
LmConnection *connection; |
238 |
|
LmMessageHandler *handler; |
239 |
|
LmMessageHandler *rpchandler; |
240 |
|
gboolean result; |
241 |
|
UserInfo *info; |
242 |
|
|
243 |
|
if (argc < 4) { |
244 |
|
g_print (USAGE); |
245 |
|
return 1; |
246 |
|
} |
247 |
|
if(gw_init()<0) |
248 |
|
{ |
249 |
|
printf("Could not start DGEE connection !\n"); |
250 |
|
return 1; |
251 |
|
} |
252 |
|
gw_logf( LOG_AUDIT, "DGClient started" ); |
253 |
|
|
254 |
|
connection = lm_connection_new (argv[1]); |
255 |
|
|
256 |
|
handler = lm_message_handler_new (handle_messages, NULL, NULL); |
257 |
|
rpchandler = lm_message_handler_new (handle_rpc, NULL, NULL); |
258 |
|
lm_connection_register_message_handler (connection, handler, |
259 |
|
LM_MESSAGE_TYPE_MESSAGE, |
260 |
|
LM_HANDLER_PRIORITY_NORMAL); |
261 |
|
lm_connection_register_message_handler (connection, rpchandler, |
262 |
|
LM_MESSAGE_TYPE_IQ, |
263 |
|
LM_HANDLER_PRIORITY_NORMAL); |
264 |
|
|
265 |
|
lm_message_handler_unref (handler); |
266 |
|
|
267 |
|
info = g_new0 (UserInfo, 1); |
268 |
|
info->name = g_strdup (argv[2]); |
269 |
|
info->passwd = g_strdup (argv[3]); |
270 |
|
|
271 |
|
result = lm_connection_open (connection, |
272 |
|
(LmResultFunction) connection_open_cb, |
273 |
|
info, NULL, NULL); |
274 |
|
|
275 |
|
if (!result) { |
276 |
|
g_print ("Opening connection failed: %d\n", result); |
277 |
|
} else { |
278 |
|
g_print ("Returned from the connection_open\n"); |
279 |
|
} |
280 |
|
|
281 |
|
main_loop = g_main_loop_new (NULL, FALSE); |
282 |
|
g_main_loop_run (main_loop); |
283 |
|
|
284 |
|
gw_release(1); /* Drop connection to Goldwater application (1 = forced) */ |
285 |
|
|
286 |
|
return 0; |
287 |
|
} |