[mw-devel] MW3 r1091 - trunk/src

arthur at sucs.org arthur at sucs.org
Tue Oct 27 16:50:07 GMT 2009


Author: arthur
Date: 2009-10-27 16:50:07 +0000 (Tue, 27 Oct 2009)
New Revision: 1091

Modified:
   trunk/src/Parse.c
   trunk/src/bb.h
   trunk/src/chattable.c
   trunk/src/init.c
   trunk/src/sqlite.c
   trunk/src/sqlite.h
   trunk/src/talker.c
   trunk/src/talker.h
Log:
allow storage of mwrc filename/url on per user basis


Modified: trunk/src/Parse.c
===================================================================
--- trunk/src/Parse.c	2009-10-27 14:27:23 UTC (rev 1090)
+++ trunk/src/Parse.c	2009-10-27 16:50:07 UTC (rev 1091)
@@ -282,7 +282,7 @@
 
 void InitParser(void)
 {
-	LoadInitFile(".mwrc");
+	LoadInitFile(NULL);
 }
 
 #define min(a,b)  a<b?a:b

Modified: trunk/src/bb.h
===================================================================
--- trunk/src/bb.h	2009-10-27 14:27:23 UTC (rev 1090)
+++ trunk/src/bb.h	2009-10-27 16:50:07 UTC (rev 1091)
@@ -39,6 +39,10 @@
 #define TALKHELP	"talkhelp"
 #define MUDHELPDIR	"mudhelp"
 
+#define USERSQL		"users.db"
+#define USERDB_PRIVATE	"private"
+#define USERDB_PUBLIC	"public"
+
 #define TEXT_END	".t"
 #define INDEX_END	".i"
 #define MOD_END		".m"

Modified: trunk/src/chattable.c
===================================================================
--- trunk/src/chattable.c	2009-10-27 14:27:23 UTC (rev 1090)
+++ trunk/src/chattable.c	2009-10-27 16:50:07 UTC (rev 1091)
@@ -49,6 +49,7 @@
 {"kick"		,0x0804	,1, "Usage: kick <user> [reason]", "Eject user from talker, ignoring protection", t_kick, 1},
 {"mrod"		,0x0008	,1, "Usage: mrod <user> [reason]", "Eject user from the BBS", t_mrod, 1},
 {"mudmode"	,0x0000	,1, "Usage: mudmode <on|off>", "Toggle MUD Mode - allow MUD extensions", t_mudmode, 1},
+{"mwrc"		,0x0200	,0, "Usage: mwrc [filename]", "Set/View your mwrc startup file", t_mwrc, 1},
 {"notsayto"	,0x0003	,2, "Usage: notsayto <user> <message>", "Talk normally but exclude a single user", t_notsayto, 1},
 {"protect"	,0x0020	,1, "Usage: protect <user> [level]", "Protect user from harm [to optional level]", t_protect, 1},
 {"quit"		,0x0000	,0, "Usage: quit", "Leave Talker", t_quit, 1},

Modified: trunk/src/init.c
===================================================================
--- trunk/src/init.c	2009-10-27 14:27:23 UTC (rev 1090)
+++ trunk/src/init.c	2009-10-27 16:50:07 UTC (rev 1091)
@@ -14,6 +14,7 @@
 #include "bb.h"
 #include "alias.h"
 #include "js.h"
+#include "sqlite.h"
 #include <curl/curl.h>
 
 extern Alias alias_list;
@@ -28,6 +29,8 @@
 #include "script.h"
 #include "frl.h"
 
+extern struct person *user;
+
 /* Prototypes */
 int ReadInitFile(char *base, char *filename);
 void LoadFunction(char *, char *, FILE *, int *, const char *, int);
@@ -36,7 +39,19 @@
 void LoadInitFile(char *name)
 {
 	struct passwd *pw;
+	int dofree=0;
 
+	if (name == NULL) {
+		char * value = userdb_get(USERDB_PRIVATE, user->name, "mwrc");
+		if (value != NULL && value[0]!=0) {
+			name=value;
+			dofree=1;
+		}
+		if (value != NULL && value[0]==0)
+			free(value);
+	}
+	if (name == NULL) name = ".mwrc";
+
 	if ((pw=getpwuid(getuid()))!=NULL)
 	{
 		/* try to load the personal copy*/
@@ -48,6 +63,7 @@
 			}
 		}
 	}
+	if (dofree) free(name);
 }
 
 int ReadInitFile(char *base, char *filename)

Modified: trunk/src/sqlite.c
===================================================================
--- trunk/src/sqlite.c	2009-10-27 14:27:23 UTC (rev 1090)
+++ trunk/src/sqlite.c	2009-10-27 16:50:07 UTC (rev 1091)
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include "sqlite.h"
 #include <sqlite3.h>
+#include "bb.h"
 
 sqlite3*
 db_open(char *dbname)
@@ -174,6 +175,31 @@
 	return result->rows;
 }
 
+char * db_item(struct db_result *result, int row, const char *field)
+{
+	int i;
+	int col=-1;
+	struct db_data *ndata;
+
+	if (result == NULL) return NULL;
+	if (row < 0 || row >= result->rows) return NULL;
+
+	col = -1;
+	for(i=0;i<result->cols;i++) {
+		if (strcasecmp(field, result->colNames[i])==0)
+			col = i;
+	}
+	if (col == -1) return NULL;
+
+	/* find the row */
+	ndata = result->data;
+	while (ndata != NULL && ndata->row != row) ndata=ndata->next;
+	if (ndata == NULL)  return NULL;
+
+	return ndata->field[col];
+}
+
+
 /* fetch a row, return it as a struct */
 void db_getrow(struct db_result *result, int *row)
 {
@@ -248,3 +274,69 @@
 	}
 	free(result);
 }
+
+
+char * userdb_get(const char *table, const char *user, const char *opt)
+{
+	char path[PATHSIZE];
+	struct db_result *old;
+	char *value;
+	char *out;
+
+	snprintf(path, PATHSIZE, "%s/%s", STATEDIR, USERSQL);
+
+	char *query = sqlite3_mprintf("SELECT * from %Q WHERE user=%Q AND opt=%Q", table, user, opt);
+	old  = db_query(path, query, 1);
+	sqlite3_free(query);
+
+	if (old == NULL) return NULL;
+	if (db_numrows(old) < 1) return NULL;
+
+	if ((value = db_item(old, 0, "arg"))==NULL)
+	{
+		db_free(old);
+		return NULL;
+	}
+	out = strdup(value);
+	db_free(old);
+	return out;
+}
+
+
+
+void userdb_set(const char *table, const char *user, const char *opt, const char *arg)
+{
+	char path[PATHSIZE];
+	struct db_result *old;
+
+	snprintf(path, PATHSIZE, "%s/%s", STATEDIR, USERSQL);
+
+	char *query = sqlite3_mprintf("SELECT * from %Q WHERE user=%Q AND opt=%Q", table, user, opt);
+	old  = db_query(path, query, 1);
+	sqlite3_free(query);
+
+	/* set a new value */
+	struct db_result *res;
+	if (old != NULL) {
+		char *query = sqlite3_mprintf("UPDATE %q SET arg=%Q WHERE user=%Q AND opt=%Q", table, arg, user, opt);
+		db_free(db_query(path, query, 1));
+		sqlite3_free(query);
+	} else {
+		char *query = sqlite3_mprintf("INSERT INTO %q (user, opt, arg) VALUES (%Q,%Q,%Q)", table, user, opt, arg);
+		res = db_query(path, query, 1);
+		if (res == NULL) {
+			/* it failed, try to create the table */
+			char *create = sqlite3_mprintf("CREATE TABLE %q (user text, opt text, arg text)", table);
+			res = db_query(path, create, 0);
+			sqlite3_free(create);
+			if (res != NULL) {
+				/* it worked, resubmit the insert */
+				db_free(res);
+				res = db_query(path, query, 0);
+			}
+		}
+		db_free(res);
+		sqlite3_free(query);
+	}
+	db_free(old);
+}

Modified: trunk/src/sqlite.h
===================================================================
--- trunk/src/sqlite.h	2009-10-27 14:27:23 UTC (rev 1090)
+++ trunk/src/sqlite.h	2009-10-27 16:50:07 UTC (rev 1091)
@@ -25,3 +25,7 @@
 void db_getrow(struct db_result *result, int *row);
 void db_free(struct db_result *result);
 void js_db_free(struct js_db_result *result);
+char * db_item(struct db_result *result, int row, const char *field);
+char * userdb_get(const char *table, const char *user, const char *opt);
+void userdb_set(const char *table, const char *user, const char *opt, const char *arg);
+

Modified: trunk/src/talker.c
===================================================================
--- trunk/src/talker.c	2009-10-27 14:27:23 UTC (rev 1090)
+++ trunk/src/talker.c	2009-10-27 16:50:07 UTC (rev 1091)
@@ -21,6 +21,8 @@
 #include "script.h"
 #include "js.h"
 #include "files.h"
+#include "sqlite.h"
+#include <sqlite3.h>
 
 #include "rooms.h"
 #include "ipc.h"
@@ -438,6 +440,24 @@
 	if (!strcmp(argv[1],"*")) DestroyAllFunctions(1); else DestroyFunction(argv[1]);
 }
 
+void t_mwrc(CommandList *cm, int argc, char **argv, char *args)
+{
+	if (argc < 2) {
+	        char * old = userdb_get(USERDB_PRIVATE, user->name, "mwrc");
+		if (old==NULL) {
+			printf(_("Current mwrc path: %s\n"), _("<unset>"));
+		} else {
+			printf(_("Current mwrc path: %s\n"), old);
+			free(old);
+		}
+	} else {
+		userdb_set(USERDB_PRIVATE, user->name, "mwrc", argv[1]);
+	        char * old = userdb_get(USERDB_PRIVATE, user->name, "mwrc");
+		printf(_("Setting mwrc path to: %s\n"), old);
+		free(old);
+	}
+}
+
 void t_restart(CommandList *cm, int argc, char **argv, char *args)
 {
 	extern var_list_t	var_list;
@@ -465,7 +485,7 @@
 	RoomInit(&myroom);
 	LoadRoom(&myroom, user->room);
 
-	LoadInitFile(".mwrc");
+	LoadInitFile(NULL);
 
 	/* run board init functions */
 	RunInitFuncs(0);

Modified: trunk/src/talker.h
===================================================================
--- trunk/src/talker.h	2009-10-27 14:27:23 UTC (rev 1090)
+++ trunk/src/talker.h	2009-10-27 16:50:07 UTC (rev 1091)
@@ -53,6 +53,7 @@
 void t_spy(CommandList *cm, int argc, char **argv, char *args);
 void t_showvars(CommandList *cm, int argc, char **argv, char *args);
 void t_linewrap(CommandList *cm, int argc, char **argv, char *args);
+void t_mwrc(CommandList *cm, int argc, char **argv, char *args);
 void t_chaton(void);
 
 void chat_say(char *text);




More information about the mw-devel mailing list