]> jfr.im git - irc/quakenet/newserv.git/blame - patrol/patrol_commands.c
LUA: port luadb to dbapi2 to drop postgres dependency
[irc/quakenet/newserv.git] / patrol / patrol_commands.c
CommitLineData
e78ae679
GB
1#include "../core/schedule.h"
2#include "../control/control.h"
3#include "../localuser/localuserchannel.h"
4#include "../lib/version.h"
5#include "../lib/irc_string.h"
6#include "patrol.h"
7
8MODULE_VERSION("");
9
10typedef struct patrolchannel {
11 sstring *channel;
12 nick *nick;
13
14 struct patrolchannel *next;
15} patrolchannel;
16
17static patrolchannel *patrolchannels;
18
19static void patroluserhandler(nick *np, int event, void **args) {
8995461a
GB
20 patrolchannel *pc;
21
22 if (event == LU_KILLED) {
23 for (pc = patrolchannels; pc; pc = pc->next) {
24 if (pc->nick == np) {
25 pc->nick = NULL;
26 break;
27 }
28 }
29 }
e78ae679
GB
30}
31
32static void pc_check(void) {
33 patrolchannel *pc;
34 channel *cp;
35
36 for (pc = patrolchannels; pc; pc = pc->next) {
7b862ba0 37 if (pc->nick && pc->nick->timestamp > getnettime() - 900)
e78ae679
GB
38 continue;
39
40 if (pc->nick)
41 deregisterlocaluser(pc->nick, NULL);
42
43 cp = findchannel(pc->channel->content);
44
45 if (!cp) {
46 pc->nick = NULL;
47 continue;
48 }
49
50 pc->nick = patrol_generateclone(0, patroluserhandler);
51 localjoinchannel(pc->nick, cp);
52 }
53}
54
55static void pc_sched_check(void *arg) {
56 pc_check();
57}
58
59static int pc_join(char *name) {
60 patrolchannel *pc = NULL;
61 channel *cp;
62
63 for (pc = patrolchannels; pc; pc = pc->next)
64 if (ircd_strcmp(pc->channel->content, name) == 0)
65 return 0;
66
67 cp = findchannel(name);
68
69 if (!cp)
70 return -1;
71
72 pc = malloc(sizeof(patrolchannel));
73
74 pc->channel = getsstring(name, 512);
75 pc->nick = NULL;
76
77 pc->next = patrolchannels;
78 patrolchannels = pc;
79
80 pc_check();
81
82 return 0;
83}
84
85static int pc_part(char *name) {
86 patrolchannel **pnext, *pc;
87
88 for (pnext = &patrolchannels; *pnext; pnext = &((*pnext)->next)) {
89 pc = *pnext;
90
91 if (ircd_strcmp(pc->channel->content, name) == 0) {
92 freesstring(pc->channel);
afe09004
GB
93
94 if (pc->nick)
95 deregisterlocaluser(pc->nick, NULL);
e78ae679
GB
96
97 *pnext = pc->next;
98 free(pc);
99
100 return 0;
101 }
102 }
103
104 return -1;
105}
106
107static int pc_cmd_patroljoin(void *source, int cargc, char **cargv) {
108 nick *sender = source;
109
110 if (cargc < 1)
111 return CMD_USAGE;
112
113 if (pc_join(cargv[0]) < 0) {
114 controlreply(sender, "Could not join channel.");
115
116 return CMD_ERROR;
117 }
118
119 controlreply(sender, "Done.");
120
121 return CMD_OK;
122}
123
124static int pc_cmd_patrolpart(void *source, int cargc, char **cargv) {
125 nick *sender = source;
126
127 if (cargc < 1)
128 return CMD_USAGE;
129
130 if (pc_part(cargv[0]) < 0) {
131 controlreply(sender, "Could not join channel.");
132
133 return CMD_ERROR;
134 }
135
136 controlreply(sender, "Done.");
137
138 return CMD_OK;
139}
140
141static int pc_cmd_patrollist(void *source, int cargc, char **cargv) {
142 nick *sender = source;
143 patrolchannel *pc;
144
145 for (pc = patrolchannels; pc; pc = pc->next)
146 controlreply(sender, "%s - %s", pc->channel->content, pc->nick ? controlid(pc->nick) : "Not currently joined.");
147
148 controlreply(sender, "End of list.");
149
150 return CMD_OK;
151}
152
153void _init(void) {
154 registercontrolhelpcmd("patroljoin", NO_OPER, 1, &pc_cmd_patroljoin, "Usage: patroljoin <channel>\nJoins a patrol client to a channel.");
155 registercontrolhelpcmd("patrolpart", NO_OPER, 1, &pc_cmd_patrolpart, "Usage: patrolpart <#id>\nRemoves a patrol client from a channel.");
156 registercontrolhelpcmd("patrollist", NO_OPER, 0, &pc_cmd_patrollist, "Usage: patrollist\nLists patrol channels.");
157
7b862ba0 158 schedulerecurring(time(NULL) + 5, 0, 10, &pc_sched_check, NULL);
e78ae679
GB
159}
160
161void _fini(void) {
162 patrolchannel *pc, *next;
163
164 deregistercontrolcmd("patroljoin", &pc_cmd_patroljoin);
165 deregistercontrolcmd("patrolpart", &pc_cmd_patrolpart);
166 deregistercontrolcmd("patrollist", &pc_cmd_patrollist);
167
168 deleteallschedules(&pc_sched_check);
169
170 for (pc = patrolchannels; pc; pc = next) {
171 next = pc->next;
172
173 freesstring(pc->channel);
174
175 if (pc->nick)
176 deregisterlocaluser(pc->nick, NULL);
177
178 free(pc);
179 }
180}
181