src/main.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 15:38:31 +0100
changeset 2 6a2e88214b80
parent 0 586472add385
child 6 7eb12be31bb5
permissions -rw-r--r--
freenode fix indented committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 *  $Id: main.c 51 2008-01-10 00:19:39Z mbroeker $
 * $URL: http://localhost/svn/c/mcbot/trunk/src/main.c $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <signal.h>
#include <locale.h>
#include <libintl.h>

#include <mcbot.h>
#include <config.h>

#ifndef CONFIG_FILE
#define CONFIG_FILE "/var/lib/nobody/.mcbotrc"
#endif

#ifndef LOCALE_PATH
#define LOCALE_PATH "/var/lib/nobody/data/locale"
#endif

short active = 1;

void sigproc ()
{
    signal (SIGTERM, sigproc);
    active = 0;
}

int main (int argc, char **argv)
{
    UC uc;
    MSG message;
    char buf[513];
    char *msg;
    char *command;
    int len;

    if (bindtextdomain ("mcbot", LOCALE_PATH) != NULL) {
        (void)textdomain ("mcbot");
        (void)setlocale (LC_MESSAGES, "");
    }

    printf ("mcbot-%1.2f\n", VERSION);

    if ((len = config (&uc, CONFIG_FILE)) != 0) {
        switch (len) {
        case -1:
            printf ("You need to create a config file %s\n", CONFIG_FILE);
            break;
        case -2:
            printf
                ("You need at least entries for user, password, server and channel in your config file %s\n",
                 CONFIG_FILE);
            break;
        }
        return len;
    }

    if (uc.nick)
        message.nick = uc.nick;
    else
        return -1;

    if (!(message.stream = irc_connect (uc.server, uc.port)))
        return EXIT_FAILURE;

    if ((len = irc_login (message.stream, uc.nick, uc.pass)) != 0) {
        switch (len) {
        case IRC_GENERAL_ERROR:
            printf ("GENERAL ERROR\n");
            break;
        case IRC_LOGIN_ERROR:
            printf ("LOGIN ERROR\n");
            break;
        default:
            printf ("Unknown Error %d\n", len);
        }
        return len;
    }

    signal (SIGTERM, sigproc);

    if (uc.channel) {
        fprintf (message.stream, "JOIN :%s\r\n", uc.channel);
        if (uc.topic)
            fprintf (message.stream, "TOPIC %s :%s\r\n", uc.channel, uc.topic);
    }

    while (active) {
        if (fgets (buf, 512, message.stream) == NULL) {
            active = 0;
            break;
        }

        if ((command = irc_parsemessage (buf, &message))) {
            printf ("%10s %s %s\n", command, message.channel, message.line);
        } else {
            if ((msg = parse (&message, &active)) != NULL) {
                fprintf (message.stream, "%s\r\n", msg);
                printf ("%10s %s", "WRITE", msg);
            }
        }
        *buf = 0;
    }
    printf ("\n\nClosing Connection\n\n");
    fclose (message.stream);

    /*
     * cleanup
     */

    if (uc.nick)
        free (uc.nick);
    if (uc.pass)
        free (uc.pass);
    if (uc.server)
        free (uc.server);
    if (uc.channel)
        free (uc.channel);
    if (uc.topic)
        free (uc.topic);

    return EXIT_SUCCESS;
}