aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/nntp/camel-nntp-newsrc.c
blob: 5c92074000bb4e5929289935b3f41b1aa6c52868 (plain) (blame)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-nntp-newsrc.c - .newsrc parsing/regurgitating code */
/* 
 *
 * Copyright (C) 2000 Helix Code, Inc. <toshok@helixcode.com>
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include <config.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <fcntl.h>
#include <unistd.h>
#include "camel-nntp-newsrc.h"
#include <camel/camel-folder-summary.h>

typedef struct {
    guint low;
    guint high;
} ArticleRange;

typedef struct {
    char *name;
    GArray *ranges;
    gboolean subscribed;
} NewsrcGroup;

struct CamelNNTPNewsrc {
    gchar *filename;
    GHashTable *groups;
    gboolean dirty;
} ;

static NewsrcGroup *
camel_nntp_newsrc_group_add (CamelNNTPNewsrc *newsrc, const char *group_name, gboolean subscribed)
{
    NewsrcGroup *new_group = g_malloc(sizeof(NewsrcGroup));

    new_group->name = g_strdup(group_name);
    new_group->subscribed = subscribed;
    new_group->ranges = g_array_new (FALSE, FALSE, sizeof (ArticleRange));

    g_hash_table_insert (newsrc->groups, new_group->name, new_group);

    newsrc->dirty = TRUE;

    return new_group;
}

static long
camel_nntp_newsrc_group_get_highest_article_read(CamelNNTPNewsrc *newsrc, NewsrcGroup *group)
{
    if (group->ranges->len == 0)
        return 0;

    return g_array_index(group->ranges, ArticleRange, group->ranges->len - 1).high;
}

static void
camel_nntp_newsrc_group_mark_range_read(CamelNNTPNewsrc *newsrc, NewsrcGroup *group, long low, long high)
{
    int i;

    if (group->ranges->len == 1
        && g_array_index (group->ranges, ArticleRange, 0).low == 0
        && g_array_index (group->ranges, ArticleRange, 0).high == 0) {
        g_array_index (group->ranges, ArticleRange, 0).low = low;
        g_array_index (group->ranges, ArticleRange, 0).high = high;

        newsrc->dirty = TRUE;
    }
    else  {
        ArticleRange tmp_range;

        for (i = 0; i < group->ranges->len; i ++) {
            guint range_low = g_array_index (group->ranges, ArticleRange, i).low;
            guint range_high = g_array_index (group->ranges, ArticleRange, i).high;
            
            /* if it's already part of a range, return immediately. */
            if (low >= range_low &&
                low <= range_high &&
                high >= range_low &&
                high <= range_high) {
                return;
            }
            /* if we have a new lower bound for this range, set it. */
            else if (low <= range_low
                 && high >= range_low
                 && high <= range_high) {
                g_array_index (group->ranges, ArticleRange, i).low = low;
                newsrc->dirty = TRUE;
                return;
            }
            /* if we have a new upper bound for this range, set it. */
            else if (high >= range_high
                 && low >= range_low
                 && low <= range_high) {
                g_array_index (group->ranges, ArticleRange, i).high = high;
                newsrc->dirty = TRUE;
                return;
            }
            /* if we would be inserting another range that
                           starts one index higher than an existing
                           one, make the upper value of the existing
                           range the upper value of the new one. */
            else if (low == range_high + 1) {
                g_array_index (group->ranges, ArticleRange, i).high = high;
                newsrc->dirty = TRUE;
                return;
            }
            /* if we would be inserting another range that
                           ends one index lower than an existing one,
                           group the existing range by setting its low
                           to the new low */
            else if (high == range_low - 1) {
                g_array_index (group->ranges, ArticleRange, i).low = low;
                newsrc->dirty = TRUE;
                return;
            }
            /* if the range lies entirely outside another
                           range, doesn't coincide with it's
                           endpoints, and has lower values, insert it
                           into the middle of the list. */
            else if (low < range_low
                 && high < range_low) {
                tmp_range.low = low;
                tmp_range.high = high;

                group->ranges = g_array_insert_val (group->ranges, i, tmp_range);
                newsrc->dirty = TRUE;

                return;
            }
        }

        /* if we made it here, the range needs to go at the end */
        tmp_range.low = low;
        tmp_range.high = high;
        group->ranges = g_array_append_val (group->ranges, tmp_range);
        newsrc->dirty = TRUE;
    } 
}

int
camel_nntp_newsrc_get_highest_article_read (CamelNNTPNewsrc *newsrc, const char *group_name)
{
    NewsrcGroup *group;

    group = g_hash_table_lookup (newsrc->groups, group_name);

    return camel_nntp_newsrc_group_get_highest_article_read (newsrc, group);
}

void
camel_nntp_newsrc_mark_article_read (CamelNNTPNewsrc *newsrc, const char *group_name, int num)
{
    camel_nntp_newsrc_mark_range_read (newsrc, group_name, num, num);
}

void
camel_nntp_newsrc_mark_range_read(CamelNNTPNewsrc *newsrc, const char *group_name, long low, long high)
{
    NewsrcGroup *group;

    /* swap them if they're in the wrong order. */
    if (low > high) {
        long tmp;

        tmp = high;
        high = low;
        low = tmp;
    }

    group = g_hash_table_lookup (newsrc->groups, group_name);

    camel_nntp_newsrc_group_mark_range_read (newsrc, group, low, high);
}

gboolean
camel_nntp_newsrc_article_is_read (CamelNNTPNewsrc *newsrc, const char *group_name, long num)
{
    int i;
    NewsrcGroup *group;

    group = g_hash_table_lookup (newsrc->groups, group_name);
    
    for (i = 0; i < group->ranges->len; i++) {
        if (num >= g_array_index (group->ranges, ArticleRange, i).low && 
            num <= g_array_index (group->ranges, ArticleRange, i).high) {
            return TRUE;
        }
    }

    return FALSE;
}

gboolean  
camel_nntp_newsrc_group_is_subscribed (CamelNNTPNewsrc *newsrc, const char *group_name)
{
    NewsrcGroup *group = g_hash_table_lookup (newsrc->groups, group_name);

    if (group) {
        return group->subscribed;
    }
    else {
        return FALSE;
    }
}

void
camel_nntp_newsrc_subscribe_group (CamelNNTPNewsrc *newsrc, const char *group_name)
{
    NewsrcGroup *group = g_hash_table_lookup (newsrc->groups, group_name);

    if (group) {
        if (!group->subscribed)
            newsrc->dirty = TRUE;
        group->subscribed = TRUE;
    }
    else {
        camel_nntp_newsrc_group_add (newsrc, group_name, TRUE);
    }
}

void
camel_nntp_newsrc_unsubscribe_group (CamelNNTPNewsrc *newsrc, const char *group_name)
{
    NewsrcGroup *group = g_hash_table_lookup (newsrc->groups, group_name);

    if (group) {
        if (group->subscribed)
            newsrc->dirty = TRUE;
        group->subscribed = FALSE;
    }
    else {
        camel_nntp_newsrc_group_add (newsrc, group_name, FALSE);
    }
}

struct newsrc_ptr_array {
    GPtrArray *ptr_array;
    gboolean subscribed_only;
};

static void
get_group_foreach (char *group_name, NewsrcGroup *group, struct newsrc_ptr_array *npa)
{
    if (group->subscribed || !npa->subscribed_only) {
        g_ptr_array_add (npa->ptr_array, group_name);
    }
}

GPtrArray *
camel_nntp_newsrc_get_subscribed_group_names (CamelNNTPNewsrc *newsrc)
{
    struct newsrc_ptr_array npa;

    g_return_val_if_fail (newsrc, NULL);

    npa.ptr_array = g_ptr_array_new();
    npa.subscribed_only = TRUE;

    g_hash_table_foreach (newsrc->groups,
                  (GHFunc)get_group_foreach, &npa);

    return npa.ptr_array;
}

GPtrArray *
camel_nntp_newsrc_get_all_group_names (CamelNNTPNewsrc *newsrc)
{
    struct newsrc_ptr_array npa;

    g_return_val_if_fail (newsrc, NULL);

    npa.ptr_array = g_ptr_array_new();
    npa.subscribed_only = FALSE;

    g_hash_table_foreach (newsrc->groups,
                  (GHFunc)get_group_foreach, &npa);

    return npa.ptr_array;
}

void
camel_nntp_newsrc_free_group_names (CamelNNTPNewsrc *newsrc, GPtrArray *group_names)
{
    g_ptr_array_free (group_names, TRUE);
}

struct newsrc_fp {
    CamelNNTPNewsrc *newsrc;
    FILE *fp;
};

static void
camel_nntp_newsrc_write_group_line(gpointer key, NewsrcGroup *group, struct newsrc_fp *newsrc_fp)
{
    CamelNNTPNewsrc *newsrc;
    FILE *fp;
    int i;
    int line_length = 0;

    fp = newsrc_fp->fp;
    newsrc = newsrc_fp->newsrc;

    fprintf (fp, "%s%c", group->name, group->subscribed ? ':' : '!');

    line_length += strlen(group->name) + 1;

    if (group->ranges->len == 1
        && g_array_index (group->ranges, ArticleRange, 0).low == 0
        && g_array_index (group->ranges, ArticleRange, 0).high == 0) {
        fprintf (fp, "\n");

        return; /* special case since our parsing code will insert this
               bogus range if there were no read articles.  The code
               to add a range is smart enough to remove this one if we
               ever mark an article read, but we still need to deal with
               it if that code doesn't get hit. */
    }

    fprintf (fp, " ");
    line_length += 1;

    for (i = 0; i < group->ranges->len; i ++) {
        char range_buffer[100];
        guint low = g_array_index (group->ranges, ArticleRange, i).low;
        guint high = g_array_index (group->ranges, ArticleRange, i).high;

        if (low == high)
            sprintf(range_buffer, "%d", low);
        else if (low == high - 1)
            sprintf(range_buffer, "%d,%d", low, high);
        else
            sprintf(range_buffer, "%d-%d", low, high);

        if (i != group->ranges->len - 1)
            strcat(range_buffer, ",");

        /* this constant (991) gives the same line breaking as faried's .newsrc file */
        if (line_length + strlen(range_buffer) > 991 /*XXX*/) {
            char range_buffer2[101];
            int num_to_print = 991 - line_length;

            strcpy(range_buffer2, range_buffer);
            range_buffer2[num_to_print] = '!';
            range_buffer2[num_to_print+1] = '\n';
            range_buffer2[num_to_print+2] = '\0';

            fprintf (fp, range_buffer2);

            fprintf (fp, range_buffer + num_to_print);

            line_length = strlen(range_buffer) - num_to_print;
        }
        else {
            fprintf (fp, range_buffer);
            line_length += strlen(range_buffer);
        }
    }

    fprintf (fp, "\n");
}

void 
camel_nntp_newsrc_write_to_file(CamelNNTPNewsrc *newsrc, FILE *fp)
{
    struct newsrc_fp newsrc_fp;

    g_return_if_fail (newsrc);

    newsrc_fp.newsrc = newsrc;
    newsrc_fp.fp = fp;

    g_hash_table_foreach (newsrc->groups,
                  (GHFunc)camel_nntp_newsrc_write_group_line,
                  &newsrc_fp);
}

void
camel_nntp_newsrc_write(CamelNNTPNewsrc *newsrc)
{
    FILE *fp;

    g_return_if_fail (newsrc);

    if (!newsrc->dirty)
        return;

    if ((fp = fopen(newsrc->filename, "w")) == NULL) {
        g_warning ("Couldn't open newsrc file '%s'.\n", newsrc->filename);
        return;
    }

    camel_nntp_newsrc_write_to_file(newsrc, fp);

    newsrc->dirty = FALSE;

    fclose(fp);
}

static void
camel_nntp_newsrc_parse_line(CamelNNTPNewsrc *newsrc, char *line)
{
    char *p, sep, *comma, *dash;
    gboolean is_subscribed;
    NewsrcGroup *group;

    p = strchr(line, ':');

    if (p) {
        is_subscribed = TRUE;
    }
    else {
        p = strchr(line, '!');
        if (p)
            is_subscribed = FALSE;
        else
            return; /* bogus line. */
    }

    sep = *p;
    *p = '\0';

    group = camel_nntp_newsrc_group_add (newsrc, line, is_subscribed);

    *p = sep;

    p++;

    do {
        guint high, low;

        comma = strchr(p, ',');

        if (comma)
            *comma = '\0';

        dash = strchr(p, '-');

        if (!dash) { /* there wasn't a dash.  must be just one number */
            high = low = atol(p);
        }
        else { /* there was a dash. */
            *dash = '\0';
            low = atol(p);
            *dash = '-';
            p = dash + 1;
            high = atol(p);
        }

        camel_nntp_newsrc_group_mark_range_read (newsrc, group, low, high);

        if (comma) {
            *comma = ',';
            p = comma + 1;
        }

    } while(comma);
}

#define MAX_LINE_LENGTH 1500
#define BUFFER_LENGTH (20 * MAX_LINE_LENGTH)

CamelNNTPNewsrc *
camel_nntp_newsrc_read_for_server (const char *server)
{
    FILE *fp;
    char buf[BUFFER_LENGTH];
    char *filename = g_strdup_printf ("%s/.newsrc-%s", g_get_home_dir(), server);
    CamelNNTPNewsrc *newsrc;

    newsrc = g_new0(CamelNNTPNewsrc, 1);
    newsrc->filename = filename;
    newsrc->groups = g_hash_table_new (g_str_hash, g_str_equal);

    if ((fp = fopen(filename, "r")) == NULL) {
        g_warning ("~/.newsrc-%s not present.\n", server);
        return newsrc;
    }

    while (fgets(buf, MAX_LINE_LENGTH, fp) != NULL) {
        /* we silently ignore (and lose!) lines longer than 20 * 1500 chars.
           Too bad for them.  */
        while(strlen(buf) < sizeof(buf) 
              && buf[strlen(buf) - 2] == '!') {
            fgets(&buf[strlen(buf) - 2], MAX_LINE_LENGTH, fp);
        }

        camel_nntp_newsrc_parse_line(newsrc, buf);
    }

    fclose(fp);

    return newsrc;
}