-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsetup.py
More file actions
213 lines (192 loc) · 6.38 KB
/
setup.py
File metadata and controls
213 lines (192 loc) · 6.38 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
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
import os, json, sys
from logger import Logger
# OriginChats Setup Script
Logger.info("Starting OriginChats server configuration...")
def get_input(prompt, default=None):
"""Get user input with optional default value"""
if default:
user_input = input(f"{prompt} [{default}]: ").strip()
return user_input if user_input else default
return input(f"{prompt}: ").strip()
def yes_no(prompt, default="y"):
"""Get yes/no input from user"""
while True:
response = input(f"{prompt} [{default}]: ").strip().lower()
if not response:
response = default
if response in ["y", "yes", "true", "1"]:
return True
elif response in ["n", "no", "false", "0"]:
return False
print("Please enter y/n")
def setup_directories():
"""Create necessary directories if they don't exist"""
directories = ["db", "db/backup"]
for directory in directories:
if not os.path.exists(directory):
os.makedirs(directory)
Logger.add(f"Created directory: {directory}")
def create_default_files():
"""Create default database files"""
# Default users.json
users_file = "db/users.json"
if not os.path.exists(users_file):
default_users = {}
with open(users_file, "w") as f:
json.dump(default_users, f, indent=4)
Logger.add(f"Created {users_file}")
# Default channels.json
channels_file = "db/channels.json"
if not os.path.exists(channels_file):
default_channels = [
{
"type": "text",
"name": "general",
"description": "General chat channel for everyone",
"permissions": {
"view": ["user"],
"send": ["user"],
"delete": ["admin", "moderator"]
}
}
]
with open(channels_file, "w") as f:
json.dump(default_channels, f, indent=4)
Logger.add(f"Created {channels_file}")
# Default roles.json
roles_file = "db/roles.json"
if not os.path.exists(roles_file):
default_roles = {
"owner": {
"description": "Server owner with ultimate permissions.",
"color": "#9400D3"
},
"admin": {
"description": "Administrator role with full permissions.",
"color": "#FF0000"
},
"moderator": {
"description": "Moderator role with elevated permissions.",
"color": "#FFFF00"
},
"user": {
"description": "Regular user role with standard permissions.",
"color": "#FFFFFF"
}
}
with open(roles_file, "w") as f:
json.dump(default_roles, f, indent=4)
Logger.add(f"Created {roles_file}")
def main():
"""Main setup function"""
print()
print("=" * 50)
print(" OriginChats Server Setup")
print("=" * 50)
print()
# Check if config already exists
config_exists = os.path.exists("config.json")
if config_exists:
if not yes_no("Config file already exists. Overwrite?", "n"):
Logger.warning("Setup cancelled")
return
print("Let's configure your OriginChats server...")
print()
# Server configuration
print("--- Server Configuration ---")
server_name = get_input("Server name", "My OriginChats Server")
server_icon = get_input("Server icon URL (optional)")
server_url = get_input("Server URL (optional)")
owner_name = get_input("Server owner name", "Admin")
print()
print("--- WebSocket Configuration ---")
ws_host = get_input("WebSocket host", "127.0.0.1")
ws_port = get_input("WebSocket port", "5613")
try:
ws_port = int(ws_port)
except ValueError:
Logger.warning("Invalid port number, using default 5613")
ws_port = 5613
print()
print("--- Content Limits ---")
max_message_length = get_input("Maximum message length", "2000")
try:
max_message_length = int(max_message_length)
except ValueError:
print("[OriginChats Setup] Invalid length, using default 2000")
max_message_length = 2000
# Create config structure
config = {
"limits": {
"post_content": max_message_length
},
"rate_limiting": {
"enabled": True,
"messages_per_minute": 60,
"burst_limit": 10,
"cooldown_seconds": 30
},
"DB": {
"channels": "db/channels.json",
"users": {
"file": "db/users.json",
"default": {
"roles": ["user"]
}
}
},
"websocket": {
"host": ws_host,
"port": ws_port
},
"service": {
"name": "OriginChats",
"version": "1.0.0"
},
"server": {
"name": server_name,
"owner": {
"name": owner_name
}
}
}
# Add optional fields if provided
if server_icon:
config["server"]["icon"] = server_icon
if server_url:
config["server"]["url"] = server_url
# Create directories and files
print()
print("--- Setting up directories and files ---")
setup_directories()
create_default_files()
# Write config file
with open("config.json", "w") as f:
json.dump(config, f, indent=4)
print()
print("=" * 50)
print("[OriginChats Setup] Configuration complete!")
print()
print("Your server is configured with:")
print(f" Server Name: {server_name}")
print(f" WebSocket: {ws_host}:{ws_port}")
print(f" Owner: {owner_name}")
print()
print("To start your server, run:")
print(" python init.py")
print()
print("Make sure to:")
print("1. Configure your Rotur validation key properly")
print("2. Set up any firewall rules for your WebSocket port")
print("3. Configure SSL/TLS if running in production")
print("=" * 50)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print()
Logger.warning("Setup cancelled by user")
sys.exit(1)
except Exception as e:
Logger.error(f"Error during setup: {str(e)}")
sys.exit(1)