Probiotics (in bot form) for programming.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

224 lines
5.3KB

  1. /* parse.c
  2. Probotic is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License version 3 only as
  4. published by the Free Software Foundation.
  5. Probotic is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License version 3 for more details.
  9. You should have received a copy of the GNU General Public License
  10. version 3 along with Probotic.
  11. */
  12. #define PARAMS_COUNT 6
  13. VARDECL creds_t creds =
  14. {
  15. .username = NULL,
  16. .password = NULL,
  17. .channel = NULL,
  18. .server = NULL, /* localhost? */
  19. #ifndef IRC_SSL_SUPPORT
  20. .port = 6667
  21. #else
  22. .port = 6669
  23. #endif /* IRC_SSL_SUPPORT */
  24. };
  25. #if 0
  26. DECL char **
  27. str_split(char const * s, char c)
  28. {
  29. char ** ret = NULL;
  30. size_t i = 0;
  31. size_t current_token_i = 0;
  32. size_t token_start_i = 0;
  33. size_t tokens_q = 0;
  34. /* count tokens */
  35. for (i = 1; s[i]; ++i)
  36. {
  37. /* end of a token*/
  38. if (s[i] == c && s[i - 1] != c)
  39. { ++tokens_q; }
  40. }
  41. ++tokens_q;
  42. ret = (char **)calloc(tokens_q + 1, sizeof(char *));
  43. if (!ret)
  44. { return ret; }
  45. for (i = 1; s[i]; ++i)
  46. {
  47. if ((s[i + 1] == c || !s[i + 1]) && s[i] != c)
  48. {
  49. /* end of a token*/
  50. ret[current_token_i] = strndup(s + token_start_i, i - token_start_i + 1);
  51. if (!ret[current_token_i])
  52. {
  53. split_clean(ret);
  54. return NULL;
  55. }
  56. ++current_token_i;
  57. }
  58. else if (s[i] != c && s[i - 1] == c)
  59. {
  60. /* start of a token */
  61. token_start_i = i;
  62. }
  63. }
  64. /* Signal that the split array is ended (for iteration purposes) */
  65. ret[current_token_i + 1] = NULL;
  66. return ret;
  67. }
  68. DECL void
  69. split_clean(char ** split)
  70. {
  71. while (*split)
  72. {
  73. free(*split);
  74. }
  75. free(split);
  76. }
  77. #endif /* 0 */
  78. DECL void
  79. parse_command(char const * cmd)
  80. {
  81. size_t i = 0;
  82. char* msgswp = NULL;
  83. /* size_t len = strlen(cmd); */
  84. /* TODO does not handle commands with leading space,
  85. use custom implemented to-spec isspace implementation */
  86. while (cmd[i] != '\0' &&
  87. cmd[i] != ' ')
  88. { ++i; }
  89. if (cmd[i] == '\0')
  90. {
  91. /* no arguments */
  92. #ifndef NO_VULN_COMMANDS
  93. if (strcmp(cmd, "kill") == 0)
  94. { exit(1); }
  95. #endif /* !NO_VULN_COMMANDS */
  96. if (strcmp(cmd, "remind") == 0)
  97. {
  98. msgswp = remind(current_username);
  99. ircmsg(creds.channel, "%s: %s", current_username, msgswp);
  100. }
  101. else if (strcmp(cmd, "help") == 0)
  102. { ircmsg(creds.channel, help_msg); }
  103. else if (strcmp(cmd, "magic") == 0)
  104. { ircmsg(creds.channel, "%s: " IRC_YELLOW "%d" IRC_STOP, current_username, (rand() % 100) + 1); }
  105. else if (strcmp(cmd, "dump") == 0)
  106. {
  107. #ifndef NO_VULN_COMMANDS
  108. ircmsg(creds.channel, "%s: All projects:", current_username);
  109. msgswp = dump();
  110. ircmsg(creds.channel, msgswp);
  111. #else
  112. ircmsg(creds.channel, "%s: dump disabled", current_username);
  113. #endif /* !NO_VULN_COMMANDS */
  114. }
  115. else if (strcmp(cmd, "reroll") == 0)
  116. {
  117. /* ircmsg(creds.channel, "%s: Rerolling...", current_username); */
  118. purge_assignments(current_username);
  119. random_assign(current_username);
  120. ircmsg(creds.channel, "%s: %s", current_username, remind(current_username));
  121. }
  122. }
  123. else
  124. {
  125. /* some arguments */
  126. char const * const arg = cmd + i + 1;
  127. if (strncmp(cmd, "raw", i) == 0)
  128. {
  129. #ifndef NO_VULN_COMMANDS
  130. /* ircmsg(creds.channel, "%s: Executing SQL `%s'.", current_username, arg); */
  131. msgswp = raw(arg);
  132. ircmsg(creds.channel, msgswp);
  133. #else
  134. ircmsg(creds.channel, "%s: raw disabled", current_username);
  135. #endif /* !NO_VULN_COMMANDS */
  136. }
  137. else if (strncmp(cmd, "repo", i) == 0)
  138. {
  139. /* ircmsg(creds.channel, "%s: Setting project repository...", current_username); */
  140. set_repo(creds.channel, arg);
  141. msgswp = remind(creds.channel);
  142. ircmsg(creds.channel, "%s: %s", current_username, msgswp);
  143. }
  144. }
  145. free(msgswp);
  146. }
  147. /* Parses the format username[:password]@server[:port] */
  148. #define GENCOPY(dst,src,l) \
  149. do \
  150. { \
  151. dst = malloc(sep + 1); \
  152. if (dst) \
  153. { \
  154. strncpy(dst,src,l); \
  155. dst[len] = '\0'; \
  156. } \
  157. else \
  158. { return 1; } \
  159. } while (0)
  160. DECL int
  161. parse_url(char const * url)
  162. {
  163. size_t len = strlen(url);
  164. size_t sep = 0, ls = 0, rs;
  165. while (++sep < len && url[sep] != '@');
  166. while (++ls < len && url[ls] != ':')
  167. {
  168. if (ls >= sep)
  169. { ls = 0; break; }
  170. }
  171. rs = sep;
  172. while (++rs < len && url[rs] != ':');
  173. if (rs == len)
  174. { rs = 0; }
  175. GENCOPY(creds.username, url, ls ? ls : sep);
  176. if (ls)
  177. {
  178. GENCOPY(creds.password, url + ls + 1, sep - ls - 1);
  179. }
  180. if (rs)
  181. {
  182. GENCOPY(creds.server, url + sep + 1, rs - sep - 1);
  183. creds.port = atoi(url + rs + 1);
  184. }
  185. else
  186. {
  187. GENCOPY(creds.server, url + sep + 1, len - sep - 1);
  188. }
  189. return 0;
  190. }
  191. DECL void
  192. creds_free(void)
  193. {
  194. FULL_FREE(creds.username);
  195. FULL_FREE(creds.password);
  196. FULL_FREE(creds.channel);
  197. FULL_FREE(creds.server);
  198. }