author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 15:38:48 +0100 | |
changeset 7 | b04ef0b1cd3b |
parent 6 | 7eb12be31bb5 |
child 8 | 3152de03758e |
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 |
short active = 1; |
|
33 |
||
34 |
void sigproc () |
|
35 |
{ |
|
36 |
signal (SIGTERM, sigproc); |
|
37 |
active = 0; |
|
38 |
} |
|
39 |
||
40 |
int main (int argc, char **argv) |
|
41 |
{ |
|
42 |
UC uc; |
|
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
43 |
MSG message = { NULL, 0, 0, 0, 0, 0, 0 }; |
0 | 44 |
char buf[513]; |
45 |
char *msg; |
|
46 |
char *command; |
|
47 |
int len; |
|
48 |
||
49 |
if (bindtextdomain ("mcbot", LOCALE_PATH) != NULL) { |
|
50 |
(void)textdomain ("mcbot"); |
|
51 |
(void)setlocale (LC_MESSAGES, ""); |
|
52 |
} |
|
53 |
||
54 |
printf ("mcbot-%1.2f\n", VERSION); |
|
55 |
||
56 |
if ((len = config (&uc, CONFIG_FILE)) != 0) { |
|
57 |
switch (len) { |
|
58 |
case -1: |
|
59 |
printf ("You need to create a config file %s\n", CONFIG_FILE); |
|
60 |
break; |
|
61 |
case -2: |
|
62 |
printf |
|
63 |
("You need at least entries for user, password, server and channel in your config file %s\n", |
|
64 |
CONFIG_FILE); |
|
65 |
break; |
|
66 |
} |
|
67 |
return len; |
|
68 |
} |
|
69 |
||
70 |
if (uc.nick) |
|
71 |
message.nick = uc.nick; |
|
72 |
else |
|
73 |
return -1; |
|
74 |
||
75 |
if (!(message.stream = irc_connect (uc.server, uc.port))) |
|
76 |
return EXIT_FAILURE; |
|
77 |
||
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
78 |
if ((len = irc_login (message.stream, uc.server, uc.nick, uc.pass)) != 0) { |
0 | 79 |
switch (len) { |
80 |
case IRC_GENERAL_ERROR: |
|
81 |
printf ("GENERAL ERROR\n"); |
|
82 |
break; |
|
83 |
case IRC_LOGIN_ERROR: |
|
84 |
printf ("LOGIN ERROR\n"); |
|
85 |
break; |
|
86 |
default: |
|
87 |
printf ("Unknown Error %d\n", len); |
|
88 |
} |
|
89 |
return len; |
|
90 |
} |
|
91 |
||
92 |
signal (SIGTERM, sigproc); |
|
93 |
||
94 |
if (uc.channel) { |
|
95 |
fprintf (message.stream, "JOIN :%s\r\n", uc.channel); |
|
96 |
if (uc.topic) |
|
97 |
fprintf (message.stream, "TOPIC %s :%s\r\n", uc.channel, uc.topic); |
|
98 |
} |
|
99 |
||
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
100 |
while (!feof (message.stream)) { |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
101 |
message.line = NULL; |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
102 |
*buf = 0; |
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
103 |
fgets (buf, 512, message.stream); |
0 | 104 |
|
105 |
if ((command = irc_parsemessage (buf, &message))) { |
|
106 |
printf ("%10s %s %s\n", command, message.channel, message.line); |
|
107 |
} else { |
|
108 |
if ((msg = parse (&message, &active)) != NULL) { |
|
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 |
} |