]> git.stg.codes - stg.git/blob - projects/convertor/main.cpp
Rename BASE_AUTH and BASE_STORE to AUTH and STORE
[stg.git] / projects / convertor / main.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21  /*
22  $Revision: 1.11 $
23  $Date: 2010/03/25 12:32:30 $
24  $Author: faust $
25  */
26
27 #include <dlfcn.h>
28
29 #include <string>
30 #include <vector>
31 #include <iostream>
32 #include <ctime>
33 #include <algorithm>
34
35 #include "common.h"
36 #include "store.h"
37 #include "settings.h"
38 #include "conffiles.h"
39
40 #include "user_stat.h"
41 #include "user_conf.h"
42 #include "corp_conf.h"
43 #include "service_conf.h"
44 #include "admin_conf.h"
45 #include "tariff_conf.h"
46 #include "module_settings.h"
47 #include "stg_message.h"
48
49 using namespace std;
50
51 volatile time_t stgTime = time(NULL);
52
53 int main(int argc, char **argv)
54 {
55 printfd(__FILE__, "Start\n");
56
57 STORE * fromStore = NULL;
58 STORE * toStore = NULL;
59
60 SETTINGS * settings = NULL;
61
62 string modulePath;
63
64 MODULE_SETTINGS fromStoreSettings;
65 MODULE_SETTINGS toStoreSettings;
66
67 ADMIN_CONF ac;
68 USER_CONF uc;
69 USER_STAT us;
70 STG_MSG msg;
71 TARIFF_DATA td;
72 CORP_CONF cc;
73 SERVICE_CONF sc;
74 vector<STG_MSG_HDR> hdrs;
75 vector<STG_MSG_HDR>::iterator mit;
76
77 void * src_lh;
78 void * dst_lh;
79
80 if (argc == 2)
81     settings = new SETTINGS(argv[1]);
82 else
83     settings = new SETTINGS();
84
85 if (settings->ReadSettings())
86 {
87     printfd(__FILE__, "Error reading settings\n");
88     delete settings;
89     return -1;
90 }
91
92 fromStoreSettings = settings->GetSourceStoreModuleSettings();
93 toStoreSettings = settings->GetDestStoreModuleSettings();
94 modulePath = settings->GetModulesPath();
95
96 string sourcePlugin(modulePath + "/mod_" + fromStoreSettings.moduleName + ".so");
97 string destPlugin(modulePath + "/mod_" + toStoreSettings.moduleName + ".so");
98
99 src_lh = dlopen(sourcePlugin.c_str(), RTLD_NOW);
100 if (!src_lh)
101     {
102     printfd(__FILE__, "Source storage plugin loading failed: %s\n", dlerror());
103     delete settings;
104     return -1;
105     }
106
107 dst_lh = dlopen(destPlugin.c_str(), RTLD_NOW);
108 if (!dst_lh)
109     {
110     printfd(__FILE__, "Destination storage plugin loading failed: %s\n", dlerror());
111     delete settings;
112     return -1;
113     }
114
115 STORE * (*GetSourceStore)();
116 STORE * (*GetDestStore)();
117 GetSourceStore = (BASE_STORE * (*)())dlsym(src_lh, "GetStore");
118 if (!GetSourceStore)
119     {
120     printfd(__FILE__, "Source storage plugin loading failed. GetStore not found: %s\n", dlerror());
121     delete settings;
122     return -1;
123     }
124 GetDestStore = (BASE_STORE * (*)())dlsym(dst_lh, "GetStore");
125 if (!GetDestStore)
126     {
127     printfd(__FILE__, "Storage plugin (firebird) loading failed. GetStore not found: %s\n", dlerror());
128     delete settings;
129     return -1;
130     }
131
132 fromStore = GetSourceStore();
133 toStore = GetDestStore();
134
135 vector<string> entities;
136 vector<string> ready;
137 vector<string>::const_iterator it;
138 fromStore->SetSettings(fromStoreSettings);
139 fromStore->ParseSettings();
140 toStore->SetSettings(toStoreSettings);
141 toStore->ParseSettings();
142
143 printfd(__FILE__, "Importing admins:\n");
144 entities.erase(entities.begin(), entities.end());
145 ready.erase(ready.begin(), ready.end());
146 if (fromStore->GetAdminsList(&entities))
147     {
148     printfd(__FILE__, "Error getting admins list: %s\n", fromStore->GetStrError().c_str());
149     dlclose(src_lh);
150     dlclose(dst_lh);
151     delete settings;
152     return -1;
153     }
154 if (toStore->GetAdminsList(&ready))
155     {
156     printfd(__FILE__, "Error getting admins list: %s\n", toStore->GetStrError().c_str());
157     dlclose(src_lh);
158     dlclose(dst_lh);
159     delete settings;
160     return -1;
161     }
162 for (it = entities.begin(); it != entities.end(); ++it)
163     {
164     printfd(__FILE__, "\t - %s\n", it->c_str());
165     if (find(ready.begin(), ready.end(), *it) == ready.end())
166         if (toStore->AddAdmin(*it))
167             {
168             printfd(__FILE__, "Error adding admin: %s\n", toStore->GetStrError().c_str());
169             dlclose(src_lh);
170             dlclose(dst_lh);
171             delete settings;
172             return -1;
173             }
174     if (fromStore->RestoreAdmin(&ac, *it))
175         {
176         printfd(__FILE__, "Error getting admin's confi: %s\n", fromStore->GetStrError().c_str());
177         dlclose(src_lh);
178         dlclose(dst_lh);
179         delete settings;
180         return -1;
181         }
182     ac.login = *it;
183     if (toStore->SaveAdmin(ac))
184         {
185         printfd(__FILE__, "Error saving admin's conf: %s\n", toStore->GetStrError().c_str());
186         dlclose(src_lh);
187         dlclose(dst_lh);
188         delete settings;
189         return -1;
190         }
191     }
192
193 printfd(__FILE__, "Importing tariffs:\n");
194 entities.erase(entities.begin(), entities.end());
195 ready.erase(ready.begin(), ready.end());
196 if (fromStore->GetTariffsList(&entities))
197     {
198     printfd(__FILE__, "Error getting tariffs list: %s\n", fromStore->GetStrError().c_str());
199     dlclose(src_lh);
200     dlclose(dst_lh);
201     delete settings;
202     return -1;
203     }
204 if (toStore->GetTariffsList(&ready))
205     {
206     printfd(__FILE__, "Error getting tariffs list: %s\n", toStore->GetStrError().c_str());
207     dlclose(src_lh);
208     dlclose(dst_lh);
209     delete settings;
210     return -1;
211     }
212 for (it = entities.begin(); it != entities.end(); ++it)
213     {
214     printfd(__FILE__, "\t - %s\n", it->c_str());
215     if (find(ready.begin(), ready.end(), *it) == ready.end())
216         if (toStore->AddTariff(*it))
217             {
218             printfd(__FILE__, "Error adding tariff: %s\n", toStore->GetStrError().c_str());
219             dlclose(src_lh);
220             dlclose(dst_lh);
221             delete settings;
222             return -1;
223             }
224     if (fromStore->RestoreTariff(&td, *it))
225         {
226         printfd(__FILE__, "Error getting tariff's data: %s\n", fromStore->GetStrError().c_str());
227         dlclose(src_lh);
228         dlclose(dst_lh);
229         delete settings;
230         return -1;
231         }
232     if (toStore->SaveTariff(td, *it))
233         {
234         printfd(__FILE__, "Error saving tariff's data: %s\n", toStore->GetStrError().c_str());
235         dlclose(src_lh);
236         dlclose(dst_lh);
237         delete settings;
238         return -1;
239         }
240     }
241
242 printfd(__FILE__, "Importing services:\n");
243 entities.erase(entities.begin(), entities.end());
244 ready.erase(ready.begin(), ready.end());
245 if (fromStore->GetServicesList(&entities))
246     {
247     printfd(__FILE__, "Error getting service list: %s\n", fromStore->GetStrError().c_str());
248     dlclose(src_lh);
249     dlclose(dst_lh);
250     delete settings;
251     return -1;
252     }
253 if (toStore->GetServicesList(&ready))
254     {
255     printfd(__FILE__, "Error getting service list: %s\n", toStore->GetStrError().c_str());
256     dlclose(src_lh);
257     dlclose(dst_lh);
258     delete settings;
259     return -1;
260     }
261 for (it = entities.begin(); it != entities.end(); ++it)
262     {
263     printfd(__FILE__, "\t - %s\n", it->c_str());
264     if (find(ready.begin(), ready.end(), *it) == ready.end())
265         if (toStore->AddService(*it))
266             {
267             printfd(__FILE__, "Error adding service: %s\n", toStore->GetStrError().c_str());
268             dlclose(src_lh);
269             dlclose(dst_lh);
270             delete settings;
271             return -1;
272             }
273     if (fromStore->RestoreService(&sc, *it))
274         {
275         printfd(__FILE__, "Error getting service's data: %s\n", fromStore->GetStrError().c_str());
276         dlclose(src_lh);
277         dlclose(dst_lh);
278         delete settings;
279         return -1;
280         }
281     if (toStore->SaveService(sc))
282         {
283         printfd(__FILE__, "Error saving service's data: %s\n", toStore->GetStrError().c_str());
284         dlclose(src_lh);
285         dlclose(dst_lh);
286         delete settings;
287         return -1;
288         }
289     }
290
291 printfd(__FILE__, "Importing corporations:\n");
292 entities.erase(entities.begin(), entities.end());
293 ready.erase(ready.begin(), ready.end());
294 if (fromStore->GetCorpsList(&entities))
295     {
296     printfd(__FILE__, "Error getting corporations list: %s\n", fromStore->GetStrError().c_str());
297     dlclose(src_lh);
298     dlclose(dst_lh);
299     delete settings;
300     return -1;
301     }
302 if (toStore->GetCorpsList(&ready))
303     {
304     printfd(__FILE__, "Error getting corporations list: %s\n", toStore->GetStrError().c_str());
305     dlclose(src_lh);
306     dlclose(dst_lh);
307     delete settings;
308     return -1;
309     }
310 for (it = entities.begin(); it != entities.end(); ++it)
311     {
312     printfd(__FILE__, "\t - %s\n", it->c_str());
313     if (find(ready.begin(), ready.end(), *it) == ready.end())
314         if (toStore->AddCorp(*it))
315             {
316             printfd(__FILE__, "Error adding corporation: %s\n", toStore->GetStrError().c_str());
317             dlclose(src_lh);
318             dlclose(dst_lh);
319             delete settings;
320             return -1;
321             }
322     if (fromStore->RestoreCorp(&cc, *it))
323         {
324         printfd(__FILE__, "Error getting corporation's data: %s\n", fromStore->GetStrError().c_str());
325         dlclose(src_lh);
326         dlclose(dst_lh);
327         delete settings;
328         return -1;
329         }
330     if (toStore->SaveCorp(cc))
331         {
332         printfd(__FILE__, "Error saving corporation's data: %s\n", toStore->GetStrError().c_str());
333         dlclose(src_lh);
334         dlclose(dst_lh);
335         delete settings;
336         return -1;
337         }
338     }
339
340 printfd(__FILE__, "Importing users:\n");
341 entities.erase(entities.begin(), entities.end());
342 ready.erase(ready.begin(), ready.end());
343 if (fromStore->GetUsersList(&entities))
344     {
345     printfd(__FILE__, "Error getting users list: %s\n", fromStore->GetStrError().c_str());
346     dlclose(src_lh);
347     dlclose(dst_lh);
348     delete settings;
349     return -1;
350     }
351 if (toStore->GetUsersList(&ready))
352     {
353     printfd(__FILE__, "Error getting users list: %s\n", toStore->GetStrError().c_str());
354     dlclose(src_lh);
355     dlclose(dst_lh);
356     delete settings;
357     return -1;
358     }
359 sort(ready.begin(), ready.end());
360 for (it = entities.begin(); it != entities.end(); ++it)
361     {
362     printfd(__FILE__, "\t - %s\n", it->c_str());
363     if (!binary_search(ready.begin(), ready.end(), *it)) {
364         if (toStore->AddUser(*it))
365             {
366             printfd(__FILE__, "Error adding user: %s\n", toStore->GetStrError().c_str());
367             dlclose(src_lh);
368             dlclose(dst_lh);
369             delete settings;
370             return -1;
371             }
372     } else {
373         printfd(__FILE__, "\t\t(adding passed)\n");
374     }
375     if (fromStore->RestoreUserConf(&uc, *it))
376         {
377         printfd(__FILE__, "Error getting user's conf: %s\n", fromStore->GetStrError().c_str());
378         dlclose(src_lh);
379         dlclose(dst_lh);
380         delete settings;
381         return -1;
382         }
383     if (fromStore->RestoreUserStat(&us, *it))
384         {
385         printfd(__FILE__, "Error getting user's stat: %s\n", fromStore->GetStrError().c_str());
386         dlclose(src_lh);
387         dlclose(dst_lh);
388         delete settings;
389         return -1;
390         }
391     if (toStore->SaveUserConf(uc, *it))
392         {
393         printfd(__FILE__, "Error saving user's conf: %s\n", toStore->GetStrError().c_str());
394         dlclose(src_lh);
395         dlclose(dst_lh);
396         delete settings;
397         return -1;
398         }
399     if (toStore->SaveUserStat(us, *it))
400         {
401         printfd(__FILE__, "Error saving user's stat: %s\n", toStore->GetStrError().c_str());
402         dlclose(src_lh);
403         dlclose(dst_lh);
404         delete settings;
405         return -1;
406         }
407     hdrs.erase(hdrs.begin(), hdrs.end());
408     if (fromStore->GetMessageHdrs(&hdrs, *it))
409         {
410         printfd(__FILE__, "Error getting user's messages: %s\n", fromStore->GetStrError().c_str());
411         dlclose(src_lh);
412         dlclose(dst_lh);
413         delete settings;
414         return -1;
415         }
416     for (mit = hdrs.begin(); mit != hdrs.end(); ++mit)
417         {
418         if (fromStore->GetMessage(mit->id, &msg, *it))
419             {
420             printfd(__FILE__, "Error getting message for a user: %s\n", fromStore->GetStrError().c_str());
421             dlclose(src_lh);
422             dlclose(dst_lh);
423             delete settings;
424             return -1;
425             }
426         printfd(__FILE__, "\t\t * %s\n", msg.text.c_str());
427         if (toStore->AddMessage(&msg, *it))
428             {
429             printfd(__FILE__, "Error adding message to a user: %s\n", toStore->GetStrError().c_str());
430             dlclose(src_lh);
431             dlclose(dst_lh);
432             delete settings;
433             return -1;
434             }
435         }
436
437     }
438
439 dlclose(src_lh);
440 dlclose(dst_lh);
441 printfd(__FILE__, "Done\n");
442 delete settings;
443 return 0;
444 }