Merge branch 'master' of github.com:otcshare/automotive-message-broker
[profile/ivi/automotive-message-broker.git] / plugins / bluemonkey / irccoms.cpp
1 #include "irccoms.h"
2 #include <QtDebug>
3 #include <IrcCommand>
4 #include <IrcMessage>
5 #include <QSslSocket>
6 #include <QFile>
7 #include <QNetworkProxy>
8 #include <QTimer>
9 #include <QScriptEngine>
10
11 #define foreach Q_FOREACH
12
13 IrcCommunication::IrcCommunication(std::map<std::string, std::string> config, QObject* parent)
14         :QObject(parent)
15 {
16
17         session = new IrcSession(this);
18         mSsl=false;
19
20         QObject::connect(session,SIGNAL(connected()),this,SIGNAL(connected()));
21         QObject::connect(session,SIGNAL(disconnected()),this,SIGNAL(disconnected()));
22         QObject::connect(session,SIGNAL(disconnected()),this,SLOT(reconnect()));
23         QObject::connect(session,SIGNAL(socketError(QAbstractSocket::SocketError)),this,SLOT(socketError(QAbstractSocket::SocketError)));
24         QObject::connect(session,SIGNAL(connecting()),this,SIGNAL(connecting()));
25         QObject::connect(session,SIGNAL(messageReceived(IrcMessage*)),this,SLOT(messageReceived(IrcMessage*)));
26
27         QScriptEngine *engine = new QScriptEngine(this);
28
29         QScriptValue eventEngineValue = engine->newQObject(this);
30         engine->globalObject().setProperty("irc", eventEngineValue);
31
32         QString str = config["ircSettings"].c_str();
33
34         QFile file(str);
35         if(!file.open(QIODevice::ReadOnly))
36         {
37                 qDebug()<<"failed to open irc config file: "<<str;
38                 return;
39         }
40
41         QString script = file.readAll();
42
43         file.close();
44
45         engine->evaluate(script);
46
47 }
48
49 void IrcCommunication::announce(QString s)
50 {
51         qDebug()<<"channels: "<<mChannels;
52         foreach(QString channel, mChannels)
53         {
54                 qDebug()<<"sending "<<s<<" to channel: "<<channel;
55                 IrcCommand command;
56                 command.setType(IrcCommand::Message);
57                 command.setParameters(QStringList()<<channel<<s);
58                 session->sendCommand(&command);
59         }
60 }
61
62 void IrcCommunication::respond(QString target, QString s)
63 {
64         IrcCommand *command = IrcCommand::createMessage(target,s);
65         session->sendCommand(command);
66
67         delete command;
68
69 }
70
71 void IrcCommunication::messageReceived(IrcMessage *msg)
72 {
73         qDebug()<<"message received "<<msg->type()<<" prefix: "<<msg->sender().prefix()<<" params:"<<msg->parameters();
74
75         if(msg->type() == IrcMessage::Private)
76         {
77                 if(msg->parameters().count() > 1)
78                 {
79                         QString sender = msg->parameters().at(0);
80                         QString m = msg->parameters().at(1);
81
82                         message(sender, msg->sender().prefix(), m);
83                 }
84
85         }
86 }
87
88 void IrcCommunication::connect(QString host, int port, QString proxy, QString user, QString nick, QString pass)
89 {
90         session->setHost(host);
91         session->setPort(port);
92         session->setUserName(user);
93         session->setNickName(nick);
94         session->setRealName(nick);
95
96         if(!proxy.isEmpty())
97         {
98                 if(!proxy.contains(":"))
99                 {
100                         qDebug("proxy format must be 'address:port'");
101                         return;
102                 }
103                 QString host = proxy.split(":").at(0);
104                 int port = proxy.split(":").at(1).toInt();
105
106                 QNetworkProxy netproxy;
107                 netproxy.setType(QNetworkProxy::Socks5Proxy);
108                 netproxy.setHostName(host);
109                 netproxy.setPort(port);
110
111                 QNetworkProxy::setApplicationProxy(netproxy);
112         }
113
114         qDebug()<<"opening irc session";
115         session->open();
116 }
117
118 void IrcCommunication::setSsl(bool use)
119 {
120         if(use)
121                 session->setSocket(new QSslSocket(this));
122 }
123
124 void IrcCommunication::join(QString channel)
125 {
126         if(!mChannels.contains(channel))
127                 mChannels.append(channel);
128
129         IrcCommand command;
130         command.setType(IrcCommand::Join);
131         command.setParameters(QStringList()<<channel);
132         session->sendCommand(&command);
133 }
134
135 void IrcCommunication::reconnect()
136 {
137         if(session->socket()->state() == QAbstractSocket::ConnectingState)
138                 QTimer::singleShot(5000,this,SLOT(reconnect()));
139         else
140                 QTimer::singleShot(5000,session,SLOT(open()));
141 }
142
143 /*void IrcCommunication::sslError(QList<QSslError>)
144 {
145         qDebug()<<"some ssl errors!! trying to ignore them";
146         QSslSocket* socket = qobject_cast<QSslSocket*>(session->socket());
147         if(socket)
148         {
149                 socket->ignoreSslErrors();
150         }
151 }*/
152
153 void IrcCommunication::socketError(QAbstractSocket::SocketError error)
154 {
155         qDebug()<<"Socket error.  attempting to reconnect..."<<error;
156         reconnect();
157 }
158
159 void IrcCommunication::setIgnoreInvalidCert(bool ignore)
160 {
161         if(ignore)
162                 QObject::connect(session->socket(),SIGNAL(),this,SLOT(sslError(QList<QSslError>)));
163 }