Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <fcntl.h>
- #include <ctype.h>
- #include <time.h>
- #define BNET_PORT 6112
- int bnet_socket;
- struct hostent *resolved_hostname;
- char login_packet[512];
- char* idle_message = "/users";
- int idle_interval = 10;
- void bot_error(char *msg) {
- perror(msg);
- printf("\n");
- exit(0);
- }
- void bot_connect() {
- struct sockaddr_in serv_addr;
- printf("Connecting to server...");
- bnet_socket = socket(AF_INET, SOCK_STREAM, 0);
- if (bnet_socket < 0) {
- bot_error("[FAIL]\nUnable to open socket");
- }
- memset((char *) &serv_addr, 0, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- memcpy((char *)&serv_addr.sin_addr.s_addr, (char *)resolved_hostname->h_addr, resolved_hostname->h_length);
- serv_addr.sin_port = htons(BNET_PORT);
- if (connect(bnet_socket, (const struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
- bot_error("[FAIL]\n\tUnable to connect to server");
- printf("[DONE]\nLogging in and joining channel...");
- if (send(bnet_socket, login_packet, strlen(login_packet), 0) < 1)
- bot_error("[FAIL]\n\tUnable to send login data");
- printf("[DONE]\n");
- }
- void bot_idle() {
- char buffer[2048];
- char last_line[1024];
- int buf_size = 0, line_len;
- char* line_end;
- int n;
- struct timeval timeout;
- fd_set read_select, error_select;
- time_t current_time, last_idle = time(0);
- while (1) {
- FD_ZERO(&read_select);
- FD_SET(bnet_socket, &read_select);
- FD_ZERO(&error_select);
- FD_SET(bnet_socket, &error_select);
- timeout.tv_sec = 1;
- timeout.tv_usec = 0;
- if (select(bnet_socket + 1, &read_select, NULL, &error_select, &timeout) > 0) {
- if (FD_ISSET(bnet_socket, &error_select)) {
- return;
- }
- if (FD_ISSET(bnet_socket, &read_select)) {
- n = recv(bnet_socket, buffer + buf_size, 2048 - buf_size, 0);
- if (n < 1) {
- return;
- }
- buf_size += n;
- if ((line_end = memchr(buffer, '\n', buf_size)) != NULL) {
- line_len = (unsigned int)(line_end - buffer);
- strncpy(last_line, buffer, line_end - buffer);
- last_line[line_len] = 0;
- memmove(buffer, line_end + 1, buf_size - line_len);
- buf_size -= line_len + 1;
- puts(last_line);
- }
- }
- }
- current_time = time(0);
- if (last_idle + idle_interval < current_time) {
- puts("Sending idle message...");
- if (send(bnet_socket, idle_message, strlen(idle_message), 0) < 1)
- return;
- last_idle = current_time;
- }
- }
- }
- void bot_cleanup() {
- printf("Disconnected\n");
- exit(0);
- }
- int main(int argc, char** argv) {
- char* username = "c0ol";
- char* password = "";
- char* channel = "test";
- char* server_address = "206.212.240.218";
- sprintf(login_packet, "\x03\n%s\n%s\n/join %s\n", username, password, channel);
- resolved_hostname = gethostbyname(server_address);
- if (resolved_hostname == NULL) {
- printf("Unable to resolve host %s\n", server_address);
- exit(0);
- }
- while(1) {
- bot_connect();
- bot_idle();
- bot_cleanup();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment