author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 27 Feb 2010 18:58:24 +0100 | |
changeset 50 | 412ac658eb97 |
parent 49 | 59b09b0aeb96 |
child 54 | e21f837e2b13 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* $Id: main.c 51 2008-01-10 00:19:39Z mbroeker $ |
|
3 |
* $URL: http://localhost/svn/c/mcbot/trunk/src/main.c $ |
|
4 |
* |
|
5 |
*/ |
|
6 |
||
7 |
#include <stdio.h> |
|
8 |
#include <stdlib.h> |
|
9 |
#include <string.h> |
|
10 |
||
11 |
#include <signal.h> |
|
12 |
#include <locale.h> |
|
13 |
#include <libintl.h> |
|
14 |
||
15 |
#include <mcbot.h> |
|
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 | 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 | 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 | 23 |
#endif |
24 |
||
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 | 27 |
#endif |
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 | 31 |
void sigproc () |
32 |
{ |
|
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 | 35 |
} |
36 |
||
37 |
int main (int argc, char **argv) |
|
38 |
{ |
|
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 | 42 |
char *msg; |
43 |
char *command; |
|
44 |
int len; |
|
45 |
||
46 |
if (bindtextdomain ("mcbot", LOCALE_PATH) != NULL) { |
|
47 |
(void)textdomain ("mcbot"); |
|
48 |
(void)setlocale (LC_MESSAGES, ""); |
|
49 |
} |
|
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 | 52 |
|
53 |
if ((len = config (&uc, CONFIG_FILE)) != 0) { |
|
54 |
switch (len) { |
|
55 |
case -1: |
|
56 |
printf ("You need to create a config file %s\n", CONFIG_FILE); |
|
57 |
break; |
|
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 | 60 |
break; |
61 |
} |
|
62 |
return len; |
|
63 |
} |
|
64 |
||
65 |
if (uc.nick) |
|
66 |
message.nick = uc.nick; |
|
67 |
else |
|
68 |
return -1; |
|
69 |
||
70 |
if (!(message.stream = irc_connect (uc.server, uc.port))) |
|
71 |
return EXIT_FAILURE; |
|
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 | 74 |
switch (len) { |
75 |
case IRC_GENERAL_ERROR: |
|
76 |
printf ("GENERAL ERROR\n"); |
|
77 |
break; |
|
78 |
case IRC_LOGIN_ERROR: |
|
79 |
printf ("LOGIN ERROR\n"); |
|
80 |
break; |
|
81 |
default: |
|
82 |
printf ("Unknown Error %d\n", len); |
|
83 |
} |
|
84 |
return len; |
|
85 |
} |
|
86 |
||
87 |
signal (SIGTERM, sigproc); |
|
88 |
||
89 |
if (uc.channel) { |
|
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 | 92 |
if (uc.topic) |
93 |
fprintf (message.stream, "TOPIC %s :%s\r\n", uc.channel, uc.topic); |
|
94 |
} |
|
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 | 102 |
|
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 | 106 |
printf ("%10s %s %s\n", command, message.channel, message.line); |
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 | 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 | 111 |
printf ("%10s %s", "WRITE", msg); |
112 |
} |
|
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 | 115 |
} |
116 |
printf ("\n\nClosing Connection\n\n"); |
|
117 |
fclose (message.stream); |
|
118 |
||
119 |
/* |
|
120 |
* cleanup |
|
121 |
*/ |
|
122 |
if (uc.nick) |
|
123 |
free (uc.nick); |
|
124 |
if (uc.pass) |
|
125 |
free (uc.pass); |
|
126 |
if (uc.server) |
|
127 |
free (uc.server); |
|
128 |
if (uc.channel) |
|
129 |
free (uc.channel); |
|
130 |
if (uc.topic) |
|
131 |
free (uc.topic); |
|
132 |
||
133 |
return EXIT_SUCCESS; |
|
134 |
} |