summaryrefslogtreecommitdiff
path: root/frm/from.c
blob: 6b9fdf84b495ff5648596fd11665753a445e6b21 (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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2005, 2007, 2008, 2009, 2010 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 3, 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., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

#include <frm.h>

int count_only;
char *sender_option;
char *mailbox_name;

static char doc[] = N_("GNU from -- display from and subject.");

static struct argp_option options[] = {
  {"count",  'c', NULL,   0, N_("just print a count of messages and exit")},
  {"sender", 's', N_("ADDRESS"), 0,
   N_("print only mail from addresses containing the supplied string") },
  {"file",   'f', N_("FILE"), 0,
   N_("read mail from FILE") },
  {"debug",  'd', NULL,   0, N_("enable debugging output"), 0},
  {0, 0, 0, 0}
};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  switch (key)
    {
    case 'c':
      count_only = 1;
      break;
      
    case 's':
      sender_option = arg;
      break;
      
    case 'f':
      mailbox_name = arg;
      break;
      
    case 'd':
      frm_debug++;
      break;

    default: 
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp argp = {
  options,
  parse_opt,
  N_("[OPTIONS] [USER]"),
  doc,
};

static const char *capa[] = {
  "common",
  "debug",
  "license",
  "mailbox",
  "locking",
  NULL
};

static int
from_select (size_t index, mu_message_t msg)
{
  if (count_only)
    return 0;

  if (sender_option)
    {
      int rc = 0;
      mu_header_t hdr = NULL;
      char *sender;
      mu_message_get_header (msg, &hdr);

      if (mu_header_aget_value_unfold (hdr, MU_HEADER_FROM, &sender) == 0)
	{
	  if (strstr (sender, sender_option))
	    rc = 1;
	  free (sender);
	}
      
      return rc;
    }
  
  return 1;
}

int
main (int argc, char **argv)
{
  int c;
  size_t total;
  
  /* Native Language Support */
  MU_APP_INIT_NLS ();

  /* register the formats.  */
  mu_register_all_mbox_formats ();
#ifdef WITH_TLS
  mu_gocs_register ("tls", mu_tls_module_init);
#endif

  mu_argp_init (NULL, NULL);
  if (mu_app_init (&argp, capa, NULL, argc, argv, 0, &c, NULL))
    exit (1);

  if (argc - c > 1)
    {
      mu_error (_("too many arguments"));
      exit (1);
    }
  else if (argc - c > 0)
    {
      if (mailbox_name)
	{
	  mu_error (_("both --from option and user name are specified"));
	  exit (1);
	}

      mailbox_name = xmalloc (strlen (argv[c]) + 2);
      mailbox_name[0] = '%';
      strcpy (mailbox_name + 1, argv[c]);
    }

  init_output (0);
  
  frm_scan (mailbox_name, from_select, &total);

  if (count_only)
    {
      printf (ngettext ("There is %lu message in your incoming mailbox.\n",
			"There are %lu messages in your incoming mailbox.\n",
			total),
	      (unsigned long) total);
    }
  return 0;
}
  

Return to:

Send suggestions and report system problems to the System administrator.