src/main.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 15:40:11 +0100
changeset 9 aff6726b8b87
parent 8 3152de03758e
child 10 311ea5fa60dd
permissions -rw-r--r--
Private Messages will be send to the proper location 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

void sigproc ()
{
    signal (SIGTERM, sigproc);
}

int main (int argc, char **argv)
{
    UC uc;
    MSG message = { NULL, 0, 0, 0, 0, 0, 0 };
    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.server, 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);
        message.current_channel = uc.channel;
        if (uc.topic)
            fprintf (message.stream, "TOPIC %s :%s\r\n", uc.channel, uc.topic);
    }

    while (!feof (message.stream)) {
        message.line = NULL;
        *buf = 0;
        fgets (buf, 512, message.stream);

        if ((command = irc_parsemessage (buf, &message))) {
            printf ("%10s %s %s\n", command, message.channel, message.line);
            if (!strcmp (command, "ERROR"))
                break;
        } else {
            if ((msg = parse (&message)) != NULL) {
                fprintf (message.stream, "%s\r\n", msg);
                printf ("%10s %s", "WRITE", msg);
            }
        }
    }
    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;
}