aboutsummaryrefslogtreecommitdiff
path: root/sms.c
blob: b7f49cbfccafe261e70ec312f3dfa95ed4e71aae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "sms.h"
#include <stdio.h>
#include <stdlib.h>

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;
}