Skip to content

Commit

Permalink
TRIM sql input before inserting
Browse files Browse the repository at this point in the history
* Allow casting of a vote only on existing polls
* Return formatted results for display
  • Loading branch information
hjpotter92 committed May 9, 2015
1 parent 999fd99 commit 2f27539
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
35 changes: 19 additions & 16 deletions botstats.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,20 @@ function ExecuteCommand( tUser, sCmd, sMessage )
if iLimit > 100 then iLimit = 100 end
sReply = AllTimeTopToks( iLimit )

elseif sCmd == "gift" then
local sToNick, fAmount = tTokens[1], tonumber( tTokens[2] ) or 0
if sToNick and fAmount then
sReply = gift( tUser.sNick, sToNick, fAmount, sData )
else
sReply = "Incomplete parameters"
end

elseif sCmd == "transactions" then
local sNick = tTokens[1] or tUser.sNick
sReply = Transactions( tUser, sNick )

elseif sCmd == "poll" then
if tTokens[1] == "vote" then
sReply = Vote( tUser.sNick, tTokens[2], tTokens[3] )
elseif tTokens[1] == "view" then
sReply = View( tTokens[2] )
elseif tTokens[1] == "list" then
sReply = List( tTokens[2] )
elseif tTokens[1] == "help" then
sReply = tHelp.sPollHelp
end
end

if sReply then
Expand All @@ -158,19 +161,19 @@ function ExecuteCommand( tUser, sCmd, sMessage )
local sNick = tTokens[1] or tUser.sNick
sReply = NickToks( tUser, sNick )

elseif sCmd == "gift" then
local sToNick, fAmount = tTokens[1], tonumber( tTokens[2] ) or 0
if sToNick and fAmount then
sReply = gift( tUser.sNick, sToNick, fAmount, sData )
else
sReply = "Incomplete parameters"
end

elseif sCmd == "poll" then
if tTokens[1] == "add" then
sReply = AddPoll( tUser.sNick, table.concat(tTokens, ' ', 2) )
elseif tTokens[1] == "remove" then
sReply = DeletePoll( tUser.sNick, tTokens[2] )
elseif tTokens[1] == "vote" then
sReply = Vote( tUser.sNick, tTokens[2], tTokens[3] )
elseif tTokens[1] == "view" then
sReply = View( tTokens[2] )
elseif tTokens[1] == "list" then
sReply = List( tTokens[2] )
elseif tTokens[1] == "help" then
sReply = tHelp.sPollHelp
end
end
return Reply( tUser, sReply )
Expand Down
26 changes: 17 additions & 9 deletions external/stats/polls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ AddPoll = ( function()
sNoTitle = "A poll should have a valid title.",
sFewChoices = "A poll should have at least 2 valid options. At most 10 choices are allowed.",
}, {
sQuestion = [[INSERT INTO questions (question, nick, dated) VALUES ( '%s', '%s', NOW() )]],
sChoice = [[INSERT INTO options (option_id, poll_id, `option`) VALUES ( %d, %d, '%s' )]],
sQuestion = [[INSERT INTO questions (question, nick, dated) VALUES ( TRIM('%s'), '%s', NOW() )]],
sChoice = [[INSERT INTO options (option_id, poll_id, `option`) VALUES ( %d, %d, TRIM('%s') )]],
}
local function Insert( tInput, sInput, iBegin, iEnd )
if not iEnd then iEnd = #sInput end
Expand All @@ -36,7 +36,7 @@ AddPoll = ( function()
local function ParseTitle( sInput )
local iStart, iEnd = sInput:find "%[%]"
if not iStart or iStart == 1 then return end
return sInput:sub( 1, iStart ), FindChoices( sInput:sub(iStart) )
return sInput:sub( 1, iStart - 1 ), FindChoices( sInput:sub(iStart) )
end
return function ( sNick, sData )
local sNick = sqlCon:escape( sNick )
Expand Down Expand Up @@ -75,14 +75,22 @@ DeletePoll = ( function()
end )()

Vote = ( function()
local sQuery = [[INSERT INTO votes (poll_id, option_id, nick, dated) VALUES( %d, %d, '%s', NOW() )]]
local sQuery = [[INSERT INTO votes (poll_id, option_id, nick, dated)
SELECT
o.poll_id,
o.option_id,
'%s',
NOW()
FROM options o
WHERE o.poll_id = %d
AND o.option_id = %d]]
return function ( sNick, iPollID, iChoiceID )
local sNick = sqlCon:escape( sNick )
local iPollID, iChoiceID = tonumber( iPollID ), tonumber( iChoiceID )
if not ( iPollID and iChoiceID ) then
return "The provided argument was not a number."
end
assert( sqlCon:execute(sQuery:format( iPollID, iChoiceID, sNick )) )
assert( sqlCon:execute(sQuery:format( sNick, iPollID, iChoiceID )) )
return "Your vote has been cast. Thank you!"
end
end )()
Expand All @@ -102,7 +110,7 @@ List = ( function()
ORDER BY q.poll_id DESC
LIMIT %d]]
local function Format( tInput )
return ("%03d. [%03d] %s (Created by %s on %s)"):format( tInput.poll_id, tInput.total, tInput.question, tInput.nick, tInput.dated )
return ( "%03d. [%03d] %s (Created by %s on %s)" ):format( tInput.poll_id, tInput.total, tInput.question, tInput.nick, tInput.dated )
end
return function ( iLimit )
local iLimit = tonumber( iLimit ) or 15
Expand All @@ -114,7 +122,7 @@ List = ( function()
table.insert( tResult, 1, Format(tRow) )
tRow = sqlCur:fetch( tRow, 'a' )
end
return ( "List with recent %d polls follows:\n\n%s\n" ):format( iLimit, table.concat(tResult, "\n") )
return ( "List with recent %02d polls follows:\n\n%s\n" ):format( iLimit, table.concat(tResult, "\n") )
end
end )()

Expand All @@ -141,7 +149,7 @@ View = ( function()
return tReturn
end
local function Format( tInput )
return ( "%d. [ %-30s ] (%d) %s" ):format( tInput.option_id, ('='):rep(tInput.total)..'>', tInput.total, tInput.option )
return ( "%02d. [ %-30s ] (%02d) %s" ):format( tInput.option_id, ('='):rep(tInput.total)..'>', tInput.total, tInput.option )
end
return function ( iPollID )
local iPollID = tonumber( iPollID )
Expand All @@ -161,6 +169,6 @@ View = ( function()
table.insert( tList, Format(tRow) )
tRow = sqlCur:fetch( tRow, 'a' )
end
return ( "\n\t%d. %s\n\t\t - by %s (%s)\n\n%s\n" ):format( iPollID, tQuestion.question, tQuestion.nick, tQuestion.dated, table.concat(tList, '\n') )
return ( "\n\t%03d. %s\n\t\t - by %s (%s)\n\n%s\n" ):format( iPollID, tQuestion.question, tQuestion.nick, tQuestion.dated, table.concat(tList, '\n') )
end
end )()
18 changes: 9 additions & 9 deletions texts/pollHelp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ POLLS
While any user can participate in an ongoing poll, only registered users are allowed to
create new ones. The following commands are available to the end users:

!poll add <msg> - To add a new poll to the database. The format for providing list of
choices is as follows:
!poll add <msg> - To add a new poll to the database. The format for providing list of
choices is as follows:

!poll add Is this feature awesome?[]Yes [] No[]Maybe
!poll add Is this feature awesome?[]Yes [] No[]Maybe

NOTE: Spaces around the `[]` delimiters will be ignored.
NOTE: Spaces around the `[]` delimiters will be ignored.

!poll remove <#ID> - Provide a valid ID for a poll to delete it.
!poll remove <#ID> - Provide a valid ID for a poll to delete it.

!poll view <#ID> - Detailed voting break-down for the poll with provided ID will be returned.
!poll view <#ID> - Detailed voting break-down for the poll with provided ID will be returned.

!poll list [<#Limit>] - Default limit is 15. only the most recent #Limit polls will be displayed.
!poll list [<#Limit>] - Default limit is 15. only the most recent #Limit polls will be displayed.

!poll vote <#Poll> <#Choice> - Cast your vote for the #Choice of the poll with ID #Poll.
!poll vote <#Poll> <#Choice> - Cast your vote for the #Choice of the poll with ID #Poll.

!poll help - Display help selectively for the polling features.
!poll help - Display help selectively for the polling features.

0 comments on commit 2f27539

Please sign in to comment.