Replace server_id with generic configurable string replacements usable in banners.

git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@613 db0b04b0-f4d1-0310-9a6d-de3e77497b0e
This commit is contained in:
Pasi Kallinen 2011-10-09 15:31:34 +00:00
parent 78e94fa5de
commit 0ba0998ece
7 changed files with 79 additions and 18 deletions

View File

@ -87,7 +87,7 @@ extra_info_file { return TYPE_EXTRA_INFO_FILE; }
"max_idle_time" { return TYPE_MAX_IDLE_TIME; }
"rc_fmt" { return TYPE_RC_FMT; }
"ttyrecdir" { return TYPE_PATH_TTYREC; }
server_id { return TYPE_SERVER_ID; }
bannervars { return TYPE_BANNERVARS; }
sortmode { return TYPE_WATCH_SORTMODE; }
watch_columns { return TYPE_WATCH_COLUMNS; }
commands { return TYPE_CMDQUEUE; }

View File

@ -50,7 +50,7 @@ static int sortmode_number(const char *sortmode_name) {
}
%token TYPE_SUSER TYPE_SGROUP TYPE_SGID TYPE_SUID TYPE_MAX TYPE_MAXNICKLEN
%token TYPE_GAME_SHORT_NAME TYPE_WATCH_SORTMODE TYPE_SERVER_ID
%token TYPE_GAME_SHORT_NAME TYPE_WATCH_SORTMODE TYPE_BANNERVARS
%token TYPE_ALLOW_REGISTRATION TYPE_WATCH_COLUMNS
%token TYPE_PATH_GAME TYPE_NAME_GAME TYPE_PATH_DGLDIR TYPE_PATH_SPOOL
%token TYPE_PATH_BANNER TYPE_PATH_CANNED TYPE_PATH_CHROOT
@ -181,11 +181,6 @@ KeyPair: TYPE_CMDQUEUE '[' TYPE_CMDQUEUENAME ']'
}
break;
case TYPE_SERVER_ID:
if (globalconfig.server_id) free(globalconfig.server_id);
globalconfig.server_id = strdup($3);
break;
case TYPE_PATH_DGLDIR:
if (globalconfig.dglroot) free(globalconfig.dglroot);
globalconfig.dglroot = strdup($3);
@ -284,6 +279,9 @@ KeyPair: TYPE_CMDQUEUE '[' TYPE_CMDQUEUENAME ']'
exit(1);
}
}
| TYPE_BANNERVARS '=' '[' banner_vars ']'
{
}
| TYPE_WATCH_COLUMNS '=' '[' watch_columns ']' {
memcpy(globalconfig.watch_columns,
curr_watch_columns,
@ -294,6 +292,14 @@ KeyPair: TYPE_CMDQUEUE '[' TYPE_CMDQUEUENAME ']'
};
banner_vars : banner_vars ',' banner_var
| banner_var;
banner_var : TYPE_VALUE '=' TYPE_VALUE
{
banner_var_add($1, $3);
};
watch_columns: watch_columns ',' watch_column
| watch_column;
@ -634,7 +640,6 @@ KeyType : TYPE_SUSER { $$ = TYPE_SUSER; }
| TYPE_UTF8ESC { $$ = TYPE_UTF8ESC; }
| TYPE_RC_FMT { $$ = TYPE_RC_FMT; }
| TYPE_WATCH_SORTMODE { $$ = TYPE_WATCH_SORTMODE; }
| TYPE_SERVER_ID { $$ = TYPE_SERVER_ID; }
;
%%
@ -666,7 +671,7 @@ const char* lookup_token (int t)
case TYPE_RC_FMT: return "rc_fmt";
case TYPE_WATCH_SORTMODE: return "sortmode";
case TYPE_WATCH_COLUMNS: return "watch_columns";
case TYPE_SERVER_ID: return "server_id";
case TYPE_BANNERVARS: return "bannervars";
case TYPE_LOCALE: return "locale";
case TYPE_UTF8ESC: return "utf8esc";
default: abort();

View File

@ -404,6 +404,45 @@ bannerstrmangle(char *buf, char *fromstr, char *tostr)
return bufnew;
}
void
banner_var_add(char *name, char *value)
{
struct dg_banner_var *tmp = (struct dg_banner_var *)malloc(sizeof(struct dg_banner_var));
if (!tmp) return;
tmp->name = strdup(name);
tmp->value = strdup(value);
tmp->next = globalconfig.banner_var_list;
globalconfig.banner_var_list = tmp;
}
void
banner_var_free()
{
struct dg_banner_var *tmp;
struct dg_banner_var *bv = globalconfig.banner_var_list;
while (bv) {
tmp = bv->next;
free(bv->name);
free(bv->value);
free(bv);
bv = tmp;
}
globalconfig.banner_var_list = NULL;
}
char *
banner_var_value(char *name)
{
struct dg_banner_var *bv = globalconfig.banner_var_list;
while (bv) {
if (!strcmp(bv->name, name)) return bv->name;
bv = bv->next;
}
return NULL;
}
void
freebanner(struct dg_banner *ban)
{
@ -477,8 +516,12 @@ loadbanner (char *fname, struct dg_banner *ban)
}
}
} else {
struct dg_banner_var *bv = globalconfig.banner_var_list;
while (bv) {
strncpy(bufnew, bannerstrmangle(bufnew, bv->name, bv->value), 80);
bv = bv->next;
}
strncpy(bufnew, bannerstrmangle(bufnew, "$VERSION", PACKAGE_STRING), 80);
strncpy(bufnew, bannerstrmangle(bufnew, "$SERVERID", globalconfig.server_id ? globalconfig.server_id : ""), 80);
if (me && loggedin) {
strncpy(bufnew, bannerstrmangle(bufnew, "$USERNAME", me->username), 80);
} else {
@ -2782,7 +2825,7 @@ main (int argc, char** argv)
free (me);
freebanner(&banner);
banner_var_free();
graceful_exit (1);
return 1;

View File

@ -101,6 +101,12 @@ static const char *SORTMODE_NAME[NUM_SORTMODES] = {
#endif
};
struct dg_banner_var {
char *name;
char *value;
struct dg_banner_var *next;
};
struct dg_cmdpart
{
dglcmd_actions cmd;
@ -224,7 +230,7 @@ struct dg_globalconfig
char* lockfile;
int allow_registration; /* allow registering new nicks */
int sortmode; /* default watching-screen sortmode */
char *server_id; /* string for the server name or whatever */
struct dg_banner_var *banner_var_list;
char *locale;
int utf8esc; /* send select-utf8-charset escape code */
@ -273,6 +279,9 @@ extern char *gen_inprogress_lock(int game, pid_t pid, char *ttyrec_filename);
extern void catch_sighup(int signum);
extern void loadbanner(char *fname, struct dg_banner *ban);
extern void drawbanner(struct dg_banner *ban, unsigned int start_line, unsigned int howmany);
extern void banner_var_add(char *name, char *value);
extern char *banner_var_value(char *name);
extern void banner_var_free(void);
extern int check_retard(int reset);
extern char *dgl_format_str(int game, struct dg_user *me, char *str, char *plrname);

View File

@ -743,7 +743,7 @@ create_config ()
if (!globalconfig.allow_registration) globalconfig.allow_registration = 1;
globalconfig.menulist = NULL;
globalconfig.server_id = NULL;
globalconfig.banner_var_list = NULL;
globalconfig.locale = NULL;
globalconfig.shed_uid = (uid_t)-1;

View File

@ -46,14 +46,17 @@ chroot_path = "/opt/nethack/nethack.alt.org/"
# From inside the jail, dgamelaunch's working directory for rcfiles/ttyrec/etc
dglroot = "/dgldir/"
# Server ID string
server_id = "nethack.alt.org - http://nethack.alt.org/"
# Strings to be replaced in every banner
bannervars = [ "$MOTDTIME" = "2011.10.08",
"$SERVERID" = "nethack.alt.org - http://nethack.alt.org/"
]
# From inside the jail, location of a banner file, the topmost line will be
# shown in submenus that cannot be defined separately.
# Some string substitution is done for the file:
# $VERSION replaced with "dgamelaunch v" + dgl version number.
# $SERVERID replaced with the server_id string, as defined above.
# - bannervars from above
# - $VERSION replaced with "dgamelaunch v" + dgl version number.
# - $USERNAME replaced with logged-in user's name, or with "[Anonymous]"
banner = "/dgl-banner"
# The following two options are fairly insecure. They will force us to

View File

@ -99,6 +99,7 @@ ttyrec_id(int game, char *username, char *ttyrec_filename)
Header h;
char *buf = (char *)malloc(1024);
char tmpbuf[256];
char *server_id = banner_var_value("$SERVERID");
if (!buf) return;
tstamp = time(NULL);
@ -115,7 +116,7 @@ ttyrec_id(int game, char *username, char *ttyrec_filename)
dCLRSCR,
username,
myconfig[game]->game_name,
globalconfig.server_id,
(server_id ? server_id : "Unknown"),
ttyrec_filename,
tstamp, ctime(&tstamp)
);