4 #include "ClientSocket.h"
9 struct trigger_callback {
11 trigger_callback_t *func;
13 struct trigger_callback *next;
16 static struct cmd_binding **cmd_binds;
17 static struct cmd_function *cmd_functions = NULL;
18 static struct trigger_callback *trigger_callbacks = NULL;
20 static int get_binds_index(char first_char) {
21 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
22 return tolower(first_char - 'a');
27 struct ClientSocket* get_prefered_bot(int botid) {
28 struct ClientSocket *client, *source = NULL;
29 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
30 if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERED))
36 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
37 struct trigger_cache *trigger;
38 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
39 if(trigger->botid == botid)
40 return trigger->trigger;
42 struct trigger_callback *cb;
43 for(cb = trigger_callbacks; cb; cb = cb->next) {
44 if(cb->botid == botid)
47 char triggerStr[TRIGGERLEN];
49 cb->func(chan, triggerStr);
51 strcpy(triggerStr, "+");
52 trigger = malloc(sizeof(*trigger));
54 perror("malloc() failed");
57 trigger->botid = botid;
58 trigger->trigger = strdup(triggerStr);
59 trigger->next = chan->trigger;
60 chan->trigger = trigger;
61 return trigger->trigger;
64 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
65 if(!strcmp(message, "users")) {
66 struct ChanUser *chanuser;
67 putsock(client, "PRIVMSG %s :[BOT JOIN] Users on this Channel:", chan->name);
68 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
69 putsock(client, "PRIVMSG %s : %s!%s@%s [%s] rights: %d", chan->name, chanuser->user->nick, chanuser->user->ident, chanuser->user->host, ((chanuser->user->flags & USERFLAG_ISAUTHED) ? chanuser->user->auth : "*"), chanuser->flags);
72 if(!strcmp(message, "modes")) {
74 getModeString(chan, modeBuf);
75 putsock(client, "PRIVMSG %s :Modes: %s", chan->name, modeBuf);
79 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
82 struct ClientSocket *client;
84 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
85 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERED) && !FD_ISSET(client->botid, &fds)) {
86 FD_SET(client->botid, &fds);
87 trigger = get_channel_trigger(client->botid, chan);
88 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
89 handle_command(client, user, chan, message + strlen(trigger));
93 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
94 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
95 FD_SET(client->botid, &fds);
96 trigger = get_channel_trigger(client->botid, chan);
97 if(stricmplen(message, trigger, strlen(trigger)) == 0) {
98 handle_command(client, user, chan, message + strlen(trigger));
104 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
105 struct ClientSocket *client;
106 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
107 if(client->user == target) {
108 handle_command(client, user, NULL, message);
113 int register_command(int botid, char *name, cmd_bind_t *func) {
114 struct cmd_function *cmdfunc;
115 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
116 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
119 cmdfunc = malloc(sizeof(*cmdfunc));
121 perror("malloc() failed");
124 cmdfunc->botid = botid;
125 cmdfunc->name = strdup(name);
126 cmdfunc->func = func;
127 cmdfunc->next = cmd_functions;
128 cmd_functions = cmdfunc;
132 int set_trigger_callback(int botid, trigger_callback_t *func) {
133 static struct trigger_callback *cb = NULL;
134 for(cb = trigger_callbacks; cb; cb = cb->next) {
135 if(cb->botid == botid)
139 cb = malloc(sizeof(*cb));
141 perror("malloc() failed");
145 cb->next = trigger_callbacks;
146 trigger_callbacks = cb;
152 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
153 struct trigger_cache *trigger;
154 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
155 if(trigger->botid == botid) {
156 free(trigger->trigger);
157 trigger->trigger = strdup(new_trigger);
164 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
165 int bind_index = get_binds_index(cmd[0]);
166 struct cmd_binding *cbind;
167 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
168 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
171 cbind = malloc(sizeof(*cbind));
173 perror("malloc() failed");
176 cbind->botid = botid;
177 cbind->cmd = strdup(cmd);
179 cbind->next = cmd_binds[bind_index];
180 cmd_binds[bind_index] = cbind;
184 int bind_cmd_to_command(int botid, char *cmd, char *func) {
185 struct cmd_function *cmdfunc;
186 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
187 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
190 if(!cmdfunc) return 0;
191 int bind_index = get_binds_index(cmd[0]);
192 struct cmd_binding *cbind;
193 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
194 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
197 cbind = malloc(sizeof(*cbind));
199 perror("malloc() failed");
202 cbind->botid = botid;
203 cbind->cmd = strdup(cmd);
204 cbind->func = cmdfunc;
205 cbind->next = cmd_binds[bind_index];
206 cmd_binds[bind_index] = cbind;
210 int unbind_cmd(int botid, char *cmd) {
211 int bind_index = get_binds_index(cmd[0]);
212 struct cmd_binding *cbind, *last = NULL;
213 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
214 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
216 last->next = cbind->next;
218 cmd_binds[bind_index] = cbind->next;
229 cmd_binds = calloc(27, sizeof(*cmd_binds));
230 bind_chanmsg(got_chanmsg);
231 bind_privmsg(got_privmsg);