150 lines
5.1 KiB
C++
150 lines
5.1 KiB
C++
#include "todomodule.h"
|
|
#include "message.h"
|
|
#include <time.h>
|
|
#include <QStringList>
|
|
#include <QDateTime>
|
|
|
|
TodoModule::TodoModule()
|
|
{
|
|
|
|
}
|
|
|
|
bool TodoModule::messageHandler(Message msg)
|
|
{
|
|
if(msg.command.compare(QString("PRIVMSG"), Qt::CaseInsensitive) == 0)
|
|
{
|
|
if(msg.args.startsWith("!todo "))
|
|
{
|
|
answer = usage(TODO);
|
|
Task t;
|
|
int deltaIndex = 1;
|
|
t.isPrivate = !msg.target.contains(getChan());
|
|
t.recipient = msg.nick;
|
|
t.repeat = 0;
|
|
QStringList splitter = msg.args.split(' ');
|
|
if(splitter[deltaIndex].compare("to") == 0)
|
|
{
|
|
deltaIndex += 2;
|
|
t.recipient = splitter[2];
|
|
}
|
|
if(splitter.size() < deltaIndex+2)
|
|
return true;
|
|
QString dateString = splitter[deltaIndex];
|
|
QStringList dateSplitter;
|
|
bool ok;
|
|
if(dateString.startsWith("date("))
|
|
{
|
|
answer = usage(DATE);
|
|
dateString = dateString.mid(5, dateString.size() - 6);
|
|
dateSplitter = dateString.split('/');
|
|
if(dateSplitter.size() < 2)
|
|
return true;
|
|
int day = dateSplitter[0].toUInt(&ok);
|
|
if(!ok || day > 31 || day < 1) return true;
|
|
dateSplitter = dateSplitter[1].split('-');
|
|
int month = dateSplitter[0].toUInt(&ok);
|
|
if(!ok || month > 12 || month < 1) return true;
|
|
int hours = 0;
|
|
int minutes = 0;
|
|
int seconds = 0;
|
|
if(dateSplitter.size() > 1)
|
|
{
|
|
dateSplitter = dateSplitter[1].split(':');
|
|
hours = dateSplitter[0].toUInt(&ok);
|
|
if(!ok || hours > 23) return true;
|
|
if(dateSplitter.size() > 1)
|
|
{
|
|
minutes = dateSplitter[1].toUInt(&ok);
|
|
if(!ok || minutes > 59) return true;
|
|
}
|
|
if(dateSplitter.size() > 2)
|
|
{
|
|
seconds = dateSplitter[2].toUInt(&ok);
|
|
if(!ok || seconds > 59) return true;
|
|
}
|
|
}
|
|
QDateTime date(QDate(QDate::currentDate().year(), month, day), QTime(hours, minutes, seconds));
|
|
t.date = date.toTime_t();
|
|
}
|
|
else
|
|
{
|
|
answer = usage(DURATION);
|
|
dateSplitter = dateString.split(':');
|
|
int hours = dateSplitter[0].toUInt(&ok);
|
|
int minutes = 0;
|
|
int seconds = 0;
|
|
if(!ok) return true;
|
|
if(dateSplitter.size() > 1)
|
|
{
|
|
minutes = dateSplitter[1].toUInt(&ok);
|
|
if(!ok || minutes > 59) return true;
|
|
}
|
|
if(dateSplitter.size() > 2)
|
|
{
|
|
seconds = dateSplitter[2].toUInt(&ok);
|
|
if(!ok || seconds > 59) return true;
|
|
}
|
|
QDateTime date = QDateTime::currentDateTime();
|
|
minutes += hours*60;
|
|
seconds += minutes*60;
|
|
t.date = date.addSecs(seconds).toTime_t();
|
|
}
|
|
++deltaIndex;
|
|
if(splitter[deltaIndex].compare("every") == 0)
|
|
{
|
|
answer = usage(DURATION);
|
|
dateSplitter = splitter[deltaIndex+1].split(':');
|
|
int hours = dateSplitter[0].toUInt(&ok);
|
|
int minutes = 0;
|
|
int seconds = 0;
|
|
if(!ok) return true;
|
|
if(dateSplitter.size() > 1)
|
|
{
|
|
minutes = dateSplitter[1].toUInt(&ok);
|
|
if(!ok || minutes > 59) return true;
|
|
}
|
|
if(dateSplitter.size() > 2)
|
|
{
|
|
seconds = dateSplitter[2].toUInt(&ok);
|
|
if(!ok || seconds > 59) return true;
|
|
}
|
|
minutes += hours*60;
|
|
seconds += minutes*60;
|
|
t.repeat = seconds;
|
|
deltaIndex += 2;
|
|
}
|
|
QString desc;
|
|
bool first = true;
|
|
for(int i=deltaIndex; i<splitter.size(); ++i)
|
|
{
|
|
if(first)
|
|
first = false;
|
|
else
|
|
desc.append(" ");
|
|
desc.append(splitter[i]);
|
|
}
|
|
t.description = desc;
|
|
t.emitter = msg.nick;
|
|
tasks.push_back(t);
|
|
answer = say(QString("todo successfully added");
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
QString TodoModule::usage(int type)
|
|
{
|
|
switch(type)
|
|
{
|
|
default:
|
|
case TODO:
|
|
return say("usage = !todo [to TARGET] DATE | DURATION [every DURATION] DESCRIPTION");
|
|
case DURATION:
|
|
return say("DURATION = HOURS[:MINUTES[:SECONDS]]");
|
|
case DATE:
|
|
return say("DATE = date(DAY/MONTH[-HOUR[:MINUTE[:SECOND]]])");
|
|
}
|
|
}
|
|
|