Updated translation references
[centerim.git] / src / captcha.cc
blobb52637b5105fefe2dbff39c81e8e229b1b139d6a
1 #include "icqconf.h"
2 #include "icqface.h"
3 #include "captcha.h"
5 captcha::captcha() {
8 captcha::~captcha() {
9 questions_t::iterator i;
11 for (i = questions.begin() ; i != questions.end() ; i++ ) {
12 delete *i;
16 bool captcha::docaptcha(imcontact c)
18 if (captchas.find(c) == captchas.end()) {
19 return false;
20 } else {
21 captchas_t::iterator i;
22 captchaasked_t *ca;
23 set<string> *a;
24 i = captchas.find(c);
25 ca = &(i->second);
26 a = ca->answers;
27 return true;
31 void captcha::donecaptcha(imcontact c)
33 captchas.erase(c);
36 bool captcha::checkcaptcha(imcontact c, string answer)
38 captchas_t::iterator i;
39 captchaasked_t *ca;
41 i = captchas.find(c);
43 if (i != captchas.end()) {
44 ca = &(i->second);
45 if (ca->expiry < time(NULL)) {
46 captchas.erase(c);
47 return false;
48 } else {
49 return (ca->answers->find(lo(answer)) != ca->answers->end());
52 return false;
55 string captcha::getcaptchaquestion(imcontact c) {
56 question_t *t;
57 captchaasked_t ca;
59 t = questions[(int)(questions.size() * (rand() / (RAND_MAX + 1.0)))];
60 ca.answers = &t->answers;
61 ca.expiry = conf.getcaptchatimeout() * 60 + time(NULL);
62 captchas[c] = ca;
64 return t->question;
67 void captcha::addquestion(string data) {
68 question_t *q;
69 string::size_type pos;
72 pos = data.find("//");
73 if (pos == string::npos) {
74 return;
75 } else {
76 q = new question_t;
77 q->question = data.substr(0, pos);
78 data.erase(0, pos + 2);
80 while (data.length()) {
81 pos = data.find("::");
82 if (pos == string::npos) {
83 q->answers.insert(lo(data));
84 data.erase();
85 } else if (pos == 0) {
86 data.erase();
87 } else {
88 q->answers.insert(lo(data.substr(0, pos)));
89 data.erase(0, pos + 2);
93 if (q->answers.empty()) {
94 delete q;
95 } else {
96 questions.push_back(q);
101 bool captcha::empty()
103 return questions.empty();
106 unsigned int captcha::size()
108 return questions.size();