Skip to content

Commit

Permalink
Basic set-up for Twitter authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
akoprow committed Mar 28, 2012
1 parent 9d7e3bb commit 4d66ed5
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,7 +1,7 @@
NAME = webshell.exe NAME = webshell.exe


SRC = service.opa editor.opa config.opa login.opa webshell.opa \ SRC = service.opa editor.opa config.opa login.opa webshell.opa \
calc.opa facebook.opa search.opa dropbox.opa calc.opa facebook.opa search.opa dropbox.opa twitter.opa
SRCS = $(SRC:%=src/%) SRCS = $(SRC:%=src/%)


all: $(NAME) all: $(NAME)
Expand Down
94 changes: 94 additions & 0 deletions src/twitter.opa
@@ -0,0 +1,94 @@
// license: AGPL
// (c) MLstate, 2011, 2012
// author: Adam Koprowski

import stdlib.apis.{twitter, oauth}

database Twitter.configuration /twitter_config

type Twitter.credentials = {no_credentials}
or {string request_secret, string request_token}
or {Twitter.credentials authenticated}

module TwitterConnect
{

server config =
_ = CommandLine.filter(
{ init: void
, parsers: [{ CommandLine.default_parser with
names: ["--twitter-config"],
param_doc: "APP_KEY,APP_SECRET",
description: "Sets the application data for the associated Twitter application",
function on_param(state) {
parser {
case app_key=Rule.alphanum_string [,] app_secret=Rule.alphanum_string:
{
/twitter_config <- { consumer_key: app_key,
consumer_secret: app_secret
}
{no_params: state}
}
}
}
}]
, anonymous: []
, title: "Twitter configuration"
}
)
match (?/twitter_config) {
case {some: config}: config
default:
Log.error("webshell[config]", "Cannot read Twitter configuration (application key and/or secret key)
Please re-run your application with: --twitter-config option")
System.exit(1)
}

private TW = Twitter(config)
private TWA = OAuth(TW.oauth_params({fast}))

private redirect = "http://{Config.host}/connect/twitter"

function login(raw_token) {
error("YYY")
}

private function authenticate() {
match (TWA.get_request_token(redirect)) {
case {~error}:
Service.respond_with(<>Twitter authorization failed</>)
case {success: token}:
auth_url = TWA.build_authorize_url(token.token)
auth_state = {request_secret: token.secret, request_token: token.token}
{ response: {redirect: auth_url},
state_change: {new_state: auth_state}
}
}
}

private function tweet(state, content) {
match (state) {
case {authenticated: creds}:
Service.respond_with(<>Unimplemented...</>)
default:
authenticate()
}
}

Service.spec spec =
{ initial_state: Twitter.credentials {no_credentials},
metadata: {
id: "twitter",
description: "Managing Twitter account",
cmds: [
{ cmd: "tweet [content]", description: "Publishes a given tween" }
],
},
function parse_cmd(state) {
parser {
case "tweet" Rule.ws content=(.*) : tweet(state, Text.to_string(content))
}
}
}

}
5 changes: 4 additions & 1 deletion src/webshell.opa
Expand Up @@ -11,8 +11,9 @@ WB = WBootstrap
calc = Service.make(Calc) calc = Service.make(Calc)
search = Service.make(Search) search = Service.make(Search)
dropbox = Service.make(DropboxConnect) dropbox = Service.make(DropboxConnect)
twitter = Service.make(TwitterConnect)


shell = Shell.build([calc.handler, search.handler, dropbox.handler]) shell = Shell.build([calc.handler, search.handler, dropbox.handler, twitter.handler])


function focus(set) { function focus(set) {
Log.warning("focus", set); Log.warning("focus", set);
Expand Down Expand Up @@ -112,6 +113,8 @@ function connect(connector, raw_data) {
dispatcher = parser { dispatcher = parser {
case "/connect/facebook?" data=(.*) -> case "/connect/facebook?" data=(.*) ->
connect(FacebookConnect.login, data) connect(FacebookConnect.login, data)
case "/connect/twitter?" data=(.*) ->
connect(TwitterConnect.login, data)
case "/connect/dropbox?" data=(.*) -> case "/connect/dropbox?" data=(.*) ->
connect(DropboxConnect.login(dropbox.fun_executor), data) connect(DropboxConnect.login(dropbox.fun_executor), data)
case .* -> case .* ->
Expand Down

0 comments on commit 4d66ed5

Please sign in to comment.