예제 #1
0
파일: ident.go 프로젝트: treeherder/glenda
func (i *IdentMod) Init(b *Bot, conn irc.SafeConn) (err error) {
	conf := b.Config.Search("mod", "ident")
	i.nick = conf.Search("nick")
	if i.nick == "" {
		i.nick = b.IrcConfig.Nick
	}
	i.pass = conf.Search("pass")

	if i.pass == "" {
		log.Printf("ident: no pass")
		return nil
	}

	conn.AddHandler(irc.CONNECTED, func(c *irc.Conn, l irc.Line) {
		go func() {
			// curse you freenode NickServ..
			time.Sleep(2 * time.Second)
			c.Privmsg("nickserv", fmt.Sprintf("identify %s %s", i.nick, i.pass))
			log.Printf("ident: identified")
			time.Sleep(2 * time.Second)
			c.Privmsg("nickserv", fmt.Sprintf("regain %s", i.nick))
			log.Printf("ident: regained")
		}()
	})

	return nil
}
예제 #2
0
파일: stat.go 프로젝트: treeherder/glenda
func (m *StatMod) Init(b *Bot, conn irc.SafeConn) (err error) {
	conf := b.Config.Search("mod", "stat")

	m.db, err = sql.Open("sqlite3", conf.Search("path"))

	if err != nil {
		log.Printf("stat module failed to open %q: %s\n", conf.Search("path"), err)
		return
	}

	for _, t := range tables {
		_, err = m.db.Exec(t)

		if err != nil {
			log.Printf("stat module failed to create table: %s\n%q\n", err, t)
			return
		}
	}

	b.Hook("stat", func(b *Bot, sender, cmd string, args ...string) error {
		if len(args) != 1 {
			return fmt.Errorf("not enough arguments")
		}

		s, err := m.stat(args[0])

		if err != nil {
			return fmt.Errorf("stat failed for %q: %s", args[0], err)
		}

		if s == nil {
			return fmt.Errorf("no such user: %s", args[0])
		}

		b.Conn.Privmsg(sender, s.String())
		return nil
	})

	conn.AddHandler("PRIVMSG", func(c *irc.Conn, l irc.Line) {
		if err := m.update(l.Src.String(), l.Args[1]); err != nil {
			log.Printf("update failed for %q, %q: %s",
				l.Src.String(),
				l.Args[1:],
				err,
			)
		}
	})

	conn.AddHandler(irc.ACTION, func(c *irc.Conn, l irc.Line) {
		if err := m.action(l.Src.String()); err != nil {
			log.Printf("action update failed for %q: %s",
				l.Src.String(),
				err,
			)
		}
	})

	log.Println("stat module initialised")
	return nil
}
예제 #3
0
파일: notify.go 프로젝트: treeherder/glenda
func (m *NotifyMod) Init(b *Bot, conn irc.SafeConn) error {
	m.notes = make(map[string][]Note)

	conn.AddHandler("PRIVMSG", func(con *irc.Conn, line irc.Line) {
		args := strings.SplitN(line.Args[1], " ", 3)

		if len(args) == 3 && args[0] == ".notify" {

			note := Note{
				from:    line.Src.String(),
				message: args[2],
				sent:    time.Now(),
			}

			m.notes[args[1]] = append(m.notes[args[1]], note)

			conn.Privmsg(getContext(line), fmt.Sprintf("added note to %q: %q", args[1], note))
		}
	})

	notify := func(conn *irc.Conn, line irc.Line) {
		m.NotifyIfQueued(conn, line)
	}

	conn.AddHandler("PRIVMSG", notify)
	conn.AddHandler("JOIN", notify)

	return nil
}
예제 #4
0
파일: markov.go 프로젝트: treeherder/glenda
func (m *MarkovMod) Init(b *Bot, conn irc.SafeConn) error {
	//conf := b.Config.Search("mod", "markov")

	c, err := NewChain(filepath.Join(b.DataDir, "markov"))
	if err != nil {
		return fmt.Errorf("error opening db: %s", err)
	}

	m.chain = c

	generate := func() string {
		return m.chain.Generate(rand.Intn(10) + 10)
	}

	b.Hook("markov", func(b *Bot, sender, cmd string, args ...string) error {
		b.Conn.Privmsg(sender, generate())
		return nil
	})

	conn.AddHandler("PRIVMSG", func(c *irc.Conn, l irc.Line) {
		if strings.HasPrefix(l.Args[0], b.Magic) {
			return
		}

		getAddressee := func(line string) string {
			if s := strings.SplitN(line, " ", 2); len(s) == 2 {

				t := rune(s[0][len(s[0])-1])

				if strings.ContainsRune(":,", t) {
					return s[0][:len(s[0])-1]
				}
			}

			return ""
		}

		if addressee := getAddressee(l.Args[1]); addressee != "" {
			if addressee == c.Me().String() {
				c.Privmsg(l.Args[0], generate())
			}
		} else {
			m.chain.Build(strings.NewReader(l.Args[1]))
		}
	})

	log.Printf("markov module initialized")
	return nil
}
예제 #5
0
파일: wtmp.go 프로젝트: treeherder/glenda
func (w *WtmpMod) Init(b *Bot, conn irc.SafeConn) error {
	conf := b.Config.Search("mod", "wtmp")
	w.file = conf.Search("file")
	w.last = time.Now()
	w.cron = cron.New()
	w.on = make(map[string]string)

	channel := conf.Search("channel")

	w.cron.AddFunc("@every 1m", func() {
		//log.Printf("checking wtmp %s...", w.file)

		wtmps, err := wtmp(w.file)
		if err != nil {
			log.Printf("error checking wtmp: %s", err)
			return
		}

		for _, wtr := range wtmps {
			if wtr.name != "" {
				w.on[wtr.line] = wtr.name
			}

			if w.last.Before(wtr.date) {
				log.Printf("wtmp: %q %q %q %q", wtr.line, wtr.name, wtr.host, wtr.date)

				in := "in "
				if wtr.name == "" {
					in = "out"
					wtr.name = w.on[wtr.line]
				}
				conn.Privmsg(channel, fmt.Sprintf("log%s: %s on %s", in, wtr.name, wtr.line))
			}
		}

		w.last = time.Now()
	})

	go func() {
		time.Sleep(10 * time.Second)
		w.cron.Start()
	}()

	log.Printf("wtmp module initialized with file %s", w.file)

	return nil
}
예제 #6
0
func (m *MailwatchMod) Init(b *Bot, conn irc.SafeConn) error {
	conf := b.Config.Search("mod", "mailwatch")
	dir := conf.Search("dir")
	channel := conf.Search("channel")

	if dir != "" {
		m.dir = maildir.Dir(dir)
		m.cronjob = cron.New()

		m.cronjob.AddFunc("@every 1m", func() {
			//log.Printf("checking mail %s...", m.dir)

			if newmail, err := m.dir.Unseen(); err != nil {
				conn.Privmsg(channel, fmt.Sprintf("maildir error: %s, err"))
			} else {
				l := len(newmail)

				if l > 0 {
					conn.Privmsg(channel, fmt.Sprintf("%d new mail:", l))

					for _, k := range newmail {
						hdr, err := m.dir.Header(k)
						if err != nil {
							conn.Privmsg(channel, fmt.Sprintf("maildir header error: %s", err))
						} else {
							conn.Privmsg(channel, fmt.Sprintf("from   : %s", hdr.Get("From")))
							conn.Privmsg(channel, fmt.Sprintf("subject: %s", hdr.Get("Subject")))
						}
					}
				}
			}
		})

		go func() {
			time.Sleep(10 * time.Second)
			m.cronjob.Start()
		}()

		log.Printf("mailwatch module initialized with dir: %s", dir)
	}

	return nil
}
예제 #7
0
func (g *AdventureMod) Init(b *Bot, conn irc.SafeConn) error {
	conf := b.Config.Search("mod", "adventure")
	channel := conf.Search("channel")

	go func() {
		var err error

		time.Sleep(5 * time.Second)

		for err == nil {
			if err = g.spawn(); err != nil {
				log.Printf("adventure spawn error: %s", err)
				break
			}

			for g.out.Scan() {
				line := g.out.Text()
				log.Printf("adventure: %s", line)
				conn.Privmsg(channel, line)
			}

			if err := g.out.Err(); err != nil {
				log.Printf("adventure read error: %s", err)
			}
		}
	}()

	conn.AddHandler("PRIVMSG", func(c *irc.Conn, l irc.Line) {
		args := strings.Split(l.Args[1], " ")
		if args[0] == ".a" {
			line := strings.Join(args[1:], " ")
			log.Printf("adventure: writing %q", line)
			if _, err := fmt.Fprintf(g.in, "%s\n", line); err != nil {
				log.Printf("adventure: error writing to subprocess: %s", err)
			}
		}
	})

	log.Printf("adventure module initialized with channel %s", channel)

	return nil
}