/
channel.c
186 lines (150 loc) · 5.38 KB
/
channel.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
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
184
185
#include "channel.h"
#include "server.h"
#include "client.h"
static int lirc_channel_cmp(const void *channel, const void *c_str);
static LIRCChannelData* lirc_channel_new_metadata()
{
LIRCChannelData* channel = safe_malloc(sizeof(LIRCChannelData));
/* Initialize default values for new channel */
channel->name = NULL;
channel->client_list = make_empty_list();
return channel;
}
static int lirc_channel_cmp(const void *channel, const void *c_str)
{
LIRCChannelData* chan = (LIRCChannelData*)channel;
char *c = (char *)c_str;
return strcmp(chan->name, c);
}
static int lirc_client_cmp(const void *client1, const void *client2)
{
return (client1 == client2) ? 0 : -1;
}
void lirc_channel_join(struct LIRCServer_struct* server,
struct LIRCClientData_struct* client,
char *channel)
{
char temp[MAX_IRC_MESSAGE_SIZE];
dlnode_t* client_node;
LIRCChannelData* c =
(LIRCChannelData*)find_element(server->channel_list,
channel, lirc_channel_cmp);
if (c == NULL)
{
/* The channel doesn't exist in the list so we
* will need to create one */
c = lirc_channel_new_metadata();
c->name = safe_strdup(channel);
insert_at_front(server->channel_list, c, sizeof(c));
}
/* Add this client to the list */
insert_at_front(c->client_list, client,
sizeof(client));
/* Add a weak reference to the joined channels list of the client */
insert_at_front(client->channel_list, c, sizeof(c));
/* Send a response message to all connected clients inside the
* channel indicating that a join was made */
client_node = c->client_list->head;
sprintf(temp, ":%s!~%s@anon.com JOIN %s\r\n", client->nick, client->user,
channel);
while (client_node != NULL)
{
struct LIRCClientData_struct *client_node_data =
(struct LIRCClientData_struct *)client_node->data;
send(client_node_data->socket, temp, strlen(temp), 0);
client_node = client_node->next;
}
}
void lirc_channel_who(struct LIRCServer_struct* server,
struct LIRCClientData_struct* client,
char *channel)
{
int size = 0;
char temp[MAX_IRC_MESSAGE_SIZE];
LIRCChannelData* c;
dlnode_t* client_node;
c = (LIRCChannelData*)find_element(server->channel_list,
channel, lirc_channel_cmp);
client_node = c->client_list->head;
memset(temp, '\0', sizeof (char) * MAX_IRC_MESSAGE_SIZE);
sprintf(temp, ":lirc.org 353 %s = %s :", client->nick, channel);
size += strlen(temp);
while (client_node != NULL)
{
struct LIRCClientData_struct* chan_client =
((struct LIRCClientData_struct*)(client_node->data));
strcat(temp, chan_client->nick);
strcat(temp, " ");
client_node = client_node->next;
}
/* Minus 1 to remove the space at the end of the list */
strcat(temp, "\r\n");
send(client->socket, temp, strlen(temp), 0);
/* Tell the IRC client that we're done listing client names */
memset(temp, '\0', sizeof (char) * MAX_IRC_MESSAGE_SIZE);
sprintf(temp, ":lirc.org 366 %s %s :End of /NAMES list\r\n",
client->nick, channel);
send(client->socket, temp, strlen(temp), 0);
}
void lirc_channel_message(struct LIRCServer_struct* server,
struct LIRCClientData_struct* client,
char *channel, char *message)
{
char temp[MAX_IRC_MESSAGE_SIZE];
dlnode_t* client_node;
LIRCChannelData* c =
(LIRCChannelData*)find_element(server->channel_list,
channel, lirc_channel_cmp);
if (c == NULL)
{
return;
}
/* Send a message to all connected clients inside the channel */
client_node = c->client_list->head;
sprintf(temp, ":%s!~%s@anon.com PRIVMSG %s %s\r\n",
client->nick, client->user, channel, message);
while (client_node != NULL)
{
struct LIRCClientData_struct *client_node_data =
(struct LIRCClientData_struct *)client_node->data;
if (client_node_data == client)
{
client_node = client_node->next;
continue;
}
send(client_node_data->socket, temp, strlen(temp), 0);
client_node = client_node->next;
}
}
void lirc_channel_leave(struct LIRCServer_struct* server,
struct LIRCClientData_struct* client,
char *channel)
{
char temp[MAX_IRC_MESSAGE_SIZE];
dlnode_t* client_node;
LIRCChannelData* c =
(LIRCChannelData*)find_element(server->channel_list,
channel, lirc_channel_cmp);
if (c == NULL)
{
return;
}
/* Remove weak reference to the joined channels list of the client */
remove_element(client->channel_list, c, lirc_channel_cmp);
/* Remove this client from the list */
remove_element(c->client_list, client, lirc_client_cmp);
/* Send a response message to all remaining clients inside the
* channel indicating that a part was made */
client_node = c->client_list->head;
sprintf(temp, ":%s!~%s@anon.com PART %s\r\n", client->nick, client->user,
channel);
while (client_node != NULL)
{
struct LIRCClientData_struct *client_node_data =
(struct LIRCClientData_struct *)client_node->data;
send(client_node_data->socket, temp, strlen(temp), 0);
client_node = client_node->next;
}
/* Finally let the client know the part was successful */
send(client->socket, temp, strlen(temp), 0);
}