Beispiel #1
0
func (s *t) snarf(conn *irc.Connection, msg *irc.Message) {
	if msg.Command != "PRIVMSG" {
		return
	}

	matches := urlPattern.FindAllString(*msg.LastParameter(), -1)
	stmt, err := s.db.Prepare(
		fmt.Sprintf("insert into %s (url, nick, datetime, rawmessage) values(?, ?, ?, ?)", tableName))
	defer stmt.Finalize()

	if err != nil {
		return
	}

	for _, match := range matches {
		stmt.Exec(match, msg.Nick, time.Seconds(), *msg.LastParameter())
		stmt.Next()
		stmt.Reset()
	}
}
Beispiel #2
0
func (s *t) seen(conn *irc.Connection, msg *irc.Message) {
	if msg.Command != "PRIVMSG" {
		return
	}

	fs := flag.NewFlagSet("seen", flag.ContinueOnError)
	fs.Usage = func() {
		conn.Privmsg(*msg.Target(), "usage: seen <nick>")
	}

	parts := msg.Split()
	if fs.Parse(parts[1:]) != nil {
		return
	}

	if fs.NArg() == 0 {
		fs.Usage()
		return
	}

	target := fs.Arg(0)
	stmt, err := s.db.Prepare(
		fmt.Sprintf("SELECT datetime, message FROM %s WHERE nick LIKE ? ORDER BY datetime desc LIMIT 1", tableName))
	defer stmt.Finalize()
	if err != nil {
		log.Printf("seen.seen(): sql error: %s", err)
		return
	}

	stmt.Exec(target)
	found := stmt.Next()
	if found {
		var datetime int64
		var rawmessage string
		stmt.Scan(&datetime, &rawmessage)
		t := time.SecondsToLocalTime(datetime)
		str := t.Format(time.UnixDate)
		conn.Privmsg(*msg.Target(), str+": "+rawmessage)
	} else {
		conn.Privmsg(*msg.Target(), "I haven't seen "+target)
	}
}
Beispiel #3
0
func (s *t) snarf(conn *irc.Connection, msg *irc.Message) {
	if msg.String() == "" {
		return
	}

	stmt, err := s.db.Prepare(
		fmt.Sprintf("select id from %s where nick like ?", tableName))
	defer stmt.Finalize()

	if err != nil {
		log.Printf("seen.snarf() 1: sql error: %s", err)
		return
	}

	stmt.Exec(msg.Nick)
	found := stmt.Next()

	if found {
		var id int
		stmt.Scan(&id)
		stmt.Reset()
		stmt, err = s.db.Prepare(
			fmt.Sprintf("update %s set datetime = ?, message = ? where id = ?", tableName))

		if err != nil {
			log.Printf("seen.snarf() 2: sql error: %s", err)
			return
		}

		stmt.Exec(time.Seconds(), msg.String(), id)
		stmt.Next()
	} else {
		stmt.Reset()
		stmt, err = s.db.Prepare(
			fmt.Sprintf("insert into %s (datetime, nick, message) values(?, ?, ?)", tableName))

		if err != nil {
			log.Printf("seen.snarf() 3: sql error: %s", err)
			return
		}

		stmt.Exec(time.Seconds(), msg.Nick, msg.String())
		stmt.Next()
	}
}
Beispiel #4
0
func (s *t) lastUrl(conn *irc.Connection, msg *irc.Message) {
	if msg.Command != "PRIVMSG" {
		return
	}

	var nFlag int
	var contextFlag string
	var fromFlag string
	var contains string = ""

	fs := flag.NewFlagSet("urls", flag.ContinueOnError)
	fs.IntVar(&nFlag, "n", 1, "number of results")
	fs.StringVar(&fromFlag, "from", "", "sender's nick contains string")
	fs.StringVar(&contextFlag, "context", "", "context of the message")
	fs.Usage = func() {
		conn.Privmsg(*msg.Target(), "usage: url [-from=<nick>] [-n=<number to display>] [-context=<string>] [<substring>]")
	}

	parts := msg.Split()
	if fs.Parse(parts[1:]) != nil {
		return
	}

	if fs.NArg() > 0 {
		contains = fs.Arg(0)
	}

	query := fmt.Sprintf(
		"SELECT url FROM %s WHERE nick LIKE ? AND url LIKE ? AND rawmessage LIKE ? ORDER BY datetime DESC LIMIT ?", tableName)
	stmt, err := s.db.Prepare(query)
	defer stmt.Finalize()
	if err != nil {
		conn.Privmsg(*msg.Target(), "sql error")
		return
	}

	stmt.Exec("%"+fromFlag+"%",
		"%"+contains+"%",
		"%"+contextFlag+"%",
		nFlag)
	more := stmt.Next()
	var output string

	if !more {
		output = "no URL matches those criterias"
	}

	left := maxResponses
	for more && left > 0 {
		var url string

		stmt.Scan(&url)
		output += url + "   "

		more = stmt.Next()
		left--
	}

	conn.Privmsg(*msg.Target(), output)

}
Beispiel #5
0
func snarf(conn *irc.Connection, msg *irc.Message) {
	if msg.Command == "PING" {
		conn.Pong(*msg.LastParameter())
	}
}