1 /*
2 Copyright (C) 2008 Lucas Holt. All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 1. Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 2. Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12
13 THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 SUCH DAMAGE.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <stdbool.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <limits.h>
34
35 #include <xmlrpc-c/base.h>
36 #include <xmlrpc-c/client.h>
37
38 #define NAME "JustJournal/UNIX"
39 #define VERSION "1.0.2"
40 #define ENTRY_MAX 32000
41 #define USERLEN 16
42 #define PASSLEN 19
43
44 void usage( const char *name );
45 static void die_if_fault_occurred( xmlrpc_env *env );
46
main(int argc,char * argv[])47 int main( int argc, char *argv[] )
48 {
49 xmlrpc_env env;
50 xmlrpc_value * resultP;
51 const char * postResult;
52 char username[USERLEN];
53 char password[PASSLEN];
54 char entry[ENTRY_MAX];
55 char c;
56 int i;
57 bool debug = false;
58
59 if ( argc < 3 )
60 usage( argv[0] );
61
62 while ((c = getopt( argc, argv, "u:p:d" )) != -1) {
63 switch( c )
64 {
65 case 'u':
66 strncpy( username, optarg, USERLEN - 1 );
67 username[USERLEN -1] = '\0';
68 break;
69 case 'p':
70 strncpy( password, optarg, PASSLEN - 1 );
71 password[PASSLEN -1] = '\0';
72 break;
73 case 'd':
74 debug = true;
75 fprintf( stderr, "Debug enabled.\n");
76 break;
77 case '?': /* fall through */
78 default:
79 usage( argv[0] );
80 }
81 }
82 argc -= optind;
83
84 for ( i = 0; i < ENTRY_MAX -1; i++ )
85 {
86 c = getchar();
87 if ( c == EOF )
88 break;
89 entry[i] = c;
90 }
91 entry[i] = '\0';
92
93 if ( i == 0 )
94 {
95 fprintf( stderr, "No entry specified." );
96 exit(1);
97 }
98
99 xmlrpc_client_init( XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION );
100 xmlrpc_env_init( &env );
101
102 resultP = xmlrpc_client_call( &env, "http://www.justjournal.com/xml-rpc",
103 "blogger.newPost",
104 "(sssssb)",
105 "", /* key, not used */
106 username, /* journal unique name */
107 username, /* journal username */
108 password, /* journal password */
109 entry, /* blog content */
110 true ); /* post now */
111 die_if_fault_occurred( &env );
112
113 xmlrpc_read_string( &env, resultP, &postResult );
114 if (debug)
115 fprintf( stderr, "Debug: post result is: %s\n", postResult );
116 die_if_fault_occurred( &env );
117 /* if ( strcmp(postResult,"0") != 0 )
118 fprintf( stderr, "Error posting blog entry.\n" );*/
119 free((char *)postResult);
120
121 xmlrpc_DECREF( resultP );
122 xmlrpc_env_clean( &env );
123 xmlrpc_client_cleanup();
124
125 return 0;
126 }
127
usage(const char * name)128 void usage( const char *name )
129 {
130 fprintf( stderr, "usage: %s -u USERNAME -p PASSWORD\n", name );
131 exit(0);
132 }
133
die_if_fault_occurred(xmlrpc_env * env)134 static void die_if_fault_occurred( xmlrpc_env *env )
135 {
136 if ( env->fault_occurred )
137 {
138 if (env->fault_code == -501)
139 fprintf( stderr, "ERROR: Your account info %s",
140 "might be incorrect.\n" );
141 else
142 fprintf( stderr, "XML-RPC Fault: %s (%d)\n",
143 env->fault_string, env->fault_code );
144 exit(1);
145 }
146 }
147
148