aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheJackiMonster <thejackimonster@gmail.com>2022-04-17 23:31:37 +0200
committerTheJackiMonster <thejackimonster@gmail.com>2022-04-17 23:31:37 +0200
commitd75a06e86407204e302fe3a49d8e2c137d1fbe2b (patch)
treec9bcc9673d31bad8dc0716ed1d6f223fcc0a43e9
parent85e489d74a931a1bbc809933b53e593da0f880a7 (diff)
downloadmessenger-cli-d75a06e86407204e302fe3a49d8e2c137d1fbe2b.tar.gz
messenger-cli-d75a06e86407204e302fe3a49d8e2c137d1fbe2b.zip
Added very basic messages list
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r--Makefile12
-rw-r--r--src/application.h7
-rw-r--r--src/chat.c82
-rw-r--r--src/chat.h4
-rw-r--r--src/messenger_cli.c84
-rw-r--r--src/ui/messages.c80
-rw-r--r--src/ui/messages.h64
7 files changed, 202 insertions, 131 deletions
diff --git a/Makefile b/Makefile
index 24f771d..237681e 100644
--- a/Makefile
+++ b/Makefile
@@ -4,10 +4,14 @@ INSTALL_DIR ?= /usr/local/
4 4
5BINARY = messenger-cli 5BINARY = messenger-cli
6SOURCES = messenger_cli.c\ 6SOURCES = messenger_cli.c\
7 application.c\ 7 application.c\
8 chat.c\ 8 chat.c\
9 ui/accounts.c 9 ui/accounts.c\
10HEADERS = 10 ui/messages.c
11HEADERS = application.h\
12 chat.h\
13 ui/accounts.h\
14 ui/messages.h
11 15
12LIBRARIES = gnunetchat gnunetutil ncurses 16LIBRARIES = gnunetchat gnunetutil ncurses
13 17
diff --git a/src/application.h b/src/application.h
index 16909c9..0eeb37e 100644
--- a/src/application.h
+++ b/src/application.h
@@ -30,7 +30,9 @@
30 30
31#include "chat.h" 31#include "chat.h"
32#include "util.h" 32#include "util.h"
33//#include "ui/accounts.h" 33
34#include "ui/accounts.h"
35#include "ui/messages.h"
34 36
35typedef struct MESSENGER_Application 37typedef struct MESSENGER_Application
36{ 38{
@@ -40,6 +42,9 @@ typedef struct MESSENGER_Application
40 int status; 42 int status;
41 43
42 MESSENGER_Chat chat; 44 MESSENGER_Chat chat;
45
46 UI_ACCOUNTS_Handle accounts;
47 UI_MESSAGES_Handle messages;
43} MESSENGER_Application; 48} MESSENGER_Application;
44 49
45void 50void
diff --git a/src/chat.c b/src/chat.c
index c4b67b2..4645c6b 100644
--- a/src/chat.c
+++ b/src/chat.c
@@ -25,36 +25,24 @@
25#include "chat.h" 25#include "chat.h"
26 26
27#include "application.h" 27#include "application.h"
28#include "ui/accounts.h"
29 28
30UI_ACCOUNTS_Handle accounts; 29static void
31 30_chat_refresh(MESSENGER_Application *app)
32int lc = 0;
33
34static int
35_chat_message(UNUSED void *cls,
36 UNUSED struct GNUNET_CHAT_Context *context,
37 UNUSED const struct GNUNET_CHAT_Message *message)
38{ 31{
39 // MESSENGER_Application *app = cls; 32 const struct GNUNET_CHAT_Account *account = GNUNET_CHAT_get_connected(
40 33 app->chat.handle
41 enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(
42 message
43 ); 34 );
44 35
45 move(lc++, 50); 36 app->accounts.window = NULL;
46 printw("TEST %d", (int) kind); 37 app->messages.window = NULL;
47 38
48 return GNUNET_YES; 39 if (!account)
49} 40 app->accounts.window = stdscr;
41 else if (app->chat.context)
42 app->messages.window = stdscr;
50 43
51static void 44 accounts_print(&(app->accounts), app);
52_chat_refresh(MESSENGER_Application *app) 45 messages_print(&(app->messages), app);
53{
54 // TODO
55
56 accounts.window = stdscr;
57 accounts_print(&accounts, app);
58} 46}
59 47
60static int 48static int
@@ -64,19 +52,44 @@ _chat_event(MESSENGER_Application *app,
64 if (key < 0) 52 if (key < 0)
65 goto refresh; 53 goto refresh;
66 54
55 const struct GNUNET_CHAT_Account *account = GNUNET_CHAT_get_connected(
56 app->chat.handle
57 );
58
67 if ('q' == key) 59 if ('q' == key)
68 return 1; 60 return 1;
69 61
70 move(lc++, 0); 62 if (!account)
71 printw("KEY %d", key); 63 accounts_event(&(app->accounts), app, key);
64 else if (app->chat.context)
65 messages_event(&(app->messages), app, key);
66 else
67 {
68 struct GNUNET_CHAT_Group *test = GNUNET_CHAT_group_create(
69 app->chat.handle,
70 "test"
71 );
72 72
73 accounts_event(&accounts, app, key); 73 app->chat.context = GNUNET_CHAT_group_get_context(test);
74 }
74 75
75refresh: 76refresh:
76 _chat_refresh(app); 77 _chat_refresh(app);
77 return 0; 78 return 0;
78} 79}
79 80
81int lc = 0;
82
83static int
84_chat_message(void *cls,
85 UNUSED struct GNUNET_CHAT_Context *context,
86 UNUSED const struct GNUNET_CHAT_Message *message)
87{
88 MESSENGER_Application *app = cls;
89 _chat_event(app, KEY_RESIZE);
90 return GNUNET_YES;
91}
92
80static void 93static void
81_chat_idle(void *cls) 94_chat_idle(void *cls)
82{ 95{
@@ -107,11 +120,12 @@ chat_start(MESSENGER_Chat *chat,
107{ 120{
108 chat->handle = GNUNET_CHAT_start( 121 chat->handle = GNUNET_CHAT_start(
109 cfg, 122 cfg,
110 "appdir",
111 &_chat_message, 123 &_chat_message,
112 app 124 app
113 ); 125 );
114 126
127 chat->context = NULL;
128
115 chat->idle = GNUNET_SCHEDULER_add_now( 129 chat->idle = GNUNET_SCHEDULER_add_now(
116 &_chat_idle, 130 &_chat_idle,
117 app 131 app
@@ -127,18 +141,6 @@ chat_stop(MESSENGER_Chat *chat)
127 chat->idle = NULL; 141 chat->idle = NULL;
128 } 142 }
129 143
130 if (chat->key)
131 {
132 GNUNET_SCHEDULER_cancel(chat->key);
133 chat->key = NULL;
134 }
135
136 if (chat->fdset)
137 {
138 GNUNET_NETWORK_fdset_destroy(chat->fdset);
139 chat->fdset = NULL;
140 }
141
142 GNUNET_CHAT_stop(chat->handle); 144 GNUNET_CHAT_stop(chat->handle);
143 chat->handle = NULL; 145 chat->handle = NULL;
144} 146}
diff --git a/src/chat.h b/src/chat.h
index 8248da3..0635336 100644
--- a/src/chat.h
+++ b/src/chat.h
@@ -34,8 +34,8 @@ struct MESSENGER_Application;
34typedef struct MESSENGER_Chat 34typedef struct MESSENGER_Chat
35{ 35{
36 struct GNUNET_CHAT_Handle *handle; 36 struct GNUNET_CHAT_Handle *handle;
37 struct GNUNET_NETWORK_FDSet *fdset; 37 struct GNUNET_CHAT_Context *context;
38 struct GNUNET_SCHEDULER_Task *key; 38
39 struct GNUNET_SCHEDULER_Task *idle; 39 struct GNUNET_SCHEDULER_Task *idle;
40} MESSENGER_Chat; 40} MESSENGER_Chat;
41 41
diff --git a/src/messenger_cli.c b/src/messenger_cli.c
index 0c9fda2..1c68b9a 100644
--- a/src/messenger_cli.c
+++ b/src/messenger_cli.c
@@ -24,90 +24,6 @@
24 24
25#include "application.h" 25#include "application.h"
26 26
27/*static void
28run (void *cls, char* const* args,
29 const char *cfgfile,
30 const struct GNUNET_CONFIGURATION_Handle *cfg)
31{
32 struct GNUNET_CHAT_Handle *handle = GNUNET_CHAT_start(
33 cfg,
34 "appdir",
35 NULL, NULL
36 );
37
38 initscr();
39 noecho();
40
41 int bx, by, mx, my;
42 getbegyx(stdscr, by, bx);
43 getmaxyx(stdscr, my, mx);
44
45 WINDOW *win = newwin(15, 30, by + (my - by - 15) / 2, bx + (mx - bx - 30) / 2);
46 keypad(win, TRUE);
47
48 int selected = 0;
49
50 int c;
51 do {
52 getbegyx(stdscr, by, bx);
53 getmaxyx(stdscr, my, mx);
54
55 int x = bx + (mx - bx - 30) / 2;
56 int y = by + (my - by - 15) / 2;
57
58 int w = 30;
59 int h = 15;
60
61 if (mx - x < w)
62 w = (mx - x > 0? mx - x : 0);
63
64 if (my - y < h)
65 h = (my - y > 0? my - y : 0);
66
67 if (w * h > 0)
68 {
69 mvwin(win, y, x);
70 wresize(win, h, w);
71
72 werase(win);
73 box(win, 0, 0);
74
75 wmove(win, 1, 1);
76 wprintw(win, "%d %d, %d %d | %d %d", bx, by, mx, my, c, KEY_DOWN);
77
78 const int attrs_select = A_BOLD;
79
80 for (int i = 0; i < 5; i++) {
81 if (i == selected) wattron(win, attrs_select);
82
83 wmove(win, i+2, 1);
84 wprintw(win, "Option %d", i+1);
85
86 if (i == selected) wattroff(win, attrs_select);
87 }
88
89 wmove(win, 7, 1);
90 c = wgetch(win);
91 }
92 else
93 {
94 c = getch();
95 }
96
97 if (KEY_UP == c) selected = (selected > 0? selected - 1 : 0);
98 else if (KEY_DOWN == c) selected = (selected < 4? selected + 1 : 4);
99
100 clear();
101 refresh();
102 } while (c != 'q');
103
104 delwin(win);
105
106 endwin();
107
108 GNUNET_CHAT_stop(handle);
109}*/
110
111int 27int
112main (int argc, char** argv) 28main (int argc, char** argv)
113{ 29{
diff --git a/src/ui/messages.c b/src/ui/messages.c
new file mode 100644
index 0000000..780dd45
--- /dev/null
+++ b/src/ui/messages.c
@@ -0,0 +1,80 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file ui/messages.c
23 */
24
25#include "messages.h"
26
27#include "../application.h"
28
29void
30messages_event(UI_MESSAGES_Handle *messages,
31 struct MESSENGER_Application *app,
32 int key)
33{
34 // TODO
35}
36
37int
38_messages_iterate_print(void *cls,
39 struct GNUNET_CHAT_Context *context,
40 const struct GNUNET_CHAT_Message *message)
41{
42 UI_MESSAGES_Handle *messages = cls;
43
44 const int y = messages->line_index++;
45
46 enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message);
47 const char *text = GNUNET_CHAT_message_get_text(message);
48
49 struct GNUNET_TIME_Absolute timestamp = GNUNET_CHAT_message_get_timestamp(
50 message
51 );
52
53 wmove(messages->window, y, 0);
54 wprintw(
55 messages->window,
56 "%s | [%d]: %s",
57 GNUNET_TIME_absolute2s(timestamp),
58 (int) kind,
59 text
60 );
61
62 return GNUNET_YES;
63}
64
65void
66messages_print(UI_MESSAGES_Handle *messages,
67 struct MESSENGER_Application *app)
68{
69 if (!(messages->window))
70 return;
71
72 struct GNUNET_CHAT_Context *context = app->chat.context;
73
74 messages->line_index = 0;
75 GNUNET_CHAT_context_iterate_messages(
76 context,
77 _messages_iterate_print,
78 messages
79 );
80}
diff --git a/src/ui/messages.h b/src/ui/messages.h
new file mode 100644
index 0000000..305ad01
--- /dev/null
+++ b/src/ui/messages.h
@@ -0,0 +1,64 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file ui/messages.h
23 */
24
25#ifndef UI_MESSAGES_H_
26#define UI_MESSAGES_H_
27
28#include <stdlib.h>
29#include <curses.h>
30
31#include <gnunet/platform.h>
32#include <gnunet/gnunet_chat_lib.h>
33#include <gnunet/gnunet_util_lib.h>
34
35struct MESSENGER_Application;
36
37typedef struct UI_MESSAGES_List
38{
39 const struct GNUNET_CHAT_Message *message;
40
41 struct UI_MESSAGES_List *prev;
42 struct UI_MESSAGES_List *next;
43} UI_MESSAGES_List;
44
45typedef struct UI_MESSAGES_Handle
46{
47 WINDOW *window;
48
49 UI_MESSAGES_List *head;
50 UI_MESSAGES_List *tail;
51
52 int line_index;
53} UI_MESSAGES_Handle;
54
55void
56messages_event(UI_MESSAGES_Handle *messages,
57 struct MESSENGER_Application *app,
58 int key);
59
60void
61messages_print(UI_MESSAGES_Handle *messages,
62 struct MESSENGER_Application *app);
63
64#endif /* UI_MESSAGES_H_ */