-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate-xchat.js
executable file
·224 lines (190 loc) · 6.55 KB
/
migrate-xchat.js
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
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/gjs
const Lang = imports.lang;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const GObject = imports.gi.GObject;
const GIRepository = imports.gi.GIRepository;
const System = imports.system;
const SEPARATOR = '='
const FLAG_USE_SSL = 4
const FLAG_AUTO_CONNECT = 8
function uniq(a) {
return a.sort().filter(function(item, pos, ary) {
return !pos || item != ary[pos - 1];
})
}
function flagMatches(flag, bitmask) {
if (flag == NaN)
return false;
return (flag & bitmask);
}
function parseSection(section) {
// Split into lines, and stuff into a dict
let keys = {}
let lines = section.split('\n');
let array;
for (let i = 0; i < lines.length; i++) {
if (lines[i].length <= 2 || lines[i][1] != '=') {
//log('Ignoring ' + lines[i]);
continue;
}
let key = lines[i][0];
// Ignore keys that have already been set
if (key in keys)
continue;
let value = lines[i].substring(2);
keys[key] = value;
}
// Ignore if there's no server line
if (!keys['S'])
return null;
let name = ('N' in keys) ? keys['N'] : keys['S'];
// Ignore if the server isn't autoconnect
if (!flagMatches(Number(keys['F']), FLAG_AUTO_CONNECT)) {
//log('Server ' + name + ' is not autoconnect, ignoring');
return null;
}
let useSSL = flagMatches(Number(keys['F']), FLAG_USE_SSL);
let serverIP, port;
if (keys['S'].indexOf('/') == -1) {
serverIP = keys['S'];
} else {
serverIP = keys['S'].split('/')[0];
let portSection = keys['S'].split('/')[1];
if (portSection[0] == '+') {
useSSL = true;
port = portSection.substring(1);
} else {
port = portSection;
}
}
let server = {};
server.networkName = name;
server.serverAddress = serverIP;
server.secureConnection = useSSL;
server.nickname = keys['I'];
server.realName = keys['R'];
//FIXME ignore accounts without join?
if (keys['J']) {
server.rooms = keys['J'].split(',');
server.rooms = uniq(server.rooms);
}
server.nickservPassword = keys['B'];
server.serverPassword = keys['P'];
server.serverPort = port;
server.nickServ = keys['B'];
print ('Parsed auto-connect server ' + name);
return server;
}
function parseConfiguration(data) {
// Split into sections and parse each one
let sections = String(data).split('\n\n');
let nickUsage = [];
let servers = [];
for (let i = 0; i < sections.length; i++) {
let server = parseSection(sections[i]);
if (server) {
if (!(server.nickname in nickUsage))
nickUsage[server.nickname] = 0;
server.accountNum = nickUsage[server.nickname]++;
servers.push(server);
}
}
return servers
}
function writeTelepathyConfig(data) {
let str = '# Telepathy accounts\n\n';
let nickUsage = [];
for (let i = 0; i < data.length; i++) {
let server = data[i];
str += "[idle/irc/" + server.nickname + server.accountNum + ']\n';
str += 'manager=idle\n';
str += 'protocol=irc\n';
str += 'DisplayName=' + server.networkName + '\n';
str += 'Enabled=true\n';
if (server.nickname != GLib.get_user_name())
str += 'param-username=' + server.nickname + '\n';
str += 'Service=' + server.networkName.replace(/[^\w]/gi, '_').toLowerCase() + '\n';
str += 'param-account=' + server.nickname + '\n';
str += 'param-server=' + server.serverAddress + '\n';
str += 'param-use-ssl=' + (server.secureConnection ? 'true' : 'false') + '\n';
if (server.serverPort != undefined)
str += 'param-port=' + server.serverPort + '\n';
str += '\n';
}
return str;
}
function getSavedChannedList(data) {
let list = [];
for (let i = 0; i < data.length; i++) {
let server = data[i];
let accountPath = "/org/freedesktop/Telepathy/Account/idle/irc/" + server.nickname + server.accountNum;
if (server.rooms != undefined) {
for (let idx in server.rooms) {
list.push({
account: new GLib.Variant('s', accountPath),
channel: new GLib.Variant('s', server.rooms[idx])
});
}
}
}
return list;
}
function main(ARGV) {
// Argument parsing
let input, output, testOutput;
if (ARGV.length >= 1)
input = ARGV[0];
else
input = GLib.get_home_dir() + '/.xchat-gnome/servlist_.conf'; //FIXME look in more places
testOutput = false;
if (ARGV.length == 2) {
output = ARGV[1] + '/accounts.cfg';
testOutput = true;
} else
output = GLib.get_user_data_dir() + '/telepathy/mission-control/accounts.cfg'
let inputFile = Gio.File.new_for_path(input);
let outputFile = Gio.File.new_for_path(output);
print ("Processing " + inputFile.get_path() + " to " + outputFile.get_path());
// Check that output doesn't already exist
if (GLib.file_test(output, GLib.FileTest.EXISTS)) {
print ('Output file ' + output + ' already exists, aborting.');
return 1;
}
// Parse the configuration
let success, data, servers;
try {
[success, data, ] = inputFile.load_contents(null);
servers = parseConfiguration(data);
} catch(e) {
log('Failed to load configuration file: ' + e.message);
return 1;
}
print ('Parsed ' + servers.length + ' servers');
// Write the telepathy configuration
let str;
try {
str = writeTelepathyConfig(servers);
outputFile.replace_contents(str, null, false, Gio.FileCreateFlags.NONE, null);
} catch(e) {
log('Failed to write Telepathy config: ' + e.message);
return 1;
}
// Write the Polari channels configuration
try {
let list = getSavedChannedList(servers);
let variant = GLib.Variant.new('aa{sv}', list);
if (!testOutput) {
let settings = new Gio.Settings({ schema_id: 'org.gnome.Polari' });
settings.set_value('saved-channel-list', variant);
} else {
let settingsOutput = ARGV[1] + '/settings.txt';
let settingsOutputFile = Gio.File.new_for_path(settingsOutput);
settingsOutputFile.replace_contents(variant.print(true), null, false, Gio.FileCreateFlags.NONE, null);
}
} catch(e) {
log("Failed to set 'saved-channel-list' settings: " + e.message);
return 1;
}
}
main(ARGV);