11218
|
1 #include <assert.h>
|
|
2 #include <stdlib.h>
|
|
3 #include <string.h>
|
|
4 #include "cc_interface.h"
|
|
5
|
|
6 struct cc_session_node {
|
|
7 struct cc_session session;
|
|
8 struct cc_session_node *next;
|
|
9 };
|
|
10
|
|
11 struct cc_session *cc_find_session(struct crazychat *cc, char *handle)
|
|
12 {
|
|
13 struct cc_session_node *curr;
|
|
14
|
|
15 assert(cc);
|
|
16 assert(handle);
|
|
17
|
|
18 curr = cc->sessions;
|
|
19 while (curr) {
|
|
20 struct cc_session *session = &curr->session;
|
|
21 if (!strncmp(session->name, handle, strlen(session->name))) {
|
|
22 return session;
|
|
23 }
|
|
24 curr = curr->next;
|
|
25 }
|
|
26 return NULL;
|
|
27 }
|
|
28
|
|
29 struct cc_session *cc_add_session(struct crazychat *cc, char *handle)
|
|
30 {
|
|
31 struct cc_session_node *curr;
|
|
32
|
|
33 assert(cc);
|
|
34 assert(handle);
|
|
35
|
|
36 if (!cc->sessions) {
|
|
37 cc->sessions = (struct cc_session_node*)
|
|
38 malloc(sizeof(*cc->sessions));
|
|
39 memset(cc->sessions, 0, sizeof(*cc->sessions));
|
|
40 cc->sessions->session.cc = cc;
|
|
41 cc->sessions->session.name = strdup(handle);
|
|
42 return &cc->sessions->session;
|
|
43 } else {
|
|
44 if (!strncmp(cc->sessions->session.name, handle,
|
|
45 strlen(cc->sessions->session.name))) {
|
|
46 return &cc->sessions->session;
|
|
47 }
|
|
48 }
|
|
49
|
|
50 curr = cc->sessions;
|
|
51 while (curr->next) {
|
|
52 struct cc_session *session = &curr->next->session;
|
|
53 if (!strncmp(session->name, handle, strlen(session->name))) {
|
|
54 return session;
|
|
55 }
|
|
56 curr = curr->next;
|
|
57 }
|
|
58 curr->next = (struct cc_session_node*)malloc(sizeof(*curr->next));
|
|
59 memset(curr->next, 0, sizeof(*curr->next));
|
|
60 curr->next->session.cc = cc;
|
|
61 curr->next->session.name = strdup(handle);
|
|
62 return &curr->next->session;
|
|
63 }
|
|
64
|
|
65 void cc_remove_session(struct crazychat *cc, struct cc_session *session)
|
|
66 {
|
|
67 struct cc_session_node *curr, *prev;
|
|
68
|
|
69 assert(cc);
|
|
70 assert(session);
|
|
71
|
|
72 assert(cc->sessions);
|
|
73
|
|
74 curr = cc->sessions;
|
|
75 prev = NULL;
|
|
76
|
|
77 while (curr) {
|
|
78 if (&curr->session == session) {
|
|
79 if (prev) {
|
|
80 prev->next = curr->next;
|
|
81 } else {
|
|
82 cc->sessions = curr->next;
|
|
83 }
|
|
84 /* destroy curr */
|
|
85 free(curr);
|
|
86 g_source_remove(session->timer_id);
|
|
87 free(session->name);
|
|
88 free(session);
|
|
89 return;
|
|
90 }
|
|
91 prev = curr;
|
|
92 curr = curr->next;
|
|
93 }
|
|
94
|
|
95 assert(0);
|
|
96 }
|