Ejemplo n.º 1
0
func (m *Module) AddResponse(reg *regexp.Regexp, f func(*Response), permission permissions.Permission) error {
	name := reg.String()
	wrap := func(message *irc.Message) {
		switch message.Command {
		case "PRIVMSG", "NOTICE":
			nick := message.Server.CurrentNick()
			text := strings.Join(message.Arguments[1:], " ")
			if message.Arguments[0] == nick { //direct privmsg
				if reg.MatchString(strings.Trim(text, " ")) {
					f(&Response{message, text, reg.FindStringSubmatch(text)})
				}
			} else { //asked from channel
				current, err := regexp.Compile("^" + nick + "[ ,;:]")
				if err != nil {
					log.Error("Failed to compile nick regexp: ", err)
				} else if current.MatchString(text) {
					nl := len(nick) + 1
					if len(text) > nl {
						just_text := strings.Trim(text[nl:], " ")
						if reg.MatchString(just_text) {
							f(&Response{message, text, reg.FindStringSubmatch(just_text)})
						}
					}
				}
			}
		}
	}

	if _, ok := m.handlers[name]; ok {
		return errors.New("Response with same regexp already exist's!")
	}

	m.handlers[name] = m.connection.AddHandler(wrap, permission)
	return nil
}
Ejemplo n.º 2
0
func RegexpFilter(filterReg *regexp.Regexp, data []byte) (outdata []byte) {
	if nil == filterReg {
		return data
	}

	matches := filterReg.FindAllSubmatch(data, -1)
	if nil == matches {
		log.Printf("[ERROR] failed to match filter regex, pattern %s did not match", filterReg.String())
		if *gDebug {
			log.Println("======= debug: target data =======")
			log.Println(string(data))
			log.Println("==============")
		}
		return nil
	}

	for _, match := range matches {
		for patInd, patName := range filterReg.SubexpNames() {
			switch patName {
			case PATTERN_FILTER:
				outdata = append(outdata, match[patInd]...)
			}
		}
	}

	if *gDebug {
		log.Println("======= debug: filter regex ========")
		log.Println(filterReg.String())
		log.Println("======= debug: filtered data =======")
		log.Println(string(outdata))
		log.Println("==============")
	}
	return
}
Ejemplo n.º 3
0
func GetParamRegexp(r *http.Request, param string, re *regexp.Regexp) (string, error) {
	s := GetParam(r, param)
	if !re.MatchString(s) {
		return "", fmt.Errorf(param, "Did not match regular expression %v", re.String())
	}
	return s, nil
}
Ejemplo n.º 4
0
// FIXME: too much magic, just do explicit validation of the few
//        fields we have
// verifyStructStringsAgainstWhitelist takes a struct and ensures that
// the given whitelist regexp matches all string fields of the struct
func verifyStructStringsAgainstWhitelist(s interface{}, whitelist *regexp.Regexp) error {
	// check all members of the services struct against our whitelist
	t := reflect.TypeOf(s)
	v := reflect.ValueOf(s)
	for i := 0; i < t.NumField(); i++ {

		// PkgPath means its a unexported field and we can ignore it
		if t.Field(i).PkgPath != "" {
			continue
		}
		if v.Field(i).Kind() == reflect.Ptr {
			vi := v.Field(i).Elem()
			if vi.Kind() == reflect.Struct {
				return verifyStructStringsAgainstWhitelist(vi.Interface(), whitelist)
			}
		}
		if v.Field(i).Kind() == reflect.Struct {
			vi := v.Field(i).Interface()
			return verifyStructStringsAgainstWhitelist(vi, whitelist)
		}
		if v.Field(i).Kind() == reflect.String {
			key := t.Field(i).Name
			value := v.Field(i).String()
			if !whitelist.MatchString(value) {
				return &ErrStructIllegalContent{
					Field:     key,
					Content:   value,
					Whitelist: whitelist.String(),
				}
			}
		}
	}

	return nil
}
Ejemplo n.º 5
0
func TestMatchResourceAttr(name, key string, r *regexp.Regexp) TestCheckFunc {
	return func(s *terraform.State) error {
		ms := s.RootModule()
		rs, ok := ms.Resources[name]
		if !ok {
			return fmt.Errorf("Not found: %s", name)
		}

		is := rs.Primary
		if is == nil {
			return fmt.Errorf("No primary instance: %s", name)
		}

		if !r.MatchString(is.Attributes[key]) {
			return fmt.Errorf(
				"%s: Attribute '%s' didn't match %q, got %#v",
				name,
				key,
				r.String(),
				is.Attributes[key])
		}

		return nil
	}
}
Ejemplo n.º 6
0
// NewRegexMatcher returns a new matcher that compares values against
// a regular expression. The matcher is already initialized.
//
// TODO(fabxc): refactor usage.
func NewRegexMatcher(name model.LabelName, re *regexp.Regexp) *Matcher {
	return &Matcher{
		Name:    string(name),
		Value:   re.String(),
		IsRegex: true,
		regex:   re,
	}
}
Ejemplo n.º 7
0
func (route *Route) safelyCall(args []reflect.Value, routePath *regexp.Regexp) (result []reflect.Value, err interface{}) {
	defer func() {
		if err = recover(); err != nil {
			log.Println("Handler for route", routePath.String(), "paniced with", err)
		}
	}()
	return route.handler.Call(args), err
}
Ejemplo n.º 8
0
// ResponseMatchesGeneral is an abstraction of ResponseMatches and
// ResponseMatchesInsecure that simply varies in the security of the connection
func ResponseMatchesGeneral(urlstr string, re *regexp.Regexp, secure bool) (int, string, error) {
	body := chkutil.URLToBytes(urlstr, secure)
	if re.Match(body) {
		return errutil.Success()
	}
	msg := "Response didn't match regexp"
	return errutil.GenericError(msg, re.String(), []string{string(body)})
}
Ejemplo n.º 9
0
func Regexp(p *regexp.Regexp) Validator {
	return NewFuncValidator(func(v *Value, r Requester) error {
		if !p.MatchString(v.RawString()) {
			return ErrRegexp.Err([]interface{}{v.Name(), p.String()}...)
		}
		return nil
	}, MsgRegexp.Msg(p.String()))
}
Ejemplo n.º 10
0
/*
I'm sorry, dear reader. I really am.

The problem here is to take an arbitrary regular expression and:
1. return a regular expression that is just like it, but left-anchored,
   preferring to return the original if possible.
2. determine a string literal prefix that all matches of this regular expression
   have, much like regexp.Regexp.Prefix(). Unfortunately, Prefix() does not work
   in the presence of anchors, so we need to write it ourselves.

What this actually means is that we need to sketch on the internals of the
standard regexp library to forcefully extract the information we want.

Unfortunately, regexp.Regexp hides a lot of its state, so our abstraction is
going to be pretty leaky. The biggest leak is that we blindly assume that all
regular expressions are perl-style, not POSIX. This is probably Mostly True, and
I think most users of the library probably won't be able to notice.
*/
func sketchOnRegex(re *regexp.Regexp) (*regexp.Regexp, string) {
	rawRe := re.String()
	sRe, err := syntax.Parse(rawRe, syntax.Perl)
	if err != nil {
		log.Printf("WARN(web): unable to parse regexp %v as perl. "+
			"This route might behave unexpectedly.", re)
		return re, ""
	}
	sRe = sRe.Simplify()
	p, err := syntax.Compile(sRe)
	if err != nil {
		log.Printf("WARN(web): unable to compile regexp %v. This "+
			"route might behave unexpectedly.", re)
		return re, ""
	}
	if p.StartCond()&syntax.EmptyBeginText == 0 {
		// I hope doing this is always legal...
		newRe, err := regexp.Compile(`\A` + rawRe)
		if err != nil {
			log.Printf("WARN(web): unable to create a left-"+
				"anchored regexp from %v. This route might "+
				"behave unexpectedly", re)
			return re, ""
		}
		re = newRe
	}

	// Run the regular expression more or less by hand :(
	pc := uint32(p.Start)
	atStart := true
	i := &p.Inst[pc]
	var buf bytes.Buffer
Sadness:
	for {
		switch i.Op {
		case syntax.InstEmptyWidth:
			if !atStart {
				break Sadness
			}
		case syntax.InstCapture, syntax.InstNop:
			// nop!
		case syntax.InstRune, syntax.InstRune1, syntax.InstRuneAny,
			syntax.InstRuneAnyNotNL:

			atStart = false
			if len(i.Rune) != 1 ||
				syntax.Flags(i.Arg)&syntax.FoldCase != 0 {
				break Sadness
			}
			buf.WriteRune(i.Rune[0])
		default:
			break Sadness
		}
		pc = i.Out
		i = &p.Inst[pc]
	}
	return re, buf.String()
}
Ejemplo n.º 11
0
func (rs *SearchResults) HasResultForFileAndPattern(si *SearchItem,
	pattern *regexp.Regexp) bool {
	for _, r := range rs.SearchResults {
		if pattern.String() == r.Pattern.String() && si.String() == r.File.String() {
			return true
		}
	}
	return false
}
Ejemplo n.º 12
0
func newRegexpCache(r *regexp.Regexp) *regexpCache {
	s := r.String()
	re, _ := syntax.Parse(s, syntax.Perl)
	return &regexpCache{
		re:    re,
		min:   minCap(re),
		max:   re.MaxCap(),
		cache: make(map[string]*regexp.Regexp),
	}
}
Ejemplo n.º 13
0
func literalRegexp(r *regexp.Regexp) string {
	re, _ := syntax.Parse(r.String(), syntax.Perl)
	if re.MaxCap() == 0 && re.Op == syntax.OpConcat && len(re.Sub) == 3 &&
		re.Sub[0].Op == syntax.OpBeginText &&
		re.Sub[1].Op == syntax.OpLiteral &&
		re.Sub[2].Op == syntax.OpEndText {

		return string(re.Sub[1].Rune)
	}
	return ""
}
Ejemplo n.º 14
0
// NewRegexMatcher returns a new matcher that treats value as a regular
// expression which is used for matching.
func NewRegexMatcher(name model.LabelName, re *regexp.Regexp) *Matcher {
	value := strings.TrimSuffix(strings.TrimPrefix(re.String(), "^(?:"), ")$")
	if len(re.String())-len(value) != 6 {
		// Any non-anchored regexp is a bug.
		panic(fmt.Errorf("regexp %q not properly anchored", re))
	}
	return &Matcher{
		Name:    name,
		Value:   value,
		isRegex: true,
		regex:   re,
	}
}
Ejemplo n.º 15
0
func Regexp(re *regexp.Regexp) func(v reflect.Value) error {
	return func(v reflect.Value) error {
		if v.Kind() != reflect.String {
			return fmt.Errorf("must be a string")
		}

		if !re.MatchString(v.String()) {
			return fmt.Errorf("must be %s", re.String())
		}

		return nil
	}
}
Ejemplo n.º 16
0
func NewDetectorMatcher(namePattern *regexp.Regexp, tagPatterns map[string]*regexp.Regexp, detectorBuilder DetectorBuilder) *DetectorMatcher {
	tags := make(map[string]string, len(tagPatterns))
	for tag, pattern := range tagPatterns {
		tags[tag] = pattern.String()
	}
	return &DetectorMatcher{
		NamePattern:     namePattern,
		TagPatterns:     tagPatterns,
		DetectorBuilder: detectorBuilder,
		Stats: DetectorMatcherStats{
			NamePattern: namePattern.String(),
			TagPatterns: tags,
		},
	}
}
Ejemplo n.º 17
0
// Register console commands. Registered with "command" but triggered
// as ":moduleName command". Returns an error if trigger.String() is already registered
func (self *Console) registerRegexp(trigger *regexp.Regexp, fn func(string)) error {
	self.reMut.Lock()
	defer self.reMut.Unlock()

	rStr := trigger.String()
	for _, v := range self.reTriggers {
		if v.trigger.String() == rStr {
			return fmt.Errorf("Console.RegisterRegexp(): %v is already registered", trigger)
		}
	}

	self.reTriggers = append(self.reTriggers, &reCon{trigger, fn})

	return nil
}
Ejemplo n.º 18
0
func assertCompilePattern(t *testing.T, re, exRe *regexp.Regexp, prefix, exPrefix string,
	paramsIndex, exParamsIndex map[int]int, err error) {

	if err != nil {
		t.Fatalf("unexpected error: `%v`\n", err)
	}
	if exRe == nil && re != nil || re == nil && exRe != nil || re.String() != exRe.String() {
		t.Fatalf("expected regexp `%v`, got: `%v`\n", exRe, re)
	}
	if prefix != exPrefix {
		t.Fatalf("expected prefix `%v`, got: `%v`\n", exPrefix, prefix)
	}
	if !intIntMapEq(paramsIndex, exParamsIndex) {
		t.Fatalf("expected paramsIndex `%v`, got: `%v'\n", exParamsIndex, paramsIndex)
	}
}
Ejemplo n.º 19
0
func (m *Module) RemoveResponse(reg *regexp.Regexp) error {
	name := reg.String()

	if len(m.handlers) < 0 {
		return errors.New("This module has no responses!")
	}

	kill, ok := m.handlers[name]
	if !ok {
		return errors.New("This response is not defined")
	}

	kill <- true
	delete(m.handlers, name)

	return nil
}
Ejemplo n.º 20
0
func process_page(base *url.URL, page int, body string, rxp *regexp.Regexp, ctrl chan bool) {
	say(fmt.Sprintf("Page %d (%s) - processing regexp '%s'", page, base.String(), rxp.String()))
	matches := rxp.FindAllStringSubmatch(body, -1)
	say(fmt.Sprintf("Page %d (%s) - found %d matches", page, base.String(), len(matches)))
	for i := 0; i < len(matches); i++ {
		uri_str := matches[i][1]
		this_uri, err := url.Parse(uri_str)
		if err != nil {
			log.Fatalln("Error parsing url '" + uri_str + "'!")
			continue
		}
		if !this_uri.IsAbs() {
			this_uri = base.ResolveReference(this_uri)
		}
		fmt.Println(this_uri.String())
	}
	ctrl <- true
}
Ejemplo n.º 21
0
func (route *Route) CacheCallType(routePath *regexp.Regexp) {
	handlerType := route.handler.Type()

	if (handlerType.Kind() == reflect.Ptr && handlerType.Elem().Implements(httpHandlerType)) || handlerType.Implements(httpHandlerType) {
		httpHandler := route.handler.Interface().(http.Handler)
		route.handler = reflect.ValueOf(func(res http.ResponseWriter, req *http.Request) {
			httpHandler.ServeHTTP(res, req)
		})
		route.callType = CALL_TYPE_HANDLER_FUNC
		route.argCount = 2
		return
	}

	if handlerType.Kind() != reflect.Func {
		panic("Route handlers must be a http.Handler or a func. Broken for " + routePath.String())
	}

	argCount := handlerType.NumIn()
	route.argCount = argCount
	if argCount == 0 {
		route.callType = CALL_TYPE_EMPTY
		return
	}

	firstArg := handlerType.In(0)
	if firstArg.Kind() == reflect.Ptr && firstArg.Elem() == contextType {
		if argCount > 1 {
			route.callType = CALL_TYPE_CTX_AND_PARAMS
		} else {
			route.callType = CALL_TYPE_CTX_ONLY
		}
		return
	}

	if argCount == 2 {
		secondArg := handlerType.In(1)
		if firstArg.Implements(httpResponseWriterType) && secondArg.Kind() == reflect.Ptr && secondArg.Elem() == requestType {
			route.callType = CALL_TYPE_HANDLER_FUNC
			return
		}
	}

	route.callType = CALL_TYPE_PARAMS_ONLY
}
Ejemplo n.º 22
0
func TestMatchOutput(name string, r *regexp.Regexp) TestCheckFunc {
	return func(s *terraform.State) error {
		ms := s.RootModule()
		rs, ok := ms.Outputs[name]
		if !ok {
			return fmt.Errorf("Not found: %s", name)
		}

		if !r.MatchString(rs.Value.(string)) {
			return fmt.Errorf(
				"Output '%s': %#v didn't match %q",
				name,
				rs,
				r.String())
		}

		return nil
	}
}
Ejemplo n.º 23
0
// This helper func is missing in Go 1.0 (String type)
func splitWithRegexp(s string, re *regexp.Regexp) []string {
	if len(re.String()) > 0 && len(s) == 0 {
		return []string{""}
	}
	matches := re.FindAllStringIndex(s, -1)
	strings := make([]string, 0, len(matches))
	beg := 0
	end := 0
	for _, match := range matches {
		end = match[0]
		if match[1] != 0 {
			strings = append(strings, s[beg:end])
		}
		beg = match[1]
	}
	if end != len(s) {
		strings = append(strings, s[beg:])
	}
	return strings
}
Ejemplo n.º 24
0
// ConfigureRecoverer adds a recoverer for wal records with the matching user
// id.
//
// uid: User id for the record.
//
// recoverer: Recoverer for the records with matching user id.
//
// Returns nil on success.
func (this *WriteAheadLog) ConfigureRecoverer(re *regexp.Regexp,
	recoverer wal.Recoverer) (status error) {

	this.mutex.Lock()
	defer this.mutex.Unlock()

	reStr := re.String()
	if _, ok := this.recovererMap[reStr]; ok {
		if recoverer == nil {
			delete(this.recovererMap, reStr)
			return nil
		}
		this.Errorf("recoverer for %s is already configured", reStr)
		return errs.ErrExist
	}

	this.regexpList = append(this.regexpList, re)
	this.recovererMap[reStr] = recoverer
	return nil
}
Ejemplo n.º 25
0
// Unregister trigger. Return an error if trigger.String() is not registered
func (self *Console) unregisterRegexp(trigger *regexp.Regexp) error {
	self.reMut.Lock()
	defer self.reMut.Unlock()

	rStr := trigger.String()
	for i, v := range self.reTriggers {
		if v.trigger.String() != rStr {
			continue
		}

		trigLen := len(self.reTriggers) - 1
		self.reTriggers[i] = self.reTriggers[trigLen]
		self.reTriggers[trigLen] = nil
		self.reTriggers = self.reTriggers[:trigLen]

		return nil
	}

	return fmt.Errorf("Console.UnregisterRegexp(): %v is not registered", trigger.String())
}
Ejemplo n.º 26
0
func (route *Route) CallHandler(ctx *Context, routePath *regexp.Regexp, relativePath string) {
	var args []reflect.Value
	callType := route.callType

	urlParams := routePath.FindStringSubmatch(relativePath)[1:]
	ctx.Req.URLParams = urlParams

	switch callType {
	case CALL_TYPE_HANDLER_FUNC:
		args = []reflect.Value{reflect.ValueOf(ctx.Res), reflect.ValueOf(ctx.Req.Request)}
	case CALL_TYPE_CTX_ONLY, CALL_TYPE_CTX_AND_PARAMS:
		args = append(args, reflect.ValueOf(ctx))
	}

	if callType == CALL_TYPE_PARAMS_ONLY || callType == CALL_TYPE_CTX_AND_PARAMS {
		for _, param := range urlParams {
			args = append(args, reflect.ValueOf(param))
		}
	}

	if len(args) < route.argCount {
		log.Println("Route", routePath.String(), "expects", route.argCount, "arguments but only got", len(args), ". Padding.")
		for len(args) < route.argCount {
			args = append(args, reflect.ValueOf(""))
		}
	} else if len(args) > route.argCount {
		log.Println("Route", routePath.String(), "expects", route.argCount, "arguments but got", len(args), ". Trimming.")
		args = args[:route.argCount]
	}

	result, err := route.safelyCall(args, routePath)
	if err != nil {
		ctx.Next(err)
		return
	}

	err = route.renderResult(ctx, result)
	if err != nil {
		ctx.Next(err)
	}
}
Ejemplo n.º 27
0
Archivo: util.go Proyecto: 9466/jiebago
/*
RegexpSplit split slices s into substrings separated by the expression and
returns a slice of the substrings between those expression matches.
If capturing parentheses are used in expression, then the text of all groups
in the expression are also returned as part of the resulting slice.

This function acts consistent with Python's re.split function.
*/
func RegexpSplit(re *regexp.Regexp, s string, n int) []string {
	if n == 0 {
		return nil
	}

	if len(re.String()) > 0 && len(s) == 0 {
		return []string{""}
	}

	var matches [][]int
	if len(re.SubexpNames()) > 1 {
		matches = re.FindAllStringSubmatchIndex(s, n)
	} else {
		matches = re.FindAllStringIndex(s, n)
	}
	strings := make([]string, 0, len(matches))

	beg := 0
	end := 0
	for _, match := range matches {
		if n > 0 && len(strings) >= n-1 {
			break
		}

		end = match[0]
		if match[1] != 0 {
			strings = append(strings, s[beg:end])
		}
		beg = match[1]
		if len(re.SubexpNames()) > 1 {
			strings = append(strings, s[match[0]:match[1]])
		}
	}

	if end != len(s) {
		strings = append(strings, s[beg:])
	}

	return strings
}
Ejemplo n.º 28
0
// existsRepoWithProperty is an abstraction of YumRepoExists and YumRepoURL.
// It takes a struct field name to check, and an expected value. If the expected
// value is found in the field of a repo, it returns 0, "" else an error message.
// Valid choices for prop: "URL" | "Name" | "Name"
func existsRepoWithProperty(prop string, val *regexp.Regexp, manager string) (int, string, error) {
	var properties []string
	for _, repo := range getRepos(manager) {
		switch prop {
		case "URL":
			properties = append(properties, repo.URL)
		case "Name":
			properties = append(properties, repo.Name)
		case "Status":
			properties = append(properties, repo.Status)
		case "ID":
			properties = append(properties, repo.ID)
		default:
			log.Fatal("Repos don't have the requested property: " + prop)
		}
	}
	if tabular.ReIn(val, properties) {
		return errutil.Success()
	}
	msg := "Repo with given " + prop + " not found"
	return errutil.GenericError(msg, val.String(), properties)
}
Ejemplo n.º 29
0
func (c *Checker) checkTextCore(bw *Browser, r *regexp.Regexp, f func(m [][]byte) bool, s, msg, title, body string) error {
	matchs := r.FindSubmatch([]byte(bw.Find(s).Text()))
	if len(matchs) == 3 {
		if f(matchs) {
			flg := c.Cache.GetBool("CheckText:" + s + r.String())
			if !flg {
				if !c.Silent {
					fmt.Println(msg)
				}
				c.Cache.Set("CheckText:"+s+r.String(), true)
				return c.pushNotify(title, body)
			}
		} else {
			c.Cache.Del("CheckText:" + s + r.String())
		}
	}
	return nil
}
Ejemplo n.º 30
0
Archivo: cleaner.go Proyecto: ngs/GoOse
func (this *cleaner) removeNodesRegEx(doc *goquery.Document, pattern *regexp.Regexp) *goquery.Document {
	selectors := [3]string{"id", "class", "name"}
	for _, selector := range selectors {
		naughtyList := doc.Find("*[" + selector + "]")
		cont := 0
		naughtyList.Each(func(i int, s *goquery.Selection) {
			attribute, _ := s.Attr(selector)
			if this.matchNodeRegEx(attribute, pattern) {
				cont++
				this.config.parser.removeNode(s)
			}
		})

		if this.config.debug {
			log.Printf("regExRemoveNodes %d %s elements found against pattern %s\n", cont, selector, pattern.String())
		}
	}
	return doc
}