~ubuntu-branches/ubuntu/hardy/migration-assistant/hardy-proposed

« back to all changes in this revision

Viewing changes to firefox-import.c

  • Committer: Bazaar Package Importer
  • Author(s): Evan Dandrea
  • Date: 2007-02-06 00:58:57 UTC
  • Revision ID: james.westby@ubuntu.com-20070206005857-1yxpcc2kvpwquso9
Tags: 0.3
* Added tests for empty passwords, reserved usernames, bad usernames and
  more.
* removed the administrator debconf option.
* fixed quite a few bugs in ma-ask's regular expressions, mounting of
  filesystems and ifs handling.
* Added bookmarks importing from Firefox and IE.
* Removed the last bit of code referencing the administrator, allowing
  installs to work again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* Copyright (C) 2007 Evan Dandrea <evand@ubuntu.com> under the GPLv2
 
3
*/
 
4
 
 
5
 /*
 
6
 * This file should cover importing into all Firefox versions up to and
 
7
 * including Firefox 2.  Firefox 3.0 will introduce a new backend (finally) for
 
8
 * bookmarks and history using SQLite.
 
9
 */
 
10
 
 
11
#include <stdlib.h>
1
12
#include <stdio.h>
2
 
#include <stdlib.h>
3
13
#include <string.h>
4
 
 
5
 
#include <ctype.h> // isspace
6
 
 
7
 
char* readtag(const char* line) {
8
 
    //printf("got:%s", line);
 
14
#include <stdbool.h>
 
15
#include <ctype.h>
 
16
 
 
17
#include <sys/types.h>
 
18
#include <dirent.h>
 
19
#include <sys/stat.h>
 
20
#include <unistd.h>
 
21
 
 
22
#include "utils.h"
 
23
 
 
24
typedef struct folder {
 
25
    char* title;
 
26
    char* description;
 
27
} folder;
 
28
 
 
29
typedef struct hlink {
 
30
    char* title;
 
31
    char* url;
 
32
    char* icon;
 
33
    char* feed;
 
34
    char* description;
 
35
} hlink;
 
36
 
 
37
typedef enum { HREF, ICON, FEED } attr_type;
 
38
typedef enum { FOLDER, LINK, HR } item_type;
 
39
 
 
40
typedef struct element {
 
41
    void* attributes;
 
42
    item_type type;
 
43
    struct element* next;
 
44
    struct element* children;
 
45
} element;
 
46
 
 
47
char* read_tag(const char* line) {
9
48
    char* ret;
10
49
    const char* ptr = line;
11
50
    int count = 0;
29
68
    return ret;
30
69
}
31
70
 
32
 
int countwhitespace(const char* string) {
 
71
char* getcontent(const char* string) {
 
72
    char* ret;
 
73
    const char* ptr = string;
33
74
    int count = 0;
34
 
    while(isspace(*string)) {
 
75
 
 
76
    while(*ptr != '\0')
 
77
        ptr++;
 
78
 
 
79
    // Find the </TAG> >
 
80
    while(*ptr != '>') {
 
81
        if(ptr == string) return NULL;
 
82
        ptr--;
 
83
    }
 
84
    ptr--;
 
85
    // Find the <TAG> >
 
86
    if(*ptr != 'D') {
 
87
        while(*ptr != '>') {
 
88
            if(ptr == string) return NULL;
 
89
            ptr--;
 
90
        }
 
91
    } else ptr++;
 
92
 
 
93
    string = ++ptr;
 
94
 
 
95
    // DD ends in a newline
 
96
    while(*ptr != '<' && *ptr != '\n') {
 
97
        if(*ptr == '\0') return NULL;
35
98
        count++;
36
 
        string++;
 
99
        ptr++;
37
100
    }
38
101
 
39
 
    return count;
 
102
    ret = malloc(count+1);
 
103
    memcpy(ret,string,count);
 
104
    ret[count] = '\0';
 
105
 
 
106
    return ret;
40
107
}
41
 
char* geturl(const char* string) {
42
 
    // extracts a URL from an <A> tag.
 
108
 
 
109
char* getattrib(const char* string, attr_type attr) {
43
110
    const char* href;
44
111
    const char* end;
45
112
    char* ret = NULL;
46
113
    int count = 0;
 
114
    const char* temp;
 
115
    if(attr == HREF)
 
116
        temp = "HREF=";
 
117
    else if(attr == ICON)
 
118
        temp = "ICON=";
 
119
    else if(attr == FEED)
 
120
        temp = "FEEDURL=";
47
121
 
48
 
    href = strstr(string, "HREF=");
 
122
    href = strstr(string, temp);
49
123
    if(!href) return NULL;
50
124
 
51
125
    while(*href != '\"')
65
139
    return ret;
66
140
}
67
141
 
68
 
char* getcontent(const char* string) {
69
 
    char* ret;
70
 
    const char* ptr = string;
 
142
int count_whitespace(const char* string) {
71
143
    int count = 0;
72
 
    while(*ptr != '>') {
73
 
        if(*ptr == '\0' || *ptr == '<') return NULL;
74
 
        ptr++;
75
 
    }
76
 
 
77
 
    string = ++ptr;
78
 
 
79
 
    // DD ends in a newline
80
 
    while(*ptr != '<' && *ptr != '\n') {
81
 
        if(*ptr == '\0') return NULL;
 
144
    while(isspace(*string)) {
82
145
        count++;
83
 
        ptr++;
84
 
    }
85
 
 
86
 
    ret = malloc(count+1);
87
 
    memcpy(ret,string,count);
88
 
    ret[count] = '\0';
89
 
 
90
 
    return ret;
91
 
}
92
 
/* Make a state machine that turns bookmarks.html into a tree of:
93
 
 * BookmarksPtr
94
 
 *   next
95
 
 *   children
96
 
 *   type
97
 
 *   value
98
 
 *     (union)
99
 
 *     url_value
100
 
 *       desc
101
 
 *       properties
102
 
 *       name
103
 
 *       url
104
 
 *     folder_value
105
 
 *       desc
106
 
 *       properties
107
 
 *       name
108
 
 *
109
 
 * The state machine is necessary as we need to process line-by-line.
110
 
 * The structure is necessary because we need to be able to easily merge a set
111
 
 * of bookmarks and folders into the existing bookmarks.html.
112
 
 */
113
 
/*
114
 
void new_firefox_worker(void) {
115
 
    while(fgets(line, 1024, fp) != NULL) {
116
 
        tag = readtag(line);
117
 
        if(!tag) continue;
118
 
 
119
 
        if(strcmp(tag,"/DL") == 0) return;
120
 
        if(strcmp(tag,"HR") == 0) ctx->next = HR;
121
 
*/
122
 
 
123
 
void firefox_worker(FILE* fp, const char* foldername) {
124
 
    char line[1024];
125
 
    char* tag;
126
 
 
127
 
    // State machine it. ctx can be the parent tag.
128
 
    // Write to a tree structure similar to libxml2's.
129
 
    while(fgets(line, 1024, fp) != NULL) {
130
 
        
131
 
        tag = readtag(line);
132
 
        if(!tag) continue;
133
 
        
134
 
        if(strcmp(tag,"HR") == 0)
135
 
            printf("horizontal rule\n");
136
 
        
137
 
        else if(strcmp(tag, "/DL") == 0)
138
 
            return;
139
 
 
140
 
        // Either a bookmark or a folder.
141
 
        else if(strcmp(tag, "DT") == 0) {
142
 
            int offset = countwhitespace(line) + 4;
143
 
 
144
 
            free(tag);
145
 
            tag = readtag(line+offset);
146
 
            if(!tag) continue;
147
 
 
148
 
 
149
 
            // A bookmark
150
 
            if(strcmp(tag, "A") == 0) {
151
 
                char* url = geturl(line+offset+2);
152
 
                char* name = getcontent(line+offset+2);
153
 
                if(foldername)
154
 
                    printf("folder (%s) url: %s, name: %s\n", foldername, url, name);
155
 
                else
156
 
                    printf("url: %s, name: %s\n", url, name);
157
 
                free(url);
158
 
                free(name);
159
 
            }
160
 
            // A folder
161
 
            else if(strcmp(tag, "H3") == 0) {
162
 
                char* folder = getcontent(line+offset+3);
163
 
                printf("folder: %s\n", folder);
164
 
 
165
 
                if(fgets(line, 1024, fp) == NULL) return;
166
 
                char* tag2 = readtag(line);
167
 
                char* description = NULL;
168
 
                if(strcmp(tag2, "DD") == 0) {
169
 
                    // Skip past the <
170
 
                    // FIXME: add getwhitespace to the offset
171
 
                    description = getcontent(line+1);
172
 
                    printf("description (%s): %s\n", folder, description);
173
 
                    free(description);
174
 
                    free(tag2);
175
 
                }
176
 
 
177
 
                // skip past <DL>
178
 
                if(fgets(line, 1024, fp) == NULL) return;
179
 
 
180
 
                firefox_worker(fp, folder);
181
 
                free(folder);
182
 
            }
183
 
        }
184
 
        free(tag);
185
 
    }
186
 
 
187
 
}
 
146
        string++;
 
147
    }
 
148
 
 
149
    return count;
 
150
}
 
151
 
 
152
element* current; // To avoid having to iterate the entire list on every addition.
 
153
element* new_element(element** lst, item_type t, void* attributes) {
 
154
    element* el;
 
155
    el = (element *)malloc(sizeof(element));
 
156
    el->next = NULL;
 
157
    el->children = NULL;
 
158
    el->type = t;
 
159
    el->attributes = attributes;
 
160
    if(current)
 
161
        current->next = el;
 
162
    if(!(*lst)) {
 
163
        *lst = el;
 
164
    }
 
165
 
 
166
    current = el;
 
167
    return el;
 
168
}
 
169
element* append_element(element* parent, item_type t, void* attributes) {
 
170
    element* el;
 
171
    el = (element *)malloc(sizeof(element));
 
172
    el->next = NULL;
 
173
    el->children = NULL;
 
174
    el->type = t;
 
175
    el->attributes = attributes;
 
176
    
 
177
    if(parent->children) {
 
178
        element* ptr = parent->children;
 
179
        while(ptr->next != NULL) {
 
180
            ptr = ptr->next;
 
181
        }
 
182
 
 
183
        ptr->next = el;
 
184
 
 
185
    } else {
 
186
        parent->children = el;
 
187
    }
 
188
 
 
189
    return el;
 
190
}
 
191
void internet_explorer_build(const char* path, element* parent, element** list) {
 
192
    struct dirent *dp;
 
193
    struct stat statbuf;
 
194
    DIR *dfd;
 
195
    FILE* fp;
 
196
    
 
197
    char * line = NULL;
 
198
    size_t len = 0;
 
199
    ssize_t read;
 
200
 
 
201
    char* url = NULL;
 
202
 
 
203
    if(chdir(path) == -1) {
 
204
        fprintf(stderr, "Error: Could not chdir into %s\n", path);
 
205
        return;
 
206
    }
 
207
    dfd = opendir(".");
 
208
    if(dfd != NULL) {
 
209
        while((dp = readdir(dfd)) != NULL) {
 
210
            if((strcmp(dp->d_name,".") == 0) || (strcmp(dp->d_name,"..") == 0))
 
211
                continue;
 
212
            char* tmp = dp->d_name;
 
213
            while(*tmp != '\0') tmp++;
 
214
            while(tmp != dp->d_name && *tmp != '.') tmp--;
 
215
            if(tmp != dp->d_name) {
 
216
                if(strcmp(tmp,".url") == 0) {
 
217
                    fp = fopen(dp->d_name, "r");
 
218
                    while((read = getline(&line, &len, fp)) != -1) {
 
219
                        char utag[5];
 
220
                        strncpy(utag, line, 5);
 
221
                        utag[4] = '\0';
 
222
                        if(strcmp(utag,"URL=") == 0) {
 
223
                            url = malloc(strlen(line+4));
 
224
                            memcpy(url,line+4, strlen(line+4)-1);
 
225
                            url[strlen(line+4)-2] = '\0';
 
226
                        }
 
227
                    }
 
228
                    *tmp = '\0';
 
229
                    if(url) {
 
230
                        char* title = malloc(strlen(dp->d_name)+1);
 
231
                        strcpy(title, dp->d_name);
 
232
                        hlink* l;
 
233
                        l = (hlink *)malloc(sizeof(hlink));
 
234
                        l->url = url;
 
235
                        l->title = title;
 
236
                        l->feed = NULL;
 
237
                        l->description = NULL;
 
238
                        l->icon = NULL;
 
239
                        
 
240
                        if(parent) {
 
241
                            append_element(parent, LINK, l);
 
242
                        } else {
 
243
                            new_element(list, LINK, l);
 
244
                        }
 
245
                    }
 
246
                    fclose(fp);
 
247
                }
 
248
            } else {
 
249
                stat(dp->d_name,&statbuf);
 
250
                if((statbuf.st_mode & S_IFMT) == S_IFDIR) {
 
251
                    // is a directory
 
252
                    char* title = malloc(strlen(dp->d_name)+1);
 
253
                    strcpy(title, dp->d_name);
 
254
                    
 
255
                    folder* f;
 
256
                    f = (folder *)malloc(sizeof(folder));
 
257
                    f->title = title;
 
258
                    f->description = NULL;
 
259
 
 
260
                    element* p;
 
261
                    if(parent) {
 
262
                        p = append_element(parent, FOLDER, f);
 
263
                    } else {
 
264
                        p = new_element(list, FOLDER, f);
 
265
                    }
 
266
                    internet_explorer_build(dp->d_name, p, list);
 
267
 
 
268
                }
 
269
            }
 
270
        }
 
271
        closedir(dfd);
 
272
    }
 
273
    chdir("..");
 
274
    return;
 
275
}
 
276
void firefox_build(FILE* fp, element* parent, element** list) {
 
277
    // if parent is set then we will descend into parent and look at that level.
 
278
    char * line = NULL;
 
279
    char* tag = NULL;
 
280
    size_t len = 0;
 
281
    ssize_t read;
 
282
    bool repeat = false;
 
283
 
 
284
 
 
285
    while (true) {
 
286
        if(!repeat) {
 
287
            if((read = getline(&line, &len, fp)) == -1) break;
 
288
        } else {
 
289
            repeat = false;
 
290
        }
 
291
        tag = read_tag(line);
 
292
        if(!tag)
 
293
            continue;
 
294
 
 
295
        if(strcmp(tag, "/DL") == 0) {
 
296
            return;
 
297
        }
 
298
        // Horizontal Rule
 
299
        else if(strcmp(tag, "HR") == 0) {
 
300
            if(parent) {
 
301
                append_element(parent, HR, NULL);
 
302
            } else {
 
303
                new_element(list, HR, NULL);
 
304
            }
 
305
        }
 
306
        // Either a bookmark or a folder.
 
307
        else if(strcmp(tag, "DT") == 0) {
 
308
            int offset = count_whitespace(line)+4;
 
309
            free(tag);
 
310
            tag = read_tag(line + offset);
 
311
            if(!tag) {
 
312
                fprintf(stderr, "Could not find a tag after DT in line: %s\n", line + offset);
 
313
                return;
 
314
            }
 
315
            // Bookmark
 
316
            if(strcmp(tag, "A") == 0) {
 
317
                char* url = getattrib(line+offset+2, HREF);
 
318
                char* title = getcontent(line+offset+2);
 
319
                char* icon = getattrib(line+offset+2, ICON);
 
320
                char* feed = getattrib(line+offset+2, FEED);
 
321
                char* description = NULL;
 
322
                
 
323
                if((read = getline(&line, &len, fp)) == -1) {
 
324
                    puts("Got an EOF after an H3 tag.  Corrupt file?");
 
325
                    return;
 
326
                }
 
327
                tag = read_tag(line);
 
328
                if(strcmp(tag, "DD") == 0) {
 
329
                    description = getcontent(line+1);
 
330
                } else
 
331
                    repeat = true;
 
332
 
 
333
                hlink* l;
 
334
                l = (hlink *)malloc(sizeof(hlink));
 
335
                l->url = url;
 
336
                l->title = title;
 
337
                l->icon = icon;
 
338
                l->feed = feed;
 
339
                l->description = description;
 
340
                
 
341
                if(parent) {
 
342
                    append_element(parent, LINK, l);
 
343
                } else {
 
344
                    new_element(list, LINK, l);
 
345
                }
 
346
            }
 
347
            // Folder
 
348
            else if(strcmp(tag, "H3") == 0) {
 
349
                char* title = getcontent(line+offset+3);
 
350
                char* description = NULL;
 
351
                if((read = getline(&line, &len, fp)) == -1) {
 
352
                    puts("Got an EOF after an H3 tag.  Corrupt file?");
 
353
                    return;
 
354
                }
 
355
                tag = read_tag(line);
 
356
                if(!tag)
 
357
                    continue;
 
358
                if(strcmp(tag, "DD") == 0 || strcmp(tag, "DL") == 0) {
 
359
                    description = getcontent(line+1);
 
360
                } else {
 
361
                    puts("Tag after H3 was not DD or DL.");
 
362
                    return;
 
363
                }
 
364
                
 
365
                folder* f;
 
366
                f = (folder *)malloc(sizeof(folder));
 
367
                f->title = title;
 
368
                f->description = description;
 
369
 
 
370
                element* p;
 
371
                if(parent) {
 
372
                    p = append_element(parent, FOLDER, f);
 
373
                } else {
 
374
                    p = new_element(list, FOLDER, f);
 
375
                }
 
376
                firefox_build(fp, p, list);
 
377
            }
 
378
        }
 
379
        free(tag);
 
380
    }
 
381
    if (line)
 
382
        free(line);
 
383
 
 
384
    return;
 
385
}
 
386
void firefox_format_worker(FILE* fp, const element* ptr) {
 
387
 
 
388
    char* title = NULL;
 
389
    char* description = NULL;
 
390
    char* url = NULL;
 
391
    char* feed = NULL;
 
392
    char* icon = NULL;
 
393
 
 
394
    char linkstr[64] = "\0";
 
395
    while(ptr) {
 
396
        switch(ptr->type) {
 
397
            case HR :
 
398
                fprintf(fp, "<HR>\n");
 
399
                break;
 
400
            case LINK :
 
401
                title = ((hlink *)ptr->attributes)->title;
 
402
                url = ((hlink *)ptr->attributes)->url;
 
403
                feed = ((hlink *)ptr->attributes)->feed;
 
404
                description = ((hlink *)ptr->attributes)->description;
 
405
                icon = ((hlink *)ptr->attributes)->icon;
 
406
                strcpy(linkstr, "<DT><A HREF=\"%s\"");
 
407
 
 
408
                if(feed) {
 
409
                    strcat(linkstr, " FEEDURL=\"%s\"");
 
410
                }
 
411
                if(icon) {
 
412
                    strcat(linkstr, " ICON=\"%s\"");
 
413
                }
 
414
                strcat(linkstr, ">%s</A>\n");
 
415
 
 
416
                if(icon && feed) fprintf(fp, linkstr, url, feed, icon, title);
 
417
                else if(icon) fprintf(fp, linkstr, url, icon, title);
 
418
                else if(feed) fprintf(fp, linkstr, url, feed, title);
 
419
                else fprintf(fp, linkstr, url, title);
 
420
                linkstr[0] = '\0';
 
421
 
 
422
                if(description)
 
423
                    fprintf(fp, "<DD>%s\n", description);
 
424
                break;
 
425
 
 
426
            case FOLDER :
 
427
                title = ((folder *)ptr->attributes)->title;
 
428
                description = ((folder *)ptr->attributes)->description;
 
429
                
 
430
                fprintf(fp, "<DT><H3>%s</H3>\n", title);
 
431
                if(description)
 
432
                    fprintf(fp, "<DD>%s\n", description);
 
433
                fprintf(fp, "<DL><p>\n");
 
434
                if(ptr->children)
 
435
                    firefox_format_worker(fp, ptr->children);
 
436
                fprintf(fp, "</DL><p>\n");
 
437
                break;
 
438
            default :
 
439
                puts("Error: Type not A, H3, or HR.");
 
440
                break;
 
441
        }
 
442
        ptr = ptr->next;
 
443
    }
 
444
    return;
 
445
}
 
446
 
 
447
void firefox_format(const element* ptr, const char* file) {
 
448
    FILE* fp;
 
449
    const char intro[] = "<!DOCTYPE NETSCAPE-Bookmark-file-1>\n"
 
450
    "<!-- This is an automatically generated file.\n"
 
451
    "     It will be read and overwritten.\n"
 
452
    "     DO NOT EDIT! -->\n"
 
453
    "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\n"
 
454
    "<TITLE>Bookmarks</TITLE>\n"
 
455
    "<H1>Bookmarks</H1>\n"
 
456
    "<DL><p>\n\n";
 
457
 
 
458
    fp = fopen(file, "w");
 
459
    if(fp) {
 
460
        fprintf(fp, intro);
 
461
        firefox_format_worker(fp, ptr);
 
462
        fprintf(fp, "</DL><p>\n");
 
463
        fclose(fp);
 
464
    } else {
 
465
        fprintf(stderr, "Could not open %s for writing.\n", file);
 
466
    }
 
467
}
 
468
#if 0
 
469
void firefox_format_test(element* curr) {
 
470
    while(curr) {
 
471
        switch(curr->type) {
 
472
            case HR :
 
473
                puts("Horizontal Rule");
 
474
                break;
 
475
            case LINK :
 
476
                printf("Link: %s (%s)\n",   ((hlink *)curr->attributes)->title,
 
477
                                            ((hlink *)curr->attributes)->url);
 
478
                /* printf("Link: %s (%s)\nFeed: %s\n%s\n",   ((hlink *)curr->attributes)->title,
 
479
                                                ((hlink *)curr->attributes)->url,
 
480
                                                ((hlink *)curr->attributes)->feed,
 
481
                                                ((hlink *)curr->attributes)->icon); */
 
482
                break;
 
483
            case FOLDER :
 
484
                printf("Folder: %s (%s)\n", ((folder *)curr->attributes)->title,
 
485
                                            ((folder *)curr->attributes)->description);
 
486
                
 
487
                if(curr->children) {
 
488
                    puts("Folder has children.");
 
489
                    firefox_format_test(curr->children);
 
490
                    puts("Done iterating children.");
 
491
                }
 
492
                break;
 
493
            default :
 
494
                puts("Unknown");
 
495
                break;
 
496
        }
 
497
        curr = curr->next;
 
498
    }
 
499
    puts("");
 
500
    return;
 
501
 
 
502
}
 
503
#endif
 
504
void merge(element** to, element* from) {
 
505
    element* t = *to;
 
506
    element* f = from;
 
507
    bool found = false;
 
508
 
 
509
    while(f) {
 
510
        while(t) {
 
511
            if(t->type == f->type) {
 
512
                int type = t->type;
 
513
                switch(type) {
 
514
                    case HR :
 
515
                        found = true;
 
516
                        break;
 
517
                    case LINK :
 
518
                        if(strcmp(((hlink *)t->attributes)->url,
 
519
                            ((hlink *)f->attributes)->url) == 0) {
 
520
                            found = true;
 
521
                        }
 
522
                        break;
 
523
                    case FOLDER :
 
524
                        if(strcmp(((folder *)t->attributes)->title,
 
525
                            ((folder *)f->attributes)->title) == 0) {
 
526
 
 
527
                            if(f->children && t->children) {
 
528
                                merge(&t->children, f->children);
 
529
                            }
 
530
 
 
531
                            found = true;
 
532
                        }
 
533
                        break;
 
534
                    default :
 
535
                        puts("Link type unknown.");
 
536
                        break;
 
537
                }
 
538
                if(found) break;
 
539
            }
 
540
            t = t->next;
 
541
        }
 
542
        if(!found) {
 
543
            element* ptr;
 
544
            element* temp = f;
 
545
            // Remove from its list
 
546
            if(from != f) {
 
547
                ptr = from;
 
548
                while(ptr->next != f)
 
549
                    ptr = ptr->next;
 
550
                ptr->next = f->next;
 
551
                f = ptr->next;
 
552
            } else {
 
553
                // It was the first element in the list
 
554
                from = f->next;
 
555
                f = from;
 
556
            }
 
557
 
 
558
            // Append to the other list
 
559
            ptr = *to;
 
560
            while(ptr->next)
 
561
                ptr = ptr->next;
 
562
            ptr->next = temp;
 
563
            temp->next = NULL;
 
564
 
 
565
        } else {
 
566
            f = f->next;
 
567
        }
 
568
        found = false;
 
569
        t = *to;
 
570
    }
 
571
}
 
572
 
 
573
void setup_import(char** fullpath, element** to_bookmarks) {
 
574
    struct dirent *dp;
 
575
    struct stat statbuf;
 
576
    DIR *dfd;
 
577
    char* to_firefoxdir = NULL;
 
578
    char* d_name;
 
579
    FILE* fp;
 
580
    char* path = NULL;
 
581
    char bookmarksfile[32];
 
582
    *bookmarksfile = '\0';
 
583
 
 
584
    char random[] = "abcdefghijklmnopqrstuvwxyz0123456789"; //26
 
585
    asprintf(&to_firefoxdir, "%s/home/%s/.mozilla/firefox", to_location, to_user);
 
586
    char* mkdir = NULL;
 
587
    // This might be dangerous.
 
588
    asprintf(&mkdir, "mkdir -p %s", to_firefoxdir);
 
589
    system(mkdir);
 
590
    free(mkdir);
 
591
 
 
592
    if(chdir(to_firefoxdir) == -1) {
 
593
        fprintf(stderr, "Could not change directory to %s\n", to_firefoxdir);
 
594
        return;
 
595
    }
 
596
 
 
597
    dfd = opendir(".");
 
598
    if(dfd == NULL) {
 
599
        fprintf(stderr, "Could not open directory.");
 
600
        return;
 
601
    }
 
602
    
 
603
    while((dp = readdir(dfd)) != NULL) {
 
604
        if((strcmp(dp->d_name,".") == 0) || (strcmp(dp->d_name,"..") == 0))
 
605
            continue;
 
606
        stat(dp->d_name,&statbuf);
 
607
        if(!S_ISDIR(statbuf.st_mode)) continue;
 
608
 
 
609
        // Make sure we're looking at the default profile.
 
610
        d_name = malloc(strlen(dp->d_name)+1);
 
611
        strcpy(d_name,dp->d_name);
 
612
        char* d_nameptr = d_name;
 
613
        while(*d_nameptr != '\0') d_nameptr++;
 
614
        while(*d_nameptr != '.' && d_nameptr != dp->d_name) d_nameptr--;
 
615
        if(strcmp(d_nameptr,".default") == 0) {
 
616
            strcpy(bookmarksfile, dp->d_name);
 
617
            strcat(bookmarksfile, "/bookmarks.html");
 
618
            break;
 
619
        }
 
620
        free(d_name);
 
621
 
 
622
    }
 
623
    closedir(dfd);
 
624
    if(bookmarksfile[0] != '\0') {
 
625
        fp = fopen(bookmarksfile, "r");
 
626
        if(fp == NULL) {
 
627
            fprintf(stderr, "Could not open file, %s\n", bookmarksfile);
 
628
            return;
 
629
        }
 
630
        firefox_build(fp, NULL, to_bookmarks);
 
631
        fclose(fp);
 
632
        asprintf(fullpath, "%s/%s", to_firefoxdir, bookmarksfile);
 
633
    } else {
 
634
        int r, i;
 
635
        char b[17];
 
636
        for(i=0; i<8; i++) {
 
637
            r = (int)((double)rand() / ((double)RAND_MAX + 1) * 26);
 
638
            b[i] = random[r];
 
639
        }
 
640
        b[8] = '\0';
 
641
        strcat(b, ".default");
 
642
        
 
643
        asprintf(&path, "%s/profiles.ini", to_firefoxdir);
 
644
        fp = fopen(path, "a");
 
645
        free(path);
 
646
        fprintf(fp, "[General]\nStartWithLastProfile=1\n\n[Profile0]\n"
 
647
                    "Name=default\nIsRelative=1\nPath=%s\n\n", b);
 
648
        fclose(fp);
 
649
 
 
650
        asprintf(&path, "%s/%s", to_firefoxdir, b);
 
651
        asprintf(&mkdir, "mkdir -p %s", path);
 
652
        system(mkdir);
 
653
        free(mkdir);
 
654
        asprintf(fullpath, "%s/bookmarks.html", path);
 
655
        free(path);
 
656
    }
 
657
    free(to_firefoxdir);
 
658
 
 
659
}
 
660
 
188
661
void firefox_import_firefox(void) {
 
662
    struct dirent *dp, *dp2;
 
663
    struct stat statbuf;
 
664
    DIR *dfd, *dfd2;
 
665
    char bookmarksfile[32] = "\0";
189
666
    FILE* fp;
190
 
    //char line[1024];
191
 
    //char* tag;
192
 
 
193
 
    fp = fopen("/home/evan/.mozilla/firefox/j539x5mu.default/bookmarks.html", "r");
194
 
    firefox_worker(fp, NULL);
195
 
    fclose(fp);
196
 
}
 
667
    element* from_bookmarks = NULL;
 
668
    element* to_bookmarks = NULL;
 
669
 
 
670
    char* from_firefoxdir = NULL;
 
671
    char* fullpath = NULL;
 
672
    bool found = false;
 
673
 
 
674
    setup_import(&fullpath, &to_bookmarks);
 
675
 
 
676
    // Build a tree of the bookmarks file(s) we're importing from, then merge it
 
677
    // with the new bookmarks tree.
 
678
    if(os_type == LINUX) {
 
679
        asprintf(&from_firefoxdir, "%s/home/%s/.mozilla/firefox",
 
680
            from_location, from_user);
 
681
    }
 
682
    else if(os_type == WINDOWSXP) {
 
683
        asprintf(&from_firefoxdir, "%s/Documents and Settings/%s/"
 
684
            "Application Data/Mozilla/Firefox/Profiles", from_location,
 
685
            from_user);
 
686
    }
 
687
    if(chdir(from_firefoxdir) == -1) {
 
688
        fprintf(stderr, "Could not change directory to %s.\n", from_firefoxdir);
 
689
        return;
 
690
    }
 
691
    free(from_firefoxdir);
 
692
 
 
693
    dfd = opendir(".");
 
694
    if(dfd != NULL) {
 
695
        while((dp = readdir(dfd)) != NULL) {
 
696
            if((strcmp(dp->d_name,".") == 0) || (strcmp(dp->d_name,"..") == 0))
 
697
                continue;
 
698
            stat(dp->d_name,&statbuf);
 
699
            if(!S_ISDIR(statbuf.st_mode)) continue;
 
700
            
 
701
            dfd2 = opendir(dp->d_name);
 
702
            if(dfd != NULL) {
 
703
                while((dp2 = readdir(dfd2)) != NULL) {
 
704
                    if(strcmp(dp2->d_name,"bookmarks.html") == 0)
 
705
                        found = true;
 
706
                }
 
707
            }
 
708
            closedir(dfd2);
 
709
            if(found) {
 
710
                found = false;
 
711
                strcpy(bookmarksfile,dp->d_name);
 
712
                strcat(bookmarksfile,"/bookmarks.html");
 
713
                fp = fopen(bookmarksfile,"r");
 
714
                if(fp == NULL) {
 
715
                    fprintf(stderr, "Could not open file, %s.", bookmarksfile);
 
716
                    continue;
 
717
                }
 
718
                // TODO: get rid of current.
 
719
                current = NULL;
 
720
                firefox_build(fp, NULL, &from_bookmarks);
 
721
                fclose(fp);
 
722
                if(to_bookmarks)
 
723
                    merge(&to_bookmarks, from_bookmarks);
 
724
                else
 
725
                    to_bookmarks = from_bookmarks;
 
726
            }
 
727
        }
 
728
    closedir(dfd);
 
729
    }
 
730
 
 
731
    // Now we translate the tree into a bookmarks.html file.
 
732
    firefox_format(to_bookmarks, fullpath);
 
733
    free(fullpath);
 
734
}
 
735
 
 
736
void firefox_import_internetexplorer(void) {
 
737
    char* from_iedir = NULL;
 
738
    element* to_bookmarks = NULL;
 
739
    element* from_bookmarks = NULL;
 
740
    char* fullpath = NULL;
 
741
 
 
742
    setup_import(&fullpath, &to_bookmarks);
 
743
 
 
744
    asprintf(&from_iedir, "%s/Documents and Settings/%s/Favorites", from_location, from_user);
 
745
    current = NULL;
 
746
    internet_explorer_build(from_iedir, NULL, &from_bookmarks);
 
747
    if(to_bookmarks)
 
748
        merge(&to_bookmarks, from_bookmarks);
 
749
    else
 
750
        to_bookmarks = from_bookmarks;
 
751
    
 
752
    firefox_format(to_bookmarks, fullpath);
 
753
    free(fullpath);
 
754
}
 
755
 
 
756
// vim:ai:et:sts=4:tw=80:sw=4: