Ejemplo n.º 1
0
// interacter thread
int interacter()
{
    char input;
    char tmp[100];
    
    printf(HELPINFO);

    while (1) {
        printf("ipmsg~> ");
/*        if (fflush(stdin) != 0) {
            gets(stdin);
        }
*/
        fgets(tmp, 100, stdin);
        input = *tmp;
        switch(input) {
            case 'h':
                printf(HELPINFO);
                break;
            case 'l':
                pthread_mutex_lock(&user_lock);
                list_users();
                pthread_mutex_unlock(&user_lock);
                break;
            case 'r':
                login();
                pthread_mutex_lock(&user_lock);
                list_users();
                pthread_mutex_unlock(&user_lock);
                break;
            case 't':
                talkto_user();
                break;
            case 's':
                send_files();
                break;
            case 'q':
                logout();
                printf("Bye!!!\n");
                exit(0);
            case 10:
                break;
            default:
                printf("Input error command, please input again.\n");
                break;
        }
    }

    return 0;
}
Ejemplo n.º 2
0
int	handle_client_connection(t_client_info *client, t_client_info *list,
				 char *cmd, t_channel **channels)
{
  char	*buff;

  buff = strdup(cmd);
  bzero(cmd, 4096);
  if (strncmp(buff, "PASS ", 5) == 0)
    return (0);
  else if (strncmp(buff, "NICK ", 5) == 0)
    return (handle_nick(client, buff + 5, list));
  else if (strncmp(buff, "USER ", 5) == 0)
    return (0);
  else if (strncmp(buff, "JOIN ", 5) == 0)
    return (join_channel(client, buff + 5, channels));
  else if (strncmp(buff, "PART ", 5) == 0)
    return (part_channel(client, channels));
  else if (strncmp(buff, "LIST", 4) == 0)
    return (list_channels(*channels, cmd, client));
  else if (strncmp(buff, "USERS", 5) == 0)
    return (list_users(client, *channels));
  else if (strncmp(buff, "MSG", 3) == 0)
    return (private_message(client, list, buff));
  else
    return (send_messages(client, *channels, buff));
  return (0);
}
Ejemplo n.º 3
0
/* 
 * Read and process calendar commands
 * Return:  -1 for quit command
 *          0 otherwise
 */
int process_args(int cmd_argc, char **cmd_argv, Calendar **calendar_list_ptr, User **user_list_ptr) {

    Calendar *calendar_list = *calendar_list_ptr; 
    User *user_list = *user_list_ptr;

    if (cmd_argc <= 0) {
        return 0;
    } else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
        return -1;
        
    } else if (strcmp(cmd_argv[0], "add_calendar") == 0 && cmd_argc == 2) {
        if (add_calendar(calendar_list_ptr, cmd_argv[1]) == -1) {
            error("Calendar by this name already exists");
        }
        
    } else if (strcmp(cmd_argv[0], "list_calendars") == 0 && cmd_argc == 1) {
        printf("%s", list_calendars(calendar_list));
        
    } else if (strcmp(cmd_argv[0], "add_event") == 0 && cmd_argc >= 4) {
        // Parameters for convert_time are the values in the array
        // cmd_argv but starting at cmd_argv[3]. The first 3 are not
        // part of the time.
        // So subtract these 3 from the count and pass the pointer 
        // to element 3 (where the first is element 0).
        time_t time = convert_time(cmd_argc-3, &cmd_argv[3]);

        if (add_event(calendar_list, cmd_argv[1], cmd_argv[2], time) == -1) {
           error("Calendar by this name does not exist");
        }

    } else if (strcmp(cmd_argv[0], "list_events") == 0 && cmd_argc == 2) {
        printf("%s", list_events(calendar_list, cmd_argv[1]));

    } else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) {
        if (add_user(user_list_ptr, cmd_argv[1]) == -1) {
                error("User by this name already exists");
        }

    } else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) {
        printf("%s", list_users(user_list));
        
    } else if (strcmp(cmd_argv[0], "subscribe") == 0 && cmd_argc == 3) {
        int return_code = subscribe(user_list, calendar_list, cmd_argv[1], cmd_argv[2]);
        if (return_code == -1) {
           error("User by this name does not exist");
        } else if (return_code == -2) {
           error("Calendar by this name does not exist");
        } else if (return_code == -3) {
           error("This user is already subscribed to this calendar");
        }
      
    } else {
        error("Incorrect syntax");
    }
    return 0;
}
Ejemplo n.º 4
0
Archivo: main.c Proyecto: bq/qeo-core
static void handle_command(const char *cmd,
                           org_qeo_sample_simplechat_ChatMessage_t *chat_msg,
                           int *done,
                           qeo_event_writer_t *writer,
                           qeo_event_reader_t *reader)
{
    if (0 == strcmp("bye", cmd)) {
        *done = 1;
    }
    else if (0 == strcmp("help", cmd)) {
        help();
    }
    else if (0 == strncmp("name ", cmd, 5)) {
        free(chat_msg->from);
        chat_msg->from = strdup(&cmd[5]);
    }
    else if (0 == strcmp("users", cmd)) {
        list_users();
    }
    else if (0 == strncmp("privatewrite ", cmd, 8)) {
        int64_t uid = strtoll(&cmd[13], NULL, 10);

        if (!uid_exists(uid)) {
            printf("Unknown user ID %" PRId64 "\n", uid);
        }
        else {
            _single_peer = true;
            _single_uid = uid;
            qeo_event_writer_policy_update(writer);
        }
    }
    else if (0 == strncmp("privateread ", cmd, 8)) {
        int64_t uid = strtoll(&cmd[12], NULL, 10);

        if (!uid_exists(uid)) {
            printf("Unknown user ID %" PRId64 "\n", uid);
        }
        else {
            _single_peer = true;
            _single_uid = uid;
            qeo_event_reader_policy_update(reader);
        }
    }
    else if (0 == strcmp("public", cmd)) {
        _single_peer = false;
        qeo_event_reader_policy_update(reader);
        qeo_event_writer_policy_update(writer);
    }
}
Ejemplo n.º 5
0
void main(void)
{
    char filename[80];


    printf("\n Legnth of User structure now = %d bytes\n\n",sizeof(struct user_data_record));

    printf("User file LISTER\n\nEnter filename\n\n--> ");


    scanf("%s",filename);

    printf("-> Reading > %s <\n\n\n",filename);

    list_users(filename);

}
Ejemplo n.º 6
0
StartWindow::StartWindow(QWidget *parent) 
	:QWidget(parent)
{
	this->setWindowTitle("Serious Game");

	create = new QPushButton(tr("Create profile"));
	select = new QPushButton(tr("Select profile"));
	quit = new QPushButton(tr("Quit"));

	// layout
	vbox = new QVBoxLayout(this);
	vbox->addWidget(create);
	vbox->addWidget(select);
	vbox->addWidget(quit);

	// signals connection
	connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
	connect(create, SIGNAL(clicked()), this, SLOT(new_user_profile()));
	connect(select, SIGNAL(clicked()), this, SLOT(list_users()));
	this->show();
}
Ejemplo n.º 7
0
void do_retr(client *cl)
{
  char *queryname;
  int status;

  if (cl->req.mr_argc < 1)
    {
      client_reply(cl, MR_ARGS);
      com_err(whoami, MR_ARGS, "got nameless query");
      return;
    }
  queryname = cl->req.mr_argv[0];
  newqueries++;

  if (!strcmp(queryname, "_list_users"))
    status = list_users(cl);
  else
    status = mr_process_query(cl, queryname, cl->req.mr_argc - 1,
			      cl->req.mr_argv + 1, retr_callback, cl);

  client_reply(cl, status);

  com_err(whoami, 0, "Query complete.");
}
Ejemplo n.º 8
0
int
main(int argc, char *argv[])
{
  char *sql_db = NULL;
  sqlite3* db;
  char *username = NULL;
  struct user user;
  enum manage_action action = MANAGE_ACTION_HELP;
  int opt;
  char privid[OTP_PRIVID_HEX_LEN];
  unsigned char *privid_bin;
  int temp, ret;
  struct otp_data* data;
  char digest_name[DIGEST_NAME_MAX_SIZE];
  char *ctemp;

  while((opt = getopt(argc, argv, "hs:lg:r:a:c")) != -1) {
    switch(opt) {
      case 'h':
        usage(0);
        break;
      case 's':
        sql_db = optarg;
        break;
      case 'l':
        if (action != MANAGE_ACTION_HELP) {
          usage(1);
        }
        action = MANAGE_ACTION_LIST;
        break;
      case 'g':
        if (action != MANAGE_ACTION_HELP) {
          usage(1);
        }
        action = MANAGE_ACTION_GET;
        username = optarg;
        break;
      case 'r':
        if (action != MANAGE_ACTION_HELP) {
          usage(1);
        }
        action = MANAGE_ACTION_DELETE;
        username = optarg;
        break;
      case 'a':
        if (action != MANAGE_ACTION_HELP) {
          usage(1);
        }
        action = MANAGE_ACTION_ADD;
        username = optarg;
        break;
      case 'c':
        if (action != MANAGE_ACTION_HELP) {
          usage(1);
        }
        action = MANAGE_ACTION_CREATE;
        break;
      default:
        usage(0);
     }
  }

  if(argc > optind) {
    usage(1);
  }

  if (action == MANAGE_ACTION_HELP) {
    usage(0);
  }

  if (sql_db == NULL) {
    usage(2);
  }

  if (forget_real_credentials() != 0) {
    printf("Unable to fix uid/gid\n");
    return -EPERM;
  }

  db = init(sql_db);
  if (db == NULL) {
    printf("Unable to open the database\n");
    return 1;
  }
  switch(action) {
    case MANAGE_ACTION_HELP:
       /* Already done */
      break;
    case MANAGE_ACTION_LIST:
      list_users(db);
      sql_close(db);
      return 0;
    case MANAGE_ACTION_CREATE:
      create_database(db);
      return 0;
    case MANAGE_ACTION_GET:
    case MANAGE_ACTION_ADD:
    case MANAGE_ACTION_DELETE:
      /* Later */
      break;
  }
  if (username == NULL) {
    usage(3);
  }
  if (verify_user(username, strlen(username), &user) != 0) {
    printf("Unauthorized char in the username");
    return OTP_ERR;
  }

  switch(action) {
    case MANAGE_ACTION_HELP:
    case MANAGE_ACTION_LIST:
    case MANAGE_ACTION_CREATE:
      /* Already done */
      break;
    case MANAGE_ACTION_GET:
      data = get_otp_data(db, &user);
      if (data == NULL) {
        printf("No such user\n");
        break;
      }
      printf("User '%.*s':\n", (unsigned int) user.len, user.name);
      printf("Public ID  : %.*s\n", (int) OTP_PUB_ID_HEX_LEN, data->pubid);
      printf("Private Key: %.*s\n", (int) OTP_KEY_HEX_LEN, data->key);
      printf("Private ID digest: %s\n", data->digest_name);
      printf("Private ID hash:   %s\n", data->privid_hash);
      free(data);
      break;
    case MANAGE_ACTION_DELETE:
      for (temp = 0; temp < MAX_RETRIES; ++temp) {
        ret = try_start_transaction(db);
        switch (ret) {
          case OTP_SQL_ERR:
            printf("SQL error during the transaction initialisation");
            goto free_db;
          case OTP_SQL_MAY_RETRY:
            break;
          case OTP_SQL_OK:
            ret = try_delete_credentials(db, &user);
            switch (ret) {
              case OTP_SQL_ERR:
                printf("SQL error while trying to remove user");
                goto free_db;
              case OTP_SQL_MAY_RETRY:
                break;
              case OTP_SQL_OK:
                ret = try_end_transaction(db);
                switch (ret) {
                  case OTP_SQL_MAY_RETRY:
                    break;
                  case OTP_SQL_ERR:
                    printf("SQL error when trying to commit the transaction");
                    goto free_db;
                  case OTP_SQL_OK:
                    sql_close(db);
                    return 0;
                }
            }
        }
      }
      printf("Unable to remove user (Database busy)\n");
      break;
    case MANAGE_ACTION_ADD:
      data = calloc(sizeof(struct otp_data), 1ul);
      if (data == NULL) {
        printf("Malloc error\n");
        goto free_db;
      }
      if (read_input_word(data->pubid, OTP_PUB_ID_HEX_LEN, "Public ID")) {
        goto free_data;
      }
      if (check_modhex(data->pubid, OTP_PUB_ID_HEX_LEN) != 0) {
        printf("Non hex character in input, please retry\n");
        goto free_data;
      }

      if (read_input_word(data->key, OTP_KEY_HEX_LEN, "AES key")) {
        goto free_data;
      }
      if (check_hex(data->key, (int) OTP_KEY_HEX_LEN) != 0) {
        printf("Non hex character in input, please retry\n");
        goto free_data;
      }

      if (read_input_word(privid, OTP_PRIVID_HEX_LEN, "Private ID")) {
        goto free_data;
      }
      if (check_hex(privid, (int) OTP_PRIVID_HEX_LEN) != 0) {
        printf("Non hex character in input, please retry\n");
        goto free_data;
      }
      privid_bin = hex2bin(privid, OTP_PRIVID_HEX_LEN);
      if (privid_bin == NULL) {
        printf("Malloc error (bis)\n");
        goto free_data;
      }

      printf("Please Specify a valid digest algorithm [%s]\n", DEFAULT_DIGEST);
      memset(digest_name, 0, (size_t) DIGEST_NAME_MAX_SIZE);
      ctemp = fgets(digest_name, DIGEST_NAME_MAX_SIZE, stdin);
      if (ctemp == NULL) {
        printf("Unable to read input\n");
        goto free_data;
      }
      if (digest_name[DIGEST_NAME_MAX_SIZE - 1] != 0 && digest_name[DIGEST_NAME_MAX_SIZE - 1] != '\n') {
        printf("Digest algorithm name too long, please retry\n");
        goto free_data;
      }
      if (digest_name[0] == '\n') {
        data->digest_name = strdup(DEFAULT_DIGEST);
      } else {
        ctemp = memchr(digest_name, '\n', (size_t) DIGEST_NAME_MAX_SIZE);
        if (ctemp != NULL) {
          *ctemp = '\0';
        }
        data->digest_name = digest_name;
      }
      ctemp = (char*)compute_hash(data->digest_name, (char*)privid_bin, OTP_PRIVID_BIN_LEN);
      if (ctemp == NULL) {
        goto free_data;
      }
      data->privid_hash = bin2hex(ctemp, strlen(ctemp));
      if (data->privid_hash == NULL) {
        goto free_data;
      }
      printf("New user :\n");
      printf("Name: '%.*s':\n", (unsigned int) user.len, user.name);
      printf("Public ID  : %.*s\n", (int) OTP_PUB_ID_HEX_LEN, data->pubid);
      printf("Private Key: %.*s\n", (int) OTP_KEY_HEX_LEN, data->key);
      printf("Private ID:        %.*s\n", (int) OTP_PRIVID_HEX_LEN, privid);
      printf("Private ID digest: %s\n", data->digest_name);
      printf("Private ID hash:   %s\n", data->privid_hash);
      printf("Press enter to create this new user\n");
      if (getc(stdin) != '\n') {
        goto free_data;
      }
      for (temp = 0; temp < MAX_RETRIES; ++temp) {
        ret = try_start_transaction(db);
        switch (ret) {
          case OTP_SQL_ERR:
            printf("SQL error during the transaction initialisation");
            goto free_data;
          case OTP_SQL_MAY_RETRY:
            break;
          case OTP_SQL_OK:
            ret = try_create_credentials(db, data, &user);
            switch (ret) {
              case OTP_SQL_ERR:
                printf("SQL error while trying to add the user");
                goto free_data;
              case OTP_SQL_MAY_RETRY:
                break;
              case OTP_SQL_OK:
                ret = try_end_transaction(db);
                switch (ret) {
                  case OTP_SQL_MAY_RETRY:
                    break;
                  case OTP_SQL_ERR:
                    printf("SQL error when trying to commit the transaction");
                    goto free_data;
                  case OTP_SQL_OK:
                    goto free_data;
                }
            }
        }
      }
      printf("Unable to create user (Database busy)\n");
      break;
  }
  goto free_db;

free_data:
  free_otp_data(data);
free_db:
  sql_close(db);
  return 0;
}
Ejemplo n.º 9
0
int
main(int argc, char * const argv[])
{
	int o;

	while ((o = getopt(argc, argv, "adg:l:mopstux")) != -1)
		switch (o) {
		case 'a':
			a_flag = 1;
			break;
		case 'd':
			everything = 0;
			d_flag = 1;
			break;
		case 'g':
			everything = 0;
			g_args = optarg;
			break;
		case 'l':
			everything = 0;
			l_args = optarg;
			break;
		case 'm':
			m_flag = 1;
			break;
		case 'o':
			o_flag = 1;
			break;
		case 'p':
			everything = 0;
			p_flag = 1;
			break;
		case 's':
			everything = 0;
			s_flag = 1;
			break;
		case 't':
			t_flag = 1;
			break;
		case 'u':
			everything = 0;
			u_flag = 1;
			break;
		case 'x':
			x_flag = 1;
			break;
		default:
			usage();
		}

	argc -= optind;
	argv += optind;

	if (argc > 0)
		usage();

	get_groups();
	get_users();
	select_users();
	sort_users();
	list_users();
	exit(0);
}
Ejemplo n.º 10
0
int handle_A_input()
{
	char buf[3] = {0};
	// same for all users
	// handle create user
	// handle login
	// handle exit
	size_t l = 0;
	while (l < 1)
		l = receive_until( buf, '\n', 2);

	if (buf[0] == '1')
	{
		// CREATE USER
		create_user(0);
	}
	else if (buf[0] == '2')
	{
		// LOGIN

		list_users();

		char name[MAX_NAME_LEN];
		char pswd[MAX_PASS_LEN];

		printf("Username: \n");
		size_t len = receive_until( name, '\n', MAX_NAME_LEN - 1);
		name[len++] = 0;
		printf("Password: \n");
		len = receive_until( pswd, '\n', MAX_PASS_LEN - 1);
		pswd[len++] = 0;

		msg_count_login = 0;

		if (login(name, pswd) == 1)
		{
			// successful login
			
			return 2;
		}
		else
		{
			printf("Bad login.\n");
			bad_login_count++;
			if (bad_login_count >= 3)
			{
				create_user(bad_login_count); // this allows a user to be re-created
				bad_login_count = 0;
			}
		}
	}
	else if (buf[0] == '3')
	{
		// EXIT
		printf("The end.\n");
		_terminate(0);
	}
	else
		printf("unknown input: @c @c\n", buf[0], buf[1]);
	return 0;
}
Ejemplo n.º 11
0
int main(int argc, char **argv)
{

	user_chat_box_t user_list[MAX_USERS];
	server_ctrl_t server_shell;
	char arg1[MSG_SIZE];
	char arg2[MSG_SIZE];
	char command[MSG_SIZE];
	int cmd_num;
	char* str_pid;
	char* user_name;
	int k;
	int i;
	int idx;
	/* open non-blocking bi-directional pipes for communication with server shell */

	initialize_users(user_list);
	pipe(server_shell.ptoc);
	pipe(server_shell.ctop);
	fcntl(server_shell.ptoc[0],F_SETFL,O_NONBLOCK);
	fcntl(server_shell.ctop[0],F_SETFL,O_NONBLOCK);

	/* Fork the server shell */	
	pid_t pid = fork();
	if (pid < 0) 
		printf("Fork error.\n");
	else if (pid == 0)
	{
		sprintf(arg1, "%d", server_shell.ptoc[0]);
		sprintf(arg2, "%d", server_shell.ctop[1]);
		if(execl("./shell", "shell", arg1, arg2, "Server", (char *)0) < 0){
			perror("execl error");
		}
	}
		/* 
	 	 * Inside the child.
		 * Start server's shell.
	 	 * exec the SHELL program with the required program arguments.
	 	 */

	/* Inside the parent. This will be the most important part of this program. */

		/* Start a loop which runs every 1000 usecs.
	 	 * The loop should read messages from the server shell, parse them using the 
	 	 * parse_command() function and take the appropriate actions. */
	else 
	{	
		server_shell.pid = pid;
		while (1) 
		{
			/* Let the CPU breathe */
			usleep(1000);
			/* 
		 	 * 1. Read the message from server's shell, if any
		 	 * 2. Parse the command
		 	 * 3. Begin switch statement to identify command and take appropriate action
		 	 *
		 	 * 		List of commands to handle here:
		 	 * 			CHILD_PID
		 	 * 			LIST_USERS
		 	 * 			ADD_USER
		 	 * 			KICK
		 	 * 			EXIT
		 	 * 			BROADCAST 
		 	 */

			/* Fork a process if a user was added (ADD_USER) */
				/* Inside the child */
				/*
			 	 * Start an xterm with shell program running inside it.
			 	 * execl(XTERM_PATH, XTERM, "+hold", "-e", <path for the SHELL program>, ..<rest of the arguments for the shell program>..);
			 	 */
		 	if (read(server_shell.ctop[0], command, MSG_SIZE) > 0)
		 	{
		 		cmd_num = parse_command(command);
		 		switch (cmd_num)
		 		{
		 			case CHILD_PID:
		 				str_pid = extract_name(CHILD_PID, command);
		 				server_shell.child_pid = atoi(str_pid);
		 				break;
		 			case LIST_USERS:
		 				list_users(user_list, server_shell.ptoc[1]);
		 				break;
		 			case ADD_USER:
		 				add_user(user_list, command, server_shell.ptoc[1]);
		 				break;
		 			case KICK:
		 				user_name = extract_name(KICK, command);
		 				if ((idx = find_user_index(user_list, user_name)) == -1)
		 					break;
		 				cleanup_user(idx, user_list);
		 				break;
		 			case EXIT:
		 				cleanup_users(user_list);
		 				cleanup_server(server_shell);
		 				break;
		 			case BROADCAST:
		 				broadcast_msg(user_list, command, server_shell.ptoc[1], "Server");
		 				break;
		 		}
		 		if(cmd_num == EXIT)break;
		 	}
		 	
			/* Back to our main while loop for the "parent" */
			/* 
		 	 * Now read messages from the user shells (ie. LOOP) if any, then:
		 	 * 		1. Parse the command
		 	 * 		2. Begin switch statement to identify command and take appropriate action
		 	 *
		 	 * 		List of commands to handle here:
		 	 * 			CHILD_PID
		 	 * 			LIST_USERS
		 	 * 			P2P
		 	 * 			EXIT
		 	 * 			BROADCAST
		 	 *
		 	 * 		3. You may use the failure of pipe read command to check if the 
		 	 * 		user chat windows has been closed. (Remember waitpid with WNOHANG 
		 	 * 		from recitations?)
		 	 * 		Cleanup user if the window is indeed closed.
		 	 */
		 	int nsize;
		 	for (i = 0; i < MAX_USERS; i++)
		 	{
		 		if (user_list[i].status == SLOT_EMPTY)
					continue;
		 		if ((nsize = read(user_list[i].ctop[0], command, MSG_SIZE)) > 0)
		 		{
			 		cmd_num = parse_command(command);
			 		switch (cmd_num)
			 		{
			 			case CHILD_PID:
			 				str_pid = extract_name(CHILD_PID, command);
			 				user_list[i].child_pid = atoi(str_pid);
			 				break;
			 			case LIST_USERS:
			 				list_users(user_list, user_list[i].ptoc[1]);
			 				break;
			 			case P2P:
			 				send_p2p_msg(i, user_list, command);
			 				break;
			 			case EXIT:
			 				cleanup_user(i, user_list);
			 				break;
			 			case BROADCAST:
			 				broadcast_msg(user_list, command, server_shell.ptoc[1], user_list[i].name);
			 				break;
			 		}
		 		} else if(nsize == 0) {
		 			int status;
		 			if(waitpid(user_list[i].child_pid, &status, WNOHANG) < 0) {
		 				cleanup_user(i, user_list);
		 			}
		 		}
		 	}
		}
	}	/* while loop ends when server shell sees the \exit command */

	return 0;
}
/* 
 * Read and process commands
 * Return:  -1 for quit command
 *          0 otherwise
 */
int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) {
    User *user_list = *user_list_ptr;

    if (cmd_argc <= 0) {
        return 0;
    } else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
        return -1;
    } else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) {
        switch (create_user(cmd_argv[1], user_list_ptr)) {
            case 1:
                error("user by this name already exists");
                break;
            case 2:
                error("username is too long");
                break;
        }
    } else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) {
        list_users(user_list);
    } else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) {
        User *user = find_user(cmd_argv[1], user_list);
        if (user == NULL) {
            error("user not found");
        }

        if (update_pic(user, cmd_argv[2]) == 1) {
            error("file not found");
        }
    } else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) {
        if (delete_user(cmd_argv[1], user_list_ptr) == 1) {
            error("user by this name does not exist");
        }
    } else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) {
        switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) {
            case 1:
                error("users are already friends");
                break;
            case 2:
                error("at least one user you entered has the max number of friends");
                break;
            case 3:
                error("you must enter two different users");
                break;
            case 4:
                error("at least one user you entered does not exist");
                break;
        }
    } else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) {
        // first determine how long a string we need
        int space_needed = 0;
        for (int i = 3; i < cmd_argc; i++) {
            space_needed += strlen(cmd_argv[i]) + 1;
        }

        // allocate the space
        char *contents = malloc(space_needed);

        // copy in the bits to make a single string
        strcpy(contents, cmd_argv[3]);
        for (int i = 4; i < cmd_argc; i++) {
            strcat(contents, " ");
            strcat(contents, cmd_argv[i]);
        }

        User *author = find_user(cmd_argv[1], user_list);
        User *target = find_user(cmd_argv[2], user_list);
        switch (make_post(author, target, contents)) {
            case 1:
                error("the users are not friends");
                break;
            case 2:
                error("at least one user you entered does not exist");
                break;
        }
    } else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) {
        User *user = find_user(cmd_argv[1], user_list);
        if (print_user(user) == 1) {
            error("user not found");
        }
    } else {
        error("Incorrect syntax");
    }
    return 0;
}
Ejemplo n.º 13
0
/* 
 * Read and process buxfer commands
 * Return -1 to indicate user has quit
 * Return 1 to indicate broadcast message must be sent to all other users
 * Return 0 otherwise
 */
int process_args(int cmd_argc, char **cmd_argv, Client *c, Pool *p) 
{
    Group *g;
    char *buf;
    char *username = c->username;

    if (cmd_argc <= 0) {
        return 0;

    } else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
        return -1;  // -1 indicates used has quit
        
    } else if (strcmp(cmd_argv[0], "add_group") == 0 && cmd_argc == 2) {
        
        // add group
        buf = add_group(&p->group, cmd_argv[1]);  
        Write(c->soc, buf);

        // add user
        if ((g = find_group(p->group, cmd_argv[1])) == NULL) {    
            buf = "Cannot add user. Group does not exist";    
        } else{
            buf = add_user(g, username);                               
            strncpy(c->groupname, cmd_argv[1], MAXLINE);    // set groupname for this client            
            Write(c->soc, buf);   
            return 1;       // Return 1 indicates notification should be sent to all other users
        }   

    } else if (strcmp(cmd_argv[0], "list_groups") == 0 && cmd_argc == 1) {
        buf = list_groups(p->group);
    
    } else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 2) {
        if ((g = find_group(p->group, cmd_argv[1])) == NULL) {
            buf = "Group does not exist";
        } else {
            buf = list_users(g);
        }
      
    } else if (strcmp(cmd_argv[0], "user_balance") == 0 && cmd_argc == 2) {
        if ((g = find_group(p->group, cmd_argv[1])) == NULL) {
            buf = "Group does not exist";
        } else {
            buf = user_balance(g, username);            
        }
        
    } else if (strcmp(cmd_argv[0], "add_xct") == 0 && cmd_argc == 3) {
        if ((g = find_group(p->group, cmd_argv[1])) == NULL) {
            buf = "Group does not exist";            
        } else {
            char *end;
            double amount = strtod(cmd_argv[2], &end);
            if (end == cmd_argv[2]) {
                buf = "Incorrect number format";
            } else {
                buf = add_xct(g, username, amount);
            }
        }

    } else {
        buf = "Incorrect syntax";
    }

    // Write output to client
    Write(c->soc, buf);     // Write output to client
    return 0;
}