# documentation: http://wiki.developers.facebook.com/index.php/Category:API_functions

package require rest
package require tls
::http::register https 443 [list ::tls::socket]
package require md5

set facebook(auth.createToken) {
    description {Creates an auth_token to be passed in as a parameter to
                 loginLink and then to auth.getSession after the user has
                 logged in. The user must log in soon after you create this
                 token. }
    url http://api.facebook.com/restserver.php
    method post
    auth { sign sign }
    req_args { api_key: secret: }
    static_args { v 1.0 format json method Auth.createToken }
    check_result { {} {[string match "\{error_code*" $result]} }
    post_transform { return [string trim $result \"] }
}

set facebook(auth.getSession) {
    url https://api.facebook.com/restserver.php
    method post
    auth { sign sign }
    req_args { api_key: auth_token: secret: }
    static_args { v 1.0 format json method Auth.getSession }
    check_result { {} {[string match "\{error_code*" $result]} }
}

set facebook(friends.get) {
    url http://api.facebook.com/restserver.php
    auth { sign sign }
    req_args { api_key: secret: call_id: }
    opt_args { session_key: flid: uid: } 
    static_args { v 1.0 format json method Friends.get }
    post_transform { return [split [string trim $result \[\]] ,] }
    check_result { {} {[string match "\{error_code*" $result]} }
}

set facebook(users.getInfo) {
    url http://api.facebook.com/restserver.php
    auth { sign sign }
    req_args { api_key: secret: call_id: uids: fields: }
    opt_args { session_key: } 
    static_args { v 1.0 format json Users.getInfo }
    check_result { {} {[string match "\{error_code*" $result]} }
}

set facebook(users.setStatus) {
    url http://api.facebook.com/restserver.php
    auth { sign sign }
    req_args { api_key: secret: call_id: }
    opt_args { session_key: status: clear: status_includes_verb: uid: } 
    static_args { v 1.0 format json Users.setStatus }
    check_result { {} {[string match "\{error_code*" $result]} }
}

set facebook(groups.get) {
    url http://api.facebook.com/restserver.php
    auth { sign sign }
    req_args { api_key: secret: session_key: call_id: }
    opt_args { gids: uid: } 
    static_args { v 1.0 format json method Groups.get }
    check_result { {} {[string match "\{error_code*" $result]} }
}

set facebook(notifications.get) {
    url http://api.facebook.com/restserver.php
    auth { sign sign }
    req_args { api_key: secret: session_key: call_id: }
    static_args { v 1.0 format json method Notifications.get }
    check_result { {} {[string match "\{error_code*" $result]} }
}

rest::create_interface facebook

proc ::facebook::sign {query} {
    set str ""
    set secret [dict get $query secret]
    set query [dict remove $query secret]
    foreach x [lsort [dict keys $query]] {
        append str $x=[dict get $query $x]
    }
    append str $secret
    dict append query sig [string tolower [md5::md5 -hex $str]]
    return $query
}

proc ::facebook::loginLink {args} {
    set query [lindex [::rest::parse_opts {} {api_key: auth_token:} {} $args] 0]
    return http://www.facebook.com/login.php?api_key=[dict get $query api_key]&v=1.0&auth_token=[dict get $query auth_token]
}
