/
subreq.cpp
460 lines (431 loc) · 12.7 KB
/
subreq.cpp
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**************************************************************************/
/* */
/* WWIV Version 5.x */
/* Copyright (C)1998-2022, WWIV Software Services */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, */
/* software distributed under the License is distributed on an */
/* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, */
/* either express or implied. See the License for the specific */
/* language governing permissions and limitations under the License. */
/* */
/**************************************************************************/
#include "bbs/subreq.h"
#include "bbs/bbs.h"
#include "bbs/connect1.h"
#include "bbs/email.h"
#include "bbs/mmkey.h"
#include "bbs/utility.h"
#include "common/com.h"
#include "common/input.h"
#include "common/output.h"
#include "core/datetime.h"
#include "core/stl.h"
#include "core/strings.h"
#include "core/textfile.h"
#include "sdk/filenames.h"
#include "sdk/subxtr.h"
#include "sdk/net/networks.h"
#include "sdk/net/subscribers.h"
#include <string>
using namespace wwiv::core;
using namespace wwiv::sdk;
using namespace wwiv::sdk::net;
using namespace wwiv::stl;
using namespace wwiv::strings;
static void maybe_netmail(subboard_network_data_t* ni, bool bAdd) {
bout << "|#5Send email request to the host now? ";
if (bin.yesno()) {
auto title = StrCat("Sub type ", ni->stype);
if (bAdd) {
title += " - ADD request";
} else {
title += " - DROP request";
}
set_net_num(ni->net_num);
email(title, 1, ni->host, false, 0);
}
}
static bool display_sub_categories(const Network& net) {
if (!net.sysnum) {
return false;
}
TextFile ff(FilePath(net.dir, CATEG_NET), "rt");
if (!ff.IsOpen()) {
return false;
}
bout.nl();
bout << "Available sub categories are:\r\n";
auto abort = false;
std::string s;
while (!abort && ff.ReadLine(&s)) {
StringTrim(&s);
bout.bpla(s, &abort);
}
ff.Close();
return true;
}
static void sub_req(uint16_t main_type, int tosys, const std::string& stype,
const Network& net) {
net_header_rec nh{};
nh.tosys = static_cast<uint16_t>(tosys);
nh.touser = 1;
nh.fromsys = net.sysnum;
nh.fromuser = 1;
nh.main_type = main_type;
// always use 0 since we use the "stype" field
nh.minor_type = 0;
nh.list_len = 0;
nh.daten = daten_t_now();
nh.method = 0;
// This is an alphanumeric sub type.
auto text = stype;
text.push_back(0);
nh.length = size_uint32(text);
send_net(&nh, {}, text, "");
bout.nl();
if (main_type == main_type_sub_add_req) {
bout << "Automated add request sent to @" << tosys << wwiv::endl;
} else {
bout << "Automated drop request sent to @" << tosys << wwiv::endl;
}
bout.pausescr();
}
static constexpr short OPTION_AUTO = 0x0001;
static constexpr short OPTION_NO_TAG = 0x0002;
static constexpr short OPTION_GATED = 0x0004;
static constexpr short OPTION_NETVAL = 0x0008;
static constexpr short OPTION_ANSI = 0x0010;
static int find_hostfor(const Network& net, const std::string& type, short* ui,
char* description, short* opt) {
int rc = 0;
if (description) {
*description = 0;
}
*opt = 0;
bool done = false;
for (int i = 0; i < 256 && !done; i++) {
std::filesystem::path fn;
if (i) {
fn = FilePath(net.dir, StrCat(SUBS_NOEXT, ".", i));
} else {
fn = FilePath(net.dir, StrCat(SUBS_LST));
}
TextFile file(fn, "r");
if (!file) {
return rc;
}
char s[255];
while (!done && file.ReadLine(s, 160)) {
if (s[0] <= ' ') {
continue;
}
auto* ss = strtok(s, " \r\n\t");
if (!ss) {
continue;
}
if (!iequals(ss, type)) {
continue;
}
ss = strtok(nullptr, " \r\n\t");
if (!ss) {
continue;
}
auto h = to_number<short>(ss);
short o = 0;
ss = strtok(nullptr, "\r\n");
if (!ss) {
continue;
}
int i1 = 0;
while (*ss && ((*ss == ' ') || (*ss == '\t'))) {
++ss;
++i1;
}
if (i1 < 4) {
while (*ss && (*ss != ' ') && (*ss != '\t')) {
switch (*ss) {
case 'T':
o |= OPTION_NO_TAG;
break;
case 'R':
o |= OPTION_AUTO;
break;
case 'G':
o |= OPTION_GATED;
break;
case 'N':
o |= OPTION_NETVAL;
break;
case 'A':
o |= OPTION_ANSI;
break;
}
++ss;
}
while (*ss && ((*ss == ' ') || (*ss == '\t'))) {
++ss;
}
}
if (*ui) {
if (*ui == h) {
done = true;
*opt = o;
rc = h;
if (description) {
strcpy(description, ss);
}
}
} else {
bout.nl();
bout << "Type: " << type << wwiv::endl;
bout << "Host: " << h << wwiv::endl;
bout << "Sub : " << ss << wwiv::endl;
bout.nl();
bout << "|#5Is this the sub you want? ";
if (bin.yesno()) {
done = true;
*ui = h;
*opt = o;
rc = h;
if (description) {
strcpy(description, ss);
}
}
}
}
}
return rc;
}
void sub_xtr_del(int n, int nn, int f) {
// make a copy of the old network info.
auto xn = a()->subs().sub(n).nets[nn];
if (f) {
erase_at(a()->subs().sub(n).nets, nn);
}
set_net_num(xn.net_num);
const auto& net = a()->nets().at(xn.net_num);
if (xn.host != 0 && valid_system(xn.host)) {
short opt;
const auto ok = find_hostfor(net, xn.stype, &xn.host, nullptr, &opt);
if (ok) {
if (opt & OPTION_AUTO) {
bout << "|#5Attempt automated drop request? ";
if (bin.yesno()) {
sub_req(main_type_sub_drop_req, xn.host, xn.stype, net);
}
} else {
maybe_netmail(&xn, false);
}
} else {
bout << "|#5Attempt automated drop request? ";
if (bin.yesno()) {
sub_req(main_type_sub_drop_req, xn.host, xn.stype, net);
} else {
maybe_netmail(&xn, false);
}
}
}
}
bool sub_xtr_add(int n, int nn) {
// nn may be -1
while (nn >= size_int(a()->subs().sub(n).nets)) {
a()->subs().sub(n).nets.push_back({});
}
subboard_network_data_t xnp = {};
// Start with the first network number, only ask if
// we have more than 1 network to choose from.
int network_number = 0;
const auto num_nets = wwiv::stl::ssize(a()->nets());
if (num_nets == 0) {
LOG(ERROR) << "Called sub_xtr_add when no networks defined.";
return false;
}
if (num_nets > 1) {
std::set<char> odc;
char onx[20];
onx[0] = 'Q';
onx[1] = 0;
auto onxi = 1;
bout.nl();
for (auto ii = 0; ii < wwiv::stl::ssize(a()->nets()); ii++) {
if (ii < 9) {
onx[onxi++] = static_cast<char>(ii + '1');
onx[onxi] = 0;
} else {
auto odci = (ii + 1) / 10;
odc.insert(static_cast<char>(odci + '0'));
}
bout << "(" << ii + 1 << ") " << a()->nets()[ii].name << wwiv::endl;
}
bout << "Q. Quit\r\n\n";
bout << "|#2Which network (number): ";
if (wwiv::stl::ssize(a()->nets()) < 9) {
auto ch = onek(onx);
if (ch == 'Q') {
return false;
}
network_number = ch - '1';
} else {
auto mmk = mmkey(odc);
if (mmk == "Q") {
return false;
}
network_number = to_number<int>(mmk) - 1;
}
if (network_number >= 0 && network_number < wwiv::stl::ssize(a()->nets())) {
set_net_num(network_number);
} else {
return false;
}
}
xnp.net_num = static_cast<int16_t>(network_number);
const auto& net = a()->nets()[network_number];
bout.nl();
auto stype_len = 7;
if (net.type == network_type_t::ftn) {
bout << "|#2What echomail area: ";
stype_len = 40;
} else {
bout << "|#2What sub type? ";
}
xnp.stype = bin.input(stype_len, true);
if (xnp.stype.empty()) {
return false;
}
bool is_hosting = false;
if (net.type == network_type_t::wwivnet || net.type == network_type_t::internet ||
net.type == network_type_t::news) {
bout << "|#5Will you be hosting the sub? ";
is_hosting = bin.yesno();
}
if (is_hosting) {
auto file_name = StrCat(net.dir, "n", xnp.stype, ".net");
File file(file_name);
if (file.Open(File::modeBinary | File::modeCreateFile | File::modeReadWrite)) {
file.Close();
}
bout << "|#5Allow auto add/drop requests? ";
if (bin.noyes()) {
xnp.flags |= XTRA_NET_AUTO_ADDDROP;
}
bout << "|#5Make this sub public (in subs.lst)?";
if (bin.noyes()) {
xnp.flags |= XTRA_NET_AUTO_INFO;
if (display_sub_categories(net)) {
auto gc = 0;
while (!gc) {
bout.nl();
bout << "|#2Which category is this sub in (0 for unknown/misc)? ";
auto s = bin.input(3);
auto i = to_number<uint16_t>(s);
if (i || s == "0") {
TextFile ff(FilePath(net.dir, CATEG_NET), "rt");
while (ff.ReadLine(&s)) {
int i1 = to_number<uint16_t>(s);
if (i1 == i) {
gc = 1;
xnp.category = i;
break;
}
}
file.Close();
if (s == "0") {
gc = 1;
} else if (!xnp.category) {
bout << "Illegal/invalid category.\r\n\n";
}
} else {
if (s.size() == 1 && s.front() == '?') {
display_sub_categories(net);
continue;
}
}
}
}
}
} else if (net.type == network_type_t::ftn) {
// Set the fake fido node up as the host.
xnp.host = FTN_FAKE_OUTBOUND_NODE;
const auto sub_file_name = FilePath(net.dir, StrCat("n", xnp.stype, ".net"));
auto addresses = ReadFidoSubcriberFile(sub_file_name);
bool done = false;
do {
a()->CheckForHangup();
bout.nl();
bout << "|#2Which FTN system (address) is the host? ";
const auto host = bin.input_text(20);
try {
fido::FidoAddress a(host);
addresses.insert(a);
if (!WriteFidoSubcriberFile(sub_file_name, addresses)) {
bout << "ERROR: Unable to add host to subscriber file";
}
done = true;
} catch (const fido::bad_fidonet_address& e) {
bout << "Bad Address: " << e.what();
}
} while (!done && !a()->sess().hangup());
} else {
char description[100];
short opt;
auto ok = find_hostfor(net, xnp.stype, &(xnp.host), description, &opt);
if (!ok) {
bout.nl();
bout << "|#2Which system (number) is the host? ";
bin.input(description, 6);
xnp.host = to_number<uint16_t>(description);
description[0] = '\0';
}
if (!a()->subs().sub(n).desc[0]) {
a()->subs().sub(n).desc = description;
}
if (xnp.host == net.sysnum) {
xnp.host = 0;
}
if (xnp.host) {
if (valid_system(xnp.host)) {
if (ok) {
if (opt & OPTION_NO_TAG) {
a()->subs().sub(n).anony |= anony_no_tag;
}
bout.nl();
if (opt & OPTION_AUTO) {
bout << "|#5Attempt automated add request? ";
if (bin.yesno()) {
sub_req(main_type_sub_add_req, xnp.host, xnp.stype, net);
}
} else {
maybe_netmail(&xnp, true);
}
} else {
bout.nl();
bout << "|#5Attempt automated add request? ";
auto bTryAutoAddReq = bin.yesno();
if (bTryAutoAddReq) {
sub_req(main_type_sub_add_req, xnp.host, xnp.stype, net);
} else {
maybe_netmail(&xnp, true);
}
}
} else {
bout.nl();
bout << "The host is not listed in the network.\r\n";
bout.pausescr();
}
}
}
if (nn == -1 || nn >= size_int(a()->subs().sub(n).nets)) {
// nn will be -1 when adding a new sub.
a()->subs().sub(n).nets.push_back(xnp);
} else {
a()->subs().sub(n).nets[nn] = xnp;
}
return true;
}