7 #include <QNetworkProxy>
9 #include <QScriptEngine>
11 #define foreach Q_FOREACH
13 IrcCommunication::IrcCommunication(std::map<std::string, std::string> config, QObject* parent)
17 session = new IrcSession(this);
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*)));
27 QScriptEngine *engine = new QScriptEngine(this);
29 QScriptValue eventEngineValue = engine->newQObject(this);
30 engine->globalObject().setProperty("irc", eventEngineValue);
32 QString str = config["ircSettings"].c_str();
35 if(!file.open(QIODevice::ReadOnly))
37 qDebug()<<"failed to open irc config file: "<<str;
41 QString script = file.readAll();
45 engine->evaluate(script);
49 void IrcCommunication::announce(QString s)
51 qDebug()<<"channels: "<<mChannels;
52 foreach(QString channel, mChannels)
54 qDebug()<<"sending "<<s<<" to channel: "<<channel;
56 command.setType(IrcCommand::Message);
57 command.setParameters(QStringList()<<channel<<s);
58 session->sendCommand(&command);
62 void IrcCommunication::respond(QString target, QString s)
64 IrcCommand *command = IrcCommand::createMessage(target,s);
65 session->sendCommand(command);
71 void IrcCommunication::messageReceived(IrcMessage *msg)
73 qDebug()<<"message received "<<msg->type()<<" prefix: "<<msg->sender().prefix()<<" params:"<<msg->parameters();
75 if(msg->type() == IrcMessage::Private)
77 if(msg->parameters().count() > 1)
79 QString sender = msg->parameters().at(0);
80 QString m = msg->parameters().at(1);
82 message(sender, msg->sender().prefix(), m);
88 void IrcCommunication::connect(QString host, int port, QString proxy, QString user, QString nick, QString pass)
90 session->setHost(host);
91 session->setPort(port);
92 session->setUserName(user);
93 session->setNickName(nick);
94 session->setRealName(nick);
98 if(!proxy.contains(":"))
100 qDebug("proxy format must be 'address:port'");
103 QString host = proxy.split(":").at(0);
104 int port = proxy.split(":").at(1).toInt();
106 QNetworkProxy netproxy;
107 netproxy.setType(QNetworkProxy::Socks5Proxy);
108 netproxy.setHostName(host);
109 netproxy.setPort(port);
111 QNetworkProxy::setApplicationProxy(netproxy);
114 qDebug()<<"opening irc session";
118 void IrcCommunication::setSsl(bool use)
121 session->setSocket(new QSslSocket(this));
124 void IrcCommunication::join(QString channel)
126 if(!mChannels.contains(channel))
127 mChannels.append(channel);
130 command.setType(IrcCommand::Join);
131 command.setParameters(QStringList()<<channel);
132 session->sendCommand(&command);
135 void IrcCommunication::reconnect()
137 if(session->socket()->state() == QAbstractSocket::ConnectingState)
138 QTimer::singleShot(5000,this,SLOT(reconnect()));
140 QTimer::singleShot(5000,session,SLOT(open()));
143 /*void IrcCommunication::sslError(QList<QSslError>)
145 qDebug()<<"some ssl errors!! trying to ignore them";
146 QSslSocket* socket = qobject_cast<QSslSocket*>(session->socket());
149 socket->ignoreSslErrors();
153 void IrcCommunication::socketError(QAbstractSocket::SocketError error)
155 qDebug()<<"Socket error. attempting to reconnect..."<<error;
159 void IrcCommunication::setIgnoreInvalidCert(bool ignore)
162 QObject::connect(session->socket(),SIGNAL(),this,SLOT(sslError(QList<QSslError>)));