summaryrefslogtreecommitdiff
path: root/src/wildcard.cpp
blob: 47fbaf0024db42902082b2f9373a11956c2533e7 (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
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
 *                       E-mail:
 *                <brain@chatspike.net>
 *           	  <Craig@chatspike.net>
 *     
 * Written by Craig Edwards, Craig McLure, and others.
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

#include <string>
#include "inspircd_config.h"
#include "inspircd.h"
#include "inspstring.h"

void Delete(char* str,int pos)
{
	char moo[MAXBUF];
	strlcpy(moo,str,MAXBUF);
	moo[pos] = '\0';
	strlcpy(str,moo,MAXBUF);
	strlcat(str,moo+pos+1,MAXBUF);
}

void Insert(char* substr,char* str,int pos)
{
	std::string a = str;
	a.insert(pos,substr);
	strlcpy(str,a.c_str(),MAXBUF);
}


int MWC = 0;

bool match2(char* literal,char* mask)
{

char OldM[MAXBUF];
int I,I2;

if (MWC)
	return true;

if ((strstr(mask,"*")==0) && (strlen(literal) != strlen(mask)))
	return 0;
 I=0;
 I2=0;
 while (I < strlen(mask))
 {
   if (I2 >= strlen(literal))
	   return 0;
 
   if ((mask[I]=='*') && (MWC==0))
   {
     strlcpy(OldM,mask,MAXBUF);
     
     Delete(mask,I);
     
     while (strlen(mask)<255)
     {
       match2(literal,mask);
       if (MWC==2)
	       return 1;

       Insert("?",mask,I);
     }
     strlcpy(mask,OldM,MAXBUF);
     Delete(mask,I);
     Insert("?",mask,I);
   }
   if (mask[I]=='?')
   {
     I++;
     I2++;
     continue;
   }
   if (mask[I] != literal[I2])
	   return 0;
   if (MWC)
	   return 1;
   I++;
   I2++;
 }
 if (strlen(literal)==strlen(mask))
		 MWC=2;

}

bool match(const char* literal, const char* mask)
{
	char L[10240];
	char M[10240];
	MWC = 0;
	strlcpy(L,literal,10240);
	strlcpy(M,mask,10240);
	strlower(L);
	strlower(M);
	// short circuit literals
	log(DEBUG,"Match '%s' to '%s'",L,M);
	if ((!strchr(M,'*')) && (!strchr(M,'?')))
	{
		log(DEBUG,"Short circuiting literal");
		if (!strcasecmp(L,M))
		{
			log(DEBUG,"Literal match");
			return true;
		}
	}
	match2(L,M);
	return (MWC == 2);
}