aboutsummaryrefslogtreecommitdiff
path: root/user_command.h
blob: 803fadae96750c0d806cf3be104022c1cdbfe727 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
 * Localchat
 * Ben Burwell
 *
 * Process user input
 */

void process_user_command() {

	int i;

	if (respond_to_chat_request) {

		// reset the global variable
		respond_to_chat_request = 0;

		if (strcmp(command, "y\n") == 0) {
			accept_callback_accept();
		} else {
			accept_callback_decline();
		}

		return;
	}

	if (strcmp(in_chat, "Y") == 0) {

		// in chat, send input to the peer
		
		if (strcmp(command, "/q\n") == 0) {

			// send the quit message
			strcpy(out_buf, "SESQ");
			send(client_s, out_buf, strlen(out_buf)+1, 0);

			// close the socket
			close(client_s);

			// set my flag
			strcpy(in_chat, "N");

			// reset prompt
			strcpy(prompt, ">");

		} else {

			strcpy(out_buf, "SESMSG:");
			strcat(out_buf, command);
			send(client_s, out_buf, strlen(out_buf)+1, 0);

		}

		return;

	}

	if (strcmp(command, "quit\n") == 0 || strcmp(command, "q\n") == 0) {
		exit(0);
	} else if (strcmp(command, "who\n") == 0 || strcmp(command, "w\n") == 0) {

		// print a list of online peers
		pthread_mutex_lock(&peer_table_lock);

		printf("  +---------+-----------------+----------------------------------+ \n");
		printf("  | In Chat | IP              | Username                         | \n");
		printf("  +---------+-----------------+----------------------------------+ \n");

		for (i = 0; i < num_peers_in_table; i++) {
			printf("  |       %s | %-15s | %-32s | \n", peers[i].in_chat, peers[i].ip, peers[i].username);
		}

		printf("  +---------+-----------------+----------------------------------+ \n");

		pthread_mutex_unlock(&peer_table_lock);

	} else if (strcmp(command, "chat\n") == 0 || strcmp(command, "c\n") == 0) {

		char user[32];
		int exists;

		printf("user: ");
		fgets(user, 32, stdin);

		user[strlen(user) - 1] = 0;

		exists = check_user_name(user);

		if (exists) {
			printf("Sending chat request to %s... \n", user);
			send_chat_request(user);
		} else {
			printf("%s does not exist. \n", user);
		}


	} else if (strcmp(command, "help\n") == 0 || strcmp(command, "?\n") == 0) {

		printf("Command Summary: \n");
		printf("  who:  display a list of online peers \n");
		printf("  chat: initiate a chat session \n");
		printf("  quit: exit the localchat program \n");

	} else if (strcmp(command, "\n") == 0) {
		// no command, do nothing.
	} else {
		printf("Unrecognized command. Type `help` or `?` for help. \n");
	}
}