summaryrefslogtreecommitdiffstats
path: root/modules/channels.bash
blob: 04c8a3b96c0d1899fed04e9f41850263ab5bc449 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# modules/channels.bash
#
# Provides persistant storage of channels the bot is in.
#
# Settings:
# CHANNELS_LIST: file to store channel list in


if [ -z "$IRCBOT_MODULE" ]; then
	printf "error: %s is a module for ircbot.bash and should not be run separately\n" "$0"
	exit 1
fi


channels_list=()


# joins channels in the list after connecting
on_connect() { # no args
	local ch
	channels_load
	for ch in "${channels_list[@]}"; do
		sendmsg JOIN "$ch"
	done
}

# adds a joined channel to the list
on_self_join() { # args: $1 - channel
	channels_add "$1"
}

# removes a channel from the list
on_self_kick() { # args: $1 - source, $2 - channel, $3 - kick message
	channels_remove "$2"
}

# removes a channel from the list
on_self_part() { # args: $1 - channel, $2 - part message
	channels_remove "$1"
}


channels_load() { # no args
	local line
	channels_list=()
	if [ -f "$CHANNELS_LIST" ]; then
		while IFS= read -r line || [ -n "$line" ]; do
			verbose 'loaded channel %s' "$line"
			channels_list+=("$line")
		done < "$CHANNELS_LIST"
	fi
}

channels_add() { # args: $1 - channel
	channels_load
	verbose 'adding channel %s' "$1"
	channels_list+=("$1")
	channels_dump
}

channels_remove() { # args: $1 - channel
	channels_load
	verbose 'removing channel %s' "$1"
	printf "%s\n" "${channels_list[@]}" | grep -xvF "$1" > "$CHANNELS_LIST" # XXX: ugly hack
	channels_load
}

channels_dump() {
	verbose 'dumping channel list: %s' "${channels_list[*]}"
	printf "%s\n" "${channels_list[@]}" | sort | uniq > "$CHANNELS_LIST"
}