]> jfr.im git - irc/quakenet/newserv.git/blame - patrol/patrol_commands.c
CHANSERV: fix issue where chanserv_relay doesn't wait for db to be loaded before...
[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) {
20 /* Nothing to do here. */
21}
22
23static void pc_check(void) {
24 patrolchannel *pc;
25 channel *cp;
26
27 for (pc = patrolchannels; pc; pc = pc->next) {
7b862ba0 28 if (pc->nick && pc->nick->timestamp > getnettime() - 900)
e78ae679
GB
29 continue;
30
31 if (pc->nick)
32 deregisterlocaluser(pc->nick, NULL);
33
34 cp = findchannel(pc->channel->content);
35
36 if (!cp) {
37 pc->nick = NULL;
38 continue;
39 }
40
41 pc->nick = patrol_generateclone(0, patroluserhandler);
42 localjoinchannel(pc->nick, cp);
43 }
44}
45
46static void pc_sched_check(void *arg) {
47 pc_check();
48}
49
50static int pc_join(char *name) {
51 patrolchannel *pc = NULL;
52 channel *cp;
53
54 for (pc = patrolchannels; pc; pc = pc->next)
55 if (ircd_strcmp(pc->channel->content, name) == 0)
56 return 0;
57
58 cp = findchannel(name);
59
60 if (!cp)
61 return -1;
62
63 pc = malloc(sizeof(patrolchannel));
64
65 pc->channel = getsstring(name, 512);
66 pc->nick = NULL;
67
68 pc->next = patrolchannels;
69 patrolchannels = pc;
70
71 pc_check();
72
73 return 0;
74}
75
76static int pc_part(char *name) {
77 patrolchannel **pnext, *pc;
78
79 for (pnext = &patrolchannels; *pnext; pnext = &((*pnext)->next)) {
80 pc = *pnext;
81
82 if (ircd_strcmp(pc->channel->content, name) == 0) {
83 freesstring(pc->channel);
afe09004
GB
84
85 if (pc->nick)
86 deregisterlocaluser(pc->nick, NULL);
e78ae679
GB
87
88 *pnext = pc->next;
89 free(pc);
90
91 return 0;
92 }
93 }
94
95 return -1;
96}
97
98static int pc_cmd_patroljoin(void *source, int cargc, char **cargv) {
99 nick *sender = source;
100
101 if (cargc < 1)
102 return CMD_USAGE;
103
104 if (pc_join(cargv[0]) < 0) {
105 controlreply(sender, "Could not join channel.");
106
107 return CMD_ERROR;
108 }
109
110 controlreply(sender, "Done.");
111
112 return CMD_OK;
113}
114
115static int pc_cmd_patrolpart(void *source, int cargc, char **cargv) {
116 nick *sender = source;
117
118 if (cargc < 1)
119 return CMD_USAGE;
120
121 if (pc_part(cargv[0]) < 0) {
122 controlreply(sender, "Could not join channel.");
123
124 return CMD_ERROR;
125 }
126
127 controlreply(sender, "Done.");
128
129 return CMD_OK;
130}
131
132static int pc_cmd_patrollist(void *source, int cargc, char **cargv) {
133 nick *sender = source;
134 patrolchannel *pc;
135
136 for (pc = patrolchannels; pc; pc = pc->next)
137 controlreply(sender, "%s - %s", pc->channel->content, pc->nick ? controlid(pc->nick) : "Not currently joined.");
138
139 controlreply(sender, "End of list.");
140
141 return CMD_OK;
142}
143
144void _init(void) {
145 registercontrolhelpcmd("patroljoin", NO_OPER, 1, &pc_cmd_patroljoin, "Usage: patroljoin <channel>\nJoins a patrol client to a channel.");
146 registercontrolhelpcmd("patrolpart", NO_OPER, 1, &pc_cmd_patrolpart, "Usage: patrolpart <#id>\nRemoves a patrol client from a channel.");
147 registercontrolhelpcmd("patrollist", NO_OPER, 0, &pc_cmd_patrollist, "Usage: patrollist\nLists patrol channels.");
148
7b862ba0 149 schedulerecurring(time(NULL) + 5, 0, 10, &pc_sched_check, NULL);
e78ae679
GB
150}
151
152void _fini(void) {
153 patrolchannel *pc, *next;
154
155 deregistercontrolcmd("patroljoin", &pc_cmd_patroljoin);
156 deregistercontrolcmd("patrolpart", &pc_cmd_patrolpart);
157 deregistercontrolcmd("patrollist", &pc_cmd_patrollist);
158
159 deleteallschedules(&pc_sched_check);
160
161 for (pc = patrolchannels; pc; pc = next) {
162 next = pc->next;
163
164 freesstring(pc->channel);
165
166 if (pc->nick)
167 deregisterlocaluser(pc->nick, NULL);
168
169 free(pc);
170 }
171}
172