-
Notifications
You must be signed in to change notification settings - Fork 0
/
discussion.cc
285 lines (265 loc) · 10.5 KB
/
discussion.cc
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// -*- C++ -*-
//
// Discussion class implementation.
//
// Copyright 1992-2021 Deven T. Corzine <[email protected]>
//
// SPDX-License-Identifier: MIT
//
#include "phoenix.h"
Discussion::Discussion(Session *s, const char *Name, const char *Title, boolean ispublic) {
name = Name;
title = Title;
Public = ispublic;
if (s) {
creator = s->name_obj;
members.Add(s);
moderators.Add(s->name_obj);
}
}
Name *Discussion::Allowed(Session *session) {
SetIter<Name> name(allowed);
while (name++) if (!strcasecmp(~name->name, ~session->name)) return name;
return NULL;
}
Name *Discussion::Denied(Session *session) {
SetIter<Name> name(denied);
while (name++) if (!strcasecmp(~name->name, ~session->name)) return name;
return NULL;
}
boolean Discussion::IsCreator(Session *session) {
return boolean(creator && !strcasecmp(~creator->name, ~session->name));
}
Name *Discussion::IsModerator(Session *session) {
SetIter<Name> name(moderators);
while (name++) if (!strcasecmp(~name->name, ~session->name)) return name;
return NULL;
}
boolean Discussion::Permitted(Session *session) {
SetIter<Name> name;
if (IsCreator(session) || IsModerator(session)) return true;
if (!Public && !Allowed(session)) return false;
if (Denied(session)) return false;
return true;
}
void Discussion::EnqueueOthers(OutputObj *out, Session *sender) {
SetIter<Session> session(members);
while (session++) if (session != sender) session->Enqueue(out);
}
void Discussion::Destroy(Session *session) {
if (IsCreator(session) || IsModerator(session)) {
Session::RemoveDiscussion(this);
session->EnqueueOthers(new DestroyNotify(this, session));
session->print("You have destroyed discussion %s.\n", ~name);
} else {
session->print("You are not a moderator of discussion %s.\n", ~name);
}
}
void Discussion::Join(Session *session) {
if (members.In(session)) {
session->print("You are already a member of discussion %s.\n", ~name);
} else {
if (Permitted(session)) {
EnqueueOthers(new JoinNotify(this, session), session);
members.Add(session);
session->print("You are now a member of discussion %s.\n", ~name);
} else {
session->print("You are not permitted to join discussion %s.\n",
~name);
}
}
}
void Discussion::Quit(Session *session) {
if (members.In(session)) {
members.Remove(session);
if (session->SignedOn) {
EnqueueOthers(new QuitNotify(this, session), session);
session->print("You are no longer a member of discussion %s.\n",
~name);
}
} else {
session->print("You are not a member of discussion %s.\n", ~name);
}
}
void Discussion::Permit(Session *session, char *args) {
Set<Session> matches;
Session *s;
char *user;
Name *n;
if (IsCreator(session) || IsModerator(session)) {
while ((user = getword(args, Comma))) {
if (match(user, "others", 6)) {
if (Public) {
session->print("Discussion %s is already public.\n", ~name);
} else {
Public = true;
session->EnqueueOthers(new PublicNotify(this, session));
session->print("You have made discussion %s public.\n", ~name);
}
} else {
if ((s = session->FindSession(user, matches))) {
if (Public) {
if ((n = Denied(s))) {
denied.Remove(n);
s->Enqueue(new PermitNotify(this, session, true));
session->print("You have repermitted %s to discussion "
"%s.\n", ~s->name, ~name);
} else if (Allowed(s)) {
session->print("%s is already explicitly permitted to "
"public discussion %s.\n", ~s->name, ~name);
} else {
allowed.Add(s->name_obj);
s->Enqueue(new PermitNotify(this, session, false));
session->print("You have explicitly permitted %s to "
"public discussion %s.\n", ~s->name, ~name);
}
} else {
if ((n = Denied(s))) {
denied.Remove(n);
allowed.Add(s->name_obj);
s->Enqueue(new PermitNotify(this, session, true));
session->print("You have repermitted %s to discussion "
"%s.\n", ~s->name, ~name);
} else if (Allowed(s)) {
session->print("%s is already permitted to discussion "
"%s.\n", ~s->name, ~name);
} else {
allowed.Add(s->name_obj);
s->Enqueue(new PermitNotify(this, session, false));
session->print("You have permitted %s to discussion "
"%s.\n", ~s->name, ~name);
}
}
} else {
session->SessionMatches(user, matches);
}
}
}
} else {
session->print("You are not a moderator of discussion %s.\n", ~name);
}
}
void Discussion::Depermit(Session *session, char *args) {
Set<Session> matches;
Session *s;
char *user;
Name *n;
if (IsCreator(session) || IsModerator(session)) {
while ((user = getword(args, Comma))) {
if (match(user, "others", 6)) {
if (Public) {
Public = false;
SetIter<Session> s(members);
while (s++) if (!Allowed(s)) allowed.Add(s->name_obj);
session->EnqueueOthers(new PrivateNotify(this, session));
session->print("You have made discussion %s private.\n", ~name);
} else {
session->print("Discussion %s is already private.\n", ~name);
}
} else {
if ((s = session->FindSession(user, matches))) {
if (Public) {
if ((n = Allowed(s))) allowed.Remove(n);
if (Denied(s)) {
session->print("%s is already depermitted from "
"discussion %s.\n", ~s->name, ~name);
} else {
denied.Add(s->name_obj);
if (members.In(s)) {
members.Remove(s);
EnqueueOthers(new DepermitNotify(this, session, false,
s), session);
session->print("You have depermitted and removed "
"%s from discussion %s.\n", ~s->name,
~name);
} else {
s->Enqueue(new DepermitNotify(this, session, false, 0));
session->print("You have depermitted %s from "
"discussion %s.\n", ~s->name, ~name);
}
}
} else {
if ((n = Allowed(s))) {
allowed.Remove(n);
if (members.In(s)) {
members.Remove(s);
EnqueueOthers(new DepermitNotify(this, session, false,
s), session);
session->print("You have depermitted and removed "
"%s from discussion %s.\n", ~s->name,
~name);
} else {
s->Enqueue(new DepermitNotify(this, session, false, 0));
session->print("You have depermitted %s from "
"discussion %s.\n", ~s->name, ~name);
}
} else if (Denied(s)) {
session->print("%s is already explicitly depermitted "
"from private discussion %s.\n", ~s->name,
~name);
} else {
denied.Add(s->name_obj);
s->Enqueue(new DepermitNotify(this, session, true, 0));
session->print("You have explicitly depermitted %s "
"from private discussion %s.\n", ~s->name,
~name);
}
}
} else {
session->SessionMatches(user, matches);
}
}
}
} else {
session->print("You are not a moderator of discussion %s.\n", ~name);
}
}
void Discussion::Appoint(Session *session, char *args) {
Set<Session> matches;
Session *s;
char *user;
if (IsCreator(session) || IsModerator(session) || session->priv >= 50) {
while ((user = getword(args, Comma))) {
if ((s = session->FindSession(user, matches))) {
if (IsModerator(s)) {
session->print("%s is already a moderator of discussion %s.\n",
~s->name, ~name);
} else {
moderators.Add(s->name_obj);
EnqueueOthers(new AppointNotify(this, session, s), session);
session->print("You have appointed %s as a moderator of "
"discussion %s.\n", ~s->name, ~name);
}
} else {
session->SessionMatches(user, matches);
}
}
} else {
session->print("You are not a moderator of discussion %s.\n", ~name);
}
}
void Discussion::Unappoint(Session *session, char *args) {
Set<Session> matches;
Session *s;
char *user;
Name *n;
if (IsCreator(session) || IsModerator(session)) {
while ((user = getword(args, Comma))) {
if ((s = session->FindSession(user, matches))) {
if ((n = IsModerator(s))) {
moderators.Remove(n);
EnqueueOthers(new UnappointNotify(this, session, s), session);
session->print("You have unappointed %s as a moderator of "
"discussion %s.\n", ~s->name, ~name);
} else {
session->print("%s is not a moderator of discussion %s.\n",
~s->name, ~name);
}
} else {
session->SessionMatches(user, matches);
}
}
} else {
session->print("You are not a moderator of discussion %s.\n", ~name);
}
}