Blame view

examples/addr.c 4.42 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2010 Free Software
   Foundation, Inc.
4

5
   GNU Mailutils is free software; you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; either version 3, or (at your option)
8 9
   any later version.

10
   GNU Mailutils is distributed in the hope that it will be useful,
11 12 13 14 15
   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
16
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
17

18 19 20
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
Alain Magloire authored
21
#include <stdio.h>
Sergey Poznyakoff authored
22
#include <string.h>
Wojciech Polak authored
23
#include <errno.h>
Sam Roberts authored
24

Alain Magloire authored
25
#include <mailutils/address.h>
Sam Roberts authored
26
#include <mailutils/errno.h>
27
#include <mailutils/kwd.h>
28
#include <mailutils/util.h>
Alain Magloire authored
29

30
#define EPARSE MU_ERR_NOENT
Alain Magloire authored
31

Sergey Poznyakoff authored
32
struct mu_address hint;
33 34
int hflags;

Sam Roberts authored
35 36
static int
parse (const char *str)
Alain Magloire authored
37
{
Sam Roberts authored
38 39 40
  size_t no = 0;
  size_t pcount = 0;
  int status;
41
  const char *buf;
42
  mu_address_t address = NULL;
Alain Magloire authored
43

44
  status = mu_address_create_hint (&address, str, &hint, hflags);
45
  mu_address_get_count (address, &pcount);
Alain Magloire authored
46

Sam Roberts authored
47 48
  if (status)
    {
Sergey Poznyakoff authored
49
      printf ("%s=> error %s\n\n", str, mu_errname (status));
Sam Roberts authored
50 51 52 53
      return 0;
    }
  else
    {
Sergey Poznyakoff authored
54
      printf ("%s=> pcount %lu\n", str, (unsigned long) pcount);
Alain Magloire authored
55
    }
Alain Magloire authored
56

Sam Roberts authored
57 58 59
  for (no = 1; no <= pcount; no++)
    {
      int isgroup;
Alain Magloire authored
60

61
      mu_address_is_group (address, no, &isgroup);
62
      printf ("%lu ", (unsigned long) no);
Alain Magloire authored
63

Sam Roberts authored
64
      if (isgroup)
65
        {
66
          mu_address_sget_personal (address, no, &buf);
67 68
          printf ("group <%s>\n", buf);
        }
Sam Roberts authored
69
      else
70
        {
71
          mu_address_sget_email (address, no, &buf);
72 73
          printf ("email <%s>\n", buf);
        }
Alain Magloire authored
74

75
      if (mu_address_sget_personal (address, no, &buf) == 0 && buf && !isgroup)
76
        printf ("   personal <%s>\n", buf);
Alain Magloire authored
77

78
      if (mu_address_sget_comments (address, no, &buf) == 0 && buf)
79
        printf ("   comments <%s>\n", buf);
Alain Magloire authored
80

81
      if (mu_address_sget_local_part (address, no, &buf) == 0 && buf)
82 83
        {
          printf ("   local-part <%s>", buf);
Alain Magloire authored
84

85
          if (mu_address_sget_domain (address, no, &buf) == 0 && buf)
86
            printf (" domain <%s>", buf);
87

88 89
          printf ("\n");
        }
Alain Magloire authored
90

91
      if (mu_address_sget_route (address, no, &buf) == 0 && buf)
92
        printf ("   route <%s>\n", buf);
93
    }
94
  mu_address_destroy (&address);
Alain Magloire authored
95

Sam Roberts authored
96 97
  printf ("\n");
  return 0;
Alain Magloire authored
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
struct mu_kwd hintnames[] = {
  { "comments", MU_ADDR_HINT_COMMENTS },
  { "personal", MU_ADDR_HINT_PERSONAL },
  { "email", MU_ADDR_HINT_EMAIL },
  { "local", MU_ADDR_HINT_LOCAL },
  { "domain", MU_ADDR_HINT_DOMAIN },
  { "route", MU_ADDR_HINT_ROUTE },
  { NULL }
};

static char **
addr_fieldptr_by_mask (mu_address_t addr, int mask)
{
  switch (mask)						
    {
    case MU_ADDR_HINT_ADDR:
      return &addr->addr;
	  
    case MU_ADDR_HINT_COMMENTS:				
      return &addr->comments;					
	  
    case MU_ADDR_HINT_PERSONAL:				
      return &addr->personal;					

    case MU_ADDR_HINT_EMAIL:
      return &addr->email;

    case MU_ADDR_HINT_LOCAL:
      return &addr->local_part;
      
    case MU_ADDR_HINT_DOMAIN:				
      return &addr->domain;					

    case MU_ADDR_HINT_ROUTE:
      return &addr->route;
    }
  return NULL;
}							

void
sethint (char *str)
{
  int mask;
  char *p = strchr (str, '=');

  if (!p)
    {
      printf ("%s=> bad assignment\n\n", str);
      return;
    }
  *p++ = 0;
  if (mu_kwd_xlat_name (hintnames, str, &mask) == 0)
    {
      char **fptr = addr_fieldptr_by_mask (&hint, mask);

      if (*p == 0)
	hflags &= ~mask;
      else
	{
	  *fptr = strdup (p);
	  hflags |= mask;
	}
    }
  else
    printf ("%s=> unknown hint name\n\n", str);
}
	
Sam Roberts authored
167 168
static int
parseinput (void)
Alain Magloire authored
169
{
Sam Roberts authored
170
  char buf[BUFSIZ];
Alain Magloire authored
171

Sam Roberts authored
172 173 174
  while (fgets (buf, sizeof (buf), stdin) != 0)
    {
      buf[strlen (buf) - 1] = 0;
175 176 177 178
      if (buf[0] == '\\')
	sethint (buf + 1);
      else
	parse (buf);
Alain Magloire authored
179
    }
Alain Magloire authored
180

Sam Roberts authored
181
  return 0;
Alain Magloire authored
182 183
}

Sam Roberts authored
184
int
185
main (int argc, char *argv[])
Alain Magloire authored
186
{
187 188 189 190 191 192
  int i;
  
  hint.domain = "localhost";
  hflags = MU_ADDR_HINT_DOMAIN;
  
  if (argc == 1)
Wojciech Polak authored
193
    return parseinput ();
194 195
  
  for (i = 1; i < argc; i++)
Sam Roberts authored
196
    {
197
      if (strcmp (argv[i], "-") == 0)
Wojciech Polak authored
198
	parseinput ();
199 200
      else if (strncmp (argv[i], "-v", 2) == 0)
	sethint (argv[i] + 2);
Sam Roberts authored
201
      else
202
	parse (argv[i]);
Alain Magloire authored
203
    }
Alain Magloire authored
204

Sam Roberts authored
205
  return 0;
Alain Magloire authored
206
}