2 * This is the first client module for newserv :)
4 * A very simple bot which should give people some ideas for how to
5 * implement stuff on this thing
8 #include "../irc/irc_config.h"
9 #include "../parser/parser.h"
10 #include "../localuser/localuser.h"
11 #include "../localuser/localuserchannel.h"
12 #include "../nick/nick.h"
13 #include "../lib/sstring.h"
14 #include "../core/config.h"
15 #include "../irc/irc.h"
16 #include "../lib/splitline.h"
17 #include "../channel/channel.h"
18 #include "../lib/flags.h"
19 #include "../core/schedule.h"
20 #include "../lib/base64.h"
21 #include "../core/modules.h"
22 #include "../lib/version.h"
35 CommandTree
*controlcmds
;
36 ControlMsg controlreply
;
37 ControlWall controlwall
;
39 void handlemessages(nick
*target
, int messagetype
, void **args
);
40 int controlstatus(void *sender
, int cargc
, char **cargv
);
41 void controlconnect(void *arg
);
42 int controlwhois(void *sender
, int cargc
, char **cargv
);
43 int controlchannel(void *sender
, int cargc
, char **cargv
);
44 int relink(void *sender
, int cargc
, char **cargv
);
45 int die(void *sender
, int cargc
, char **cargv
);
46 int controlinsmod(void *sender
, int cargc
, char **cargv
);
47 int controllsmod(void *sender
, int cargc
, char **cargv
);
48 int controlrehash(void *sender
, int cargc
, char **cargv
);
49 int controlreload(void *sender
, int cargc
, char **cargv
);
50 int controlhelpcmd(void *sender
, int cargc
, char **cargv
);
51 void controlnoticeopers(flag_t permissionlevel
, flag_t noticelevel
, char *format
, ...);
54 controlcmds
=newcommandtree();
55 controlreply
=&controlmessage
;
56 controlwall
=&controlnoticeopers
;
58 registercontrolhelpcmd("status",NO_DEVELOPER
,1,&controlstatus
,"Usage: status ?level?\nDisplays status information, increasing level gives more verbose information.");
59 registercontrolhelpcmd("whois",NO_OPERED
,1,&controlwhois
,"Usage: whois <nickname|#numeric>\nDisplays lots of information about the specified nickname or numeric.");
60 registercontrolhelpcmd("channel",NO_OPER
,1,&controlchannel
,"Usage: channel <#channel>\nDisplays channel information.");
61 registercontrolhelpcmd("relink",NO_DEVELOPER
,1,&relink
,"Usage: relink\nRelinks service to the network.");
62 registercontrolhelpcmd("die",NO_DEVELOPER
,1,&die
,"Usage: die <reason>\nTerminates the service.");
63 registercontrolhelpcmd("insmod",NO_DEVELOPER
,1,&controlinsmod
,"Usage: insmod <module>\nAdds a module to the running instance.");
64 registercontrolhelpcmd("rmmod",NO_DEVELOPER
,1,&controlrmmod
,"Usage: rmmod <module>\nRemoves a module from the running instance.");
65 registercontrolhelpcmd("lsmod",NO_DEVELOPER
,0,&controllsmod
,"Usage: lsmod\nLists currently running modules.");
66 registercontrolhelpcmd("rehash",NO_DEVELOPER
,1,&controlrehash
,"Usage: rehash\nReloads configuration file.");
67 registercontrolhelpcmd("showcommands",NO_ACCOUNT
,0,&controlshowcommands
,"Usage: showcommands\nShows all registered commands.");
68 registercontrolhelpcmd("reload",NO_DEVELOPER
,1,&controlreload
,"Usage: reload <module>\nReloads specified module.");
69 registercontrolhelpcmd("help",NO_ANYONE
,1,&controlhelpcmd
,"Usage: help <command>\nShows help for specified command.");
71 scheduleoneshot(time(NULL
)+1,&controlconnect
,NULL
);
74 void registercontrolhelpcmd(const char *name
, int level
, int maxparams
, CommandHandler handler
, char *help
) {
75 addcommandhelptotree(controlcmds
,name
,level
,maxparams
,handler
,help
);
78 int deregistercontrolcmd(const char *name
, CommandHandler handler
) {
79 return deletecommandfromtree(controlcmds
, name
, handler
);
82 void controlconnect(void *arg
) {
83 sstring
*cnick
, *myident
, *myhost
, *myrealname
, *myauthname
;
86 cnick
=getcopyconfigitem("control","nick","C",NICKLEN
);
87 myident
=getcopyconfigitem("control","ident","control",NICKLEN
);
88 myhost
=getcopyconfigitem("control","hostname",myserver
->content
,HOSTLEN
);
89 myrealname
=getcopyconfigitem("control","realname","NewServ Control Service",REALLEN
);
90 myauthname
=getcopyconfigitem("control","authname","C",ACCOUNTLEN
);
92 mynick
=registerlocaluser(cnick
->content
,myident
->content
,myhost
->content
,myrealname
->content
,myauthname
->content
,UMODE_SERVICE
|UMODE_DEAF
|UMODE_OPER
|UMODE_ACCOUNT
,&handlemessages
);
93 triggerhook(HOOK_CONTROL_REGISTERED
, mynick
);
94 cp
=findchannel("#twilightzone");
96 localcreatechannel(mynick
,"#twilightzone");
98 localjoinchannel(mynick
,cp
);
99 localgetops(mynick
,cp
);
103 freesstring(myident
);
105 freesstring(myrealname
);
106 freesstring(myauthname
);
109 void handlestats(int hooknum
, void *arg
) {
110 controlreply(hooknick
,"%s",(char *)arg
);
113 int controlstatus(void *sender
, int cargc
, char **cargv
) {
114 unsigned int level
=5;
116 hooknick
=(nick
*)sender
;
119 level
=strtoul(cargv
[0],NULL
,10);
122 registerhook(HOOK_CORE_STATSREPLY
,&handlestats
);
123 triggerhook(HOOK_CORE_STATSREQUEST
,(void *)level
);
124 deregisterhook(HOOK_CORE_STATSREPLY
,&handlestats
);
128 int controlrehash(void *sender
, int cargc
, char **cargv
) {
129 nick
*np
=(nick
*)sender
;
131 controlreply(np
,"Rehashing the config file.");
134 triggerhook(HOOK_CORE_REHASH
,(void *)0);
138 void handlewhois(int hooknum
, void *arg
) {
139 controlreply(hooknick
,"%s",(char *)arg
);
142 int controlwhois(void *sender
, int cargc
, char **cargv
) {
151 if (cargv
[0][0]=='#') {
152 if (!(target
=getnickbynumericstr(cargv
[0]+1))) {
153 controlreply(sender
,"Sorry, couldn't find numeric %s",cargv
[0]+1);
157 if ((target
=getnickbynick(cargv
[0]))==NULL
) {
158 controlreply((nick
*)sender
,"Sorry, couldn't find that user");
163 controlreply((nick
*)sender
,"Nick : %s",target
->nick
);
164 controlreply((nick
*)sender
,"Numeric : %s",longtonumeric(target
->numeric
,5));
165 controlreply((nick
*)sender
,"User@Host : %s@%s (%d user(s) on this host)",target
->ident
,target
->host
->name
->content
,target
->host
->clonecount
);
166 if (IsSetHost(target
)) {
167 if (target
->shident
) {
168 controlreply((nick
*)sender
,"Fakehost : %s@%s",target
->shident
->content
, target
->sethost
->content
);
170 controlreply((nick
*)sender
,"Fakehost : %s",target
->sethost
->content
);
173 controlreply((nick
*)sender
,"Timestamp : %lu",target
->timestamp
);
174 controlreply((nick
*)sender
,"IP address: %s",IPtostr(target
->p_ipaddr
));
175 controlreply((nick
*)sender
,"Realname : %s (%d user(s) have this realname)",target
->realname
->name
->content
,target
->realname
->usercount
);
176 if (target
->umodes
) {
177 controlreply((nick
*)sender
,"Umode(s) : %s",printflags(target
->umodes
,umodeflags
));
179 if (IsAccount(target
)) {
180 controlreply((nick
*)sender
,"Account : %s",target
->authname
);
181 if (target
->accountts
)
182 controlreply((nick
*)sender
,"AccountTS : %ld",target
->accountts
);
185 hooknick
=(nick
*)sender
;
186 registerhook(HOOK_CONTROL_WHOISREPLY
,&handlewhois
);
187 triggerhook(HOOK_CONTROL_WHOISREQUEST
,target
);
188 deregisterhook(HOOK_CONTROL_WHOISREPLY
,&handlewhois
);
190 if (target
->channels
->cursi
==0) {
191 controlreply((nick
*)sender
,"Channels : none");
192 } else if (target
->channels
->cursi
>50) {
193 controlreply((nick
*)sender
,"Channels : - (total: %d)",target
->channels
->cursi
);
196 channels
=(channel
**)target
->channels
->content
;
197 for (i
=0;i
<=target
->channels
->cursi
;i
++) {
198 if (!((i
==target
->channels
->cursi
) || ((70-strlen(buf
))<channels
[i
]->index
->name
->length
&& strlen(buf
)>0))) {
199 strcat(buf
,channels
[i
]->index
->name
->content
);
202 if (strlen(buf
)==0) {
205 controlreply((nick
*)sender
,"Channels : %s",buf
);
216 int controlinsmod(void *sender
, int cargc
, char **cargv
) {
220 switch(insmod(cargv
[0])) {
222 controlreply((nick
*)sender
,"Unable to load module %s",cargv
[0]);
226 controlreply((nick
*)sender
,"Module %s already loaded, or name not valid",cargv
[0]);
230 controlreply((nick
*)sender
,"Module %s loaded.",cargv
[0]);
234 controlreply((nick
*)sender
,"An unknown error occured.");
239 int controlrmmod(void *sender
, int cargc
, char **cargv
) {
243 switch(rmmod(cargv
[0])) {
245 controlreply((nick
*)sender
,"Module %s is not loaded.",cargv
[0]);
249 controlreply((nick
*)sender
,"Module %s unloaded.",cargv
[0]);
253 controlreply((nick
*)sender
,"An unknown error occured.");
258 int controllsmod(void *sender
, int cargc
, char **cargv
) {
262 if (cargc
< 1) { /* list all loaded modules */
264 controlreply((nick
*)sender
,"Module");
265 while (ptr
!= NULL
) {
266 const char *ver
= lsmodver(i
);
267 controlreply((nick
*)sender
,"%s%s%s%s", ptr
, ver
?" (":"", ver
?ver
:"", ver
?")":"");
271 ptr
= lsmod(getindex(cargv
[0]));
272 controlreply((nick
*)sender
,"Module \"%s\" %s", cargv
[0], (ptr
? "is loaded." : "is NOT loaded."));
277 int controlreload(void *sender
, int cargc
, char **cargv
) {
281 controlrmmod(sender
, cargc
, cargv
);
282 return controlinsmod(sender
, cargc
, cargv
);
285 int relink(void *sender
, int cargc
, char **cargv
) {
287 controlreply((nick
*)sender
,"You must give a reason.");
291 irc_send("%s SQ %s 0 :%s",mynumeric
->content
,myserver
->content
,cargv
[0]);
297 int die(void *sender
, int cargc
, char **cargv
) {
299 controlreply((nick
*)sender
,"You must give a reason.");
303 irc_send("%s SQ %s 0 :%s",mynumeric
->content
,myserver
->content
,cargv
[0]);
309 int controlchannel(void *sender
, int cargc
, char **cargv
) {
320 if ((cp
=findchannel(cargv
[0]))==NULL
) {
321 controlreply((nick
*)sender
,"Couldn't find channel: %s",cargv
[0]);
326 sprintf(buf2
,"%d",cp
->limit
);
329 controlreply((nick
*)sender
,"Channel : %s",cp
->index
->name
->content
);
331 controlreply((nick
*)sender
,"Topic : %s",cp
->topic
->content
);
332 controlreply((nick
*)sender
,"T-time : %ld [%s]",cp
->topictime
,ctime(&(cp
->topictime
)));
334 controlreply((nick
*)sender
,"Mode(s) : %s %s%s%s",printflags(cp
->flags
,cmodeflags
),IsLimit(cp
)?buf2
:"",
335 IsLimit(cp
)?" ":"",IsKey(cp
)?cp
->key
->content
:"");
336 controlreply((nick
*)sender
,"Users : %d (hash size %d, utilisation %.1f%%); %d unique hosts",
337 cp
->users
->totalusers
,cp
->users
->hashsize
,((float)(100*cp
->users
->totalusers
)/cp
->users
->hashsize
),
338 countuniquehosts(cp
));
342 for (j
=0;j
<=cp
->users
->hashsize
;j
++) {
343 if (i
==4 || j
==cp
->users
->hashsize
) {
345 controlreply((nick
*)sender
,"Users : %s",buf
);
349 if (j
==cp
->users
->hashsize
)
352 if (cp
->users
->content
[j
]!=nouser
) {
353 np
=getnickbynumeric(cp
->users
->content
[j
]);
354 sprintf(&buf
[i
*18],"%c%c%-15s ",cp
->users
->content
[j
]&CUMODE_VOICE
?'+':' ',
355 cp
->users
->content
[j
]&CUMODE_OP
?'@':' ', np
?np
->nick
:"!BUG-NONICK!");
362 for (cbp
=cp
->bans
;cbp
;cbp
=cbp
->next
) {
363 controlreply((nick
*)sender
,"Ban : %s",bantostringdebug(cbp
));
368 int controlshowcommands(void *sender
, int cargc
, char **cargv
) {
369 nick
*np
=(nick
*)sender
;
370 Command
*cmdlist
[100];
373 n
=getcommandlist(controlcmds
,cmdlist
,100);
375 controlreply(np
,"The following commands are registered at present:");
378 controlreply(np
,"%s",cmdlist
[i
]->command
->content
);
381 controlreply(np
,"End of list.");
385 void handlemessages(nick
*target
, int messagetype
, void **args
) {
391 switch(messagetype
) {
394 /* If it's a message, first arg is nick and second is message */
395 sender
=(nick
*)args
[0];
397 Error("control",ERR_INFO
,"From: %s!%s@%s: %s",sender
->nick
,sender
->ident
,sender
->host
->name
->content
, (char *)args
[1]);
399 /* Split the line into params */
400 cargc
=splitline((char *)args
[1],cargv
,50,0);
407 cmd
=findcommandintree(controlcmds
,cargv
[0],1);
409 controlreply(sender
,"Unknown command.");
413 if (cmd
->level
>0 && !IsOper(sender
)) {
414 controlreply(sender
,"You need to be opered to use this command.");
418 /* If we were doing "authed user tracking" here we'd put a check in for authlevel */
420 /* Check the maxargs */
421 if (cmd
->maxparams
<(cargc
-1)) {
422 /* We need to do some rejoining */
423 rejoinline(cargv
[cmd
->maxparams
],cargc
-(cmd
->maxparams
));
424 cargc
=(cmd
->maxparams
)+1;
427 if((cmd
->handler
)((void *)sender
,cargc
-1,&(cargv
[1])) == CMD_USAGE
)
428 controlhelp(sender
, cmd
);
432 /* someone killed me? Bastards */
433 scheduleoneshot(time(NULL
)+1,&controlconnect
,NULL
);
442 void controlmessage(nick
*target
, char *message
, ... ) {
450 va_start(va
,message
);
451 vsnprintf(buf
,512,message
,va
);
454 sendmessagetouser(mynick
,target
,"%s",buf
);
457 void controlchanmsg(channel
*cp
, char *message
, ...) {
465 va_start(va
,message
);
466 vsnprintf(buf
,512,message
,va
);
469 sendmessagetochannel(mynick
,cp
,"%s",buf
);
472 void controlnotice(nick
*target
, char *message
, ... ) {
480 va_start(va
,message
);
481 vsnprintf(buf
,512,message
,va
);
484 sendnoticetouser(mynick
,target
,"%s",buf
);
487 void controlspecialrmmod(void *arg
) {
488 struct specialsched
*a
= (struct specialsched
*)arg
;
491 sstring
*froo
= a
->modulename
;
493 rmmod(froo
->content
);
497 void controlspecialreloadmod(void *arg
) {
498 struct specialsched
*a
= (struct specialsched
*)arg
;
501 sstring
*froo
= a
->modulename
;
503 rmmod(froo
->content
);
504 insmod(froo
->content
);
508 void controlhelp(nick
*np
, Command
*cmd
) {
509 char *cp
= cmd
->help
, *sp
= cp
;
511 controlreply(np
, "Sorry, no help for this command.");
515 if(*cp
== '\0' || *cp
== '\n') {
523 controlreply(np
, "%s", sp
);
536 int controlhelpcmd(void *sender
, int cargc
, char **cargv
) {
538 nick
*np
= (nick
*)sender
;
543 cmd
=findcommandintree(controlcmds
,cargv
[0],1);
545 controlreply(np
,"Unknown command.");
549 controlhelp(np
, cmd
);
553 void controlnoticeopers(flag_t permissionlevel
, flag_t noticelevel
, char *format
, ...) {
559 va_start(va
, format
);
560 vsnprintf(broadcast
, sizeof(broadcast
), format
, va
);
563 for(i
=0;i
<NICKHASHSIZE
;i
++)
564 for(np
=nicktable
[i
];np
;np
=np
->next
)
566 controlnotice(np
, "%s", broadcast
);