summaryrefslogtreecommitdiff
path: root/plugins/bluemonkey/irccoms.cpp
blob: 1cdb12e4bafb5c6c82281c65a57ba76fb486ddb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "irccoms.h"
#include <QtDebug>
#include <IrcCommand>
#include <IrcMessage>
#include <QSslSocket>
#include <QFile>
#include <QNetworkProxy>
#include <QTimer>

#define foreach Q_FOREACH

IrcCommunication::IrcCommunication(QObject* parent)
	:QObject(parent)
{
	session = new IrcSession(this);
	mSsl=false;

	QObject::connect(session,SIGNAL(connected()),this,SIGNAL(connected()));
	QObject::connect(session,SIGNAL(disconnected()),this,SIGNAL(disconnected()));
	QObject::connect(session,SIGNAL(disconnected()),this,SLOT(reconnect()));
	QObject::connect(session,SIGNAL(socketError(QAbstractSocket::SocketError)),this,SLOT(socketError(QAbstractSocket::SocketError)));
	QObject::connect(session,SIGNAL(connecting()),this,SIGNAL(connecting()));
	QObject::connect(session,SIGNAL(messageReceived(IrcMessage*)),this,SLOT(messageReceived(IrcMessage*)));

}

void IrcCommunication::announce(QString s)
{
	qDebug()<<"channels: "<<mChannels;
	foreach(QString channel, mChannels)
	{
		qDebug()<<"sending "<<s<<" to channel: "<<channel;
		IrcCommand command;
		command.setType(IrcCommand::Message);
		command.setParameters(QStringList()<<channel<<s);
		session->sendCommand(&command);
	}
}

void IrcCommunication::respond(QString target, QString s)
{
	IrcCommand command;
	command.setType(IrcCommand::Message);
	command.setParameters(QStringList()<<target<<s);
	session->sendCommand(&command);
}

void IrcCommunication::messageReceived(IrcMessage *msg)
{
	qDebug()<<"message received "<<msg->type()<<" prefix: "<<msg->sender().prefix()<<" params:"<<msg->parameters();

	if(msg->type() == IrcMessage::Private)
	{
		if(msg->parameters().count() > 1)
		{
			QString sender = msg->parameters().at(0);
			QString m = msg->parameters().at(1);

			message(sender,msg->sender().prefix(),m);
		}

	}
}

void IrcCommunication::connect(QString host, int port, QString proxy, QString user, QString nick, QString pass)
{
	session->setHost(host);
	session->setPort(port);
	session->setUserName(user);
	session->setNickName(nick);
	session->setRealName(nick);

	if(!proxy.isEmpty())
	{
		if(!proxy.contains(":"))
		{
			qDebug("proxy format must be 'address:port'");
			return;
		}
		QString host = proxy.split(":").at(0);
		int port = proxy.split(":").at(1).toInt();

		QNetworkProxy netproxy;
		netproxy.setType(QNetworkProxy::Socks5Proxy);
		netproxy.setHostName(host);
		netproxy.setPort(port);

		QNetworkProxy::setApplicationProxy(netproxy);
	}

	qDebug()<<"opening irc session";
	session->open();
}

void IrcCommunication::setSsl(bool use)
{
	if(use)
		session->setSocket(new QSslSocket(this));
}

void IrcCommunication::join(QString channel)
{
	if(!mChannels.contains(channel))
		mChannels.append(channel);

	IrcCommand command;
	command.setType(IrcCommand::Join);
	command.setParameters(QStringList()<<channel);
	session->sendCommand(&command);
}

void IrcCommunication::reconnect()
{
	QTimer::singleShot(5000,session,SLOT(open()));
}

/*void IrcCommunication::sslError(QList<QSslError>)
{
	qDebug()<<"some ssl errors!! trying to ignore them";
	QSslSocket* socket = qobject_cast<QSslSocket*>(session->socket());
	if(socket)
	{
		socket->ignoreSslErrors();
	}
}*/

void IrcCommunication::socketError(QAbstractSocket::SocketError error)
{
	qDebug()<<"Socket error.  attempting to reconnect..."<<error;
	reconnect();
}

void IrcCommunication::setIgnoreInvalidCert(bool ignore)
{
	if(ignore)
		QObject::connect(session->socket(),SIGNAL(),this,SLOT(sslError(QList<QSslError>)));
}