aboutsummaryrefslogtreecommitdiff
path: root/close_direct_conversations
diff options
context:
space:
mode:
authorJulien Dessaux2019-08-06 15:44:18 +0200
committerJulien Dessaux2019-08-06 17:42:06 +0200
commitfea924350b6ba50dc0cb0e5e133cbbd914137e61 (patch)
tree68e119dec63829356db8614204d0a6b4bb7e5221 /close_direct_conversations
parentCosmetics (diff)
downloadrocket-cli-client-fea924350b6ba50dc0cb0e5e133cbbd914137e61.tar.gz
rocket-cli-client-fea924350b6ba50dc0cb0e5e133cbbd914137e61.tar.bz2
rocket-cli-client-fea924350b6ba50dc0cb0e5e133cbbd914137e61.zip
Added a proper subscriptions listing for the direct conversations closer
Diffstat (limited to 'close_direct_conversations')
-rw-r--r--close_direct_conversations/CMakeLists.txt6
-rw-r--r--close_direct_conversations/main.c91
2 files changed, 97 insertions, 0 deletions
diff --git a/close_direct_conversations/CMakeLists.txt b/close_direct_conversations/CMakeLists.txt
new file mode 100644
index 0000000..7aba3e4
--- /dev/null
+++ b/close_direct_conversations/CMakeLists.txt
@@ -0,0 +1,6 @@
+file(GLOB_RECURSE SOURCES *.c)
+
+ADD_EXECUTABLE(rocket_close_direct_conversations ${SOURCES})
+target_link_libraries(rocket_close_direct_conversations common restapi)
+
+install(TARGETS rocket_close_direct_conversations DESTINATION bin)
diff --git a/close_direct_conversations/main.c b/close_direct_conversations/main.c
new file mode 100644
index 0000000..0938982
--- /dev/null
+++ b/close_direct_conversations/main.c
@@ -0,0 +1,91 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <termios.h>
+#include <unistd.h>
+
+#include "common/config.h"
+#include "common/util.h"
+#include "restapi/auth.h"
+#include "restapi/im.h"
+#include "restapi/subscriptions.h"
+
+void print_subscription(const struct subscription* sub)
+{
+ if (sub->type == SUBSCRIPTION_DIRECT)
+ printf("\t%s\n", sub->name);
+}
+
+int main(void)
+{
+ if (config_load(CONFIG_PATH) != 0) {
+ return 1;
+ }
+
+ const char* login = config_get_login();
+ char* termlogin = NULL;
+ if (login == NULL) {
+ size_t len = 0;
+ printf("Login: ");
+ ssize_t read = getline(&termlogin, &len, stdin);
+ if (read > 1) termlogin[read-1] = 0;
+ login = termlogin;
+ }
+
+ const char* password = config_get_password();
+ char* termpassword = NULL;
+ if (password == NULL) {
+ struct termios oflags, nflags;
+ tcgetattr(fileno(stdin), &oflags);
+ nflags = oflags;
+ nflags.c_lflag &= ~ECHO;
+ nflags.c_lflag |= ECHONL;
+
+ if (tcsetattr(fileno(stdin), TCSADRAIN, &nflags) != 0) {
+ perror("tcsetattr");
+ return -1;
+ }
+
+ size_t len = 0;
+ printf("Password: ");
+ size_t read = getline(&termpassword, &len, stdin);
+ if (read > 1) termpassword[read-1] = 0;
+
+ if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
+ perror("tcsetattr");
+ return -1;
+ }
+ password = termpassword;
+ }
+
+ if (restapi_login(login, password) == 0) {
+ struct subscription* subscriptions = restapi_subscriptions_get();
+
+ printf("Active direct conversations :\n");
+ common_subscriptions_const_walk(subscriptions, &print_subscription);
+ common_subscriptions_free(subscriptions);
+
+ while(1) {
+ char* buff = NULL;
+ size_t len2;
+ printf("Direct conversation to close: ");
+ ssize_t entry = getline(&buff, &len2, stdin);
+ if (entry > 1) {
+ buff[entry-1] = 0;
+ } else {
+ free(buff);
+ break;
+ }
+ restapi_im_close(buff);
+ free(buff);
+ }
+ } else {
+ printf("Couldn't init rest api.\n");
+ }
+
+ restapi_logout();
+ config_clean();
+ free(termlogin);
+ free(termpassword);
+
+ return 0;
+}