1 /* IRCQueue.c - NeonServ v5.5
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "ClientSocket.h"
19 #include "IOHandler.h"
21 #define MAXPENALTY 8 /* 4 messages */
25 struct QueueEntry *next;
29 struct ClientSocket *client;
30 struct IODescriptor *iofd;
33 struct QueueEntry *fastqueue_first, *fastqueue_last;
34 struct QueueEntry *normalqueue_first, *normalqueue_last;
35 struct QueueEntry *textqueue_first, *textqueue_last;
38 static IOHANDLER_CALLBACK(queue_callback);
40 static struct BotQueue *initialize_queue(struct ClientSocket *client) {
41 struct BotQueue *queue = malloc(sizeof(*queue));
43 perror("malloc() failed");
46 queue->client = client;
47 client->queue = queue;
50 queue->fastqueue_first = NULL;
51 queue->fastqueue_last = NULL;
52 queue->normalqueue_first = NULL;
53 queue->normalqueue_last = NULL;
54 queue->textqueue_first = NULL;
55 queue->textqueue_last = NULL;
59 static int calculate_penalty(char *message) {
60 int msglen = strlen(message);
61 int penalty = (2 + msglen / 100);
65 int queue_add(struct ClientSocket *client, char* msg, int len) {
67 client->queue = initialize_queue(client);
68 struct BotQueue *queue = client->queue;
69 char *args = strstr(msg, " ");
74 if(!stricmp(msg, "MODE"))
76 else if(!stricmp(msg, "KICK"))
78 else if(!stricmp(msg, "PONG"))
80 else if(!stricmp(msg, "PRIVMSG"))
82 else if(!stricmp(msg, "NOTICE"))
84 else if(!stricmp(msg, "WHO"))
92 //check if we need to queue
95 if(queue->textqueue_first) {
100 if(queue->normalqueue_first) {
105 if(queue->fastqueue_first) {
110 if(queue->penalty >= MAXPENALTY)
116 int penalty = calculate_penalty(msg);
117 write_socket_force(client, msg, len);
118 queue->penalty += penalty;
120 struct timeval timeout;
121 gettimeofday(&timeout, NULL);
122 if(queue->penalty >= MAXPENALTY)
123 queue->rem_penalty = (queue->penalty - MAXPENALTY)+1;
125 queue->rem_penalty = queue->penalty;
126 timeout.tv_sec += queue->rem_penalty;
127 queue->iofd = iohandler_timer(timeout, queue_callback);
128 queue->iofd->data = queue;
131 struct QueueEntry *entry = malloc(sizeof(*entry));
133 perror("malloc() failed");
136 entry->msg = strdup(msg);
138 if(type == 1) { //low priority
139 if(queue->textqueue_last) {
140 queue->textqueue_last->next = entry;
141 queue->textqueue_last = entry;
143 queue->textqueue_last = entry;
144 queue->textqueue_first = entry;
146 } else if(type == 2) { //normal priority
147 if(queue->normalqueue_last) {
148 queue->normalqueue_last->next = entry;
149 queue->normalqueue_last = entry;
151 queue->normalqueue_last = entry;
152 queue->normalqueue_first = entry;
154 } else if(type == 3) { //high priority
155 if(queue->fastqueue_last) {
156 queue->fastqueue_last->next = entry;
157 queue->fastqueue_last = entry;
159 queue->fastqueue_last = entry;
160 queue->fastqueue_first = entry;
167 static void dequeue_bot(struct ClientSocket *client) {
168 if(client->queue->penalty >= MAXPENALTY) return;
170 //try to send high priority messages
171 if(client->queue->fastqueue_first) {
173 struct QueueEntry *entry = client->queue->fastqueue_first;
175 client->queue->fastqueue_last = NULL;
176 client->queue->fastqueue_first = client->queue->fastqueue_first->next;
177 penalty = calculate_penalty(entry->msg);
178 write_socket_force(client, entry->msg, strlen(entry->msg));
179 client->queue->penalty += penalty;
182 } while(client->queue->penalty < MAXPENALTY && client->queue->fastqueue_first);
184 if(client->queue->penalty >= MAXPENALTY) return;
185 //try to send normal priority messages
186 if(client->queue->normalqueue_first) {
188 struct QueueEntry *entry = client->queue->normalqueue_first;
190 client->queue->normalqueue_last = NULL;
191 client->queue->normalqueue_first = client->queue->normalqueue_first->next;
192 penalty = calculate_penalty(entry->msg);
193 write_socket_force(client, entry->msg, strlen(entry->msg));
194 client->queue->penalty += penalty;
197 } while(client->queue->penalty < MAXPENALTY && client->queue->normalqueue_first);
199 if(client->queue->penalty >= MAXPENALTY) return;
200 //try to send low priority messages
201 if(client->queue->textqueue_first) {
203 struct QueueEntry *entry = client->queue->textqueue_first;
205 client->queue->textqueue_last = NULL;
206 client->queue->textqueue_first = client->queue->textqueue_first->next;
207 penalty = calculate_penalty(entry->msg);
208 write_socket_force(client, entry->msg, strlen(entry->msg));
209 client->queue->penalty += penalty;
212 } while(client->queue->penalty < MAXPENALTY && client->queue->textqueue_first);
216 void queue_destroy(struct ClientSocket *client) {
217 if(!client->queue) return;
218 struct QueueEntry *entry, *next;
219 for(entry = client->queue->fastqueue_first; entry; entry = next) {
224 for(entry = client->queue->normalqueue_first; entry; entry = next) {
229 for(entry = client->queue->textqueue_first; entry; entry = next) {
234 if(client->queue->iofd)
235 iohandler_close(client->queue->iofd);
237 client->queue = NULL;
240 static IOHANDLER_CALLBACK(queue_callback) {
241 struct BotQueue *queue = event->iofd->data;
242 switch(event->type) {
243 case IOEVENT_TIMEOUT:
244 queue->penalty -= queue->rem_penalty;
245 dequeue_bot(queue->client);
246 if(queue->penalty > 0) {
247 struct timeval timeout;
248 gettimeofday(&timeout, NULL);
249 if(queue->penalty >= MAXPENALTY)
250 queue->rem_penalty = (queue->penalty - MAXPENALTY)+1;
252 queue->rem_penalty = queue->penalty;
253 timeout.tv_sec += queue->rem_penalty;
254 queue->iofd = iohandler_timer(timeout, queue_callback);
255 queue->iofd->data = queue;