[mw-devel] [Git][arthur/mw][master] 3 commits: Move the global 'myroom' into struct user

Andrew Price welshbyte at sucs.org
Fri Nov 6 22:31:18 GMT 2015


Andrew Price pushed to branch master at Justin Mitchell / mw


Commits:
5285e0ce by Andrew Price at 2015-11-06T20:46:15Z
Move the global 'myroom' into struct user

- - - - -
94013d39 by Andrew Price at 2015-11-06T21:16:38Z
Report errors from userdb_open() and fetch_user()

- - - - -
c30536af by Andrew Price at 2015-11-06T21:44:44Z
Use struct user and fetch_user in replay()

- - - - -


14 changed files:

- src/client/edit.c
- src/client/folders.c
- src/client/incoming.c
- src/client/main.c
- src/client/script_inst.c
- src/client/talker.c
- src/client/talker_privs.c
- src/client/user.c
- src/client/who.c
- src/rooms.c
- src/server/replay.c
- src/server/servsock.c
- src/user.c
- src/user.h


Changes:

=====================================
src/client/edit.c
=====================================
--- a/src/client/edit.c
+++ b/src/client/edit.c
@@ -603,6 +603,8 @@ static void users_lastread(int folnum)
 	int ufile;
 
 	ufile=userdb_open(O_RDWR);
+	if (ufile < 0)
+		return;
 	Lock_File(ufile);
 	while (read(ufile,&usr,sizeof(usr))>0)
 	{


=====================================
src/client/folders.c
=====================================
--- a/src/client/folders.c
+++ b/src/client/folders.c
@@ -80,7 +80,9 @@ void auto_subscribe(int folnum, int state)
 	struct person *urec = &usr.record;
 	char buff[10];
 
-	ufile=userdb_open(O_RDWR);
+	ufile = userdb_open(O_RDWR);
+	if (ufile < 0)
+		return;
 	Lock_File(ufile); /*user file*/
 	while(get_person(ufile, &usr))
 	{


=====================================
src/client/incoming.c
=====================================
--- a/src/client/incoming.c
+++ b/src/client/incoming.c
@@ -36,7 +36,6 @@ extern Alias event_list;
 extern Alias onoff_list;
 extern Alias ipc_list;
 extern struct user *user;
-extern struct room myroom;
 extern int quietmode;
 extern ipc_connection_t *ipcsock;
 
@@ -439,8 +438,8 @@ void handle_mesg()
 	while (msg != NULL) {
 		if (msg->head.src == SYSTEM_USER) {
 			strcpy(mesg_user.record.name,"System");
-		} else {
-			fetch_user(&mesg_user, msg->head.src);
+		} else if (fetch_user(&mesg_user, msg->head.src) != 0) {
+			return;
 		}
 		accept_pipe_cmd(msg, &mesg_user);
 
@@ -510,7 +509,8 @@ static void display_content(ipc_message_t *msg)
 		whom = "System";
 	} else {
 		msg_posn = msg->head.src;
-		fetch_user(&msg_user, msg_posn);
+		if (fetch_user(&msg_user, msg_posn) != 0)
+			return;
 		whom = msg_user.record.name;
 	}
 
@@ -976,9 +976,9 @@ static void force_channel(char *newbuff, char *from, unsigned long theirprivs)
 			talk_send_rawbcast(text);
 			user->record.chatmode = cm_set(user, CM_ONCHAT);
 
-			RoomDestroy(&myroom);
-			RoomInit(&myroom);
-			LoadRoom(&myroom, newroom);
+			RoomDestroy(&user->room);
+			RoomInit(&user->room);
+			LoadRoom(&user->room, newroom);
 
 			/* force update of user information _before_ scripts are run */
 			update_user(user);
@@ -1011,9 +1011,9 @@ static void force_channel(char *newbuff, char *from, unsigned long theirprivs)
 			snprintf(text,MAXTEXTLENGTH-1,"\03315You have been summoned to room %d by %s.",newroom,from);
 			display_message(text, 1, 1);
 
-			RoomDestroy(&myroom);
-			RoomInit(&myroom);
-			LoadRoom(&myroom, newroom);
+			RoomDestroy(&user->room);
+			RoomInit(&user->room);
+			LoadRoom(&user->room, newroom);
 			enter_room(newroom);
 		}
 	}


=====================================
src/client/main.c
=====================================
--- a/src/client/main.c
+++ b/src/client/main.c
@@ -49,9 +49,6 @@
 #include "bb.h"
 #include "userio.h"
 #include "who.h"
-
-extern struct room myroom;
-
 #include "alias.h"
 extern Alias bind_list;
 extern Alias alias_list;
@@ -734,18 +731,11 @@ int main(int argc, char **argv)
 
 	InitParser();
 	set_rights();
-
 	init_colour();
-
-	RoomInit(&myroom);
-
-/*	if (!(rights&65l))  a pleb - timeout of 10 mins
-		user->timeout=600; */
+	RoomInit(&user->room);
 	update_user(user);
-
 	/* run all board init functions */
 	RunInitFuncs(0);
-
 	/* broadcast board logon signal to other users */
 	broadcast_onoffcode(3, autochat, NULL, NULL);
 
@@ -917,7 +907,7 @@ void close_down(int exitmode, char *sourceuser, char *reason)
 
 	ScriptCleanup();
 
-	RoomDestroy(&myroom);
+	RoomDestroy(&user->room);
 	ClearStack();
 	stop_js();
 	alarm_cleanup();
@@ -1768,8 +1758,9 @@ char *part_user(const char *text, int status)
 
 	if (status==0)
 	{
-		if (file!=0) close(file);
-		file=userdb_open(O_RDONLY);
+		file = userdb_open(O_RDONLY);
+		if (file < 0)
+			return NULL;
 		len=strlen(text);
 	}
 


=====================================
src/client/script_inst.c
=====================================
--- a/src/client/script_inst.c
+++ b/src/client/script_inst.c
@@ -31,9 +31,6 @@
 extern struct user *user;
 extern Alias alias_list;
 extern Alias bind_list;
-
-extern struct room myroom;
-
 /* current script runaway variable */
 extern long runaway;
 
@@ -388,7 +385,7 @@ void scr_roomnum( struct code *pc, int fargc, char **fargv )
 				snprintf(rn, 9, "%d", user->record.room);
 			}
 			/* if room is not hidden, or locked, then return room number */
-			else if (myroom.hidden<=0 && myroom.locked<=0)
+			else if (user->room.hidden <= 0 && user->room.locked <= 0)
 			{
 				snprintf(rn, 9, "%d", user->record.room);
 			}


=====================================
src/client/talker.c
=====================================
--- a/src/client/talker.c
+++ b/src/client/talker.c
@@ -53,8 +53,6 @@ extern unsigned long loggedin;
 extern int quietmode;
 static int runautoexec = 1;
 
-extern struct room myroom;
-
 extern CommandList table[];
 extern CommandList chattable[];
 
@@ -390,10 +388,10 @@ void t_restart(CommandList *cm, int argc, const char **argv, char *args)
 	stop_js();
 	setup_js();
 
-	RoomDestroy(&myroom);
+	RoomDestroy(&user->room);
 
-	RoomInit(&myroom);
-	LoadRoom(&myroom, user->record.room);
+	RoomInit(&user->room);
+	LoadRoom(&user->room, user->record.room);
 
 	LoadInitFile(NULL);
 
@@ -1146,9 +1144,9 @@ void t_chaton(void)
 	urec->chatmode = cm_set(user, CM_ONCHAT);
 	update_user(user);
 
-	if (myroom.name!=NULL)
+	if (user->room.name!=NULL)
 		snprintf(mtext, MAXTEXTLENGTH-1, "\03310%s has just joined talker in %s",
-		         urec->name, myroom.name);
+		         urec->name, user->room.name);
 	else
 		snprintf(mtext, MAXTEXTLENGTH-1, "\03310%s has just joined talker room %d",
 		         urec->name, urec->room);
@@ -1419,9 +1417,9 @@ int ChangeRoom(char *room, int quiet)
 			user->record.room = i;
 			update_user(user);
 
-			RoomDestroy(&myroom);
-			RoomInit(&myroom);
-			LoadRoom(&myroom, user->record.room);
+			RoomDestroy(&user->room);
+			RoomInit(&user->room);
+			LoadRoom(&user->room, user->record.room);
 
 			SAFE_FREE(buff);
 			if (asprintf(&buff, "\03313%s has left to room %d", user->record.name, i) < 0)


=====================================
src/client/talker_privs.c
=====================================
--- a/src/client/talker_privs.c
+++ b/src/client/talker_privs.c
@@ -13,7 +13,6 @@
 
 extern struct user *user;
 extern int quietmode;
-extern struct room myroom;
 
 unsigned long chatmode_describe(unsigned long old, unsigned long new, int ourapl, int theirapl, const char *from)
 {
@@ -180,9 +179,9 @@ unsigned long chatmode_describe(unsigned long old, unsigned long new, int ourapl
 
 		set_talk_rights();
 
-		if (myroom.name!=NULL)
+		if (user->room.name != NULL)
 			snprintf(mtext,MAXTEXTLENGTH-1,"\03310%s has just joined talker in %s",
-			         user->record.name, myroom.name);
+			         user->record.name, user->room.name);
 		else
 			snprintf(mtext,MAXTEXTLENGTH-1,"\03310%s has just joined talker room %d",
 			         user->record.name, user->record.room);


=====================================
src/client/user.c
=====================================
--- a/src/client/user.c
+++ b/src/client/user.c
@@ -351,6 +351,9 @@ void list_users(int newonly)
 	long logout_time;
 
 	file=userdb_open(O_RDONLY);
+	if (file < 0)
+		return;
+
 	while (get_person(file, &usr))
 	{
 		if (!newonly || ( newonly && !u_reg(&usr) ) )
@@ -391,6 +394,9 @@ void list_users_since(long date)
 	long logout_time;
 
 	file=userdb_open(O_RDONLY);
+	if (file < 0)
+		return;
+
 	while (get_person(file, &usr))
 	{
 		if (urec->lastlogout > date)
@@ -434,6 +440,9 @@ static void list_users_flags(int flags, int type, int inv)
 	int screen_height=screen_h();
 
 	file=userdb_open(O_RDONLY);
+	if (file < 0)
+		return;
+
 	while (get_person(file,&usr))
 	{
 		if (type==0) check=urec->status;


=====================================
src/client/who.c
=====================================
--- a/src/client/who.c
+++ b/src/client/who.c
@@ -27,7 +27,6 @@
 
 extern int busy;
 extern struct user *user;
-extern struct room myroom;
 
 char *itime(unsigned long t)
 {


=====================================
src/rooms.c
=====================================
--- a/src/rooms.c
+++ b/src/rooms.c
@@ -4,8 +4,6 @@
 #include "rooms.h"
 #include "util.h"
 
-struct room myroom;
-
 int isanum(char *a, int *result, int onlydecimal);
 
 void RoomInit(struct room *room)


=====================================
src/server/replay.c
=====================================
--- a/src/server/replay.c
+++ b/src/server/replay.c
@@ -134,19 +134,13 @@ void replay(ipc_connection_t *conn, ipc_message_t *msg)
 	json_decref(cmd);
 
 	/* who are we doing this for */
-	struct person user;
-	int users_fd = userdb_open(O_RDONLY);
-	lseek(users_fd, conn->user, SEEK_SET);
-	if (read(users_fd, &user, sizeof(user)) <= 0) {
-		close(users_fd);
+	struct user user;
+	if (fetch_user(&user, conn->user) != 0) {
 		send_error(conn, msg, "Error cannot find your user record");
 		return;
 	}
-	close(users_fd);
-
-	struct room room;
-	RoomInit(&room);
-	LoadRoom(&room, user.room);
+	RoomInit(&user.room);
+	LoadRoom(&user.room, user.record.room);
 
 	/* now, go and replay those messages that are appropriate */
 	for (;idx != store_next; idx = store_wrap( idx + 1 )) {
@@ -160,18 +154,18 @@ void replay(ipc_connection_t *conn, ipc_message_t *msg)
 
 			const char * exclude = json_getstring(j, "exclude");
 
-			if (exclude != NULL && strcasecmp(exclude, user.name)==0) {
+			if (exclude != NULL && strcasecmp(exclude, user.record.name)==0) {
 				/* thats us ! shhh... */
 				json_decref(j);
 				continue;
 			}
 			json_decref(j);
 
-			if (user.room == store[idx]->head.dst) {
+			if (user.record.room == store[idx]->head.dst) {
 				/* right room, send it */
 				msg_attach(store[idx], conn);
 			} else
-			if (room.sproof < 1 && (user.chatmode & CM_GLOBAL)) {
+			if (user.room.sproof < 1 && (cm_test(&user, CM_GLOBAL))) {
 			/* room not soundproof, and user has global on */
 				msg_attach(store[idx], conn);
 			}
@@ -184,7 +178,7 @@ void replay(ipc_connection_t *conn, ipc_message_t *msg)
 
 			const char * target = json_getstring(j, "target");
 
-			if (target!=NULL && strcasecmp(target, user.name)==0) {
+			if (target!=NULL && strcasecmp(target, user.record.name)==0) {
 				/* yes, its for us */
 				msg_attach(store[idx], conn);
 			}
@@ -197,7 +191,7 @@ void replay(ipc_connection_t *conn, ipc_message_t *msg)
 		msg_attach(store[idx], conn);
 	}
 
-	RoomDestroy(&room);
+	RoomDestroy(&user.room);
 
 	return;
 }


=====================================
src/server/servsock.c
=====================================
--- a/src/server/servsock.c
+++ b/src/server/servsock.c
@@ -434,8 +434,11 @@ void msg_attach_to_channel(ipc_message_t *msg, int channel, const char * exclude
 	int users_fd = userdb_open(O_RDONLY);
 	struct user user;
 	struct person *urec = &user.record;
-
 	struct room room;
+
+	if (users_fd < 0)
+		return;
+
 	RoomInit(&room);
 	LoadRoom(&room, channel);
 
@@ -472,8 +475,11 @@ int msg_attach_to_username(ipc_message_t *msg, const char * username)
 	struct user user;
 	struct person *urec = &user.record;
 	int found = 0;
-
 	struct list_head *pos;
+
+	if (users_fd < 0)
+		return 0;
+
 	list_for_each(pos, &connection_list) {
 		ipc_connection_t *c = list_entry(pos, ipc_connection_t, list);
 		if (c->state != IPCSTATE_VALID) continue;
@@ -497,8 +503,11 @@ void msg_attach_to_all(ipc_message_t *msg)
 	int users_fd = userdb_open(O_RDONLY);
 	struct user user;
 	struct person *urec = &user.record;
-
 	struct list_head *pos;
+
+	if (users_fd < 0)
+		return;
+
 	list_for_each(pos, &connection_list) {
 		ipc_connection_t *c = list_entry(pos, ipc_connection_t, list);
 		if (c->state != IPCSTATE_VALID) continue;
@@ -683,12 +692,14 @@ void init_server()
 
 ipc_message_t * msg_wholist(void)
 {
-	ipc_message_t * msg = ipcmsg_create(IPC_WHOLIST, SYSTEM_USER);
-
 	int users_fd = userdb_open(O_RDONLY);
 	struct user user;
 	struct person *urec = &user.record;
 
+	if (users_fd < 0)
+		return NULL;
+
+	ipc_message_t * msg = ipcmsg_create(IPC_WHOLIST, SYSTEM_USER);
 	json_t * list = json_array();
 
 	struct list_head *pos;
@@ -735,8 +746,11 @@ int32_t who_find(const char * username)
 {
 	int users_fd = userdb_open(O_RDONLY);
 	struct person user;
-
 	struct list_head *pos;
+
+	if (users_fd < 0)
+		return -1;
+
 	list_for_each(pos, &connection_list) {
 		ipc_connection_t *c = list_entry(pos, ipc_connection_t, list);
 		if (c->state != IPCSTATE_VALID) continue;


=====================================
src/user.c
=====================================
--- a/src/user.c
+++ b/src/user.c
@@ -13,10 +13,8 @@
 int userdb_open(int flags)
 {
 	int ufd = open(USERFILE, flags|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP);
-	if (ufd < 0) {
+	if (ufd < 0)
 		perror(USERFILE);
-		exit(-1);
-	}
 	return ufd;
 }
 
@@ -48,20 +46,27 @@ void update_user(struct user *user)
 	int outfile;
 
 	outfile = userdb_open(O_WRONLY|O_CREAT);
+	if (outfile < 0)
+		return;
 	update_user_fd(outfile, user);
 	close(outfile);
 }
 
-void fetch_user(struct user *user, int32_t userposn)
+int fetch_user(struct user *user, int32_t userposn)
 {
 	struct person *record = &user->record;
-        int outfile;
+        int outfile = userdb_open(O_RDWR|O_CREAT);
+	if (outfile < 0)
+		return 1;
 
-        outfile=userdb_open(O_RDWR|O_CREAT);
-        lseek(outfile, userposn, SEEK_SET);
-        read(outfile,record,sizeof(*record));
+        if (pread(outfile, record, sizeof(*record), userposn) != sizeof(*record)) {
+		perror("pread");
+		close(outfile);
+		return 1;
+	}
         close(outfile);
 	user->posn = userposn;
+	return 0;
 }
 
 static int user_find_name(const char *name, struct user *user, int *found)


=====================================
src/user.h
=====================================
--- a/src/user.h
+++ b/src/user.h
@@ -2,6 +2,7 @@
 #define USER_H
 
 #include <stdint.h>
+#include "rooms.h"
 
 #define NAMESIZE 	16	/* username */
 #define PASSWDSIZE 	20	/* password (after encryption) */
@@ -70,11 +71,12 @@ struct person
 struct user {
 	int32_t posn;
 	struct person record;
+	struct room room;
 };
 
 extern int userdb_open(int flags);
 extern void userdb_write(struct user *user);
-extern void fetch_user(struct user *record, int32_t userposn);
+extern int fetch_user(struct user *record, int32_t userposn);
 extern void update_user(struct user *user);
 extern int update_user_fd(int fd, struct user *user);
 extern int is_old(const char *name, struct user *user);



View it on GitLab: https://projects.sucs.org/arthur/mw/compare/3f71da2332897f7b51e860a188d847ecfa2aafad...c30536af79c24adf5249ae06ad4c8e8e44b7ea67
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20151106/3b05a699/attachment-0001.html>


More information about the mw-devel mailing list