-
Notifications
You must be signed in to change notification settings - Fork 2
/
ioup.c
183 lines (157 loc) · 4.15 KB
/
ioup.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
/**
* ioup.c - upload files to pub.iotek.org
*
*
* Copyright (c) 2014, Broseph <dcat (at) iotek (dot) org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <curl/curl.h>
#define IOUP_BASE "http://pub.iotek.org/"
#define IOUP_LIST IOUP_BASE "p/list.php"
#define IOUP_REMOVE IOUP_BASE "remove.php"
#define IOUP_UPLOAD IOUP_BASE "post.php"
#define IOUP_TMP_FILE "/tmp/ioup.stdin"
typedef struct {
const char *xt;
char *token,*name,*file;
bool list,remove,std_in;
} ioup_t;
/* get string after last dot */
const char *
last_dot (const char *s) {
const char *d = strrchr(s, '.');
return ! d || d == s ? NULL : d + 1;
}
void
io_post (ioup_t io) {
FILE *in,*out;
char *url = io.list ? IOUP_LIST : IOUP_UPLOAD;
int chr;
CURL *c;
CURLcode res;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_slist *headerlist = NULL;
struct stat sstat;
static const char buf[] = "Expect:";
/* read from stdin */
if (io.std_in) {
/* interactive mode? */
fstat(0, &sstat);
if (S_ISCHR(sstat.st_mode))
puts("^C: exit, ^D: post");
io.file = IOUP_TMP_FILE;
io.name = "stdin";
out = fopen(io.file, "w");
in = stdin;
/* write to temp file */
if (in && out) {
while (!feof(in)) {
chr = fgetc(in);
if (chr != EOF)
fputc(chr, out);
}
}
fclose(in);
fclose(out);
} else
io.xt = last_dot(io.file);
curl_global_init(CURL_GLOBAL_ALL);
curl_formadd( &formpost,
&lastptr,
CURLFORM_COPYNAME, "token",
CURLFORM_COPYCONTENTS, io.token,
CURLFORM_END);
curl_formadd( &formpost,
&lastptr,
CURLFORM_COPYNAME, "is_ioup",
CURLFORM_COPYCONTENTS, "1",
CURLFORM_END);
if (!io.list && !io.remove) {
curl_formadd( &formpost,
&lastptr,
CURLFORM_COPYNAME, "xt",
CURLFORM_COPYCONTENTS, io.xt,
CURLFORM_END);
curl_formadd( &formpost,
&lastptr,
CURLFORM_COPYNAME, "pdata",
CURLFORM_FILE, io.file,
CURLFORM_END);
}
if (io.remove) {
url = IOUP_REMOVE;
curl_formadd( &formpost,
&lastptr,
CURLFORM_COPYNAME, io.file,
CURLFORM_COPYCONTENTS, "1",
CURLFORM_END);
}
curl_formadd( &formpost,
&lastptr,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "sent",
CURLFORM_END);
c = curl_easy_init();
headerlist = curl_slist_append(headerlist, buf);
if (c) {
curl_easy_setopt(c, CURLOPT_URL, url);
curl_easy_setopt(c, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(c, CURLOPT_HTTPPOST, formpost);
res = curl_easy_perform(c);
if (res != CURLE_OK)
fprintf(stderr, "curl error: %s",
curl_easy_strerror(res));
if (io.std_in)
unlink(io.file);
curl_easy_cleanup(c);
curl_formfree(formpost);
curl_slist_free_all(headerlist);
}
}
int main (int argc, char *argv[]) {
ioup_t io;
/* if TOKEN is defined, hardcode it */
io.token = getenv("IOUP_TOKEN");
io.list = io.remove = io.std_in = false;
io.file = (argc >= 2) ? argv[1] : NULL;
io.name = (argc >= 2) ? argv[1] : "stdin";
if (argc >= 2 && argv[1][0] == '-') {
switch (argv[1][1]) {
case 'l':
io.list = true;
break;
case 'r':
if (argc > 1) {
printf("%s\n", argv[2]);
io.file = argv[2];
io.remove = true;
}
break;
case 'V':
printf("ioup-%s\n", VERSION);
return 0;
}
}
if (argc < 2) io.std_in = true;
io_post(io);
putchar('\n');
return 0;
}