[project @ 2002-07-07 02:33:51 by unknown_lamer]
[clinton/bobotpp.git] / source / ServerQueue.C
1 // ServerQueue.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (C) 2002 Clinton Ebadi
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
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 //#include <limits>
24 #include "ServerQueue.H"
25
26 ServerQueue::ServerQueue(Socket * s, bool d)
27 : Queue(s,d), penalty(0)
28 {
29 #ifdef HAVE_STL_CLEAR
30 serverQueue.clear();
31 #endif
32 }
33
34 ServerQueue::~ServerQueue()
35 {
36 penalty = INT_MIN;
37 flush();
38 }
39
40 void
41 ServerQueue::addItem(ServerQueueItem *sqi)
42 {
43 std::list<ServerQueueItem *>::iterator it, it2;
44
45 for (it = serverQueue.begin(); it != serverQueue.end(); ++it) {
46 if ((*it)->priority > sqi->priority)
47 break;
48 }
49 it2 = it; --it2;
50 if (it2 != serverQueue.end() && *it2) {
51 // All right, we try to merge this item to the previous
52 if ((*it2)->merge(sqi)) {
53 delete sqi;
54 return;
55 }
56 }
57 serverQueue.insert(it, sqi);
58 }
59
60 void
61 ServerQueue::addLine(String l, int pr, int pen, int t)
62 {
63 ServerQueueOtherItem * sqoi =
64 new ServerQueueOtherItem(l, pr, pen, t);
65 addItem(sqoi);
66 }
67
68 bool
69 ServerQueue::flush()
70 {
71 // Called every second, we decrement the penalty
72 if (penalty > 0)
73 penalty--;
74
75 while (!serverQueue.empty() && (penalty < 5)) {
76 ServerQueueItem * sqi = (*serverQueue.begin());
77 penalty += sqi->penalty + sqi->getLine().length()/100 + 1;
78 bool res = sendLine(sqi->getLine());
79 serverQueue.erase(serverQueue.begin());
80 delete sqi;
81 if (!res)
82 return false;
83 }
84 return true;
85 }
86
87 void
88 ServerQueue::sendCTCP(String to, String command,
89 String message)
90 {
91 sendPrivmsg(to, String("\001") + command + " " + message + "\001");
92 }
93
94 void
95 ServerQueue::sendCTCPReply(String to, String command,
96 String message)
97 {
98 sendNotice(to, String("\001") + command + " " +
99 message + "\001");
100 }
101
102 void
103 ServerQueue::sendChannelMode(String mode)
104 {
105 addLine(mode, CHANNELMODE_PRIORITY, CHANNELMODE_PENALTY,
106 ServerQueueItem::CHANNELMODE);
107 }
108
109 void
110 ServerQueue::sendChannelMode(String channel, String mode, String parameters)
111 {
112 ServerQueueChannelModeItem * sqcmi =
113 new ServerQueueChannelModeItem(channel, mode, parameters);
114 addItem(sqcmi);
115 }
116
117 void
118 ServerQueue::sendInvite(String channel, String nick)
119 {
120 addLine(String("INVITE ") + nick + " " + channel,
121 INVITE_PRIORITY, INVITE_PENALTY, ServerQueueItem::INVITE);
122 }
123
124 void
125 ServerQueue::sendJoin(String channel, String key)
126 {
127 addLine(String("JOIN ") + channel + " " + key,
128 JOIN_PRIORITY, JOIN_PENALTY, ServerQueueItem::JOIN);
129 }
130
131 void
132 ServerQueue::sendKick(String channel, String nick, String reason)
133 {
134 ServerQueueKickItem * sqki =
135 new ServerQueueKickItem(channel, nick, reason);
136 addItem(sqki);
137 }
138
139 void
140 ServerQueue::sendNick(String nick)
141 {
142 addLine(String("NICK ") + nick,
143 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::NICK);
144 }
145
146 void
147 ServerQueue::sendNotice(String to, String message)
148 {
149 ServerQueueNoticeItem *sqni =
150 new ServerQueueNoticeItem(to, message);
151 addItem(sqni);
152 }
153
154 void
155 ServerQueue::sendPart(String channel)
156 {
157 addLine(String("PART ") + channel,
158 PART_PRIORITY, PART_PENALTY, ServerQueueItem::PART);
159 }
160
161 void
162 ServerQueue::sendPass(String pass)
163 {
164 addLine(String("PASS ") + pass,
165 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::PASS);
166 }
167
168 void
169 ServerQueue::sendPing(String server)
170 {
171 addLine(String("PING :") + server,
172 PING_PRIORITY, PING_PENALTY, ServerQueueItem::PING);
173 }
174
175 void
176 ServerQueue::sendPong(String crap)
177 {
178 addLine(String("PONG ") + crap,
179 PONG_PRIORITY, PONG_PENALTY, ServerQueueItem::PONG);
180 }
181
182 void
183 ServerQueue::sendPrivmsg(String dest, String message)
184 {
185 addLine(String("PRIVMSG ") + dest + " :" + message,
186 PRIVMSG_PRIORITY, PRIVMSG_PENALTY, ServerQueueItem::PRIVMSG);
187 }
188
189 void
190 ServerQueue::sendQuit(String reason)
191 {
192 addLine(String("QUIT :") + reason,
193 QUIT_PRIORITY, QUIT_PENALTY, ServerQueueItem::QUIT);
194 }
195
196 void
197 ServerQueue::sendTopic(String channel, String topic)
198 {
199 addLine(String("TOPIC ") + channel + " :" + topic,
200 TOPIC_PRIORITY, TOPIC_PENALTY, ServerQueueItem::TOPIC);
201 }
202
203 void
204 ServerQueue::sendUser(String username, String ircname)
205 {
206 addLine(String("USER ") + username + " . . :" + ircname,
207 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::USER);
208 }
209
210 void
211 ServerQueue::sendUserMode(String nick, String mode)
212 {
213 addLine(String("MODE ") + nick + " " + mode,
214 USERMODE_PRIORITY, USERMODE_PENALTY,
215 ServerQueueItem::USERMODE);
216 }
217
218 void
219 ServerQueue::sendUserhost(String nick)
220 {
221 addLine(String("USERHOST ") + nick,
222 USERHOST_PRIORITY, USERHOST_PENALTY, ServerQueueItem::USERHOST);
223 }
224
225 void
226 ServerQueue::sendWho(String who)
227 {
228 addLine(String("WHO ") + who,
229 WHO_PRIORITY, WHO_PENALTY, ServerQueueItem::WHO);
230 }
231
232 void
233 ServerQueue::sendWhois(String nick)
234 {
235 addLine(String("WHOIS ") + nick,
236 NICK_PRIORITY, WHOIS_PENALTY, ServerQueueItem::WHOIS);
237 }