File: chat_lists.c

package info (click to toggle)
everybuddy 0.2.1beta6-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,936 kB
  • ctags: 5,627
  • sloc: ansic: 50,783; sh: 8,559; yacc: 191; makefile: 182; perl: 97; lex: 50
file content (183 lines) | stat: -rw-r--r-- 4,851 bytes parent folder | download | duplicates (2)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * libmsn
 *
 * Copyright (C) 1999, Shane P. Brady <shane@jxie.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "chat_lists.h"
#include "libmsn.h"

/*
** Name:    AddUserToChatList
** Purpose: This function adds a user to the current chat list
** Input:   chatSession   - chat list structure
**          userHandle    - user's handle
**          userFriendlyHandle - user's friendly handle 
**          index         - index of user
**          userState     - user state
** Output:  0 on success, -1 on failure
*/

int AddUserToChatList(ChatSession *chatSession, char *userHandle, 
                      char *userFriendlyHandle, int index,
                      UserState userState)
{
    GList *node;
    ChatUser *cu;
    ChatUser *newUser;

    node = g_list_find_custom(chatSession->users, userHandle, CompareUserName);
    if (node != NULL) {
        cu = node->data;
        cu->state = userState;
    }
    
    newUser = (ChatUser *)malloc(sizeof(ChatUser));
    if (newUser == NULL)
        return -1;
    strcpy(newUser->userHandle, userHandle);
    strcpy(newUser->userFriendlyHandle, userFriendlyHandle);
    newUser->index = index;
    newUser->state = userState;
 
    chatSession->users = g_list_append(chatSession->users, newUser); 
    chatSession->numOfUsers += 1;
    return 0;
}

/*
** Name:    RemoveUserFromChatList
** Purpose: This function removes a user from the list
** Input:   chatSession    - chat list structure
**          userHandle     - user handle
** Output:  0 on success, -1 on failure
*/

int RemoveUserFromChatList(ChatSession *chatSession, char *userHandle)
{
    GList     *node;

    if (chatSession->users == NULL)
        return -1;

    node = g_list_find_custom(chatSession->users, userHandle, CompareUserName);
    if (node == NULL) 
        return -1; 
    chatSession->users = g_list_remove(chatSession->users, node);
    free(node->data);
    node->data = NULL;
    chatSession->numOfUsers--;
    return 0; 
}

/*
** Name:    SearchForUser
** Purpose: This function searches a chat list and returns whether the
**          user is indeed, on a list
** Input:   chatSession - chat list structure
**          handle      - handle to search for
** Output:  0 if found, -1 if not
*/

int SearchForUser(ChatSession chatSession, char *handle)
{
    GList *node;

    node = g_list_find_custom(chatSession.users, handle, CompareUserName);
    if (node == NULL)
        return 0;
    else
        return -1;
}

/*
** Name:    ChangeUserState
** Purpose: This function changes a users state
** Input:   chatSession - chat list structure
**          userHandle  - user handle
**          newState    - new state of user
** Output:  0 on success, -1 on failure
*/

int ChangeUserState(ChatSession *chatSession, char *userHandle,
                    UserState userState)
{
    GList     *node;
    ChatUser  *cu;

    if (chatSession->users == NULL)
        return -1;

    node = g_list_find_custom(chatSession->users, userHandle, CompareUserName);
    if (node == NULL)
        return -1;
    cu = node->data;
    cu->state = userState;
    return 0;
}

/*
** Name:    DestroyChatList
** Purpose: This function destroys the list of chat users
** Input:   chatSession - chat list structure
** Output:  0 on success, -1 on failure
*/

int DestroyChatList(ChatSession chatSession)
{
    GList    *node;
    ChatUser *cu;

    for (node = chatSession.users; node; node=node->next) {
        cu = node->data;
        free(cu);
	node->data = NULL;
	cu = NULL;
    }
//    free(node->data);
    g_list_free(chatSession.users);
    chatSession.users = NULL;
    chatSession.numOfUsers = 0;
    return 0;         
}

/*
** Name:    CompareUserName
** Purpose: This function is used to compare nodes in the list
** Input:   a - node
**          b - name
** Output:  0 if found, something else
*/

gint CompareUserName(gconstpointer a, gconstpointer b)
{
    ChatUser *cu;
    char     *handle;

    handle = (char *)b;
    cu = (ChatUser *)a;

    if (!handle || !cu->userHandle)
        return (-1);

    return strcmp(handle, cu->userHandle);
}