]>
Commit | Line | Data |
---|---|---|
1 | /* control.c */ | |
2 | ||
3 | /* | |
4 | * This is the first client module for newserv :) | |
5 | * | |
6 | * A very simple bot which should give people some ideas for how to | |
7 | * implement stuff on this thing | |
8 | */ | |
9 | ||
10 | #include "../irc/irc_config.h" | |
11 | #include "../parser/parser.h" | |
12 | #include "../localuser/localuser.h" | |
13 | #include "../localuser/localuserchannel.h" | |
14 | #include "../nick/nick.h" | |
15 | #include "../lib/sstring.h" | |
16 | #include "../core/config.h" | |
17 | #include "../irc/irc.h" | |
18 | #include "../lib/splitline.h" | |
19 | #include "../lib/flags.h" | |
20 | #include "../core/schedule.h" | |
21 | #include "../lib/base64.h" | |
22 | #include "../core/modules.h" | |
23 | ||
24 | #include <stdio.h> | |
25 | #include <string.h> | |
26 | ||
27 | nick *mynick; | |
28 | ||
29 | nick *statsnick; | |
30 | ||
31 | CommandTree *controlcmds; | |
32 | ||
33 | void handlemessages(nick *target, int messagetype, void **args); | |
34 | int controlstatus(void *sender, int cargc, char **cargv); | |
35 | void controlconnect(void *arg); | |
36 | int controlwhois(void *sender, int cargc, char **cargv); | |
37 | int relink(void *sender, int cargc, char **cargv); | |
38 | int controlinsmod(void *sender, int cargc, char **cargv); | |
39 | int controlrmmod(void *sender, int cargc, char **cargv); | |
40 | ||
41 | void _init(void) { | |
42 | controlcmds=newcommandtree(); | |
43 | ||
44 | addcommandtotree(controlcmds,"status",10,0,&controlstatus); | |
45 | addcommandtotree(controlcmds,"whois",10,1,&controlwhois); | |
46 | addcommandtotree(controlcmds,"relink",10,1,&relink); | |
47 | addcommandtotree(controlcmds,"insmod",10,1,&controlinsmod); | |
48 | addcommandtotree(controlcmds,"rmmod",10,1,&controlrmmod); | |
49 | scheduleoneshot(time(NULL)+1,&controlconnect,NULL); | |
50 | } | |
51 | ||
52 | void controlconnect(void *arg) { | |
53 | sstring *cnick, *myident, *myhost, *myrealname; | |
54 | ||
55 | cnick=getcopyconfigitem("control","nick","C",NICKLEN); | |
56 | myident=getcopyconfigitem("control","ident","control",NICKLEN); | |
57 | myhost=getcopyconfigitem("control","hostname",myserver->content,HOSTLEN); | |
58 | myrealname=getcopyconfigitem("control","realname","NewServ Control Service",REALLEN); | |
59 | ||
60 | mynick=registerlocaluser(cnick->content,myident->content,myhost->content,myrealname->content,NULL,UMODE_SERVICE|UMODE_DEAF|UMODE_OPER,&handlemessages); | |
61 | ||
62 | freesstring(cnick); | |
63 | freesstring(myident); | |
64 | freesstring(myhost); | |
65 | freesstring(myrealname); | |
66 | } | |
67 | ||
68 | void handlestats(int hooknum, void *arg) { | |
69 | sendmessagetouser(mynick,statsnick,(char *)arg); | |
70 | } | |
71 | ||
72 | int controlstatus(void *sender, int cargc, char **cargv) { | |
73 | statsnick=(nick *)sender; | |
74 | registerhook(HOOK_CORE_STATSREPLY,&handlestats); | |
75 | triggerhook(HOOK_CORE_STATSREQUEST,(void *)6); | |
76 | deregisterhook(HOOK_CORE_STATSREPLY,&handlestats); | |
77 | return CMD_OK; | |
78 | } | |
79 | ||
80 | int controlwhois(void *sender, int cargc, char **cargv) { | |
81 | nick *target; | |
82 | ||
83 | if (cargc<1) { | |
84 | sendmessagetouser(mynick,(nick *)sender,"Usage: whois <user>"); | |
85 | return CMD_ERROR; | |
86 | } | |
87 | ||
88 | if ((target=getnickbynick(cargv[0]))==NULL) { | |
89 | sendmessagetouser(mynick,(nick *)sender,"Sorry, couldn't find that user"); | |
90 | return CMD_ERROR; | |
91 | } | |
92 | ||
93 | sendmessagetouser(mynick,(nick *)sender,"Nick : %s",target->nick); | |
94 | sendmessagetouser(mynick,(nick *)sender,"Numeric : %s",longtonumeric(target->numeric,5)); | |
95 | sendmessagetouser(mynick,(nick *)sender,"User@Host : %s@%s (%d user(s) on this host)",target->ident,target->host->name->content,target->host->clonecount); | |
96 | sendmessagetouser(mynick,(nick *)sender,"IP address: %s",IPtostr(target->ipaddress)); | |
97 | sendmessagetouser(mynick,(nick *)sender,"Realname : %s (%d user(s) have this realname)",target->realname->name->content,target->realname->usercount); | |
98 | if (target->umodes) { | |
99 | sendmessagetouser(mynick,(nick *)sender,"Umode(s) : %s",printflags(target->umodes,umodeflags)); | |
100 | } | |
101 | if (IsAccount(target)) { | |
102 | sendmessagetouser(mynick,(nick *)sender,"Account : %s",target->authname); | |
103 | } | |
104 | ||
105 | return CMD_OK; | |
106 | } | |
107 | ||
108 | int controlinsmod(void *sender, int cargc, char **cargv) { | |
109 | if (cargc<1) { | |
110 | sendmessagetouser(mynick,(nick *)sender,"Usage: insmod <modulename>"); | |
111 | return CMD_ERROR; | |
112 | } | |
113 | ||
114 | switch(insmod(cargv[0])) { | |
115 | case -1: | |
116 | sendmessagetouser(mynick,(nick *)sender,"Unable to load module %s",cargv[0]); | |
117 | return CMD_ERROR; | |
118 | ||
119 | case 1: | |
120 | sendmessagetouser(mynick,(nick *)sender,"Module %s already loaded, or name not valid",cargv[0]); | |
121 | return CMD_ERROR; | |
122 | ||
123 | case 0: | |
124 | sendmessagetouser(mynick,(nick *)sender,"Module %s loaded.",cargv[0]); | |
125 | return CMD_OK; | |
126 | ||
127 | default: | |
128 | sendmessagetouser(mynick,(nick *)sender,"An unknown error occured."); | |
129 | return CMD_ERROR; | |
130 | } | |
131 | } | |
132 | ||
133 | int controlrmmod(void *sender, int cargc, char **cargv) { | |
134 | if (cargc<1) { | |
135 | sendmessagetouser(mynick,(nick *)sender,"Usage: insmod <modulename>"); | |
136 | return CMD_ERROR; | |
137 | } | |
138 | ||
139 | switch(rmmod(cargv[0])) { | |
140 | case 1: | |
141 | sendmessagetouser(mynick,(nick *)sender,"Module %s is not loaded.",cargv[0]); | |
142 | return CMD_ERROR; | |
143 | ||
144 | case 0: | |
145 | sendmessagetouser(mynick,(nick *)sender,"Module %s unloaded.",cargv[0]); | |
146 | return CMD_OK; | |
147 | ||
148 | default: | |
149 | sendmessagetouser(mynick,(nick *)sender,"An unknown error occured."); | |
150 | return CMD_ERROR; | |
151 | } | |
152 | } | |
153 | ||
154 | int relink(void *sender, int cargc, char **cargv) { | |
155 | if (cargc<1) { | |
156 | sendmessagetouser(mynick,(nick *)sender,"You must give a reason."); | |
157 | return CMD_ERROR; | |
158 | } | |
159 | ||
160 | irc_send("%s SQ %s 0 :%s",mynumeric->content,myserver->content,cargv[0]); | |
161 | irc_disconnected(0); | |
162 | ||
163 | return CMD_OK; | |
164 | } | |
165 | ||
166 | void handlemessages(nick *target, int messagetype, void **args) { | |
167 | Command *cmd; | |
168 | char *cargv[20]; | |
169 | int cargc; | |
170 | nick *sender; | |
171 | ||
172 | switch(messagetype) { | |
173 | case LU_PRIVMSG: | |
174 | case LU_SECUREMSG: | |
175 | /* If it's a message, first arg is nick and second is message */ | |
176 | sender=(nick *)args[0]; | |
177 | ||
178 | /* Split the line into params */ | |
179 | cargc=splitline((char *)args[1],cargv,20,0); | |
180 | ||
181 | cmd=findcommandintree(controlcmds,cargv[0],1); | |
182 | if (cmd==NULL) { | |
183 | sendmessagetouser(mynick,sender,"Unknown command."); | |
184 | return; | |
185 | } | |
186 | ||
187 | if (cmd->level>=10 && !IsOper(sender)) { | |
188 | sendmessagetouser(mynick,sender,"You need to be opered to use this command."); | |
189 | return; | |
190 | } | |
191 | ||
192 | /* If we were doing "authed user tracking" here we'd put a check in for authlevel */ | |
193 | ||
194 | /* Check the maxargs */ | |
195 | if (cmd->maxparams<(cargc-1)) { | |
196 | /* We need to do some rejoining */ | |
197 | rejoinline(cargv[cmd->maxparams],cargc-(cmd->maxparams)); | |
198 | cargc=(cmd->maxparams)+1; | |
199 | } | |
200 | ||
201 | (cmd->handler)((void *)sender,cargc-1,&(cargv[1])); | |
202 | break; | |
203 | ||
204 | case LU_KILLED: | |
205 | /* someone killed me? Bastards */ | |
206 | scheduleoneshot(time(NULL)+1,&controlconnect,NULL); | |
207 | break; | |
208 | ||
209 | default: | |
210 | break; | |
211 | } | |
212 | } |