2 Copyright (C) 2004-2005 Helge Hess
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "XmlRpcClientTool.h"
23 #include "HandleCredentialsClient.h"
24 #include <NGStreams/NGLocalSocketAddress.h>
25 #include <NGExtensions/NSString+Ext.h>
30 @interface NSObject(Printing)
31 - (void)printWithTool:(XmlRpcClientTool *)_tool;
34 @implementation XmlRpcClientTool
39 return [self initWithArguments:nil];
42 - (id)initWithArguments:(NSArray *)_arguments {
43 if ((self = [super init])) {
48 argc = [_arguments count];
50 [self help:[_arguments objectAtIndex:0]];
54 s = [_arguments objectAtIndex:1];
55 if (![self initXmlRpcClientWithStringURL:s]) {
56 printf("Error initializing the XML-RPC client\n");
62 [self initMethodCall:_arguments];
65 self->methodName = @"system.listMethods";
66 self->parameters = nil;
69 ud = [NSUserDefaults standardUserDefaults];
70 if ([ud boolForKey:@"forceauth"]) {
71 [self->client setUserName:[ud stringForKey:@"login"]];
72 [self->client setPassword:[ud stringForKey:@"password"]];
75 [self->client setDefLogin:[ud stringForKey:@"login"]];
76 [self->client setDefPassword:[ud stringForKey:@"password"]];
82 - (BOOL)initXmlRpcClientWithStringURL:(NSString *)_url {
85 if (![_url isAbsoluteURL]) {
86 /* make a raw, Unix domain socket connection */
87 NGLocalSocketAddress *addr;
89 addr = [NGLocalSocketAddress addressWithPath:_url];
90 self->client = [[HandleCredentialsClient alloc] initWithRawAddress:addr];
94 if ((url = [NSURL URLWithString:_url]) != nil) {
96 if ((uri = [url path]) == nil)
100 self->client = [[HandleCredentialsClient alloc] initWithURL:url];
105 printf("Invalid URL\n");
110 - (void)initMethodCall:(NSArray *)_arguments {
111 self->methodName = [_arguments objectAtIndex:2];
113 if ([_arguments count] > 2) {
114 NSRange range = NSMakeRange(3, [_arguments count] - 3);
115 self->parameters = [[_arguments subarrayWithRange:range] retain];
120 [self->client release];
121 [self->methodName release];
122 [self->parameters release];
128 - (void)printElement:(id)element {
129 [element printWithTool:self];
132 /* printing objects */
134 - (void)printDictionary:(NSDictionary *)_dict {
135 NSEnumerator *dictEnum;
138 dictEnum = [_dict keyEnumerator];
139 while((dictKey = [dictEnum nextObject]) != nil) {
140 printf("%s=", [dictKey cString]);
141 [self printElement:[_dict objectForKey:dictKey]];
145 - (void)printArray:(NSArray *)_array {
146 NSEnumerator *arrayEnum;
149 arrayEnum = [_array objectEnumerator];
150 while((arrayElem = [arrayEnum nextObject]) != nil) {
151 [self printElement:arrayElem];
155 - (void)help:(NSString *)pn {
157 "usage: %s <url> [<method-name>] [<arg1>,...]\n"
158 " sample: %s http://localhost:20000/RPC2 bc 1 2\n",
159 [pn cString], [pn cString]);
169 if (self->client == nil) {
170 NSLog(@"missing XML-RPC client object ...");
175 result = [self->client
176 invokeMethodNamed:self->methodName
177 parameters:self->parameters];
180 while ((result == nil) && loopCount < 20);
183 NSLog(@"call failed, no result (looped %i times) ?!", loopCount);
187 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"plist"]) {
190 s = [result description];
191 printf("%s\n", [s cString]);
194 [self printElement:result];
196 if ([result isKindOfClass:[NSException class]]) {
203 @end /* XmlRpcClientTool */