func (p *aqlPlugin) handle(conn ldap.Conn, cmd *mup.Command) { var args struct{ Nick, Message string } cmd.Args(&args) search := &ldap.Search{ Filter: fmt.Sprintf("(mozillaNickname=%s)", ldap.EscapeFilter(args.Nick)), Attrs: []string{"mozillaNickname", "mobile"}, } results, err := conn.Search(search) if err != nil { p.plugger.Logf("Cannot search LDAP server: %v", err) p.plugger.Sendf(cmd, "Cannot search LDAP server: %v", err) return } if len(results) == 0 { p.plugger.Logf("Cannot find requested IRC nick in LDAP server: %q", args.Nick) p.plugger.Sendf(cmd, "Cannot find anyone with that IRC nick in the directory. :-(") return } receiver := results[0] mobile := receiver.Value("mobile") if mobile == "" { p.plugger.Sendf(cmd, "Person doesn't have a mobile phone in the directory.") } else if !strings.HasPrefix(mobile, "+") { p.plugger.Sendf(cmd, "This person's mobile number is not in international format (+NN...): %s", mobile) } else { err := p.sendSMS(cmd, args.Nick, args.Message, receiver) if err != nil { p.plugger.Logf("Error sending SMS to %s (%s): %v", args.Nick, mobile, err) p.plugger.Sendf(cmd, "Error sending SMS to %s (%s): %v", args.Nick, mobile, err) } } }
func (p *ldapPlugin) handle(conn ldap.Conn, cmd *mup.Command) { var args struct{ Query string } cmd.Args(&args) query := ldap.EscapeFilter(args.Query) search := ldap.Search{ Filter: fmt.Sprintf("(|(mozillaNickname=%s)(cn=*%s*))", query, query), Attrs: ldapAttributes, } result, err := conn.Search(&search) if err != nil { p.plugger.Logf("Cannot search LDAP server: %v", err) p.plugger.Sendf(cmd, "Cannot search LDAP server: %v", err) } else if len(result) > 1 { p.plugger.Sendf(cmd, "%s", p.formatEntries(result)) } else if len(result) > 0 { p.plugger.Sendf(cmd, "%s", p.formatEntry(&result[0])) } else { p.plugger.Sendf(cmd, "Cannot find anyone matching this. :-(") } }
func (p *alphaPlugin) ldapLocation(cmd *mup.Command) string { if p.config.LDAP == "" { p.plugger.Debugf("No LDAP server configured.") return "" } // Two generations of locCacheLen expiring after locCacheExpire. now := time.Now() oldest := now.Add(-locCacheExpire) entry, ok := p.newLoc[cmd.Nick] if ok && entry.when.After(oldest) { p.plugger.Debugf("Obtained location for %q from the new cache generation: %q", cmd.Nick, entry.loc) return entry.loc } entry, ok = p.oldLoc[cmd.Nick] if ok && entry.when.After(oldest) { p.plugger.Debugf("Obtained location for %q from the old cache generation: %q", cmd.Nick, entry.loc) p.newLoc[cmd.Nick] = entry return entry.loc } // Not in the cache. Get a connection to look it up. conn, err := p.plugger.LDAP(p.config.LDAP) if err != nil { p.plugger.Logf("Plugin configuration error: %s.", err) p.plugger.Sendf(cmd, "Plugin configuration error: %s.", err) return "" } defer conn.Close() // Search for the nick in use, and take city, state, and country. search := &ldap.Search{ Filter: fmt.Sprintf("(mozillaNickname=%s)", ldap.EscapeFilter(cmd.Nick)), Attrs: []string{"c", "l", "st"}, } loc := "" results, err := conn.Search(search) if err != nil { p.plugger.Logf("Cannot search LDAP server: %v", err) return "" } // Assemble the string as "city, state, country". if len(results) == 0 { p.plugger.Logf("Cannot find requested IRC nick in LDAP server: %q", cmd.Nick) } else { r := results[0] for _, name := range search.Attrs { if s := r.Value(name); s != "" { loc = s break } } } // Rotate the cache generations if the current one is at the limit. if len(p.newLoc) == locCacheLen { p.oldLoc = p.newLoc p.newLoc = make(map[string]locEntry) } // Cache successful positive and negative lookups. p.newLoc[cmd.Nick] = locEntry{loc, now} p.plugger.Debugf("Added location for %q to the cache: %q", cmd.Nick, loc) return loc }
func (s *S) TestEscapeFilter(c *C) { c.Assert(ldap.EscapeFilter("a\x00b(c)d*e\\f"), Equals, `a\00b\28c\29d\2ae\5cf`) c.Assert(ldap.EscapeFilter("Lučić"), Equals, `Lu\c4\8di\c4\87`) }