Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
UCC
Discord Bot
Commits
45084f53
Commit
45084f53
authored
Feb 04, 2020
by
Ash
Committed by
tec
Feb 04, 2020
Browse files
Change some massive indents to use guard!() pattern
parent
139910c4
Changes
4
Hide whitespace changes
Inline
Side-by-side
Cargo.toml
View file @
45084f53
...
...
@@ -15,3 +15,4 @@ serde = "^1.0.104"
serde_yaml
=
"^0.8"
serenity
=
"0.8.0"
simplelog
=
"^0.7.4"
guard
=
"0.5.0"
src/main.rs
View file @
45084f53
...
...
@@ -4,6 +4,7 @@ extern crate lazy_static;
#[macro_use]
extern
crate
log
;
extern
crate
simplelog
;
#[macro_use]
extern
crate
guard
;
use
simplelog
::
*
;
use
std
::
fs
::{
read_to_string
,
File
};
...
...
src/token_management.rs
View file @
45084f53
...
...
@@ -18,20 +18,19 @@ fn text_encrypt(plaintext: &str) -> String {
}
fn
text_decrypt
(
ciphertext
:
&
str
)
->
Option
<
String
>
{
let
iv
:
&
[
u8
;
16
]
=
&
[
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
];
if
let
Ok
(
cipher_vec
)
=
base64
::
decode
(
ciphertext
)
{
if
let
Ok
(
decrypted_vec
)
=
decrypt
(
*
CIPHER
,
&*
KEY
,
Some
(
iv
),
&
cipher_vec
)
{
if
let
Ok
(
decrypted_token
)
=
str
::
from_utf8
(
decrypted_vec
.as_slice
())
{
return
Some
(
decrypted_token
.to_owned
());
}
else
{
warn!
(
"Invalid utf8 in text"
);
}
}
else
{
warn!
(
"Text decryption failed"
);
}
}
else
{
guard!
(
let
Ok
(
cipher_vec
)
=
base64
::
decode
(
ciphertext
)
else
{
warn!
(
"Unable to decode base64 text"
);
}
None
return
None
});
guard!
(
let
Ok
(
decrypted_vec
)
=
decrypt
(
*
CIPHER
,
&*
KEY
,
Some
(
iv
),
&
cipher_vec
)
else
{
warn!
(
"Text decryption failed"
);
return
None
});
guard!
(
let
Ok
(
decrypted_token
)
=
str
::
from_utf8
(
decrypted_vec
.as_slice
())
else
{
warn!
(
"Invalid utf8 in text"
);
return
None
});
Some
(
decrypted_token
.to_owned
())
}
pub
fn
generate_token
(
discord_user
:
&
User
,
username
:
&
str
)
->
String
{
...
...
@@ -60,34 +59,33 @@ impl std::fmt::Display for TokenError {
}
pub
fn
parse_token
(
discord_user
:
&
User
,
encrypted_token
:
&
str
)
->
Result
<
String
,
TokenError
>
{
if
let
Some
(
token
)
=
text_decrypt
(
encrypted_token
)
{
let
token_components
:
Vec
<
_
>
=
token
.splitn
(
3
,
','
)
.collect
();
info!
(
"Verification attempt from '{}'(uid: {}) for account '{}' with token from {}"
,
discord_user
.name
,
token_components
[
1
],
token_components
[
2
],
token_components
[
0
]
);
let
token_timestamp
=
DateTime
::
parse_from_rfc3339
(
token_components
[
0
])
.expect
(
"Invalid date format"
);
let
token_discord_user
=
token_components
[
1
];
let
token_username
=
token_components
[
2
];
if
token_discord_user
!=
discord_user
.id
.0
.to_string
()
{
warn!
(
"... attempt failed : DiscordID mismatch"
);
return
Err
(
TokenError
::
DiscordIdMismatch
);
}
let
time_delta_seconds
=
Utc
::
now
()
.timestamp
()
-
token_timestamp
.timestamp
();
if
time_delta_seconds
>
5
*
60
{
warn!
(
"... attempt failed : token expired ({} seconds old)"
,
time_delta_seconds
);
return
Err
(
TokenError
::
TokenExpired
);
}
info!
(
"... verification successful (token {} seconds old)"
,
guard!
(
let
Some
(
token
)
=
text_decrypt
(
encrypted_token
)
else
{
return
Err
(
TokenError
::
TokenInvalid
)
});
let
token_components
:
Vec
<
_
>
=
token
.splitn
(
3
,
','
)
.collect
();
info!
(
"Verification attempt from '{}'(uid: {}) for account '{}' with token from {}"
,
discord_user
.name
,
token_components
[
1
],
token_components
[
2
],
token_components
[
0
]
);
let
token_timestamp
=
DateTime
::
parse_from_rfc3339
(
token_components
[
0
])
.expect
(
"Invalid date format"
);
let
token_discord_user
=
token_components
[
1
];
let
token_username
=
token_components
[
2
];
if
token_discord_user
!=
discord_user
.id
.0
.to_string
()
{
warn!
(
"... attempt failed : DiscordID mismatch"
);
return
Err
(
TokenError
::
DiscordIdMismatch
);
}
let
time_delta_seconds
=
Utc
::
now
()
.timestamp
()
-
token_timestamp
.timestamp
();
if
time_delta_seconds
>
5
*
60
{
warn!
(
"... attempt failed : token expired ({} seconds old)"
,
time_delta_seconds
);
Ok
(
token_username
.to_owned
())
}
else
{
Err
(
TokenError
::
TokenInvalid
)
return
Err
(
TokenError
::
TokenExpired
);
}
info!
(
"... verification successful (token {} seconds old)"
,
time_delta_seconds
);
Ok
(
token_username
.to_owned
())
}
src/voting.rs
View file @
45084f53
...
...
@@ -349,63 +349,64 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
let
react_as_string
=
get_string_from_react
(
&
add_reaction
.emoji
);
match
add_reaction
.message
(
&
ctx
.http
)
{
Ok
(
mut
message
)
=>
{
if
let
Ok
(
user
)
=
add_reaction
.user
(
&
ctx
)
{
match
user
.has_role
(
&
ctx
,
CONFIG
.server_id
,
CONFIG
.vote_role
)
{
Ok
(
true
)
=>
{
// remove vote if already voted
for
react
in
[
CONFIG
.for_vote
.to_string
(),
CONFIG
.against_vote
.to_string
(),
CONFIG
.abstain_vote
.to_string
(),
]
.iter
()
.filter
(|
r
|
r
!=
&&
react_as_string
)
guard!
(
let
Ok
(
user
)
=
add_reaction
.user
(
&
ctx
)
else
{
return
});
match
user
.has_role
(
&
ctx
,
CONFIG
.server_id
,
CONFIG
.vote_role
)
{
Ok
(
true
)
=>
{
// remove vote if already voted
for
react
in
[
CONFIG
.for_vote
.to_string
(),
CONFIG
.against_vote
.to_string
(),
CONFIG
.abstain_vote
.to_string
(),
]
.iter
()
.filter
(|
r
|
r
!=
&&
react_as_string
)
{
for
a_user
in
message
.reaction_users
(
&
ctx
,
react
.as_str
(),
None
,
None
)
.unwrap
()
{
for
a_user
in
message
.reaction_users
(
&
ctx
,
react
.as_str
(),
None
,
None
)
.unwrap
()
{
if
a_user
.id
.0
==
user
.id
.0
{
if
let
Err
(
why
)
=
add_reaction
.delete
(
&
ctx
)
{
error!
(
"Error deleting react: {:?}"
,
why
);
};
return
;
}
if
a_user
.id
.0
==
user
.id
.0
{
if
let
Err
(
why
)
=
add_reaction
.delete
(
&
ctx
)
{
error!
(
"Error deleting react: {:?}"
,
why
);
};
return
;
}
}
// remove 'illegal' reacts
if
!
CONFIG
.allowed_reacts
()
.contains
(
&
react_as_string
)
{
if
let
Err
(
why
)
=
add_reaction
.delete
(
&
ctx
)
{
error!
(
"Error deleting react: {:?}"
,
why
);
};
return
;
}
// update motion
let
mut
motion_info
=
get_cached_motion
(
&
ctx
,
&
message
);
if
let
Some
(
vote
)
=
motion_info
.votes
.get_mut
(
&
react_as_string
)
{
vote
.retain
(|
u
|
u
.id
!=
user
.id
);
vote
.push
(
user
.clone
());
}
set_cached_motion
(
message
.id
,
motion_info
);
update_motion
(
&
ctx
,
&
mut
message
,
&
user
,
"add"
,
add_reaction
);
}
Ok
(
false
)
=>
{
if
!
[
CONFIG
.approve_react
.to_string
(),
CONFIG
.disapprove_react
.to_string
(),
]
.contains
(
&
react_as_string
)
{
if
let
Err
(
why
)
=
add_reaction
.delete
(
&
ctx
)
{
error!
(
"Error deleting react: {:?}"
,
why
);
};
return
;
}
// remove 'illegal' reacts
if
!
CONFIG
.allowed_reacts
()
.contains
(
&
react_as_string
)
{
if
let
Err
(
why
)
=
add_reaction
.delete
(
&
ctx
)
{
error!
(
"Error deleting react: {:?}"
,
why
);
};
return
;
}
// update motion
let
mut
motion_info
=
get_cached_motion
(
&
ctx
,
&
message
);
if
let
Some
(
vote
)
=
motion_info
.votes
.get_mut
(
&
react_as_string
)
{
vote
.retain
(|
u
|
u
.id
!=
user
.id
)
;
vote
.push
(
user
.clone
());
}
Err
(
why
)
=>
{
error!
(
"Error getting user role: {:?}"
,
why
);
set_cached_motion
(
message
.id
,
motion_info
);
update_motion
(
&
ctx
,
&
mut
message
,
&
user
,
"add"
,
add_reaction
);
}
Ok
(
false
)
=>
{
if
!
[
CONFIG
.approve_react
.to_string
(),
CONFIG
.disapprove_react
.to_string
(),
]
.contains
(
&
react_as_string
)
{
if
let
Err
(
why
)
=
add_reaction
.delete
(
&
ctx
)
{
error!
(
"Error deleting react: {:?}"
,
why
);
};
return
;
}
}
Err
(
why
)
=>
{
error!
(
"Error getting user role: {:?}"
,
why
);
}
}
}
Err
(
why
)
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment