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"
23 #include "../lib/irc_string.h"
36 CommandTree
*controlcmds
;
37 ControlMsg controlreply
;
38 ControlWall controlwall
;
40 void handlemessages(nick
*target
, int messagetype
, void **args
);
41 int controlstatus(void *sender
, int cargc
, char **cargv
);
42 void controlconnect(void *arg
);
43 int controlwhois(void *sender
, int cargc
, char **cargv
);
44 int controlchannel(void *sender
, int cargc
, char **cargv
);
45 int relink(void *sender
, int cargc
, char **cargv
);
46 int die(void *sender
, int cargc
, char **cargv
);
47 int controlinsmod(void *sender
, int cargc
, char **cargv
);
48 int controllsmod(void *sender
, int cargc
, char **cargv
);
49 int controlrehash(void *sender
, int cargc
, char **cargv
);
50 int controlreload(void *sender
, int cargc
, char **cargv
);
51 int controlhelpcmd(void *sender
, int cargc
, char **cargv
);
52 void controlnoticeopers(flag_t permissionlevel
, flag_t noticelevel
, char *format
, ...);
53 void handlesignals(int hooknum
, void *arg
);
56 controlcmds
=newcommandtree();
57 controlreply
=&controlmessage
;
58 controlwall
=&controlnoticeopers
;
60 registercontrolhelpcmd("status",NO_DEVELOPER
,1,&controlstatus
,"Usage: status ?level?\nDisplays status information, increasing level gives more verbose information.");
61 registercontrolhelpcmd("whois",NO_OPERED
,1,&controlwhois
,"Usage: whois <nickname|#numeric>\nDisplays lots of information about the specified nickname or numeric.");
62 registercontrolhelpcmd("channel",NO_OPER
,1,&controlchannel
,"Usage: channel <#channel>\nDisplays channel information.");
63 registercontrolhelpcmd("relink",NO_DEVELOPER
,1,&relink
,"Usage: relink\nRelinks service to the network.");
64 registercontrolhelpcmd("die",NO_DEVELOPER
,1,&die
,"Usage: die <reason>\nTerminates the service.");
65 registercontrolhelpcmd("insmod",NO_DEVELOPER
,1,&controlinsmod
,"Usage: insmod <module>\nAdds a module to the running instance.");
66 registercontrolhelpcmd("rmmod",NO_DEVELOPER
,1,&controlrmmod
,"Usage: rmmod <module>\nRemoves a module from the running instance.");
67 registercontrolhelpcmd("lsmod",NO_OPER
,0,&controllsmod
,"Usage: lsmod\nLists currently running modules.");
68 registercontrolhelpcmd("rehash",NO_DEVELOPER
,1,&controlrehash
,"Usage: rehash\nReloads configuration file.");
69 registercontrolhelpcmd("showcommands",NO_ACCOUNT
,0,&controlshowcommands
,"Usage: showcommands\nShows all registered commands.");
70 registercontrolhelpcmd("reload",NO_DEVELOPER
,1,&controlreload
,"Usage: reload <module>\nReloads specified module.");
71 registercontrolhelpcmd("help",NO_ANYONE
,1,&controlhelpcmd
,"Usage: help <command>\nShows help for specified command.");
73 registerhook(HOOK_CORE_REHASH
, &handlesignals
);
74 registerhook(HOOK_CORE_SIGINT
, &handlesignals
);
75 scheduleoneshot(time(NULL
)+1,&controlconnect
,NULL
);
79 deleteallschedules(&controlconnect
);
81 deregisterlocaluser(mynick
,"Leaving");
84 deregistercontrolcmd("status",&controlstatus
);
85 deregistercontrolcmd("whois",&controlwhois
);
86 deregistercontrolcmd("channel",&controlchannel
);
87 deregistercontrolcmd("relink",&relink
);
88 deregistercontrolcmd("die",&die
);
89 deregistercontrolcmd("insmod",&controlinsmod
);
90 deregistercontrolcmd("rmmod",&controlrmmod
);
91 deregistercontrolcmd("lsmod",&controllsmod
);
92 deregistercontrolcmd("rehash",&controlrehash
);
93 deregistercontrolcmd("showcommands",&controlshowcommands
);
94 deregistercontrolcmd("reload",&controlreload
);
95 deregistercontrolcmd("help",&controlhelpcmd
);
97 destroycommandtree(controlcmds
);
99 deregisterhook(HOOK_CORE_REHASH
, &handlesignals
);
100 deregisterhook(HOOK_CORE_SIGINT
, &handlesignals
);
103 void registercontrolhelpcmd(const char *name
, int level
, int maxparams
, CommandHandler handler
, char *help
) {
104 addcommandhelptotree(controlcmds
,name
,level
,maxparams
,handler
,help
);
107 int deregistercontrolcmd(const char *name
, CommandHandler handler
) {
108 return deletecommandfromtree(controlcmds
, name
, handler
);
111 void controlconnect(void *arg
) {
112 sstring
*cnick
, *myident
, *myhost
, *myrealname
, *myauthname
;
115 cnick
=getcopyconfigitem("control","nick","C",NICKLEN
);
116 myident
=getcopyconfigitem("control","ident","control",NICKLEN
);
117 myhost
=getcopyconfigitem("control","hostname",myserver
->content
,HOSTLEN
);
118 myrealname
=getcopyconfigitem("control","realname","NewServ Control Service",REALLEN
);
119 myauthname
=getcopyconfigitem("control","authname","C",ACCOUNTLEN
);
121 mynick
=registerlocaluser(cnick
->content
,myident
->content
,myhost
->content
,myrealname
->content
,myauthname
->content
,UMODE_SERVICE
|UMODE_DEAF
|UMODE_OPER
|UMODE_ACCOUNT
|UMODE_INV
,&handlemessages
);
122 triggerhook(HOOK_CONTROL_REGISTERED
, mynick
);
123 cp
=findchannel("#twilightzone");
125 localcreatechannel(mynick
,"#twilightzone");
127 localjoinchannel(mynick
,cp
);
128 localgetops(mynick
,cp
);
132 freesstring(myident
);
134 freesstring(myrealname
);
135 freesstring(myauthname
);
138 void handlestats(int hooknum
, void *arg
) {
139 controlreply(hooknick
,"%s",(char *)arg
);
142 int controlstatus(void *sender
, int cargc
, char **cargv
) {
143 unsigned long level
=999;
144 hooknick
=(nick
*)sender
;
147 level
=strtoul(cargv
[0],NULL
,10);
150 registerhook(HOOK_CORE_STATSREPLY
,&handlestats
);
152 triggerhook(HOOK_CORE_STATSREQUEST
,(void *)level
);
153 deregisterhook(HOOK_CORE_STATSREPLY
,&handlestats
);
157 int controlrehash(void *sender
, int cargc
, char **cargv
) {
158 nick
*np
=(nick
*)sender
;
160 controlreply(np
,"Rehashing the config file.");
163 triggerhook(HOOK_CORE_REHASH
,(void *)0);
167 void handlewhois(int hooknum
, void *arg
) {
168 controlreply(hooknick
,"%s",(char *)arg
);
171 int controlwhois(void *sender
, int cargc
, char **cargv
) {
180 if (cargv
[0][0]=='#') {
181 if (!(target
=getnickbynumericstr(cargv
[0]+1))) {
182 controlreply(sender
,"Sorry, couldn't find numeric %s",cargv
[0]+1);
186 if ((target
=getnickbynick(cargv
[0]))==NULL
) {
187 controlreply((nick
*)sender
,"Sorry, couldn't find that user");
192 controlreply((nick
*)sender
,"Nick : %s",target
->nick
);
193 controlreply((nick
*)sender
,"Numeric : %s",longtonumeric(target
->numeric
,5));
194 controlreply((nick
*)sender
,"User@Host : %s@%s (%d user(s) on this host)",target
->ident
,target
->host
->name
->content
,target
->host
->clonecount
);
195 if (IsSetHost(target
)) {
196 if (target
->shident
) {
197 controlreply((nick
*)sender
,"Fakehost : %s@%s",target
->shident
->content
, target
->sethost
->content
);
199 controlreply((nick
*)sender
,"Fakehost : %s",target
->sethost
->content
);
202 controlreply((nick
*)sender
,"Timestamp : %lu",target
->timestamp
);
203 controlreply((nick
*)sender
,"IP address: %s",IPtostr(target
->p_ipaddr
));
204 controlreply((nick
*)sender
,"Realname : %s (%d user(s) have this realname)",target
->realname
->name
->content
,target
->realname
->usercount
);
205 if (target
->umodes
) {
206 controlreply((nick
*)sender
,"Umode(s) : %s",printflags(target
->umodes
,umodeflags
));
208 if (IsOper(target
) && target
->opername
)
209 controlreply((nick
*)sender
,"Opered as : %s",target
->opername
->content
);
210 if (IsAccount(target
)) {
211 controlreply((nick
*)sender
,"Account : %s",target
->authname
);
212 if (target
->accountts
)
213 controlreply((nick
*)sender
,"AccountTS : %ld",target
->accountts
);
215 controlreply((nick
*)sender
,"UserID : %ld",target
->auth
->userid
);
216 if (target
->auth
->flags
)
217 controlreply((nick
*)sender
,"AccFlags : %s",printflags(target
->auth
->flags
,accountflags
));
221 hooknick
=(nick
*)sender
;
222 registerhook(HOOK_CONTROL_WHOISREPLY
,&handlewhois
);
223 triggerhook(HOOK_CONTROL_WHOISREQUEST
,target
);
224 deregisterhook(HOOK_CONTROL_WHOISREPLY
,&handlewhois
);
226 if (target
->channels
->cursi
==0) {
227 controlreply((nick
*)sender
,"Channels : none");
228 } else if (target
->channels
->cursi
>50) {
229 controlreply((nick
*)sender
,"Channels : - (total: %d)",target
->channels
->cursi
);
232 channels
=(channel
**)target
->channels
->content
;
233 for (i
=0;i
<=target
->channels
->cursi
;i
++) {
234 if (!((i
==target
->channels
->cursi
) || ((70-strlen(buf
))<channels
[i
]->index
->name
->length
&& strlen(buf
)>0))) {
235 strcat(buf
,channels
[i
]->index
->name
->content
);
238 if (strlen(buf
)==0) {
241 controlreply((nick
*)sender
,"Channels : %s",buf
);
252 int controlinsmod(void *sender
, int cargc
, char **cargv
) {
256 switch(insmod(cargv
[0])) {
258 controlreply((nick
*)sender
,"Unable to load module %s",cargv
[0]);
262 controlreply((nick
*)sender
,"Module %s already loaded, or name not valid",cargv
[0]);
266 controlreply((nick
*)sender
,"Module %s loaded.",cargv
[0]);
270 controlreply((nick
*)sender
,"An unknown error occured.");
275 int controlrmmod(void *sender
, int cargc
, char **cargv
) {
279 switch(rmmod(cargv
[0])) {
281 controlreply((nick
*)sender
,"Module %s is not loaded.",cargv
[0]);
285 controlreply((nick
*)sender
,"Module %s unloaded.",cargv
[0]);
289 controlreply((nick
*)sender
,"An unknown error occured.");
294 int controllsmod(void *sender
, int cargc
, char **cargv
) {
298 if (cargc
< 1) { /* list all loaded modules */
299 const char *ver
, *buildid
;
300 time_t t
, t2
= time(NULL
);
301 ptr
= lsmod(i
, &ver
, &buildid
, &t
);
303 /* 9999d 24h 59m 59s fbf2a4a69ee1-tip */
304 controlreply((nick
*)sender
,"Module Loaded for Version Build id");
305 while (ptr
!= NULL
) {
306 controlreply((nick
*)sender
," %-40s %-17s %-30s %s", ptr
, longtoduration(t2
-t
, 2), ver
?ver
:"", buildid
?buildid
:"");
307 ptr
= lsmod(++i
, &ver
, &buildid
, &t
);
310 ptr
= lsmod(getindex(cargv
[0]), NULL
, NULL
, NULL
);
311 controlreply((nick
*)sender
,"Module \"%s\" %s", cargv
[0], (ptr
? "is loaded." : "is NOT loaded."));
316 int controlreload(void *sender
, int cargc
, char **cargv
) {
320 controlreply((nick
*)sender
,"Imma gonna try and reload %s",cargv
[0]);
322 safereload(cargv
[0]);
327 int relink(void *sender
, int cargc
, char **cargv
) {
329 controlreply((nick
*)sender
,"You must give a reason.");
333 irc_send("%s SQ %s 0 :%s",mynumeric
->content
,myserver
->content
,cargv
[0]);
339 int die(void *sender
, int cargc
, char **cargv
) {
340 if (cargc
<1 || (strlen(cargv
[0]) < 10)) {
341 controlreply((nick
*)sender
,"You must give a reason.");
345 controlwall(NO_OPER
,NL_OPERATIONS
,"DIE from %s: %s",((nick
*)sender
)->nick
, cargv
[0]);
347 newserv_shutdown_pending
=1;
352 int controlchannel(void *sender
, int cargc
, char **cargv
) {
358 int i
,j
, ops
=0, voice
=0;
364 if ((cp
=findchannel(cargv
[0]))==NULL
) {
365 controlreply((nick
*)sender
,"Couldn't find channel: %s",cargv
[0]);
370 sprintf(buf2
,"%d",cp
->limit
);
373 controlreply((nick
*)sender
,"Channel : %s",cp
->index
->name
->content
);
374 strftime(timebuf
, 30, "%d/%m/%y %H:%M", localtime(&(cp
->timestamp
)));
375 controlreply((nick
*)sender
,"C-time : %ld [%s]",cp
->timestamp
,timebuf
);
377 controlreply((nick
*)sender
,"Topic : %s",cp
->topic
->content
);
378 strftime(timebuf
, 30, "%d/%m/%y %H:%M", localtime(&(cp
->topictime
)));
379 controlreply((nick
*)sender
,"T-time : %ld [%s]",cp
->topictime
,timebuf
);
381 controlreply((nick
*)sender
,"Topic : (none)");
383 controlreply((nick
*)sender
,"Mode(s) : %s %s%s%s",printflags(cp
->flags
,cmodeflags
),IsLimit(cp
)?buf2
:"",
384 IsLimit(cp
)?" ":"",IsKey(cp
)?cp
->key
->content
:"");
385 controlreply((nick
*)sender
,"Users : %d (hash size %d, utilisation %.1f%%); %d unique hosts",
386 cp
->users
->totalusers
,cp
->users
->hashsize
,((float)(100*cp
->users
->totalusers
)/cp
->users
->hashsize
),
387 countuniquehosts(cp
));
391 for (j
=0;j
<=cp
->users
->hashsize
;j
++) {
392 if (i
==4 || j
==cp
->users
->hashsize
) {
394 controlreply((nick
*)sender
,"Users : %s",buf
);
398 if (j
==cp
->users
->hashsize
)
401 if (cp
->users
->content
[j
]!=nouser
) {
402 if (cp
->users
->content
[j
]&CUMODE_OP
)
404 else if (cp
->users
->content
[j
]&CUMODE_VOICE
)
406 np
=getnickbynumeric(cp
->users
->content
[j
]);
407 sprintf(&buf
[i
*18],"%c%c%-15s ",cp
->users
->content
[j
]&CUMODE_VOICE
?'+':' ',
408 cp
->users
->content
[j
]&CUMODE_OP
?'@':' ', np
?np
->nick
:"!BUG-NONICK!");
414 controlreply((nick
*)sender
, "Users : Opped: %d, Voiced: %d", ops
,voice
);
415 for (cbp
=cp
->bans
;cbp
;cbp
=cbp
->next
) {
416 controlreply((nick
*)sender
,"Ban : %s",bantostringdebug(cbp
));
421 int controlshowcommands(void *sender
, int cargc
, char **cargv
) {
422 nick
*np
=(nick
*)sender
;
423 Command
*cmdlist
[100];
426 n
=getcommandlist(controlcmds
,cmdlist
,100);
428 controlreply(np
,"The following commands are registered at present:");
431 controlreply(np
,"%s",cmdlist
[i
]->command
->content
);
434 controlreply(np
,"End of list.");
438 void handlemessages(nick
*target
, int messagetype
, void **args
) {
444 switch(messagetype
) {
447 /* If it's a message, first arg is nick and second is message */
448 sender
=(nick
*)args
[0];
450 Error("control",ERR_INFO
,"From: %s!%s@%s: %s",sender
->nick
,sender
->ident
,sender
->host
->name
->content
, (char *)args
[1]);
452 /* Split the line into params */
453 cargc
=splitline((char *)args
[1],cargv
,50,0);
460 cmd
=findcommandintree(controlcmds
,cargv
[0],1);
462 controlreply(sender
,"Unknown command.");
466 if (cmd
->level
>0 && !IsOper(sender
)) {
467 controlreply(sender
,"You need to be opered to use this command.");
471 /* If we were doing "authed user tracking" here we'd put a check in for authlevel */
473 /* Check the maxargs */
474 if (cmd
->maxparams
<(cargc
-1)) {
475 /* We need to do some rejoining */
476 rejoinline(cargv
[cmd
->maxparams
],cargc
-(cmd
->maxparams
));
477 cargc
=(cmd
->maxparams
)+1;
480 if((cmd
->handler
)((void *)sender
,cargc
-1,&(cargv
[1])) == CMD_USAGE
)
481 controlhelp(sender
, cmd
);
485 /* someone killed me? Bastards */
486 scheduleoneshot(time(NULL
)+1,&controlconnect
,NULL
);
488 triggerhook(HOOK_CONTROL_REGISTERED
, NULL
);
496 void controlmessage(nick
*target
, char *message
, ... ) {
504 va_start(va
,message
);
505 vsnprintf(buf
,512,message
,va
);
508 sendmessagetouser(mynick
,target
,"%s",buf
);
511 void controlchanmsg(channel
*cp
, char *message
, ...) {
519 va_start(va
,message
);
520 vsnprintf(buf
,512,message
,va
);
523 sendmessagetochannel(mynick
,cp
,"%s",buf
);
526 void controlnotice(nick
*target
, char *message
, ... ) {
534 va_start(va
,message
);
535 vsnprintf(buf
,512,message
,va
);
538 sendnoticetouser(mynick
,target
,"%s",buf
);
541 void controlspecialrmmod(void *arg
) {
542 struct specialsched
*a
= (struct specialsched
*)arg
;
543 sstring
*froo
= a
->modulename
;
547 rmmod(froo
->content
);
551 void controlspecialreloadmod(void *arg
) {
552 struct specialsched
*a
= (struct specialsched
*)arg
;
553 sstring
*froo
= a
->modulename
;
557 safereload(froo
->content
);
561 void controlhelp(nick
*np
, Command
*cmd
) {
562 sstring
*scp
= cmd
->help
;
564 controlreply(np
, "Sorry, no help for this command.");
566 char *cp
= scp
->content
, *sp
= cp
;
569 if(*cp
== '\0' || *cp
== '\n') {
577 controlreply(np
, "%s", sp
);
590 int controlhelpcmd(void *sender
, int cargc
, char **cargv
) {
592 nick
*np
= (nick
*)sender
;
597 cmd
=findcommandintree(controlcmds
,cargv
[0],1);
599 controlreply(np
,"Unknown command.");
603 controlhelp(np
, cmd
);
607 void controlnoticeopers(flag_t permissionlevel
, flag_t noticelevel
, char *format
, ...) {
613 va_start(va
, format
);
614 vsnprintf(broadcast
, sizeof(broadcast
), format
, va
);
617 for(i
=0;i
<NICKHASHSIZE
;i
++)
618 for(np
=nicktable
[i
];np
;np
=np
->next
)
620 controlnotice(np
, "%s", broadcast
);
623 void controlnswall(int noticelevel
, char *format
, ...) {
627 va_start(va
, format
);
628 vsnprintf(broadcast
, sizeof(broadcast
), format
, va
);
631 controlwall(NO_OPER
, noticelevel
, "%s", broadcast
);
634 void handlesignals(int hooknum
, void *arg
) {
635 char *signal
, *action
;
637 if(hooknum
== HOOK_CORE_SIGINT
) {
639 action
= "terminating";
641 long hupped
= (long)arg
;
646 action
= "rehashing";
649 controlwall(NO_OPER
, NL_OPERATIONS
, "SIG%s received, %s...", signal
, action
);