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