;ς = ΔDc@s dkZdkZdkiZdkiZdkiZdki Z dk Tdk i Z dk iZdkiZdefd„ƒYZdeifd„ƒYZdeifd„ƒYZdefd „ƒYZe id hd e<ƒZd eifd „ƒYZeZdS( N(s*s PollErrorcBstZRS(N(s__name__s __module__(((s./Poll/plugin.pys PollError+ss OptionRecordcBstZddgZd„ZRS(NstextsvotescCstd|i|iƒSdS(Ns#%i: %q(sformatsselfsidstext(sself((s./Poll/plugin.pys__str__3s(s__name__s __module__s __fields__s__str__(((s./Poll/plugin.pys OptionRecord.s s PollRecordcBs#tZddddgZd„ZRS(NsbysquestionsoptionssstatuscCs‡ti|iƒ}|io(tdditt |iƒƒƒ}nd}|i o d}nd}td|i |i |||ƒSdS(Ns Options: %ss; sThe poll has no options, yetsopenscloseds,Poll #%i: %q started by %s. %s. Poll is %s.( spluginss getUserNamesselfsbysusersoptionssformatsjoinsmapsstrsstatussidsquestion(sselfsstatussusersoptions((s./Poll/plugin.pys__str__=s (  (s__name__s __module__s __fields__s__str__(((s./Poll/plugin.pys PollRecord6ss SqlitePollDBcBsbtZd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Z d „Z d „Z RS( NcCstiƒ|_||_dS(N(sircutilssIrcDictsselfsdbssfilename(sselfsfilename((s./Poll/plugin.pys__init__KscCs(x!|iiƒD]}|iƒqWdS(N(sselfsdbss itervaluessdbsclose(sselfsdb((s./Poll/plugin.pyscloseOscCsόy dk}Wntj otid‚nXti|i|ƒ}||i jo|i |Snt i i |ƒo%|i |ƒ|i |<|i |Sn|i |ƒ}||i |<|iƒ}|idƒ|idƒ|idƒ|iƒ|SdS(NsWYou need to have PySQLite installed to use Poll. Download it at sίCREATE TABLE polls ( id INTEGER PRIMARY KEY, question TEXT UNIQUE ON CONFLICT IGNORE, started_by INTEGER, open INTEGER)sΡCREATE TABLE options ( id INTEGER, poll_id INTEGER, option TEXT, UNIQUE (poll_id, id) ON CONFLICT IGNORE)sωCREATE TABLE votes ( user_id INTEGER, poll_id INTEGER, option_id INTEGER, UNIQUE (user_id, poll_id) ON CONFLICT IGNORE)(ssqlites ImportErrors callbackssErrorspluginssmakeChannelFilenamesselfsfilenameschannelsdbssosspathsexistssconnectsdbscursorsexecutescommit(sselfschannelssqlitesdbsfilenamescursor((s./Poll/plugin.pys_getDbSs$       c Csζ|i|ƒ}|iƒ} | id|ƒ| io| iƒ\}} }n t i ‚| id|ƒ| ioIgi} | iƒD](\}}| t|d|ddƒƒq…~ }ng}t|d|d|d| d |ƒSdS( NsRSELECT question, started_by, open FROM polls WHERE id=%ss/SELECT id, option FROM options WHERE poll_id=%sstextsvotesisquestionsstatussbysoptions(sselfs_getDbschannelsdbscursorsexecutespoll_idsrowcountsfetchonesquestionsbysstatussdbis NoRecordErrorsappends_[1]sfetchallsisos OptionRecordsoptionss PollRecord( sselfschannelspoll_idsstatussosisdbsquestionsoptionsscursors_[1]sby((s./Poll/plugin.pysgetvs     IcCs]|i|ƒ}|iƒ}|id||iƒ|i ƒ|id|ƒ|i ƒdSdS(Ns*INSERT INTO polls VALUES (NULL, %s, %s, 1)s&SELECT id FROM polls WHERE question=%si( sselfs_getDbschannelsdbscursorsexecutesquestionsusersidscommitsfetchone(sselfschannelsusersquestionsdbscursor((s./Poll/plugin.pysopen‰s    cCsf|i|ƒ}|iƒ}|id|ƒ|idjo ti ‚n|id|ƒ|i ƒdS(Ns SELECT id FROM polls WHERE id=%sis#UPDATE polls SET open=0 WHERE id=%s( sselfs_getDbschannelsdbscursorsexecutesidsrowcountsdbis NoRecordErrorscommit(sselfschannelsidsdbscursor((s./Poll/plugin.pys closePoll’s  cCs |i|ƒ}|iƒ}|id|ƒ|idjo ti ‚n|i|i ƒdjpt i |idƒ o td‚n|id|ƒt|i ƒdƒdjo td‚n|id|ƒ|i ƒdpd}|id |||ƒ|iƒdS( NsBSELECT started_by FROM polls WHERE id=%sisadmins.That poll isn't yours and you aren't an admin.sKSELECT COUNT(user_id) FROM votes WHERE poll_id=%ss(Cannot add options to a poll with votes.sHSELECT MAX(id)+1 FROM options WHERE poll_id=%sisAINSERT INTO options VALUES (%s, %s, %s)(sselfs_getDbschannelsdbscursorsexecutesidsrowcountsdbis NoRecordErrorsusersfetchonesircdbscheckCapabilitys PollErrorsints option_idsoptionscommit(sselfschannelsusersidsoptions option_idsdbscursor((s./Poll/plugin.pysaddœs$   1      cCs |i|ƒ}|iƒ}|id|ƒ|idjo ti ‚n+t |i ƒdƒdjo t d‚n|id||ƒ|idjo t d‚n|id|i|ƒ|idjo|id|i||ƒn|id||i|ƒ|iƒdS( Ns<SELECT open FROM polls WHERE id=%sisThat poll is closed.seSELECT id FROM options WHERE poll_id=%s AND id=%ssThere is no such option.sUSELECT option_id FROM votes WHERE user_id=%s AND poll_id=%ss%INSERT INTO votes VALUES (%s, %s, %s)s[UPDATE votes SET option_id=%s WHERE user_id=%s AND poll_id=%s(sselfs_getDbschannelsdbscursorsexecutesidsrowcountsdbis NoRecordErrorsintsfetchones PollErrorsoptionsuserscommit(sselfschannelsusersidsoptionsdbscursor((s./Poll/plugin.pysvoteΉs(           c Cs3|i|ƒ}|iƒ} | id|ƒ| idjo ti ‚n| i ƒ\} }}}ti|ƒ}| id|||ƒ| idjo td‚nlg} xb| iƒD]T\}} | id| |ƒ| i ƒd}| it| dt|ƒd|ƒƒq΅Wt|d|d |d |d | ƒSdS( NsVSELECT id, question, started_by, open FROM polls WHERE id=%sis?SELECT count(user_id), option_id FROM votes WHERE poll_id=%s GROUP BY option_id UNION SELECT 0, id AS option_id FROM options WHERE poll_id=%s AND id NOT IN ( SELECT option_id FROM votes WHERE poll_id=%s) GROUP BY option_id ORDER BY count(user_id) DESCsThis poll has no votes yet.sWSELECT option FROM options WHERE id=%s AND poll_id=%ssvotesstextsquestionsstatussbysoptions(sselfs_getDbschannelsdbscursorsexecutespoll_idsrowcountsdbis NoRecordErrorsfetchonesidsquestionsbysstatusspluginss getUserNames PollErrorsoptionssfetchallscounts option_idsoptionsappends OptionRecordsints PollRecord( sselfschannelspoll_idsstatussoptionsquestionsdbscountsbyscursorsoptionss option_idsid((s./Poll/plugin.pysresultsΥs,        c Cs|i|ƒ}|iƒ}|idƒ|ioPgi}|i ƒD]1\}}}|t |d|d|ddƒƒqF~Sn ti‚dS(NskSELECT id, started_by, question FROM polls WHERE open=1squestionsbysstatusi(sselfs_getDbschannelsdbscursorsexecutesrowcountsappends_[1]sfetchallsidsbysqs PollRecordsdbis NoRecordError(sselfschannels_[1]sdbsbyscursorsqsid((s./Poll/plugin.pysselectϋs    P( s__name__s __module__s__init__scloses_getDbsgetsopens closePollsaddsvotesresultssselect(((s./Poll/plugin.pys SqlitePollDBJs   #    &sPollssqlitecBstZd„Zd„Zd„ZeeddgƒZd„ZeedddgƒZd „Zeeddd fgƒZd „Zeedddd fdgƒZd „Z ee dddd fdd fgƒZ d„Z ee ddd fgƒZ d„Z ee dgƒZ RS(NcCs2tt|ƒ|_|ii|ƒtƒ|_dS(N(ssupersPollsselfs _Poll__parents__init__sircsPollDBsdb(sselfsirc((s./Poll/plugin.pys__init__ scCs|iiƒ|iiƒdS(N(sselfs _Poll__parentsdiesdbsclose(sself((s./Poll/plugin.pysdies cCs_y|ii||ƒ}Wn2tij o#|i t d|ƒdt ƒnX|i |ƒdS(s½[] Displays the poll question and options for the given poll id. is only necessary if the message isn't sent in the channel itself. sThere is no poll with id %i.sRaiseN( sselfsdbsgetschannelsidsrecordsdbis NoRecordErrorsircserrorsformatsTruesreply(sselfsircsmsgsargsschannelsidsrecord((s./Poll/plugin.pyspolls !s channeldbsidcCs,|itd|ii|||ƒƒƒdS(sͺ[] Creates a new poll with the given question. is only necessary if the message isn't sent in the channel itself. s(poll #%i added)N( sircs replySuccesssformatsselfsdbsopenschannelsusersquestion(sselfsircsmsgsargsschannelsusersquestion((s./Poll/plugin.pysopen!s suserstextcCsKy!|ii||ƒ|iƒWn#tij o|i dƒnXdS(sΗ[] Closes the poll with the given ; further votes will not be allowed. is only necessary if the message isn't sent in the channel itself. spoll idN( sselfsdbs closePollschannelsidsircs replySuccesssdbis NoRecordErrors errorInvalid(sselfsircsmsgsargsschannelsid((s./Poll/plugin.pysclose+s spollc Csxy'|ii||||ƒ|iƒWnJt i j o|i dƒn)t j o}|it|ƒƒnXdS(sΞ[]