forked from nicotine-plus/nicotine-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nicotine-import-winconfig
339 lines (256 loc) · 9.97 KB
/
nicotine-import-winconfig
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python
# Import the windows slsk config files in Nicotine.
# Geert Kloosterman <[email protected]>
from struct import unpack
import getopt
import sys
import os
import string
import imghdr
try:
from pynicotine.config import *
except ImportError,e:
print """Error: Cannot find the Nicotine config module.
If you're running Nicotine from a src tarball, copy this program to the
source directory and try running it from there."""
sys.exit(1)
def usage():
print """Usage: nicotine-import-winconfig [OPTIONS] <path_to_the_winslsk_files>
-c file, --config=file Use non-default nicotine configuration file to write to
-q, --queue Import the download queue
-u, --userlist Import the userlist
-l, --login Import the login information
-C, --chatrooms Import the list of autojoined chatrooms
-b, --banned Import the list of banned users
-i, --userinfo Import the userinfo and picture
-h, --help Print this help message and exit
If no configuration items are specified all are imported.
Run this script when Nicotine is _not_ running.
See README.import-winconfig from the Nicotine distribution for more info.
Please report any problems to our bugtracker :
http://www.nicotine-plus.org/newticket"""
def parse_args():
global opt
opt = {}
try:
opts, args = getopt.getopt(sys.argv[1:],
"c:qulCbIih",
["config=", "queue", "userlist", "ignored", "login",
"chatrooms", "banned", "userinfo", "help"])
except getopt.GetoptError:
usage()
sys.exit(1)
# check for winpath
if len(args) < 1:
usage()
sys.exit(1)
slskdir = args[0]
check_slskdir(slskdir)
opt['slskdir'] = slskdir
# defaults
opt['config'] = os.path.join(os.path.expanduser("~"),'.nicotine/config')
queue = userlist = ignored = login = chatrooms = banned = userinfo = None
for o, a in opts:
if o in ("-c", "--config"):
opt['config'] = a
if o in ("-q", "--queue"):
queue = 1
if o in ("-u", "--userlist"):
userlist = 1
if o in ("-l", "--login"):
login = 1
if o in ("-C", "--chatrooms"):
chatrooms = 1
if o in ("-b", "--banned"):
banned = 1
if o in ("-I", "--ignored"):
ignored = 1
if o in ("-i", "--userinfo"):
userinfo = 1
if o in ("-h", "--help"):
usage()
sys.exit()
# import all when none specified
if not (queue or userlist or login or chatrooms or banned or userinfo):
queue = userlist = login = ignored = chatrooms = banned = userinfo = 1
opt['queue'] = queue
opt['userlist'] = userlist
opt['login'] = login
opt['chatrooms'] = chatrooms
opt['banned'] = banned
opt['ignored'] = ignored
opt['userinfo'] = userinfo
def check_slskdir(slskdir):
# we check if the file queue2.cfg exists under the slsk dir
queue2path = os.path.join(slskdir, 'queue2.cfg')
if not os.access(queue2path, os.F_OK):
print "nicotine-import-winconfig: Error: Could not find \"queue2.cfg\" in \"%s\"" % (slskdir)
print "Are you sure you've supplied the right path?"
sys.exit(1)
def winpath(file):
global opt
return os.path.join(opt['slskdir'], file)
def get_downloads(fname):
"""Returns the list of downloads in the queue2.cnf file
The windows slsk queue2.cnf file format:
- little-endian, wordsize=4
- The file starts with 3 ints (4bytes per int).
- First 2 ints are unknown
- The 3rd one is the number of entries in the file
Then for each download entry:
- The length of the username, followed by the username
- The length of the remote filename, followed by the remote filename
- The length of the local dir, followed by the local dir
The file can contain some unknown stuff at the end."""
infile = open(fname, 'r')
intsize = 4
downloads = []
str = infile.read(3*intsize)
i1, i2, n_entries = unpack("iii", str)
for i in range(n_entries):
length = unpack("i", infile.read(intsize))[0]
uname = infile.read(length)
length = unpack("i", infile.read(intsize))[0]
remfile = infile.read(length)
length = unpack("i", infile.read(intsize))[0]
localdir = infile.read(length)
downloads.append([uname, remfile, localdir])
infile.close()
return downloads
def get_user_and_pass(fname):
"""Returns a (user,passwd) tuple.
File format of login.cfg (see also get_downloads):
- two unknown ints at the beginning
- the number of bytes of the username, followed by the username
- the number of bytes from the passwd, followed by the passwd
- followed by some undetermined bytes"""
infile = open(fname, 'r')
intsize = 4
downloads = []
str = infile.read(3*intsize)
i1, i2, length = unpack("iii", str)
user = infile.read(length)
length = unpack("i", infile.read(intsize))[0]
passwd = infile.read(length)
infile.close()
return (user, passwd)
def get_basic_config(fname):
"""Works for userlist, chatrooms, dlbans, etc.
The hotlist.cnf file format (see get_downloads for more info):
- The file starts with an int: the number of users in the list
Then for each user:
- The length of the username, followed by the username"""
infile = open(fname, 'r')
intsize = 4
users = []
str = infile.read(1*intsize)
n_entries = unpack("i", str)[0]
for i in range(n_entries):
length = unpack("i", infile.read(intsize))[0]
user = infile.read(length)
users.append(user)
infile.close()
return users
def get_userinfo(fname):
"""The userinfo file format:
- an int with the size of the text part, followed by the text part
- one byte, an indication for an image. 0 is no image, 1 is image
- an int with the image size, followed by the image data """
infile = open(fname, 'r')
intsize = 4
imgdata = None
str = infile.read(intsize)
length = unpack("i", str)[0]
descr = infile.read(length)
descr = string.replace(descr, "\r", "")
has_image = unpack("b",infile.read(1))[0]
if has_image:
length = unpack("i", infile.read(intsize))[0]
imgdata = infile.read(length)
infile.close()
return (descr, imgdata)
def save_image(imgdata):
"""Save IMGDATA to file and return the filename or None."""
global opt
if not imgdata:
print "No image data. No image saved."
return ""
extension = imghdr.what(None, imgdata)
if not extension:
# how to print to stderr in python? (no real problem here)
print "Could not determine image type. Image not saved."
return ""
fname = os.path.abspath(opt['config'] + '-userinfo.' + extension)
outfile = open(fname, "w")
outfile.write(imgdata)
outfile.close()
print "Wrote image to \"%s\"" % (fname)
return fname
def is_in_user_list(user, user_list):
"""Checks if USER is in USER_LIST, ignoring the comment field"""
for i in user_list:
if type(i) == type(''):
sys.stderr.write("\nError: The nicotine userlist is in an old pyslsk format.\n" +
"Please run Nicotine once before running this script.\n" +
"Config file not updated.\n")
sys.exit(1)
if user == i[0]:
return 1
return 0
def main():
global opt
parse_args()
# get the Nicotine configfile
config = Config(opt['config'])
config.readConfig()
# get the windows info and update the sections
if opt['queue']:
print "Getting download queue..."
windownloads = get_downloads(winpath('queue2.cfg'))
for i in windownloads:
if not i in config.sections["transfers"]["downloads"]:
config.sections["transfers"]["downloads"].append(i)
if opt['userlist']:
print "Getting userlist..."
users = get_basic_config(winpath('hotlist.cfg'))
for i in users:
if not is_in_user_list(i,config.sections["server"]["userlist"]):
config.sections["server"]["userlist"].append([i,''])
if opt['login']:
print "Getting login and password..."
(login,passw) = get_user_and_pass(winpath('login.cfg'))
config.sections["server"]["login"] = login
config.sections["server"]["passw"] = passw
if opt['chatrooms']:
print "Getting the list of autojoined chatrooms..."
chatrooms = get_basic_config(winpath('chatrooms.cfg'))
chatrooms.append('nicotine')
for i in chatrooms:
if i not in config.sections["server"]["autojoin"]:
config.sections["server"]["autojoin"].append(i)
if opt['banned']:
print "Getting the list of banned users..."
banlist = get_basic_config(winpath('dlbans.cfg'))
for i in banlist:
if i not in config.sections["server"]["banlist"]:
config.sections["server"]["banlist"].append(i)
if opt['ignored']:
print "Getting the list of ignored users..."
ignorelist = get_basic_config(winpath('ignores.cfg'))
for i in ignorelist:
if i not in config.sections["server"]["ignorelist"]:
config.sections["server"]["ignorelist"].append(i)
if opt['userinfo']:
print "Getting userinfo and image..."
(descr, imgdata) = get_userinfo(winpath('userinfo.cfg'))
if descr:
config.sections["userinfo"]['descr'] = descr.__repr__()
if imgdata:
config.sections["userinfo"]['pic'] = save_image(imgdata)
# write it
config.writeConfig()
print "Updated the Nicotine config file (%s)" % (opt['config'])
print "\nMake sure you set the server option and download folder in the Settings panel."
print "Don't forget to add your shares..."
if __name__ == '__main__':
main()