forked from jasonroman/eggdrop-slackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.tcl
135 lines (105 loc) · 3.87 KB
/
slack.tcl
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
##############################################################################
#
# Slack Functionality
# https://slack.com/
#
# @author Jason Roman <[email protected]>
#
##############################################################################
# rest package also includes the http and json packages
package require rest
package require tls
package require json::write
package require yaml
# add https support - force TLS, no longer use SSLv3 (POODLE exploit)
http::register https 443 [list ::tls::socket -tls1 true -ssl2 false -ssl3 false]
# do not add newlines in json - keep it condensed
json::write indented 0
# define the slack namespace and all variables that must be defined and non-empty in config.yml
namespace eval slack {
namespace eval webhook {
variable url {}
variable unfurl_links {true}
}
namespace eval channel {
variable mapping {}
variable command_prefix {}
}
set optional {::slack::channel::command_prefix}
}
# processes the slack configuration file
#
# @return true|exit - 1 on success, exit on failure
proc ::slack::processConfig {} {
global SlackbotScriptDir
# load config.yml in the script's directory into the config dictionary
set config [yaml::yaml2dict -file [file join $SlackbotScriptDir config.yml]]
# loop through all config values and set their corresponding namespace variable
dict for {topLevelKey subDict} [dict get $config] {
dict for {key value} [dict get $subDict] {
# check if the key is previously defined in our namespace declaration; if so, set its value
if {[info exists ::slack::${topLevelKey}::$key]} {
set ::slack::${topLevelKey}::$key $value
}
}
}
# all required namespace parameters must be defined with a non-empty value - exit if any are missing
set missingKeys {}
# loops through each sub-namespace here (::slack::*) and every declared namespace variable
foreach {subNamespaceName} [listns slack] {
foreach {key} [listnsvars $subNamespaceName] {
# add to the missing keys if the value is blank and the key is required
if {[subst $$key] == "" && [lsearch $::slack::optional $key] == -1} {
lappend missingKeys $key
}
}
}
# notify the user of what required values are missing and stop execution
if {[llength $missingKeys]} {
puts "Undefined configuration variables: \n[join $missingKeys \n]"
exit
}
return 1
}
# check if a mapping exists from the given irc channel to a corresponding slack channel
#
# @param string channel
# @return bool
proc ::slack::channel::mappingExists {channel} {
return [dict exists $slack::channel::mapping $channel]
}
# retrieve the name of the slack channel that correspondings to the irc channel
#
# @param string channel
# @return string|false - slack channel if exists, 0 if it does not
proc ::slack::channel::ircToSlack {channel} {
if {[::slack::channel::mappingExists $channel]} {
return [dict get $slack::channel::mapping $channel]
}
return 0
}
# check if the given irc message is a command
#
# @param string msg
# @return bool
proc ::slack::channel::isCommand {msg} {
# if the message starts with any of the specified prefixes, it is considered a command
foreach {prefix} [split $::slack::channel::command_prefix ","] {
if {[string first $prefix $msg] == 0} {
return 1
}
}
return 0
}
# process the configuration file to setup the slack parameters
::slack::processConfig
# set the REST command to push data to slack via the webhook
set slack(push) {
url $::slack::webhook::url
method post
req_args { payload: }
}
# make sure the variables are substituted with their appropriate values
set slack(push) [subst $slack(push)]
# create the interface for all slack rest commands
rest::create_interface slack