Skip to content

Commit 6c3975c

Browse files
authored
Merge pull request #12 from adator85/dev
Dev
2 parents 50b0c75 + 7bfeea0 commit 6c3975c

11 files changed

+86
-20
lines changed
30.6 KB
Binary file not shown.

dist/unrealircd_rpc_py-0.1.2.tar.gz

21.1 KB
Binary file not shown.
30.6 KB
Binary file not shown.

dist/unrealircd_rpc_py-0.1.3.tar.gz

21 KB
Binary file not shown.
30.6 KB
Binary file not shown.

dist/unrealircd_rpc_py-0.1.4.tar.gz

21 KB
Binary file not shown.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='unrealircd_rpc_py',
5-
version='0.1.1',
5+
version='0.1.4',
66
packages=find_packages(),
77
install_requires=[
88
"requests>=2.25.1"

unrealircd_rpc_py/Connection.py

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json.scanner
2-
import requests, json, urllib3, socket, re, sys
2+
import requests, json, urllib3, socket, re, sys, os
33
from requests.auth import HTTPBasicAuth
44
import base64, ssl, time, logging, random
55
from typing import Literal, Union
@@ -18,6 +18,7 @@ def __init__(self, req_method:str, url: str, path_to_socket_file: str, username:
1818
self.debug_level = debug_level
1919
self.Logs: logging
2020
self.__init_log_system()
21+
self.Error = self.ErrorModel(0, '')
2122

2223
self.url = url
2324
self.path_to_socket_file = path_to_socket_file
@@ -28,19 +29,14 @@ def __init__(self, req_method:str, url: str, path_to_socket_file: str, username:
2829
self.username = username
2930
self.password = password
3031

31-
if not self.__check_url(url) and not url is None:
32-
self.Logs.critical('You must provide the url in this format: https://your.rpcjson.link:port/api')
33-
sys.exit(3)
34-
3532
self.request: str = ''
3633
self.req_method = req_method
3734
self.str_response = ''
3835
self.json_response = ''
3936

40-
# Option 2 with Namespaces
37+
# Option 2 with Namespacescs
4138
self.json_response_np: SimpleNamespace
42-
43-
self.Error = self.ErrorModel(0, '')
39+
self.query('stats.get')
4440

4541
def __check_url(self, url: str) -> bool:
4642
"""Check provided url if it follow the format
@@ -66,16 +62,66 @@ def __check_url(self, url: str) -> bool:
6662
self.port = match.group(2)
6763
self.endpoint = match.group(3)
6864
response = True
65+
else:
66+
self.Error.code = -1
67+
self.Error.message = 'You must provide the url in this format: https://your.rpcjson.link:port/api'
68+
69+
return response
70+
except NameError as nameerr:
71+
self.Logs.critical(f'NameError: {nameerr}')
72+
73+
def __check_unix_socket_file(self, path_to_socket_file: str) -> bool:
74+
"""Check provided full path to socket file if it exist
75+
76+
Args:
77+
path_to_socket_file (str): Full path to unix socket file
78+
79+
Returns:
80+
bool: True if path is correct else False
81+
"""
82+
try:
83+
response = False
84+
85+
if path_to_socket_file is None:
86+
self.Error.code = -1
87+
self.Error.message = 'The Path to your socket file is empty ? please be sure that you are providing the correct socket path'
88+
return response
89+
90+
if not os.path.exists(path_to_socket_file):
91+
self.Error.code = -1
92+
self.Error.message = 'The Path to your socket file is wrong ? please make sure that you are providing the correct socket path'
93+
return response
94+
95+
response = True
6996

7097
return response
7198
except NameError as nameerr:
7299
self.Logs.critical(f'NameError: {nameerr}')
73100

101+
def __is_error_connection(self, response: str) -> bool:
102+
"""If True, it means that there is an error
103+
104+
Args:
105+
response (str): The response to analyse
106+
107+
Returns:
108+
bool: True if there is a connection error
109+
"""
110+
if 'authentication required' == response.lower().strip():
111+
self.Error.code = -1
112+
self.Error.message = '>> Authentication required'
113+
return True
114+
else:
115+
return False
116+
74117
def __send_to_unixsocket(self):
75118
try:
76119

77120
sock = socket.socket(socket.AddressFamily.AF_UNIX, socket.SocketKind.SOCK_STREAM)
78121

122+
if not self.__check_unix_socket_file(self.path_to_socket_file):
123+
return None
124+
79125
sock.connect(self.path_to_socket_file)
80126
sock.settimeout(10)
81127

@@ -102,16 +148,21 @@ def __send_to_unixsocket(self):
102148

103149
except AttributeError as attrerr:
104150
self.Logs.critical(f'AF_Unix Error: {attrerr}')
105-
sys.exit('AF_UNIX Are you sure you want to use Unix socket ?')
151+
self.Error.code = -1
152+
self.Error.message = 'AF_UNIX Are you sure you want to use Unix socket ?'
106153
except OSError as oserr:
107154
self.Logs.critical(f'System Error: {oserr}')
108-
sys.exit(3)
109155
except Exception as err:
110156
self.Logs.error(f'General Error: {err}')
111157

112158
def __send_srequest(self):
113159
"""S For socket connection"""
114160
try:
161+
162+
if not self.__check_url(self.url) and not self.url is None:
163+
self.Logs.critical('You must provide the url in this format: https://your.rpcjson.link:port/api')
164+
return None
165+
115166
get_url = self.url
116167
get_host = self.host
117168
get_port = self.port
@@ -148,15 +199,26 @@ def __send_srequest(self):
148199
else:
149200
body = response_str
150201

202+
if self.__is_error_connection(body):
203+
return None
204+
151205
self.json_response = json.loads(body)
152206
self.json_response_np: SimpleNamespace = json.loads(body, object_hook=lambda d: SimpleNamespace(**d))
153207

208+
except (socket.error, ssl.SSLError) as serror:
209+
self.Logs.error(f'Socket Error: {serror}')
154210
except Exception as err:
155211
self.Logs.error(f'General Error: {err}')
212+
# self.Logs.error(f'General Error: {traceback.format_exc()}')
156213

157214
def __send_request(self) :
158215
"""Use requests module"""
159216
try:
217+
218+
if not self.__check_url(self.url) and not self.url is None:
219+
self.Logs.critical('You must provide the url in this format: https://your.rpcjson.link:port/api')
220+
return None
221+
160222
verify = False
161223

162224
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
@@ -166,6 +228,9 @@ def __send_request(self) :
166228

167229
response = requests.post(url=self.url, auth=credentials, data=jsonrequest, verify=verify)
168230

231+
if self.__is_error_connection(response.text):
232+
return None
233+
169234
decodedResponse = json.dumps(response.text)
170235

171236
self.str_response = decodedResponse
@@ -181,7 +246,6 @@ def __send_request(self) :
181246
except requests.ConnectionError as ce:
182247
self.Logs.critical(f"Connection Error : {ce}")
183248
self.Logs.critical(f"Initial request: {self.request}")
184-
sys.exit(3)
185249
except json.decoder.JSONDecodeError as jsonerror:
186250
self.Logs.error(f"jsonError {jsonerror}")
187251
self.Logs.error(f"Initial request: {self.request}")

unrealircd_rpc_py/Live.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ def __init__(self, path_to_socket_file: str, callback_object_instance: object, c
1818
self.Logs: logging
1919
self.__init_log_system()
2020

21+
self.Error = self.ErrorModel(0, '')
22+
2123
if not self.__check_unix_socket_file(path_to_socket_file=path_to_socket_file):
2224
self.Logs.critical(f'The socket file is not available, please check the full path of your socket file')
23-
sys.exit('please check the full path of your socket file')
25+
self.Error.code = -1
26+
self.Error.message = 'The socket file is not available, please check the full path of your socket file'
27+
return None
2428

2529
self.to_run = getattr(callback_object_instance, callback_method_name)
2630

@@ -35,8 +39,6 @@ def __init__(self, path_to_socket_file: str, callback_object_instance: object, c
3539
# Option 2 with Namespaces
3640
self.json_response_np: SimpleNamespace
3741

38-
self.Error = self.ErrorModel(0, '')
39-
4042
def __check_unix_socket_file(self, path_to_socket_file: str) -> bool:
4143
"""Check provided full path to socket file if it exist
4244

unrealircd_rpc_py/User.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ def get(self, nickoruid: str) -> Union[ModelUser, None]:
143143
hostname=user['hostname'] if 'hostname' in user else None,
144144
ip=user['ip'] if 'ip' in user else None,
145145
details=user['details'] if 'details' in user else None,
146-
server_port=user['server_port'] if 'server_port' in user else 0,
147-
client_port=user['client_port'] if 'client_port' in user else 0,
148-
connected_since=user['connected_since'] if 'connected_since' in user else None,
146+
server_port=user['server_port'] if 'server_port' in user else 0,
147+
client_port=user['client_port'] if 'client_port' in user else 0,
148+
connected_since=user['connected_since'] if 'connected_since' in user else None,
149149
idle_since=user['idle_since'] if 'idle_since' in user else None,
150150
username=user['user']['username'] if 'user' in user and 'username' in user['user'] else None,
151151
realname=user['user']['realname'] if 'user' in user and 'realname' in user['user'] else None,
@@ -155,7 +155,7 @@ def get(self, nickoruid: str) -> Union[ModelUser, None]:
155155
reputation=user['user']['reputation'] if 'user' in user and 'reputation' in user['user'] else 0,
156156
security_groups=user['user']['security-groups'] if 'user' in user and 'security-groups' in user['user'] else [],
157157
modes=user['user']['modes'] if 'user' in user and 'modes' in user['user'] else None,
158-
channels=user['user']['channels'],
158+
channels=user['user']['channels'] if 'user' in user and 'channels' in user['user'] else [],
159159
cipher=user['tls']['cipher'] if 'tls' in user and 'cipher' in user['tls'] else None,
160160
certfp=user['tls']['certfp'] if 'tls' in user and 'certfp' in user['tls'] else None,
161161
country_code=user['geoip']['country_code'] if 'geoip' in user and 'country_code' in user['geoip'] else None,

0 commit comments

Comments
 (0)