Created
November 1, 2012 22:48
-
-
Save neuro-sys/3997238 to your computer and use it in GitHub Desktop.
rezil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <sys/types.h> | |
| #include <sys/socket.h> | |
| #include <netdb.h> | |
| #include <stdio.h> | |
| #include <pthread.h> | |
| #include <string.h> | |
| #include <stdlib.h> | |
| #define PORT "6667" | |
| #define HOST "irc.freenode.net" | |
| int sock; | |
| void *ThreadHTTPTitleCheck(void *arg) | |
| { | |
| char buffer[8048]; | |
| struct addrinfo hints; | |
| struct addrinfo *servinfo; | |
| char *url; | |
| char domain[255]; | |
| char path[255]; | |
| char *t; | |
| char *title; | |
| int sock2; | |
| url = (char *) arg; | |
| t = strtok(url, "/"); | |
| t = strtok(NULL, "/"); | |
| if (t == NULL) | |
| return; | |
| strcpy(domain, t); | |
| if (t == NULL) | |
| return; | |
| t = strtok(NULL, "\r\n "); | |
| if (t == NULL) | |
| return; | |
| strcpy(path, t); | |
| printf("%s\n", path); | |
| /* Connect to the URL, parse the <title>, send the result if exists */ | |
| memset(&hints, 0, sizeof(hints)); | |
| hints.ai_family = AF_UNSPEC; | |
| hints.ai_socktype = SOCK_STREAM; | |
| getaddrinfo(domain, "80", &hints, &servinfo); | |
| sock2 = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol); | |
| connect(sock2, servinfo->ai_addr, servinfo->ai_addrlen); | |
| sprintf(buffer, "GET /%s HTTP/1.0\r\n\r\n", path); | |
| printf("%s\n", buffer); | |
| send(sock2, buffer, strlen(buffer), 0); | |
| recv(sock2, buffer, 1024, 0); | |
| t = strtok(buffer, "<>"); | |
| while (t != NULL) { | |
| t = strtok(NULL, "<>"); | |
| if (t == NULL) | |
| break; | |
| if (strstr(t, "title") != NULL) { | |
| t = strtok(NULL, "<>"); | |
| title = t; | |
| break; | |
| } | |
| } | |
| if (title == NULL) | |
| return; | |
| close(sock2); | |
| sprintf(buffer, "PRIVMSG #archlinux-tr :Title: [%s]\r\n", title); | |
| send(sock, buffer, strlen(buffer), 0); | |
| return NULL; | |
| } | |
| void *ThreadRead(void *arg) | |
| { | |
| char buffer[1024]; | |
| int bytes_received; | |
| pthread_t thread; | |
| strcpy(buffer, "NICK sabribeyler\r\n" | |
| "USER sabribeyler 8 * :foo\r\n" | |
| "JOIN #archlinux-tr\r\n"); | |
| send(sock, buffer, strlen(buffer), 0); | |
| while(1) | |
| { | |
| char *s; | |
| char sp[256]; | |
| bytes_received = recv(sock, buffer, 1024, 0); | |
| buffer[bytes_received] = '\0'; | |
| printf(buffer); | |
| /* Handle PING */ | |
| if (!strncmp(buffer, "PING", 4)) { | |
| s = strchr(buffer, ':'); | |
| sprintf(sp, "PONG %s\r\n", s); | |
| send(sock, sp, strlen(sp), 0); | |
| } else if (( s = strstr(buffer, "PRIVMSG")) != NULL) { /* We have a PRIVMSG */ | |
| char chan[50]; | |
| char *msg; | |
| char *t_ptr; | |
| t_ptr = strtok(s, " "); | |
| t_ptr = strtok(NULL, " "); | |
| strcpy(chan, t_ptr); | |
| msg = strtok(NULL, "\n"); | |
| if (msg != NULL) msg += 1; | |
| if (strstr(msg, "selam") != NULL) { | |
| sprintf(sp, "PRIVMSG %s :%s\r\n", chan, msg); | |
| send(sock, sp, strlen(sp), 0); | |
| } | |
| if (strstr(msg, "http") != NULL) { | |
| pthread_create(&thread, NULL, ThreadHTTPTitleCheck, (void *)msg); | |
| } | |
| } | |
| } | |
| return NULL; | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| struct addrinfo hints; | |
| struct addrinfo *servinfo; | |
| pthread_t thread; | |
| memset(&hints, 0, sizeof(hints)); | |
| hints.ai_family = AF_UNSPEC; | |
| hints.ai_socktype = SOCK_STREAM; | |
| printf("getaddrinfo...\t"); | |
| if (getaddrinfo(HOST, PORT, &hints, &servinfo) != 0) { | |
| fprintf(stderr, "getaddrinfo failed.\n"); | |
| exit(1); | |
| } | |
| printf("OK.\n"); | |
| printf("socket...\t"); | |
| if ((sock = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) < 0) { | |
| fprintf(stderr, "Socket failed.\n"); | |
| exit(1); | |
| } | |
| printf("OK.\n"); | |
| printf("connect...\t"); | |
| if (connect(sock, servinfo->ai_addr, servinfo->ai_addrlen) < 0) { | |
| fprintf(stderr, "connect failed.\n"); | |
| exit(1); | |
| } | |
| printf("OK.\n"); | |
| pthread_create(&thread, NULL, ThreadRead, NULL); | |
| while(1) | |
| { | |
| char buffer[1024]; | |
| gets(buffer); | |
| strcat(buffer, "\r\n"); | |
| send(sock, buffer, strlen(buffer), 0); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment