author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 15:40:11 +0100 | |
changeset 9 | aff6726b8b87 |
parent 8 | 3152de03758e |
child 10 | 311ea5fa60dd |
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); |
|
9
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
93 |
message.current_channel = uc.channel; |
0 | 94 |
if (uc.topic) |
95 |
fprintf (message.stream, "TOPIC %s :%s\r\n", uc.channel, uc.topic); |
|
96 |
} |
|
97 |
||
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
98 |
while (!feof (message.stream)) { |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
99 |
message.line = NULL; |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
100 |
*buf = 0; |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
101 |
fgets (buf, 512, message.stream); |
0 | 102 |
|
103 |
if ((command = irc_parsemessage (buf, &message))) { |
|
104 |
printf ("%10s %s %s\n", command, message.channel, message.line); |
|
9
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
105 |
if (!strcmp (command, "ERROR")) |
aff6726b8b87
Private Messages will be send to the proper location
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
106 |
break; |
0 | 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); |
110 |
printf ("%10s %s", "WRITE", msg); |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
printf ("\n\nClosing Connection\n\n"); |
|
115 |
fclose (message.stream); |
|
116 |
||
117 |
/* |
|
118 |
* cleanup |
|
119 |
*/ |
|
120 |
||
121 |
if (uc.nick) |
|
122 |
free (uc.nick); |
|
123 |
if (uc.pass) |
|
124 |
free (uc.pass); |
|
125 |
if (uc.server) |
|
126 |
free (uc.server); |
|
127 |
if (uc.channel) |
|
128 |
free (uc.channel); |
|
129 |
if (uc.topic) |
|
130 |
free (uc.topic); |
|
131 |
||
132 |
return EXIT_SUCCESS; |
|
133 |
} |