Browse Source

Creating new users

Bob Mottram 3 years ago
parent
commit
19adf540ae
4 changed files with 94 additions and 15 deletions
  1. 3 0
      Makefile
  2. 4 0
      README.md
  3. BIN
      man/toxid.1.gz
  4. 87 15
      src/main.c

+ 3 - 0
Makefile

@@ -5,9 +5,11 @@ ARCH_TYPE=`uname -m`
 PREFIX?=/usr/local
 
 all:
+	rm -f src/*.plist
 	gcc -Wall -pedantic -std=gnu1x -O3 -o ${APP} -Isrc src/*.c -ltoxcore
 
 debug:
+	rm -f src/*.plist
 	gcc -Wall -pedantic -std=gnu11 -g -o ${APP} -Isrc src/*.c -ltoxcore
 
 install:
@@ -30,4 +32,5 @@ uninstall:
 	rm -f ${DESTDIR}${PREFIX}/bin/lstoxusers
 
 clean:
+	rm -f src/*.plist
 	rm -f toxid

+ 4 - 0
README.md

@@ -20,3 +20,7 @@ To advertise your Tox ID via an Avahi service
 Then on another system on the same network to discover Tox IDs:
 
     lstox
+
+To create a new tox user named "bar" which will exist in /home/foo/.config.tox
+
+    toxid -u foo -n bar

BIN
man/toxid.1.gz


+ 87 - 15
src/main.c

@@ -144,33 +144,87 @@ int tox_data_file_exists(char * data_file)
 	return 0;
 }
 
-int create_new_user(char * username)
+Tox *create_tox(char * savedata_filename)
 {
-	/*
-	TOX_ERR_NEW err;
-	Tox *m=NULL;
-	struct Tox_Options *options;
+	Tox *tox;
 
-	options->savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
-    options->proxy_type = TOX_PROXY_TYPE_NONE;
-	m = tox_new(options, &err);
-	if (err != TOX_ERR_NEW_OK) {
-		fprintf(stderr, "tox_new failed with error %d\n", err);
-		return 1;
+	struct Tox_Options options;
+
+	tox_options_default(&options);
+
+	FILE *f = fopen(savedata_filename, "rb");
+	if (f) {
+		fseek(f, 0, SEEK_END);
+		long fsize = ftell(f);
+		fseek(f, 0, SEEK_SET);
+
+		char *savedata = malloc(fsize);
+
+		fread(savedata, fsize, 1, f);
+		fclose(f);
+
+		options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
+		options.savedata_data = (uint8_t *)savedata;
+		options.savedata_length = fsize;
+
+		tox = tox_new(&options, NULL);
+
+		free(savedata);
+	} else {
+		tox = tox_new(&options, NULL);
 	}
-	*/
+
+	return tox;
+}
+
+void update_savedata_file(const Tox *tox,
+						  char * filename)
+{
+	char savedata_tmp_filename[256];
+	size_t size = tox_get_savedata_size(tox);
+	char *savedata = malloc(size);
+
+	tox_get_savedata(tox, (uint8_t *)savedata);
+
+	sprintf(savedata_tmp_filename,"%s.tmp", filename);
+	FILE *f = fopen(savedata_tmp_filename, "wb");
+	if (f) {
+		fwrite(savedata, size, 1, f);
+		fclose(f);
+	}
+
+	rename(savedata_tmp_filename, filename);
+	free(savedata);
+}
+
+int create_new_user(char * os_username, char * tox_username)
+{
+	char filename[256];
+	char commandstr[256];
+	Tox *tox = create_tox(tox_username);
+
+	tox_self_set_name(tox, (const uint8_t *)tox_username, strlen(tox_username), NULL);
+
+	/* create the directory if needed */
+	sprintf(commandstr,"mkdir -p /home/%s/.config/tox", os_username);
+	system(commandstr);
+
+	sprintf(filename, "/home/%s/.config/tox/%s.tox", os_username, tox_username);
+	update_savedata_file(tox, filename);
+	tox_kill(tox);
 	return 0;
 }
 
 int main(int argc, char* argv[])
 {
 	char data_file[256], address[TOX_ADDRESS_SIZE];
-	char tox_username[256], username[256];
+	char tox_username[256], username[256], new_username[256];
 	struct Tox_Options tox_opts;
 	Tox * tox;
 	int i;
 	char search[256];
 	int show_user=0;
+	int new_user=0;
 
 	/* get the user's home directory */
 	struct passwd *pw = getpwuid(getuid());
@@ -181,6 +235,7 @@ int main(int argc, char* argv[])
 
 	tox_username[0] = 0;
 	username[0] = 0;
+	new_username[0] = 0;
 
 	/* parse the command line */
 	for (i = 1; i < argc; i++) {
@@ -197,8 +252,9 @@ int main(int argc, char* argv[])
 			}
 			if ((strcmp(argv[i], "--new") == 0) ||
 				(strcmp(argv[i], "-n") == 0)) {
-				create_new_user(argv[i+1]);
-				return 0;
+				sprintf(new_username, "%s", argv[i+1]);
+				new_user=1;
+				i++;
 			}
 		}
 		if ((strcmp(argv[i], "--showuser") == 0) ||
@@ -208,6 +264,22 @@ int main(int argc, char* argv[])
 		}
 	}
 
+	/* create a new user */
+	if (new_user==1) {
+		if (username[0] != 0) {
+			if (strlen(new_username) > 0) {
+				create_new_user(username, new_username);
+				return 0;
+			}
+			else {
+				printf("Specify the tox user with --user\n");
+				return 6272;
+			}
+		}
+		printf("Specify the operating system user with --user\n");
+		return 6245;
+	}
+
 	/* path for the tox user data file */
 	if (username[0] != 0) {
 		if (tox_username[0] != 0) {