#include #include #include #include #include "gui.h" #include "data.h" static roster_item_t* roster = NULL; /* check if JID is already in roster data structure */ bool is_jid_known(const char* jid) { roster_item_t* i; // iterate over all roster items for (i = roster; i != NULL; i=i->next) { if (strcmp(i->jid, jid) == 0) { return true; } } return false; } void data_add_roster_item(const char* jid, const char* subscription, const char* name) { // FIXME handle roster updates if (is_jid_known(jid)) { return; } // allocate datastructures roster_item_t* item = malloc(sizeof(roster_item_t)); chat_t* chat = malloc(sizeof(chat_t)); // initialize chat chat->messages = NULL; chat->jid = malloc(sizeof(char)*strlen(jid)); strcpy(chat->jid, jid); gui_add_chat_widget(chat); // initialize roster item if (name == NULL) { item->name = NULL; } else { item->name = malloc(sizeof(char)*strlen(name)); strcpy(item->name, name); } item->jid = malloc(sizeof(char)*strlen(jid)); strcpy(item->jid, jid); if (strcmp(subscription, "none") == 0) { item->sub = SUB_NONE; } else if (strcmp(subscription, "to") == 0) { item->sub = SUB_TO; } else if (strcmp(subscription, "from") == 0) { item->sub = SUB_FROM; } else if (strcmp(subscription, "both") == 0) { item->sub = SUB_BOTH; } else { fprintf(stderr, "Invalid subscription '%s'!\n", subscription); return; } item->chat = chat; item->next = NULL; gui_add_roster_item_widget(item); // add item to roster datastructure if (roster == NULL) { roster = item; } else { roster_item_t* current = roster; while (current->next != NULL) // loop until end of linked list current = current->next; current->next = item; } } void data_add_incoming_message(const char* sender_jid, const char* content) { // TODO rework based on chat->jid // find correct chat chat_t* chat = NULL; roster_item_t* i; int bare_len; // length of bare JID for (i = roster; i != NULL; i=i->next) { bare_len = strlen(i->jid); if (strncmp(i->jid, sender_jid, bare_len) == 0) { chat = i->chat; break; } } if (chat == NULL) { fprintf(stderr, "Could not find chat for message from '%s'!\n", sender_jid); return; } // initialize message_t message_t* msg = malloc(sizeof(message_t)); msg->sender_jid = malloc(sizeof(char)*strlen(sender_jid)); strcpy(msg->sender_jid, sender_jid); msg->content = malloc(sizeof(char)*strlen(content)); strcpy(msg->content, content); msg->next = NULL; gui_add_message_widget(msg, i->chat); // find pointer to next message of chat message_t* m; for (m = chat->messages; m != NULL; m=m->next); // append message to chat m = msg; // TODO could be more correct to update chat->jid to full JID of just // received message so that future responses go to this ressource } void data_add_outgoing_message(const char* sender_jid, const char* content, chat_t* chat) { // allocate and initialize message_t message_t* msg = malloc(sizeof(message_t)); msg->sender_jid = malloc(sizeof(char)*strlen(sender_jid)); strcpy(msg->sender_jid, sender_jid); msg->recipient_jid = malloc(sizeof(char)*strlen(chat->jid)); strcpy(msg->recipient_jid, chat->jid); msg->content = malloc(sizeof(char)*strlen(content)); strcpy(msg->content, content); msg->next = NULL; // find pointer to next message of chat and add this message message_t* m; for (m = chat->messages; m != NULL; m=m->next); m = msg; // TODO send this message via XMPP // create GUI widget for this message gui_add_message_widget(msg, chat); }