Skip to content

Instantly share code, notes, and snippets.

@ekini
Created July 12, 2012 14:40
Show Gist options
  • Save ekini/3098532 to your computer and use it in GitHub Desktop.
Save ekini/3098532 to your computer and use it in GitHub Desktop.
gets contact info from icq v7
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from cStringIO import StringIO
from struct import unpack
import time
import struct
import pprint
try:
import sqlite3
except ImportError:
try:
from pysqlite2 import dbapi2 as sqlite3
except ImportError:
sqlite3 = None
getstr = lambda s, length: s.read(length).decode("utf-16").encode("utf-8").strip("\x00")
icqtime2string = lambda x: time.ctime(time.mktime(time.localtime(x*86400-2208988800-172800+3600)))
def main():
con = sqlite3.connect("Owner.qdb", 10)
cur = con.cursor()
cur.execute("select data from users WHERE Section=\"Details\"")
result = cur.fetchall()
for r in result:
s = StringIO(r[0])
def gettype(f):
isarray, = unpack("<l", f.read(4))
if not isarray:
ty = f.read(2)
return (False, ty)
if isarray == 0x11:
num, = unpack("<l", f.read(4))
return (True, num)
if isarray == 0x10:
num = 1
return (True, num)
raise ValueError(hex(isarray))
def getvalue(typeofval, f):
if typeofval == "\x08\x00":
valuelen, = unpack("<l", s.read(4))
value = getstr(s, valuelen)
return value
if typeofval == "\x03\x00": # long
value, = unpack("<l", f.read(4))
return value
if typeofval == "\x07\x00": # double, assume that it is always time
value, = unpack("<d", f.read(8))
return icqtime2string(value)
if typeofval == "\x0b\x00": # short
value, = unpack("<h", f.read(2))
return value
raise ValueError("unknown type %s" % typeofval.encode("hex"))
def getchain(f):
propertylen, = unpack("<l", f.read(4))
while propertylen:
prop = getstr(s, propertylen)
# got \x00\x00\x00\x00
if not propertylen:
raise StopIteration
t = gettype(f)
if t[0]: # if it is array
res = []
for x in xrange(0, t[1]):
f.seek(4, 1) # skip delimiter, \x10\x00\x00\x00
res.append(dict(getchain(f)))
yield(prop, res)
else:
value = getvalue(t[1], f)
yield prop, value
propertylen, = unpack("<l", f.read(4))
def getprops(f):
sep = f.read(4)
if sep == "\x10\x00\x00\x00":
return dict(getchain(f))
pprint.pprint(getprops(s))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment