#include "sms.h" #include #include conversation* listConv; size_t nbConv; extern config_t config; 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, char state) { 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; if( state == SMS_UNREAD && getSms.in_out == SMS_IN ) { getSms.state = state; } int r = insertSMS(&getSms); if( r ) { fclose(file); 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, SMS_READ); if( r ) { return 2; } } } closedir(dirHandler); } else { fprintf(stderr, "Directory can't be opened: "); return 1; } return 0; } int loadConv(void) { int r = 0; if( *config.SMS_INBOX && (r = listSMS(config.SMS_INBOX, SMS_IN)) ) { fprintf(stderr, "Impossible de lire les SMS reçu\n"); return 1; } ; if( *config.SMS_SENTBOX && (r = listSMS(config.SMS_SENTBOX , SMS_OUT)) ) { fprintf(stderr, "Impossible de lire les SMS envoyés\n"); 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; }