aboutsummaryrefslogtreecommitdiff
path: root/sms.c
diff options
context:
space:
mode:
authorache <ache@ache.one>2017-11-17 11:21:12 +0100
committerache <ache@ache.one>2017-11-17 11:21:12 +0100
commit20a87184b1c06f63b2b3d8b9c48a68535b31b4bb (patch)
treee3ad5a41ac00d71ef95bd58fd1c4d4bf3eaa07a7 /sms.c
parentNumber to contact option (diff)
New mesms
Diffstat (limited to 'sms.c')
-rw-r--r--sms.c230
1 files changed, 230 insertions, 0 deletions
diff --git a/sms.c b/sms.c
new file mode 100644
index 0000000..05e8c91
--- /dev/null
+++ b/sms.c
@@ -0,0 +1,230 @@
+#include "sms.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+
+
+
+conversation* listConv;
+size_t nbConv;
+
+int insertSMS(sms* S) {
+ int index = -1;
+
+
+ for(int i = 0 ; i < nbConv ; i++) {
+ if( !strcmp(listConv[i].number, S->number) ) {
+ index = i;
+ break;
+ }
+ }
+
+ if( index >= 0 ) {
+ sms* tmp = realloc(listConv[index].listSMS, (listConv[index].nbSMS+1) * sizeof *tmp);
+ if( tmp ) {
+ listConv[index].listSMS = tmp;
+ } else {
+ fprintf(stderr, "Erreur d'allocation");
+ return 1;
+ }
+ int indexSMS = -1;
+
+ for(int i = 0 ; i < listConv[index].nbSMS; i++) {
+ if( listConv[index].listSMS[i].outDate > S->outDate ) {
+ indexSMS = i;
+ break;
+ }
+ }
+ if( indexSMS >= 0 ) {
+ memmove(indexSMS+1+listConv[index].listSMS,
+ indexSMS+0+listConv[index].listSMS,
+ (listConv[index].nbSMS-indexSMS)* sizeof listConv[index].listSMS[indexSMS]);
+ memcpy(&listConv[index].listSMS[indexSMS],S, sizeof(sms));
+ } else {
+ memcpy(&listConv[index].listSMS[listConv[index].nbSMS],S, sizeof(sms));
+ }
+ listConv[index].nbSMS++;
+ } else {
+ conversation* tmp = realloc(listConv, (nbConv+1) * sizeof *tmp);
+ if( tmp ) {
+ listConv = tmp;
+ listConv[nbConv].listSMS = realloc(NULL, sizeof(sms) );
+ memcpy(listConv[nbConv].listSMS, S, sizeof(sms));
+ listConv[nbConv].nbSMS = 1;
+ strcpy( listConv[nbConv].number, S->number);
+
+ nbConv++;
+ } else {
+ fprintf(stderr, "Erreur d'allocation");
+ return 2;
+ }
+ }
+ return 0;
+}
+
+void showSMS(sms* S) {
+ if( S->in_out == SMS_IN )
+ printf("From %s @%s{%s}\n", S->number, ctime(&S->outDate), S->text);
+ else
+ printf("To %s @%s{%s}\n", S->number, ctime(&S->outDate), S->text);
+}
+void showConv(const char* number) {
+
+ for(int i = 0 ; i < nbConv ; i++) {
+ if( !strcmp( listConv[i].number, number) ) {
+ for(int j = 0 ; j < listConv[i].nbSMS ; j++) {
+ showSMS(listConv[i].listSMS + j);
+ }
+ break;
+ }
+ }
+}
+
+int readSMS(char* filename, int inbox_outbox) {
+ FILE* file = fopen(filename,"r");
+
+ if( file ) {
+ char line[1024] = "";
+ int sms_len = 0;
+ char* sms_text = malloc(1);
+ char lastChar = 0;
+ int ok = 0;
+ int sms_number = 0;
+ sms getSms = {.in_out = inbox_outbox};
+
+ *sms_text= 0;
+
+ loop:
+ // On cherche [SMSBackup]
+ while( fgets(line, 1024, file) != NULL) {
+
+ if( strstr(line, "[SMSBackup") ) {
+ char SMSBackup[30] = "";
+ sprintf(SMSBackup, "[SMSBackup%.03d]\n", sms_number);
+ if( !strcmp(line, SMSBackup) ) {
+ sms_number++;
+ break;
+ }
+ }
+ }
+
+ // Donc maintenant, tant que ça commence par "; ", c'est du SMS
+ //
+ while( fgets(line, 1024, file) != NULL) {
+ if(line[0] == ';' && line[1] == ' ') {
+ sms_len += strlen(line) - 2;
+ char* tmp = realloc(sms_text, sms_len+1);
+ if( tmp ) {
+ sms_text = tmp;
+ strcat(sms_text, line+2);
+ }
+ ok = 1;
+ }else break;
+ }
+ if( ok ) {
+ lastChar = sms_text[sms_len-1];
+ sms_text[--sms_len] = 0;
+ ok = 0;
+ }
+
+ // Ici, line contient une ligne qui n'est pas un SMS.
+ // Il faut parcer jusqu'à ce qu'on aie une ligne vide
+ while( fgets(line, 1024, file) != NULL && line[0] && line[0] != '\n' ) {
+
+ if(line[0] == ';') { // Si c'est un commentaire, on s'en fou
+ continue;
+ } else if( strstr(line, "DateTime = ") ) {
+ struct tm time_tm = {};
+ if( strptime(line+11, "%Y%m%jT%H%M%S", &time_tm) ) {
+ getSms.outDate = mktime(&time_tm);
+ }else{
+ puts("Error");
+ }
+
+ } else if( strstr(line, "State = ") ) {
+ if( line[9] == 'U' )
+ getSms.state = SMS_UNREAD;
+ } else if( strstr(line, "Class = ") ) {
+ if( line[9] == '0' )
+ getSms.type = FLASH_SMS;
+ } else if( strstr(line, "Number = ") ) {
+ strcpy(getSms.number, line+10);
+ getSms.number[strlen(getSms.number)-3]=0;
+ }
+ }
+ if( !feof(file) )
+ goto loop;
+ sms_text[++sms_len] = lastChar;
+ getSms.text = sms_text;
+ getSms.len = sms_len;
+
+ int r = insertSMS(&getSms);
+ if( r )
+ return 2;
+
+ fclose(file);
+ }else{
+ fprintf(stderr, "Erreur à l'ouverture du fichier");
+ return 1;
+ }
+ return 0;
+}
+int listSMS(char* smsInboxDir, int inorout) {
+ DIR *dirHandler;
+ struct dirent *dir;
+
+ dirHandler = opendir( smsInboxDir );
+ if (dirHandler) {
+ while ( (dir = readdir(dirHandler)) != NULL ) {
+ if( dir->d_type == DT_REG ) {
+ char filename[1024] ="";
+
+ sprintf(filename, "%s/%s", smsInboxDir, dir->d_name);
+ int r =
+ readSMS(filename, inorout);
+ if( r )
+ return 2;
+
+ }
+ }
+ closedir(dirHandler);
+ } else {
+ fprintf(stderr, "Directory can't be opened");
+ return 1;
+ }
+ return 0;
+}
+
+
+
+
+int loadConv(void) {
+
+ int r =
+
+ listSMS("/var/spool/sms/inbox/", SMS_IN);
+ if( r )
+ return 1;
+ r =
+ listSMS("/var/spool/sms/sent/" , SMS_OUT);
+ if( r )
+ return 2;
+
+ for(int i = 0 ; i < nbConv ; i++) {
+ contact* cntct = NULL;
+ if( findContact(listConv[i].number,&cntct) == 0 ) {
+ cntct->conversation = &listConv[i];
+ } else {
+ contact c = {};
+ strcpy(c.display_name,listConv[i].number);
+ strcpy(c.name,listConv[i].number);
+ strcpy(c.number,listConv[i].number);
+ c.conversation = &listConv[i];
+ addContact(&c);
+ }
+ }
+
+
+ return 0;
+}
+