|
@@ -39,7 +39,7 @@
|
|
|
#include <klist.h>
|
|
#include <klist.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
-extern struct klist_node connections;
|
|
|
|
|
|
|
+extern struct klist_node connections, logged_users;
|
|
|
|
|
|
|
|
int relay_new_msg(int fd, void *t) {
|
|
int relay_new_msg(int fd, void *t) {
|
|
|
struct vcc_relay_header *req;
|
|
struct vcc_relay_header *req;
|
|
@@ -56,49 +56,81 @@ int relay_new_msg(int fd, void *t) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
read_until(fd, msg, len);
|
|
read_until(fd, msg, len);
|
|
|
- relay_send_to(msg, 0, req);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (!*req->visible)
|
|
|
|
|
+ relay_broadcast(msg, fd, req);
|
|
|
|
|
+ else
|
|
|
|
|
+ /* only send to somebody */
|
|
|
|
|
+ relay_send_to(msg, req->visible, fd, req);
|
|
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-int relay_send_to(const char *msg, int expect_fd, struct vcc_relay_header *hdr) {
|
|
|
|
|
- struct vcc_relay_header *req;
|
|
|
|
|
- struct klist_node *n;
|
|
|
|
|
- int i, len;
|
|
|
|
|
|
|
+int send_to(int fd, const char *restrict msg, int is_only_visible,
|
|
|
|
|
+ const struct vcc_relay_header *restrict hdr, int size) {
|
|
|
|
|
+ struct vcc_relay_header req;
|
|
|
|
|
|
|
|
- /* make sure about that the write() bottom will execute after these */
|
|
|
|
|
- vcc_write_all();
|
|
|
|
|
|
|
+ memset(&req, 0, sizeof(struct vcc_relay_header));
|
|
|
|
|
|
|
|
- i = 0;
|
|
|
|
|
- len = strlen(msg);
|
|
|
|
|
|
|
+ req.magic = htonl(VCC_MAGIC_RL);
|
|
|
|
|
+ req.reqtype = htonl(REQ_REL_NEW);
|
|
|
|
|
+ req.uid = htonl(is_only_visible);
|
|
|
|
|
+ req.session = 0;
|
|
|
|
|
+ req.size = htonl(sizeof(struct vcc_relay_header) + size);
|
|
|
|
|
+
|
|
|
|
|
+ strncpy(req.usrname, hdr->usrname, USRNAME_SIZE);
|
|
|
|
|
+
|
|
|
|
|
+ /* direct writes here
|
|
|
|
|
+ * vcc_send_req(n, req);
|
|
|
|
|
+ * */
|
|
|
|
|
+
|
|
|
|
|
+ write(fd, &req, sizeof(struct vcc_relay_header));
|
|
|
|
|
+ write(fd, msg, size);
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int relay_send_to(const char *restrict msg, const char *restrict visible, int expect_fd,
|
|
|
|
|
+ const struct vcc_relay_header *restrict hdr) {
|
|
|
|
|
+ struct klist_node *n;
|
|
|
|
|
+ struct user *u;
|
|
|
|
|
+
|
|
|
|
|
+ for (n = logged_users.next; n != &logged_users; n = n->next) {
|
|
|
|
|
+ u = n->data;
|
|
|
|
|
|
|
|
- for (n = connections.next; n != &connections; n = n->next) {
|
|
|
|
|
if (n->value == expect_fd)
|
|
if (n->value == expect_fd)
|
|
|
continue;
|
|
continue;
|
|
|
|
|
|
|
|
- if (unlikely(!(req = amalloc(sizeof(struct vcc_relay_header))))) {
|
|
|
|
|
- vcc_log("*** malloc() failed\n");
|
|
|
|
|
|
|
+ if (!strcmp(visible, u->username)) {
|
|
|
|
|
+ send_to(n->value, msg, 1, hdr, strlen(msg));
|
|
|
|
|
+ vcc_log("Relay message sent to the visible user. \n");
|
|
|
|
|
|
|
|
- return 1;
|
|
|
|
|
|
|
+ return 0;
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- memset(req, 0, sizeof(struct vcc_relay_header));
|
|
|
|
|
|
|
+ vcc_log("*** user not found. \n");
|
|
|
|
|
+
|
|
|
|
|
+ return 1;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- req->magic = htonl(VCC_MAGIC_RL);
|
|
|
|
|
- req->reqtype = htonl(REQ_REL_NEW);
|
|
|
|
|
- req->uid = 0;
|
|
|
|
|
- req->session = 0;
|
|
|
|
|
- req->size = htonl(sizeof(struct vcc_relay_header) + len);
|
|
|
|
|
|
|
|
|
|
- strncpy(req->usrname, hdr->usrname, USRNAME_SIZE);
|
|
|
|
|
|
|
+int relay_broadcast(const char *restrict msg, int expect_fd, const struct vcc_relay_header *restrict hdr) {
|
|
|
|
|
+ struct klist_node *n;
|
|
|
|
|
+ int i, len;
|
|
|
|
|
|
|
|
- /* direct writes here
|
|
|
|
|
- * vcc_send_req(n, req);
|
|
|
|
|
- * */
|
|
|
|
|
|
|
+ /* make sure about that the write() bottom will execute after these */
|
|
|
|
|
+ vcc_write_all();
|
|
|
|
|
+
|
|
|
|
|
+ len = strlen(msg);
|
|
|
|
|
+ i = 0;
|
|
|
|
|
+
|
|
|
|
|
+ for (n = connections.next; n != &connections; n = n->next) {
|
|
|
|
|
+ if (n->value == expect_fd)
|
|
|
|
|
+ continue;
|
|
|
|
|
|
|
|
- write(n->value, req, sizeof(struct vcc_relay_header));
|
|
|
|
|
- write(n->value, msg, len);
|
|
|
|
|
|
|
+ send_to(n->value, msg, 0, hdr, len);
|
|
|
|
|
|
|
|
i++;
|
|
i++;
|
|
|
}
|
|
}
|