]> sjero.net Git - linphone/blob - coreapi/message_storage.c
add message storage
[linphone] / coreapi / message_storage.c
1 /*
2 message_storage.c
3 Copyright (C) 2012  Belledonne Communications, Grenoble, France
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20 #include "private.h"
21 #include "linphonecore.h"
22
23 #ifdef WIN32
24
25 static inline char *my_ctime_r(const time_t *t, char *buf){
26         strcpy(buf,ctime(t));
27         return buf;
28 }
29
30 #else
31 #define my_ctime_r ctime_r
32 #endif
33
34 #ifdef MSG_STORAGE_ENABLED
35
36 #include "sqlite3.h"
37
38 static const char *days[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};\r
39 static const char *months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
40
41 #define CONFIG_FILE ".linphone-history.db"
42
43 char *linphone_message_storage_get_config_file(const char *filename){
44         const int path_max=1024;
45         char *config_file=(char *)malloc(path_max*sizeof(char));
46         if (filename==NULL) filename=CONFIG_FILE;
47         /*try accessing a local file first if exists*/
48         if (access(CONFIG_FILE,F_OK)==0){
49                 snprintf(config_file,path_max,"%s",filename);
50         }else{
51 #ifdef WIN32
52                 const char *appdata=getenv("APPDATA");
53                 if (appdata){
54                         snprintf(config_file,path_max,"%s\\%s",appdata,LINPHONE_CONFIG_DIR);
55                         CreateDirectory(config_file,NULL);
56                         snprintf(config_file,path_max,"%s\\%s\\%s",appdata,LINPHONE_CONFIG_DIR,filename);
57                 }
58 #else
59                 const char *home=getenv("HOME");
60                 if (home==NULL) home=".";
61                 snprintf(config_file,path_max,"%s/%s",home,filename);
62 #endif
63         }
64         return config_file;
65 }
66
67 void create_chat_message(char **argv, void *data){
68         LinphoneChatRoom *cr = (LinphoneChatRoom *)data;
69         LinphoneChatMessage* new_message = linphone_chat_room_create_message(cr,argv[4]);
70         struct tm ret={0};\r
71         char tmp1[80]={0};\r
72         char tmp2[80]={0};
73         
74         if(atoi(argv[3])==INCOMING){
75                 linphone_chat_message_set_from(new_message,linphone_address_new(argv[2]));
76         } else {
77                 linphone_chat_message_set_from(new_message,linphone_address_new(argv[1]));
78         }
79
80         if(argv[5]!=NULL){\r
81                 int i,j;\r
82                 sscanf(argv[5],"%3c %3c%d%d:%d:%d %d",tmp1,tmp2,&ret.tm_mday,\r
83                      &ret.tm_hour,&ret.tm_min,&ret.tm_sec,&ret.tm_year);\r
84                 ret.tm_year-=1900;\r
85                 for(i=0;i<7;i++) { \r
86                         if(strcmp(tmp1,days[i])==0) ret.tm_wday=i; \r
87                 }\r
88                 for(j=0;j<12;j++) { \r
89                         if(strcmp(tmp2,months[j])==0) ret.tm_mon=j; \r
90                 }
91         }
92         new_message->time=argv[5]!=NULL ? mktime(&ret) : time(NULL);
93         new_message->state=atoi(argv[7]);
94         cr->messages_hist=ms_list_prepend(cr->messages_hist,(void *)new_message);
95 }
96
97 static int callback(void *data, int argc, char **argv, char **colName){
98     create_chat_message(argv,data);
99     return 0;
100 }
101
102 void linphone_sql_request_message(sqlite3 *db,const char *stmt,void *data){
103         char* errmsg;
104         int ret;
105         ret=sqlite3_exec(db,stmt,callback,data,&errmsg);
106         if(ret != SQLITE_OK) {
107                 printf("Error in creation: %s.\n", errmsg);
108         }
109 }
110
111 void linphone_sql_request(sqlite3* db,const char *stmt){
112         char* errmsg;
113         int ret;
114         ret=sqlite3_exec(db,stmt,0,0,&errmsg);
115         if(ret != SQLITE_OK) {
116                 printf("Error in creation: %s.\n", errmsg);
117         }
118 }
119
120 void linphone_core_set_history_message(LinphoneChatRoom *cr,const char *local_contact,const char *remote_contact, 
121                      int direction, const char *message,const char *date, int read, int state){
122         LinphoneCore *lc=linphone_chat_room_get_lc(cr);
123         char *buf=sqlite3_mprintf("insert into history values(NULL,%Q,%Q,%i,%Q,%Q,%i,%i);",
124                                         local_contact,remote_contact,direction,message,date,read,state);
125         linphone_sql_request(lc->db,buf);
126 }
127
128 void linphone_core_set_message_state(LinphoneChatRoom *cr,const char *message, int state, time_t date){
129         LinphoneCore *lc=linphone_chat_room_get_lc(cr);
130         char time_str[26];
131         char *buf=sqlite3_mprintf("update history set status=%i where message = %Q and time = %Q;",
132                        state,message,my_ctime_r(&date,time_str));
133         linphone_sql_request(lc->db,buf);
134 }
135
136 void linphone_core_set_messages_flag_read(LinphoneChatRoom *cr,const char *from, int read){
137         LinphoneCore *lc=linphone_chat_room_get_lc(cr);
138         char *buf=sqlite3_mprintf("update history set read=%i where remoteContact = %Q;",
139                        read,from);
140         linphone_sql_request(lc->db,buf);
141 }
142
143 MSList *linphone_chat_room_get_history(const char *to,LinphoneChatRoom *cr,int nb_message){
144         LinphoneCore *lc=linphone_chat_room_get_lc(cr);
145         cr->messages_hist = NULL;
146         char *buf=sqlite3_mprintf("select * from history where remoteContact = %Q order by id DESC limit %i ;",to,nb_message);
147         linphone_sql_request_message(lc->db,buf,(void *)cr);
148         return cr->messages_hist;
149 }
150
151 void linphone_close_storage(sqlite3* db){
152         sqlite3_close(db);
153 }
154
155 void linphone_create_table(sqlite3* db){
156         char* errmsg;
157         int ret;
158         ret=sqlite3_exec(db,"CREATE TABLE if not exists history (id INTEGER PRIMARY KEY AUTOINCREMENT, localContact TEXT NOT NULL, remoteContact TEXT NOT NULL, direction INTEGER, message TEXT, time TEXT NOT NULL, read INTEGER, status INTEGER);",
159                 0,0,&errmsg);
160         if(ret != SQLITE_OK) {
161                 printf("Error in creation: %s.\n", errmsg);
162         }
163 }
164
165 sqlite3 * linphone_message_storage_init(){
166         int ret;
167         char *errmsg;
168         sqlite3 *db;
169         char *filename;
170         filename=linphone_message_storage_get_config_file(NULL);
171         ret=sqlite3_open(filename,&db);
172         if(ret != SQLITE_OK) {
173                 printf("Error in the opening: %s.\n", errmsg);
174                 sqlite3_close(db);
175         }
176         linphone_create_table(db);
177         return db;
178 }
179 #else 
180
181 void linphone_core_set_history_message(LinphoneChatRoom *cr,const char *local_contact,const char *remote_contact, 
182                      int direction, const char *message,const char *date, int read, int state){
183 }
184
185 void linphone_core_set_message_state(LinphoneChatRoom *cr,const char *message, int state, time_t date){
186 }
187
188 void linphone_core_set_messages_flag_read(LinphoneChatRoom *cr,const char *from, int read){
189 }
190
191 MSList *linphone_chat_room_get_history(const char *to,LinphoneChatRoom *cr,int nb_message){
192         return NULL;
193 }
194 #endif