Public feed #887897

Something

public text v1 · immutable
Raw Edit
·published 2008-02-01 18:18 UTC
rendered paste body
/***************************************************************************
 *   Copyright (C) 2008 by Daniel Moss   *
 *   daniel@mrmoss.net   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h> /*used for gethostbyname()*/
#include <string.h>
#include <fcntl.h>
#include "connection.h"

/* 
 * basically, send to the socket.... --Acidic32 
 */

int mossy_send(char *buf, int fd)
{
 if ((write(fd, buf, strlen(buf))) == 0) return -1;
 bzero(buf, strlen(buf));
 return 0;
}

/*
 * this is basically a host resolver, resolv the hostname! --Acidic32 
 */

int mossy_host_resolver(char *from, char *to)
{
 struct hostent *host = NULL;

 if ((host = gethostbyname(from)) == 0) return -1;
 sprintf(to, "%d.%d.%d.%d", host->h_addr[0] & 0xff,
                            host->h_addr[1] & 0xff,
                            host->h_addr[2] & 0xff,
                            host->h_addr[3] & 0xff);
 if ((strcmp(to, from)) == 0) {
  bzero(to, strlen(to));
  strncpy(to, host->h_name, strlen(host->h_name));
  return 0;
  }

 return 0;
}

/*
 * Nice simple routine to connect to the irc server, peaceful! --Acidic32
 */

int mossy_connect_server(Connection *c)
{
 char buf[1024];
 struct sockaddr_in ircserv;
 int current_serv = 0;

start_connect:

 c->port    = 6667;
 strncpy(c->dnsname, "irc.equiphase.net", sizeof("irc.equiphase.net"));
 c->irc_status=0;

 mossy_host_resolver(c->dnsname, (char *)&c->ip);
 ircserv.sin_addr.s_addr = inet_addr(c->ip);
 ircserv.sin_port        = htons(c->port);
 ircserv.sin_family      = AF_INET;

 if ((c->sock = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
  printf("Fatal error creating socket:\n%s\n", strerror(c->sock));
  exit(-1);
  }

 if ((connect(c->sock, (struct sockaddr *)&ircserv, sizeof(ircserv))) == -1) {
  printf("Error connecting socket:\n\t%s\n", strerror(errno));
  if (current_serv == 14) {
   printf("Can't connect to any server! Exiting...\n");
   exit(-1);
   }
  current_serv++;
  goto start_connect;
  }

 // printf("connected!\n");
 mossy_nick_register(c);
 return 0;
}

/* 
 * register to the server, nice! --Acidic32
 */

int mossy_nick_register(Connection *c)
{
 char buf[255], str[255], buf2[255];
 int ret=0;
 int x=1;

 while (ret < 1) {
  bzero(buf2, sizeof(buf2));
  read(c->sock, buf2, sizeof(buf2));
  printf("read> %s \n", buf2);
  ret += mossy_ln_finder(buf2);
  }

 bzero(buf, sizeof(buf));
 sprintf(buf, "NICK %s\r\n", "MossyBot");
 printf("Sending nick: %s\n", buf);
 mossy_send(buf, c->sock);
 bzero(buf2, sizeof(buf2));
 read(c->sock, buf2, sizeof(buf2));
 printf("read: %s\n", buf2);

 /* Here we return the ping and finish
  * our registration with the server!
  */

 if ((strncmp(buf2, "PING", 4)) == 0) {
  printf("pinged!\n");
  bzero(str, sizeof(str));
  strncpy(str, &buf[6], strlen(buf)-6);
  bzero(buf, sizeof(buf));
  sprintf(buf, "PONG :%s\r\n", str);
  printf("sending: %s", buf);
  mossy_send(buf, c->sock);

  bzero(buf, sizeof(buf));
  sprintf(buf, "USER mossy mossy mossy :%s\r\n", "Mossy Bot");
  printf("Sending: %s\n", buf);
  mossy_send(buf, c->sock);
  }

}

/* 
 * read from the server... i think this is important! --Acidic32 
 */

int mossy_irc_reader(int fd, Connection *c)
{
 char buf[1024*4];

 while(read(fd, buf, sizeof(buf)) != 0) {
   mossy_parser(buf, c);
   printf("%s", buf);
   bzero(&buf, sizeof(buf));
  }

 /* If we get this far.. the server dropped us. */
 main();

 return 0;
}