-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregComponent.cpp
More file actions
175 lines (152 loc) · 3.77 KB
/
regComponent.cpp
File metadata and controls
175 lines (152 loc) · 3.77 KB
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
/*
(c) Matthew Slocum
regComponent.cpp
regComponent is responsible for passing data around the rest of the program.
it tracks users, ips, the current destination, and the crypto key.
*/
#include <iostream>
#include <string.h>
#include <cstring>
#include <fstream>
#include <stdlib.h>
#include "regComponent.h"
#include "msgComponent.h"
using namespace std;
regComponent::regComponent() {
loadKey();
loadIPTable();
loadUsername();
}
//let the reg component know about the msgComponent
void regComponent::bind_msgComponent(msgComponent * in) {
msg = in;
}
void regComponent::loadKey() {
ifstream dest_file;
//******************************//
//****** READ CRYPTO KEY
//******************************//
//get lines in file
dest_file.open("key.sec");
if(dest_file.is_open()) {
while(!dest_file.eof()) {
string s;
getline(dest_file,s);
if(s!="") {
key=s;
}
}
}
dest_file.close();
}
void regComponent::loadUsername() {
ifstream dest_file;
//******************************//
//****** READ CRYPTO KEY
//******************************//
//get lines in file
dest_file.open("username.sec");
if(dest_file.is_open()) {
while(!dest_file.eof()) {
string s;
getline(dest_file,s);
if(s!="") {
username=s;
//cout << username << endl;
}
}
}
dest_file.close();
}
void regComponent::loadIPTable() {
//******************************//
//****** READ IP/USER LIST
//******************************//
//read line in file
ifstream dest_file;
//load last dest
dest_file.open("curdest.sec");
if(dest_file.is_open()) {
while(!dest_file.eof()) {
string s;
getline(dest_file,s);
if(s!="") {
dest=atoi(s.c_str());
}
}
}
dest_file.close();
dest_count = 0;
dest_file.open("dest.sec");
int lines = 0;
if(dest_file.is_open()) {
while(!dest_file.eof()) {
string s;
getline(dest_file,s);
if(s!="") {
lines++;
}
}
}
dest_file.close();
//init arrays
dest_list = new string[lines];
name_list = new string[lines];
//read file
dest_file.open("dest.sec");
if(dest_file.is_open()) {
while(!dest_file.eof()) {
string user;
string ip;
string s;
getline(dest_file,s);
if(s!="") {
user=s.substr(0,s.find(","));
ip=s.substr(s.find(",")+1);
dest_list[dest_count]=ip;
name_list[dest_count++]=user;
}
}
}
dest_file.close();
}
void regComponent::addIPToTable(string ip) {
ofstream dest_file;
dest_file.open("dest.sec", ios::app);
dest_file << endl << ip;
dest_file.close();
delete [] dest_list;
delete [] name_list;
loadIPTable();
}
void regComponent::saveKey(string k) {
system("rm key.sec");
ofstream key_file;
key_file.open("key.sec");
key_file << k;
key_file.close();
}
void regComponent::setKey(string k) {
key=k;
saveKey(k);
}
void regComponent::printState() {
cout << "Dest: " << name_list[dest];
}
void regComponent::printDest() {
int i=0;
for (i=0; i<dest_count; i++) {
cout << "| " << i+1 << ": [" << name_list[i] << "] " << dest_list[i] << endl;
}
}
void regComponent::setDest(int d) {
dest = d;
system("rm curdest.sec");
ofstream key_file;
key_file.open("curdest.sec");
key_file << d;
key_file.close();
}
int regComponent::getDest() {
return dest;
}