[mw-devel] MW3 r1225 - trunk/src/webclient

arthur at sucs.org arthur at sucs.org
Fri Oct 15 10:51:13 BST 2010


Author: arthur
Date: 2010-10-15 10:51:13 +0100 (Fri, 15 Oct 2010)
New Revision: 1225

Modified:
   trunk/src/webclient/comms.c
   trunk/src/webclient/comms.h
   trunk/src/webclient/mwpoll.c
Log:
add auto register users, adding the -a option expects a second line of input which is the initial password to set.
add logging of whats said
only update idletime for user actions not polling


Modified: trunk/src/webclient/comms.c
===================================================================
--- trunk/src/webclient/comms.c	2010-10-13 16:21:56 UTC (rev 1224)
+++ trunk/src/webclient/comms.c	2010-10-15 09:51:13 UTC (rev 1225)
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <errno.h>
 #include <strings.h>
+#include <pwd.h>
 
 #include <talker_privs.h>
 #include <special.h>
@@ -351,7 +352,6 @@
 	printf("handle_command(%s)\n", buff);
 
 	lastcomm = time(NULL);
-	user->idletime = lastcomm;
 
 	if (strcasecmp(buff, "quit")==0) {
 		snprintf(buff, sizeof buff, "BYE");
@@ -385,6 +385,7 @@
 		int newroom;
 		oldroom = user->room;
 		newroom = atoi(&buff[8]);
+		user->idletime = lastcomm;
 		if (newroom == oldroom) {
 			return 1;
 		}else{
@@ -425,9 +426,11 @@
 		}else {
 			snprintf(line, sizeof line, "%s says: %s", user->name, msg);
 		}
+		mwlog("SAYTO %s %s", duser, msg);
                 ipc_send_to_username(duser, IPC_TEXT, line);
                 snprintf(buff, sizeof buff, "{\"status\":\"sayto sent to %s (%s)\"}", duser, msg);
                 send(co->fd, buff, strlen(buff), 0);
+		user->idletime = lastcomm;
                 return 1;			
 	}else
         if (co->authd && strncasecmp(buff, "emote ", 6)==0) {
@@ -436,6 +439,7 @@
                 memset(&f_info, 0, sizeof(f_info));
                 f_info.channel = user->room;
                 snprintf(line, sizeof line, "%s %s", user->name, &buff[6]);
+		mwlog("EMOTE %s", &buff[6]);
                 ipc_send_to_all(IPC_TEXT, line, send_filter_oneroom, &f_info);
 		//Strictly speaking, the following should check whether or not the room is hidden.
 		//If anyone cares about it enough to fix it, go right ahead
@@ -443,6 +447,7 @@
         	ipc_send_to_all(IPC_TEXT, line, send_filter_global, &f_info);
                 snprintf(buff, sizeof buff, "{\"status\":\"Emote Sent to channel %d\"}", user->room);
                 send(co->fd, buff, strlen(buff), 0);
+		user->idletime = lastcomm;
                 return 1;
         }else
 	if (co->authd && strcasecmp(buff, "who")==0) {
@@ -525,6 +530,7 @@
 		memset(&f_info, 0, sizeof(f_info));
 		f_info.channel = user->room;
 		snprintf(line, sizeof line, "%s: %s", user->name, &buff[4]);
+		mwlog("SAY %s", &buff[4]);
 		ipc_send_to_all(IPC_TEXT, line, send_filter_oneroom, &f_info);
                 //Strictly speaking, the following should check whether or not the room is hidden.
                 //If anyone cares about it enough to fix it, go right ahead
@@ -532,6 +538,7 @@
                 ipc_send_to_all(IPC_TEXT, line, send_filter_global, &f_info);
 		snprintf(buff, sizeof buff, "{\"status\":\"Message Sent to channel %d\"}", user->room);
 		send(co->fd, buff, strlen(buff), 0);
+		user->idletime = lastcomm;
 		return 1;
 	}else
 	if (co->authd && strcasecmp(buff, "fetch")==0) {
@@ -651,3 +658,35 @@
 	free(form);
 
 }
+
+void create_user(struct person *me, int *userposn, const char *username, const char *password)
+{
+	struct passwd *pw;
+	char salt[3];
+
+	salt[0] = 'a' + (rand() % 26);
+	salt[1] = 'a' + (rand() % 26);
+	salt[2] = 0;
+	bzero(me, sizeof(*me));
+	me->colour=0;
+	me->status |= (1<<6);
+	me->special |= (1<<12);
+	me->lastlogout = time(0);
+	me->timeout = 86400;
+	me->doing[0]=0;
+	me->folders[0] = SETALLLONG;
+	me->folders[1] = SETALLLONG;
+	me->status |= 1;
+	me->groups |= 1;
+	me->chatprivs |= CP_SCRIPT;
+	snprintf(me->name, sizeof(me->name), "%s", username);
+	snprintf(me->passwd, sizeof(me->passwd), "%s", crypt(password,salt));
+	if ((pw=getpwnam(username))!=NULL) {
+		snprintf(me->realname, sizeof(me->realname), "%s", pw->pw_gecos);
+		snprintf(me->contact, sizeof(me->contact), "%s at sucs.org", pw->pw_name);
+		mwlog("CREATED %s <%s>", me->realname, me->contact);
+	} else {
+		mwlog("CREATED Auto web user");
+	}
+	write_usr(me, userposn);
+}

Modified: trunk/src/webclient/comms.h
===================================================================
--- trunk/src/webclient/comms.h	2010-10-13 16:21:56 UTC (rev 1224)
+++ trunk/src/webclient/comms.h	2010-10-15 09:51:13 UTC (rev 1225)
@@ -6,3 +6,4 @@
 char *json_escape(char *original);
 void close_cmd(void);
 void talk_rawbcast(char *fmt, ...);
+void create_user(struct person *me, int *userposn, const char *username, const char *password);

Modified: trunk/src/webclient/mwpoll.c
===================================================================
--- trunk/src/webclient/mwpoll.c	2010-10-13 16:21:56 UTC (rev 1224)
+++ trunk/src/webclient/mwpoll.c	2010-10-15 09:51:13 UTC (rev 1225)
@@ -54,14 +54,16 @@
 	}
 	return 0;
 }
+			
 
 void usage(const char *name)
 {
-	printf("Usage: %s [-u username] [-c channel] [-ds]\n", name);
+	printf("Usage: %s [-u username] [-c channel] [-dsa]\n", name);
 	printf("-u username	Select username\n");
 	printf("-c channel	Initial channel to join\n");
 	printf("-d		Debug (foreground) mode\n");
 	printf("-s		Session mode for sucssite\n");
+	printf("-a		Auto create user if required\n");
 }
 
 int main(int argc, char ** argv)
@@ -72,9 +74,12 @@
 	int channel = 0;
 	int debug = 0;
 	int dbsession = 0;
+	int autocreate = 0;
+	char passwd[80];
+	char rawpw[80];
 
 	username = strdup("Arthur2");
-	while ((opt=getopt(argc,argv,"u:c:ds"))!=-1) {
+	while ((opt=getopt(argc,argv,"u:c:dsa"))!=-1) {
 		switch (opt) {
 			case 'u':
 				if (username) free(username);
@@ -89,6 +94,9 @@
 			case 's':
 				dbsession++;
 				break;
+			case 'a':
+				autocreate++;
+				break;
 			default:
 				usage(argv[0]);
 				return 1;
@@ -96,67 +104,84 @@
 		}
 	}
 
-	/* fetch the user record */
-	if (!is_old(&me, username, &userposn)) {
-		printf("User '%s' not found.\n", username);
-		return 1;
+
+	/* read the password / session string */
+	{ 
+		char *p;
+		if (fgets(passwd, sizeof passwd, stdin) == NULL) {
+			printf("Expected password\n");
+			return 1;
+		}
+		if ((p=strchr(passwd,'\r'))!=NULL) *p=0;
+		if ((p=strchr(passwd,'\n'))!=NULL) *p=0;
 	}
 
-	/* read a password and check it */
-	{
-		char buff[80];
+	/* if the option is set, read an initial password */
+	rawpw[0]=0;
+	if (autocreate) {
 		char *p;
-		char salt[3];
+		if (fgets(rawpw, sizeof rawpw, stdin) == NULL) {
+			printf("Expected initial password\n");
+			return 1;
+		}
+		if ((p=strchr(rawpw,'\r'))!=NULL) *p=0;
+		if ((p=strchr(rawpw,'\n'))!=NULL) *p=0;
+	}
 
-		if (fgets(buff, 80, stdin) == NULL) {
-			printf("Expected password\n");
+	/* fetch the user record */
+	if (!is_old(&me, username, &userposn)) {
+		if (autocreate) {
+			/* user doesnt exist, create them */
+			create_user(&me, &userposn, username, rawpw);
+		} else {
+			printf("User '%s' not found.\n", username);
 			return 1;
 		}
-		if ((p=strchr(buff,'\r'))!=NULL) *p=0;
-		if ((p=strchr(buff,'\n'))!=NULL) *p=0;
+	}
 
-		if (dbsession) {
-			PGconn *dbconn;
-			PGresult *dbres;
-			dbconn = PQconnectdb("dbname=sucssite");
-			if (PQstatus(dbconn) != CONNECTION_OK) {
-				printf("DB Connect error\n");
-				PQfinish(dbconn);
-				return 1;
-			}
-			const char *vals[1];
-			vals[0] = buff;
-			dbres = PQexecParams(dbconn, "SELECT username from session where hash = $1", 1, NULL, vals, NULL, NULL, 0);
-			if (PQresultStatus(dbres) != PGRES_TUPLES_OK)
-			{
-				printf("SELECT failed: %s", PQerrorMessage(dbconn));
-				PQclear(dbres);
-				PQfinish(dbconn);
-				return 1;
-			}
-			if (PQntuples(dbres) < 1) {
-				printf("Session not found\n");
-				PQclear(dbres);
-				PQfinish(dbconn);
-				return 1;
-			}
-			char *who = PQgetvalue(dbres, 0, 0);
-			if (strcasecmp(who, username)!=0) {
-				printf("Username mismatch '%s' != '%s'\n", username, who);
-				PQclear(dbres);
-				PQfinish(dbconn);
-				return 1;
-			}
-
+	/* check the session / password */
+	if (dbsession) {
+		PGconn *dbconn;
+		PGresult *dbres;
+		dbconn = PQconnectdb("dbname=sucssite");
+		if (PQstatus(dbconn) != CONNECTION_OK) {
+			printf("DB Connect error\n");
+			PQfinish(dbconn);
+			return 1;
+		}
+		const char *vals[1];
+		vals[0] = passwd;
+		dbres = PQexecParams(dbconn, "SELECT username from session where hash = $1", 1, NULL, vals, NULL, NULL, 0);
+		if (PQresultStatus(dbres) != PGRES_TUPLES_OK)
+		{
+			printf("SELECT failed: %s", PQerrorMessage(dbconn));
 			PQclear(dbres);
 			PQfinish(dbconn);
-		} else {
-			strncpy(salt, me.passwd, 2);
-			if (strcmp(crypt(buff,salt), me.passwd)!=0) {
-				printf("Incorrect password\n");
-				return 1;
-			}
+			return 1;
 		}
+		if (PQntuples(dbres) < 1) {
+			printf("Session not found\n");
+			PQclear(dbres);
+			PQfinish(dbconn);
+			return 1;
+		}
+		char *who = PQgetvalue(dbres, 0, 0);
+		if (strcasecmp(who, username)!=0) {
+			printf("Username mismatch '%s' != '%s'\n", username, who);
+			PQclear(dbres);
+			PQfinish(dbconn);
+			return 1;
+		}
+
+		PQclear(dbres);
+		PQfinish(dbconn);
+	} else {
+		char salt[3];
+		strncpy(salt, me.passwd, 2);
+		if (strcmp(crypt(passwd,salt), me.passwd)!=0) {
+			printf("Incorrect password\n");
+			return 1;
+		}
 	}
 
 	/* mark as in talker */




More information about the mw-devel mailing list