[mw-devel] MW3 r1348 - in trunk/src: . client server webclient

welshbyte at sucs.org welshbyte at sucs.org
Thu Oct 31 22:37:07 GMT 2013


Author: welshbyte
Date: 2013-10-31 22:37:07 +0000 (Thu, 31 Oct 2013)
New Revision: 1348

Added:
   trunk/src/who.c
Modified:
   trunk/src/Makefile
   trunk/src/client/js.c
   trunk/src/client/main.c
   trunk/src/client/mesg.c
   trunk/src/client/script_inst.c
   trunk/src/client/talker.c
   trunk/src/client/who.c
   trunk/src/files.c
   trunk/src/files.h
   trunk/src/ipc.c
   trunk/src/server/mwserv.c
   trunk/src/server/servsock.c
   trunk/src/webclient/comms.c
   trunk/src/webclient/import.c
   trunk/src/webclient/import.h
   trunk/src/who.h
Log:
Split out the who file stuff from files.* into who.* and tidy up the code a bit. Intention here is to modularise the who file code so that it can be replaced with a server-managed sqlite db (or whatever) easily in future. There's still a lot of code which does its own operations on the who file with read() and lseek() so a good next step will be to hide that code behind an iterator or similar.


Modified: trunk/src/Makefile
===================================================================
--- trunk/src/Makefile	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/Makefile	2013-10-31 22:37:07 UTC (rev 1348)
@@ -6,6 +6,7 @@
 	$(MAKE) -C server $@
 	$(MAKE) -C client $@
 	$(MAKE) -C webclient $@
+	$(MAKE) -C utils $@
 	ln -fs server/mwserv client/mw .
 
 libmw.a: $(CODE:.c=.o)
@@ -33,6 +34,7 @@
 	$(MAKE) -C client clean
 	$(MAKE) -C server clean
 	$(MAKE) -C webclient clean
+	$(MAKE) -C utils clean
 
 ifndef TESTDIR
 test testclean:

Modified: trunk/src/client/js.c
===================================================================
--- trunk/src/client/js.c	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/client/js.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -666,7 +666,7 @@
 	JSObject *res;
 	int n=0;
 
-	wfile=openwhofile(O_RDWR);
+	wfile=who_open(O_RDWR);
 	ufile=openuserfile(O_RDONLY);
 	if (wfile<0 || ufile<0) {
 		JS_ReportError(cx, "wholist() could not open userdb.");

Modified: trunk/src/client/main.c
===================================================================
--- trunk/src/client/main.c	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/client/main.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -1794,7 +1794,7 @@
 	{
 		if (wfile!=0) close(wfile);
 		if (ufile!=0) close(ufile);
-		wfile=openwhofile(O_RDONLY);
+		wfile=who_open(O_RDONLY);
 		ufile=openuserfile(O_RDONLY);
 		if (wfile<0 || ufile<0) return(NULL); /* whoops */
 
@@ -1837,7 +1837,7 @@
 	{
 		if (wfile!=0) close(wfile);
 		if (ufile!=0) close(ufile);
-		wfile=openwhofile(O_RDONLY);
+		wfile=who_open(O_RDONLY);
 		ufile=openuserfile(O_RDONLY);
 		if (wfile<0 || ufile<0) return(NULL); /* whoops */
 

Modified: trunk/src/client/mesg.c
===================================================================
--- trunk/src/client/mesg.c	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/client/mesg.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -60,7 +60,7 @@
         struct who w;
         int ufile,wfile;
 
-        wfile=openwhofile(O_RDONLY);
+        wfile=who_open(O_RDONLY);
         ufile=openuserfile(O_RDONLY);
         
         while (read(wfile,&w,sizeof(w)))
@@ -130,7 +130,7 @@
 	else if (state==2) mwlog("WIZ %s", text);
 	else if (state==5) mwlog("STATUS %s", text);
 
-	wfile=openwhofile(O_RDONLY);
+	wfile=who_open(O_RDONLY);
 	ufile=openuserfile(O_RDONLY);
 	 
 	while (read(wfile,&w,sizeof(w)))

Modified: trunk/src/client/script_inst.c
===================================================================
--- trunk/src/client/script_inst.c	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/client/script_inst.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -170,7 +170,7 @@
 
 		if (script_debug) escprintf("- %s: Getting idletime of user '%s'.\n", pc->inst->name, uname); 
 
-		wfile=openwhofile(O_RDWR);
+		wfile=who_open(O_RDWR);
 		ufile=openuserfile(O_RDONLY);
 		if (wfile<0 || ufile<0)
 		{
@@ -363,7 +363,7 @@
 	}
 	what=eval_arg(pc->argv[0], fargc, fargv);
 
-	wfile=openwhofile(O_RDWR);
+	wfile=who_open(O_RDWR);
 	ufile=openuserfile(O_RDONLY);
 	if (wfile<0 || ufile<0) return; /* whoops */
 
@@ -454,7 +454,7 @@
 
 		if (script_debug) escprintf("- %s: Getting room of user '%s'.\n", pc->inst->name, uname); 
 
-		wfile=openwhofile(O_RDWR);
+		wfile=who_open(O_RDWR);
 		ufile=openuserfile(O_RDONLY);
 		if (wfile<0 || ufile<0)
 		{
@@ -635,7 +635,7 @@
 			struct person p;
 			struct who w;   
 
-			wfile=openwhofile(O_RDWR);
+			wfile=who_open(O_RDWR);
 			ufile=openuserfile(O_RDONLY);
 			if (wfile<0 || ufile<0) return; /* whoops */
 

Modified: trunk/src/client/talker.c
===================================================================
--- trunk/src/client/talker.c	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/client/talker.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -1017,7 +1017,7 @@
 		struct person p;
 		struct who w;
 
-		wfile=openwhofile(O_RDWR);   
+		wfile=who_open(O_RDWR);   
 		ufile=openuserfile(O_RDONLY);
 		if (wfile<0 || ufile<0) return; /* whoops */
 			

Modified: trunk/src/client/who.c
===================================================================
--- trunk/src/client/who.c	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/client/who.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -60,7 +60,7 @@
 	if (u_god(user->status)) wiz=1; else wiz=0;
 	busy++;
 
-	wfile=openwhofile(O_RDWR);
+	wfile=who_open(O_RDWR);
 	ufile=openuserfile(O_RDONLY);
 	if (wfile<0 || ufile<0) return; /* whoops */
 
@@ -197,7 +197,7 @@
 	int wfile;
 	long found=-1;
 	
-	if ((wfile=openwhofile(O_RDONLY))<0) return(-1);
+	if ((wfile=who_open(O_RDONLY))<0) return(-1);
 
 	while (read(wfile,&w,sizeof(w)))
 	{
@@ -271,7 +271,7 @@
 	int count;
 	char buff[5];
 
-	file=openwhofile(O_RDWR);
+	file=who_open(O_RDWR);
 
 	if (file<0) return; /* whoops */
 	count=0;

Modified: trunk/src/files.c
===================================================================
--- trunk/src/files.c	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/files.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -92,85 +92,6 @@
 	close(outfile);
 }
 
-static int createwhofile(void)
-{
-	int fd;
-	fd = creat(WHOFILE, 0644);
-	if (fd < 0) {
-		perror("Create Who File");
-		return -1;
-	}
-	close(fd);
-	return 0;
-}
-
-int openwhofile(int flags)
-{
-	int x;
-	x=open(WHOFILE,flags);
-	if (x>=0) return x;
-
-	if (errno != ENOENT || createwhofile())
-	{
-		perror("Open Who File");
-		return(-1);
-	}
-
-	x=open(WHOFILE,flags);
-	if (x>=0) return x;
-
-	perror("Open Who File After Create");
-	return -1;
-}
-
-void who_add(int pid, int32_t posn)
-{
-	struct who rec;
-	int wfile;
-
-	if ((wfile=err_open(WHOFILE,O_RDWR|O_CREAT,0600))<0)
-		{exit(-1);}
-	Lock_File(wfile);
-	while(read(wfile,&rec,sizeof(rec))==sizeof(rec))
-	{
-		if(rec.pid<0)
-		{
-			lseek(wfile,-sizeof(rec),1);
-			break;
-		}
-	}
-	rec.pid=pid;
-	rec.posn=posn;
-	Lock_File(wfile);
-	write(wfile,&rec,sizeof(rec));
-	Unlock_File(wfile);
-	close(wfile);
-}
-
-void who_delete(int pid)
-{
-	int fd;
-	struct who temp;
-	fd=openwhofile(O_RDWR);
-	if(fd==-1)
-	{
-		perror("who_delete");
-		exit(1);
-	}
-	Lock_File(fd);
-	while(read(fd,&temp,sizeof(temp))==sizeof(temp))
-	{
-		if (temp.pid==pid) {
-			lseek(fd,(long)-sizeof(temp),1);
-			temp.pid=-1;
-			temp.posn=-1L;
-			write(fd,&temp,sizeof(temp));
-		}
-	}
-	Unlock_File(fd);
-	close(fd);
-}
-
 int openfolderfile(int mode)
 {
 	int x;

Modified: trunk/src/files.h
===================================================================
--- trunk/src/files.h	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/files.h	2013-10-31 22:37:07 UTC (rev 1348)
@@ -6,7 +6,6 @@
 
 #define FOLDERFILE  STATEDIR"/folders.bb"
 #define USERFILE    STATEDIR"/users.bb"
-#define WHOFILE     MSGDIR"/who.bb"
 
 void open_incoming_fifo(const char *host);
 void close_fifo(void);
@@ -16,9 +15,6 @@
 int nofolders(void);
 int openuserfile(int flags);
 void write_usr(struct person *record, int32_t *userposn);
-int openwhofile(int mode);
-void who_add(int pid, int32_t posn);
-void who_delete(int pid);
 void create_folder_file(void);
 int foldernumber(const char *name);
 int get_folder_entry(int file, struct folder *tmp);

Modified: trunk/src/ipc.c
===================================================================
--- trunk/src/ipc.c	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/ipc.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -164,7 +164,7 @@
 	struct person	user;
 	unsigned int	count = 0;
 
-	who_fd = openwhofile(O_RDONLY);
+	who_fd = who_open(O_RDONLY);
 	if (who_fd < 0) return 0;
 
 	users_fd = openuserfile(O_RDONLY);

Modified: trunk/src/server/mwserv.c
===================================================================
--- trunk/src/server/mwserv.c	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/server/mwserv.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -8,9 +8,10 @@
 #include <pwd.h>
 #include <string.h>
 #include <time.h>
-#include "../socket.h"
+#include <socket.h>
+#include <files.h>
+#include <who.h>
 #include "servsock.h"
-#include "../files.h"
 
 /* unused, but necessary to link other util functions */
 int idle = 0;
@@ -118,11 +119,9 @@
 	}
 
 	/* at server start nobody is logged in, wipe who list */
-	int fd = openwhofile(O_TRUNC|O_WRONLY);
-	if (fd < 0) {
-		fprintf(stderr, "Failed to open who list\n");
+	int fd = who_open(O_TRUNC|O_WRONLY);
+	if (fd < 0)
 		return 1;
-	}
 	close(fd);
 
 	if (!opts.foreground) daemon(0,0);

Modified: trunk/src/server/servsock.c
===================================================================
--- trunk/src/server/servsock.c	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/server/servsock.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -107,6 +107,7 @@
 void drop_connection(ipc_connection_t * conn)
 {
 	struct epoll_event ev;
+	int wfd;
 
 	printf("Drop connection fd=%d\n", conn->fd);
 	bzero(&ev, sizeof(ev));
@@ -114,7 +115,9 @@
 	list_del_init(&conn->list);
 	if (conn->fd != -1) close(conn->fd);
 	conn->fd = -1;
-	who_delete(conn->addr);
+	wfd = who_open(O_RDWR);
+	who_delete(wfd, conn->addr);
+	close(wfd);
 	conn->state = IPCSTATE_DELETED;
 	if (!list_empty(&conn->outq)) {
 		struct list_head *pos, *q;
@@ -258,6 +261,7 @@
 
 	/* client just told us who they are */
 	if (msg->head.type == FOURCC("HELO")) {
+		int wfd;
 		memcpy(&conn->addr, &msg->head.src, sizeof(conn->addr));
 		if (msg->bodylen < 4) {
 			printf("Invalid HELO from fd=%d. dropping.\n", conn->fd);
@@ -273,7 +277,9 @@
 		}
 		memcpy(&conn->user, msg->body, sizeof(conn->user));
 		printf("WHO Add: pid=%d posn=%d\n", conn->addr, conn->user);
-		who_add(conn->addr, conn->user);
+		wfd = who_open(O_RDWR);
+		who_add(wfd, conn->addr, conn->user);
+		close(wfd);
 		conn->state = IPCSTATE_VALID;
 		ipcmsg_destroy(msg);
 		return;
@@ -306,7 +312,7 @@
 	/* send message to everyone in the room */
 	if (msg->head.type == FOURCC("SAYR")) {
 		/* eventually this should be a server maintained list */
-		int who_fd = openwhofile(O_RDONLY);
+		int who_fd = who_open(O_RDONLY);
 		int users_fd = openuserfile(O_RDONLY);
 		struct who who;
 		struct person user;
@@ -348,7 +354,7 @@
 	/* message is for a specific username */
 	if (msg->head.type == FOURCC("SAYU")) {
 		/* eventually this should be a server maintained list */
-		int who_fd = openwhofile(O_RDONLY);
+		int who_fd = who_open(O_RDONLY);
 		int users_fd = openuserfile(O_RDONLY);
 		struct who who;
 		struct person user;
@@ -384,7 +390,7 @@
 	/* send message to everyone (unless this room is soundproof) */
 	if (msg->head.type == FOURCC("WALL")) {
 		/* eventually this should be a server maintained list */
-		int who_fd = openwhofile(O_RDONLY);
+		int who_fd = who_open(O_RDONLY);
 		int users_fd = openuserfile(O_RDONLY);
 		struct who who;
 		struct person user;

Modified: trunk/src/webclient/comms.c
===================================================================
--- trunk/src/webclient/comms.c	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/webclient/comms.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -453,7 +453,7 @@
 		mwstring *line = mws_new(2048);
 		int count=0;
 
-		wfile = openwhofile(O_RDONLY);
+		wfile = who_open(O_RDONLY);
 		ufile = openuserfile(O_RDONLY);
 
 		mws_add(line, "[");

Modified: trunk/src/webclient/import.c
===================================================================
--- trunk/src/webclient/import.c	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/webclient/import.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -70,7 +70,7 @@
         int wfile;
         long found=-1;
 
-        if ((wfile=openwhofile(O_RDONLY))<0) return(-1);
+        if ((wfile=who_open(O_RDONLY))<0) return(-1);
 
         while (read(wfile,&w,sizeof(w)))
         {

Modified: trunk/src/webclient/import.h
===================================================================
--- trunk/src/webclient/import.h	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/webclient/import.h	2013-10-31 22:37:07 UTC (rev 1348)
@@ -4,7 +4,6 @@
 unsigned long cm_flags(unsigned long cm, unsigned long flags, int mode);
 int32_t get_who_userposn(int pid);
 void fetch_user(struct person *record, int32_t userposn);
-void who_delete(int pid);
 char *quotetext(const char *a);
 void strip_quote(char *a);
 void broadcast_onoffcode(int code, int method, const char *sourceuser, const char *reason);

Added: trunk/src/who.c
===================================================================
--- trunk/src/who.c	                        (rev 0)
+++ trunk/src/who.c	2013-10-31 22:37:07 UTC (rev 1348)
@@ -0,0 +1,48 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+#include "who.h"
+#include "files.h"
+
+int who_open(int flags)
+{
+	int wfd = open(WHOFILE, flags|O_CREAT, 0644);
+	if (wfd < 0)
+		perror(WHOFILE);
+	return wfd;
+}
+
+void who_add(int wfd, int pid, int32_t posn)
+{
+	struct who rec;
+	Lock_File(wfd);
+	while(read(wfd,&rec,sizeof(rec))==sizeof(rec))
+	{
+		if(rec.pid<0)
+		{
+			lseek(wfd,-sizeof(rec), SEEK_CUR);
+			break;
+		}
+	}
+	rec.pid=pid;
+	rec.posn=posn;
+	write(wfd,&rec,sizeof(rec));
+	Unlock_File(wfd);
+}
+
+void who_delete(int wfd, int pid)
+{
+	struct who temp;
+	Lock_File(wfd);
+	while(read(wfd,&temp,sizeof(temp))==sizeof(temp))
+	{
+		if (temp.pid==pid) {
+			lseek(wfd,(long)-sizeof(temp),1);
+			temp.pid=-1;
+			temp.posn=-1L;
+			write(wfd,&temp,sizeof(temp));
+		}
+	}
+	Unlock_File(wfd);
+}

Modified: trunk/src/who.h
===================================================================
--- trunk/src/who.h	2013-10-31 20:52:36 UTC (rev 1347)
+++ trunk/src/who.h	2013-10-31 22:37:07 UTC (rev 1348)
@@ -3,10 +3,16 @@
 
 #include <stdint.h>
 
+#define WHOFILE MSGDIR"/who.bb"
+
 struct who 
 {
 	int32_t pid;
 	int32_t posn;
 };
 
+extern int who_open(int mode);
+extern void who_add(int wfd, int pid, int32_t posn);
+extern void who_delete(int wfd, int pid);
+
 #endif /* WHO_H */




More information about the mw-devel mailing list