示例#1
0
文件: aql.go 项目: jmptrader/mup
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)
		}
	}
}
示例#2
0
文件: aql.go 项目: jmptrader/mup
func (p *aqlPlugin) receiveSMS(conn ldap.Conn, sms *smsMessage) {
	query := strings.TrimSpace(sms.Message)
	fields := strings.SplitN(query, " ", 2)
	for i := range fields {
		fields[i] = strings.TrimSpace(fields[i])
	}
	if len(fields) != 2 || len(fields[0]) == 0 || len(fields[1]) == 0 {
		p.plugger.Logf("Received invalid SMS message text: %q", sms.Message)
		return
	}
	target := fields[0]
	text := fields[1]

	number := trimPhone(sms.Sender)[1:]
	numberQuery := make([]byte, len(number)*2+1)
	numberQuery[0] = '*'
	for i, c := range number {
		numberQuery[i*2+1] = byte(c)
		numberQuery[i*2+2] = '*'
	}
	search := &ldap.Search{
		Filter: fmt.Sprintf("(mobile=%s)", string(numberQuery)),
		Attrs:  []string{"mozillaNickname"},
	}
	sender := sms.Sender
	results, err := conn.Search(search)
	if err != nil {
		p.plugger.Logf("Cannot search LDAP server for SMS sender: %v", err)
	} else if len(results) > 0 {
		nick := results[0].Value("mozillaNickname")
		if nick != "" {
			sender = nick
		}
	}
	msg := &mup.Message{Text: fmt.Sprintf("[SMS] <%s> %s", sender, text)}
	isChan := isChannel(target)
	if isChan {
		msg.Channel = target
	} else {
		msg.Nick = target
	}
	for _, target := range p.plugger.Targets() {
		a := target.Address()
		if (a.Nick != "" || a.Channel != "") && (a.Nick != msg.Nick || a.Channel != msg.Channel) {
			continue
		}
		msg.Account = a.Account
		p.plugger.Logf("[%s] Delivering SMS from %s (%s) to %s: %s\n", msg.Account, sender, sms.Sender, target, text)
		err = p.plugger.Send(msg)
		if err == nil && !strings.HasPrefix(sender, "+") {
			p.plugger.Sendf(msg, "Answer with: !sms %s <your message>", sender)
		}
	}
	p.tomb.Go(func() error {
		_ = p.deleteSMS(sms)
		return nil
	})
}
示例#3
0
文件: ldap.go 项目: jmptrader/mup
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. :-(")
	}
}