Advertisement
Guest User

Untitled

a guest
Apr 8th, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <netdb.h>
  5. #include <stdarg.h>
  6.  
  7. int conn;
  8. char sbuf[512];
  9.  
  10. void raw(char *fmt, ...) {
  11. va_list ap;
  12. va_start(ap, fmt);
  13. vsnprintf(sbuf, 512, fmt, ap);
  14. va_end(ap);
  15. printf("<< %s", sbuf);
  16. write(conn, sbuf, strlen(sbuf));
  17. }
  18.  
  19. int main() {
  20.  
  21. char *nick = "Random";
  22. char *channel = "#philosophical";
  23. char *host = "efnet.deic.eu";
  24. char *port = "6667";
  25.  
  26. char *user, *command, *where, *message, *sep, *target;
  27. int i, j, l, sl, o = -1, start, wordcount;
  28. char buf[513];
  29. struct addrinfo hints, *res;
  30.  
  31. memset(&hints, 0, sizeof hints);
  32. hints.ai_family = AF_INET;
  33. hints.ai_socktype = SOCK_STREAM;
  34. getaddrinfo(host, port, &hints, &res);
  35. conn = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  36. connect(conn, res->ai_addr, res->ai_addrlen);
  37.  
  38. raw("USER %s 0 0 :%s\r\n", nick, nick);
  39. raw("NICK %s\r\n", nick);
  40.  
  41. while ((sl = read(conn, sbuf, 512))) {
  42. for (i = 0; i < sl; i++) {
  43. o++;
  44. buf[o] = sbuf[i];
  45. if ((i > 0 && sbuf[i] == '\n' && sbuf[i - 1] == '\r') || o == 512) {
  46. buf[o + 1] = '\0';
  47. l = o;
  48. o = -1;
  49.  
  50. printf(">> %s", buf);
  51.  
  52. if (!strncmp(buf, "PING", 4)) {
  53. buf[1] = 'O';
  54. raw(buf);
  55. } else if (buf[0] == ':') {
  56. wordcount = 0;
  57. user = command = where = message = NULL;
  58. for (j = 1; j < l; j++) {
  59. if (buf[j] == ' ') {
  60. buf[j] = '\0';
  61. wordcount++;
  62. switch(wordcount) {
  63. case 1: user = buf + 1; break;
  64. case 2: command = buf + start; break;
  65. case 3: where = buf + start; break;
  66. }
  67. if (j == l - 1) continue;
  68. start = j + 1;
  69. } else if (buf[j] == ':' && wordcount == 3) {
  70. if (j < l - 1) message = buf + j + 1;
  71. break;
  72. }
  73. }
  74.  
  75. if (wordcount < 2) continue;
  76.  
  77. if (!strncmp(command, "001", 3) && channel != NULL) {
  78. raw("JOIN %s\r\n", channel);
  79. } else if (!strncmp(command, "PRIVMSG", 7) || !strncmp(command, "NOTICE", 6)) {
  80. if (where == NULL || message == NULL) continue;
  81. if ((sep = strchr(user, '!')) != NULL) user[sep - user] = '\0';
  82. if (where[0] == '#' || where[0] == '&' || where[0] == '+' || where[0] == '!') target = where; else target = user;
  83. printf("[from: %s] [reply-with: %s] [where: %s] [reply-to: %s] %s", user, command, where, target, message);
  84. //raw("%s %s :%s", command, target, message); // If you enable this the IRCd will get its "*** Looking up your hostname..." messages thrown back at it but it works...
  85. }
  86. }
  87.  
  88. }
  89. }
  90.  
  91. }
  92.  
  93. return 0;
  94.  
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement