fixed syntax of last commit
[NeonServV5.git] / ChanNode.c
1 #include "ChanNode.h"
2 #include "ChanUser.h"
3 #include "UserNode.h"
4
5 static struct ChanNode **chanList;
6
7 void init_ChanNode() {
8     /*
9      len pos chars 
10      26  0   a-z
11      10  26  0-9
12      10  36  {|}~[\]^_`
13      1   46  *everything else*
14      ---------------------------
15      = 47
16     */
17     chanList = calloc(47, sizeof(*chanList));
18 }
19
20 int is_valid_chan(const char *name) {
21     unsigned int ii;
22     if (*name !='#')
23         return 0;
24     for (ii=1; name[ii]; ++ii) {
25         if ((name[ii] > 0) && (name[ii] <= 32))
26             return 0;
27         if (name[ii] == ',')
28             return 0;
29         if (name[ii] == '\xa0')
30             return 0;
31     }
32     return 1;
33 }
34
35 static int get_chanlist_entry(int name) {
36     if((name > 0 && name <= 32) || name == ',' || name == '\xa0') return -1; //invalid name
37     if(tolower(name) >= 97 && tolower(name) <= 122) {
38         return (tolower(name) - 97);
39     }
40     if(tolower(name) >= 48 && tolower(name) <= 57) {
41         return (tolower(name) - 48 + 26);
42     }
43     /* {|}~[\]^_` */
44     if(name == '{') return 36;
45     if(name == '|') return 37;
46     if(name == '}') return 38;
47     if(name == '~') return 39;
48     if(name == '[') return 40;
49     if(name == '\\') return 41;
50     if(name == ']') return 42;
51     if(name == '^') return 43;
52     if(name == '_') return 44;
53     if(name == '`') return 45;
54     return 46;
55 }
56
57 struct ChanNode* getChanByName(const char *name) { //case insensitive
58     int chanListIndex = get_chanlist_entry(name[1]);
59     if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
60         return NULL;
61     struct ChanNode *chan;
62     for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
63         if(!stricmp(name, chan->name))
64             return chan;
65     }
66     return NULL;
67 }
68
69 struct ChanNode* addChannel(const char *name) {
70     int chanListIndex = get_chanlist_entry(name[1]);
71     if(chanListIndex == -1 || !is_valid_chan(name))
72         return NULL;
73     struct ChanNode *chan = malloc(sizeof(*chan));
74     if (!chan)
75     {
76         perror("malloc() failed");
77         return NULL;
78     }
79     strcpy(chan->name, name);
80     chan->user = NULL;
81     chan->topic[0] = 0;
82     chan->flags = 0;
83     chan->next = chanList[chanListIndex];
84     chanList[chanListIndex] = chan;
85     return chan;
86 }
87
88 void delChannel(struct ChanNode* chan, int freeChan) {
89     int chanListIndex = get_chanlist_entry(chan->name[1]);
90     if(chanListIndex == -1) return;
91     struct ChanNode *cchan, *last_chan = NULL;
92     for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
93         if(cchan == chan) {
94             if(last_chan)
95                 last_chan->next = chan->next;
96             else
97                 chanList[chanListIndex] = chan->next;
98             break;
99         } else
100             last_chan = cchan;
101     }
102     if(chan->user) {
103         //free all chanusers
104         struct ChanUser *chanuser, *next;
105         for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
106             next = getChannelUsers(chan, chanuser);
107             removeChanUserFromLists(chanuser, 0, 1, 1);
108         }
109     }
110     if(freeChan)
111         free(chan);
112     else
113         chan->next = NULL;
114 }
115
116 void checkChannelVisibility(struct ChanNode* chan) {
117     struct ChanUser *chanuser, *next;
118     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
119         if(chanuser->user->flags & USERFLAG_ISBOT) return;
120     }
121     //free the channel...
122     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
123         next = getChannelUsers(chan, chanuser);
124         //remove the channel from the user's channel-list
125         removeChanUserFromLists(chanuser, 0, 1, 0);
126         if(!chanuser->user->channel) {
127             //free the user (no more channels)
128             delUser(chanuser->user, 1);
129         }
130         free(chanuser);
131     }
132     chan->user = NULL;
133     delChannel(chan, 1);
134 }