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