Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Created October 31, 2012 15:55
Show Gist options
  • Select an option

  • Save neuro-sys/3987860 to your computer and use it in GitHub Desktop.

Select an option

Save neuro-sys/3987860 to your computer and use it in GitHub Desktop.
irc.c
#include <Ws2tcpip.h>
#include <stdio.h>
#pragma comment (lib, "Ws2_32.lib")
#define PORT "6667"
#define HOST "irc.freenode.net"
DWORD WINAPI ThreadHTTPTitleCheck(LPVOID lpParam)
{
int sock;
char buffer[1024];
int bytes_received;
struct addrinfo hints;
struct addrinfo *servinfo;
void **params;
char *url;
char *t;
params = (void **) lpParam;
url = (char *) params[1];
sock = (int) lpParam;
t = strtok(url, "/");
t = strtok(NULL, "/");
send(sock, t, strlen(t), 0);
/* Connect to the URL, parse the <title>, send the result if exists */
return 0;
}
DWORD WINAPI ThreadRead(LPVOID lpParam)
{
int sock;
char buffer[1024];
int bytes_received;
sock = (int) lpParam;
strcpy(buffer, "NICK sabribeyler\r\n"
"USER sabribeyler 8 * :foo\r\n"
"JOIN #gameover\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 :selam\r\n", chan, msg);
send(sock, sp, strlen(sp), 0);
}
if (strstr(msg, "http") != NULL) {
void *params[2];
params[0] = (void *) &sock;
params[1] = (void *) msg;
CreateThread(NULL, 0, ThreadHTTPTitleCheck, (LPVOID) params, 0, NULL);
}
}
}
return 0;
}
void initWinSock()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}
}
int __cdecl main(int argc, char *argv[])
{
int sock;
struct addrinfo hints;
struct addrinfo *servinfo;
DWORD threadId;
initWinSock();
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");
WSACleanup();
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");
WSACleanup();
exit(1);
}
printf("OK.\n");
printf("connect...\t");
if (connect(sock, servinfo->ai_addr, servinfo->ai_addrlen) < 0) {
fprintf(stderr, "connect failed.\n");
WSACleanup();
exit(1);
}
printf("OK.\n");
CreateThread(NULL, 0, ThreadRead, (LPVOID) sock, 0, &threadId);
while(1)
{
char buffer[1024];
gets(buffer);
if (strstr(buffer, "QUIT") != NULL)
break;
strcat(buffer, "\r\n");
send(sock, buffer, strlen(buffer), 0);
}
WSACleanup();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment