/
skype_exporter
executable file
·191 lines (183 loc) · 5.54 KB
/
skype_exporter
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
#!/usr/bin/env ruby
require "sqlite3"
require "fileutils"
require "pathname"
require "json"
require "pp"
class MainDb
def initialize(path)
@path = path
@db = SQLite3::Database.new(path)
end
def get_data(query)
columns, *rows = @db.execute2(query)
rv = []
rows.each do |row|
ht = {}
(0...columns.size).each do |i|
ht[columns[i]] = row[i] unless row[i].nil?
end
rv << ht
end
rv
end
def get_all_data(table)
get_data("select * from #{table}")
end
end
class LogFile
def initialize
@msgs = []
end
def cleanup_xml(msg)
# Still need to be converted:
# <partlist alt="">
msg.gsub(%r[<ss type="(?:\S+?)">(\S+)</ss>]){ $1 }
.gsub(%r[<a href="([^"]+)">\1</a>]){ $1 }
.gsub(%r['], "'")
.gsub(%r["], '"')
.gsub(%r[>], ">")
.gsub(%r[<], "<") # This interferes with XML unescaping
.gsub(%r[&], "&")
end
def add!(tm, from, msg)
tm = Time.at(tm).utc.strftime("%Y-%m-%d %H:%M:%S")
@msgs << [tm, from, cleanup_xml(msg)]
end
def to_s
@msgs.sort.map{|tm,from,msg|
"#{tm} #{from}: #{msg}\n"
}.join("\n")
end
end
class SkypeExporter
def initialize(db_path, output_path)
@db = MainDb.new(db_path)
@path = Pathname(output_path)
@logs = {}
end
def log(key)
@logs[key] ||= LogFile.new
end
def save_binary_file!(file_name, data)
file_path = @path + file_name
file_path.dirname.mkpath
file_path.open('wb') do |fh|
fh.write(data)
end
file_path
end
def get_all_data(table)
@db.get_all_data(table)
end
def save_profile_attachments!(skype_name, data)
path_end_idx = 12+data[12..-1].index("\x00")
path = data[12...path_end_idx]
image_start_idx = path_end_idx+1+data[path_end_idx+1..-1].index("\x00")
image = data[image_start_idx+1..-1]
warn "Attachment isn't called MyAvatar" unless path == "profile://#{skype_name}/MyAvatar"
warn "Attachment for #{skype_name} doesn't look like JPEG image" unless image[0,4].bytes == "\xFF\xD8\xFF\xE0".bytes
save_binary_file!("attachments/#{skype_name}.jpg", image)
end
def save_avatar!(skype_name, data)
warn "Avatar for #{skype_name} might be incorrect" unless data[0, 5].bytes == "\x00\xFF\xD8\xFF\xE0".bytes
save_binary_file!("avatars/#{skype_name}.jpg", data[1..-1])
end
def save_text_file!(file_name, converter, data)
return if data.empty?
(@path + file_name).open('w') do |fh|
data.each_with_index do |row, i|
send("convert_#{converter}", row) do |key, val|
fh.puts "#{key}: #{val}"
end
fh.puts "" unless i == data.size-1
end
end
end
# accounts and contacts are very similar, so use the same code for both
def convert_account(row)
row.each do |key, val|
case key
when "capabilities",
"synced_email",
"cbl_profile_blob",
"authorization_certificate",
"saved_directory_blob",
"buddyblob"
# pass
when "avatar_image"
yield key, save_avatar!(row["skypename"], val)
when "profile_attachments"
yield key, save_profile_attachments!(row["skypename"], val)
else
yield key, val.inspect
end
end
end
def convert_basic(row)
row.each do |key, val|
yield key, val.inspect
end
end
def convert_transfer(row)
row.each do |key, val|
case key
when "nodeid",
"extprop_transfer_alias",
"chatmsg_guid"
# pass
when "starttime", "finishtime"
yield key, val == 0 ? "-" : Time.at(val)
else
yield key, val.inspect
end
end
end
def convert_message(row)
a = row["author"]
fd = row["from_dispname"]
b = row["body_xml"]
tm = row["timestamp"]
dp = row["dialog_partner"] || a
path = "#{dp}.txt"
log(dp).add! tm, "#{fd} (#{a})", b if b
row.each do |key, val|
case key
when "guid",
"extprop_chatmsg_ft_index_timestamp",
"crc"
# pass
when "timestamp"
yield key, val == 0 ? "-" : Time.at(val)
else
yield key, val.inspect
end
end
end
def export!
@path.rmtree if @path.exist?
@path.mkpath
save_text_file! "accounts.txt", :account, get_all_data("Accounts")
save_text_file! "contacts.txt", :account, get_all_data("Contacts")
save_text_file! "sms.txt", :basic, get_all_data("SMSes")
save_text_file! "legacy_messages.txt", :basic, get_all_data("LegacyMessages")
save_text_file! "alerts.txt", :basic, get_all_data("Alerts")
save_text_file! "transfers.txt", :transfer, get_all_data("Transfers")
save_text_file! "voicemails.txt", :basic, get_all_data("Voicemails")
save_text_file! "call_members.txt", :basic, get_all_data("CallMembers")
save_text_file! "calls.txt", :basic, get_all_data("Calls")
save_text_file! "chat_members.txt", :basic, get_all_data("ChatMembers")
save_text_file! "chats.txt", :basic, get_all_data("Chats")
save_text_file! "contact_groups.txt", :basic, get_all_data("ContactGroups")
save_text_file! "conversations.txt", :basic, get_all_data("Conversations")
save_text_file! "messages.txt", :message, get_all_data("Messages")
save_text_file! "participants.txt", :basic, get_all_data("Participants")
save_text_file! "video_messages.txt", :basic, get_all_data("VideoMessages")
save_text_file! "videos.txt", :basic, get_all_data("Videos")
@logs.each do |key, log|
save_binary_file!("logs/#{key}.txt", log.to_s)
end
end
end
se = SkypeExporter.new("main.db", "output")
se.export!