/
req.cpp
96 lines (84 loc) · 3.32 KB
/
req.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
/**************************************************************************/
/* */
/* WWIV Version 5.x */
/* Copyright (C)2015-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 "wwivutil/net/req.h"
#include "core/command_line.h"
#include "core/datetime.h"
#include "core/log.h"
#include "core/strings.h"
#include "sdk/bbslist.h"
#include "sdk/net/networks.h"
#include "sdk/net/packets.h"
#include <iostream>
#include <string>
#include <vector>
using namespace wwiv::core;
using namespace wwiv::sdk;
using namespace wwiv::sdk::net;
using namespace wwiv::strings;
namespace wwiv::wwivutil {
std::string SubReqCommand::GetUsage() const {
std::ostringstream ss;
ss << "Usage: req <A|D> <subtype> <host>" << std::endl;
ss << "Example: req A GENCHAT 1" << std::endl;
return ss.str();
}
bool SubReqCommand::AddSubCommands() {
add_argument({"net", "Network number to use (i.e. 0).", "0"});
return true;
}
int SubReqCommand::Execute() {
const Networks networks(*config()->config());
if (!networks.IsInitialized()) {
LOG(ERROR) << "Unable to load networks.";
return 1;
}
const auto r = this->remaining();
if (r.size() < 3) {
std::cout << GetUsage() << GetHelp();
return 2;
}
const auto net = stl::at(config()->networks(),arg("net").as_int());
const auto packet_filename = create_pend(net.dir, false, 'r');
const auto add_drop = upcase(stl::at(r, 0).front());
net_header_rec nh = {};
auto host = to_number<uint16_t>(stl::at(r, 2));
nh.tosys = static_cast<uint16_t>(host);
nh.touser = 1;
nh.fromsys = net.sysnum;
nh.fromuser = 1;
nh.main_type = add_drop == 'A' ? main_type_sub_add_req : main_type_sub_drop_req;
// always use 0 since we use the stype
nh.minor_type = 0;
nh.list_len = 0;
nh.daten = daten_t_now();
nh.method = 0;
// This is an alphanumeric sub type.
const auto& subtype = stl::at(r, 1);
nh.length = subtype.size() + 1;
auto text = ToStringUpperCase(subtype);
text.push_back('\0');
Packet packet(nh, {}, text);
const auto ok = write_wwivnet_packet(FilePath(net.dir, packet_filename), packet);
if (!ok) {
LOG(ERROR) << "Error writing packet: " << packet_filename;
return 1;
}
LOG(INFO) << "Wrote Packet: " << packet_filename;
return 0;
}
}