]> err.no Git - sope/blob - xmlrpc_call/XmlRpcClientTool.m
added missing pref check
[sope] / xmlrpc_call / XmlRpcClientTool.m
1 /*
2   Copyright (C) 2004 Helge Hess
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "XmlRpcClientTool.h"
23 #include "HandleCredentialsClient.h"
24 #include <NGStreams/NGLocalSocketAddress.h>
25 #include "common.h"
26
27 #define EXIT_FAIL -1
28
29 @interface NSObject(Printing)
30 - (void)printWithTool:(XmlRpcClientTool *)_tool;
31 @end
32
33 @implementation XmlRpcClientTool
34
35 /* initialization */
36
37 - (id)init {
38   return [self initWithArguments:nil];
39 }
40
41 - (id)initWithArguments:(NSArray *)_arguments {
42   if ((self = [super init])) {
43     NSUserDefaults *ud;
44     NSString *s;
45     int argc;
46
47     argc = [_arguments count];
48     if(argc == 1) {
49       [self help:[_arguments objectAtIndex:0]];
50       return nil;
51     }
52     
53     s = [_arguments objectAtIndex:1];
54     if (![self initXmlRpcClientWithStringURL:s]) {
55       printf("Error initializing the XML-RPC client\n");
56       [self release];
57       return nil;
58     }
59     
60     if (argc > 2) {
61       [self initMethodCall:_arguments];
62     }
63     else {
64       self->methodName = @"system.listMethods";
65       self->parameters = nil;
66     }
67     
68     ud = [NSUserDefaults standardUserDefaults];
69     if ([ud boolForKey:@"forceauth"]) {
70       [self->client setUserName:[ud stringForKey:@"login"]];
71       [self->client setPassword:[ud stringForKey:@"password"]];
72     }
73     else {
74       [self->client setDefLogin:[ud stringForKey:@"login"]];
75       [self->client setDefPassword:[ud stringForKey:@"password"]];
76     }
77   }
78   return self;
79 }
80
81 - (BOOL)initXmlRpcClientWithStringURL:(NSString *)_url {
82   NSURL *url = nil;
83   
84   if (![_url isAbsoluteURL]) {
85     /* make a raw, Unix domain socket connection */
86     NGLocalSocketAddress *addr;
87     
88     addr = [NGLocalSocketAddress addressWithPath:_url];
89     self->client = [[HandleCredentialsClient alloc] initWithRawAddress:addr];
90     return YES;
91   }
92   
93   if ((url = [NSURL URLWithString:_url]) != nil) {
94 #if 0
95     if ((uri = [url path]) == nil)
96       uri = @"/RPC2";
97 #endif
98     
99     self->client = [[HandleCredentialsClient alloc] initWithURL:url];
100     return YES;
101
102   }
103   else {
104     printf("Invalid URL\n");
105     return NO;
106   }
107 }
108
109 - (void)initMethodCall:(NSArray *)_arguments {
110   self->methodName = [_arguments objectAtIndex:2];
111
112   if ([_arguments count] > 2) {
113     NSRange range = NSMakeRange(3, [_arguments count] - 3);
114     self->parameters = [[_arguments subarrayWithRange:range] retain];
115   }
116 }
117
118 - (void)dealloc {
119   [self->client      release];
120   [self->methodName  release];
121   [self->parameters  release];
122   [super dealloc];
123 }
124
125 /* printing */
126
127 - (void)printElement:(id)element {
128   [element printWithTool:self];
129 }
130
131 /* printing objects */
132
133 - (void)printDictionary:(NSDictionary *)_dict {
134   NSEnumerator *dictEnum;
135   NSString     *dictKey;
136   
137   dictEnum = [_dict keyEnumerator];
138   while((dictKey = [dictEnum nextObject]) != nil) {
139     printf("%s=", [dictKey cString]);    
140     [self printElement:[_dict objectForKey:dictKey]];
141   }
142 }
143
144 - (void)printArray:(NSArray *)_array {
145   NSEnumerator *arrayEnum;
146   id arrayElem;
147   
148   arrayEnum = [_array objectEnumerator];
149   while((arrayElem = [arrayEnum nextObject]) != nil) {
150     [self printElement:arrayElem];
151   }
152 }
153
154 - (void)help:(NSString *)pn {
155   fprintf(stderr,
156           "usage:    %s <url> [<method-name>] [<arg1>,...]\n"
157           "  sample: %s http://localhost:20000/RPC2 bc 1 2\n",
158           [pn cString], [pn cString]);
159 }
160
161 /* running */
162
163 - (int)run {
164   int  exitCode  = 0;
165   int  loopCount = 0;
166   id   result;
167
168   if (self->client == nil) {
169     NSLog(@"missing XML-RPC client object ...");
170     return EXIT_FAIL;
171   }
172   
173   do {
174     result = [self->client
175                   invokeMethodNamed:self->methodName
176                   parameters:self->parameters];
177     loopCount++;
178   }
179   while ((result == nil) && loopCount < 20);
180
181   if (result == nil) {
182     NSLog(@"call failed, no result (looped %i times) ?!", loopCount);
183     return EXIT_FAIL;
184   }
185
186   if ([[NSUserDefaults standardUserDefaults] boolForKey:@"plist"]) {
187     NSString *s;
188     
189     s = [result description];
190     printf("%s\n", [s cString]);
191   }
192   else
193     [self printElement:result];
194   
195   if ([result isKindOfClass:[NSException class]]) {
196     exitCode = 255;
197   }
198
199   return exitCode;
200 }
201
202 @end /* XmlRpcClientTool */