cb21075d |
1 | // ServerQueueItem.C -*- C++ -*- |
2 | // Copyright (c) 1998 Etienne BERNARD |
a6339323 |
3 | // Copyright (c) 2005 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 | |
cb21075d |
20 | #include "ServerQueueItem.H" |
21 | |
cfa82921 |
22 | #include "ServerQueue.H" |
23 | #include "StringTokenizer.H" |
24 | |
cb21075d |
25 | ServerQueueItem::ServerQueueItem(int pr, int pen, int t) |
26 | : priority(pr), penalty(pen), type(t) |
27 | { } |
28 | |
cb21075d |
29 | ServerQueueOtherItem::ServerQueueOtherItem(String l, int pr, |
30 | int pen, int t) |
31 | : ServerQueueItem(pr, pen, t), line(l) |
32 | { } |
33 | |
94a03421 |
34 | bool ServerQueueItem::operator< (const ServerQueueItem & item) |
35 | { |
36 | // lower numeric priority is higher priority |
37 | return item.priority < priority; |
38 | } |
39 | |
cb21075d |
40 | String |
41 | ServerQueueOtherItem::getLine() |
42 | { |
43 | return line; |
44 | } |
45 | |
cb21075d |
46 | ServerQueueChannelModeItem::ServerQueueChannelModeItem(String c, String m, String p) |
47 | : ServerQueueItem(CHANNELMODE_PRIORITY, |
48 | CHANNELMODE_PENALTY, |
49 | CHANNELMODE), |
50 | channel(c), mode(m), parameters(p) |
51 | { |
52 | StringTokenizer st(p); |
a6339323 |
53 | paramcount = st.count_tokens(); |
cb21075d |
54 | } |
55 | |
56 | bool |
57 | ServerQueueChannelModeItem::merge(ServerQueueItem *sqi) |
58 | { |
59 | if (sqi->type != CHANNELMODE) |
60 | return false; |
61 | |
62 | ServerQueueChannelModeItem * sqcmi = (ServerQueueChannelModeItem *)sqi; |
63 | |
64 | if (sqcmi->channel != channel || (paramcount + sqcmi->paramcount > 3)) |
65 | return false; |
66 | |
67 | char sign = '+'; |
68 | char c; |
69 | |
70 | for (int i = 0; i < mode.length(); i++) { |
71 | c = mode[i]; |
72 | switch (c) { |
73 | case '+': |
74 | case '-': |
75 | sign = c; |
76 | } |
77 | } |
78 | |
79 | char othersign = '+'; |
80 | |
81 | for (int i = 0; i < sqcmi->mode.length(); i++) { |
82 | c = sqcmi->mode[i]; |
83 | switch (c) { |
84 | case '+': |
85 | case '-': |
86 | othersign = c; |
87 | break; |
88 | default: |
89 | if (othersign == sign) |
90 | mode = mode + String(c); |
91 | else { |
92 | mode = mode + String(othersign) + String(c); |
93 | sign = othersign; |
94 | } |
95 | } |
96 | } |
97 | |
98 | parameters = parameters + " " + sqcmi->parameters; |
99 | paramcount += sqcmi->paramcount; |
100 | penalty += sqcmi->penalty; |
101 | |
102 | return true; |
103 | } |
104 | |
105 | String |
106 | ServerQueueChannelModeItem::getLine() |
107 | { |
108 | return String("MODE ") + channel + " " + mode + " " + parameters; |
109 | } |
110 | |
111 | |
112 | |
113 | ServerQueueKickItem::ServerQueueKickItem(String c, String w, String r) |
114 | : ServerQueueItem(KICK_PRIORITY, |
115 | KICK_PENALTY, |
116 | KICK), |
117 | channel(c), who(w), reason(r), count(1) |
118 | { } |
119 | |
120 | bool |
121 | ServerQueueKickItem::merge(ServerQueueItem *sqi) |
122 | { |
123 | if (sqi->type != KICK) |
124 | return false; |
125 | ServerQueueKickItem * sqki = (ServerQueueKickItem *)sqi; |
126 | if (sqki->channel != channel) |
127 | return false; |
128 | if (count + sqki->count > 4) |
129 | return false; |
130 | who = who + "," + sqki->who; |
131 | if (reason != sqki->reason) |
132 | reason = "Mixed kick ! Top efficiency"; |
133 | penalty += sqki->penalty; |
134 | count += sqki->count; |
135 | |
136 | return true; |
137 | } |
138 | |
139 | String |
140 | ServerQueueKickItem::getLine() |
141 | { |
142 | return String("KICK ") + channel + " " + who + " :" + reason; |
143 | } |
144 | |
145 | ServerQueueNoticeItem::ServerQueueNoticeItem(String d, String m) |
146 | : ServerQueueItem(NOTICE_PRIORITY, |
147 | NOTICE_PENALTY, |
148 | NOTICE), |
149 | dest(d), message(m), count(1) |
150 | { } |
151 | |
152 | bool |
153 | ServerQueueNoticeItem::merge(ServerQueueItem *sqi) |
154 | { |
155 | if (sqi->type != NOTICE) |
156 | return false; |
157 | ServerQueueNoticeItem * sqni = (ServerQueueNoticeItem *)sqi; |
158 | if (sqni->message != message) |
159 | return false; |
160 | if (count + sqni->count > 4) |
161 | return false; |
162 | dest = dest + "," + sqni->dest; |
163 | penalty += sqni->penalty; |
164 | count += sqni->count; |
165 | |
166 | return true; |
167 | } |
168 | |
169 | String |
170 | ServerQueueNoticeItem::getLine() |
171 | { |
172 | return String("NOTICE ") + dest + " :" + message; |
173 | } |