Skip to content

Commit

Permalink
MOTD implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kev009 committed Dec 18, 2010
1 parent 89bc312 commit a20f2ac
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
3 changes: 2 additions & 1 deletion motd.conf.dist
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Temporary MOTD
Welcome to craftd! This is a test server.
Check us out at http://mc.kev009.com/craftd/
63 changes: 54 additions & 9 deletions src/craftd-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,23 @@
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <jansson.h>

#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()!
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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) */
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down
14 changes: 11 additions & 3 deletions src/craftd-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,22 @@
#include <stdbool.h>
#include <stdint.h>

#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
Expand Down
1 change: 1 addition & 0 deletions src/craftd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/network/send.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit a20f2ac

Please sign in to comment.