forked from hbbio/webshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.opa
120 lines (108 loc) · 3.53 KB
/
twitter.opa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// license: AGPL
// (c) MLstate, 2011, 2012
// author: Adam Koprowski
import stdlib.apis.{twitter, oauth}
database Twitter.configuration /twitter_config
type Twitter.status = {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(executor)(raw_token) {
function connect(auth_data) {
Log.info("Twitter", "connection data: {raw_token}")
authentication_failed = {no_credentials}
match (auth_data) {
case ~{request_secret, request_token}:
match (TWA.connection_result(raw_token)) {
case {success: s}:
if (s.token == request_token) {
match (TWA.get_access_token(s.token, request_secret, s.verifier)) {
case {success: s}:
twitter_creds = {access_token: s.token, access_secret: s.secret}
Log.info("Twitter", "got credentials: {twitter_creds}")
{authenticated: twitter_creds}
default:
authentication_failed
}
} else
authentication_failed
default:
authentication_failed
}
default:
authentication_failed
}
}
executor(connect)
}
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}:
tweet = TW.post_status(content, "", creds)
Service.respond_with(<>Tweeted: <em>«{tweet.text}»</></>)
default:
authenticate()
}
}
Service.spec spec =
{ initial_state: Twitter.status {no_credentials},
metadata: {
id: "twitter",
description: "Managing Twitter account",
cmds: [
{ cmd: "tweet [content]", description: "Publishes a given tweet" }
],
},
function parse_cmd(state) {
parser {
case "tweet" Rule.ws content=(.*) : tweet(state, Text.to_string(content))
}
}
}
}