about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenny Morrison <benmorrison@ttm.sh>2019-03-17 22:44:13 -0400
committerBenny Morrison <benmorrison@ttm.sh>2019-03-17 22:44:13 -0400
commit7ce80ae85d43888b0c8e01e767322f3d1f9e2628 (patch)
tree039a62245661b0744dd96b44d5cf05cc5f6b47bc
parentaacc26efd314c4dbf7e1865d5527b50d9a54876f (diff)
downloadgoofbot-7ce80ae85d43888b0c8e01e767322f3d1f9e2628.tar.gz
refactored !users, still a WIP
-rw-r--r--main.go34
1 files changed, 16 insertions, 18 deletions
diff --git a/main.go b/main.go
index ee74072..9a11c14 100644
--- a/main.go
+++ b/main.go
@@ -5,6 +5,7 @@ import (
 	"encoding/json"
 	"flag"
 	"fmt"
+	"io"
 	"io/ioutil"
 	"log"
 	"os"
@@ -132,24 +133,21 @@ func main() {
 		// respond with currently connected users
 		// TODO: prepend names with _ to avoid pings in irc
 		if strings.HasPrefix(e.Last(), "!users") {
-			users := exec.Command("who", "-q")
-			var out bytes.Buffer
-			users.Stdout = &out
-			err := users.Run()
-			if err != nil {
-				log.Fatalln("Error while running 'who -q'")
-			}
-			userlist := strings.Split(out.String(), " ")
-			var sanilist string
-			for i, e := range userlist {
-				if userlist[i] != " " {
-					userlist[i] = "_" + e
-				}
-			}
-			for i := range userlist {
-				sanilist += userlist[i]
-			}
-			c.Cmd.Reply(e, sanilist[:len(sanilist)-9])
+			who := exec.Command("who", "-q")
+			awk := exec.Command("awk", "NR==1")
+			r, w := io.Pipe()
+			who.Stdout = w
+			awk.Stdin = r
+			var bytestream bytes.Buffer
+			awk.Stdout = &bytestream
+			who.Start()
+			awk.Start()
+			who.Wait()
+			w.Close()
+			awk.Wait()
+			io.Copy(os.Stdout, &bytestream)
+
+			c.Cmd.Reply(e, bytestream.String())
 			return
 		}
 		// number of total human users on the server