     res.reply "Twitter's oAuth has been removed"
   
   robot.router.all '/twitter/oauth(/)?', (req, res) ->
-    #/twitter/oauth?oauth_token=jpD7mAAAAAAAx3LuAAABWE9W-uY&oauth_verifier=bEU7Zs7lpXhTXsI9ajDG6FFARo3utYcH
     query = QUERY_STRING.parse(url.parse(req.url).query)
     access_token = query["oauth_token"]
     oauth_verifier = query["oauth_verifier"]

    access_token = robot.brain.get "#{user.id}:twitter:access_token"
    access_token_secret = robot.brain.get "#{user.id}:twitter:access_token_secret" 
    if access_token?
      oauth = new TwitterOAuth({consumer_key:TWITTER_CONSUMER_KEY,consumer_secret_key:TWITTER_CONSUMER_SECERT_KEY, access_token:"", access_token_secret:""})
      res.reply "I'm posting that tweet: #{tweet}"
       #tweet with oauth
    else
      robot.brain.set("#{user.id}:twitter:draft", tweet)
      oauth = new TwitterOAuth({consumer_key:TWITTER_CONSUMER_KEY, consumer_secret_key:TWITTER_CONSUMER_SECERT_KEY, callback: TWITTER_CALLBACK_URL})
      oauth.request_access_token (oa, verify_url) =>
        robot.brain.set("#{user.id}:twitter:access_token_t", oa.access_token)
        robot.brain.set("#{user.id}:twitter:access_token_secret_t", oa.access_token_secret)
        robot.brain.set("twitter:#{oa.access_token}", user)

          st.push "#{kv[0]}=#{encodeURIComponent(kv[1])}"
    st.join(joiner)

  oauth_params: (sources...)->
    options = [
      ["oauth_consumer_key" , @consumer_key],
      ["oauth_nonce", @oauth_nonce()],
      ["oauth_signature_method" , "HMAC-SHA1"],
      ["oauth_timestamp" , Math.floor(Date.now() / 1000)],
      ["oauth_version" , "1.0"],
      ["oauth_token" , @access_token]
    ]
    for source in sources
      options.push source
    options

  log:(o) ->
    console.log(o)
    
  extend_list:(obj, sources...) ->
    for source in sources
      for el in source
        obj.push el
    obj
 
  signature: (sign_data = [], url='', method = 'POST', csonly = false) ->
    sign_data = @sort_options sign_data
    sign_data = @join_options sign_data, '&', false
    sb = "#{method.toUpperCase()}&"+encodeURIComponent(url)
    sb = sb + "&" + encodeURIComponent(sign_data)
    key = @consumer_secret_key+"&"
    unless csonly
      key += @access_token_secret
    @log("do signing...")
    @log(sb)
    @log(key)
    signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(sb, key))
    signature

  post: (host, path, oauth_data, post_data, callback) ->
    options = 
      hostname : host
      host : host
      port : 443
      path : path
      method : 'POST'
      headers : 
        'Accept' : '*/*'
        'Content-Length' : if post_data? then Buffer.byteLength(post_data) else 0
        'Content-Type' : 'application/x-www-form-urlencoded'
        'Authorization' : oauth_data
    options.agent = new https.Agent(options)
    req = https.request options, (res) ->
      res.on 'data', (chunk) =>
        @log chunk+""
        callback? chunk
      res.on 'error', (e) =>
        @log e.message
    if post_data?
      req.write(post_data)
    req.end()

  authorization: (oauth_headers=[], sign) ->
    oauth_headers.push ["oauth_signature", sign]  if sign?
    oauth_headers = @sort_options oauth_headers
    oauth_data = @join_options oauth_headers, ", ", true
    oauth_data = "OAuth #{@oauth_data}"
    @log(oauth_data)
    oauth_data

  request_token: (callback) ->
    oauth_headers = @oauth_params(["oauth_callback", @callback])
    sign = @signature(@extend_list([], oauth_headers), TwitterOAuth.URL.REQUEST, "POST", true)
    oauth_data = @authorization(oauth_headers, sign)
    @post TwitterOAuth.URL.BASE, TwitterOAuth.URL.REQUEST_PATH, oauth_data, null, (chunk) =>
      @log chunk+""
      dt = QUERY_STRING.parse(chunk+"")
      @access_token_secret = dt["oauth_token_secret"]
      @access_token = dt["oauth_token"]
      callback?@, "#{TwitterOAuth.URL.AUTHORIZE}?oauth_token=#{@access_token}"

  access_token: (@access_token, @access_token_secret, @oauth_verifier, callback) ->
    oauth_headers = @oauth_params()
    post_data = [["oauth_verifier", @oauth_verifier]]
    sign = @signature(@extend_list(post_data, oauth_headers), TwitterOAuth.URL.ACCESS, "POST", false)
    oauth_data = @authorization(oauth_headers, sign)

    @post TwitterOAuth.URL.BASE, TwitterOAuth.URL.ACCESS, oauth_data, @join_options(post_data, '&', false), (chunk) =>
      @log "access_token"
      @log chunk+""
      dt = QUERY_STRING.parse(chunk+"")
      @access_token_secret = dt["oauth_token_secret"]
      @access_token = dt["oauth_token"]
      callback?@

  tweet: (t) ->
    oauth_headers = @oauth_params()
    post_data = [["status", t]]
    sign = @signature(@extend_list(post_data, oauth_headers), TwitterOAuth.URL.POST, "POST", false)
    oauth_data = @authorization(oauth_headers, sign)
    @post TwitterOAuth.URL.BASE, TwitterOAuth.URL.POST+'?'+@join_options(post_data, '&', false), oauth_data,  null, (chunk) =>
      @log "access_token"
      @log chunk+""
      callback?@

TWITTER_CONSUMER_KEY = process.env.TWITTER_CONSUMER_KEY
TWITTER_CONSUMER_SECERT_KEY = process.env.TWITTER_CONSUMER_SECERT_KEY
