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.

237 lines
5.7KB

  1. /* irc.c - IRC interface */
  2. #define PREFIX_COMMAND_CHAR '!'
  3. #define IRCMSG(msg) irc_cmd_msg(session, creds.channel, msg)
  4. typedef struct
  5. {
  6. char * username;
  7. char * password;
  8. char * channel;
  9. char * server;
  10. int port;
  11. } creds_t;
  12. VARDECL creds_t creds =
  13. {
  14. .username = NULL,
  15. .password = NULL,
  16. .channel = NULL,
  17. .server = NULL, /* localhost? */
  18. #ifndef IRC_SSL_SUPPORT
  19. .port = 6667
  20. #else
  21. .port = 6669
  22. #endif /* !IRC_SSL_SUPPORT */
  23. };
  24. VARDECL char * ident_password = NULL;
  25. VARDECL irc_session_t * session;
  26. VARDECL irc_callbacks_t callbacks;
  27. VARDECL char * current_username = NULL;
  28. VARDECL char const * help_msg =
  29. /* IRC_GREEN "!help " IRC_STOP " : This message\n" */
  30. IRC_GREEN "!remind " IRC_STOP " : Dump current assignment\n"
  31. IRC_GREEN "!reroll " IRC_STOP " : Rerolls assignment\n"
  32. /* IRC_GREEN "!repo <link>" IRC_STOP " : Sets project repository link\n" */
  33. /* IRC_GREEN "!raw <sql> " IRC_STOP " : Execute raw SQL\n" */
  34. /* IRC_GREEN "!dump " IRC_STOP " : List all possible projects\n" */
  35. IRC_GREEN "!request " IRC_STOP " : Request personal project\n"
  36. IRC_GREEN "!remind " IRC_STOP " : Prints your assignment\n";
  37. DECL void parse_command(char const * cmd);
  38. DECL char *
  39. get_username(const char * origin)
  40. {
  41. const char USERNAME_TERMINATOR = '!';
  42. int i = 0;
  43. char * r;
  44. while (origin[i] != USERNAME_TERMINATOR)
  45. { i++; }
  46. r = (char *) malloc(i + 1);
  47. if (r)
  48. {
  49. strncpy(r, origin, i);
  50. r[i] = '\00';
  51. }
  52. return r;
  53. }
  54. DECL void
  55. ircmsg(char const * reciever, char const * fmt,
  56. ...)
  57. {
  58. va_list args;
  59. char * fmtdmsg;
  60. char * swp;
  61. const char * delim = "\n";
  62. char * data;
  63. if(!strcmp(fmt, "") || fmt == NULL)
  64. { return; }
  65. va_start(args, fmt);
  66. if(vasprintf(&fmtdmsg, fmt, args) == -1)
  67. { exit(1); }
  68. puts(fmtdmsg);
  69. data = strtok(fmtdmsg, delim);
  70. do
  71. {
  72. swp = irc_color_convert_to_mirc(data);
  73. irc_cmd_msg(session, reciever, swp);
  74. free(swp);
  75. } while((data = strtok(NULL, delim), data));
  76. free(fmtdmsg);
  77. va_end(args);
  78. }
  79. DECL void
  80. event_connect(irc_session_t * lsession,
  81. char const * event,
  82. char const * origin,
  83. char const ** params,
  84. unsigned int count)
  85. {
  86. (void) event;
  87. (void) origin;
  88. (void) params;
  89. (void) count;
  90. /* msg ChanServ IDENTIFY? */
  91. irc_cmd_join(lsession, creds.channel, 0);
  92. if (ident_password)
  93. {
  94. ircmsg("NickServ", "IDENTIFY %s", ident_password);
  95. memset(ident_password, '\0', strlen(ident_password));
  96. }
  97. #ifdef INITIAL_ASSIGNMENT_MESSAGE
  98. if(is_no_assignment(creds.channel))
  99. {
  100. ircmsg(creds.channel, IRC_RED "No assignment for this channel. Finding a new..." IRC_STOP);
  101. random_assign(creds.channel);
  102. }
  103. ircmsg(creds.channel, remind(creds.channel));
  104. #endif /* INITIAL_ASSIGNMENT_MESSAGE */
  105. }
  106. DECL void
  107. event_channel(irc_session_t * lsession,
  108. char const * event,
  109. char const * origin,
  110. char const ** params,
  111. unsigned int count)
  112. {
  113. /* char const * channel = params[0]; */
  114. char const * message = params[1];
  115. (void) lsession;
  116. (void) event;
  117. (void) origin;
  118. /* (void) channel; */
  119. (void) message;
  120. (void) count;
  121. /* Logs the message */
  122. printf(message);
  123. /* parses the command */
  124. if (*message == PREFIX_COMMAND_CHAR)
  125. { current_username = get_username(origin); }
  126. if (!current_username ||
  127. message[1] == '\0')
  128. { return; }
  129. parse_command(message+1);
  130. free(current_username);
  131. current_username = NULL;
  132. }
  133. /* 'abc' SINGLE
  134. 'def ' SINGLE
  135. 'ghi jkl' MULTI */
  136. DECL int
  137. has_arg(char const * cmd)
  138. {
  139. char const * start = cmd;
  140. while (isalnum(*cmd))
  141. {
  142. if (*cmd == '\0')
  143. { break; }
  144. ++cmd;
  145. }
  146. while (*cmd != '\0')
  147. {
  148. if (!isspace(*cmd))
  149. { return cmd - start; }
  150. ++cmd;
  151. }
  152. return 0;
  153. }
  154. DECL void
  155. parse_command(char const * cmd)
  156. {
  157. size_t i = 0;
  158. char* msgswp = NULL;
  159. /* size_t len = strlen(cmd); */
  160. printf("Handling '%s'\n", cmd);
  161. if (!(i = has_arg(cmd)))
  162. {
  163. /* NO ARGUMENTS */
  164. if (strcmp(cmd, "remind") == 0)
  165. {
  166. msgswp = remind(current_username);
  167. ircmsg(creds.channel, "%s: %s", current_username, msgswp);
  168. }
  169. else if (strcmp(cmd, "help") == 0)
  170. { ircmsg(creds.channel, help_msg); }
  171. else if (strcmp(cmd, "magic") == 0)
  172. { ircmsg(creds.channel, "%s: " IRC_YELLOW "%d" IRC_STOP, current_username, (rand() % 100) + 1); }
  173. #ifndef NO_VULN_COMMANDS
  174. else if (strcmp(cmd, "dump") == 0)
  175. {
  176. ircmsg(creds.channel, "%s: All projects:", current_username);
  177. msgswp = dump();
  178. ircmsg(creds.channel, msgswp);
  179. }
  180. #endif /* !NO_VULN_COMMANDS */
  181. else if (strcmp(cmd, "reroll") == 0)
  182. {
  183. purge_assignments(current_username);
  184. random_assign(current_username);
  185. ircmsg(creds.channel, "%s: %s", current_username, remind(current_username));
  186. }
  187. }
  188. else /* HAS ARGUMENTS */
  189. {
  190. char const * const arg = cmd + i;
  191. printf("argoff: %p; i: %ld; arg: %sEOA\n", cmd + i + 1, i, arg);
  192. #ifndef NO_VULN_COMMANDS
  193. if (strncmp(cmd, "raw", 3) == 0)
  194. {
  195. printf("RAW\n");
  196. /* ircmsg(creds.channel, "%s: Executing SQL `%s'.", current_username, arg); */
  197. msgswp = raw(arg);
  198. ircmsg(creds.channel, msgswp);
  199. } else
  200. #endif /* !NO_VULN_COMMANDS */
  201. if (strncmp(cmd, "repo", 4) == 0)
  202. {
  203. /* ircmsg(creds.channel, "%s: Setting project repository...", current_username); */
  204. set_repo(creds.channel, arg);
  205. msgswp = remind(creds.channel);
  206. ircmsg(creds.channel, "%s: %s", current_username, msgswp);
  207. }
  208. else if (strncmp(cmd, "magic", 5) == 0)
  209. { ircmsg(creds.channel, "%s: " IRC_YELLOW "%d" IRC_STOP, current_username, (rand() % atoi(arg)) + 1); }
  210. else if (strncmp(cmd, "say", 3) == 0)
  211. { ircmsg(creds.channel, "%s", arg); }
  212. }
  213. free(msgswp);
  214. }