-
Notifications
You must be signed in to change notification settings - Fork 4
/
callbacks.c
112 lines (97 loc) · 3.45 KB
/
callbacks.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
#include <stdio.h>
#include <ctype.h>
#include <lua.h>
#include <lauxlib.h>
#include "globals.h"
#include "error.h"
#include "callbacks.h"
#include "lua_callback_table.h"
// Generic libircclient callback function, passes events to registered Lua callbacks
void event_generic(irc_session_t *session, const char *event, const char *origin, const char **params, unsigned int count){
CBENTRY* func;
printf("Received '%s' event.\n", event);
for(func = cbtable_next(event, 0); func; func = cbtable_next(event, func)){
int j;
printf(" -> Sending to callback '%s'...\n", func->func);
lua_getglobal(L, func->func);
lua_pushstring(L, event);
lua_pushstring(L, origin);
lua_newtable(L);
for(j = 0; j < count; j++){
lua_pushnumber(L, j+1);
lua_pushstring(L, params[j]);
lua_settable(L, -3);
}
if(lua_pcall(L, 3, 0, 0) != 0){
// catch ERROR events so we don't make an infinite loop
if(strcmp(event, "ERROR")){
error(0, "Attempt to invoke Lua '%s' callback failed:\n%s\n", func->func ,lua_tostring(L, -1));
}
else{
fprintf(stderr, "Error calling error callback!\n%s\n", lua_tostring(L,-1));
}
}
}
}
// Callback for !commands
void event_command(irc_session_t *session, const char *event, const char *origin, const char **params, unsigned int count){
CBENTRY* func;
int i, called = 0;
char *tmp, *end, *cmd, *parms;
tmp = malloc(strlen(params[1]) + 1);
strcpy(tmp, params[1]);
end = tmp + strlen(tmp);
// use generic callback if first char isn't !
if(*tmp != '!'){
event_generic(session, event, origin, params, count);
return;
}
// split into 'command' and 'params' parts
for(parms = tmp; parms < end && *parms!=' '; parms++);
if(parms < end)
*(parms++) = '\0';
else
*parms = '\0';
cmd = malloc(strlen(tmp) + 1);
for(i = 0; i < strlen(tmp); i++)
cmd[i] = tolower(tmp[i]);
cmd[i] = '\0';
printf("Received '%s' command, params '%s'.\n", cmd, parms);
for(func = cbtable_next(cmd, 0); func; func = cbtable_next(cmd, func)){
int j;
printf(" -> Sending to callback '%s'...\n", func->func);
lua_getglobal(L, func->func);
lua_pushstring(L, cmd);
lua_pushstring(L, origin);
lua_newtable(L);
for(j = 0; j < count; j++){
lua_pushnumber(L, j+1);
if(j == 1)
lua_pushstring(L, parms);
else
lua_pushstring(L, params[j]);
lua_settable(L, -3);
}
if(lua_pcall(L, 3, 0, 0) != 0){
// catch ERROR events so we don't make an infinite loop
if(strcmp(event, "ERROR")){
error(0, "Attempt to invoke Lua '%s' callback failed:\n%s\n", func->func ,lua_tostring(L, -1));
}
else{
fprintf(stderr, "Error calling error callback!\n%s\n", lua_tostring(L,-1));
}
}
called++;
}
free(tmp);
free(cmd);
// legacy support
if(called < 1)
event_generic(session, event, origin, params, count);
}
// Callback for numeric events, just translates the numeric event code to a string and gives it to event_generic
void event_numeric(irc_session_t *session, unsigned int event, const char *origin, const char **params, unsigned int count){
char str[16];
sprintf(str, "%d", event);
event_generic(session, str, origin, params, count);
}