Skip to content

Instantly share code, notes, and snippets.

@mdukat
Last active June 30, 2019 15:47
Show Gist options
  • Save mdukat/cdb394b1f39681b124b9917a5c003590 to your computer and use it in GitHub Desktop.
Save mdukat/cdb394b1f39681b124b9917a5c003590 to your computer and use it in GitHub Desktop.
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#define PORT 6667
#ifndef DEBUG
#define FREENODE "107.182.226.199" // chat.freenode.org
#else
#define FREENODE "127.0.0.1" // localhost
#endif
void ircSend(int sockfd, char* message){
write(sockfd, message, strlen(message));
}
void ircSend_char(int sockfd, char message_char){
write(sockfd, &message_char, 1);
}
void socketConnect(int* sockfd){
struct sockaddr_in servaddr, cli;
*sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (*sockfd == -1) {
printf("Socket failed\n");
exit(-1);
}else
printf("Socket ok\n");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(FREENODE);
servaddr.sin_port = htons(PORT);
if (connect(*sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0){
printf("Connection failed\n");
exit(-2);
}else
printf("Connected\n");
}
int main()
{
int sockfd;
// Connect to freenode
socketConnect(&sockfd);
// buffers for communication
char* buff = malloc(500);
char* buffa = malloc(1);
int n = 0;
bzero(buff, 500);
bzero(buffa, 1);
// "main loop"
while(1){
while(buffa[0] != '\n'){
read(sockfd, buffa, 1);
buff[n] = buffa[0];
n++;
}
printf("%s", buff);
// Login
if(strstr(buff, ":livingstone.freenode.net NOTICE * :*** Checking Ident") != NULL){
ircSend(sockfd, "NICK d3bot\n");
ircSend(sockfd, "USER d3bot * 8 :fug\n");
}
// Join channel
if(strstr(buff, "NOTICE d3bot :Welcome to freenode.") != NULL){
ircSend(sockfd, "JOIN ##d3suu-talk\n");
}
// Server PING (used to keep-alive) (see RFC 1459) (Dont delete!)
if(strstr(buff, "PING :livingstone.freenode.net") != NULL){
ircSend(sockfd, "PONG :livingstone.freenode.net\n");
}
// Ping-pong
if(strstr(buff, "PRIVMSG ##d3suu-talk :!ping") != NULL){
// When user sends message, it looks like this: ":[user]!*"
// Calculate nick length
unsigned int nick_n = (unsigned int)(strchr(buff, '!') - buff);
// Send first part of PONG
ircSend(sockfd, "PRIVMSG ##d3suu-talk :");
// Send nick
for(int i = 1; i < nick_n; i++){
ircSend_char(sockfd, buff[i]);
}
// Send last part of PONG
ircSend(sockfd, ": pong\n");
}
#ifdef KILL
// Kill
if(strstr(buff, "PRIVMSG ##d3suu-talk :!kill") != NULL){
// Check if d3s send this
if(strstr(buff, ":d3s!d3s") != NULL){
ircSend(sockfd, "QUIT :bbbbb\n");
exit(0);
}
}
#endif
// dupa (ping-pong priv)
if(strstr(buff, ":!dupa") != NULL){
unsigned int nick_n = (unsigned int)(strchr(buff, '!') - buff);
ircSend(sockfd, "PRIVMSG ");
for(int i = 1; i < nick_n; i++){
ircSend_char(sockfd, buff[i]);
}
ircSend(sockfd, " :dupa\n");
}
// clear buffers
bzero(buff, 500);
bzero(buffa, 1);
n = 0;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment