summaryrefslogtreecommitdiff
path: root/messages/messages.c
blob: a5fe1d556f1a0487035d94227e4c76c793bb10dd (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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001, 2002, 2003,
   2004 Free Software Foundation, Inc.

   GNU Mailutils 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, or (at your option)
   any later version.

   GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#ifdef HAVE_MALLOC_H
# include <malloc.h>
#endif

#include <mailutils/mailutils.h>

static int messages_count (const char *);

const char *program_version = "messages (" PACKAGE_STRING ")";
static char doc[] = N_("GNU messages -- count the number of messages in a mailbox");
static char args_doc[] = N_("[mailbox...]");

static struct argp_option options[] = {
  { NULL,         0, NULL,  0,
    /* TRANSLATORS: 'messages' is a program name. Do not translate it! */
    N_("messages specific switches:"), 0},
  {"quiet",	'q',	0,	0,	N_("Only display number of messages")},
  {"silent",	's',	0,	0,	N_("Same as -q")},
  { 0 }
};

static const char *argp_capa[] = {
  "common",
  "license",
  "mailbox",
#ifdef WITH_TLS
  "tls",
#endif
  NULL
};

struct arguments
{
  int argc;
  char **argv;
};

/* are we loud or quiet? */
static int silent = 0;

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *args = state->input;
  switch (key)
    {
    case 'q':
    case 's':
      silent = 1;
      break;
    case ARGP_KEY_ARG:
      args->argv = realloc (args->argv,
			    sizeof (char *) * (state->arg_num + 2));
      args->argv[state->arg_num] = arg;
      args->argv[state->arg_num + 1] = NULL;
      args->argc++;
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp argp = {
  options,
  parse_opt,
  args_doc,
  doc,
  NULL,
  NULL, NULL
};

int
main (int argc, char **argv)
{
  int i = 1;
  int err = 0;
  struct arguments args = {0, NULL};

  /* Native Language Support */
  mu_init_nls ();

  mu_argp_init (program_version, NULL);
#ifdef WITH_TLS
  mu_tls_init_client_argp ();
#endif
  mu_argp_parse (&argp, &argc, &argv, 0, argp_capa, NULL, &args);

  /* register the formats.  */
  mu_register_all_mbox_formats ();

  if (args.argc < 1 && messages_count (NULL) < 0)
    err = 1;
  else if (args.argc >= 1)
    {
      for (i = 0; i < args.argc; i++)
	{
	  if (messages_count (args.argv[i]) < 0)
	    err = 1;
	}
    }

  return err;
}

static int
messages_count (const char *box)
{
  mailbox_t mbox;
  url_t url = NULL;
  size_t count;
  int status = 0;

  status =  mailbox_create_default (&mbox, box);
  if (status != 0)
    {
      if (box)
	mu_error (_("Could not create mailbox `%s': %s"),
		  box, mu_strerror (status));
      else
	mu_error (_("Could not create default mailbox: %s"),
		  mu_strerror (status));
      return -1;
    }

  mailbox_get_url (mbox, &url);
  box = url_to_string (url);

  status =  mailbox_open (mbox, MU_STREAM_READ);
  if (status != 0)
    {
      mu_error (_("Could not open mailbox `%s': %s"),
		box, mu_strerror (status));
      return -1;
    }

  status = mailbox_messages_count (mbox, &count);
  if (status != 0)
    {
      mu_error (_("Could not count messages in mailbox `%s': %s"),
		box, mu_strerror (status));
      return -1;
    }

  if (silent)
    printf ("%d\n", count);
  else
    printf (_("Number of messages in %s: %d\n"), box, count);

  status = mailbox_close (mbox);
  if (status != 0)
    {
      mu_error (_("Could not close `%s': %s"),
		box, mu_strerror (status));
      return -1;
    }

  mailbox_destroy (&mbox);
  return count;
}

Return to:

Send suggestions and report system problems to the System administrator.