func ldapConnFor(nick string, attrs ...string) ldap.Conn { res := ldap.Result{Attrs: []ldap.Attr{ {Name: "mozillaNickname", Values: []string{nick}}, }} for i := 0; i < len(attrs); i += 2 { res.Attrs = append(res.Attrs, ldap.Attr{Name: attrs[i], Values: []string{attrs[i+1]}}) } return ldapConn{nick, res} }
func (p *aqlPlugin) sendSMS(cmd *mup.Command, nick, message string, receiver ldap.Result) error { var content string if cmd.Channel != "" { content = fmt.Sprintf("%s %s> %s", cmd.Channel, cmd.Nick, message) } else { content = fmt.Sprintf("%s> %s", cmd.Nick, message) } // This API is documented at http://aql.com/sms/integrated/sms-api mobile := trimPhone(receiver.Value("mobile")) form := url.Values{ "username": []string{p.config.AQLUser}, "password": []string{p.config.AQLPass}, "destination": []string{mobile}, "originator": []string{"+447766404142"}, "message": []string{content}, } resp, err := httpClient.PostForm(p.config.AQLEndpoint, form) if err != nil { return err } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { return err } // Response format is "<status code>:<credits used> <description>". // For example: "2:0 Authentication error" i := bytes.IndexByte(data, ':') j := bytes.IndexByte(data, ' ') if i <= 0 || j <= i { return fmt.Errorf("AQL response not recognized.") } status := data[:i] credits := data[i+1 : j] info := data[j+1:] p.plugger.Logf("SMS delivery result: from=%s to=%s mobile=%s status=%s credits=%s info=%s", cmd.Nick, nick, mobile, status, credits, info) if len(status) == 1 && (status[0] == '0' || status[0] == '1') { p.plugger.Sendf(cmd, "SMS is on the way!") } else { p.plugger.Sendf(cmd, "SMS delivery failed: %s", info) } return nil }
func (p *ldapPlugin) formatEntry(result *ldap.Result) string { var buf bytes.Buffer buf.Grow(250) cn := result.Value("cn") nick := result.Value("mozillaNickname") if nick != "" { buf.WriteString(nick) buf.WriteString(" is ") buf.WriteString(cn) } else { buf.WriteString(cn) } for _, item := range ldapFormat { for _, value := range result.Values(item.attr) { if value == "" { continue } if item.filter != nil { value = item.filter(value) } buf.WriteByte(' ') buf.WriteString(fmt.Sprintf(item.format, value)) } } return buf.String() }