]> jfr.im git - irc/quakenet/newserv.git/blame - patrol/patrol_commands.c
patrol: Make clients change nicks less often.
[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);
84 deregisterlocaluser(pc->nick, NULL);
85
86 *pnext = pc->next;
87 free(pc);
88
89 return 0;
90 }
91 }
92
93 return -1;
94}
95
96static int pc_cmd_patroljoin(void *source, int cargc, char **cargv) {
97 nick *sender = source;
98
99 if (cargc < 1)
100 return CMD_USAGE;
101
102 if (pc_join(cargv[0]) < 0) {
103 controlreply(sender, "Could not join channel.");
104
105 return CMD_ERROR;
106 }
107
108 controlreply(sender, "Done.");
109
110 return CMD_OK;
111}
112
113static int pc_cmd_patrolpart(void *source, int cargc, char **cargv) {
114 nick *sender = source;
115
116 if (cargc < 1)
117 return CMD_USAGE;
118
119 if (pc_part(cargv[0]) < 0) {
120 controlreply(sender, "Could not join channel.");
121
122 return CMD_ERROR;
123 }
124
125 controlreply(sender, "Done.");
126
127 return CMD_OK;
128}
129
130static int pc_cmd_patrollist(void *source, int cargc, char **cargv) {
131 nick *sender = source;
132 patrolchannel *pc;
133
134 for (pc = patrolchannels; pc; pc = pc->next)
135 controlreply(sender, "%s - %s", pc->channel->content, pc->nick ? controlid(pc->nick) : "Not currently joined.");
136
137 controlreply(sender, "End of list.");
138
139 return CMD_OK;
140}
141
142void _init(void) {
143 registercontrolhelpcmd("patroljoin", NO_OPER, 1, &pc_cmd_patroljoin, "Usage: patroljoin <channel>\nJoins a patrol client to a channel.");
144 registercontrolhelpcmd("patrolpart", NO_OPER, 1, &pc_cmd_patrolpart, "Usage: patrolpart <#id>\nRemoves a patrol client from a channel.");
145 registercontrolhelpcmd("patrollist", NO_OPER, 0, &pc_cmd_patrollist, "Usage: patrollist\nLists patrol channels.");
146
7b862ba0 147 schedulerecurring(time(NULL) + 5, 0, 10, &pc_sched_check, NULL);
e78ae679
GB
148}
149
150void _fini(void) {
151 patrolchannel *pc, *next;
152
153 deregistercontrolcmd("patroljoin", &pc_cmd_patroljoin);
154 deregistercontrolcmd("patrolpart", &pc_cmd_patrolpart);
155 deregistercontrolcmd("patrollist", &pc_cmd_patrollist);
156
157 deleteallschedules(&pc_sched_check);
158
159 for (pc = patrolchannels; pc; pc = next) {
160 next = pc->next;
161
162 freesstring(pc->channel);
163
164 if (pc->nick)
165 deregisterlocaluser(pc->nick, NULL);
166
167 free(pc);
168 }
169}
170