summaryrefslogtreecommitdiff
path: root/talk.c
diff options
context:
space:
mode:
authorthing 1 <thing1@seacrossedlovers.xyz>2024-12-10 08:35:59 +0000
committerthing 1 <thing1@seacrossedlovers.xyz>2024-12-10 08:35:59 +0000
commit4dd9290648ccb0d5fa19df956f970dda9afd9db7 (patch)
tree6e3927fca87daaf925d4630d263442a1b4ea68ea /talk.c
init commit of talk and talk serverHEADmaster
Diffstat (limited to 'talk.c')
-rw-r--r--talk.c142
1 files changed, 142 insertions, 0 deletions
diff --git a/talk.c b/talk.c
new file mode 100644
index 0000000..03b94dd
--- /dev/null
+++ b/talk.c
@@ -0,0 +1,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;
+}