messenger.c (4990B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2021--2024 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 chat/messenger.c 23 */ 24 25 #include "messenger.h" 26 27 #include "../event.h" 28 #include <gnunet/gnunet_chat_lib.h> 29 30 static int 31 _chat_messenger_message(void *cls, 32 struct GNUNET_CHAT_Context *context, 33 struct GNUNET_CHAT_Message *message) 34 { 35 g_assert((cls) && (message)); 36 37 MESSENGER_Application *app = (MESSENGER_Application*) cls; 38 39 if (GNUNET_YES == GNUNET_CHAT_message_is_deleted(message)) 40 { 41 application_call_message_event( 42 app, 43 event_delete_message, 44 context, 45 message 46 ); 47 48 goto skip_message_handling; 49 } 50 51 // Handle each kind of message as proper event regarding context 52 switch (GNUNET_CHAT_message_get_kind(message)) 53 { 54 case GNUNET_CHAT_KIND_WARNING: 55 application_call_message_event( 56 app, 57 event_handle_warning, 58 context, 59 message 60 ); 61 break; 62 case GNUNET_CHAT_KIND_REFRESH: 63 { 64 application_call_event(app, event_refresh_accounts); 65 break; 66 } 67 case GNUNET_CHAT_KIND_LOGIN: 68 { 69 application_call_event(app, event_update_profile); 70 break; 71 } 72 case GNUNET_CHAT_KIND_LOGOUT: 73 { 74 application_call_sync_event(app, event_cleanup_profile); 75 break; 76 } 77 case GNUNET_CHAT_KIND_CREATED_ACCOUNT: 78 case GNUNET_CHAT_KIND_UPDATE_ACCOUNT: 79 { 80 application_call_message_event( 81 app, 82 event_select_profile, 83 context, 84 message 85 ); 86 break; 87 } 88 case GNUNET_CHAT_KIND_UPDATE_CONTEXT: 89 { 90 application_call_message_event( 91 app, 92 event_update_chats, 93 context, 94 message 95 ); 96 break; 97 } 98 case GNUNET_CHAT_KIND_JOIN: 99 case GNUNET_CHAT_KIND_LEAVE: 100 { 101 application_call_message_event( 102 app, 103 (GNUNET_YES == GNUNET_CHAT_message_is_sent(message)? 104 event_update_chats : 105 event_presence_contact 106 ), 107 context, 108 message 109 ); 110 break; 111 } 112 case GNUNET_CHAT_KIND_CONTACT: 113 case GNUNET_CHAT_KIND_SHARED_ATTRIBUTES: 114 { 115 application_call_message_event( 116 app, 117 event_update_contacts, 118 context, 119 message 120 ); 121 break; 122 } 123 case GNUNET_CHAT_KIND_INVITATION: 124 { 125 application_call_message_event( 126 app, 127 event_invitation, 128 context, 129 message 130 ); 131 break; 132 } 133 case GNUNET_CHAT_KIND_TEXT: 134 case GNUNET_CHAT_KIND_FILE: 135 { 136 application_call_message_event( 137 app, 138 event_receive_message, 139 context, 140 message 141 ); 142 break; 143 } 144 case GNUNET_CHAT_KIND_DELETION: 145 { 146 struct GNUNET_CHAT_Message *target; 147 target = GNUNET_CHAT_message_get_target(message); 148 149 if (target) 150 application_call_message_event( 151 app, 152 event_delete_message, 153 context, 154 target 155 ); 156 break; 157 } 158 case GNUNET_CHAT_KIND_TAG: 159 { 160 application_call_message_event( 161 app, 162 event_tag_message, 163 context, 164 message 165 ); 166 break; 167 } 168 case GNUNET_CHAT_KIND_ATTRIBUTES: 169 { 170 application_call_event(app, event_update_attributes); 171 break; 172 } 173 case GNUNET_CHAT_KIND_DISCOURSE: 174 { 175 application_call_message_event( 176 app, 177 event_discourse, 178 context, 179 message 180 ); 181 break; 182 } 183 case GNUNET_CHAT_KIND_DATA: 184 { 185 application_call_message_event( 186 app, 187 event_discourse_data, 188 context, 189 message 190 ); 191 break; 192 } 193 default: 194 break; 195 } 196 197 skip_message_handling: 198 return GNUNET_YES; 199 } 200 201 void 202 chat_messenger_run(void *cls, 203 UNUSED char *const *args, 204 UNUSED const char *cfgfile, 205 const struct GNUNET_CONFIGURATION_Handle *cfg) 206 { 207 g_assert((cls) && (cfg)); 208 209 MESSENGER_Application *app = (MESSENGER_Application*) cls; 210 211 schedule_load_gnunet(&(app->chat.schedule)); 212 213 app->chat.messenger.handle = GNUNET_CHAT_start( 214 cfg, 215 &_chat_messenger_message, 216 app 217 ); 218 }