Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdarg.h>
- #define IRC_PORT 6667
- #define BUFFER_SIZE 2048
- int connect_to_irc(int* sockfd, char* irc_channel, int port);
- int get_on_channel(int* sockfd, char* channel, char* nick, char* buf);
- int write_to_socket(int* sockfd, char* buf, char* fmt, ...);
- int main(int argc, char** argv) {
- int sockfd;
- connect_to_irc(&sockfd, "irc.freenode.net", 6667);
- char buf[BUFFER_SIZE+1]; char out[BUFFER_SIZE+1];
- int n;
- get_on_channel(&sockfd, "#nullbyte", "PineTree", out);
- while( (n = read(sockfd, buf, BUFFER_SIZE)) ) {
- if (n <= 0) continue;
- buf[n] = 0;
- if (strncmp(buf, "PING", 4) {
- printf("We got PING'd\n");
- buf[1] = 'O';
- send(sockfd, out, strlen(out), 0);
- }
- if (strstr(buf, "@slap") != NULL) {
- out[0] = 0;
- char* pos = strstr(buf, "@slap ") + 6;
- sprintf(out, "\rPRIVMSG #nullbyte :ACTION slapped the hell outta %s\r\n", pos);
- send(sockfd, out, strlen(out), 0);
- }
- if (strstr(buf, "@google ") != NULL) {
- char* google_url = "http://google.com/#q=";
- char* pos = strstr(buf, "@google ") + 8;
- char* result = malloc(strlen(google_url)+strlen(pos));
- if (result == NULL) continue;
- strcpy(result, google_url);
- int x = strlen(google_url);
- for (int i = 0, n = strlen(pos); i < n; i++) {
- if ( isalnum(pos[i]) )
- result[x] = pos[i];
- else if ( isspace(pos[i]) )
- result[x] = '+';
- x++;
- }
- out[0] = 0;
- sprintf(out, "\rPRIVMSG #nullbyte :%s\r\n", result);
- send(sockfd, out, strlen(out),0);
- memset(result, 0, strlen(result));
- free(result);
- }
- printf("%s\n", buf);
- }
- printf("click enter to exit");
- scanf("%c",buf);
- close(sockfd);
- return 0;
- }
- int connect_to_irc(int* sockfd, char* irc_channel, int port) {
- struct sockaddr_in serv_addr;
- struct hostent *server;
- *sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (*sockfd < 0) {
- perror("socket could not be opened");
- exit(1);
- }
- server = gethostbyname("irc.freenode.net");
- if (server == NULL) {
- perror("host not found");
- close(*sockfd);
- exit(2);
- }
- memset(&serv_addr, 0, sizeof(serv_addr));
- bcopy(
- (char*)server->h_addr,
- (char*)&serv_addr.sin_addr.s_addr,
- server->h_length
- );
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(IRC_PORT);
- if ( connect(*sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0 ) {
- perror("error connecting...");
- close(*sockfd);
- exit(3);
- }
- return *sockfd;
- }
- int get_on_channel(int* sockfd, char* channel, char* nick, char* buf) {
- write_to_socket(sockfd, buf, "\rUSER %s 0 * :%s\r\n", nick, nick);
- write_to_socket(sockfd, buf, "\rNICK %s\r\n", nick);
- write_to_socket(sockfd, buf, "\rJOIN %s\r\n", channel);
- return 0;
- }
- int write_to_socket(int* sockfd, char* buf, char* fmt, ...) {
- memset(buf, 0, BUFFER_SIZE);
- va_list ap;
- va_start(ap,fmt);
- vsnprintf(buf, BUFFER_SIZE, fmt, ap);
- va_end(ap);
- write(*sockfd, buf, strlen(buf));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement