author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 15:38:49 +0100 | |
changeset 8 | 3152de03758e |
parent 6 | 7eb12be31bb5 |
child 9 | aff6726b8b87 |
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 <sys/types.h> |
|
12 |
#include <sys/socket.h> |
|
13 |
#include <netinet/in.h> |
|
14 |
#include <arpa/inet.h> |
|
15 |
#include <netdb.h> |
|
16 |
||
17 |
#include <signal.h> |
|
18 |
#include <locale.h> |
|
19 |
#include <libintl.h> |
|
20 |
||
21 |
#include <mcbot.h> |
|
22 |
#include <config.h> |
|
23 |
||
24 |
#ifndef CONFIG_FILE |
|
25 |
#define CONFIG_FILE "/var/lib/nobody/.mcbotrc" |
|
26 |
#endif |
|
27 |
||
28 |
#ifndef LOCALE_PATH |
|
29 |
#define LOCALE_PATH "/var/lib/nobody/data/locale" |
|
30 |
#endif |
|
31 |
||
32 |
void sigproc () |
|
33 |
{ |
|
34 |
signal (SIGTERM, sigproc); |
|
35 |
} |
|
36 |
||
37 |
int main (int argc, char **argv) |
|
38 |
{ |
|
39 |
UC uc; |
|
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
40 |
MSG message = { NULL, 0, 0, 0, 0, 0, 0 }; |
0 | 41 |
char buf[513]; |
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 |
||
51 |
printf ("mcbot-%1.2f\n", VERSION); |
|
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: |
|
59 |
printf |
|
60 |
("You need at least entries for user, password, server and channel in your config file %s\n", |
|
61 |
CONFIG_FILE); |
|
62 |
break; |
|
63 |
} |
|
64 |
return len; |
|
65 |
} |
|
66 |
||
67 |
if (uc.nick) |
|
68 |
message.nick = uc.nick; |
|
69 |
else |
|
70 |
return -1; |
|
71 |
||
72 |
if (!(message.stream = irc_connect (uc.server, uc.port))) |
|
73 |
return EXIT_FAILURE; |
|
74 |
||
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
75 |
if ((len = irc_login (message.stream, uc.server, uc.nick, uc.pass)) != 0) { |
0 | 76 |
switch (len) { |
77 |
case IRC_GENERAL_ERROR: |
|
78 |
printf ("GENERAL ERROR\n"); |
|
79 |
break; |
|
80 |
case IRC_LOGIN_ERROR: |
|
81 |
printf ("LOGIN ERROR\n"); |
|
82 |
break; |
|
83 |
default: |
|
84 |
printf ("Unknown Error %d\n", len); |
|
85 |
} |
|
86 |
return len; |
|
87 |
} |
|
88 |
||
89 |
signal (SIGTERM, sigproc); |
|
90 |
||
91 |
if (uc.channel) { |
|
92 |
fprintf (message.stream, "JOIN :%s\r\n", uc.channel); |
|
93 |
if (uc.topic) |
|
94 |
fprintf (message.stream, "TOPIC %s :%s\r\n", uc.channel, uc.topic); |
|
95 |
} |
|
96 |
||
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
97 |
while (!feof (message.stream)) { |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
98 |
message.line = NULL; |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
99 |
*buf = 0; |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
100 |
fgets (buf, 512, message.stream); |
0 | 101 |
|
102 |
if ((command = irc_parsemessage (buf, &message))) { |
|
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); |
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 |
||
118 |
if (uc.nick) |
|
119 |
free (uc.nick); |
|
120 |
if (uc.pass) |
|
121 |
free (uc.pass); |
|
122 |
if (uc.server) |
|
123 |
free (uc.server); |
|
124 |
if (uc.channel) |
|
125 |
free (uc.channel); |
|
126 |
if (uc.topic) |
|
127 |
free (uc.topic); |
|
128 |
||
129 |
return EXIT_SUCCESS; |
|
130 |
} |