~ubuntu-branches/debian/sid/conky/sid

« back to all changes in this revision

Viewing changes to src/mboxscan.c

  • Committer: Bazaar Package Importer
  • Author(s): Cesare Tirabassi
  • Date: 2010-03-28 21:19:51 UTC
  • mfrom: (1.3.1 upstream) (16.1.17 lucid)
  • Revision ID: james.westby@ubuntu.com-20100328211951-ejq536k2r858srli
Tags: 1.8.0-1
* New upstream release:
  - add AF_UNIX socket support
  - fix sigsegv if config file is deleted (LP: #525926)
  - the following debian bugs are closed by this upload:
    + change automake1.10 to automake1.11 (Closes: #550929)
    + hwmon made compatible with kernels >= 2.6.31 (Closes: #556926)
    + text_buffer_size change is well documented (Closes: #519401)
    + fix diskio for not existing devices (Closes: #536557)
    + fix wrong mixer values on some systems (Closes: #540282)
    + fix minor memory leak (Closes: #566524)
    + fix some documentation error re. graphs (Closes: #564518)
    + add -p/--pause startup option (Closes: #513440)
    + fix documentation about mixer values (Closes: #538760)
* This release is based on the Ubuntu package with the following changes
  necessary for Debian (Closes: #536320):
  - change control and rules to build correctly on non-Linux arches
    (Closes: #536326)
  - updated NEWS, descriptions in control and changelog
  - change archive area to contrib
* Change priority of metapackage to extra
* My utmost thanks go to Kapil Hari Paranjape for his packaging work and to
  Luca Falavigna for being so kind to review and sponsor this release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Conky, a system monitor, based on torsmo
 
1
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
 
2
 * vim: ts=4 sw=4 noet ai cindent syntax=c
 
3
 *
 
4
 * Conky, a system monitor, based on torsmo
2
5
 *
3
6
 * Any original torsmo code is licensed under the BSD license
4
7
 *
7
10
 * Please see COPYING for details
8
11
 *
9
12
 * Copyright (c) 2006 Marco Candrian <mac@calmar.ws>
10
 
 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
 
13
 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
11
14
 *      (see AUTHORS)
12
15
 * All rights reserved.
13
16
 *
28
31
#include "conky.h"
29
32
#include "logging.h"
30
33
#include "mail.h"
 
34
#include "text_object.h"
31
35
#include <sys/stat.h>
32
36
#include <sys/time.h>
33
37
#include <errno.h>
34
 
#include "mboxscan.h"
35
38
 
36
39
#define FROM_WIDTH 10
37
40
#define SUBJECT_WIDTH 22
52
55
static int args_ok = 0;
53
56
static int from_width;
54
57
static int subject_width;
55
 
static int print_mails;
 
58
static int print_num_mails;
56
59
static int time_delay;
57
60
 
58
61
static char mbox_mail_spool[DEFAULT_TEXT_BUFFER_SIZE];
59
62
 
60
 
void mbox_scan(char *args, char *output, size_t max_len)
 
63
static void mbox_scan(char *args, char *output, size_t max_len)
61
64
{
62
65
        int i, u, flag;
63
66
        int force_rescan = 0;
78
81
                char *substr = strstr(args, "-n");
79
82
 
80
83
                if (substr) {
81
 
                        if (sscanf(substr, "-n %i", &print_mails) != 1) {
82
 
                                print_mails = PRINT_MAILS;
 
84
                        if (sscanf(substr, "-n %i", &print_num_mails) != 1) {
 
85
                                print_num_mails = PRINT_MAILS;
83
86
                        }
84
87
                } else {
85
 
                        print_mails = PRINT_MAILS;
 
88
                        print_num_mails = PRINT_MAILS;
86
89
                }
87
 
                if (print_mails < 1) {
88
 
                        print_mails = 1;
 
90
                if (print_num_mails < 1) {
 
91
                        print_num_mails = 1;
89
92
                }
90
93
 
91
94
                substr = strstr(args, "-t");
137
140
                        free(copy_args);
138
141
                }
139
142
                if (strlen(mbox_mail_spool) < 1) {
140
 
                        CRIT_ERR("Usage: ${mboxscan [-n <number of messages to print>] "
 
143
                        CRIT_ERR(NULL, NULL, "Usage: ${mboxscan [-n <number of messages to print>] "
141
144
                                "[-fw <from width>] [-sw <subject width>] "
142
145
                                "[-t <delay in sec> mbox]}");
143
146
                }
148
151
                }
149
152
 
150
153
                if (stat(mbox_mail_spool, &statbuf)) {
151
 
                        CRIT_ERR("can't stat %s: %s", mbox_mail_spool, strerror(errno));
 
154
                        CRIT_ERR(NULL, NULL, "can't stat %s: %s", mbox_mail_spool, strerror(errno));
152
155
                }
153
156
                args_ok = 1;    /* args-computing necessary only once */
154
157
        }
162
165
 
163
166
        /* mbox still exists? and get stat-infos */
164
167
        if (stat(mbox_mail_spool, &statbuf)) {
165
 
                ERR("can't stat %s: %s", mbox_mail_spool, strerror(errno));
 
168
                NORM_ERR("can't stat %s: %s", mbox_mail_spool, strerror(errno));
166
169
                output[0] = '\0';       /* delete any output */
167
170
                return;
168
171
        }
178
181
 
179
182
        /* build up double-linked ring-list to hold data, while scanning down the
180
183
         * mbox */
181
 
        for (i = 0; i < print_mails; i++) {
 
184
        for (i = 0; i < print_num_mails; i++) {
182
185
                curr = (struct ring_list *) malloc(sizeof(struct ring_list));
183
186
                curr->from = (char *) malloc(sizeof(char[from_width + 1]));
184
187
                curr->subject = (char *) malloc(sizeof(char[subject_width + 1]));
337
340
 
338
341
        output[0] = '\0';
339
342
 
340
 
        i = print_mails;
 
343
        i = print_num_mails;
341
344
        while (i) {
342
345
                struct ring_list *tmp;
343
346
                if (curr->from[0] != '\0') {
344
 
                        if (i != print_mails) {
 
347
                        if (i != print_num_mails) {
345
348
                                snprintf(buf, text_buffer_size, "\nF: %-*s S: %-*s", from_width,
346
349
                                        curr->from, subject_width, curr->subject);
347
350
                        } else {        /* first time - no \n in front */
362
365
                i--;
363
366
        }
364
367
}
 
368
 
 
369
struct mboxscan_data {
 
370
        char *args;
 
371
        char *output;
 
372
};
 
373
 
 
374
void parse_mboxscan_arg(struct text_object *obj, const char *arg)
 
375
{
 
376
        struct mboxscan_data *msd;
 
377
 
 
378
        msd = malloc(sizeof(struct mboxscan_data));
 
379
        memset(msd, 0, sizeof(struct mboxscan_data));
 
380
 
 
381
        msd->args = strndup(arg, text_buffer_size);
 
382
        msd->output = (char *) malloc(text_buffer_size);
 
383
        /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
 
384
        msd->output[0] = 1;
 
385
 
 
386
        obj->data.opaque = msd;
 
387
}
 
388
 
 
389
void print_mboxscan(struct text_object *obj, char *p, int p_max_size)
 
390
{
 
391
        struct mboxscan_data *msd = obj->data.opaque;
 
392
 
 
393
        if (!msd)
 
394
                return;
 
395
 
 
396
        mbox_scan(msd->args, msd->output, text_buffer_size);
 
397
        snprintf(p, p_max_size, "%s", msd->output);
 
398
}
 
399
 
 
400
void free_mboxscan(struct text_object *obj)
 
401
{
 
402
        struct mboxscan_data *msd = obj->data.opaque;
 
403
 
 
404
        if (!msd)
 
405
                return;
 
406
        if (msd->args)
 
407
                free(msd->args);
 
408
        if (msd->output)
 
409
                free(msd->output);
 
410
        free(obj->data.opaque);
 
411
        obj->data.opaque = NULL;
 
412
}
 
413