author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 30 May 2009 21:56:21 +0200 | |
changeset 38 | c2f62d1b8d76 |
parent 33 | 56571d34d754 |
child 47 | a689b6a8e6ed |
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 |
|
19 |
#ifndef CONFIG_FILE |
|
31
bbcb8a3366b4
mcbot needs a proper system account
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
20 |
#define CONFIG_FILE "/var/lib/mcbot/.mcbotrc" |
0 | 21 |
#endif |
22 |
||
23 |
#ifndef LOCALE_PATH |
|
31
bbcb8a3366b4
mcbot needs a proper system account
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
24 |
#define LOCALE_PATH "/var/lib/mcbot/data/locale" |
0 | 25 |
#endif |
26 |
||
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
|
27 |
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
|
28 |
|
0 | 29 |
void sigproc () |
30 |
{ |
|
31 |
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
|
32 |
active = 0; |
0 | 33 |
} |
34 |
||
35 |
int main (int argc, char **argv) |
|
36 |
{ |
|
37 |
UC uc; |
|
10
311ea5fa60dd
Code cleanups and debian changelogs
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
38 |
MSG message; |
33
56571d34d754
safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
31
diff
changeset
|
39 |
char buf[DEFAULT_BUF_SIZE]; |
0 | 40 |
char *msg; |
41 |
char *command; |
|
42 |
int len; |
|
43 |
||
44 |
if (bindtextdomain ("mcbot", LOCALE_PATH) != NULL) { |
|
45 |
(void)textdomain ("mcbot"); |
|
46 |
(void)setlocale (LC_MESSAGES, ""); |
|
47 |
} |
|
48 |
||
23
1cd79bb84e9c
ChangeLog for mcbot-0.96
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
18
diff
changeset
|
49 |
printf ("mcbot-%1.2f Build %s\n", BOT_VERSION, BOT_BUILD); |
0 | 50 |
|
51 |
if ((len = config (&uc, CONFIG_FILE)) != 0) { |
|
52 |
switch (len) { |
|
53 |
case -1: |
|
54 |
printf ("You need to create a config file %s\n", CONFIG_FILE); |
|
55 |
break; |
|
56 |
case -2: |
|
18
4435146391ae
LOGIN without Password added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
17
diff
changeset
|
57 |
printf ("You need at least entries for user, server and channel in your config file %s\n", CONFIG_FILE); |
0 | 58 |
break; |
59 |
} |
|
60 |
return len; |
|
61 |
} |
|
62 |
||
63 |
if (uc.nick) |
|
64 |
message.nick = uc.nick; |
|
65 |
else |
|
66 |
return -1; |
|
67 |
||
68 |
if (!(message.stream = irc_connect (uc.server, uc.port))) |
|
69 |
return EXIT_FAILURE; |
|
70 |
||
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
71 |
if ((len = irc_login (message.stream, uc.server, uc.nick, uc.pass)) != 0) { |
0 | 72 |
switch (len) { |
73 |
case IRC_GENERAL_ERROR: |
|
74 |
printf ("GENERAL ERROR\n"); |
|
75 |
break; |
|
76 |
case IRC_LOGIN_ERROR: |
|
77 |
printf ("LOGIN ERROR\n"); |
|
78 |
break; |
|
79 |
default: |
|
80 |
printf ("Unknown Error %d\n", len); |
|
81 |
} |
|
82 |
return len; |
|
83 |
} |
|
84 |
||
85 |
signal (SIGTERM, sigproc); |
|
86 |
||
87 |
if (uc.channel) { |
|
88 |
fprintf (message.stream, "JOIN :%s\r\n", uc.channel); |
|
15
f19c1f9b4cd3
ChangeLog mcbot-0.95-1
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
11
diff
changeset
|
89 |
strncpy (message.current_channel, uc.channel, 40); |
0 | 90 |
if (uc.topic) |
91 |
fprintf (message.stream, "TOPIC %s :%s\r\n", uc.channel, uc.topic); |
|
92 |
} |
|
93 |
||
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
94 |
while (!feof (message.stream)) { |
33
56571d34d754
safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
31
diff
changeset
|
95 |
fgets (buf, sizeof (buf), message.stream); |
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
|
96 |
|
17
9a32b5242320
BUG INTRODUCED from a6c36a2b97f6bed5577d66474949c8ec8b32c8e8
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
16
diff
changeset
|
97 |
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
|
98 |
break; |
0 | 99 |
|
100 |
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
|
101 |
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
|
102 |
break; |
0 | 103 |
printf ("%10s %s %s\n", command, message.channel, message.line); |
104 |
} else { |
|
8
3152de03758e
A critical change was introduced here...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
105 |
if ((msg = parse (&message)) != NULL) { |
0 | 106 |
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
|
107 |
fflush (message.stream); |
0 | 108 |
printf ("%10s %s", "WRITE", msg); |
109 |
} |
|
110 |
} |
|
33
56571d34d754
safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
31
diff
changeset
|
111 |
fflush (stdout); |
0 | 112 |
} |
113 |
printf ("\n\nClosing Connection\n\n"); |
|
114 |
fclose (message.stream); |
|
115 |
||
116 |
/* |
|
117 |
* cleanup |
|
118 |
*/ |
|
119 |
if (uc.nick) |
|
120 |
free (uc.nick); |
|
121 |
if (uc.pass) |
|
122 |
free (uc.pass); |
|
123 |
if (uc.server) |
|
124 |
free (uc.server); |
|
125 |
if (uc.channel) |
|
126 |
free (uc.channel); |
|
127 |
if (uc.topic) |
|
128 |
free (uc.topic); |
|
129 |
||
130 |
return EXIT_SUCCESS; |
|
131 |
} |