|
|
@@ -5,6 +5,7 @@
|
|
|
#include "time.h"
|
|
|
#include "paras.h"
|
|
|
#include "cfg.h"
|
|
|
+#include "smtp.h"
|
|
|
#include "switch_ctrl.h"
|
|
|
|
|
|
#define TOTAL_COUNT 60//统计总数
|
|
|
@@ -2230,6 +2231,9 @@ int dev_insert_alarm_info(sqlite3* db,int product_id,int alarm_type,char* date_t
|
|
|
return -1 ;
|
|
|
}
|
|
|
sqlite3_free_table(pResult);
|
|
|
+
|
|
|
+ //smtp_send();
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
@@ -3505,7 +3509,7 @@ static int sw_update_port(sqlite3 *db, port_info_t *pt)
|
|
|
char *err_msg=NULL;
|
|
|
char *table=SWITCH_TABLE;
|
|
|
|
|
|
- sprintf(temp, "UPDATE %s SET up=%d,ip='%s',mac='%s',desc='%s' WHERE id=%d;",
|
|
|
+ sprintf(temp, "UPDATE %s SET up=%d,ip='%s',mac='%s',desc='%s' WHERE id=%d;",
|
|
|
table,pt->up,pt->ip,pt->mac,pt->desc,pt->id);
|
|
|
r = sqlite3_exec(db, temp, NULL, 0, &err_msg);
|
|
|
if(r != SQLITE_OK) {
|
|
|
@@ -3616,7 +3620,7 @@ int dev_smtp_init(sqlite3 *db, smtp_sett_t *sett)
|
|
|
table = SMTP_MANAGER_TABLE;
|
|
|
if(!tab_exists(db, table)) {
|
|
|
sprintf(temp, "CREATE TABLE IF NOT EXISTS %s("
|
|
|
- "product_id INTEGER,"
|
|
|
+ "product_id INTEGER PRIMARY KEY,"
|
|
|
"mode INTEGER,"
|
|
|
"account TEXT,"
|
|
|
"password TEXT,"
|
|
|
@@ -3638,22 +3642,24 @@ int dev_smtp_init(sqlite3 *db, smtp_sett_t *sett)
|
|
|
return -1 ;
|
|
|
}
|
|
|
|
|
|
- smtp_manager_t *man=&sett->man;
|
|
|
- memset(&man, 0, sizeof(smtp_manager_t));
|
|
|
- man->product_id = atoi(pResult[ncol+0]);
|
|
|
- man->mode = atoi(pResult[ncol+1]);
|
|
|
- strcpy(man->account, pResult[ncol+2]);
|
|
|
- strcpy(man->password, pResult[ncol+3]);
|
|
|
- strcpy(man->server, pResult[ncol+4]);
|
|
|
- strcpy(man->port, pResult[ncol+5]);
|
|
|
- strcpy(man->auth, pResult[ncol+6]);
|
|
|
+ if(ncol>0) {
|
|
|
+ smtp_manager_t *man=&sett->man;
|
|
|
+ memset(man, 0, sizeof(smtp_manager_t));
|
|
|
+ man->product_id = atoi(pResult[ncol+0]);
|
|
|
+ man->mode = atoi(pResult[ncol+1]);
|
|
|
+ strcpy(man->account, pResult[ncol+2]);
|
|
|
+ strcpy(man->password, pResult[ncol+3]);
|
|
|
+ strcpy(man->server, pResult[ncol+4]);
|
|
|
+ strcpy(man->port, pResult[ncol+5]);
|
|
|
+ strcpy(man->auth, pResult[ncol+6]);
|
|
|
+ }
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
table = SMTP_RECVEIVE_TABLE;
|
|
|
if(!tab_exists(db, table)) {
|
|
|
sprintf(temp, "CREATE TABLE IF NOT EXISTS %s("
|
|
|
+ "idx INTEGER PRIMARY KEY,"
|
|
|
"use INTEGER,"
|
|
|
- "idx INTEGER,"
|
|
|
"account TEXT);", table);
|
|
|
r = sqlite3_exec(db, temp, 0, 0, &err_msg);
|
|
|
if(r!=SQLITE_OK) {
|
|
|
@@ -3672,54 +3678,134 @@ int dev_smtp_init(sqlite3 *db, smtp_sett_t *sett)
|
|
|
|
|
|
smtp_recvs_t *rcv=&sett->rcv;
|
|
|
memset(rcv, 0, sizeof(smtp_recvs_t));
|
|
|
- for(i=1; i<=nrow; i++) {
|
|
|
- rcv->recv[i-1].use = atoi(pResult[i*ncol+0]);
|
|
|
- rcv->recv[i-1].idx = i-1;
|
|
|
- strcpy(rcv->recv[i-1].account, pResult[i*ncol+1]);
|
|
|
+ if(ncol>0) {
|
|
|
+ for(i=1; i<=nrow; i++) {
|
|
|
+ rcv->recv[i-1].idx = i-1;
|
|
|
+ rcv->recv[i-1].use = atoi(pResult[i*ncol+0]);
|
|
|
+ strcpy(rcv->recv[i-1].account, pResult[i*ncol+1]);
|
|
|
+ }
|
|
|
}
|
|
|
sqlite3_free_table(pResult);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-int dev_smtp_update_mang(sqlite3 *db, smtp_manager_t *man)
|
|
|
+static int get_count(sqlite3 *db, char *table)
|
|
|
{
|
|
|
- int i,r=0;
|
|
|
+ int r;
|
|
|
+ char temp[1024];
|
|
|
+ sqlite3_stmt *stmt;
|
|
|
+
|
|
|
+ sprintf(temp, "SELECT * FROM %s", table);
|
|
|
+ r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
|
|
|
+ if (r != SQLITE_OK) {
|
|
|
+ log_e("Failed to prepare statement: %s\n", sqlite3_errmsg(db));
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ int count = 0;
|
|
|
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+
|
|
|
+ return count;
|
|
|
+}
|
|
|
+int dev_smtp_update_man(sqlite3 *db, smtp_manager_t *man)
|
|
|
+{
|
|
|
+ int i,r=0,exist=0,cnt=0;
|
|
|
char temp[1024];
|
|
|
sqlite3_stmt *stmt;
|
|
|
char *err_msg=NULL;
|
|
|
char *table=SMTP_MANAGER_TABLE;
|
|
|
|
|
|
- sprintf(temp, "UPDATE %s SET product_id=%d, mode='%d', account='%s', password='%s', server='%s', port=%s, auth='%s';",
|
|
|
- table, man->product_id, man->mode, man->account, man->password, man->server, man->port, man->auth);
|
|
|
- r = sqlite3_exec(db, temp, NULL, 0, &err_msg);
|
|
|
- if(r != SQLITE_OK) {
|
|
|
- log_e("___sw_update_port failed, %s, %s\n\n", temp, err_msg);
|
|
|
+ cnt = get_count(db, table);
|
|
|
+ if(cnt>0) {
|
|
|
+ sprintf(temp, "UPDATE %s SET product_id=%d, mode=%d, account='%s', password='%s', server='%s', port='%s', auth='%s' WHERE product_id=%d;",
|
|
|
+ table, man->product_id, man->mode, man->account, man->password, man->server, man->port, man->auth, man->product_id);
|
|
|
+ r = sqlite3_exec(db, temp, NULL, 0, &err_msg);
|
|
|
+ if(r != SQLITE_OK) {
|
|
|
+ log_e("___sw_update_port failed, %s, %s\n\n", temp, err_msg);
|
|
|
+ sqlite3_free(err_msg);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ exist = sqlite3_changes(db);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- sqlite3_free(err_msg);
|
|
|
- return -1;
|
|
|
+ if(exist==0) {
|
|
|
+ sprintf(temp, "INSERT INTO %s (product_id,mode,account,password,server,port,auth) VALUES (?,?,?,?,?,?,?);", table);
|
|
|
+ r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
|
|
|
+ if (r != SQLITE_OK) {
|
|
|
+ log_e("___dev_smtp_update_man, sqlite3_prepare_v2 failed, %s\n", sqlite3_errmsg(db));
|
|
|
+ r = -1; goto quit;
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlite3_bind_int(stmt, 1, man->product_id);
|
|
|
+ sqlite3_bind_int(stmt, 2, man->mode);
|
|
|
+ sqlite3_bind_text(stmt, 3, man->account, -1, NULL);
|
|
|
+ sqlite3_bind_text(stmt, 4, man->password, -1, NULL);
|
|
|
+ sqlite3_bind_text(stmt, 5, man->server, -1, NULL);
|
|
|
+ sqlite3_bind_text(stmt, 6, man->port, -1, NULL);
|
|
|
+ sqlite3_bind_text(stmt, 7, man->auth, -1, NULL);
|
|
|
+
|
|
|
+ r = sqlite3_step(stmt);
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+
|
|
|
+ if (r != SQLITE_DONE) {
|
|
|
+ log_e("___sw_update_all, sqlite3_step failed\n");
|
|
|
+ r = -1;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- return 0;
|
|
|
+quit:
|
|
|
+ return r;
|
|
|
}
|
|
|
|
|
|
-int dev_smtp_update_recv(sqlite3 *db, smtp_recv_t *rcv)
|
|
|
+int dev_smtp_update_recv(sqlite3 *db, smtp_recv_t *recv)
|
|
|
{
|
|
|
- int i,r=0;
|
|
|
+ int i,r=0,exist=0,cnt=0;
|
|
|
char temp[1024];
|
|
|
sqlite3_stmt *stmt;
|
|
|
char *err_msg=NULL;
|
|
|
char *table=SMTP_RECVEIVE_TABLE;
|
|
|
+
|
|
|
+ cnt = get_count(db, table);
|
|
|
+ if(cnt>0) {
|
|
|
+ sprintf(temp, "UPDATE %s SET idx=%d, use=%d, account='%s' WHERE idx=%d;",
|
|
|
+ table, recv->idx, recv->use, recv->account, recv->idx);
|
|
|
+ r = sqlite3_exec(db, temp, NULL, 0, &err_msg);
|
|
|
+ if(r != SQLITE_OK) {
|
|
|
+ log_e("___sw_update_port failed, %s, %s\n\n", temp, err_msg);
|
|
|
+ sqlite3_free(err_msg);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ exist = sqlite3_changes(db);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- sprintf(temp, "UPDATE %s SET use=%d, idx=%d, account='%s' WHERE idx=%d;",
|
|
|
- table, rcv->use, rcv->idx, rcv->account, rcv->idx);
|
|
|
- r = sqlite3_exec(db, temp, NULL, 0, &err_msg);
|
|
|
- if(r != SQLITE_OK) {
|
|
|
- log_e("___sw_update_port failed, %s, %s\n\n", temp, err_msg);
|
|
|
+ if(exist==0) {
|
|
|
+ sprintf(temp, "INSERT INTO %s (idx,use,account) VALUES (?,?,?);", table);
|
|
|
+ r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
|
|
|
+ if (r != SQLITE_OK) {
|
|
|
+ log_e("___dev_smtp_update_recv, sqlite3_prepare_v2 failed, %s\n", sqlite3_errmsg(db));
|
|
|
+ r = -1;
|
|
|
+ goto quit;
|
|
|
+ }
|
|
|
|
|
|
- sqlite3_free(err_msg);
|
|
|
- return -1;
|
|
|
+ sqlite3_bind_int(stmt, 1, recv->idx);
|
|
|
+ sqlite3_bind_int(stmt, 2, recv->use);
|
|
|
+ sqlite3_bind_text(stmt, 3, recv->account, -1, NULL);
|
|
|
+
|
|
|
+ r = sqlite3_step(stmt);
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+
|
|
|
+ if (r != SQLITE_DONE) {
|
|
|
+ log_e("___sw_update_all, sqlite3_step failed\n");
|
|
|
+ r = -1;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- return 0;
|
|
|
+quit:
|
|
|
+ return r;
|
|
|
}
|