This repository was archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
225 lines (177 loc) · 5.62 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* syn: a utility bot to manage IRC network access
* Copyright (C) 2009-2016 Stephen Bennett
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "atheme.h"
static void syn_handler(sourceinfo_t *si, int parc, char *parv[]);
static void syn_join_channel(void *unused);
service_t *syn;
struct
{
char *channel;
char *debugchannel;
unsigned int debug;
unsigned int verbosity;
} syn_config;
static void mod_init(module_t *m)
{
// command_add(&syn_help, &syn_cmdtree);
syn = service_add("syn", syn_handler);
service_set_chanmsg(syn, true);
hook_add_event("config_ready");
hook_add_config_ready((void(*)(void*))syn_join_channel);
hook_add_server_eob((void(*)(server_t*))syn_join_channel);
add_dupstr_conf_item("CHANNEL", &syn->conf_table, 0, &syn_config.channel, 0);
add_dupstr_conf_item("DEBUGCHANNEL", &syn->conf_table, 0, &syn_config.debugchannel, 0);
add_uint_conf_item("DEBUG", &syn->conf_table, 0, &syn_config.debug, 0, 15, 0);
add_uint_conf_item("VERBOSE", &syn->conf_table, 0, &syn_config.verbosity, 0, 15, 0);
}
static void mod_deinit(module_unload_intent_t intent)
{
// command_delete(&syn_help, &syn_cmdtree);
del_conf_item("CHANNEL", &syn->conf_table);
del_conf_item("DEBUGCHANNEL", &syn->conf_table);
del_conf_item("DEBUG", &syn->conf_table);
del_conf_item("VERBOSE", &syn->conf_table);
hook_del_config_ready((void(*)(void*))syn_join_channel);
hook_del_server_eob((void(*)(server_t*))syn_join_channel);
service_delete(syn);
}
static void syn_cmd_success_nodata(sourceinfo_t *si, const char *text)
{
if (si->c)
notice_channel_sts(si->service->me, si->c, text);
else
notice_user_sts(si->service->me, si->su, text);
}
static void syn_cmd_success_string(sourceinfo_t *si, const char *string, const char *text)
{
if (si->c)
notice_channel_sts(si->service->me, si->c, text);
else
notice_user_sts(si->service->me, si->su, text);
}
static void syn_cmd_fail(sourceinfo_t *si, cmd_faultcode_t fault, const char *text)
{
if (si->c)
notice_channel_sts(si->service->me, si->c, text);
else
notice_user_sts(si->service->me, si->su, text);
}
struct sourceinfo_vtable syn_si_vtable = {
.description = "syn",
.cmd_fail = syn_cmd_fail,
.cmd_success_nodata = syn_cmd_success_nodata,
.cmd_success_string = syn_cmd_success_string,
};
static void syn_handler(sourceinfo_t *si, int parc, char *parv[])
{
char *cmd;
char *text;
char orig[BUFSIZE];
/* this should never happen */
if (parv[0][0] == '&')
{
slog(LG_ERROR, "services(): got parv with local channel: %s", parv[0]);
return;
}
/* make a copy of the original for debugging */
mowgli_strlcpy(orig, parv[parc - 1], BUFSIZE);
// Is this a message to a channel?
if (parv[0][0] == '#')
{
if (!syn_config.channel || 0 != strcmp(syn_config.channel, parv[0]))
return;
char *firstarg = strtok(parv[parc-1], " ");
if (!firstarg || 0 != strncmp(si->service->nick, firstarg, strlen(si->service->nick)))
return;
si->c = channel_find(parv[0]);
cmd = strtok(NULL, " ");
text = strtok(NULL, "");
}
else
{
cmd = strtok(parv[parc - 1], " ");
text = strtok(NULL, "");
}
if (!cmd)
return;
if (*cmd == '\001')
{
handle_ctcp_common(si, cmd, text);
return;
}
si->v = &syn_si_vtable;
/* take the command through the hash table */
command_exec_split(si->service, si, cmd, text, syn->commands);
}
static void syn_join_channel(void *unused)
{
if (syn_config.channel && me.connected)
join(syn_config.channel, syn->nick);
if (syn_config.debugchannel && me.connected)
join(syn_config.debugchannel, syn->nick);
}
void syn_debug(int debuglevel, char *fmt, ...)
{
if (debuglevel > syn_config.debug)
return;
va_list ap;
char buf[BUFSIZE];
char *debugchannel = syn_config.debugchannel;
if (!debugchannel)
debugchannel = syn_config.channel;
if (!debugchannel)
return;
if (!channel_find(debugchannel))
return;
va_start(ap, fmt);
vsnprintf(buf, BUFSIZE, fmt, ap);
va_end(ap);
msg(syn->nick, debugchannel, "[debug%d] %s", debuglevel, buf);
}
void syn_vreport(char *fmt, va_list ap)
{
char buf[BUFSIZE];
if (!syn_config.channel)
return;
if (!channel_find(syn_config.channel))
return;
vsnprintf(buf, BUFSIZE, fmt, ap);
msg(syn->nick, syn_config.channel, "%s", buf);
}
void syn_report(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
syn_vreport(fmt, ap);
va_end(ap);
}
void syn_report2(unsigned int level, char *fmt, ...)
{
if (syn_config.verbosity < level)
return;
va_list ap;
va_start(ap, fmt);
syn_vreport(fmt, ap);
va_end(ap);
}
DECLARE_MODULE_V1
(
"syn/main", false, mod_init, mod_deinit,
"$Revision$",
"Stephen Bennett <stephen -at- freenode.net>"
);