cb21075d |
1 | // ServerQueue.C -*- C++ -*- |
2 | // Copyright (c) 1997, 1998 Etienne BERNARD |
46af1667 |
3 | // Copyright (C) 2002,2005,2008 Clinton Ebadi |
cb21075d |
4 | |
5 | // This program is free software; you can redistribute it and/or modify |
6 | // it under the terms of the GNU General Public License as published by |
7 | // the Free Software Foundation; either version 2 of the License, or |
8 | // any later version. |
9 | |
10 | // This program is distributed in the hope that it will be useful, |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | // GNU General Public License for more details. |
14 | |
15 | // You should have received a copy of the GNU General Public License |
16 | // along with this program; if not, write to the Free Software |
c99c411a |
17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 | // 02110-1301, USA. |
cb21075d |
19 | |
20 | #ifdef HAVE_CONFIG_H |
21 | #include "config.h" |
22 | #endif |
23 | |
4ce3a244 |
24 | #include <limits> |
cb21075d |
25 | #include "ServerQueue.H" |
fd7440f1 |
26 | #include "Utils.H" |
cb21075d |
27 | |
28 | ServerQueue::ServerQueue(Socket * s, bool d) |
29 | : Queue(s,d), penalty(0) |
30 | { |
31 | #ifdef HAVE_STL_CLEAR |
32 | serverQueue.clear(); |
33 | #endif |
34 | } |
35 | |
36 | ServerQueue::~ServerQueue() |
37 | { |
4ce3a244 |
38 | penalty = std::numeric_limits<int>::min (); |
cb21075d |
39 | flush(); |
40 | } |
41 | |
42 | void |
43 | ServerQueue::addItem(ServerQueueItem *sqi) |
44 | { |
45 | std::list<ServerQueueItem *>::iterator it, it2; |
46 | |
c99c411a |
47 | for (it = serverQueue.begin(); it != serverQueue.end(); ++it) |
48 | { |
94a03421 |
49 | if (**it < *sqi) |
c99c411a |
50 | { |
51 | break; |
52 | } |
53 | } |
54 | |
55 | it2 = it; |
56 | --it2; |
57 | if (it2 != serverQueue.end() && *it2) |
58 | { |
59 | // All right, we try to merge this item to the previous |
60 | if ((*it2)->merge(sqi)) |
61 | { |
62 | delete sqi; |
63 | return; |
64 | } |
cb21075d |
65 | } |
c99c411a |
66 | |
cb21075d |
67 | serverQueue.insert(it, sqi); |
68 | } |
69 | |
70 | void |
71 | ServerQueue::addLine(String l, int pr, int pen, int t) |
72 | { |
73 | ServerQueueOtherItem * sqoi = |
74 | new ServerQueueOtherItem(l, pr, pen, t); |
75 | addItem(sqoi); |
76 | } |
77 | |
78 | bool |
79 | ServerQueue::flush() |
80 | { |
81 | // Called every second, we decrement the penalty |
82 | if (penalty > 0) |
c99c411a |
83 | { |
84 | penalty--; |
85 | } |
86 | |
87 | while (!serverQueue.empty() && (penalty < 5)) |
88 | { |
89 | ServerQueueItem * sqi = (*serverQueue.begin()); |
90 | penalty += sqi->penalty + sqi->getLine().length()/100 + 1; |
91 | |
92 | bool res = sendLine(sqi->getLine()); |
93 | serverQueue.erase(serverQueue.begin()); |
94 | delete sqi; |
95 | |
96 | if (!res) |
97 | { |
98 | return false; |
99 | } |
100 | } |
101 | |
cb21075d |
102 | return true; |
103 | } |
104 | |
fd7440f1 |
105 | #define MNICK (Interp::bot->nickName + "!" + Interp::bot->userHost) |
cb21075d |
106 | void |
107 | ServerQueue::sendCTCP(String to, String command, |
108 | String message) |
109 | { |
110 | sendPrivmsg(to, String("\001") + command + " " + message + "\001"); |
fd7440f1 |
111 | |
fd7440f1 |
112 | #ifdef USESCRIPTS |
113 | if (command == "ACTION") |
114 | { |
fed59248 |
115 | Interp::bot->botInterp->RunHooks (Hook::SEND_ACTION, |
116 | MNICK + " " + to + |
fd7440f1 |
117 | " " + message, |
46af1667 |
118 | scm_list_n (Utils:: |
a6339323 |
119 | str2scm (MNICK), |
fd7440f1 |
120 | Utils:: |
a6339323 |
121 | str2scm (to), |
fd7440f1 |
122 | Utils:: |
a6339323 |
123 | str2scm (message), |
fd7440f1 |
124 | SCM_UNDEFINED)); |
125 | } |
fed59248 |
126 | else |
127 | Interp::bot->botInterp->RunHooks (Hook::SEND_CTCP, |
c99c411a |
128 | MNICK + " " + to + " " + |
129 | command + " " + message, |
46af1667 |
130 | scm_list_n (Utils:: |
c99c411a |
131 | str2scm (MNICK), |
132 | Utils:: |
133 | str2scm (to), |
134 | Utils:: |
135 | str2scm |
136 | (command), |
137 | Utils:: |
138 | str2scm (message), |
139 | SCM_UNDEFINED)); |
fd7440f1 |
140 | #endif |
141 | |
cb21075d |
142 | } |
fd7440f1 |
143 | #undef MNICK |
cb21075d |
144 | |
145 | void |
146 | ServerQueue::sendCTCPReply(String to, String command, |
147 | String message) |
148 | { |
149 | sendNotice(to, String("\001") + command + " " + |
150 | message + "\001"); |
151 | } |
152 | |
153 | void |
154 | ServerQueue::sendChannelMode(String mode) |
155 | { |
156 | addLine(mode, CHANNELMODE_PRIORITY, CHANNELMODE_PENALTY, |
157 | ServerQueueItem::CHANNELMODE); |
158 | } |
159 | |
160 | void |
161 | ServerQueue::sendChannelMode(String channel, String mode, String parameters) |
162 | { |
163 | ServerQueueChannelModeItem * sqcmi = |
164 | new ServerQueueChannelModeItem(channel, mode, parameters); |
165 | addItem(sqcmi); |
166 | } |
167 | |
168 | void |
169 | ServerQueue::sendInvite(String channel, String nick) |
170 | { |
171 | addLine(String("INVITE ") + nick + " " + channel, |
172 | INVITE_PRIORITY, INVITE_PENALTY, ServerQueueItem::INVITE); |
173 | } |
174 | |
175 | void |
176 | ServerQueue::sendJoin(String channel, String key) |
177 | { |
178 | addLine(String("JOIN ") + channel + " " + key, |
179 | JOIN_PRIORITY, JOIN_PENALTY, ServerQueueItem::JOIN); |
180 | } |
181 | |
182 | void |
183 | ServerQueue::sendKick(String channel, String nick, String reason) |
184 | { |
185 | ServerQueueKickItem * sqki = |
186 | new ServerQueueKickItem(channel, nick, reason); |
187 | addItem(sqki); |
188 | } |
189 | |
190 | void |
191 | ServerQueue::sendNick(String nick) |
192 | { |
193 | addLine(String("NICK ") + nick, |
194 | NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::NICK); |
195 | } |
196 | |
197 | void |
198 | ServerQueue::sendNotice(String to, String message) |
199 | { |
200 | ServerQueueNoticeItem *sqni = |
201 | new ServerQueueNoticeItem(to, message); |
202 | addItem(sqni); |
203 | } |
204 | |
205 | void |
206 | ServerQueue::sendPart(String channel) |
207 | { |
208 | addLine(String("PART ") + channel, |
209 | PART_PRIORITY, PART_PENALTY, ServerQueueItem::PART); |
210 | } |
211 | |
212 | void |
213 | ServerQueue::sendPass(String pass) |
214 | { |
215 | addLine(String("PASS ") + pass, |
216 | NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::PASS); |
217 | } |
218 | |
219 | void |
220 | ServerQueue::sendPing(String server) |
221 | { |
222 | addLine(String("PING :") + server, |
223 | PING_PRIORITY, PING_PENALTY, ServerQueueItem::PING); |
224 | } |
225 | |
226 | void |
227 | ServerQueue::sendPong(String crap) |
228 | { |
229 | addLine(String("PONG ") + crap, |
230 | PONG_PRIORITY, PONG_PENALTY, ServerQueueItem::PONG); |
231 | } |
232 | |
233 | void |
234 | ServerQueue::sendPrivmsg(String dest, String message) |
235 | { |
236 | addLine(String("PRIVMSG ") + dest + " :" + message, |
237 | PRIVMSG_PRIORITY, PRIVMSG_PENALTY, ServerQueueItem::PRIVMSG); |
fd7440f1 |
238 | // hook stuff |
239 | #ifdef USESCRIPTS |
240 | if (message[0] != '\001') |
a6339323 |
241 | if (Utils::channel_p (dest)) |
fed59248 |
242 | Interp::bot->botInterp->RunHooks (Hook::SEND_PUBLIC, |
fd7440f1 |
243 | Interp::bot->nickName + " " + dest + |
244 | " " + message, |
46af1667 |
245 | scm_list_n |
c99c411a |
246 | (Utils::str2scm |
247 | (Interp::bot->nickName), |
248 | Utils::str2scm (dest), |
249 | Utils::str2scm (message), |
250 | SCM_UNDEFINED)); |
fd7440f1 |
251 | else |
ae97d6ec |
252 | Interp::bot->botInterp->RunHooks |
253 | (Hook::SEND_MESSAGE, |
254 | Interp::bot->nickName + " " + dest + |
255 | message, |
46af1667 |
256 | scm_list_n (Utils::str2scm (Interp::bot->nickName), |
ae97d6ec |
257 | Utils::str2scm (dest), |
258 | Utils::str2scm |
259 | (message), SCM_UNDEFINED)); |
fd7440f1 |
260 | #endif |
cb21075d |
261 | } |
262 | |
263 | void |
264 | ServerQueue::sendQuit(String reason) |
265 | { |
266 | addLine(String("QUIT :") + reason, |
267 | QUIT_PRIORITY, QUIT_PENALTY, ServerQueueItem::QUIT); |
268 | } |
269 | |
270 | void |
271 | ServerQueue::sendTopic(String channel, String topic) |
272 | { |
273 | addLine(String("TOPIC ") + channel + " :" + topic, |
274 | TOPIC_PRIORITY, TOPIC_PENALTY, ServerQueueItem::TOPIC); |
275 | } |
276 | |
277 | void |
278 | ServerQueue::sendUser(String username, String ircname) |
279 | { |
439869bf |
280 | addLine(String("USER ") + username + " 0 * :" + ircname, |
cb21075d |
281 | NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::USER); |
282 | } |
283 | |
284 | void |
285 | ServerQueue::sendUserMode(String nick, String mode) |
286 | { |
287 | addLine(String("MODE ") + nick + " " + mode, |
288 | USERMODE_PRIORITY, USERMODE_PENALTY, |
289 | ServerQueueItem::USERMODE); |
290 | } |
291 | |
292 | void |
293 | ServerQueue::sendUserhost(String nick) |
294 | { |
295 | addLine(String("USERHOST ") + nick, |
296 | USERHOST_PRIORITY, USERHOST_PENALTY, ServerQueueItem::USERHOST); |
297 | } |
298 | |
299 | void |
300 | ServerQueue::sendWho(String who) |
301 | { |
302 | addLine(String("WHO ") + who, |
303 | WHO_PRIORITY, WHO_PENALTY, ServerQueueItem::WHO); |
528799bd |
304 | |
305 | #ifdef USESCRIPTS |
306 | Interp::bot->botInterp->RunHooks (Hook::SEND_WHO, |
307 | who, |
308 | scm_list_n (Utils:: |
309 | str2scm (who), |
310 | SCM_UNDEFINED)); |
311 | #endif |
cb21075d |
312 | } |
313 | |
314 | void |
315 | ServerQueue::sendWhois(String nick) |
316 | { |
317 | addLine(String("WHOIS ") + nick, |
318 | NICK_PRIORITY, WHOIS_PENALTY, ServerQueueItem::WHOIS); |
528799bd |
319 | |
320 | #ifdef USESCRIPTS |
321 | Interp::bot->botInterp->RunHooks (Hook::SEND_WHOIS, |
322 | nick, |
323 | scm_list_n (Utils:: |
324 | str2scm (nick), |
325 | SCM_UNDEFINED)); |
326 | #endif |
cb21075d |
327 | } |