summaryrefslogtreecommitdiff
path: root/data.c
blob: 3780fd0edefee88e41ca24568c1694c735a3f4c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129


#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "gui.h"
#include "data.h"


static roster_item_t* roster = NULL;


void data_add_roster_item(const char* jid, const char* subscription,
                          const char* name) {

	// FIXME check if roster item already exists and handle updates

	// 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 {
		printf("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) {
		printf("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);

}