 # Commands:
 #   hubot notify me by prowl with YOUR_PROWL_API_KEY
 #   hubot notify me by nma with YOUR_NMA_API_KEY
+#   hubot notify me by sms with 15556667890
 #   hubot list notifiers
 #
 # Author:
-#   marten
-
+#   marten, darvin
+#
+https = require "https"
 Prowl = require "prowler"
 QS = require "querystring"
 
 module.exports = (robot) ->
-  robot.hear /@(\w+)/i, (msg) ->
-    sender   = msg.message.user.name.toLowerCase()
-    username = msg.match[1].toLowerCase()
+  sendSms = (to, msg) ->
+    sid   = process.env.HUBOT_SMS_SID
+    tkn   = process.env.HUBOT_SMS_TOKEN
+    from  = process.env.HUBOT_SMS_FROM
+    auth  = 'Basic ' + new Buffer(sid + ':' + tkn).toString("base64")
+    data  = QS.stringify
+      From: from
+      To: to
+      Body: "#{msg.user.name}: #{msg.message.text}"
+    unless sid
+      msg.send "Twilio SID isn't set."
+      msg.send "Please set the HUBOT_SMS_SID environment variable."
+      return
+
+    unless tkn
+      msg.send "Twilio token isn't set."
+      msg.send "Please set the HUBOT_SMS_TOKEN environment variable."
+      return
+
+    unless from
+      msg.send "Twilio from number isn't set."
+      msg.send "Please set the HUBOT_SMS_FROM environment variable."
+      return
+
+    msg.http("https://api.twilio.com")
+      .path("/2010-04-01/Accounts/#{sid}/SMS/Messages.json")
+      .header("Authorization", auth)
+      .header("Content-Type", "application/x-www-form-urlencoded")
+      .post(data) (err, res, body) ->
+        json = JSON.parse body
+        switch res.statusCode
+          when 201
+            msg.send "Sent sms to #{to}"
+          when 400
+            msg.send "Failed to send. #{json.message}"
+          else
+            msg.send "Failed to send."
+    
+
+
+  notify = (username, msg) ->
     notifies = []
+    console.error "Going notify #{username}"
 
     if username == "all" or username == "everyone"
       for username, apikey of robot.brain.data.notifiers
-        unless username == sender
+        unless username.toLowerCase() == msg.message.user.name.toLowerCase()
           notifies.push apikey
-    else if apikey = robot.brain.data.notifiers[username]
+    else if apikey = robot.brain.data.notifiers[username.toLowerCase()]
       notifies.push apikey
 
     for notifier in notifies
       [protocol, apikey...] = notifier.split(':')
       apikey = apikey.join('')
-      #msg.send("Notified #{protocol} by #{apikey}")
+      msg.send("Notified #{protocol} by #{apikey}")
 
       switch protocol
         when "prowl"

            .get() (err, res, body) ->
              body


  robot.respond /notify me by prowl with (\w+)/i, (msg) ->
    apikey = msg.match[1].toLowerCase()

            application: 'RoQua Hubot'
            event: 'Mention'
            description: msg.message.text
        when "sms"
          console.error "Sending sms"
          sendSms apikey, msg
        
        when "nma"
          params =
            apikey: apikey
            application: "Hubot"
            event: "Mention"
