-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjj.c
275 lines (237 loc) · 8.57 KB
/
jj.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*-
Copyright (C) 2008, 2009 Lucas Holt. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
/* For Linux */
#define _BSD_SOURCE
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/client.h>
#define NAME "JustJournal/UNIX"
#define VERSION "2.0.0"
#define ENTRY_MAX 32000
#define USERLEN 16
#define PASSLEN 19
#define HOSTLEN 512
#define HOST "www.justjournal.com"
#define RECENT_POST_COUNT 15
static void usage( const char *name );
static void die_if_fault_occurred( xmlrpc_env *env );
static void getRecentPosts( const char *host, const char *username, const char *password );
int main( int argc, char *argv[] )
{
xmlrpc_env env;
xmlrpc_value * resultP = NULL;
const char * postResult = NULL;
char username[USERLEN];
char password[PASSLEN];
char entry[ENTRY_MAX];
char host[HOSTLEN];
char * title = NULL;
size_t titlelen;
int c;
size_t i;
bool debug = false;
bool hflag = false;
bool uflag = false;
bool pflag = false;
bool rflag = false;
if ( argc < 3 )
usage( argv[0] );
while ((c = getopt( argc, argv, "h:u:p:s:dr" )) != -1) {
switch( c )
{
case 'h': /* host */
if ( strlen(optarg) > HOSTLEN - 16)
{
fprintf( stderr, "host name is too long" );
exit(EXIT_FAILURE);
}
(void) snprintf(host, HOSTLEN, "http://%s/xml-rpc", optarg);
hflag = true;
break;
case 'u': /* username */
strncpy( username, optarg, USERLEN - 1 );
username[USERLEN -1] = '\0';
uflag = true;
break;
case 'p': /* password */
strncpy( password, optarg, PASSLEN - 1 );
password[PASSLEN -1] = '\0';
pflag = true;
break;
case 's': /* subject */
titlelen = strlen(optarg);
if ( (title = malloc((titlelen * sizeof(char)) + 1)) == NULL )
{
fprintf( stderr, "Unable to allocate memory." );
exit(EXIT_FAILURE);
}
strncpy( title, optarg, titlelen );
title[titlelen] = '\0';
break;
case 'r': /* recent entries */
rflag = true;
break;
case 'd': /* debug */
debug = true;
fprintf( stderr, "Debug enabled.\n");
break;
case '?': /* fall through */
default:
usage( argv[0] );
}
}
argc -= optind;
if ( !uflag || !pflag )
{
fprintf( stderr, "Username and password are required.\n" );
usage( argv[0] );
}
/* set host if it's not defined */
if ( !hflag )
(void) snprintf(host, HOSTLEN, "http://%s/xml-rpc", HOST);
if ( debug && hflag )
fprintf( stderr, "host is set to: %s\n", host );
if ( rflag )
{
getRecentPosts( host, username, password );
exit(0);
}
/* Copy the title into the top of entry if it's requested */
entry[0] = '\0';
if ( title != NULL )
(void) snprintf( entry, ENTRY_MAX, "<title>%s</title>", title );
/* Start from title or the beginning and copy the entry from stdin */
for ( i = strlen(entry); i < ENTRY_MAX -1; i++ )
{
c = getchar();
if ( c == EOF )
break;
entry[i] = (char) c;
}
entry[i] = '\0';
if ( i == 0 )
{
fprintf( stderr, "No entry specified." );
exit(EXIT_FAILURE);
}
xmlrpc_client_init( XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION );
xmlrpc_env_init( &env );
resultP = xmlrpc_client_call( &env, host,
"blogger.newPost",
"(sssssb)",
"", /* key, not used */
username, /* journal unique name */
username, /* journal username */
password, /* journal password */
entry, /* blog content */
true ); /* post now */
die_if_fault_occurred( &env );
xmlrpc_read_string( &env, resultP, &postResult );
if ( debug && postResult != NULL )
fprintf( stderr, "Debug: post result is: %s\n", postResult );
die_if_fault_occurred( &env );
free((char *)postResult);
xmlrpc_DECREF( resultP );
xmlrpc_env_clean( &env );
xmlrpc_client_cleanup();
return 0;
}
static void usage( const char *name )
{
fprintf( stderr, "usage: %s -u USERNAME -p PASSWORD [-h host] [-s subject] [-d]\n", name );
exit(EXIT_FAILURE);
}
static void die_if_fault_occurred( xmlrpc_env *env )
{
if ( env == NULL )
exit(EXIT_FAILURE);
if ( env->fault_occurred )
{
if ( env->fault_code == -501 )
fprintf( stderr, "ERROR: Your account info %s",
"might be incorrect.\n" );
else
fprintf( stderr, "XML-RPC Fault: %s (%d)\n",
env->fault_string, env->fault_code );
exit(EXIT_FAILURE);
}
}
static void getRecentPosts( const char *host, const char *username, const char *password )
{
xmlrpc_env env;
xmlrpc_value * resultP = NULL; // array item
xmlrpc_value * firstElementP = NULL; // first element in array
xmlrpc_value * varP = NULL;
const char * postResult = NULL;
int arrsize;
xmlrpc_client_init( XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION );
xmlrpc_env_init( &env );
resultP = xmlrpc_client_call( &env, host,
"blogger.getRecentPosts",
"(ssssi)",
"", /* key, not used */
username, /* journal unique name */
username, /* journal username */
password, /* journal password */
RECENT_POST_COUNT ); /* post count */
die_if_fault_occurred( &env );
arrsize = xmlrpc_array_size( &env, resultP );
die_if_fault_occurred( &env );
//fprintf( stderr, "Array size %d\n", arrsize );
for (int i = 0; i < arrsize; i++ )
{
xmlrpc_array_read_item( &env, resultP, i, &firstElementP);
xmlrpc_struct_find_value( &env, firstElementP, "title", &varP);
if (varP)
{
xmlrpc_read_string( &env, varP, &postResult);
printf( "%d %s\n\n", i, postResult );
free((char *)postResult);
postResult = NULL;
xmlrpc_DECREF( varP );
}
die_if_fault_occurred( &env );
xmlrpc_struct_find_value( &env, firstElementP, "content", &varP);
if (varP)
{
xmlrpc_read_string( &env, varP, &postResult);
printf( "%s\n\n", postResult );
free((char *)postResult);
postResult = NULL;
xmlrpc_DECREF( varP );
}
die_if_fault_occurred( &env );
xmlrpc_DECREF( firstElementP );
}
xmlrpc_DECREF( resultP );
xmlrpc_env_clean( &env );
xmlrpc_client_cleanup();
}