├── .gitignore ├── LICENSE ├── README.md ├── gui.go ├── handlers.go ├── main.go ├── mlog └── mlog.go └── respite.config-sample /.gitignore: -------------------------------------------------------------------------------- 1 | respite.config 2 | respite 3 | *.log 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2019 Blacksun Research Labs 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Respite 2 | 3 | > res·pite (noun): a short period of rest or relief from something difficult or unpleasant. 4 | 5 | Read-only Slack RTM API client for spying on teams. 6 | 7 | Respite is a terminal based, read-only client for Slack’s RTM API which authenticates using auth tokens to bypass 2FA and SSO. 8 | 9 | Respite was developed to provide a useful tool for leveraging tokens acquired from the `toke_em` tool by @n0ncetonic https://github.com/n0ncetonic/toke_em 10 | 11 | ## Building 12 | 13 | Run these commands. 14 | 15 | ```bash 16 | git clone https://github.com/BlacksunLabs/respite 17 | cd respite 18 | go build 19 | go install 20 | ``` 21 | 22 | ## Using Respite 23 | 24 | Assuming respite is in your path after a `go install`, respite can be called from any where. 25 | 26 | An environment variable `SLACK_TOKEN` is expected to be set in order for respite to connect to Slack. For example: 27 | 28 | ```bash 29 | export SLACK_TOKEN=xoxs-Your-Slack-Token-Here respite --log respite.log 30 | ``` 31 | 32 | By default respite does not persist logs to disk. If you would prefer to store logs to a file provide the `--log ` flag. When using the `--log` flag all messages are written regardless of whether or not channel filtering is enabled in the client. 33 | 34 | 35 | ### Navigation 36 | - Use the up and down arrow keys to move the cursor up or down along the Channel List. 37 | 38 | ### Channel Colors 39 | - Channels colored green are public channels the user you've authenticated as is a member of 40 | - Channels colored yellow are private channels the user you've authenticated as is a member of 41 | - Channels colored cyan are DMs to the user you've authenticated as 42 | 43 | ### Hotkeys 44 | - ^C - quit 45 | - Tab - disable message filtering 46 | - Enter - enable message filtering for channel under cursor 47 | 48 | 49 | TODO : Write a better readme ... 50 | 51 | ![](https://user-images.githubusercontent.com/29786827/54484700-dae76400-4828-11e9-9d53-37111a95ebfe.png) 52 | ## Roadmap 53 | **Current stable version:** _1.0.1_ 54 | 55 | **Current dev version:** _1.0.1_ 56 | 57 | ### v1.0 58 | - Utilize legacy auth tokens to authenticate with Slack's RTM API 59 | - Receive messages posted into any channel/dm which your user has permissions to 60 | - Allow filtering of messages by channel/dm 61 | 62 | ### v1.1 63 | - Supports downloading files uploaded to team 64 | - Adds functionality to export team user profile directory 65 | - Adds function to export a channel's history 66 | -------------------------------------------------------------------------------- /gui.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Blacksun Research Labs 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | package main 15 | 16 | import ( 17 | "fmt" 18 | "log" 19 | "os" 20 | "sort" 21 | 22 | "github.com/jroimartin/gocui" 23 | "github.com/nlopes/slack" 24 | ) 25 | 26 | var _Version = "1.0" 27 | var _Tagline = "Blacksun Research Labs 2019" 28 | 29 | // ANSI Color Codes 30 | const ( 31 | // 8 Bit colors 32 | ClrReset = "\u001b[0m" 33 | ClrBlack = "\u001b[30m" 34 | ClrRed = "\u001b[31m" 35 | ClrGreen = "\u001b[32m" 36 | ClrYellow = "\u001b[33m" 37 | ClrBlue = "\u001b[34m" 38 | ClrMagenta = "\u001b[35m" 39 | ClrCyan = "\u001b[36m" 40 | ClrWhite = "\u001b[37m" 41 | 42 | // 16 Bit colors 43 | ClrBrightBlack = "\u001b[30;1m" 44 | ClrBrightRed = "\u001b[31;1m" 45 | ClrBrightGreen = "\u001b[32;1m" 46 | ClrBrightYellow = "\u001b[33;1m" 47 | ClrBrightBlue = "\u001b[34;1m" 48 | ClrBrightMagenta = "\u001b[35;1m" 49 | ClrBrightCyan = "\u001b[36;1m" 50 | ClrBrightWhite = "\u001b[37;1m" 51 | 52 | // 8 Bit Background colors 53 | BgClrBlack = "\u001b[40m" 54 | BgClrRed = "\u001b[41m" 55 | BgClrGreen = "\u001b[42m" 56 | BgClrYellow = "\u001b[43m" 57 | BgClrBlue = "\u001b[44m" 58 | BgClrMagenta = "\u001b[45m" 59 | BgClrCyan = "\u001b[46m" 60 | BgClrWhite = "\u001b[47m" 61 | 62 | // 16 Bit Background colors 63 | BgClrBrightBlack = "\u001b[40;1m" 64 | BgClrBrightRed = "\u001b[41;1m" 65 | BgClrBrightGreen = "\u001b[42;1m" 66 | BgClrBrightYellow = "\u001b[43;1m" 67 | BgClrBrightBlue = "\u001b[44;1m" 68 | BgClrBrightMagenta = "\u001b[45;1m" 69 | BgClrBrightCyan = "\u001b[46;1m" 70 | BgClrBrightWhite = "\u001b[47;1m" 71 | ) 72 | 73 | var ( 74 | curMainview = make(map[string]int) 75 | curLogview = make(map[string]int) 76 | curChanlist = make(map[string]int) 77 | ) 78 | 79 | // setCurrentViewOnTop sets the current view to the top position 80 | func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) { 81 | if _, err := g.SetCurrentView(name); err != nil { 82 | postToLog(g, fmt.Sprintf("setCurrentViewOnTop : %v", err)) 83 | return nil, err 84 | } 85 | return g.SetViewOnTop(name) 86 | } 87 | 88 | func update(g *gocui.Gui) error { 89 | return nil 90 | } 91 | 92 | // postToChat print a string to the `mainview` 93 | func postToChat(g *gocui.Gui, msg string) (err error) { 94 | if msg == "" && filterChan != "" { 95 | // Handle filtered message processing here in future 96 | postToLog(g, "[i] Message filtered") 97 | return nil 98 | } 99 | 100 | var currentView = g.CurrentView() 101 | if v, err := g.SetCurrentView("mainview"); err == nil { 102 | if v.Name() == "mainview" { 103 | v.Autoscroll = true 104 | fmt.Fprintf(v, "%v\n", msg) 105 | g.Update(update) 106 | } 107 | if currentView != nil { 108 | _, err = g.SetCurrentView(currentView.Name()) 109 | if err != nil { 110 | postToLog(g, err.Error()) 111 | } 112 | } 113 | return nil 114 | } 115 | return err 116 | } 117 | 118 | // Writes input to `logview` 119 | func postToLog(g *gocui.Gui, msg string) (err error) { 120 | var currentView = g.CurrentView() 121 | if v, err := g.SetCurrentView("logview"); err == nil { 122 | if v.Name() == "logview" { 123 | v.Autoscroll = true 124 | _, err = fmt.Fprintf(v, "%v\n", msg) 125 | if err != nil { 126 | return err 127 | } 128 | g.Update(update) 129 | } 130 | if currentView != nil { 131 | _, err = g.SetCurrentView(currentView.Name()) 132 | if err != nil { 133 | postToLog(g, err.Error()) 134 | } 135 | } 136 | return nil 137 | } 138 | return err 139 | } 140 | 141 | // addToChanview adds Slack channels to the sidebar view "`chanview`" 142 | // 143 | // - Parameters: 144 | // - g *gocui.Gui : Pointer to the parent Gui of chanview 145 | // - channels []slack.Channel : Collection of slack channels to add 146 | // 147 | // - Returns: 148 | // - err error - Any errors encountered 149 | func addToChanview(g *gocui.Gui, channels []slack.Channel) (err error) { 150 | // Small procedure that adds a channel to a a channel map 151 | // if the channel does not currently exist. 152 | // Allows processing channels by type if needed. Currently 153 | // has support for public channels, private channels, and DMs 154 | for k := range channels { 155 | if _, ok := channelMap[channels[k].ID]; !ok { 156 | channelMap[channels[k].ID] = channels[k].Name 157 | } 158 | } 159 | 160 | // Buckets for different types of Slack channels 161 | var ( 162 | publicChans []string 163 | privateChans []string 164 | imChans []string 165 | ) 166 | 167 | // Iterate through channels parameter and toss channels 168 | // into their appropriate bucket 169 | for _, name := range channels { 170 | if name.IsPrivate { 171 | privateChans = append(privateChans, name.Name) 172 | } else if name.IsChannel { 173 | publicChans = append(publicChans, name.Name) 174 | } else if name.IsIM { 175 | imChans = append(imChans, name.ID) 176 | } 177 | } 178 | 179 | // TODO: Add option to toggle between sorted and 180 | // : unsorted channel listing 181 | sort.Strings(publicChans) 182 | sort.Strings(privateChans) 183 | sort.Strings(imChans) 184 | 185 | // Set `chanlist` as primary view with focus and 186 | // update its buffer with channel listing 187 | if v, err := g.SetCurrentView("chanlist"); err == nil { 188 | if v.Name() == "chanlist" { 189 | v.Autoscroll = true 190 | 191 | for c := range publicChans { 192 | fmt.Fprintf(v, "%s%s%s\n", ClrGreen, publicChans[c], ClrReset) 193 | } 194 | for c := range privateChans { 195 | fmt.Fprintf(v, "%s%s%s\n", ClrYellow, privateChans[c], ClrReset) 196 | } 197 | for c := range imChans { 198 | fmt.Fprintf(v, "%s%s%s\n", ClrCyan, imChans[c], ClrReset) 199 | } 200 | } 201 | return nil 202 | } 203 | return err 204 | } 205 | 206 | func layout(g *gocui.Gui) error { 207 | maxX, maxY := g.Size() 208 | 209 | if v, err := g.SetView("chanlist", 0, 0, int(0.2*float32(maxX)), maxY-5); err != nil { 210 | if err != gocui.ErrUnknownView { 211 | return err 212 | } 213 | 214 | v.Title = "Channels" 215 | v.Editable = false 216 | v.Wrap = true 217 | 218 | channels, err := getConversations() 219 | if err != nil { 220 | postToLog(g, err.Error()) 221 | } 222 | addToChanview(g, channels) 223 | } 224 | 225 | if v, err := g.SetView("mainview", int(0.2*float32(maxX)), 0, maxX, maxY-5); err != nil { 226 | if err != gocui.ErrUnknownView { 227 | return err 228 | } 229 | v.Title = fmt.Sprintf("\t\tRespite v%s - %s\t\t", _Version, _Tagline) 230 | v.Editable = false 231 | v.Wrap = true 232 | v.Autoscroll = true 233 | v.Overwrite = false 234 | } 235 | if v, err := g.SetView("logview", -1, maxY-5, maxX, maxY); err != nil { 236 | if err != gocui.ErrUnknownView { 237 | return err 238 | } 239 | v.Title = "Logs" 240 | v.Editable = false 241 | v.Wrap = true 242 | v.Autoscroll = true 243 | v.Overwrite = false 244 | } 245 | return nil 246 | } 247 | 248 | func startTUI() (err error) { 249 | g, err = gocui.NewGui(gocui.Output256) 250 | if err != nil { 251 | log.Panic(err) 252 | } 253 | defer g.Close() 254 | 255 | g.Cursor = true 256 | 257 | g.SetManagerFunc(layout) 258 | 259 | if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { 260 | log.Panicln(err) 261 | } 262 | 263 | if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, globalTab); err != nil { 264 | log.Panicln(err) 265 | } 266 | 267 | if err := g.SetKeybinding("chanlist", gocui.KeyArrowDown, gocui.ModNone, arrowDown); err != nil { 268 | log.Panicln(err) 269 | } 270 | 271 | if err := g.SetKeybinding("chanlist", gocui.KeyArrowUp, gocui.ModNone, arrowUp); err != nil { 272 | log.Panicln(err) 273 | } 274 | 275 | if err := g.SetKeybinding("chanlist", gocui.KeyEnter, gocui.ModNone, chanlistEnter); err != nil { 276 | log.Panicln(err) 277 | } 278 | 279 | if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { 280 | log.Panicln(err) 281 | } else if err == gocui.ErrQuit { 282 | fmt.Printf("%s", ClrReset) 283 | g.Close() 284 | os.Exit(0) 285 | } 286 | return nil 287 | } 288 | -------------------------------------------------------------------------------- /handlers.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Blacksun Research Labs 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | package main 15 | 16 | import ( 17 | "fmt" 18 | 19 | "github.com/jroimartin/gocui" 20 | ) 21 | 22 | func quit(g *gocui.Gui, v *gocui.View) error { 23 | postToLog(g, fmt.Sprintf("Quitting ...")) 24 | return gocui.ErrQuit 25 | } 26 | 27 | func globalTab(g *gocui.Gui, v *gocui.View) error { 28 | filterChan = "" 29 | toggleOmniscient(g) 30 | return nil 31 | } 32 | 33 | func arrowDown(g *gocui.Gui, v *gocui.View) error { 34 | v.MoveCursor(0, 1, false) 35 | return nil 36 | } 37 | 38 | func arrowUp(g *gocui.Gui, v *gocui.View) error { 39 | v.MoveCursor(0, -1, false) 40 | return nil 41 | } 42 | 43 | func chanlistEnter(g *gocui.Gui, v *gocui.View) error { 44 | cursorX, cursorY := v.Cursor() 45 | chanWanted, err := v.Word(cursorX, cursorY) 46 | if err != nil { 47 | postToLog(g, err.Error()) 48 | return err 49 | } 50 | filterChan = chanWanted 51 | toggleOmniscient(g) 52 | return nil 53 | } 54 | 55 | func toggleOmniscient(g *gocui.Gui) { 56 | mainview, err := g.View("mainview") 57 | if err != nil { 58 | postToLog(g, err.Error()) 59 | } 60 | var title string 61 | if filterChan == "" { 62 | postToLog(g, "Disabling channel filter") 63 | title = fmt.Sprintf("\t\tRespite v%s - %s\t\t", _Version, _Tagline) 64 | } else { 65 | title = fmt.Sprintf("\t\tRespite v%s - %s\t\t(%s)\t", _Version, _Tagline, filterChan) 66 | postToLog(g, fmt.Sprintf("Enabling channel filter on %s", filterChan)) 67 | } 68 | mainview.Title = fmt.Sprintf("%s", title) 69 | } 70 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Blacksun Research Labs 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | package main 15 | 16 | import ( 17 | "flag" 18 | "fmt" 19 | "io/ioutil" 20 | "log" 21 | "os" 22 | "strconv" 23 | "strings" 24 | "time" 25 | 26 | "github.com/BlacksunLabs/respite/mlog" 27 | "github.com/jroimartin/gocui" 28 | "github.com/nlopes/slack" 29 | ) 30 | 31 | var logger *log.Logger 32 | var api *slack.Client 33 | var g *gocui.Gui 34 | 35 | // Collections of channels, users, private messages, etc. 36 | // Useful for lookups when converting between Slack's 37 | // internally referenced object ID and its "human-friendly" 38 | // representation which are familiar to users. 39 | var ( 40 | // channelMap contains name:ID of public and private channels 41 | channelMap = make(map[string]string) 42 | // userMap contains ID:Name of users 43 | userMap = make(map[string]string) 44 | ) 45 | 46 | var filterChan = "" 47 | 48 | func stripTS(ts string) string { 49 | return strings.Split(ts, ".")[0] 50 | } 51 | 52 | func getNameForUserID(id string) (username string, err error) { 53 | if _, ok := userMap[id]; ok { 54 | return userMap[id], nil 55 | } 56 | return "", fmt.Errorf("failed to map ID %s to a username %v", id, err) 57 | } 58 | 59 | func getNameForChanID(id string) (chanName string, err error) { 60 | channel, err := api.GetChannelInfo(id) 61 | if err != nil { 62 | postToLog(g, fmt.Sprintf("failed to get channel info for channel id %s : %v", id, err)) 63 | return "", err 64 | } 65 | return channel.Name, nil 66 | } 67 | 68 | // messageFormatHumanReadable normalizes messages sent from Slack's 69 | // RTM API in preparation for displaying to the user 70 | func messageFormatHumanReadable(msg slack.Msg) (hrMsg string) { 71 | var username string 72 | user, err := api.GetUserInfo(msg.User) 73 | if err != nil { 74 | postToLog(g, fmt.Sprintf("failed to get user info from user id %s : %v", msg.User, err)) 75 | username = "" 76 | } else { 77 | username, err = getNameForUserID(user.ID) 78 | if err != nil { 79 | username = "" 80 | } 81 | } 82 | 83 | channel, err := getNameForChanID(msg.Channel) 84 | if err != nil { 85 | postToLog(g, fmt.Sprintf("failed to get channel info from channel id %s : %v", msg.Channel, err)) 86 | } 87 | 88 | text := msg.Text 89 | 90 | ts := stripTS(msg.Timestamp) 91 | tsInt64, err := strconv.ParseInt(ts, 10, 64) 92 | if err != nil { 93 | postToLog(g, fmt.Sprintf("failed to convert timestamp to Int64: %v", err)) 94 | } 95 | 96 | ut := time.Unix(tsInt64, 0) 97 | 98 | team, err := api.GetTeamInfo() 99 | if err != nil { 100 | postToLog(g, err.Error()) 101 | } 102 | logger.Printf("[%s] %s.slack.com #%s| [%s]> %s\n", ut, team.Domain, channel, username, text) 103 | 104 | if filterChan == "" { 105 | hrMsg = fmt.Sprintf("[%s] #%s| [%s]> %s", ut, channel, username, text) 106 | } else if filterChan == channel { 107 | hrMsg = fmt.Sprintf("[%s] [%s]> %s", ut, username, text) 108 | } else { 109 | hrMsg = "" 110 | } 111 | return hrMsg 112 | } 113 | 114 | func getConversations() ([]slack.Channel, error) { 115 | var channels []slack.Channel 116 | var params = slack.GetConversationsParameters{Types: strings.Fields("private_channel public_channel im")} 117 | 118 | channelsAll, _, err := api.GetConversations(¶ms) 119 | if err != nil { 120 | postToLog(g, err.Error()) 121 | } 122 | for i, j := range channelsAll { 123 | if j.IsMember { 124 | channels = append(channels, channelsAll[i]) 125 | } else if j.IsIM { 126 | channels = append(channels, channelsAll[i]) 127 | } 128 | } 129 | return channels, nil 130 | } 131 | 132 | func mapUsernamesToID(g *gocui.Gui) { 133 | users, err := api.GetUsers() 134 | if err != nil { 135 | postToLog(g, err.Error()) 136 | } 137 | for _, u := range users { 138 | userMap[u.ID] = u.Name 139 | } 140 | } 141 | 142 | var flagLogfile string 143 | 144 | func init() { 145 | flag.StringVar(&flagLogfile, "log", "", "path to log file") 146 | flag.Parse() 147 | } 148 | 149 | func main() { 150 | if flagLogfile != "" { 151 | logger = mlog.Init(flagLogfile) 152 | } else { 153 | logger = log.New(ioutil.Discard, "", 0) 154 | log.Println("Logging disabled") 155 | } 156 | api = slack.New( 157 | os.Getenv("SLACK_TOKEN"), 158 | slack.OptionLog(log.New(os.Stdout, "respite: ", log.Lshortfile|log.LstdFlags)), 159 | ) 160 | rtm := api.NewRTM() 161 | go startTUI() 162 | go rtm.ManageConnection() 163 | 164 | mapUsernamesToID(g) 165 | 166 | for msg := range rtm.IncomingEvents { 167 | switch ev := msg.Data.(type) { 168 | case *slack.HelloEvent: 169 | // Ignored 170 | 171 | case *slack.ConnectedEvent: 172 | msg := fmt.Sprintf("Connected to %s (%s.slack.com) as user %s", ev.Info.Team.Name, ev.Info.Team.Domain, ev.Info.User.Name) 173 | postToLog(g, msg) 174 | 175 | case *slack.MessageEvent: 176 | if ev.Msg.Upload { 177 | // Might handle this specially later on 178 | continue 179 | } 180 | msg := messageFormatHumanReadable(ev.Msg) 181 | postToChat(g, msg) 182 | 183 | case *slack.RTMError: 184 | postToLog(g, fmt.Sprintf("error: %s", ev.Error())) 185 | 186 | case *slack.InvalidAuthEvent: 187 | log.Panicf("Invalid credentials!") 188 | return 189 | 190 | default: 191 | // Ignored 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /mlog/mlog.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Blacksun Research Labs 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | package mlog 15 | 16 | import ( 17 | "log" 18 | "os" 19 | ) 20 | 21 | // Message logs messagse from slack 22 | var message *log.Logger 23 | 24 | // Init initializes a logger which outputs to a file 25 | func Init(logfile string) *log.Logger { 26 | 27 | f, err := os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) 28 | if err != nil { 29 | log.Println(err) 30 | } 31 | 32 | message = log.New(f, "", 0) 33 | return message 34 | } 35 | -------------------------------------------------------------------------------- /respite.config-sample: -------------------------------------------------------------------------------- 1 | SLACK_TOKEN= --------------------------------------------------------------------------------