blob: e4a495555d74366111f2f824caf5bc6a0baa429d [file] [log] [blame]
Jan Luebbe6f656fc2009-08-13 00:58:34 +02001#!/usr/bin/python2.5
2
3from __future__ import with_statement
4
5from pysqlite2 import dbapi2 as sqlite3
6import sys
7
8hlr = sqlite3.connect(sys.argv[1])
9web = sqlite3.connect(sys.argv[2])
10
11# switch to autocommit
12hlr.isolation_level = None
13web.isolation_level = None
14
15hlr.row_factory = sqlite3.Row
16web.row_factory = sqlite3.Row
17
18with hlr:
19 hlr_subscrs = hlr.execute("""
20 SELECT * FROM Subscriber
21 """).fetchall()
22 hlr_tokens = hlr.execute("""
23 SELECT * FROM AuthToken
24 """).fetchall()
25
26with web:
27 web_tokens = web.execute("""
28 SELECT * FROM reg_tokens
29 """).fetchall()
Stefan Schmidt5f8a78b2009-08-14 21:33:34 +020030 web_sms = web.execute("""
31 SELECT * FROM sms_queue
32 """).fetchall()
Jan Luebbe6f656fc2009-08-13 00:58:34 +020033
34# index by subscr id
35hlr_subscrs_by_id = {}
Stefan Schmidt5f8a78b2009-08-14 21:33:34 +020036hlr_subscrs_by_ext = {}
Jan Luebbe6f656fc2009-08-13 00:58:34 +020037hlr_tokens_by_subscr_id = {}
38for x in hlr_subscrs:
39 hlr_subscrs_by_id[x['id']] = x
Stefan Schmidt5f8a78b2009-08-14 21:33:34 +020040 hlr_subscrs_by_ext[x['extension']] = x
Jan Luebbe6f656fc2009-08-13 00:58:34 +020041del hlr_subscrs
42for x in hlr_tokens:
43 hlr_tokens_by_subscr_id[x['subscriber_id']] = x
44del hlr_tokens
45
46web_tokens_by_subscr_id = {}
47for x in web_tokens:
48 web_tokens_by_subscr_id[x['subscriber_id']] = x
49del web_tokens
50
51# remove leftover web_tokens and correct inconsistent fields
52with web:
53 for x in web_tokens_by_subscr_id.values():
54 subscr = hlr_subscrs_by_id.get(x['subscriber_id'], None)
55 if subscr is None:
56 web.execute("""
57 DELETE FROM reg_tokens WHERE subscriber_id = ?
58 """, (x['subscriber_id'],))
59 del web_tokens_by_subscr_id[x['subscriber_id']]
60 continue
61 if str(x['imsi']) != str(subscr['imsi']) or \
62 x['extension'] != subscr['extension'] or \
63 x['tmsi'] != subscr['tmsi'] or \
64 x['lac'] != subscr['lac']:
65 web.execute("""
66 UPDATE reg_tokens
67 SET imsi = ?, extension = ?, tmsi = ?, lac = ?
68 WHERE subscriber_id = ?
Jan Luebbebf0d5bd2009-08-13 20:52:55 +020069 """, (str(subscr['imsi']), subscr['extension'],
70 subscr['tmsi'], subscr['lac'], x['subscriber_id']))
Jan Luebbe6f656fc2009-08-13 00:58:34 +020071
72# add missing web_tokens
73with web:
74 for x in hlr_tokens_by_subscr_id.values():
75 subscr = hlr_subscrs_by_id.get(x['subscriber_id'], None)
76 if subscr is None:
77 hlr.execute("""
78 DELETE FROM AuthToken WHERE subscriber_id = ?
79 """, (x['subscriber_id'],))
80 del hlr_tokens_by_subscr_id[x['subscriber_id']]
81 continue
82 webtoken = web_tokens_by_subscr_id.get(x['subscriber_id'], None)
83 if webtoken is None:
84 web.execute("""
85 INSERT INTO reg_tokens
86 (subscriber_id, extension, reg_completed, name, email, lac, imsi, token, tmsi)
87 VALUES
88 (?, ?, 0, ?, '', ?, ?, ?, ?)
89 """, (x['subscriber_id'], subscr['extension'], subscr['name'],
90 subscr['lac'], str(subscr['imsi']), x['token'], subscr['tmsi']))
91
92# authorize subscribers
93with hlr:
94 for x in web_tokens_by_subscr_id.values():
95 subscr = hlr_subscrs_by_id.get(x['subscriber_id'], None)
96 if x['reg_completed'] and not subscr['authorized']:
97 hlr.execute("""
98 UPDATE Subscriber
99 SET authorized = 1
100 WHERE id = ?
101 """, (x['subscriber_id'],))
102
Stefan Schmidt5f8a78b2009-08-14 21:33:34 +0200103# Sync SMS from web to hlr
104with hlr:
105 for sms in web_sms:
106 subscr = hlr_subscrs_by_ext.get(sms['receiver_ext'])
107 if subscr is None:
108 print '%s not found' % sms['receiver_ext']
109 continue
110 hlr.execute("""
111 INSERT INTO SMS
112 (created, sender_id, receiver_id, reply_path_req, status_rep_req, protocol_id, data_coding_scheme, ud_hdr_ind, text)
113 VALUES
114 (?, 1, ?, 0, 0, 0, 0, 0, ?)
115 """, (sms['created'], subscr['id'], sms['text']))
116with web:
117 for sms in web_sms:
118 web.execute("""
119 DELETE FROM sms_queue WHERE id = ?
120 """, (sms['id'],))
121
122
Jan Luebbe6f656fc2009-08-13 00:58:34 +0200123hlr.close()
124web.close()
125