forked from pmer/yak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presence.c
62 lines (51 loc) · 1.27 KB
/
presence.c
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
#include <stdlib.h>
#include "chan.h"
#include "diagnostic.h"
#include "link.h"
#include "presence.h"
#include "usr.h"
struct presence *presence_new(struct chan *chan, struct user *user)
{
struct presence *pres;
pres = malloc(sizeof(struct presence));
pres->chan = chan;
pres->user = user;
pres->priv = PRIV_NONE;
list_add(&pres->clink, &chan->presences);
list_add(&pres->ulink, &user->presences);
return pres;
}
void presence_del(struct presence *pres)
{
struct user *user = pres->user;
list_del(&pres->clink);
list_del(&pres->ulink);
free(pres);
if (list_empty(&user->presences))
user_del(user);
}
struct presence *presence_find(struct chan *chan, struct user *user)
{
struct list_head *pos;
struct presence *pres;
list_for_each(pos, &user->presences) {
pres = list_entry(pos, struct presence, ulink);
if (pres->chan == chan)
return pres;
}
return NULL;
}
struct presence *presence_get(struct chan *chan, struct user *user)
{
struct presence *pres;
pres = presence_find(chan, user);
return pres ? pres : presence_new(chan, user);
}
struct presence *presence_get3(struct chan *chan, struct user *user,
bool *existed)
{
struct presence *pres;
pres = presence_find(chan, user);
*existed = pres != NULL;
return pres ? pres : presence_new(chan, user);
}