Guest User

Untitled

a guest
Jun 3rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <netdb.h>
  8. #include <fcntl.h>
  9. #include <ctype.h>
  10. #include <time.h>
  11.  
  12. #define BNET_PORT 6112
  13.  
  14. int bnet_socket;
  15. struct hostent *resolved_hostname;
  16. char login_packet[512];
  17. char* idle_message = "/users";
  18. int idle_interval = 10;
  19.  
  20. void bot_error(char *msg) {
  21. perror(msg);
  22. printf("\n");
  23.  
  24. exit(0);
  25. }
  26.  
  27. void bot_connect() {
  28. struct sockaddr_in serv_addr;
  29.  
  30. printf("Connecting to server...");
  31.  
  32. bnet_socket = socket(AF_INET, SOCK_STREAM, 0);
  33.  
  34. if (bnet_socket < 0) {
  35. bot_error("[FAIL]\nUnable to open socket");
  36. }
  37.  
  38. memset((char *) &serv_addr, 0, sizeof(serv_addr));
  39.  
  40. serv_addr.sin_family = AF_INET;
  41. memcpy((char *)&serv_addr.sin_addr.s_addr, (char *)resolved_hostname->h_addr, resolved_hostname->h_length);
  42. serv_addr.sin_port = htons(BNET_PORT);
  43.  
  44. if (connect(bnet_socket, (const struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
  45. bot_error("[FAIL]\n\tUnable to connect to server");
  46.  
  47. printf("[DONE]\nLogging in and joining channel...");
  48.  
  49. if (send(bnet_socket, login_packet, strlen(login_packet), 0) < 1)
  50. bot_error("[FAIL]\n\tUnable to send login data");
  51.  
  52. printf("[DONE]\n");
  53. }
  54.  
  55. void bot_idle() {
  56. char buffer[2048];
  57. char last_line[1024];
  58. int buf_size = 0, line_len;
  59. char* line_end;
  60. int n;
  61.  
  62. struct timeval timeout;
  63. fd_set read_select, error_select;
  64.  
  65. time_t current_time, last_idle = time(0);
  66.  
  67. while (1) {
  68. FD_ZERO(&read_select);
  69. FD_SET(bnet_socket, &read_select);
  70.  
  71. FD_ZERO(&error_select);
  72. FD_SET(bnet_socket, &error_select);
  73.  
  74. timeout.tv_sec = 1;
  75. timeout.tv_usec = 0;
  76.  
  77. if (select(bnet_socket + 1, &read_select, NULL, &error_select, &timeout) > 0) {
  78. if (FD_ISSET(bnet_socket, &error_select)) {
  79. return;
  80. }
  81.  
  82. if (FD_ISSET(bnet_socket, &read_select)) {
  83. n = recv(bnet_socket, buffer + buf_size, 2048 - buf_size, 0);
  84. if (n < 1) {
  85. return;
  86. }
  87.  
  88. buf_size += n;
  89.  
  90. if ((line_end = memchr(buffer, '\n', buf_size)) != NULL) {
  91. line_len = (unsigned int)(line_end - buffer);
  92.  
  93. strncpy(last_line, buffer, line_end - buffer);
  94. last_line[line_len] = 0;
  95.  
  96. memmove(buffer, line_end + 1, buf_size - line_len);
  97. buf_size -= line_len + 1;
  98.  
  99. puts(last_line);
  100. }
  101. }
  102. }
  103.  
  104. current_time = time(0);
  105.  
  106. if (last_idle + idle_interval < current_time) {
  107. puts("Sending idle message...");
  108.  
  109. if (send(bnet_socket, idle_message, strlen(idle_message), 0) < 1)
  110. return;
  111.  
  112. last_idle = current_time;
  113. }
  114. }
  115. }
  116.  
  117. void bot_cleanup() {
  118. printf("Disconnected\n");
  119. exit(0);
  120. }
  121.  
  122. int main(int argc, char** argv) {
  123. char* username = "c0ol";
  124. char* password = "";
  125. char* channel = "test";
  126. char* server_address = "206.212.240.218";
  127.  
  128. sprintf(login_packet, "\x03\n%s\n%s\n/join %s\n", username, password, channel);
  129.  
  130. resolved_hostname = gethostbyname(server_address);
  131. if (resolved_hostname == NULL) {
  132. printf("Unable to resolve host %s\n", server_address);
  133. exit(0);
  134. }
  135.  
  136. while(1) {
  137. bot_connect();
  138. bot_idle();
  139. bot_cleanup();
  140. }
  141.  
  142. return 0;
  143. }
Add Comment
Please, Sign In to add comment