File: icqdieplugin.cpp

package info (click to toggle)
psi-plugins 1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,368 kB
  • sloc: cpp: 42,063; xml: 714; ansic: 84; makefile: 61; sh: 12
file content (432 lines) | stat: -rw-r--r-- 13,226 bytes parent folder | download
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
 * icqdieplugin.cpp - plugin
 * Copyright (C) 2009  Ivan Borzenkov <ivan1986@list.ru>
 *
 * THE BEER-WARE LICENSE (Revision 42):
 * I wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day,
 * and you think this stuff is worth it, you can buy a beer in return.
 *
 * Alternatively, this file may be used under the terms of the GNU
 * General Public License version 2 or (at your option) any later version
 * or GNU Lesser General Public License version 2 or (at your option) any
 * later version as published by the Free Software Foundation and
 * appearing in the file copying.txt included in the packaging of this file.
 * Please review the following information to ensure the GNU General
 * Public License version 2.0 requirements will be met:
 * <http://www.gnu.org/licenses/>.
 */

#include <QtGui>
#include <QtCore>
#include <QMap>
#include <QDomElement>

#include "psiplugin.h"
#include "optionaccessor.h"
#include "optionaccessinghost.h"
#include "stanzafilter.h"
#include "stanzasender.h"
#include "stanzasendinghost.h"
#include "activetabaccessor.h"
#include "activetabaccessinghost.h"
#include "accountinfoaccessor.h"
#include "accountinfoaccessinghost.h"
#include "plugininfoprovider.h"
#include "ui_icqdieoptions.h"

#define cVer "0.1.6"
#define constMessageRecv "msgr"
#define constMessageNoRecv "msgnr"
#define constCustom "custom"
#define constActiveTab "actvtb"
#define constPauseTime "whttm"
#define constMessageCount "msgcnt"
#define constTransports "transp"

class IcqDie: public QObject, public PsiPlugin, public OptionAccessor, public StanzaSender,  public StanzaFilter, public ActiveTabAccessor,
		public AccountInfoAccessor, public PluginInfoProvider
{
	Q_OBJECT
#ifdef HAVE_QT5
	Q_PLUGIN_METADATA(IID "com.psi-plus.IcqDie")
#endif
		Q_INTERFACES(PsiPlugin OptionAccessor StanzaSender StanzaFilter ActiveTabAccessor
			     AccountInfoAccessor PluginInfoProvider)

public:
	IcqDie();
	virtual QString name() const;
	virtual QString shortName() const;
	virtual QString version() const;
	virtual QWidget* options();
	virtual bool enable();
	virtual bool disable();
	virtual void applyOptions();
	virtual void restoreOptions();
	virtual void setOptionAccessingHost(OptionAccessingHost* host);
	virtual void optionChanged(const QString& option);
	virtual void setStanzaSendingHost(StanzaSendingHost *host);
	virtual bool incomingStanza(int account, const QDomElement& stanza);
	virtual bool outgoingStanza(int account, QDomElement& stanza);
	virtual void setActiveTabAccessingHost(ActiveTabAccessingHost* host);
	virtual void setAccountInfoAccessingHost(AccountInfoAccessingHost* host);
	virtual QString pluginInfo();
	virtual QPixmap icon() const;

private:
	bool enabled;
	AccountInfoAccessingHost* AccInfoHost;
	ActiveTabAccessingHost* ActiveTabHost;
	OptionAccessingHost* psiOptions;
	StanzaSendingHost* StanzaHost;
	QString MessageRecv;
	QString MessageNoRecv;
	typedef QPair<QDateTime,int> CounterInfo;
	QMap<QString, CounterInfo> Counter;
	enum stat {
		ignore = '-', send = '+', block = '!',
	};
	typedef QMap<QString, stat> CustomList;
	CustomList Custom;
	QVector<QString> Transports;
	CustomList ParseCustomText(QString sCustom);
	int PauseTime;
	int MessageCount;
	bool ActiveTabIsEnable;
	Ui::options ui;

};

#ifndef HAVE_QT5
Q_EXPORT_PLUGIN(IcqDie);
#endif

IcqDie::IcqDie()
{
	ActiveTabIsEnable = true;

	Custom.clear();
	Custom["other"] = send;
	Custom["nil"] = ignore;

	Counter.clear();

	Transports.clear();
	Transports << "icq" << "jit";

	PauseTime = 120;
	MessageCount = 0;

	enabled = false;
	MessageRecv = trUtf8("I can tell you as a Linux, but do not be mad at me. "
            "Certainly this human will receive this message. But it's much better to chat to him by Jabber. "
            "You are risking, one never knows when ICQ can die, granny already outlived its. His JID: %1.\n\n"
            "Sincerely yours, Debian Sid.");
	MessageNoRecv = trUtf8("I can tell you as a Linux, but do not be mad at me. "
            "This human do not use ICQ anymore, so if you are still use that network he will not receive your message and you have to chat to him by Jabber. His JID: %1.\n\n"
            "If you don't know what Jabber is - use Google. It knows everything and ready to help everyone who ask it.\n"
            "Sincerely yours, Debian Sid.");


	ActiveTabHost = 0;
	AccInfoHost = 0;
	psiOptions = 0;
	StanzaHost = 0;
}

QString IcqDie::name() const { return "Icq Must Die Plugin"; }

QString IcqDie::shortName() const { return "icqdie"; }

QString IcqDie::version() const { return cVer; }

IcqDie::CustomList IcqDie::ParseCustomText(QString sCustom)
{
	IcqDie::CustomList Custom;
	Custom.clear();
	QStringList Clist;
	Clist = sCustom.split(QRegExp("\n"), QString::SkipEmptyParts);
	while(!Clist.isEmpty())
	{
		//удаляем пробелы и комментарии
		QString C = Clist.takeFirst().remove(QRegExp("\\s+")).remove(QRegExp("\\#.*$"));
		stat s;
		QString id = C;
		id.remove(0,1);
		if (C[0] == '-')
			s = ignore;
		else if (C[0] == '!')
			s = block;
		else if (C[0] == '+')
			s = send;
		else
		{
			s = send;
			//прилепляем назад
			id = C[0] + id;
		}
		Custom[id] = s;
	}
	//если удалили дефолтовые, то восстанавливаем
	if (Custom.find("nil") == Custom.end())
		Custom["nil"] = ignore;
	if (Custom.find("other") == Custom.end())
		Custom["other"] = send;
	//qDebug() << "ParseCustomText" << Custom;
	return Custom;
}

bool IcqDie::enable() {
	if (!psiOptions)
		return enabled;

	enabled = true;

	MessageRecv = psiOptions->getPluginOption(constMessageRecv, QVariant(MessageRecv)).toString();
	MessageNoRecv = psiOptions->getPluginOption(constMessageNoRecv, QVariant(MessageNoRecv)).toString();

	PauseTime = psiOptions->getPluginOption(constPauseTime, QVariant(PauseTime)).toInt();
	MessageCount = psiOptions->getPluginOption(constMessageCount, QVariant(MessageCount)).toInt();
	ActiveTabIsEnable = psiOptions->getPluginOption(constActiveTab, QVariant(ActiveTabIsEnable)).toBool();

	QVariant vCustom;
	vCustom = psiOptions->getPluginOption(constCustom);
	if (!vCustom.isNull())
		Custom = ParseCustomText(vCustom.toString());

	QVariant vTransports;
	vTransports = psiOptions->getPluginOption(constTransports);
	if (!vTransports.isNull())
	{
		QString sTransports = vTransports.toString();
		Transports.clear();
		QStringList Tlist;
		Tlist = sTransports.split(QRegExp("\n"), QString::SkipEmptyParts);
		while(!Tlist.isEmpty())
			Transports << Tlist.takeFirst().remove(QRegExp("\\s+"));
	}

	return enabled;
}

bool IcqDie::disable()
{
	enabled = false;
	return true;
}

void IcqDie::applyOptions()
{
	psiOptions->setPluginOption(constMessageRecv, QVariant(MessageRecv = ui.messageRecv->toPlainText()));
	psiOptions->setPluginOption(constMessageNoRecv, QVariant(MessageNoRecv = ui.messageNoRecv->toPlainText()));

	QString sCustom;
	psiOptions->setPluginOption(constCustom, QVariant(sCustom = ui.custom->toPlainText()));
	Custom = ParseCustomText(sCustom);

	psiOptions->setPluginOption(constActiveTab, QVariant(ActiveTabIsEnable = ui.activetabWidget->isChecked()));
	psiOptions->setPluginOption(constMessageCount, QVariant(MessageCount = ui.messageCount->value()));
	psiOptions->setPluginOption(constPauseTime, QVariant(PauseTime = ui.pauseWidget->value()));

	//сохранили и разобрали парсеры
	QString sTransports;
	psiOptions->setPluginOption(constTransports, QVariant(sTransports = ui.transportsWidget->toPlainText()));

	Transports.clear();
	QStringList Tlist;
	Tlist = sTransports.split(QRegExp("\n"), QString::SkipEmptyParts);
	while(!Tlist.isEmpty())
		Transports << Tlist.takeFirst().remove(QRegExp("\\s+"));
}

void IcqDie::restoreOptions()
{

	ui.messageRecv->setText(MessageRecv);
	ui.messageNoRecv->setText(MessageNoRecv);
	ui.messageCount->setValue(MessageCount);
	ui.pauseWidget->setValue(PauseTime);
	ui.activetabWidget->setChecked(ActiveTabIsEnable);

	ui.custom->setText(psiOptions->getPluginOption(constCustom, QVariant("+other\n-nil")).toString());

	QString text;
	foreach(QString t,Transports)
	{
		if (!text.isEmpty())
			text += "\n";
		text += t;
	}
	ui.transportsWidget->setText(text);
}

QWidget* IcqDie::options()
{
	if (!enabled)
		return 0;

	QWidget *options = new QWidget;
	ui.setupUi(options);
	ui.wiki->setText(tr("<a href=\"http://psi-plus.com/wiki/plugins#icq_must_die_plugin\">Wiki (Online)</a>"));
	ui.wiki->setOpenExternalLinks(true);

	restoreOptions();

	return options;
}

void IcqDie::setOptionAccessingHost(OptionAccessingHost* host) { psiOptions = host; }

void IcqDie::optionChanged(const QString& option) { Q_UNUSED(option); }

void IcqDie::setStanzaSendingHost(StanzaSendingHost *host) { StanzaHost = host; }

bool IcqDie::incomingStanza(int account, const QDomElement& stanza)
{
	if (!enabled)
		return false;
	if (stanza.tagName() != "message")
		return false;

	//реагируем только на чат и на сообщение
	QString type = stanza.attribute("type");
	if(type != "chat" && type != "")
		return false;

	QDomElement Body = stanza.firstChildElement("body");
	//если пустое сообщение, то ничего не делаем
	if(Body.isNull())
		return false;
	QDomElement rec =  stanza.firstChildElement("received");
	if(!rec.isNull())
		return false;

	QString from = stanza.attribute("from"); QStringList f = from.split("/");
	QString valF = f.takeFirst(); QStringList fid = valF.split("@");
	if (fid.count() < 2)
		return false;
	QString idF = fid.takeFirst();
	QString server = fid.takeFirst();
	QString to = stanza.attribute("to"); QStringList t = to.split("/");
	QString valT = t.takeFirst();

	//игнорируем сообщения от всех, кромя транспортов
	bool fromTransport = false;
	foreach(QString Transport, Transports)
		if (server.indexOf(Transport, Qt::CaseInsensitive) == 0)
			fromTransport = true;
	if(!fromTransport)
		return false;

	//разбираемся от кого оно
	stat todo = Custom["nil"];
	if (Custom.find(idF) != Custom.end()) //нашли в списке - делаем что указано
		todo = Custom[idF];
	else //проверяем, есть ли он в ростере
	{
		QStringList Roster = AccInfoHost->getRoster(account);
		while(!Roster.isEmpty())
		{
			QString jid = Roster.takeFirst();
			if(valF.toLower() == jid.toLower())
				todo = Custom["other"];
		}
	}

	//если игнорировать - пропускаем и ничего
	if (todo == ignore)
		return false;

	//если блокировать - отправляем сообщение и давим его
	if (todo == block)
	{
		if(!Counter.contains(from))
			Counter[from].second = 0;
		Counter[from].second++;
		//не посылать больше N раз
		if (MessageCount > 0 && Counter[from].second > MessageCount)
			return true;

		QString mes = "<message to='" + from + "'";
		if(type != "")
			mes += " type='" + type + "'";
		else
			mes += "><subject>IcqDie</subject";
		mes += "><body>" + MessageNoRecv.arg(valT) + "</body></message>";
		StanzaHost->sendStanza(account, mes);
		return true;
	}

	//если уже посылали, то таймаут
	if(!Counter.contains(from))
		Counter[from].first = QDateTime::currentDateTime();
	else
	{
		QDateTime old = Counter[from].first;
		Counter[from].first = QDateTime::currentDateTime();
		if(QDateTime::currentDateTime().secsTo(old) >= -PauseTime*60)
			return false;
	}

	//если пришло сообщение в активный чат, то не посылать
	if(ActiveTabIsEnable)
	{
		QString getJid = ActiveTabHost->getJid();
		if(getJid.toLower() == from.toLower())
			return false;
	}

	if(!Counter.contains(from))
		Counter[from].second = 0;
	Counter[from].second++;
	//не посылать больше N раз
	if (MessageCount > 0 && Counter[from].second > MessageCount)
		return false;

	//отправляем сообщение
	QString mes = "<message to='" + from + "'";
	if(type != "")
		mes += " type='" + type + "'";
	else
		mes += "><subject>IcqDie</subject";
	mes += "><body>" + MessageRecv.arg(valT) + "</body></message>";
	StanzaHost->sendStanza(account, mes);

	return false;
}

bool IcqDie::outgoingStanza(int /*account*/, QDomElement& /*stanza*/)
{
	return false;
}

void IcqDie::setActiveTabAccessingHost(ActiveTabAccessingHost* host)
{
	ActiveTabHost = host;
}

void IcqDie::setAccountInfoAccessingHost(AccountInfoAccessingHost* host)
{
	AccInfoHost = host;
}

QString IcqDie::pluginInfo()
{
	return tr("Author: ") +  "ivan1986\n\n"
			+ trUtf8("This plugin is designed to help you transfer as many contacts as possible from ICQ to Jabber.\n"
			 "The plugin has a number of simple settings that can help you:\n"
			 "* set a special message text\n"
			 "* exclude specific ICQ numbers\n"
			 "* set the time interval after which the message will be repeated\n"
			 "* set the max count of messages by contact\n"
			 "* disable the message for the active window/tab\n"
			 "* disable messages for contacts that are not in your roster");
}

QPixmap IcqDie::icon() const
{
	return QPixmap(":/icons/icqdie.png");
}

#include "icqdieplugin.moc"