/* ShipWars Plugin Sample Source To compile this plugin, type: cc command.c -o command -shared -g --- This example shows how to write (two) callbacks to handle help responses and server command. char **swplugin_help_commands_list( int *ncommands, SWServContext *in ) int swplugin_command( const char *cmd, const char *arg, int con_num, int obj_num, int uid, int gid, SWServContext *in ) Note that the above functions will be automatically detected by the server when this plugin is loaded. The names of the functions must be given exactly as shown above. The swplugin_help_commands_list() must return an array of strings each time it is called, where each string and the pointer array will not be deallocated. The ncommands must be updated to reflect the total number of command strings returned. Note that none of the strings returned will be modified or deallocated. The swplugin_command() will be called whenever a player issues a server command. This function can check the given cmd string to see if it will be handled, if it is then this function must return 1 to indicate that the command was handled. Otherwise this function should just return 0. This plugin will respond to the commands; "hostname", "ostype", and "time". When you first load this plugin, issue the server command "help" and you should see the above commands listed. Then issue the server command "hostname", then "time", then "ostype" to get yourself a demostration. When you type the "time" command, you can add an argument as either "gmt" or "local", ie: "time gmt" will give you the gmt time at the server's location. */ #include #include #include #include "../include/swserv-plugins.h" char **swplugin_help_commands_list( int *ncommands, SWServContext *ctx ); int swplugin_command( const char *cmd, const char *arg, int con_num, int obj_num, int uid, int gid, SWServContext *ctx ); /* * SWServ Plugin help commands list. * * Returns an array of strings, each string specifies a command * that this plugin's swplugin_command() function handles. * * Neither the returned pointer array nor each string is modified * or deallocated. */ char **swplugin_help_commands_list( int *ncommands, SWServContext *ctx ) { static char *help_list[] = { "hostname", "ostype", "time" }; if(ncommands != NULL) *ncommands = sizeof(help_list) / sizeof(char *); return(help_list); } /* * SWServ Plugin command. * * Checks if the given cmd is one that this plugin handles, if so * it will be handled and 1 will be returned to indicate that * it was handled. * * Otherwise 0 is returned. */ int swplugin_command( const char *cmd, const char *arg, int con_num, int obj_num, int uid, int gid, SWServContext *ctx ) { int status = 0; if((cmd == NULL) || (arg == NULL)) return(status); /* Begin checking which command is given */ if(!strcmp(cmd, "hostname")) { const char *s = getenv("HOSTNAME"); if(s == NULL) s = "Environment variable HOSTNAME is not set on server's system"; SWServConNotify(ctx, con_num, s); status = 1; } else if(!strcmp(cmd, "ostype")) { const char *s = getenv("OSTYPE"); if(s == NULL) s = "Environment variable OSTYPE is not set on server's computer"; SWServConNotify(ctx, con_num, s); status = 1; } else if(!strcmp(cmd, "time")) { time_t t; struct tm *tm_ptr; const char *s = NULL; /* Argument specifies gmt time? */ if(!strcasecmp(arg, "gmt")) { t = time(NULL); tm_ptr = gmtime(&t); t = mktime(tm_ptr); s = ctime(&t); } /* All else assume argument says "local" */ else { t = time(NULL); tm_ptr = localtime(&t); t = mktime(tm_ptr); s = ctime(&t); } if(s != NULL) SWServConNotify(ctx, con_num, s); status = 1; } return(status); } SWPLUGIN_INIT_FUNCRTN SWPLUGIN_INIT_FUNC(SWPLUGIN_INIT_FUNCINPUT) { /* Upon initialization, reset our globals */ return(0); } SWPLUGIN_MANAGE_FUNCRTN SWPLUGIN_MANAGE_FUNC(SWPLUGIN_MANAGE_FUNCINPUT) { return(0); } SWPLUGIN_SHUTDOWN_FUNCRTN SWPLUGIN_SHUTDOWN_FUNC(SWPLUGIN_SHUTDOWN_FUNCINPUT) { return; }