src/main.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 15:38:48 +0100
changeset 7 b04ef0b1cd3b
parent 6 7eb12be31bb5
child 8 3152de03758e
permissions -rw-r--r--
Debian Changelog Fix committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 *  $Id: main.c 51 2008-01-10 00:19:39Z mbroeker $
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * $URL: http://localhost/svn/c/mcbot/trunk/src/main.c $
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 *
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 */
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdio.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdlib.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <string.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <sys/types.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <sys/socket.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
#include <netinet/in.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
#include <arpa/inet.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
#include <netdb.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
#include <signal.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
#include <locale.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
#include <libintl.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
#include <mcbot.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
#include <config.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
#ifndef CONFIG_FILE
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
#define CONFIG_FILE "/var/lib/nobody/.mcbotrc"
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
#endif
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
#ifndef LOCALE_PATH
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
#define LOCALE_PATH "/var/lib/nobody/data/locale"
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
#endif
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
short active = 1;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
void sigproc ()
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
{
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
    signal (SIGTERM, sigproc);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    active = 0;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
}
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
int main (int argc, char **argv)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
{
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
    UC uc;
6
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
    43
    MSG message = { NULL, 0, 0, 0, 0, 0, 0 };
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    char buf[513];
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
    char *msg;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    char *command;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
    int len;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    if (bindtextdomain ("mcbot", LOCALE_PATH) != NULL) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
        (void)textdomain ("mcbot");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
        (void)setlocale (LC_MESSAGES, "");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    printf ("mcbot-%1.2f\n", VERSION);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
    if ((len = config (&uc, CONFIG_FILE)) != 0) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
        switch (len) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
        case -1:
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
            printf ("You need to create a config file %s\n", CONFIG_FILE);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
            break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
        case -2:
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
            printf
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
                ("You need at least entries for user, password, server and channel in your config file %s\n",
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
                 CONFIG_FILE);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
            break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
        }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
        return len;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
    if (uc.nick)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
        message.nick = uc.nick;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
    else
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
        return -1;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
    if (!(message.stream = irc_connect (uc.server, uc.port)))
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
        return EXIT_FAILURE;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
6
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
    78
    if ((len = irc_login (message.stream, uc.server, uc.nick, uc.pass)) != 0) {
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
        switch (len) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
        case IRC_GENERAL_ERROR:
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
            printf ("GENERAL ERROR\n");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
            break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
        case IRC_LOGIN_ERROR:
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    84
            printf ("LOGIN ERROR\n");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    85
            break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    86
        default:
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    87
            printf ("Unknown Error %d\n", len);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    88
        }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    89
        return len;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    91
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    92
    signal (SIGTERM, sigproc);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    93
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    94
    if (uc.channel) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    95
        fprintf (message.stream, "JOIN :%s\r\n", uc.channel);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    96
        if (uc.topic)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    97
            fprintf (message.stream, "TOPIC %s :%s\r\n", uc.channel, uc.topic);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    98
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    99
6
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
   100
    while (!feof (message.stream)) {
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
   101
        message.line = NULL;
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
   102
        *buf = 0;
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
   103
        fgets (buf, 512, message.stream);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   104
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   105
        if ((command = irc_parsemessage (buf, &message))) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   106
            printf ("%10s %s %s\n", command, message.channel, message.line);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   107
        } else {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   108
            if ((msg = parse (&message, &active)) != NULL) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   109
                fprintf (message.stream, "%s\r\n", msg);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   110
                printf ("%10s %s", "WRITE", msg);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   111
            }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   112
        }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   113
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   114
    printf ("\n\nClosing Connection\n\n");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   115
    fclose (message.stream);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   116
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   117
    /*
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   118
     * cleanup
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   119
     */
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   120
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   121
    if (uc.nick)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   122
        free (uc.nick);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   123
    if (uc.pass)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   124
        free (uc.pass);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   125
    if (uc.server)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   126
        free (uc.server);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   127
    if (uc.channel)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   128
        free (uc.channel);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   129
    if (uc.topic)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   130
        free (uc.topic);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   131
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   132
    return EXIT_SUCCESS;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   133
}