|
| 1 | +#include <argp.h> |
| 2 | +#include <strings.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include "helper.h" |
| 5 | + |
| 6 | +#include "argument_parser.h" |
| 7 | + |
| 8 | + |
| 9 | +const char *argp_program_version = "xdccget 1.0"; |
| 10 | +const char *argp_program_bug_address ="<[email protected]>"; |
| 11 | + |
| 12 | +/* Program documentation. */ |
| 13 | +static char doc[] = |
| 14 | +"xdccgget -- download from cmd with xdcc"; |
| 15 | + |
| 16 | +/* A description of the arguments we accept. */ |
| 17 | +static char args_doc[] = "<server> <channel(s)> <bot cmds>"; |
| 18 | + |
| 19 | +/* The options we understand. */ |
| 20 | +static struct argp_option options[] = { |
| 21 | +{"verbose", 'v', 0, 0, "Produce verbose output" }, |
| 22 | +{"quiet", 'q', 0, 0, "Don't produce any output" }, |
| 23 | +{"information", 'i', 0, 0, "Produce information output." }, |
| 24 | +#ifdef ENABLE_IPV6 |
| 25 | +{"ipv6", '6', 0, 0, "Use ipv6 instead of ipv4 to connect to irc server." }, |
| 26 | +#endif |
| 27 | +{"port", 'p', "<port number>", 0, "Use the following port to connect to server. default is 6667." }, |
| 28 | +{"directory", 'd', "<download-directory>", 0, "Directory, where to place the files." }, |
| 29 | +{ 0 } |
| 30 | +}; |
| 31 | + |
| 32 | +static error_t parse_opt (int key, char *arg, struct argp_state *state); |
| 33 | + |
| 34 | +/* Our argp parser. */ |
| 35 | +static struct argp argp = { options, parse_opt, args_doc, doc }; |
| 36 | + |
| 37 | +/* Parse a single option. */ |
| 38 | +static error_t parse_opt(int key, char *arg, struct argp_state *state) { |
| 39 | + /* Get the input argument from argp_parse, which we |
| 40 | + know is a pointer to our arguments structure. */ |
| 41 | + struct xdccGetConfig *cfg = state->input; |
| 42 | + |
| 43 | + switch (key) { |
| 44 | + case 'q': |
| 45 | + DBG_OK("setting log-level as quiet."); |
| 46 | + cfg->logLevel = LOG_QUIET; |
| 47 | + break; |
| 48 | + |
| 49 | + case 'v': |
| 50 | + DBG_OK("setting log-level as warn."); |
| 51 | + cfg->logLevel = LOG_WARN; |
| 52 | + break; |
| 53 | + |
| 54 | + case 'i': |
| 55 | + DBG_OK("setting log-level as info."); |
| 56 | + cfg->logLevel = LOG_INFO; |
| 57 | + break; |
| 58 | + |
| 59 | + case 'd': |
| 60 | + DBG_OK("setting target dir as %s.", arg); |
| 61 | + cfg->targetDir = sdsnew(arg); |
| 62 | + break; |
| 63 | + |
| 64 | + case 'p': |
| 65 | + cfg->port = (unsigned short) strtoul(arg, NULL, 0); |
| 66 | + DBG_OK("setting port as %u", cfg->port); |
| 67 | + break; |
| 68 | + |
| 69 | +#ifdef ENABLE_IPV6 |
| 70 | + case '6': |
| 71 | + cfg_set_bit(cfg, USE_IPV6_FLAG); |
| 72 | + break; |
| 73 | +#endif |
| 74 | + |
| 75 | + case ARGP_KEY_ARG: |
| 76 | + { |
| 77 | + if (state->arg_num >= 3) |
| 78 | + /* Too many arguments. */ |
| 79 | + argp_usage(state); |
| 80 | + |
| 81 | + cfg->args[state->arg_num] = arg; |
| 82 | + } |
| 83 | + break; |
| 84 | + |
| 85 | + case ARGP_KEY_END: |
| 86 | + if (state->arg_num < 3) |
| 87 | + /* Not enough arguments. */ |
| 88 | + argp_usage(state); |
| 89 | + break; |
| 90 | + |
| 91 | + default: |
| 92 | + return ARGP_ERR_UNKNOWN; |
| 93 | + } |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +void parseArguments(int argc, char **argv, struct xdccGetConfig *cfg) { |
| 98 | + /* Parse our arguments; every option seen by parse_opt will |
| 99 | + be reflected in arguments. */ |
| 100 | + int ret = argp_parse(&argp, argc, argv, 0, 0, cfg); |
| 101 | + |
| 102 | + if (ret != 0) { |
| 103 | + logprintf(LOG_ERR, "the parsing of the command line options failed"); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +struct dccDownload* newDccDownload(sds botNick, sds xdccCmd) { |
| 108 | + struct dccDownload *t = (struct dccDownload*) Malloc(sizeof (struct dccDownload)); |
| 109 | + t->botNick = botNick; |
| 110 | + t->xdccCmd = xdccCmd; |
| 111 | + return t; |
| 112 | +} |
| 113 | + |
| 114 | +void freeDccDownload(struct dccDownload *t) { |
| 115 | + sdsfree(t->botNick); |
| 116 | + sdsfree(t->xdccCmd); |
| 117 | + FREE(t); |
| 118 | +} |
| 119 | + |
| 120 | +struct dccDownloadProgress* newDccProgress(char *completePath, irc_dcc_size_t complFileSize) { |
| 121 | + struct dccDownloadProgress *t = (struct dccDownloadProgress*) Malloc(sizeof (struct dccDownloadProgress)); |
| 122 | + t->completeFileSize = complFileSize; |
| 123 | + t->sizeRcvd = 0; |
| 124 | + t->sizeNow = 0; |
| 125 | + t->sizeLast = 0; |
| 126 | + t->completePath = completePath; |
| 127 | + return t; |
| 128 | + |
| 129 | +} |
| 130 | + |
| 131 | +void freeDccProgress(struct dccDownloadProgress *progress) { |
| 132 | + sdsfree(progress->completePath); |
| 133 | + FREE(progress); |
| 134 | +} |
| 135 | + |
| 136 | +void parseDccDownload(char *dccDownloadString, sds *nick, sds *xdccCmd) { |
| 137 | + size_t i; |
| 138 | + size_t strLen = strlen(dccDownloadString); |
| 139 | + size_t spaceFound = 0; |
| 140 | + |
| 141 | + for (i = 0; i < strLen; i++) { |
| 142 | + if (dccDownloadString[i] == ' ') { |
| 143 | + spaceFound = i; |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + size_t nickLen = spaceFound + 1; |
| 149 | + size_t cmdLen = (strLen - spaceFound) + 1; |
| 150 | + |
| 151 | + DBG_OK("nickLen = %zu, cmdLen = %zu", nickLen, cmdLen); |
| 152 | + |
| 153 | + sds nickPtr = sdsnewlen(NULL, nickLen); |
| 154 | + sds xdccPtr = sdsnewlen(NULL, cmdLen); |
| 155 | + |
| 156 | + nickPtr = sdscpylen(nickPtr, dccDownloadString, nickLen - 1); |
| 157 | + xdccPtr = sdscpylen(xdccPtr, dccDownloadString + (spaceFound + 1), cmdLen - 1); |
| 158 | + |
| 159 | + *nick = nickPtr; |
| 160 | + *xdccCmd = xdccPtr; |
| 161 | +} |
| 162 | + |
| 163 | +sds* parseChannels(char *channelString, int *numChannels) { |
| 164 | + int numFound = 0; |
| 165 | + char *seperator = ","; |
| 166 | + sds *splittedString = sdssplitlen(channelString, strlen(channelString), seperator, strlen(seperator), &numFound); |
| 167 | + if (splittedString == NULL) { |
| 168 | + DBG_ERR("splittedString = NULL, cant continue from here."); |
| 169 | + } |
| 170 | + int i = 0; |
| 171 | + |
| 172 | + for (i = 0; i < numFound; i++) { |
| 173 | + sdstrim(splittedString[i], " \t"); |
| 174 | + DBG_OK("%d: '%s'", i, splittedString[i]); |
| 175 | + } |
| 176 | + |
| 177 | + *numChannels = numFound; |
| 178 | + |
| 179 | + return splittedString; |
| 180 | +} |
| 181 | + |
| 182 | +struct dccDownload** parseDccDownloads(char *dccDownloadString, unsigned int *numDownloads) { |
| 183 | + int numFound = 0; |
| 184 | + int i = 0, j = 0; |
| 185 | + char *seperator = ","; |
| 186 | + |
| 187 | + sds *splittedString = sdssplitlen(dccDownloadString, strlen(dccDownloadString), seperator, strlen(seperator), &numFound); |
| 188 | + |
| 189 | + if (splittedString == NULL) { |
| 190 | + DBG_ERR("splittedString = NULL, cant continue from here."); |
| 191 | + } |
| 192 | + |
| 193 | + struct dccDownload **dccDownloadArray = (struct dccDownload**) Calloc(numFound + 1, sizeof (struct dccDownload*)); |
| 194 | + |
| 195 | + *numDownloads = numFound; |
| 196 | + |
| 197 | + for (i = 0; i < numFound; i++) { |
| 198 | + sdstrim(splittedString[i], " \t"); |
| 199 | + sds nick = NULL; |
| 200 | + sds xdccCmd = NULL; |
| 201 | + DBG_OK("%d: '%s'\n", i, splittedString[i]); |
| 202 | + parseDccDownload(splittedString[i], &nick, &xdccCmd); |
| 203 | + DBG_OK("%d: '%s' '%s'\n", i, nick, xdccCmd); |
| 204 | + if (nick != NULL && xdccCmd != NULL) { |
| 205 | + dccDownloadArray[j] = newDccDownload(nick, xdccCmd); |
| 206 | + j++; |
| 207 | + } |
| 208 | + else { |
| 209 | + if (nick != NULL) |
| 210 | + sdsfree(nick); |
| 211 | + |
| 212 | + if (xdccCmd != NULL) |
| 213 | + sdsfree(xdccCmd); |
| 214 | + } |
| 215 | + sdsfree(splittedString[i]); |
| 216 | + } |
| 217 | + |
| 218 | + FREE(splittedString); |
| 219 | + return dccDownloadArray; |
| 220 | +} |
0 commit comments