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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/* Module: buddylist.c
Purpose: Manages your list of buddies in memory
Author: leecher
Date: 30.08.2009
*/
#include <stdlib.h>
#include <string.h>
#include "memlist.h"
#include "buddylist.h"
static void SetEntry(NICKENTRY *pEntry, cJSON *pNick);
// -----------------------------------------------------------------------------
// Interface
// -----------------------------------------------------------------------------
TYP_LIST *BuddyList_Init(void)
{
TYP_LIST *hList = List_Init(16);
return hList;
}
// -----------------------------------------------------------------------------
void BuddyList_Exit(TYP_LIST *hList)
{
NICKENTRY *pEntry;
while (pEntry=(NICKENTRY*)List_Pop(hList))
{
BuddyList_FreeEntry(pEntry);
free (pEntry);
}
List_Exit(hList);
}
// -----------------------------------------------------------------------------
BOOL BuddyList_Insert(TYP_LIST *hList, cJSON *pNick)
{
NICKENTRY *pEntry;
if (pEntry=BuddyList_Find (hList, cJSON_GetObjectItem(pNick, "buid")->valuestring))
BuddyList_FreeEntry (pEntry);
else
{
if (!(pEntry = calloc (1, sizeof(NICKENTRY)))) return FALSE;
if (!List_Push(hList, pEntry)) return FALSE;
}
SetEntry(pEntry, pNick);
pEntry->iBuddyStatus = 3;
return TRUE;
}
// -----------------------------------------------------------------------------
BOOL BuddyList_AddTemporaryUser(TYP_LIST *hList, char *pszUser)
{
NICKENTRY *pEntry;
if (BuddyList_Find (hList, pszUser)) return TRUE;
if (!(pEntry = calloc (1, sizeof(NICKENTRY)))) return FALSE;
pEntry->pszUser = strdup(pszUser);
pEntry->pszAlias = strdup(pszUser);
strcpy (pEntry->szStatus, "OFFLINE");
pEntry->iBuddyStatus = 2;
return List_Push(hList, pEntry);
}
// -----------------------------------------------------------------------------
BOOL BuddyList_Remove(TYP_LIST *hList, NICKENTRY *pEntry)
{
NICKENTRY *pListEntry;
int i, nCount;
for (i=0, nCount=List_Count(hList); i<nCount; i++)
{
pListEntry = List_ElementAt (hList, i);
if (pListEntry == pEntry) break;
}
if (i<nCount)
{
BuddyList_FreeEntry (pEntry);
List_RemoveElementAt(hList, i);
free (pEntry);
return TRUE;
}
return FALSE;
}
// -----------------------------------------------------------------------------
NICKENTRY *BuddyList_Find(TYP_LIST *hList, char *pszUser)
{
int i, nCount;
NICKENTRY *pEntry;
for (i=0, nCount=List_Count(hList); i<nCount; i++)
{
pEntry = List_ElementAt (hList, i);
if (strcmp(pEntry->pszUser, pszUser) == 0)
return pEntry;
}
return NULL;
}
// -----------------------------------------------------------------------------
BOOL BuddyList_SetStatus(TYP_LIST *hList, cJSON *pNick)
{
NICKENTRY *pEntry;
if ((pEntry = BuddyList_Find(hList, cJSON_GetObjectItem(pNick, "buid")->valuestring)))
{
BuddyList_FreeEntry(pEntry);
SetEntry(pEntry, pNick);
return TRUE;
}
return FALSE;
}
// -----------------------------------------------------------------------------
void BuddyList_FreeEntry(NICKENTRY *pEntry)
{
if (pEntry->pszAlias) free (pEntry->pszAlias);
if (pEntry->pszUser) free (pEntry->pszUser);
if (pEntry->pszStatusText) free(pEntry->pszStatusText);
}
// -----------------------------------------------------------------------------
// Static
// -----------------------------------------------------------------------------
static void SetEntry(NICKENTRY *pEntry, cJSON *pNick)
{
pEntry->pszAlias = strdup(cJSON_GetObjectItem(pNick, "alias")->valuestring);
pEntry->pszUser = strdup(cJSON_GetObjectItem(pNick, "buid")->valuestring);
pEntry->pszStatusText = cJSON_GetObjectItem(pNick, "status")->valuestring;
if (pEntry->pszStatusText) pEntry->pszStatusText = strdup(pEntry->pszStatusText);
strcpy (pEntry->szStatus, cJSON_GetObjectItem(pNick, "primitive")->valuestring);
}
|