Ejemplo n.º 1
0
func New(b bot.Bot, isupportPlugin *isupport.Plugin, modePlugin *mode.Plugin) *Plugin {
	plugin := &Plugin{
		bot:          b,
		tbmgr:        map[string]*TemporaryBanManager{},
		isupport:     isupportPlugin,
		mode:         modePlugin,
		OldHostmasks: []string{},
	}

	modePlugin.HandleFunc("-b", func(e *mode.ModeChangeEvent) {
		if ok, _, _ := isupportPlugin.IsChannel(e.Target); !ok {
			return // not a channel
		}

		hostmask := e.Argument
		tbmgr := plugin.ensureTemporaryBanManager(e.Target)
		if ban, ok := tbmgr.Remove(hostmask); ok {
			logging.Debug("%v: %v removed the temporary ban for %v",
				e.Target, e.Nick, ban.Hostmask)
			plugin.syncBans(e.Target)
		}
	})

	b.HandleFunc("join",
		func(conn *client.Conn, line *client.Line) {
			if line.Nick != conn.Me().Nick {
				return
			}

			plugin.loadBans(line.Args[0])
			go plugin.dumpBans(line.Args[0])
		})

	return plugin
}
Ejemplo n.º 2
0
// Creates a new plugin instance.
func New(b bot.Bot, isupportPlugin *isupport.Plugin) *Plugin {
	plugin := &Plugin{
		bot:         b,
		isupport:    isupportPlugin,
		intHandlers: handlerSet(),
		fgHandlers:  handlerSet(),
		bgHandlers:  handlerSet(),
	}

	// Handle MODE
	b.HandleFunc("mode",
		func(conn *client.Conn, line *client.Line) {
			if len(line.Args) < 2 {
				return
			}

			isChannel, _, _ := isupportPlugin.IsChannel(line.Args[0])

			modes := line.Args[1]
			var action ModeChangeAction
			hasAction := false
			paramIndex := 2

			getParam := func() (retval string, ok bool) {
				if len(line.Args) <= paramIndex {
					return
				}
				retval = line.Args[paramIndex]
				ok = true
				paramIndex++
				return
			}

			knownModes, ok := isupportPlugin.Supports().ChanModes()
			if !ok {
				return // TODO use some decent defaults
			}
			for _, mode := range modes {
				switch mode {
				case '-':
					hasAction = true
					action = ModeChangeAction_Removed
				case '+':
					hasAction = true
					action = ModeChangeAction_Added
				default:
					if !hasAction {
						return // + or - must come first!
					}

					modeChange := ModeChange{
						Mode:        mode,
						Action:      action,
						HasArgument: false,
					}

					if isChannel {
						// Find the mode in ISupports
						for _, knownMode := range knownModes {
							if knownMode.Mode != mode {
								continue
							}

							switch knownMode.Type {
							case isupport.ChanModeType_List,
								isupport.ChanModeType_Setting:
								// Always has a parameter
								arg, ok := getParam()
								if !ok {
									return // invalid syntax
								}
								modeChange.Argument = arg
							case isupport.ChanModeType_Setting_ParamWhenSet:
								// Only has parameter when set
								if action == ModeChangeAction_Added {
									arg, ok := getParam()
									if !ok {
										return // invalid syntax
									}
									modeChange.Argument = arg
								}
							case isupport.ChanModeType_Setting_NoParam:
								// No parameter
							}
						}
					}

					e := &ModeChangeEvent{
						ModeChange: modeChange,

						Host:  line.Host,
						Ident: line.Ident,
						Nick:  line.Nick,
						Src:   line.Src,
						Tags:  line.Tags,

						Target: line.Target(),
					}

					// Pass event to handlers
					plugin.dispatch("*", e)
					switch action {
					case ModeChangeAction_Added:
						plugin.dispatch(string([]rune{rune('+'), mode}), e)
					case ModeChangeAction_Removed:
						plugin.dispatch(string([]rune{rune('-'), mode}), e)
					}
				}
			}
		})

	return plugin
}