Probiotics (in bot form) for programming.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

178 lignes
3.8KB

  1. /* main.c */
  2. #define VERSION_STRING "2"
  3. /* Parses the format username[:password]@server[:port]
  4. * user:password@server:port = userNULpasswordNULserverNUL (port N/A)
  5. */
  6. DECL int
  7. parse_url(char * url)
  8. {
  9. size_t len = strlen(url);
  10. size_t sep = 0, ls = 0, rs;
  11. while (++sep < len && url[sep] != '@');
  12. while (++ls < len && url[ls] != ':')
  13. {
  14. if (ls >= sep)
  15. { ls = 0; break; }
  16. }
  17. rs = sep;
  18. while (++rs < len && url[rs] != ':');
  19. if (rs == len)
  20. { rs = 0; }
  21. creds.username = url;
  22. url[ls ? ls : sep] = '\0';
  23. if (ls)
  24. {
  25. creds.password = url + ls + 1;
  26. url[sep] = '\0';
  27. }
  28. creds.server = url + sep + 1;
  29. if (rs)
  30. {
  31. url[rs] = '\0';
  32. creds.port = atoi(url + rs + 1);
  33. }
  34. return 0;
  35. }
  36. DECL void
  37. rope(void)
  38. {
  39. if (session)
  40. { irc_destroy_session(session); }
  41. api_rope();
  42. }
  43. /* All possible failures / successes wipe the password from memory. */
  44. DECL int
  45. init(void)
  46. {
  47. int ret = 0;
  48. srand(time(NULL));
  49. memset(&callbacks, 0, sizeof(callbacks));
  50. callbacks.event_connect = event_connect;
  51. callbacks.event_channel = event_channel;
  52. if(!api_init())
  53. {
  54. session = irc_create_session(&callbacks);
  55. if (!session)
  56. {
  57. ERRMSG("Error creating IRC session");
  58. ret = 1;
  59. }
  60. else
  61. {
  62. atexit(rope);
  63. if (creds.username == NULL ||
  64. creds.server == NULL)
  65. { ERRMSG("Server and Username required. Stopping now");
  66. exit(1); }
  67. if (irc_connect(session,
  68. creds.server, creds.port, creds.password,
  69. creds.username, creds.username, creds.username))
  70. {
  71. fprintf(stderr, "IRC ERROR: %s\n", irc_strerror(irc_errno(session)));
  72. ret = 1;
  73. }
  74. }
  75. }
  76. else
  77. {
  78. ERRMSG("Error initializing database.");
  79. ret = 1;
  80. }
  81. if (creds.password)
  82. { memset(creds.password, '\0', strlen(creds.password)); }
  83. return ret;
  84. }
  85. DECL int
  86. loop(void)
  87. {
  88. /* We should figure out how the failure happens so we can tell the user that. */
  89. if (irc_run(session) != 0)
  90. { ERR(1, "Error running IRC session\nPossible issue: bad URL,"
  91. " no network connection, bad port, refused connection."); }
  92. return 0;
  93. }
  94. DECL void
  95. help(void)
  96. {
  97. ERRMSG(PROGN ": usage\n"
  98. "-channel CHANNEL - Sets the target channel\n"
  99. "-url URL - Sets the target URL\n"
  100. "-db DBFILE - Sets the database file (default: probotic_data.sqlite)\n"
  101. "-identify PASSWORD - Identifies against NickServ\n"
  102. "-version - Prints version information\n"
  103. "-help - Prints this information\n"
  104. "\nUse format username[:password]@server[:port], port defaults to 6667.\n");
  105. }
  106. DECL void
  107. version(void)
  108. {
  109. ERRMSG(PROGN ": " VERSION_STRING);
  110. }
  111. int
  112. main (int argc,
  113. char ** argv)
  114. {
  115. #ifdef NO_LOGGING
  116. freopen(stdout, "/dev/null");
  117. freopen(stderr, "/dev/null");
  118. #endif
  119. if (argc > 1)
  120. {
  121. char * arg;
  122. while (++argv, --argc)
  123. {
  124. arg = *argv;
  125. if (*arg == '-')
  126. {
  127. ++arg;
  128. if (strcmp(arg, "version") == 0)
  129. {
  130. version();
  131. return 1;
  132. }
  133. else if (strcmp(arg, "help") == 0)
  134. { goto help; }
  135. if (argc < 2)
  136. { goto help; }
  137. if (strcmp(arg, "db") == 0)
  138. { db = argv[1]; }
  139. else if (strcmp(arg, "url") == 0)
  140. { parse_url(argv[1]); }
  141. else if (strcmp(arg, "channel") == 0)
  142. { creds.channel = argv[1]; }
  143. else if (strcmp(arg, "identify") == 0)
  144. { ident_password = argv[1]; }
  145. else
  146. {
  147. ERR(1,"Unknown command provided");
  148. }
  149. ++argv; --argc;
  150. }
  151. else
  152. { goto help; }
  153. }
  154. }
  155. fprintf(stderr, "Connecting to %s:%s@%s:%d\nChannel: %s\n",
  156. creds.username, creds.password,
  157. creds.server, creds.port, creds.channel);
  158. if (init())
  159. { return 1; }
  160. return loop();
  161. help:
  162. help();
  163. return 1;
  164. }