2 Copyright (C) 2004 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>
29 @interface NSObject(Printing)
30 - (void)printWithTool:(XmlRpcClientTool *)_tool;
33 @implementation XmlRpcClientTool
38 return [self initWithArguments:nil];
41 - (id)initWithArguments:(NSArray *)_arguments {
42 if ((self = [super init])) {
47 argc = [_arguments count];
49 [self help:[_arguments objectAtIndex:0]];
53 s = [_arguments objectAtIndex:1];
54 if (![self initXmlRpcClientWithStringURL:s]) {
55 printf("Error initializing the XML-RPC client\n");
61 [self initMethodCall:_arguments];
64 self->methodName = @"system.listMethods";
65 self->parameters = nil;
68 ud = [NSUserDefaults standardUserDefaults];
69 if ([ud boolForKey:@"forceauth"]) {
70 [self->client setUserName:[ud stringForKey:@"login"]];
71 [self->client setPassword:[ud stringForKey:@"password"]];
74 [self->client setDefLogin:[ud stringForKey:@"login"]];
75 [self->client setDefPassword:[ud stringForKey:@"password"]];
81 - (BOOL)initXmlRpcClientWithStringURL:(NSString *)_url {
84 if (![_url isAbsoluteURL]) {
85 /* make a raw, Unix domain socket connection */
86 NGLocalSocketAddress *addr;
88 addr = [NGLocalSocketAddress addressWithPath:_url];
89 self->client = [[HandleCredentialsClient alloc] initWithRawAddress:addr];
93 if ((url = [NSURL URLWithString:_url]) != nil) {
95 if ((uri = [url path]) == nil)
99 self->client = [[HandleCredentialsClient alloc] initWithURL:url];
104 printf("Invalid URL\n");
109 - (void)initMethodCall:(NSArray *)_arguments {
110 self->methodName = [_arguments objectAtIndex:2];
112 if ([_arguments count] > 2) {
113 NSRange range = NSMakeRange(3, [_arguments count] - 3);
114 self->parameters = [[_arguments subarrayWithRange:range] retain];
119 [self->client release];
120 [self->methodName release];
121 [self->parameters release];
127 - (void)printElement:(id)element {
128 [element printWithTool:self];
131 /* printing objects */
133 - (void)printDictionary:(NSDictionary *)_dict {
134 NSEnumerator *dictEnum;
137 dictEnum = [_dict keyEnumerator];
138 while((dictKey = [dictEnum nextObject]) != nil) {
139 printf("%s=", [dictKey cString]);
140 [self printElement:[_dict objectForKey:dictKey]];
144 - (void)printArray:(NSArray *)_array {
145 NSEnumerator *arrayEnum;
148 arrayEnum = [_array objectEnumerator];
149 while((arrayElem = [arrayEnum nextObject]) != nil) {
150 [self printElement:arrayElem];
154 - (void)help:(NSString *)pn {
156 "usage: %s <url> [<method-name>] [<arg1>,...]\n"
157 " sample: %s http://localhost:20000/RPC2 bc 1 2\n",
158 [pn cString], [pn cString]);
168 if (self->client == nil) {
169 NSLog(@"missing XML-RPC client object ...");
174 result = [self->client
175 invokeMethodNamed:self->methodName
176 parameters:self->parameters];
179 while ((result == nil) && loopCount < 20);
182 NSLog(@"call failed, no result (looped %i times) ?!", loopCount);
186 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"plist"]) {
189 s = [result description];
190 printf("%s\n", [s cString]);
193 [self printElement:result];
195 if ([result isKindOfClass:[NSException class]]) {
202 @end /* XmlRpcClientTool */