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
;
39 ControlPermitted controlpermitted
;
40 DestroyExt controldestroyext
;
42 void controldestroycmdext(void *ext
);
43 void handlemessages(nick
*target
, int messagetype
, void **args
);
44 int controlstatus(void *sender
, int cargc
, char **cargv
);
45 void controlconnect(void *arg
);
46 int controlwhois(void *sender
, int cargc
, char **cargv
);
47 int controlchannel(void *sender
, int cargc
, char **cargv
);
48 int relink(void *sender
, int cargc
, char **cargv
);
49 int die(void *sender
, int cargc
, char **cargv
);
50 int controlinsmod(void *sender
, int cargc
, char **cargv
);
51 int controllsmod(void *sender
, int cargc
, char **cargv
);
52 int controlrehash(void *sender
, int cargc
, char **cargv
);
53 int controlreload(void *sender
, int cargc
, char **cargv
);
54 int controlhelpcmd(void *sender
, int cargc
, char **cargv
);
55 void controlnoticeopers(flag_t permissionlevel
, flag_t noticelevel
, char *format
, ...) __attribute__ ((format (printf
, 3, 4)));
56 void controlnoticeopers(flag_t permissionlevel
, flag_t noticelevel
, char *format
, ...);
57 int controlcheckpermitted(flag_t level
, nick
*user
);
58 void handlesignal(int hooknum
, void *arg
);
61 controlcmds
=newcommandtree();
62 controlreply
=&controlmessage
;
63 controlwall
=&controlnoticeopers
;
64 controlpermitted
=&controlcheckpermitted
;
65 controldestroyext
=&controldestroycmdext
;
67 registercontrolhelpcmd("status",NO_DEVELOPER
,1,&controlstatus
,"Usage: status ?level?\nDisplays status information, increasing level gives more verbose information.");
68 registercontrolhelpcmd("whois",NO_OPERED
,1,&controlwhois
,"Usage: whois <nickname|#numeric>\nDisplays lots of information about the specified nickname or numeric.");
69 registercontrolhelpcmd("channel",NO_OPER
,1,&controlchannel
,"Usage: channel <#channel>\nDisplays channel information.");
70 registercontrolhelpcmd("relink",NO_DEVELOPER
,1,&relink
,"Usage: relink\nRelinks service to the network.");
71 registercontrolhelpcmd("die",NO_DEVELOPER
,1,&die
,"Usage: die <reason>\nTerminates the service.");
72 registercontrolhelpcmd("insmod",NO_DEVELOPER
,1,&controlinsmod
,"Usage: insmod <module>\nAdds a module to the running instance.");
73 registercontrolhelpcmd("rmmod",NO_DEVELOPER
,1,&controlrmmod
,"Usage: rmmod <module>\nRemoves a module from the running instance.");
74 registercontrolhelpcmd("lsmod",NO_OPER
,0,&controllsmod
,"Usage: lsmod\nLists currently running modules.");
75 registercontrolhelpcmd("rehash",NO_DEVELOPER
,1,&controlrehash
,"Usage: rehash\nReloads configuration file.");
76 registercontrolhelpcmd("showcommands",NO_ACCOUNT
,0,&controlshowcommands
,"Usage: showcommands\nShows all registered commands.");
77 registercontrolhelpcmd("reload",NO_DEVELOPER
,1,&controlreload
,"Usage: reload <module>\nReloads specified module.");
78 registercontrolhelpcmd("help",NO_ANYONE
,1,&controlhelpcmd
,"Usage: help <command>\nShows help for specified command.");
80 registerhook(HOOK_CORE_REHASH
, &handlesignal
);
81 registerhook(HOOK_CORE_SIGINT
, &handlesignal
);
82 scheduleoneshot(time(NULL
)+1,&controlconnect
,NULL
);
86 deleteallschedules(&controlconnect
);
88 deregisterlocaluser(mynick
,"Leaving");
91 deregistercontrolcmd("status",&controlstatus
);
92 deregistercontrolcmd("whois",&controlwhois
);
93 deregistercontrolcmd("channel",&controlchannel
);
94 deregistercontrolcmd("relink",&relink
);
95 deregistercontrolcmd("die",&die
);
96 deregistercontrolcmd("insmod",&controlinsmod
);
97 deregistercontrolcmd("rmmod",&controlrmmod
);
98 deregistercontrolcmd("lsmod",&controllsmod
);
99 deregistercontrolcmd("rehash",&controlrehash
);
100 deregistercontrolcmd("showcommands",&controlshowcommands
);
101 deregistercontrolcmd("reload",&controlreload
);
102 deregistercontrolcmd("help",&controlhelpcmd
);
104 destroycommandtree(controlcmds
);
106 deregisterhook(HOOK_CORE_REHASH
, &handlesignal
);
107 deregisterhook(HOOK_CORE_SIGINT
, &handlesignal
);
110 void registercontrolhelpcmd(const char *name
, int level
, int maxparams
, CommandHandler handler
, char *helpstr
) {
114 newcmd
= addcommandtotree(controlcmds
,name
,level
,maxparams
,handler
);
117 /* Allocate a help record */
118 help
=(cmdhelp
*)malloc(sizeof(cmdhelp
));
120 Error("control",ERR_ERROR
,"Malloc failed: registercontrolhelpcmd");
123 memset((void *)help
,0,sizeof(cmdhelp
));
125 /* this isn't an sstring atm, as sstring has a max length (512) */
126 int len
=strlen(helpstr
);
127 help
->helpstr
=(char *)malloc(len
+1);
129 strncpy(help
->helpstr
, helpstr
, len
);
130 help
->helpstr
[len
] = '\0';
132 newcmd
->ext
=(void *)help
;
133 newcmd
->destroyext
=controldestroyext
;
137 void registercontrolhelpfunccmd(const char *name
, int level
, int maxparams
, CommandHandler handler
, CommandHelp helpcmd
) {
141 /* Allocate a help record */
142 help
=(cmdhelp
*)malloc(sizeof(cmdhelp
));
144 Error("control",ERR_ERROR
,"Malloc failed: registercontrolhelpfunccmd");
147 memset((void *)help
,0,sizeof(cmdhelp
));
149 help
->helpcmd
=helpcmd
;
151 newcmd
= addcommandexttotree(controlcmds
,name
,level
,maxparams
,handler
, (void *)help
);
152 newcmd
->destroyext
=controldestroyext
;
156 int deregistercontrolcmd(const char *name
, CommandHandler handler
) {
157 return deletecommandfromtree(controlcmds
, name
, handler
);
160 void controlconnect(void *arg
) {
161 sstring
*cnick
, *myident
, *myhost
, *myrealname
, *myauthname
;
164 cnick
=getcopyconfigitem("control","nick","C",NICKLEN
);
165 myident
=getcopyconfigitem("control","ident","control",NICKLEN
);
166 myhost
=getcopyconfigitem("control","hostname",myserver
->content
,HOSTLEN
);
167 myrealname
=getcopyconfigitem("control","realname","NewServ Control Service",REALLEN
);
168 myauthname
=getcopyconfigitem("control","authname","C",ACCOUNTLEN
);
170 mynick
=registerlocaluser(cnick
->content
,myident
->content
,myhost
->content
,myrealname
->content
,myauthname
->content
,UMODE_SERVICE
|UMODE_DEAF
|UMODE_OPER
|UMODE_ACCOUNT
|UMODE_INV
,&handlemessages
);
171 triggerhook(HOOK_CONTROL_REGISTERED
, mynick
);
172 cp
=findchannel("#twilightzone");
174 localcreatechannel(mynick
,"#twilightzone");
176 localjoinchannel(mynick
,cp
);
177 localgetops(mynick
,cp
);
181 freesstring(myident
);
183 freesstring(myrealname
);
184 freesstring(myauthname
);
187 void handlestats(int hooknum
, void *arg
) {
188 controlreply(hooknick
,"%s",(char *)arg
);
191 int controlstatus(void *sender
, int cargc
, char **cargv
) {
192 unsigned long level
=999;
193 hooknick
=(nick
*)sender
;
196 level
=strtoul(cargv
[0],NULL
,10);
199 registerhook(HOOK_CORE_STATSREPLY
,&handlestats
);
201 triggerhook(HOOK_CORE_STATSREQUEST
,(void *)level
);
202 deregisterhook(HOOK_CORE_STATSREPLY
,&handlestats
);
206 int controlrehash(void *sender
, int cargc
, char **cargv
) {
207 nick
*np
=(nick
*)sender
;
209 controlreply(np
,"Rehashing the config file.");
212 triggerhook(HOOK_CORE_REHASH
,(void *)0);
216 void handlewhois(int hooknum
, void *arg
) {
217 controlreply(hooknick
,"%s",(char *)arg
);
220 int controlwhois(void *sender
, int cargc
, char **cargv
) {
229 if (cargv
[0][0]=='#') {
230 if (!(target
=getnickbynumericstr(cargv
[0]+1))) {
231 controlreply(sender
,"Sorry, couldn't find numeric %s",cargv
[0]+1);
235 if ((target
=getnickbynick(cargv
[0]))==NULL
) {
236 controlreply((nick
*)sender
,"Sorry, couldn't find that user");
241 controlreply((nick
*)sender
,"Nick : %s",target
->nick
);
242 controlreply((nick
*)sender
,"Numeric : %s",longtonumeric(target
->numeric
,5));
243 controlreply((nick
*)sender
,"User@Host : %s@%s (%d user(s) on this host)",target
->ident
,target
->host
->name
->content
,target
->host
->clonecount
);
244 if (IsSetHost(target
)) {
245 if (target
->shident
) {
246 controlreply((nick
*)sender
,"Fakehost : %s@%s",target
->shident
->content
, target
->sethost
->content
);
248 controlreply((nick
*)sender
,"Fakehost : %s",target
->sethost
->content
);
251 controlreply((nick
*)sender
,"Timestamp : %lu",target
->timestamp
);
252 controlreply((nick
*)sender
,"IP address: %s",IPtostr(target
->p_ipaddr
));
253 controlreply((nick
*)sender
,"Realname : %s (%d user(s) have this realname)",target
->realname
->name
->content
,target
->realname
->usercount
);
254 if (target
->umodes
) {
255 controlreply((nick
*)sender
,"Umode(s) : %s",printflags(target
->umodes
,umodeflags
));
257 if (IsOper(target
) && target
->opername
)
258 controlreply((nick
*)sender
,"Opered as : %s",target
->opername
->content
);
259 if (IsAccount(target
)) {
260 controlreply((nick
*)sender
,"Account : %s",target
->authname
);
261 if (target
->accountts
)
262 controlreply((nick
*)sender
,"AccountTS : %ld",target
->accountts
);
264 controlreply((nick
*)sender
,"UserID : %ld",target
->auth
->userid
);
265 if (target
->auth
->flags
)
266 controlreply((nick
*)sender
,"AccFlags : %s",printflags(target
->auth
->flags
,accountflags
));
270 hooknick
=(nick
*)sender
;
271 registerhook(HOOK_CONTROL_WHOISREPLY
,&handlewhois
);
272 triggerhook(HOOK_CONTROL_WHOISREQUEST
,target
);
273 deregisterhook(HOOK_CONTROL_WHOISREPLY
,&handlewhois
);
275 if (target
->channels
->cursi
==0) {
276 controlreply((nick
*)sender
,"Channels : none");
277 } else if (target
->channels
->cursi
>50) {
278 controlreply((nick
*)sender
,"Channels : - (total: %d)",target
->channels
->cursi
);
281 channels
=(channel
**)target
->channels
->content
;
282 for (i
=0;i
<=target
->channels
->cursi
;i
++) {
283 if (!((i
==target
->channels
->cursi
) || ((70-strlen(buf
))<channels
[i
]->index
->name
->length
&& strlen(buf
)>0))) {
284 strcat(buf
,channels
[i
]->index
->name
->content
);
287 if (strlen(buf
)==0) {
290 controlreply((nick
*)sender
,"Channels : %s",buf
);
301 int controlinsmod(void *sender
, int cargc
, char **cargv
) {
305 switch(insmod(cargv
[0])) {
307 controlreply((nick
*)sender
,"Unable to load module %s",cargv
[0]);
311 controlreply((nick
*)sender
,"Module %s already loaded, or name not valid",cargv
[0]);
315 controlreply((nick
*)sender
,"Module %s loaded.",cargv
[0]);
319 controlreply((nick
*)sender
,"An unknown error occured.");
324 int controlrmmod(void *sender
, int cargc
, char **cargv
) {
328 switch(rmmod(cargv
[0])) {
330 controlreply((nick
*)sender
,"Module %s is not loaded.",cargv
[0]);
334 controlreply((nick
*)sender
,"Module %s unloaded.",cargv
[0]);
338 controlreply((nick
*)sender
,"An unknown error occured.");
343 int controllsmod(void *sender
, int cargc
, char **cargv
) {
347 if (cargc
< 1) { /* list all loaded modules */
348 const char *ver
, *buildid
;
349 time_t t
, t2
= time(NULL
);
350 ptr
= lsmod(i
, &ver
, &buildid
, &t
);
352 /* 9999d 24h 59m 59s fbf2a4a69ee1-tip */
353 controlreply((nick
*)sender
,"Module Loaded for Version Build id");
354 while (ptr
!= NULL
) {
355 controlreply((nick
*)sender
," %-40s %-17s %-30s %s", ptr
, longtoduration(t2
-t
, 2), ver
?ver
:"", buildid
?buildid
:"");
356 ptr
= lsmod(++i
, &ver
, &buildid
, &t
);
359 ptr
= lsmod(getindex(cargv
[0]), NULL
, NULL
, NULL
);
360 controlreply((nick
*)sender
,"Module \"%s\" %s", cargv
[0], (ptr
? "is loaded." : "is NOT loaded."));
365 int controlreload(void *sender
, int cargc
, char **cargv
) {
369 controlreply((nick
*)sender
,"Imma gonna try and reload %s",cargv
[0]);
371 safereload(cargv
[0]);
376 int relink(void *sender
, int cargc
, char **cargv
) {
378 controlreply((nick
*)sender
,"You must give a reason.");
382 irc_send("%s SQ %s 0 :%s",mynumeric
->content
,myserver
->content
,cargv
[0]);
388 int die(void *sender
, int cargc
, char **cargv
) {
389 if (cargc
<1 || (strlen(cargv
[0]) < 10)) {
390 controlreply((nick
*)sender
,"You must give a reason.");
394 controlwall(NO_OPER
,NL_OPERATIONS
,"DIE from %s: %s",((nick
*)sender
)->nick
, cargv
[0]);
396 newserv_shutdown_pending
=1;
401 int controlchannel(void *sender
, int cargc
, char **cargv
) {
407 int i
,j
, ops
=0, voice
=0;
413 if ((cp
=findchannel(cargv
[0]))==NULL
) {
414 controlreply((nick
*)sender
,"Couldn't find channel: %s",cargv
[0]);
419 sprintf(buf2
,"%d",cp
->limit
);
422 controlreply((nick
*)sender
,"Channel : %s",cp
->index
->name
->content
);
423 strftime(timebuf
, 30, "%d/%m/%y %H:%M", localtime(&(cp
->timestamp
)));
424 controlreply((nick
*)sender
,"C-time : %ld [%s]",cp
->timestamp
,timebuf
);
426 controlreply((nick
*)sender
,"Topic : %s",cp
->topic
->content
);
427 strftime(timebuf
, 30, "%d/%m/%y %H:%M", localtime(&(cp
->topictime
)));
428 controlreply((nick
*)sender
,"T-time : %ld [%s]",cp
->topictime
,timebuf
);
430 controlreply((nick
*)sender
,"Topic : (none)");
432 controlreply((nick
*)sender
,"Mode(s) : %s %s%s%s",printflags(cp
->flags
,cmodeflags
),IsLimit(cp
)?buf2
:"",
433 IsLimit(cp
)?" ":"",IsKey(cp
)?cp
->key
->content
:"");
434 controlreply((nick
*)sender
,"Users : %d (hash size %d, utilisation %.1f%%); %d unique hosts",
435 cp
->users
->totalusers
,cp
->users
->hashsize
,((float)(100*cp
->users
->totalusers
)/cp
->users
->hashsize
),
436 countuniquehosts(cp
));
440 for (j
=0;j
<=cp
->users
->hashsize
;j
++) {
441 if (i
==4 || j
==cp
->users
->hashsize
) {
443 controlreply((nick
*)sender
,"Users : %s",buf
);
447 if (j
==cp
->users
->hashsize
)
450 if (cp
->users
->content
[j
]!=nouser
) {
451 if (cp
->users
->content
[j
]&CUMODE_OP
)
453 else if (cp
->users
->content
[j
]&CUMODE_VOICE
)
455 np
=getnickbynumeric(cp
->users
->content
[j
]);
456 sprintf(&buf
[i
*18],"%c%c%-15s ",cp
->users
->content
[j
]&CUMODE_VOICE
?'+':' ',
457 cp
->users
->content
[j
]&CUMODE_OP
?'@':' ', np
?np
->nick
:"!BUG-NONICK!");
463 controlreply((nick
*)sender
, "Users : Opped: %d, Voiced: %d", ops
,voice
);
464 for (cbp
=cp
->bans
;cbp
;cbp
=cbp
->next
) {
465 controlreply((nick
*)sender
,"Ban : %s",bantostringdebug(cbp
));
470 int controlshowcommands(void *sender
, int cargc
, char **cargv
) {
471 nick
*np
=(nick
*)sender
;
472 Command
*cmdlist
[100];
475 n
=getcommandlist(controlcmds
,cmdlist
,100);
477 controlreply(np
,"The following commands are registered at present:");
480 controlreply(np
,"%s",cmdlist
[i
]->command
->content
);
483 controlreply(np
,"End of list.");
487 void handlemessages(nick
*target
, int messagetype
, void **args
) {
493 switch(messagetype
) {
496 /* If it's a message, first arg is nick and second is message */
497 sender
=(nick
*)args
[0];
499 Error("control",ERR_INFO
,"From: %s!%s@%s: %s",sender
->nick
,sender
->ident
,sender
->host
->name
->content
, (char *)args
[1]);
501 /* Split the line into params */
502 cargc
=splitline((char *)args
[1],cargv
,50,0);
509 cmd
=findcommandintree(controlcmds
,cargv
[0],1);
511 controlreply(sender
,"Unknown command.");
515 if (cmd
->level
>0 && !IsOper(sender
)) {
516 controlreply(sender
,"You need to be opered to use this command.");
520 /* If we were doing "authed user tracking" here we'd put a check in for authlevel */
522 /* Check the maxargs */
523 if (cmd
->maxparams
<(cargc
-1)) {
524 /* We need to do some rejoining */
525 rejoinline(cargv
[cmd
->maxparams
],cargc
-(cmd
->maxparams
));
526 cargc
=(cmd
->maxparams
)+1;
529 if((cmd
->handler
)((void *)sender
,cargc
-1,&(cargv
[1])) == CMD_USAGE
)
530 controlhelp(sender
, cmd
);
534 /* someone killed me? Bastards */
535 scheduleoneshot(time(NULL
)+1,&controlconnect
,NULL
);
537 triggerhook(HOOK_CONTROL_REGISTERED
, NULL
);
545 void controlmessage(nick
*target
, char *message
, ... ) {
553 va_start(va
,message
);
554 vsnprintf(buf
,512,message
,va
);
557 sendmessagetouser(mynick
,target
,"%s",buf
);
560 void controlchanmsg(channel
*cp
, char *message
, ...) {
568 va_start(va
,message
);
569 vsnprintf(buf
,512,message
,va
);
572 sendmessagetochannel(mynick
,cp
,"%s",buf
);
575 void controlnotice(nick
*target
, char *message
, ... ) {
583 va_start(va
,message
);
584 vsnprintf(buf
,512,message
,va
);
587 sendnoticetouser(mynick
,target
,"%s",buf
);
590 void controlspecialrmmod(void *arg
) {
591 struct specialsched
*a
= (struct specialsched
*)arg
;
592 sstring
*froo
= a
->modulename
;
596 rmmod(froo
->content
);
600 void controlspecialreloadmod(void *arg
) {
601 struct specialsched
*a
= (struct specialsched
*)arg
;
602 sstring
*froo
= a
->modulename
;
606 safereload(froo
->content
);
610 void controlhelp(nick
*np
, Command
*cmd
) {
613 cmdhelp
*help
=(cmdhelp
*)cmd
->ext
;
616 controlreply(np
, "Sorry, no help for this command.");
619 if ( help
->helpcmd
) {
620 (help
->helpcmd
)(np
, cmd
);
624 controlreply(np
, "Sorry, no help for this command.");
630 if(*cp
== '\0' || *cp
== '\n') {
638 controlreply(np
, "%s", sp
);
652 int controlhelpcmd(void *sender
, int cargc
, char **cargv
) {
654 nick
*np
= (nick
*)sender
;
659 cmd
=findcommandintree(controlcmds
,cargv
[0],1);
661 controlreply(np
,"Unknown command.");
665 controlhelp(np
, cmd
);
669 void controlnoticeopers(flag_t permissionlevel
, flag_t noticelevel
, char *format
, ...) {
675 va_start(va
, format
);
676 vsnprintf(broadcast
, sizeof(broadcast
), format
, va
);
679 for(i
=0;i
<NICKHASHSIZE
;i
++)
680 for(np
=nicktable
[i
];np
;np
=np
->next
)
682 controlnotice(np
, "%s", broadcast
);
685 void controlnswall(int noticelevel
, char *format
, ...) {
689 va_start(va
, format
);
690 vsnprintf(broadcast
, sizeof(broadcast
), format
, va
);
693 controlwall(NO_OPER
, noticelevel
, "%s", broadcast
);
696 int controlcheckpermitted(flag_t level
, nick
*user
) {
700 void handlesignal(int hooknum
, void *arg
) {
701 char *signal
, *action
;
703 if(hooknum
== HOOK_CORE_SIGINT
) {
705 action
= "terminating";
707 long hupped
= (long)arg
;
712 action
= "rehashing";
715 controlwall(NO_OPER
, NL_OPERATIONS
, "SIG%s received, %s...", signal
, action
);
718 void controldestroycmdext(void *ext
) {
719 if ( ((cmdhelp
*)ext
)->helpstr
)
720 free( ((cmdhelp
*)ext
)->helpstr
);