2
kwrited is a write(1) receiver for KDE.
3
Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
4
Copyright 2008 by George Kiagiadakis <gkiagia@users.sourceforge.net>
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26
#include <kptydevice.h>
28
#include <knotification.h>
29
#include <klocalizedstring.h>
30
#include <kaboutdata.h>
31
#include <kdeversion.h>
33
#if defined(BUILD_AS_EXECUTABLE)
34
# include <QtCore/QCoreApplication>
35
# include <kcomponentdata.h>
37
# include <sys/types.h>
40
# include <kpluginfactory.h>
41
# include <kpluginloader.h>
44
static inline KAboutData aboutData()
46
return KAboutData("kwrited", 0, ki18n("kwrited"), KDE_VERSION_STRING);
49
#if defined(BUILD_AS_EXECUTABLE)
51
static uid_t original_euid;
52
static gid_t original_egid;
54
static void sigterm_handler(int signal)
56
kDebug() << "Caught signal" << signal << ", exiting...";
57
QCoreApplication::quit();
60
int main(int argc, char **argv)
62
//drop elevated privileges temporarily
63
original_euid = geteuid();
64
original_egid = getegid();
68
//install a signal handler to exit gracefully
69
//(so that pty->logout() is called in ~KWrited())
70
signal(SIGTERM, sigterm_handler);
71
signal(SIGINT, sigterm_handler);
72
signal(SIGHUP, sigterm_handler);
74
KComponentData kcompdata(aboutData());
75
QCoreApplication a(argc, argv);
80
#else // BUILD_AS_EXECUTABLE
82
KWritedModule::KWritedModule(QObject* parent, const QList<QVariant>&)
88
KWritedModule::~KWritedModule()
93
K_PLUGIN_FACTORY(KWritedFactory,
94
registerPlugin<KWritedModule>();
96
K_EXPORT_PLUGIN(KWritedFactory(aboutData()))
98
#endif //BUILD_AS_EXECUTABLE
101
KWrited::KWrited() : QObject()
103
pty = new KPtyDevice();
106
#if defined(BUILD_AS_EXECUTABLE)
107
dup2(pty->slaveFd(), 0); //HACK: login() from glibc requires this to login correctly
108
//restore elevated privileges
109
seteuid(original_euid);
110
setegid(original_egid);
113
pty->login(KUser(KUser::UseRealUserID).loginName().toLocal8Bit().data(), qgetenv("DISPLAY"));
115
#if defined(BUILD_AS_EXECUTABLE)
116
//drop privileges again
121
connect(pty, SIGNAL(readyRead()), this, SLOT(block_in()));
122
kDebug() << "listening on device" << pty->ttyName();
127
#if defined(BUILD_AS_EXECUTABLE)
128
//restore elevated privileges
129
seteuid(original_euid);
130
setegid(original_egid);
135
#if defined(BUILD_AS_EXECUTABLE)
136
//drop privileges again
144
void KWrited::block_in()
146
QByteArray buf = pty->readAll();
147
QString msg = QString::fromLocal8Bit( buf.constData(), buf.size() );
151
KNotification *notification = new KNotification("NewMessage", 0, KNotification::Persistent);
152
#if !defined(BUILD_AS_EXECUTABLE)
153
notification->setComponentData( KWritedFactory::componentData() );
155
notification->setText( msg );
156
connect(notification, SIGNAL(closed()), notification, SLOT(deleteLater()) );
157
notification->sendEvent();
160
#include "kwrited.moc"