summaryrefslogtreecommitdiff
path: root/libmailutils/url/match.c
blob: 4932f45776436c7a0628995a1f94fb6ec8d46129 (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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2010 Free Software Foundation, Inc.

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

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library.  If not, see 
   <http://www.gnu.org/licenses/>. */

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

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

#include <mailutils/types.h>
#include <mailutils/cstr.h>
#include <mailutils/sys/url.h>

#define is_wildcard(s) ((s)[0] == '*' && s[1] == 0)

#define WEIGHT_SCHEME 3
#define WEIGHT_USER   4
#define WEIGHT_HOST   2
#define WEIGHT_PORT   1

int
mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *pwc)
{
  int wcnt = 0;
					      
  if (is_wildcard (ticket->scheme))
    wcnt += WEIGHT_SCHEME;
  else if (mu_c_strcasecmp (ticket->scheme, url->scheme))
    return 0;

  if (ticket->flags & MU_URL_HOST)
    {
      if (is_wildcard (ticket->host))
	wcnt += WEIGHT_HOST;
      else if (url->flags & MU_URL_HOST)
	{
	  if (mu_c_strcasecmp (ticket->host, url->host))
	    /* FIXME: Compare IP addresses */
	    return 0;
	}
      else
	return 0;
    }
  else
    wcnt += WEIGHT_HOST;

  if (ticket->flags & MU_URL_PORT)
    {
      if (is_wildcard (ticket->portstr))
	wcnt += WEIGHT_PORT;
      else if (url->port & MU_URL_PORT)
	{
	  if (ticket->port != url->port)
	    return 0;
	  else
	    wcnt += WEIGHT_PORT;
	}
    }
  else
    wcnt += WEIGHT_PORT;
  
  if (ticket->flags & MU_URL_USER)
    {
      if (is_wildcard (ticket->user))
	wcnt += WEIGHT_USER;
      
      /* If ticket has a user or pass, but url doesn't, that's OK, we were
	 looking for this info. But if url does have a user/pass, it
	 must match the ticket. */
      else if (url->flags & MU_URL_USER)
	{
	  if (strcmp (ticket->user, url->user))
	    return 0;
	}
    }
  else
    wcnt += WEIGHT_USER;
  
  /* Guess it matches. */
  if (pwc)
    *pwc = wcnt;
  return 1;
}

Return to:

Send suggestions and report system problems to the System administrator.