src/main.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Wed, 03 Mar 2010 18:36:54 +0100
changeset 52 c9401e6a62cb
parent 49 59b09b0aeb96
child 54 e21f837e2b13
permissions -rw-r--r--
Compatibility to other distributions... 1) find needs a path parameter and 2) the targets have to be linked against gettext 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 <signal.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <locale.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
#include <libintl.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
#include <mcbot.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
#include <config.h>
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 31
diff changeset
    17
#include "common.h"
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
49
59b09b0aeb96 safe_strncpy replaces the insecure strncpy function
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    19
#include <compat.h>
59b09b0aeb96 safe_strncpy replaces the insecure strncpy function
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    20
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
#ifndef CONFIG_FILE
31
bbcb8a3366b4 mcbot needs a proper system account
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 29
diff changeset
    22
#define CONFIG_FILE "/var/lib/mcbot/.mcbotrc"
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
#endif
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
#ifndef LOCALE_PATH
31
bbcb8a3366b4 mcbot needs a proper system account
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 29
diff changeset
    26
#define LOCALE_PATH "/var/lib/mcbot/data/locale"
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
#endif
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
16
33245bf7873a The active = 0|1 thingy is essential for a networking application -> re-activated
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 15
diff changeset
    29
short active = 1;               /* needed for a safe shutdown */
33245bf7873a The active = 0|1 thingy is essential for a networking application -> re-activated
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 15
diff changeset
    30
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
void sigproc ()
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
{
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    signal (SIGTERM, sigproc);
16
33245bf7873a The active = 0|1 thingy is essential for a networking application -> re-activated
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 15
diff changeset
    34
    active = 0;
0
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
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
int main (int argc, char **argv)
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
    UC uc;
10
311ea5fa60dd Code cleanups and debian changelogs
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    40
    MSG message;
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 31
diff changeset
    41
    char buf[DEFAULT_BUF_SIZE];
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
    char *msg;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
    char *command;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    int len;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    if (bindtextdomain ("mcbot", LOCALE_PATH) != NULL) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
        (void)textdomain ("mcbot");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
        (void)setlocale (LC_MESSAGES, "");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
23
1cd79bb84e9c ChangeLog for mcbot-0.96
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 18
diff changeset
    51
    printf ("mcbot-%1.2f Build %s\n", BOT_VERSION, BOT_BUILD);
0
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
    if ((len = config (&uc, CONFIG_FILE)) != 0) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
        switch (len) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        case -1:
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
            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
    57
            break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
        case -2:
18
4435146391ae LOGIN without Password added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 17
diff changeset
    59
            printf ("You need at least entries for user, server and channel in your config file %s\n", CONFIG_FILE);
0
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
        }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
        return len;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    if (uc.nick)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
        message.nick = uc.nick;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
    else
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
        return -1;
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 (!(message.stream = irc_connect (uc.server, uc.port)))
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
        return EXIT_FAILURE;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
6
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
    73
    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
    74
        switch (len) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
        case IRC_GENERAL_ERROR:
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
            printf ("GENERAL ERROR\n");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
            break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
        case IRC_LOGIN_ERROR:
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
            printf ("LOGIN ERROR\n");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
            break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
        default:
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
            printf ("Unknown Error %d\n", len);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
        }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    84
        return len;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    85
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    86
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    87
    signal (SIGTERM, sigproc);
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
    if (uc.channel) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
        fprintf (message.stream, "JOIN :%s\r\n", uc.channel);
49
59b09b0aeb96 safe_strncpy replaces the insecure strncpy function
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    91
        safe_strncpy (message.current_channel, uc.channel, sizeof (message.current_channel));
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    92
        if (uc.topic)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    93
            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
    94
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    95
6
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
    96
    while (!feof (message.stream)) {
47
a689b6a8e6ed fgets may return a NULL pointer...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    97
        if (fgets (buf, sizeof (buf), message.stream) == NULL)
a689b6a8e6ed fgets may return a NULL pointer...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    98
            break;
16
33245bf7873a The active = 0|1 thingy is essential for a networking application -> re-activated
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 15
diff changeset
    99
17
9a32b5242320 BUG INTRODUCED from a6c36a2b97f6bed5577d66474949c8ec8b32c8e8
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 16
diff changeset
   100
        if (!active)            /* the bot was killed with SIGTERM */
16
33245bf7873a The active = 0|1 thingy is essential for a networking application -> re-activated
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 15
diff changeset
   101
            break;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   102
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   103
        if ((command = irc_parsemessage (buf, &message))) {
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 31
diff changeset
   104
            if (!strcmp (command, "ERROR") || !strcmp (command, "ENOMEM"))
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 31
diff changeset
   105
                break;
0
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 {
8
3152de03758e A critical change was introduced here...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 6
diff changeset
   108
            if ((msg = parse (&message)) != NULL) {
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   109
                fprintf (message.stream, "%s\r\n", msg);
31
bbcb8a3366b4 mcbot needs a proper system account
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 29
diff changeset
   110
                fflush (message.stream);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   111
                printf ("%10s %s", "WRITE", msg);
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
        }
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 31
diff changeset
   114
        fflush (stdout);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   115
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   116
    printf ("\n\nClosing Connection\n\n");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   117
    fclose (message.stream);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   118
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
     * cleanup
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   121
     */
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   122
    if (uc.nick)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   123
        free (uc.nick);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   124
    if (uc.pass)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   125
        free (uc.pass);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   126
    if (uc.server)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   127
        free (uc.server);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   128
    if (uc.channel)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   129
        free (uc.channel);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   130
    if (uc.topic)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   131
        free (uc.topic);
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   132
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   133
    return EXIT_SUCCESS;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   134
}