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