Initial commit: SecourIT Gestion

This commit is contained in:
2026-08-23 09:06:18 +00:00
commit cab93c7f7c
85 changed files with 15801 additions and 0 deletions
+99
View File
@@ -0,0 +1,99 @@
#!/usr/bin/python3
import sys, json, os, urllib.request, urllib.error, base64
from email import message_from_bytes
from email.utils import parseaddr
from email.header import decode_header
def decode_str(val):
if not val:
return ''
parts = decode_header(val)
result = []
for part, charset in parts:
if isinstance(part, bytes):
try:
result.append(part.decode(charset or 'utf-8', errors='replace'))
except (LookupError, ValueError):
result.append(part.decode('utf-8', errors='replace'))
else:
result.append(part)
return ' '.join(result).strip()
def decode_payload(part):
payload = part.get_payload(decode=True)
if not payload:
return ''
charset = part.get_content_charset() or 'utf-8'
try:
return payload.decode(charset, errors='replace').strip()
except (LookupError, ValueError):
return payload.decode('utf-8', errors='replace').strip()
try:
msg = message_from_bytes(sys.stdin.buffer.read())
from_hdr = parseaddr(msg['From'] or '')[1]
subject_hdr = decode_str(msg['Subject'] or '')
text_body = ''
html_body = ''
attachments = []
if msg.is_multipart():
for part in msg.walk():
ct = part.get_content_type()
if ct == 'text/plain' and not text_body:
text_body = decode_payload(part)
elif ct == 'text/html' and not html_body:
html_body = decode_payload(part)
elif ct.startswith('image/'):
payload = part.get_payload(decode=True)
if payload:
cid = part.get('Content-ID', '')
if cid:
cid = cid.strip('<>')
filename = part.get_filename() or 'image.png'
attachments.append({
'filename': filename,
'contentType': ct,
'contentId': cid or None,
'data': base64.b64encode(payload).decode('ascii'),
})
else:
text_body = decode_payload(msg)
if not text_body and html_body:
text_body = html_body
token = ''
with open('/var/www/gestion-app/.env') as f:
for line in f:
if line.startswith('SERVER_MONITOR_SECRET='):
token = line.split('=', 1)[1].strip()
if not token:
sys.exit(1)
payload = {
'from': from_hdr,
'subject': subject_hdr,
'text': text_body,
}
if html_body:
payload['html'] = html_body
if attachments:
payload['attachments'] = attachments
data = json.dumps(payload).encode('utf-8')
req = urllib.request.Request(
'http://localhost:3005/api/tickets/email?token={}'.format(token),
data=data, headers={'Content-Type': 'application/json'}, method='POST'
)
urllib.request.urlopen(req, timeout=30)
sys.exit(0)
except Exception as e:
with open('/tmp/ticket_handler_error.log', 'w') as f:
f.write('Error: {}\n'.format(e))
f.write('Type: {}\n'.format(type(e).__name__))
import traceback
traceback.print_exc(file=f)
sys.exit(1)