This repository has been archived by the owner on Mar 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
argument_parser.c
255 lines (204 loc) · 7.55 KB
/
argument_parser.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
#include <argp.h>
#include <strings.h>
#include <stdlib.h>
#include "helper.h"
#include "argument_parser.h"
const char *argp_program_version = "xdccget 1.0";
const char *argp_program_bug_address ="<[email protected]>";
/* Program documentation. */
static char doc[] =
"xdccgget -- download from cmd with xdcc";
/* A description of the arguments we accept. */
static char args_doc[] = "<server> <channel(s)> <bot cmds>";
#define OPT_ACCEPT_ALL_NICKS 1
#define OPT_DONT_CONFIRM_OFFSETS 2
/* The options we understand. */
static struct argp_option options[] = {
{"verbose", 'v', 0, 0, "Produce verbose output", 0 },
{"quiet", 'q', 0, 0, "Don't produce any output", 0 },
{"information", 'i', 0, 0, "Produce information output.", 0 },
{"checksum-verify", 'c', 0, 0, "Stay connected after download completed to verify checksums.", 0 },
{"ipv4", '4', 0, 0, "Use ipv4 to connect to irc server.", 0 },
#ifdef ENABLE_IPV6
{"ipv6", '6', 0, 0, "Use ipv6 to connect to irc server.", 0 },
#endif
{"port", 'p', "<port number>", 0, "Use the following port to connect to server. default is 6667.", 0 },
{"directory", 'd', "<download-directory>", 0, "Directory, where to place the files." , 0 },
{"nick", 'n', "<nickname>", 0, "Use this specific nickname while connecting to the irc-server.", 0 },
{"login", 'l', "<login-command>", 0, "Use this login-command to authorize your nick to the irc-server after connecting.", 0 },
{"accept-all-nicks", OPT_ACCEPT_ALL_NICKS, 0, 0, "Accept DCC send requests from ALL bots and do not verify any nicknames of incoming dcc requests.", 0 },
{"dont-confirm-offsets", OPT_DONT_CONFIRM_OFFSETS, 0, 0, "Do not send file offsets to the bots. Can be used on bots where the transfer gets stucked after a short while.", 0 },
{ 0 }
};
static error_t parse_opt (int key, char *arg, struct argp_state *state);
/* Our argp parser. */
static struct argp argp = { options, parse_opt, args_doc, doc, NULL, NULL, NULL };
/* Parse a single option. */
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct xdccGetConfig *cfg = state->input;
switch (key) {
case 'q':
DBG_OK("setting log-level as quiet.");
cfg->logLevel = LOG_QUIET;
break;
case 'v':
DBG_OK("setting log-level as warn.");
cfg->logLevel = LOG_WARN;
break;
case 'i':
DBG_OK("setting log-level as info.");
cfg->logLevel = LOG_INFO;
break;
case 'c':
DBG_OK("setting verify checksum option.");
cfg_set_bit(cfg, VERIFY_CHECKSUM_FLAG);
break;
case 'd':
DBG_OK("setting target dir as %s", arg);
cfg->targetDir = sdsnew(arg);
break;
case 'n':
DBG_OK("setting nickname as %s", arg);
cfg->nick = sdsnew(arg);
break;
case 'l':
DBG_OK("setting login-command as %s", arg);
cfg->login_command = sdsnew(arg);
break;
case 'p':
cfg->port = (unsigned short) strtoul(arg, NULL, 0);
DBG_OK("setting port as %u", cfg->port);
break;
case OPT_ACCEPT_ALL_NICKS:
cfg_set_bit(cfg, ACCEPT_ALL_NICKS_FLAG);
break;
case OPT_DONT_CONFIRM_OFFSETS:
cfg_set_bit(cfg, DONT_CONFIRM_OFFSETS_FLAG);
break;
case '4':
cfg_set_bit(cfg, USE_IPV4_FLAG);
break;
#ifdef ENABLE_IPV6
case '6':
cfg_set_bit(cfg, USE_IPV6_FLAG);
break;
#endif
case ARGP_KEY_ARG:
{
if (state->arg_num >= 3)
/* Too many arguments. */
argp_usage(state);
cfg->args[state->arg_num] = arg;
}
break;
case ARGP_KEY_END:
if (state->arg_num < 3)
/* Not enough arguments. */
argp_usage(state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
void parseArguments(int argc, char **argv, struct xdccGetConfig *cfg) {
/* Parse our arguments; every option seen by parse_opt will
be reflected in arguments. */
int ret = argp_parse(&argp, argc, argv, 0, 0, cfg);
if (ret != 0) {
logprintf(LOG_ERR, "the parsing of the command line options failed");
}
}
struct dccDownload* newDccDownload(sds botNick, sds xdccCmd) {
struct dccDownload *t = (struct dccDownload*) Malloc(sizeof (struct dccDownload));
t->botNick = botNick;
t->xdccCmd = xdccCmd;
return t;
}
void freeDccDownload(struct dccDownload *t) {
sdsfree(t->botNick);
sdsfree(t->xdccCmd);
FREE(t);
}
struct dccDownloadProgress* newDccProgress(char *completePath, irc_dcc_size_t complFileSize) {
struct dccDownloadProgress *t = (struct dccDownloadProgress*) Malloc(sizeof (struct dccDownloadProgress));
t->completeFileSize = complFileSize;
t->sizeRcvd = 0;
t->sizeNow = 0;
t->sizeLast = 0;
t->completePath = completePath;
return t;
}
void freeDccProgress(struct dccDownloadProgress *progress) {
sdsfree(progress->completePath);
FREE(progress);
}
void parseDccDownload(char *dccDownloadString, sds *nick, sds *xdccCmd) {
size_t i;
size_t strLen = strlen(dccDownloadString);
size_t spaceFound = 0;
for (i = 0; i < strLen; i++) {
if (dccDownloadString[i] == ' ') {
spaceFound = i;
break;
}
}
size_t nickLen = spaceFound + 1;
size_t cmdLen = (strLen - spaceFound) + 1;
DBG_OK("nickLen = %zu, cmdLen = %zu", nickLen, cmdLen);
sds nickPtr = sdsnewlen(NULL, nickLen);
sds xdccPtr = sdsnewlen(NULL, cmdLen);
nickPtr = sdscpylen(nickPtr, dccDownloadString, nickLen - 1);
xdccPtr = sdscpylen(xdccPtr, dccDownloadString + (spaceFound + 1), cmdLen - 1);
*nick = nickPtr;
*xdccCmd = xdccPtr;
}
sds* parseChannels(char *channelString, uint32_t *numChannels) {
int numFound = 0;
char *seperator = ",";
sds *splittedString = sdssplitlen(channelString, strlen(channelString), seperator, strlen(seperator), &numFound);
if (splittedString == NULL) {
DBG_ERR("splittedString = NULL, cant continue from here.");
}
int i = 0;
for (i = 0; i < numFound; i++) {
sdstrim(splittedString[i], " \t");
DBG_OK("%d: '%s'", i, splittedString[i]);
}
*numChannels = numFound;
return splittedString;
}
struct dccDownload** parseDccDownloads(char *dccDownloadString, unsigned int *numDownloads) {
int numFound = 0;
int i = 0, j = 0;
char *seperator = ",";
sds *splittedString = sdssplitlen(dccDownloadString, strlen(dccDownloadString), seperator, strlen(seperator), &numFound);
if (splittedString == NULL) {
DBG_ERR("splittedString = NULL, cant continue from here.");
}
struct dccDownload **dccDownloadArray = (struct dccDownload**) Calloc(numFound + 1, sizeof (struct dccDownload*));
*numDownloads = numFound;
for (i = 0; i < numFound; i++) {
sdstrim(splittedString[i], " \t");
sds nick = NULL;
sds xdccCmd = NULL;
DBG_OK("%d: '%s'\n", i, splittedString[i]);
parseDccDownload(splittedString[i], &nick, &xdccCmd);
DBG_OK("%d: '%s' '%s'\n", i, nick, xdccCmd);
if (nick != NULL && xdccCmd != NULL) {
dccDownloadArray[j] = newDccDownload(nick, xdccCmd);
j++;
}
else {
if (nick != NULL)
sdsfree(nick);
if (xdccCmd != NULL)
sdsfree(xdccCmd);
}
sdsfree(splittedString[i]);
}
FREE(splittedString);
return dccDownloadArray;
}