📄 fxdb.cpp
字号:
/*************************************************************************** * Copyright (C) 2008 by DDD * * dedodong@163.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/#include "fxdb.h"#include "fxEncrypt.h"//#include "appconfig.h"#include <QTextStream>#include <QFile>#define AUTOLG "autologin021"#define SERVERADD "cacheserveradd"#define MUTE "mute"#define AUTOSHOWMSG "autoshowmsg"#define PROXY "proxy"#define CATSMS "catsms"#define ACINFO "acinfo"#define SQL_MAXLEN 1024*3#define MAX_PATH 512#ifdef WIN32#else#include <sys/types.h>#include <pwd.h>#endif/******************************** * * table AUTOLG * * flag * 1 auto login * 0 no * id * the fetion id * len_id * the len of fetion's id * pwd * the password * len_pwd * the len of the password * * login_state * login state..(add by iptton) * * * table SERVERADD * * id * the id of fetion * proxy * the proxy of the fetion server * * * table fx****usr * uid id of opter * msg message connect , it is base64, and XOR * msg_len msg 's len * date_time date of message * * * table PROXY * type * host * port * name * pwd * * table ACINFO * id * the id of fetion * mobilenm * the mobile number of * local_name * the account's local_name * nickname * the account's nickname * impresa * the account's impresa * showname * the account's showname * ******************************/sqlite3 * pdb = NULL; //global share sql variablechar sql[SQL_MAXLEN] = {0};bool init_db(){ char **result; char *perrmsg; int nrow, ncol; if (pdb) return true; char* DBFILEPATH = (char*) malloc(sizeof(char)*(MAX_PATH+1));#ifdef WIN32#else struct passwd *pwd; if ((pwd = getpwuid (geteuid ())) != NULL) { strcpy (DBFILEPATH, pwd->pw_dir); if (DBFILEPATH[ strlen(DBFILEPATH) - 1] != '/') strcat (DBFILEPATH, "/"); strcat (DBFILEPATH, "."); }#endif strcat (DBFILEPATH, DBNAME); //open db if (sqlite3_open(DBFILEPATH, &pdb )!= SQLITE_OK ) { pdb = NULL; return false ; } delete DBFILEPATH; // Are there DBTABLE ? if not, create it. memset(sql, 0, SQL_MAXLEN); sprintf(sql,"select * from %s limit 1", AUTOLG); if( sqlite3_get_table(pdb, sql, &result, &nrow, &ncol, &perrmsg) != SQLITE_OK) { //create the table memset(sql, 0, SQL_MAXLEN); sprintf(sql, "create table %s(flag, id, len_id, pwd, len_pwd, login_state)", AUTOLG); sqlite3_exec(pdb, sql, 0, 0, &perrmsg); } sqlite3_free_table(result); // Are there DBTABLE ? if not, create it. memset(sql, 0, SQL_MAXLEN); sprintf(sql,"select * from %s limit 1", SERVERADD); if( sqlite3_get_table(pdb, sql, &result, &nrow, &ncol, &perrmsg) != SQLITE_OK) { //create the table memset(sql, 0, SQL_MAXLEN); sprintf(sql, "create table %s(id,proxy)", SERVERADD); sqlite3_exec(pdb, sql, 0, 0, &perrmsg); } sqlite3_free_table(result); // Are there PROXY ? if not, create it. memset(sql, 0, SQL_MAXLEN); sprintf(sql,"select * from %s limit 1", PROXY); if( sqlite3_get_table(pdb, sql, &result, &nrow, &ncol, &perrmsg) != SQLITE_OK) { //create the table memset(sql, 0, SQL_MAXLEN); sprintf(sql, "create table %s(type, host, port, name, pwd)", PROXY); sqlite3_exec(pdb, sql, 0, 0, &perrmsg); } sqlite3_free_table(result); return true;}void destoy_db(){ if(pdb) sqlite3_close(pdb); pdb = NULL;}bool isAutoLogin(char** id, char** pwd, int* loginState){ if (!init_db()) return false; char **result; char *perrmsg; int nrow, ncol; int ret; // select url from databse memset(sql, 0, SQL_MAXLEN); sprintf(sql,"select id, pwd, len_id, len_pwd, login_state from %s where flag=\"1\" ", AUTOLG); ret = sqlite3_get_table(pdb, sql, &result, &nrow, &ncol, &perrmsg); if(ret != SQLITE_OK) return false; bool res = false; if(nrow) { res = true; if (id && pwd) { *id = encryPWD(result[5], strlen(result[5]), false); *pwd = encryPWD(result[6], strlen(result[6]), false); int len_id = atoi(result[7]); int len_pwd = atoi(result[8]); (*id)[len_id] = '\0'; (*pwd)[len_pwd] = '\0'; if (loginState) { if (result[9]) *loginState = atoi (result[9]); else *loginState = 0; } } } sqlite3_free_table(result); //destoy_db(); return res; }void disAutoLogin(){ if (!init_db()) return ; // select url from databse memset(sql, 0, SQL_MAXLEN); sprintf(sql,"delete from %s", AUTOLG); //exec the delete table sql sqlite3_exec(pdb, sql, 0, 0, 0); //destoy_db();}bool setAutoLogin(const char* id, const char*pwd, int login_state){ if (!id || !pwd) return false; disAutoLogin(); char *en_id = encryPWD(id, strlen(id), true); char *en_pwd = encryPWD(pwd, strlen(pwd), true); if (!en_id || !en_pwd) return false; // insert data to databse memset(sql, 0, SQL_MAXLEN); sprintf(sql,"insert into %s values(\"1\", \"%s\", \"%d\", \"%s\", \"%d\", \"%d\")", AUTOLG, en_id, strlen(id), en_pwd, strlen(pwd), login_state); sqlite3_exec(pdb, sql, 0, 0, 0); if (en_id) free(en_id); if (en_pwd) free(en_pwd); //destoy_db(); return true;}char *getCacheServerAdd(const char* id){ if (!init_db()) return false; char* proxy = NULL; char **result; char *perrmsg; int nrow, ncol; int ret; // select url from databse memset(sql, 0, SQL_MAXLEN); sprintf(sql,"select proxy from %s where id=\"%s\" ", SERVERADD, id); ret = sqlite3_get_table(pdb, sql, &result, &nrow, &ncol, &perrmsg); if(ret != SQLITE_OK) return false; if(nrow) { proxy = (char*)malloc(strlen(result[1])+1); strcpy(proxy,result[1]); } sqlite3_free_table(result); return proxy;}void disProxy(){ if (!init_db()) return ; // select url from databse memset(sql, 0, SQL_MAXLEN); sprintf(sql,"delete from %s", PROXY); //exec the delete table sql sqlite3_exec(pdb, sql, 0, 0, 0); //destoy_db();}bool isSetProxy(PROXY_ITEM *proxy_item){ if (!init_db()) return false; char **result; char *perrmsg; int nrow, ncol; int ret; // select url from databse memset(sql, 0, SQL_MAXLEN); sprintf(sql,"select type, host, port, name, pwd from %s", PROXY); ret = sqlite3_get_table(pdb, sql, &result, &nrow, &ncol, &perrmsg); if(ret != SQLITE_OK) return false; bool res = false; if(nrow) { res = true; if (proxy_item) { proxy_item->host = strdup(result[6]); proxy_item->port = strdup(result[7]); //bad coding... fuck!!!! if (atoi(result[8]) == 10 && atoi(result[9]) == 10) { proxy_item->name = NULL; proxy_item->pwd = NULL; } else { proxy_item->name = strdup(result[8]); proxy_item->pwd = strdup(result[9]); } switch (atoi(result[5])) { case 0: proxy_item->type = PROXY_DIRECT; break; case 1: proxy_item->type = PROXY_HTTP; break; case 2: proxy_item->type = PROXY_SOCKS4; break; case 3: proxy_item->type = PROXY_SOCKS5; break; case 4: proxy_item->type = PROXY_MAX; break; } } } sqlite3_free_table(result); return res; }bool setProxy(PROXY_ITEM *proxy_item){ if (!proxy_item || !init_db()) return false; disProxy(); // insert data to databse memset(sql, 0, SQL_MAXLEN); sprintf(sql,"insert into %s values(\"%d\", \"%s\", \"%s\", \"%s\", \"%s\")", PROXY, proxy_item->type, proxy_item->host, proxy_item->port, proxy_item->name?proxy_item->name:"10", proxy_item->pwd?proxy_item->pwd:"10"); sqlite3_exec(pdb, sql, 0, 0, 0); //destoy_db(); return true;}bool setCacheServerAdd(const char* id, const char* prxy){ if (!init_db()) return false; // select url from databse memset(sql, 0, SQL_MAXLEN); sprintf(sql,"delete from %s where id =\"%s\"", SERVERADD, id); //exec the delete table sql sqlite3_exec(pdb, sql, 0, 0, 0); // insert data to databse memset(sql, 0, SQL_MAXLEN); sprintf(sql,"insert into %s values(\"%s\", \"%s\")", SERVERADD, id, prxy); sqlite3_exec(pdb, sql, 0, 0, 0); return true;}void cleanCacheServerAdd(){ if (!init_db()) return ; // select url from databse memset(sql, 0, SQL_MAXLEN); sprintf(sql,"delete from %s ", SERVERADD); //exec the delete table sql sqlite3_exec(pdb, sql, 0, 0, 0); }QString create_result(char **result, int nrow, int ncol);BOOL init_histroy_db(long usr){ static BOOL have_init = FALSE; if (!init_db()) return FALSE; if (have_init) return TRUE; char **result; char *perrmsg; int nrow, ncol; // Are there DBTABLE ? if not, create it. memset(sql, 0, SQL_MAXLEN); sprintf(sql,"select * from fx%ldusr limit 1", usr); if (sqlite3_get_table(pdb, sql, &result, &nrow, &ncol, &perrmsg) != SQLITE_OK) { //create the table memset(sql, 0, SQL_MAXLEN); sprintf(sql, "create table fx%ldusr(uid,msg text, msg_len, date_time NOT NULL DEFAULT CURRENT_TIMESTAMP)", usr); //exec the create table sql if( sqlite3_exec(pdb, sql, 0, 0, &perrmsg) != SQLITE_OK ) { printf("exel create table %s\n", sql); return FALSE; } sqlite3_free_table(result); //creat index memset(sql, 0, SQL_MAXLEN); sprintf(sql,"create index uid_index on fx%ldusr(uid)", usr); if (sqlite3_exec(pdb, sql, 0, 0, &perrmsg) != SQLITE_OK ) { printf("exel create index %s\n", sql); return FALSE; } else sqlite3_free_table(result); } have_init = TRUE; return TRUE;}void saveHistroyMsg(long usr, long uid, const char* msg, const char* date){ if (!init_histroy_db(usr)) { printf("init_histrou fial \n"); return; } if (!msg) return; int msg_len = strlen(msg); char * base64_msg = encryptXOR(msg, msg_len, KEY); if (!base64_msg) return; //memset(sql, 0, sql_maxlen);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -