]> jfr.im git - irc/freenode/syn.git/blame - main.c
masks.c: add help files
[irc/freenode/syn.git] / main.c
CommitLineData
9967d39a
SB
1#include "atheme.h"
2
9967d39a 3static void syn_handler(sourceinfo_t *si, int parc, char *parv[]);
3e65cfbd 4static void syn_join_channel(void *unused);
9967d39a
SB
5
6service_t *syn;
9967d39a 7
3e65cfbd
SB
8struct
9{
10 char *channel;
af4357d7 11 char *debugchannel;
345beb92 12 unsigned int debug;
92d07997 13 unsigned int verbosity;
3e65cfbd
SB
14} syn_config;
15
492a4dc0 16static void mod_init(module_t *m)
9967d39a
SB
17{
18// command_add(&syn_help, &syn_cmdtree);
19
b918d5d9
SB
20 syn = service_add("syn", syn_handler);
21 service_set_chanmsg(syn, true);
9967d39a 22
3e65cfbd 23 hook_add_event("config_ready");
a3064be2
SB
24 hook_add_config_ready((void(*)(void*))syn_join_channel);
25 hook_add_server_eob((void(*)(server_t*))syn_join_channel);
3e65cfbd 26
b918d5d9
SB
27 add_dupstr_conf_item("CHANNEL", &syn->conf_table, 0, &syn_config.channel, 0);
28 add_dupstr_conf_item("DEBUGCHANNEL", &syn->conf_table, 0, &syn_config.debugchannel, 0);
29 add_uint_conf_item("DEBUG", &syn->conf_table, 0, &syn_config.debug, 0, 15, 0);
30 add_uint_conf_item("VERBOSE", &syn->conf_table, 0, &syn_config.verbosity, 0, 15, 0);
9967d39a
SB
31}
32
492a4dc0 33static void mod_deinit(module_unload_intent_t intent)
9967d39a
SB
34{
35// command_delete(&syn_help, &syn_cmdtree);
36
b918d5d9
SB
37 del_conf_item("CHANNEL", &syn->conf_table);
38 del_conf_item("DEBUGCHANNEL", &syn->conf_table);
39 del_conf_item("DEBUG", &syn->conf_table);
40 del_conf_item("VERBOSE", &syn->conf_table);
33ca2ed9 41
a3064be2
SB
42 hook_del_config_ready((void(*)(void*))syn_join_channel);
43 hook_del_server_eob((void(*)(server_t*))syn_join_channel);
b4fcac0e 44
9967d39a
SB
45 service_delete(syn);
46}
47
47ef7988
SB
48static void syn_cmd_success_nodata(sourceinfo_t *si, const char *text)
49{
50 if (si->c)
51 notice_channel_sts(si->service->me, si->c, text);
52 else
53 notice_user_sts(si->service->me, si->su, text);
54}
55
56
57static void syn_cmd_success_string(sourceinfo_t *si, const char *string, const char *text)
58{
59 if (si->c)
60 notice_channel_sts(si->service->me, si->c, text);
61 else
62 notice_user_sts(si->service->me, si->su, text);
63}
64
65
b918d5d9 66static void syn_cmd_fail(sourceinfo_t *si, cmd_faultcode_t fault, const char *text)
47ef7988
SB
67{
68 if (si->c)
69 notice_channel_sts(si->service->me, si->c, text);
70 else
71 notice_user_sts(si->service->me, si->su, text);
72}
73
7c6e03c2
JK
74struct sourceinfo_vtable syn_si_vtable = {
75 .description = "syn",
76 .cmd_fail = syn_cmd_fail,
77 .cmd_success_nodata = syn_cmd_success_nodata,
78 .cmd_success_string = syn_cmd_success_string,
79};
47ef7988
SB
80
81
9967d39a
SB
82static void syn_handler(sourceinfo_t *si, int parc, char *parv[])
83{
84 char *cmd;
85 char *text;
86 char orig[BUFSIZE];
87
88 /* this should never happen */
89 if (parv[0][0] == '&')
90 {
91 slog(LG_ERROR, "services(): got parv with local channel: %s", parv[0]);
92 return;
93 }
94
95 /* make a copy of the original for debugging */
b918d5d9 96 mowgli_strlcpy(orig, parv[parc - 1], BUFSIZE);
9967d39a 97
72dcadfa
SB
98 // Is this a message to a channel?
99 if (parv[0][0] == '#')
100 {
101 if (!syn_config.channel || 0 != strcmp(syn_config.channel, parv[0]))
102 return;
103
104 char *firstarg = strtok(parv[parc-1], " ");
105 if (!firstarg || 0 != strncmp(si->service->nick, firstarg, strlen(si->service->nick)))
106 return;
107
108 si->c = channel_find(parv[0]);
109
110 cmd = strtok(NULL, " ");
111 text = strtok(NULL, "");
112 }
113 else
114 {
115 cmd = strtok(parv[parc - 1], " ");
116 text = strtok(NULL, "");
117 }
9967d39a
SB
118
119 if (!cmd)
120 return;
121 if (*cmd == '\001')
122 {
123 handle_ctcp_common(si, cmd, text);
124 return;
125 }
126
47ef7988
SB
127 si->v = &syn_si_vtable;
128
9967d39a 129 /* take the command through the hash table */
b918d5d9 130 command_exec_split(si->service, si, cmd, text, syn->commands);
9967d39a
SB
131}
132
3e65cfbd
SB
133static void syn_join_channel(void *unused)
134{
b4fcac0e 135 if (syn_config.channel && me.connected)
3e65cfbd 136 join(syn_config.channel, syn->nick);
af4357d7
SB
137 if (syn_config.debugchannel && me.connected)
138 join(syn_config.debugchannel, syn->nick);
3e65cfbd
SB
139}
140
b56c9b7a
SB
141void syn_debug(int debuglevel, char *fmt, ...)
142{
143 if (debuglevel > syn_config.debug)
144 return;
145
146 va_list ap;
147 char buf[BUFSIZE];
148
af4357d7
SB
149 char *debugchannel = syn_config.debugchannel;
150
151 if (!debugchannel)
152 debugchannel = syn_config.channel;
153
154 if (!debugchannel)
b56c9b7a
SB
155 return;
156
af4357d7 157 if (!channel_find(debugchannel))
b56c9b7a
SB
158 return;
159
160 va_start(ap, fmt);
161 vsnprintf(buf, BUFSIZE, fmt, ap);
162 va_end(ap);
163
af4357d7 164 msg(syn->nick, debugchannel, "[debug%d] %s", debuglevel, buf);
b56c9b7a
SB
165}
166
92d07997 167void syn_vreport(char *fmt, va_list ap)
3e65cfbd 168{
3e65cfbd
SB
169 char buf[BUFSIZE];
170
171 if (!syn_config.channel)
172 return;
173
174 if (!channel_find(syn_config.channel))
175 return;
176
3e65cfbd 177 vsnprintf(buf, BUFSIZE, fmt, ap);
3e65cfbd
SB
178
179 msg(syn->nick, syn_config.channel, "%s", buf);
180}
92d07997
SB
181
182void syn_report(char *fmt, ...)
183{
184 va_list ap;
185 va_start(ap, fmt);
186 syn_vreport(fmt, ap);
187 va_end(ap);
188}
189
190void syn_report2(unsigned int level, char *fmt, ...)
191{
192 if (syn_config.verbosity < level)
193 return;
194
195 va_list ap;
196 va_start(ap, fmt);
197 syn_vreport(fmt, ap);
198 va_end(ap);
199}
200
492a4dc0
JK
201DECLARE_MODULE_V1
202(
203 "syn/main", false, mod_init, mod_deinit,
204 "$Revision$",
205 "Stephen Bennett <stephen -at- freenode.net>"
206);