This repository was archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatrpcclient.c
205 lines (177 loc) · 4.08 KB
/
chatrpcclient.c
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "config.h"
#include "chatrpc.h"
#include "utils.h"
#include "utils_client.h"
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <pwd.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
CLIENT *
create_client(const char *host) {
CLIENT *clnt;
clnt = clnt_create(host, CHATRPC, VERSION, "udp");
if (clnt == NULL) {
clnt_pcreateerror(host);
exit(1);
}
return clnt;
}
char *
chat_register_rpc(const char *hostname, const char *username)
{
CLIENT *clnt = create_client(hostname);
char **result;
result = register_1((char *)username, clnt);
if (result == (char **)NULL) {
clnt_perror(clnt, "register rpc failed");
exit(EXIT_FAILURE);
}
return *result;
}
int
chat_register(const char *hostname, const char *username)
{
char *passwd = chat_register_rpc(hostname, username);
if (strlen(passwd) == 0) {
fprintf(stderr, "Register failed\n");
exit(EXIT_SUCCESS);
}
char *workdir = getworkdir();
char *passwdfile = pathjoin(workdir, PASSWDFILE);
free(workdir);
FILE *fpasswd = fopen(passwdfile, "w");
fprintf(fpasswd, "%s\n", passwd);
fclose(fpasswd);
chmod(passwdfile, 0600);
fprintf(stderr, "%s\n", passwd);
return 0;
}
int
chat_sendmsg(const char *hostname,
const char *recipient,
const char *sender,
const char *message)
{
CLIENT *clnt = create_client(hostname);
int *result;
result = sendmsg_1((char *)recipient, (char *)sender, (char *)message, clnt);
if (result == (int *)NULL) {
clnt_perror(clnt, "sendmsg rpc failed");
exit(EXIT_FAILURE);
}
return *result;
}
const char *
chat_recvmsg(const char *hostname,
const char *user,
const char *passwd)
{
CLIENT *clnt = create_client(hostname);
char **result;
result = recvmsg_1((char *)user, (char *)passwd, clnt);
if (result == (char **)NULL) {
clnt_perror(clnt, "recvmsg rpc failed");
exit(EXIT_FAILURE);
}
return *result;
}
int
chat_delmsg(const char *hostname,
const char *user,
const char *passwd)
{
CLIENT *clnt = create_client(hostname);
int *result;
result = delmsg_1((char *)user, (char *)passwd, clnt);
if (result == (int *)NULL) {
clnt_perror(clnt, "delmsg rpc failed");
exit(EXIT_FAILURE);
}
return *result;
}
int
main(int argc, char *argv[])
{
const char *host_default = "127.0.0.1";
static int op = 0;
char *host = host_default;
char *user = NULL;
static struct option long_options[] = {
{"host", required_argument, 0, 'h'},
{"user", required_argument, 0, 'u'},
{"register", no_argument, &op, 1},
{"send", no_argument, &op, 2},
{"receive", no_argument, &op, 3},
{"delete", no_argument, &op, 4},
{0, 0, 0, 0}
};
int option_index = 0;
int c;
while (1) {
c = getopt_long(argc, argv, "hu", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
break;
case 'h':
host = optarg;
break;
case 'u':
user = optarg;
break;
default:
abort();
}
}
if (user == NULL)
user = getpwuid(getuid())->pw_name;
int rc;
const char *passwd;
if (op > 0)
printf("Working as %s@%s.\n", user, host);
switch (op) {
case 1:
// register
chat_register(host, user);
break;
case 2:
// send
if (argc - optind < 2) {
printf("Please specify recipient and message.\n");
exit(EXIT_FAILURE);
}
char *recipient = argv[optind];
char *message = argv[optind+1];
rc = chat_sendmsg(host, recipient, user, message);
printf("Send %s.\n", rc == 0 ? "OK" : "ERROR");
break;
case 3:
// receive
passwd = readpasswd();
if (passwd == NULL) {
printf("Can't find passwd file. Please register first.\n");
exit(EXIT_FAILURE);
}
const char *result = chat_recvmsg(host, user, passwd);
if (result == NULL || strlen(result) == 0)
printf("No messages for %s.\n", user);
else
printf("%s", result);
break;
case 4:
// delete
passwd = readpasswd();
if (passwd == NULL) {
printf("Can't find passwd file. Please register first.\n");
exit(EXIT_FAILURE);
}
rc = chat_delmsg(host, user, passwd);
printf("Delete %s.\n", rc == 0 ? "OK" : "ERROR");
break;
}
exit(EXIT_SUCCESS);
}