generated from NethServer/ns8-kickstart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
40migration
executable file
·144 lines (127 loc) · 4.68 KB
/
40migration
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
#!/usr/bin/env python3
#
# Copyright (C) 2023 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#
import json
import uuid
import os
import sys
file_path = './getmail.json'
with open(file_path, 'r') as file:
data = json.load(file)
# Extract properties for type "getmail" with unique IDs
getmail_properties = []
for entry in data:
if entry['type'] == 'getmail':
props = entry['props']
unique_id = str(uuid.uuid4())[:6]
properties = {
'ID': unique_id,
'Account': props.get('Account',''),
'Password': props.get('Password', ''),
'Server': props.get('Server',''),
'Username': props.get('Username',''),
'Retriever': props.get('Retriever',''),
'Delete': props.get('Delete', ''),
'Time': props.get('Time', ''),
'Status': props.get('status', ''),
'FilterCheck': props.get('FilterCheck', '')
}
if properties['Account'] == "":
print("Account: no argument given", file=sys.stderr)
continue # skip empty accounts
elif properties['Password'] == "":
print("Password: no argument given", file=sys.stderr)
continue # skip accounts without password
elif properties['Server'] == "":
print("Server: no argument given", file=sys.stderr)
continue # skip accounts without server
elif properties['Username'] == "":
print("Username: no argument given", file=sys.stderr)
continue # skip accounts without username
elif properties['Retriever'] == "":
print("Retriever: no argument given", file=sys.stderr)
continue # skip accounts without retriever
getmail_properties.append(properties)
# iterate over keys and write tasks files
for properties in getmail_properties:
task_id = properties['ID']
localuser = properties['Account'].split('@')[0]
remotehostname = properties['Server']
# convert getmail protocol to imapsync protocol
if properties['Retriever'] == "SimplePOP3Retriever":
remoteport = "143"
security = ""
# Pop3 to IMAP not sure it will work, we disable
cron = ""
elif properties['Retriever'] == "SimplePOP3SSLRetriever":
remoteport = "993"
security = "--ssl1"
# Pop3 to IMAP not sure it will work, we disable
cron = ""
elif properties['Retriever'] == "SimpleIMAPRetriever":
remoteport = '143'
security = ''
cron = properties['Time']
elif properties['Retriever'] == "SimpleIMAPSSLRetriever":
remoteport = '993'
security = '--ssl1'
cron = properties['Time']
remoteusername = properties['Username']
remotepassword = properties['Password']
# options not available in getmail, we disable for validation
delete_local = ""
delete_folder = ""
# delete on remote once migrated
delete_remote = properties['Delete']
# in imapsync we just have an option to remove it immediately
if delete_remote != "-1":
delete_remote = "--delete1"
expunge_remote = "--noexpungeaftereach"
else:
delete_remote = ""
expunge_remote = ""
# not available in getmail, we disable for validation
exclude = ""
# cron
if cron != "":
if cron == '60':
cron_env = "1 */1 * * * root /usr/local/bin/syncctl start "+localuser+'_'+task_id
cron = '1h'
else:
cron_env = "*/"+cron+" * * * * root /usr/local/bin/syncctl start "+localuser+'_'+task_id
cron = cron + 'm'
f = open("./cron/"+localuser+'_'+task_id+".cron", "w", encoding="utf-8")
# MAIL_HOST is an env variable used by perl/imapsync
f.write('MAIL_HOST='+os.environ['MAIL_HOST']+"\n")
f.write(cron_env+"\n")
f.close()
foldersynchronization = "inbox"
folder_inbox = "--folder\ 'INBOX'"
user_env = f"""
TASK_ID={task_id}
LOCALUSER={localuser}
REMOTEUSERNAME={remoteusername}
REMOTEHOSTNAME={remotehostname}
REMOTEPORT={remoteport}
SECURITY={security}
DELETE_LOCAL={delete_local}
DELETEFOLDER={delete_folder}
EXCLUDE={exclude}
DELETE_REMOTE={delete_remote}
EXPUNGE_REMOTE={expunge_remote}
CRON={cron}
FOLDER_INBOX={folder_inbox}
FOLDERSYNCHRONIZATION={foldersynchronization}
"""
os.umask(0o77)
f = open("./imapsync/"+localuser+'_'+task_id+".env", "w", encoding="utf-8")
f.write(user_env)
f.close()
f = open("./imapsync/"+localuser+'_'+task_id+".pwd", "w", encoding="utf-8")
f.write(remotepassword)
f.close()
# Remove the file
os.remove(file_path)
print(f"File {file_path} removed.", file=sys.stderr)