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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "talk.h"
#include "util.h"
char *hostip;
int port = 231;
char *username = "(null)";
char *password = "(null)";
char *recipient = "(null)";
packettype mode = MSG;
static int initsocket(struct sockaddr_in address, socklen_t len){
int s;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("socket");
return -1;
}
if ((connect(s, (struct sockaddr *)&address, len)) != 0) {
perror("connect");
return -1;
}
return s;
}
static void processargs(int argc, char **argv){
// -h specify the host of the server
// -P specify the port // default 231
// -u specify the username
// -p specify the password
// -r specify the recipient
// -g get mode
// -R register mode
for (int i = 1; i < argc; i++){
if (strcmp(argv[i], "-h") == 0){
i++;
hostip = argv[i];
} else if (strcmp(argv[i], "-P") == 0){
i++;
port = atoi(argv[i]);
} else if (strcmp(argv[i], "-u") == 0){
i++;
username = argv[i];
} else if (strcmp(argv[i], "-p") == 0){
i++;
password = argv[i];
} else if (strcmp(argv[i], "-r") == 0){
i++;
recipient = argv[i];
} else if (strcmp(argv[i], "-g") == 0){
mode = GET;
} else if (strcmp(argv[i], "-R") == 0){
mode = NEWUSER;
}
else {
fprintf(stderr, "unknown option: %s\n", argv[i]);
exit(1);
}
}
if (hostip == NULL) {
fprintf(stderr, "no host given!\n");
exit(1);
}
if (strcmp(username, "(null)") == 0){
fprintf(stderr, "no username given!\n");
exit(1);
}
if (strcmp(password, "(null)") == 0){
fprintf(stderr, "no password given!\n");
exit(1);
}
if (mode == GET && strcmp(recipient, "(null)") == 0){
fprintf(stderr, "recipient is needed when requesting from an individual!\n");
exit(1);
}
if (mode == MSG && strcmp(recipient, "(null)") == 0){
fprintf(stderr, "recipient is needed when sending to an individual!\n");
exit(1);
}
}
int main(int argc, char **argv){
processargs(argc, argv);
struct sockaddr_in address;
address.sin_family = AF_INET;
inet_pton(AF_INET, hostip, &address.sin_addr);
address.sin_port = htons(port);
socklen_t len = sizeof(address);
int s;
if ((s = initsocket(address, len)) < 0) return s;
int size = sizeof(packet);
packet *buf = malloc(size);
strcpy(buf->username, username);
strcpy(buf->password, password);
strcpy(buf->recipient, recipient);
buf->type = mode;
char *tmp;
if (buf->type == MSG){
tmp = readupto(stdin, '\n');
buf->msg = tmp;
buf->msglen=strlen(buf->msg);
send(s, buf, size, 0);
send(s, buf->msg, buf->msglen, 0);
free(tmp);
} else send(s, buf, size, 0);
recv(s, buf, size, 0);
switch (buf->type){
case USERMAKEERROR: fprintf(stderr, "failed to create new user on server!\n"); break;
case UNKNOWNCMD: fprintf(stderr, "unknown cmd!\n"); break;
case PASSAUTHFAIL: fprintf(stderr, "failed to authenticate password!\n"); break;
case NOSUCHUSER: fprintf(stderr, "user not found, either you tried to msg someone who doesn't exist or you don't exist!\n"); break;
case NEVERMSGED: fprintf(stderr, "you have never messaged that user, or have no new messages from them!\n"); break;
case GET:
tmp = calloc(1, buf->msglen);
recv(s, tmp, buf->msglen, 0);
printf("%s", tmp);
free(tmp);
break;
}
close(s);
return 0;
}
|