-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
168 lines (131 loc) · 3.47 KB
/
commands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "irc.h"
strmap_t factoids;
FILE *factoid_file;
void init_factoids() {
char line[512];
strmap_init(&factoids);
factoid_file = fopen("factoids", "r");
if (factoid_file == NULL) return;
while (fgets(line, 512, factoid_file)) {
strmap_insert(&factoids, line, line + strlen(line) + 1);
}
fclose(factoid_file);
}
int write_factoid(const char *key, const char *value) {
fwrite(key, 1, strlen(key) + 1, factoid_file);
fwrite(value, 1, strlen(value) + 1, factoid_file);
fputc('\n', factoid_file);
return 0;
}
void destroy_factoids() {
factoid_file = fopen("factoids", "w");
if (factoid_file == NULL) return;
strmap_iterate(&factoids, write_factoid);
strmap_destroy(&factoids);
fclose(factoid_file);
}
char *factoid_set(array *args, struct irc_message *msg) {
size_t i;
char value[512];
char *s;
if (array_count(args) < 2) return NULL;
join_string(args, 1, value, " ");
/*
value[0] = 0;
for (i = 1; i != array_count(args); ++i) {
s = *(char**)array_at(args, i);
strcat(value, s);
strcat(value, " ");
}
*/
strmap_insert(&factoids, *(char**)array_at(args, 0), value);
return NULL;
}
char *factoid_get(array *args, struct irc_message *msg) {
const char *value;
if (array_count(args) < 1) return NULL;
value = strmap_find(&factoids, *(char**)array_at(args, 0));
if (value == NULL) return NULL;
return new_string(value);
}
char *shell(array *args, struct irc_message *msg) {
char dope[512];
int hookah[6];
ssize_t readbytes;
pid_t pothead;
char *gram;
if (array_count(args) < 1) return NULL;
join_string(args, 0, dope, " ");
pipe(&hookah[0]);
pipe(&hookah[2]);
pipe(&hookah[4]);
pothead = fork();
if (pothead == 0) {
dup2(hookah[0], 0); close(hookah[1]);
dup2(hookah[3], 1); close(hookah[2]);
dup2(hookah[5], 2); close(hookah[4]);
execl("/bin/bash", "/bin/bash", "-s", NULL);
exit(0);
}
close(hookah[0]);
close(hookah[3]);
close(hookah[5]);
write(hookah[1], dope, strlen(dope));
close(hookah[1]);
memset(dope, 0, 512);
for (gram = dope; (readbytes = read(hookah[2], gram, dope + 512 - gram)) > 0; gram += readbytes);
for (gram = dope; *gram; ++gram) {
if (*gram == '\n' || *gram == '\r') *gram = ' ';
}
return new_string(dope);
}
char *irc_join(array *args, struct irc_message *msg) {
fprintf(irc, "JOIN %s\r\n", *(char**)array_at(args, 0));
return NULL;
}
char *irc_part(array *args, struct irc_message *msg) {
fprintf(irc, "PART %s :%s\r\n", *(char**)array_at(args, 0), *(char**)array_at(args, 1));
return NULL;
}
char *irc_say(array *args, struct irc_message *msg) {
int i;
if (array_count(args) < 2) return NULL;
fprintf(irc, "PRIVMSG %s :", *(char**)array_at(args, 0));
for (i = 1; i < array_count(args); ++i) {
fprintf(irc, "%s ", *(char**)array_at(args, i));
}
fputs("\r\n", irc);
return NULL;
}
char *irc_quit(array *args, struct irc_message *msg) {
fprintf(irc, "QUIT :%s\r\n", *(char**)array_at(args, 0));
return NULL;
}
struct bot_command bot_cmds[] = {
{ "@join", irc_join },
{ "@part", irc_part },
{ "@say", irc_say },
{ "@quit", irc_quit },
{ "set", factoid_set },
{ "get", factoid_get },
{ "@shell", shell },
{ NULL, NULL }
};
void (*command_init_funcs[])() = {
init_factoids,
NULL
};
void (*command_cleanup_funcs[])() = {
destroy_factoids,
NULL
};
void init_bot_commands() {
size_t i;
for (i = 0; command_init_funcs[i] != NULL; ++i)
command_init_funcs[i]();
}
void cleanup_bot_commands() {
size_t i;
for (i = 0; command_cleanup_funcs[i] != NULL; ++i)
command_cleanup_funcs[i]();
}