From a20f2ac2eaf757d6345a260c71ebc782a56da0e0 Mon Sep 17 00:00:00 2001 From: Kevin Bowling Date: Fri, 17 Dec 2010 21:09:41 -0700 Subject: [PATCH] MOTD implementation --- motd.conf.dist | 3 ++- src/craftd-config.c | 63 ++++++++++++++++++++++++++++++++++++++------- src/craftd-config.h | 14 +++++++--- src/craftd.c | 1 + src/network/send.c | 6 +++++ 5 files changed, 74 insertions(+), 13 deletions(-) diff --git a/motd.conf.dist b/motd.conf.dist index 81f6420..a26a770 100644 --- a/motd.conf.dist +++ b/motd.conf.dist @@ -1 +1,2 @@ -Temporary MOTD +Welcome to craftd! This is a test server. +Check us out at http://mc.kev009.com/craftd/ diff --git a/src/craftd-config.c b/src/craftd-config.c index 1c86ebc..45c9fe3 100644 --- a/src/craftd-config.c +++ b/src/craftd-config.c @@ -26,12 +26,23 @@ #include #include #include +#include +#include +#include #include #include "craftd-config.h" #include "util.h" +/* Networking knobs */ +const int MAX_LISTENBACKLOG = 16; +const int MAX_BUF = 8096; + +/* MOTD */ +bstring Config_motd[MOTD_LINES]; +int Config_motdsz = 0; + /* Search path for the main config file, in order */ static const char *config_searchpath[] = { "/.craftd/craftd.conf", // $HOME is appended to this in _setdefaults()! @@ -65,12 +76,46 @@ craftd_config_setdefaults() Config.max_listenbacklog = 16; Config.mcstring_max = 100; Config.workpool_size = 2; - Config.motd_file = "motd.conf"; + char *motddefault = "motd.conf"; + Config.motd_file = motddefault; // httpd settings Config.httpd_enabled = true; Config.httpd_port = 25566; - Config.docroot = "htdocs/"; + char *docrootdefault = "htdocs/"; + Config.docroot = docrootdefault; +} + +void +craftd_config_readmotd(char *file) +{ + //struct bstrList *motd = bstrListCreate(); + FILE *fp; + + LOG(LOG_DEBUG, "Reading MOTD file %s", file); + + /* Open the MOTD or exit */ + if ((fp = fopen(file, "r")) == NULL) + PERR("Error reading MOTD file"); // Read errno and quit + struct bStream *bs = bsopen((bNread)fread, fp); + + bstring line; + int i; + for (i = 0; (line = bgets((bNgetc)fgetc, fp, '\n')) != NULL; ++i) + { + if (i > MOTD_LINES) + ERR("MOTD File is too long, max lines is %d", MOTD_LINES); + + LOG(LOG_DEBUG, "MOTD: %s", line->data); + Config_motd[i] = line; + } + + Config_motdsz = i; + + bsclose(bs); + fclose(fp); + + return; } /** @@ -152,8 +197,8 @@ parseJInt(int *storage, const json_t *obj, const char *key) * @param obj json object to parse * @param key key to read */ -void -parseJString(char *storage, const json_t *obj, const char *key) +char * +parseJString(const json_t *obj, const char *key) { const char *strval; json_t *strobj = json_object_get(obj, key); @@ -162,7 +207,7 @@ parseJString(char *storage, const json_t *obj, const char *key) if (!strobj) { LOG(LOG_DEBUG, "Config: key \"%s\" is undefined. Using default.", key); - return; + return Config.motd_file; } /* Check if the value is a string (fatal) */ @@ -173,8 +218,8 @@ parseJString(char *storage, const json_t *obj, const char *key) LOG(LOG_DEBUG, "Got string value: \"%s\" for key: \"%s\"", strval, key); /* Set the storage location to the new value */ - storage = (char *)Malloc(strlen(strval)); - storage = strdup(strval); + //storage = (char *)Malloc(strlen(strval)); + return strdup(strval); } /** @@ -225,7 +270,7 @@ craftd_config_parse(const char *file) parseJInt(&Config.game_port, jsongame, "game-port"); parseJInt(&Config.mcstring_max, jsongame, "minecraft-stringmax"); parseJInt(&Config.workpool_size, jsongame, "worker-pool-size"); - parseJString(Config.motd_file, jsongame, "motd-file"); + Config.motd_file = parseJString(jsongame, "motd-file"); } else { @@ -238,7 +283,7 @@ craftd_config_parse(const char *file) { parseJBool(&Config.httpd_enabled, jsonhttp, "enabled"); parseJInt(&Config.httpd_port, jsonhttp, "httpd-port"); - parseJString(Config.docroot, jsonhttp, "static-docroot"); + Config.docroot = parseJString(jsonhttp, "static-docroot"); } else { diff --git a/src/craftd-config.h b/src/craftd-config.h index aa560fc..f9bca99 100644 --- a/src/craftd-config.h +++ b/src/craftd-config.h @@ -31,14 +31,22 @@ #include #include -#define MAX_LISTENBACKLOG (16) -#define MAX_BUF (8096) +#include "bstrlib.h" + +/* Networking knobs */ +const int MAX_LISTENBACKLOG; +const int MAX_BUF; /* Public methods */ void craftd_config_setdefaults(); -void craftd_config_parse(const char* file); +void craftd_config_parse(const char *file); +void craftd_config_readmotd(char *file); /* Public data */ +#define MOTD_LINES (20) // Max MOTD line count +extern bstring Config_motd[MOTD_LINES]; +int Config_motdsz; + struct { // Game settings diff --git a/src/craftd.c b/src/craftd.c index 0cf204b..42cdb2a 100644 --- a/src/craftd.c +++ b/src/craftd.c @@ -362,6 +362,7 @@ main(int argc, char *argv[]) /* Initialize the configuration */ craftd_config_setdefaults(); craftd_config_parse(argconfigfile); + craftd_config_readmotd(Config.motd_file); /* Tell libevent to use our logging functions */ event_set_log_callback(ev_log_callback); diff --git a/src/network/send.c b/src/network/send.c index 3384e8d..fe8d046 100644 --- a/src/network/send.c +++ b/src/network/send.c @@ -95,6 +95,12 @@ process_login(struct PL_entry *player, bstring username, uint32_t ver) player->username->data); send_syschat(loginmsg); bstrFree(loginmsg); + + /* Send player MOTD */ + for(int i=0; i < Config_motdsz; ++i) + { + send_directchat(player, Config_motd[i]); + } return; }