├── Lua4RS_de.qm ├── Lua4RS_en.qm ├── lang ├── Lua4RS_de.qm ├── Lua4RS_en.qm └── Lua4RS_de.ts ├── images └── lua_logo.png ├── Lua4RS_images.qrc ├── contrib └── get_lua5.2.3.sh ├── Lua4RS_lang.qrc ├── .gitignore ├── scripts ├── chatTest.lua ├── offlineChecker.lua ├── groupTest.lua ├── speedControl.lua ├── simpleDownloadBot.lua ├── friendsVersionOverview.lua └── buryTheDead.lua ├── Lua ├── Trigger │ ├── LuaTriggerStartup.h │ ├── LuaTriggerShutdown.h │ ├── LuaTriggerEvent.h │ ├── LuaTriggerOnce.h │ ├── LuaTriggerStartup.cpp │ ├── LuaTriggerShutdown.cpp │ ├── LuaTriggerTimerInterval.h │ ├── LuaTriggerBase.h │ ├── LuaTriggerBase.cpp │ ├── LuaTriggerEvent.cpp │ ├── LuaTriggerOnce.cpp │ └── LuaTriggerTimerInterval.cpp ├── LuaCode.h ├── LuaToRSChat.cpp ├── LuaCode.cpp ├── LuaToRS.h ├── LuaList.h ├── LuaContainer.h ├── LuaCore.h ├── LuaConfig.h ├── LuaToRSDiscovery.cpp ├── LuaToRS.cpp ├── LuaToRSServerConfig.cpp ├── LuaEvent.h ├── LuaContainer.cpp ├── LuaConfig.cpp ├── LuaList.cpp ├── LuaCore.cpp └── LuaToRSPeers.cpp ├── pseudo-documentation.md ├── interface └── L4RInterface.h ├── gui ├── Lua4RSConfig.cpp ├── Lua4RSConfig.h ├── Lua4RSWidget.h ├── Lua4RSConfig.ui └── Lua4RSWidget.cpp ├── README.md ├── _todo.md ├── helper.h ├── service ├── p3Lua4RS.h └── p3Lua4RS.cpp ├── Lua4RSPlugin.h ├── Lua4RS.pro ├── Lua4RSPlugin.cpp ├── Lua4RSNotify.h ├── Lua4RSNotify.cpp └── LICENSE /Lua4RS_de.qm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chozabu/Lua4RS/master/Lua4RS_de.qm -------------------------------------------------------------------------------- /Lua4RS_en.qm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chozabu/Lua4RS/master/Lua4RS_en.qm -------------------------------------------------------------------------------- /lang/Lua4RS_de.qm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chozabu/Lua4RS/master/lang/Lua4RS_de.qm -------------------------------------------------------------------------------- /lang/Lua4RS_en.qm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chozabu/Lua4RS/master/lang/Lua4RS_en.qm -------------------------------------------------------------------------------- /images/lua_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chozabu/Lua4RS/master/images/lua_logo.png -------------------------------------------------------------------------------- /Lua4RS_images.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | images/lua_logo.png 4 | 5 | 6 | -------------------------------------------------------------------------------- /contrib/get_lua5.2.3.sh: -------------------------------------------------------------------------------- 1 | 2 | wget http://www.lua.org/ftp/lua-5.2.3.tar.gz 3 | tar zxf lua-5.2.3.tar.gz 4 | cd lua-5.2.3 5 | make linux 6 | make install 7 | -------------------------------------------------------------------------------- /Lua4RS_lang.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | lang/Lua4RS_en.qm 4 | lang/Lua4RS_de.qm 5 | 6 | 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ objects and libs 2 | 3 | *.slo 4 | *.lo 5 | *.o 6 | *.a 7 | *.la 8 | *.lai 9 | *.so 10 | *.so.* 11 | *.dll 12 | *.dylib 13 | 14 | # Qt-es 15 | 16 | /.qmake.cache 17 | /.qmake.stash 18 | *.pro.user 19 | *.pro.user.* 20 | *.moc 21 | moc_*.cpp 22 | qrc_*.cpp 23 | ui_*.h 24 | Makefile* 25 | *-build-* 26 | 27 | # QtCreator 28 | 29 | *.autosave 30 | -------------------------------------------------------------------------------- /scripts/chatTest.lua: -------------------------------------------------------------------------------- 1 | rs.clear() 2 | 3 | msg = args.msg 4 | chatid = args.chatid 5 | 6 | rs.print("got message from lobby " .. chatid) 7 | 8 | find = string.find(msg, "ping") 9 | if find ~= nil then 10 | rs.print("found ping") 11 | chat.sendChat(chatid, "[Lua] pong") 12 | end 13 | 14 | find = string.find(msg, "PING") 15 | if find ~= nil then 16 | rs.print("found ping") 17 | chat.sendChat(chatid, "[Lua] PONG (for the funny guy)") 18 | end 19 | -------------------------------------------------------------------------------- /Lua/Trigger/LuaTriggerStartup.h: -------------------------------------------------------------------------------- 1 | #ifndef LUATRIGGERSTARTUP_H 2 | #define LUATRIGGERSTARTUP_H 3 | 4 | #include "LuaTriggerBase.h" 5 | 6 | class LuaTriggerStartup : public LuaTriggerBase 7 | { 8 | public: 9 | LuaTriggerStartup(); 10 | ~LuaTriggerStartup(); 11 | 12 | bool isTriggered (const LuaEvent& luaevent); 13 | 14 | void toSettings(QSettings& mySettings); 15 | 16 | void fromSettings(const QSettings& mySettings); 17 | 18 | QString classname(); 19 | }; 20 | 21 | #endif // LUATRIGGERSTARTUP_H 22 | -------------------------------------------------------------------------------- /Lua/Trigger/LuaTriggerShutdown.h: -------------------------------------------------------------------------------- 1 | #ifndef LUATRIGGERSHUTDOWN_H 2 | #define LUATRIGGERSHUTDOWN_H 3 | 4 | #include "LuaTriggerBase.h" 5 | 6 | class LuaTriggerShutdown : public LuaTriggerBase 7 | { 8 | public: 9 | LuaTriggerShutdown(); 10 | ~LuaTriggerShutdown(); 11 | 12 | bool isTriggered (const LuaEvent& luaevent); 13 | 14 | void toSettings(QSettings& mySettings); 15 | 16 | void fromSettings(const QSettings& mySettings); 17 | 18 | QString classname(); 19 | }; 20 | 21 | #endif // LUATRIGGERSHUTDOWN_H 22 | -------------------------------------------------------------------------------- /Lua/LuaCode.h: -------------------------------------------------------------------------------- 1 | #ifndef LUACODE_H 2 | #define LUACODE_H 3 | 4 | class QString; 5 | 6 | class LuaCode 7 | { 8 | public: 9 | LuaCode(); 10 | LuaCode(QString name); 11 | LuaCode(QString name, QString code); 12 | 13 | // bool save(); 14 | 15 | // getter/setter 16 | QString code() const; 17 | void setCode(const QString &code); 18 | 19 | QString name() const; 20 | void setName(const QString &name); 21 | 22 | private: 23 | QString _code; 24 | QString _name; 25 | }; 26 | 27 | #endif // LUACODE_H 28 | -------------------------------------------------------------------------------- /pseudo-documentation.md: -------------------------------------------------------------------------------- 1 | not really a documentation - more an overview 2 | 3 | Lua4RSPlugin 4 | |- Lua4RSWidget 5 | | |- LuaCore 6 | | 7 | |- (RsPeers) 8 | |- (RsNotify) 9 | |- (RsPluginHandler) 10 | 11 | LuaCore (singleton) 12 | |- lua_state 13 | |- LuaList 14 | | |- LuaContainer 15 | | |- LuaCode 16 | | | |- Code 17 | | | |- Description 18 | | | |- Name 19 | | | 20 | | |- LuaConfig 21 | | 22 | |- Lua4RSNotify 23 | |- Lua4RSTickThread 24 | |- Lua4RSWidget 25 | |- (RsPeers) 26 | -------------------------------------------------------------------------------- /scripts/offlineChecker.lua: -------------------------------------------------------------------------------- 1 | -- change this 2 | limit = 60 * 60 * 24 * 14 -- two weeks (in seconds) 3 | 4 | now = os.time() 5 | 6 | rs.clear() 7 | rs.print("long time offline friends:") 8 | friends = peers.getFriendList() 9 | for i = 1, #friends do 10 | f = friends[i] 11 | d = peers.getPeerDetails(f) 12 | lastTimeOnline = d["lastUsed"] 13 | diff = now - lastTimeOnline 14 | if(diff > limit) then 15 | -- how long in days 16 | days = diff / 60 / 60 / 24 17 | days = math.ceil(days) 18 | rs.print(d["name"] .. " since " .. days .. " days") 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /Lua/LuaToRSChat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "LuaCore.h" 4 | #include "LuaToRS.h" 5 | 6 | extern "C" { 7 | #include 8 | #include 9 | #include 10 | } 11 | 12 | extern "C" { 13 | int chat_sendChat(lua_State* L) 14 | { 15 | luaL_checktype(L, 1, LUA_TSTRING); 16 | 17 | const std::string s = luaL_checkstring(L, 1); 18 | const ChatId id = ChatId(s); 19 | const std::string msg = luaL_checkstring(L, 2); 20 | const bool ret = rsMsgs->sendChat(id, msg); 21 | lua_pushboolean(L, ret); 22 | return 1; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Lua/Trigger/LuaTriggerEvent.h: -------------------------------------------------------------------------------- 1 | #ifndef LUATRIGGEREVENT_H 2 | #define LUATRIGGEREVENT_H 3 | 4 | #include "LuaTriggerBase.h" 5 | 6 | class LuaTriggerEvent : public LuaTriggerBase 7 | { 8 | public: 9 | LuaTriggerEvent(); 10 | LuaTriggerEvent(uint eventId ); 11 | ~LuaTriggerEvent(); 12 | 13 | bool isTriggered (const LuaEvent& luaevent); 14 | 15 | void toSettings(QSettings& mySettings); 16 | 17 | void fromSettings(const QSettings &mySettings); 18 | 19 | QString classname(); 20 | 21 | uint getEventId(); 22 | 23 | protected: 24 | uint _eventId; 25 | }; 26 | 27 | #endif // LUATRIGGEREVENT_H 28 | -------------------------------------------------------------------------------- /Lua/Trigger/LuaTriggerOnce.h: -------------------------------------------------------------------------------- 1 | #ifndef LUATRIGGERONCE_H 2 | #define LUATRIGGERONCE_H 3 | 4 | #include "LuaTriggerBase.h" 5 | 6 | class LuaTriggerOnce : public LuaTriggerBase 7 | { 8 | public: 9 | LuaTriggerOnce(); 10 | LuaTriggerOnce(const QDateTime& onceDateTime); 11 | ~LuaTriggerOnce(); 12 | 13 | bool isTriggered (const LuaEvent& luaevent); 14 | 15 | void toSettings(QSettings& mySettings); 16 | 17 | void fromSettings(const QSettings &mySettings); 18 | 19 | QString classname(); 20 | 21 | QDateTime getValues(); 22 | 23 | protected: 24 | QDateTime _onceDateTime; 25 | }; 26 | 27 | #endif // LUATRIGGERONCE_H 28 | -------------------------------------------------------------------------------- /Lua/LuaCode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "LuaCode.h" 4 | 5 | LuaCode::LuaCode() : 6 | _code(""), 7 | _name("") 8 | { 9 | } 10 | 11 | LuaCode::LuaCode(QString name) : 12 | _code(""), 13 | _name(name) 14 | { 15 | } 16 | 17 | LuaCode::LuaCode(QString name, QString code) : 18 | _code(code), 19 | _name(name) 20 | { 21 | } 22 | 23 | // getter/setter 24 | QString LuaCode::code() const 25 | { 26 | return _code; 27 | } 28 | 29 | void LuaCode::setCode(const QString &code) 30 | { 31 | _code = code; 32 | } 33 | 34 | QString LuaCode::name() const 35 | { 36 | return _name; 37 | } 38 | 39 | void LuaCode::setName(const QString &name) 40 | { 41 | _name = name; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /interface/L4RInterface.h: -------------------------------------------------------------------------------- 1 | #ifndef LUA4RSINTERFACE_H 2 | #define LUA4RSINTERFACE_H 3 | 4 | #ifdef _WIN32 5 | typedef unsigned int uint; 6 | #else 7 | #include 8 | #endif 9 | 10 | class L4RInterface; 11 | class LuaCore; 12 | 13 | namespace L4R { 14 | extern L4RInterface* L4RConfig; 15 | } 16 | 17 | class L4RInterface 18 | { 19 | public: 20 | virtual ~L4RInterface() {} 21 | 22 | virtual LuaCore* getCore() = 0; 23 | 24 | // config 25 | virtual uint getTickIntervalInSeconds() const = 0; 26 | virtual void setTickIntervalInSeconds(const uint &value) = 0; 27 | 28 | virtual uint getSecondsToStarUpEvent() const = 0; 29 | virtual void setSecondsToStarUpEvent(const uint &value) = 0; 30 | }; 31 | 32 | #endif // LUA4RSINTERFACE_H 33 | -------------------------------------------------------------------------------- /gui/Lua4RSConfig.cpp: -------------------------------------------------------------------------------- 1 | #include "ui_Lua4RSConfig.h" 2 | #include "Lua4RSConfig.h" 3 | #include "interface/L4RInterface.h" 4 | 5 | Lua4RSConfig::Lua4RSConfig(QWidget *parent) : 6 | ConfigPage(parent), 7 | ui(new Ui::Lua4RSConfig) 8 | { 9 | ui->setupUi(this); 10 | } 11 | 12 | Lua4RSConfig::~Lua4RSConfig() 13 | { 14 | delete ui; 15 | } 16 | 17 | void Lua4RSConfig::load() 18 | { 19 | ui->sb_starupEvent->setValue(L4R::L4RConfig->getSecondsToStarUpEvent()); 20 | ui->sb_tickInterval->setValue(L4R::L4RConfig->getTickIntervalInSeconds()); 21 | } 22 | 23 | bool Lua4RSConfig::save(QString& /*errmsg*/) 24 | { 25 | L4R::L4RConfig->setSecondsToStarUpEvent(ui->sb_starupEvent->value()); 26 | L4R::L4RConfig->setTickIntervalInSeconds(ui->sb_tickInterval->value()); 27 | return true; 28 | } 29 | -------------------------------------------------------------------------------- /Lua/Trigger/LuaTriggerStartup.cpp: -------------------------------------------------------------------------------- 1 | #include "LuaTriggerStartup.h" 2 | 3 | LuaTriggerStartup::LuaTriggerStartup() 4 | { 5 | _classname = LUA_TRIGGER_STARTUP; 6 | } 7 | 8 | LuaTriggerStartup::~LuaTriggerStartup() {} 9 | 10 | bool LuaTriggerStartup::isTriggered (const LuaEvent& luaevent) 11 | { 12 | if ( luaevent.eventId == L4R_STARTUP ) 13 | { 14 | _lastRun = QDateTime().currentDateTime(); 15 | return true; 16 | } 17 | return false; 18 | } 19 | 20 | 21 | void LuaTriggerStartup::toSettings(QSettings &mySettings) 22 | { 23 | LuaTriggerBase::toSettings(mySettings); 24 | } 25 | 26 | void LuaTriggerStartup::fromSettings (const QSettings& mySettings) 27 | { 28 | LuaTriggerBase::fromSettings(mySettings); 29 | } 30 | 31 | QString LuaTriggerStartup::classname() 32 | { 33 | return _classname; 34 | } 35 | -------------------------------------------------------------------------------- /Lua/Trigger/LuaTriggerShutdown.cpp: -------------------------------------------------------------------------------- 1 | #include "LuaTriggerShutdown.h" 2 | 3 | LuaTriggerShutdown::LuaTriggerShutdown() 4 | { 5 | _classname = LUA_TRIGGER_SHUTDOWN; 6 | } 7 | 8 | LuaTriggerShutdown::~LuaTriggerShutdown() {} 9 | 10 | bool LuaTriggerShutdown::isTriggered (const LuaEvent& luaevent) 11 | { 12 | if (luaevent.eventId == L4R_SHUTDOWN) 13 | { 14 | _lastRun = QDateTime().currentDateTime(); 15 | return true; 16 | } 17 | return false; 18 | } 19 | 20 | void LuaTriggerShutdown::toSettings (QSettings& mySettings) 21 | { 22 | LuaTriggerBase::toSettings(mySettings); 23 | } 24 | 25 | void LuaTriggerShutdown::fromSettings (const QSettings &mySettings) 26 | { 27 | LuaTriggerBase::fromSettings(mySettings); 28 | } 29 | 30 | QString LuaTriggerShutdown::classname() 31 | { 32 | return _classname; 33 | } 34 | 35 | 36 | -------------------------------------------------------------------------------- /Lua/LuaToRS.h: -------------------------------------------------------------------------------- 1 | #ifndef LUATORS_H 2 | #define LUATORS_H 3 | 4 | #include "LuaCore.h" 5 | 6 | extern "C" { 7 | int rs_print(lua_State* L); 8 | int rs_clear(lua_State* L); 9 | } 10 | 11 | int inline getArgCount(lua_State* L); 12 | 13 | void pushTable(lua_State* L, int tableTop, const std::string& name, int value); 14 | void pushTable(lua_State* L, int tableTop, const std::string& name, uint value); 15 | void pushTable(lua_State* L, int tableTop, const std::string& name, const std::string& value); 16 | void pushTable(lua_State* L, int tableTop, const std::string& name, int (*f)(lua_State*)); 17 | 18 | void pushArray(lua_State* L, int tableTop, int index, int value); 19 | void pushArray(lua_State* L, int tableTop, int index, uint value); 20 | void pushArray(lua_State* L, int tableTop, int index, const std::string& value); 21 | 22 | #endif // LUATORS_H 23 | -------------------------------------------------------------------------------- /Lua/Trigger/LuaTriggerTimerInterval.h: -------------------------------------------------------------------------------- 1 | #ifndef LUATRIGGERTIMERINTERVAL_H 2 | #define LUATRIGGERTIMERINTERVAL_H 3 | 4 | #include 5 | #include "LuaTriggerBase.h" 6 | 7 | class LuaTriggerTimerInterval : public LuaTriggerBase 8 | { 9 | public: 10 | LuaTriggerTimerInterval(); 11 | LuaTriggerTimerInterval(uint timerAmount, uint timerUnit); 12 | 13 | ~LuaTriggerTimerInterval(); 14 | 15 | bool isTriggered (const LuaEvent& luaevent); 16 | 17 | void toSettings(QSettings& mySettings); 18 | 19 | void fromSettings(const QSettings &mySettings); 20 | 21 | QString classname(); 22 | 23 | void getValues(uint& timerAmount, uint& timerUnit); 24 | 25 | protected: 26 | void calculateInterval(); 27 | 28 | uint _timerAmount; 29 | uint _timerUnit; 30 | uint _timerInterval; 31 | }; 32 | 33 | #endif // LUATRIGGERTIMERINTERVAL_H 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Lua4RS 2 | ====== 3 | 4 | A RetroShare plugin to automate and script things on RetroShare using the Lua language. 5 | 6 | ##What is RetroShare?## 7 | RetroShare is a secure decentralised communication platform. 8 | http://retroshare.sourceforge.net/ 9 | 10 | ##What is Lua4RS?## 11 | This plugin let you interact with RetroShare. It exposes most RetroShare functions to Lua and provides a trigger system. You can write scripts that react to events (from RetroShare) like _a friends comes online_ or _received a new mail_. With access to most RetroShare functions, you can do the same in the Lua language as you can do in C++. 12 | 13 | ##Requirements## 14 | * RetroShare v0.6 (use the 0.5 tag for RetroShare 0.5) 15 | * Lua 5.2 16 | 17 | ##How to build## 18 | Simply run "qmake" (or "qmake-qt4") and "make". Then place libLua4RS.so/Lua4RS.dll in your extension folder. 19 | -------------------------------------------------------------------------------- /scripts/groupTest.lua: -------------------------------------------------------------------------------- 1 | rs.clear(); 2 | 3 | -- get list of oll groups 4 | grpList = peers.getGroupInfoList() 5 | 6 | for i = 1, #grpList do 7 | -- get group info 8 | grpInfo = grpList[i] 9 | 10 | -- print group name 11 | rs.print(grpInfo["name"] .. " (ID: " .. grpInfo["id"] .. ")") 12 | 13 | -- check if there is any peer in the group 14 | if #grpInfo["peerIds"] > 0 then 15 | -- if yes: get peer ids 16 | peerIds = grpInfo["peerIds"] 17 | 18 | -- print their names 19 | for j = 1, #peerIds do 20 | id = peerIds[j] 21 | rs.print(" - " .. peers.getPeerName(id) .. " (ID: " .. id .. ")") 22 | end 23 | end 24 | end 25 | 26 | -- insert the peer id from a peer that is in a group 27 | idToRemove="" 28 | 29 | if idToRemove ~= "" then 30 | -- groupId == "" && assign == false -> remove from all groups 31 | peers.assignPeerToGroup("", idToRemove, false) 32 | end 33 | -------------------------------------------------------------------------------- /_todo.md: -------------------------------------------------------------------------------- 1 | 20140531, fc 2 | - complete replacement of Lua4RSMainWidget with Lua4RSWidget. plugin does compile but not run/load. when done: delete Lua4RSMainWidget + its files completely. check 3 | - note: off for a short vacation, back on monday or tuesday 4 | 5 | 20140605, fc 6 | - lua5.2 include problem. check 7 | 8 | 20140608, sehraf 9 | - new/edit/save/delete front-end code is a mess (but working) - see next points 10 | - (DONE) need an API for LuaContainer to handle things (instead of handling things in the front-end code ....) 11 | - need a rename function that renames files on the hard disk (also need code that detects renames) 12 | - (DONE) hide LuaCode and LuaConfig completely and make necessary things available through LuaContainer API 13 | - move script description to config 14 | - (low priority) rename LuaContainerList to LuaContainerList_t 15 | - (low priority) rename LuaList to LuaContainerList (to be more clear) 16 | -------------------------------------------------------------------------------- /Lua/Trigger/LuaTriggerBase.h: -------------------------------------------------------------------------------- 1 | #ifndef LUATRIGGERBASE_H 2 | #define LUATRIGGERBASE_H 3 | 4 | #include 5 | 6 | #include "../LuaEvent.h" 7 | #include 8 | #include 9 | 10 | #define LUA_TRIGGER_TIMER_INTERVAL "LuaTriggerTimerInterval" 11 | #define LUA_TRIGGER_STARTUP "LuaTriggerStartup" 12 | #define LUA_TRIGGER_SHUTDOWN "LuaTriggerShutdown" 13 | #define LUA_TRIGGER_ONCE "LuaTriggerTimerOnce" 14 | #define LUA_TRIGGER_EVENT "LuaTriggerEvent" 15 | 16 | class LuaTriggerBase 17 | { 18 | public: 19 | LuaTriggerBase(); 20 | 21 | virtual ~LuaTriggerBase(); 22 | 23 | virtual bool isTriggered (const LuaEvent& luaevent); 24 | 25 | virtual void toSettings(QSettings& mySettings); 26 | 27 | virtual void fromSettings(const QSettings &mySettings); 28 | 29 | virtual QString classname(); 30 | 31 | protected: 32 | QString _classname; 33 | QDateTime _lastRun; 34 | 35 | }; 36 | 37 | #endif // LUATRIGGERBASE_H 38 | -------------------------------------------------------------------------------- /gui/Lua4RSConfig.h: -------------------------------------------------------------------------------- 1 | #ifndef LUA4RSCONFIG_H 2 | #define LUA4RSCONFIG_H 3 | 4 | #include 5 | 6 | #include 7 | 8 | namespace Ui { 9 | class Lua4RSConfig; 10 | } 11 | 12 | class Lua4RSConfig : public ConfigPage 13 | { 14 | Q_OBJECT 15 | 16 | public: 17 | 18 | explicit Lua4RSConfig(QWidget *parent = 0); 19 | ~Lua4RSConfig(); 20 | 21 | /** Pure virtual method. Subclassed pages load their config settings here. */ 22 | virtual void load(); 23 | 24 | /** Pure virtual method. Subclassed pages save their config settings here 25 | * and return true if everything was saved successfully. */ 26 | virtual bool save(QString &errmsg); 27 | 28 | bool wasLoaded() { return loaded ; } 29 | 30 | virtual QPixmap iconPixmap() const { return QPixmap(":/images/lua_logo.png"); } 31 | virtual QString pageName() const { return "Lua4RS"; } 32 | virtual QString helpText() const { return tr("NOBODY WILL HELP YOU :O"); } 33 | 34 | private: 35 | Ui::Lua4RSConfig *ui; 36 | }; 37 | 38 | #endif // LUA4RSCONFIG_H 39 | -------------------------------------------------------------------------------- /Lua/Trigger/LuaTriggerBase.cpp: -------------------------------------------------------------------------------- 1 | #include "LuaTriggerBase.h" 2 | 3 | #define INI_KEY_TRIGGER_CLASSNAME "Classname" 4 | #define INI_KEY_TRIGGER_LASTRUN "LastRun" 5 | 6 | LuaTriggerBase::LuaTriggerBase() 7 | { 8 | _classname = "LuaTriggerBase"; 9 | _lastRun = QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0)); 10 | } 11 | 12 | LuaTriggerBase::~LuaTriggerBase() {} 13 | 14 | bool LuaTriggerBase::isTriggered (const LuaEvent& /*luaevent*/) 15 | { 16 | std::cerr << "[Lua] isTriggered() called on LuaTriggerBase" << std::endl; 17 | return false; 18 | } 19 | 20 | void LuaTriggerBase::toSettings(QSettings& mySettings) 21 | { 22 | mySettings.setValue(INI_KEY_TRIGGER_CLASSNAME, classname()); 23 | mySettings.setValue(INI_KEY_TRIGGER_LASTRUN, _lastRun); 24 | } 25 | 26 | void LuaTriggerBase::fromSettings(const QSettings& mySettings) 27 | { 28 | _classname = mySettings.value(INI_KEY_TRIGGER_CLASSNAME, "").toString(); 29 | _lastRun = mySettings.value(INI_KEY_TRIGGER_LASTRUN, QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0))).toDateTime(); 30 | } 31 | 32 | QString LuaTriggerBase::classname() 33 | { 34 | return _classname; 35 | } 36 | -------------------------------------------------------------------------------- /Lua/Trigger/LuaTriggerEvent.cpp: -------------------------------------------------------------------------------- 1 | #include "LuaTriggerEvent.h" 2 | 3 | LuaTriggerEvent::LuaTriggerEvent() 4 | { 5 | _classname = "LuaTriggerEvent"; 6 | } 7 | 8 | LuaTriggerEvent::LuaTriggerEvent(uint eventId) 9 | { 10 | _classname = "LuaTriggerEvent"; 11 | _eventId = eventId; 12 | } 13 | 14 | 15 | 16 | LuaTriggerEvent::~LuaTriggerEvent() 17 | { 18 | } 19 | 20 | 21 | 22 | bool LuaTriggerEvent::isTriggered (const LuaEvent& luaevent) 23 | { 24 | if (luaevent.eventId == _eventId) 25 | { 26 | _lastRun = QDateTime().currentDateTime(); 27 | return true; 28 | } 29 | return false; 30 | } 31 | 32 | 33 | 34 | void LuaTriggerEvent::toSettings(QSettings &mySettings) 35 | { 36 | LuaTriggerBase::toSettings(mySettings); 37 | mySettings.setValue("EventId", _eventId); 38 | } 39 | 40 | 41 | 42 | void LuaTriggerEvent::fromSettings (const QSettings& mySettings) 43 | { 44 | LuaTriggerBase::fromSettings(mySettings); 45 | _eventId = mySettings.value ("EventId", "").toUInt(); 46 | } 47 | 48 | 49 | 50 | QString LuaTriggerEvent::classname() 51 | { 52 | return _classname; 53 | } 54 | 55 | uint LuaTriggerEvent::getEventId() 56 | { 57 | return _eventId; 58 | } 59 | -------------------------------------------------------------------------------- /scripts/speedControl.lua: -------------------------------------------------------------------------------- 1 | -- change here 2 | -- upload speed 3 | minUL = 10 4 | maxUL = 150 5 | 6 | -- download speed 7 | minDL = 50 8 | maxDL = 500 9 | 10 | -- time (in hours 0-23) 11 | slowTime = 9 12 | powerTime = 22 13 | 14 | -- change done 15 | 16 | rs.clear() 17 | 18 | if slowTime == powerTime or slowTime < 0 or slowTime >= 24 or powerTime < 0 or powerTime >= 24 then 19 | rs.print("Please choose proper values") 20 | return 21 | end 22 | 23 | swap = false 24 | if slowTime > powerTime then 25 | swap = true 26 | end 27 | 28 | -- determine if dl/up speed must be slowed down 29 | slow = false 30 | now = tonumber( os.date("%H") ) 31 | if now >= slowTime then 32 | slow = true 33 | end 34 | 35 | if now >= powerTime then 36 | slow = false 37 | end 38 | 39 | if swap then 40 | slow = not slow 41 | end 42 | 43 | rs.print("Current hour is " .. os.date("%H") .. " - slowTime goes from " .. slowTime .. " to " .. powerTime .. " -> reduce dl/ul speed: " .. tostring( slow )) 44 | 45 | if slow then 46 | config.setMaxDataRates(minDL, minUL) 47 | else 48 | config.setMaxDataRates(maxDL, maxUL) 49 | end 50 | 51 | down, up = config.getMaxDataRates() 52 | rs.print("New limits are: down=" .. down .. "kB up=" .. up .. "kB") -------------------------------------------------------------------------------- /scripts/simpleDownloadBot.lua: -------------------------------------------------------------------------------- 1 | -- peer IDs (SSL) 2 | allowedIds = {} 3 | 4 | -- split 5 | function split(str) 6 | t = {} 7 | for s in string.gmatch(str, "%S+") do 8 | table.insert(t, s) 9 | end 10 | return t 11 | end 12 | 13 | -- inTable 14 | function inTable(tbl, item) 15 | for key, value in pairs(tbl) do 16 | if value == item then return true end 17 | end 18 | return false 19 | end 20 | 21 | -- add leading 'P' for IDs 22 | for index, id in pairs(allowedIds) do 23 | if string.sub(id, 1, 1) ~= "P" then 24 | allowedIds[index] = "P" .. id 25 | end 26 | end 27 | 28 | msg = args.msg 29 | chatid = args.chatid 30 | 31 | if inTable(allowedIds, chatid) then 32 | -- peer is allowed to send commands 33 | parts = split(msg, " ") 34 | 35 | if parts[1] == "!download" then 36 | name = parts[2] 37 | hash = parts[3] 38 | size = parts[4] 39 | 40 | if name == nil or hash == nil or size == nil then 41 | chat.sendChat(chatid, "usage: !download *file name* *file hash* *file size*") 42 | else 43 | chat.sendChat(chatid, "processing request ...") 44 | ok = files.fileRequest(name, hash, tonumber(size)) 45 | if ok then 46 | chat.sendChat(chatid, "... success!") 47 | else 48 | chat.sendChat(chatid, "... failed!") 49 | end 50 | end 51 | end 52 | end 53 | 54 | -------------------------------------------------------------------------------- /helper.h: -------------------------------------------------------------------------------- 1 | #ifndef HELPER 2 | #define HELPER 3 | 4 | static void replaceAll(std::string& str, const std::string& from, const std::string& to) 5 | { 6 | if(from.empty()) 7 | return; 8 | size_t start_pos = 0; 9 | while((start_pos = str.find(from, start_pos)) != std::string::npos) { 10 | str.replace(start_pos, from.length(), to); 11 | start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' 12 | } 13 | } 14 | 15 | static std::string& stripHTMLTags(std::string& s) 16 | { 17 | // Remove all special HTML characters 18 | bool done = false; 19 | while(!done) 20 | { 21 | // Look for start of tag: 22 | size_t leftPos = s.find('<'); 23 | if(leftPos != std::string::npos) 24 | { 25 | // See if tag close is in this line: 26 | size_t rightPos = s.find('>', leftPos); 27 | if(rightPos == std::string::npos) 28 | { 29 | done = true; 30 | s.erase(leftPos); 31 | } 32 | else 33 | s.erase(leftPos, rightPos - leftPos + 1); 34 | } 35 | else 36 | done = true; 37 | } 38 | 39 | replaceAll(s, "<", "<"); 40 | replaceAll(s, ">", ">"); 41 | replaceAll(s, "&", "&"); 42 | replaceAll(s, " ", " "); 43 | 44 | return s; 45 | } 46 | 47 | #endif // HELPER 48 | 49 | -------------------------------------------------------------------------------- /service/p3Lua4RS.h: -------------------------------------------------------------------------------- 1 | #ifndef P3LUA4RS_H 2 | #define P3LUA4RS_H 3 | 4 | #include 5 | 6 | #include "interface/L4RInterface.h" 7 | 8 | const uint16_t RS_SERVICE_TYPE_L4R_PLUGIN = 0x754c; 9 | //const uint32_t CONFIG_TYPE_L4R_PLUGIN = 0x754c3461; 10 | 11 | class p3Lua4RS : public RsPQIService, public L4RInterface 12 | { 13 | public: 14 | p3Lua4RS(RsPluginHandler* rph); 15 | 16 | // p3Config interface 17 | protected: 18 | virtual bool saveList(bool& cleanup, std::list& lst); 19 | virtual bool loadList(std::list& load); 20 | RsSerialiser *setupSerialiser(); 21 | 22 | // pqiService interface 23 | public: 24 | virtual int tick(); 25 | 26 | // Lua4RSInterface interface 27 | public: 28 | LuaCore *getCore(); 29 | virtual uint getTickIntervalInSeconds() const; 30 | virtual void setTickIntervalInSeconds(const uint &value); 31 | virtual uint getSecondsToStarUpEvent() const; 32 | virtual void setSecondsToStarUpEvent(const uint &value); 33 | 34 | private: 35 | // tick thread 36 | uint _secondsToStarUpEvent; 37 | uint _tickIntervalInSeconds; 38 | time_t _lastRun; 39 | time_t _initTime; 40 | bool _startUpEventTriggered; 41 | LuaCore* _luaCore; 42 | 43 | // pqiService interface 44 | public: 45 | RsServiceInfo getServiceInfo(); 46 | }; 47 | 48 | #endif // P3LUA4RS_H 49 | -------------------------------------------------------------------------------- /Lua4RSPlugin.h: -------------------------------------------------------------------------------- 1 | #ifndef LUA4RSPLUGIN_H 2 | #define LUA4RSPLUGIN_H 3 | 4 | #include 5 | #include 6 | 7 | class p3Lua4RS; 8 | 9 | class Lua4RSPlugin : public RsPlugin 10 | { 11 | public: 12 | Lua4RSPlugin(); 13 | 14 | virtual MainPage* qt_page() const; 15 | virtual QIcon* qt_icon() const; 16 | virtual QTranslator* qt_translator(QApplication *app, const QString& languageCode, const QString& externalDir) const; 17 | virtual ConfigPage* qt_config_page() const; 18 | 19 | virtual RsPQIService* rs_pqi_service() const; 20 | uint16_t rs_service_id() const; 21 | 22 | virtual void getPluginVersion(int &major, int &minor, int &build, int &svn_rev) const; 23 | virtual void setPlugInHandler(RsPluginHandler *pgHandler); 24 | virtual void stop(); 25 | 26 | virtual std::string configurationFileName() const { return "Lua4RS.cfg"; } 27 | 28 | virtual std::string getShortPluginDescription() const; 29 | virtual std::string getPluginName() const; 30 | virtual void setInterfaces(RsPlugInInterfaces& interfaces); 31 | 32 | protected: 33 | 34 | private: 35 | mutable QIcon* _icon ; 36 | mutable MainPage* _mainpage ; 37 | mutable RsNotify* _notify; 38 | mutable RsPluginHandler * _pluginHandler; 39 | mutable RsPeers* _peers; 40 | mutable p3Lua4RS* _service; 41 | }; 42 | 43 | #endif // LUA4RSPLUGIN_H 44 | -------------------------------------------------------------------------------- /Lua/LuaList.h: -------------------------------------------------------------------------------- 1 | #ifndef LUALIST_H 2 | #define LUALIST_H 3 | 4 | #include 5 | #include 6 | 7 | #include "LuaContainer.h" 8 | 9 | ///todo find better type? maybe a map? 10 | typedef std::list LuaContainerList; 11 | 12 | class LuaList 13 | { 14 | public: 15 | LuaList(); 16 | ~LuaList(); 17 | 18 | bool loadAll(); 19 | bool saveAll(); 20 | void rename(const QString& oldName, const QString& newName); 21 | 22 | //bool itemAt(size_t index, LuaContainer*& container); 23 | bool itemByName(const QString& name, LuaContainer*& container); 24 | bool itemByName(const std::string& name, LuaContainer*& container); 25 | 26 | LuaContainerList::const_iterator begin(); 27 | LuaContainerList::const_iterator end(); 28 | 29 | LuaContainer* createItem(); 30 | 31 | void addItem(LuaContainer* container); 32 | bool addItemAndSave(LuaContainer* container); 33 | void removeItem(LuaContainer* container); 34 | bool removeItemAndDelete(LuaContainer* container); 35 | 36 | size_t size(); 37 | void sort(); 38 | 39 | void setFilePath(const std::string& path); 40 | 41 | // debug 42 | void dump(); 43 | 44 | private: 45 | bool load(const QString& name, LuaContainer* container, bool ignoreNoSettingsFile = true); 46 | bool save(LuaContainer* container); 47 | bool remove(LuaContainer* container); 48 | 49 | void getFileNames(const QString& name, QString& luaFileName, QString& settingsFileName); 50 | 51 | void clearList(); 52 | 53 | LuaContainerList _luaList; 54 | QString _filePath; 55 | }; 56 | 57 | #endif // LUALIST_H 58 | -------------------------------------------------------------------------------- /Lua/Trigger/LuaTriggerOnce.cpp: -------------------------------------------------------------------------------- 1 | #include "LuaTriggerOnce.h" 2 | 3 | #define INI_KEY_TRIGGER_WHEN "When" 4 | 5 | LuaTriggerOnce::LuaTriggerOnce() 6 | { 7 | _classname = LUA_TRIGGER_ONCE; 8 | _onceDateTime = QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0)); 9 | } 10 | 11 | LuaTriggerOnce::LuaTriggerOnce(const QDateTime& onceDateTime) 12 | { 13 | _classname = LUA_TRIGGER_ONCE; 14 | _onceDateTime = onceDateTime; 15 | } 16 | 17 | LuaTriggerOnce::~LuaTriggerOnce() {} 18 | 19 | bool LuaTriggerOnce::isTriggered (const LuaEvent& luaevent) 20 | { 21 | if (luaevent.eventId == L4R_TIMERTICK) 22 | { 23 | // not sure if this 5 second window is needed .... 24 | if (_lastRun < _onceDateTime && ( // trigger wasn't triggered yet 25 | luaevent.timeStamp == _onceDateTime || ( // direct hit 26 | // in case we missed the exact date add 5 seconds toleranz 27 | luaevent.timeStamp > _onceDateTime && 28 | luaevent.timeStamp < _onceDateTime.addSecs(5) 29 | ))) 30 | { 31 | _lastRun = QDateTime().currentDateTime(); 32 | return true; 33 | } 34 | } 35 | return false; 36 | } 37 | 38 | void LuaTriggerOnce::toSettings(QSettings &mySettings) 39 | { 40 | LuaTriggerBase::toSettings(mySettings); 41 | mySettings.setValue(INI_KEY_TRIGGER_WHEN, _onceDateTime); 42 | } 43 | 44 | void LuaTriggerOnce::fromSettings (const QSettings& mySettings) 45 | { 46 | LuaTriggerBase::fromSettings(mySettings); 47 | _onceDateTime = mySettings.value(INI_KEY_TRIGGER_WHEN, QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0))).toDateTime(); 48 | } 49 | 50 | QString LuaTriggerOnce::classname() 51 | { 52 | return _classname; 53 | } 54 | 55 | QDateTime LuaTriggerOnce::getValues() 56 | { 57 | return _onceDateTime; 58 | } 59 | -------------------------------------------------------------------------------- /Lua/LuaContainer.h: -------------------------------------------------------------------------------- 1 | #ifndef LUACONTAINER_H 2 | #define LUACONTAINER_H 3 | 4 | #include 5 | #include 6 | 7 | #include "LuaEvent.h" 8 | 9 | class LuaCode; 10 | class LuaConfig; 11 | 12 | class LuaContainer 13 | { 14 | public: 15 | LuaContainer(); 16 | LuaContainer(LuaCode* luacode); 17 | LuaContainer(LuaCode* luacode, LuaConfig* luaconfig); 18 | ~LuaContainer(); 19 | 20 | QString getCode(); 21 | QString getDesc(); 22 | QString getName(); 23 | 24 | bool isTriggered(const LuaEvent& event); 25 | 26 | void setCode(const QString& code); 27 | void setCode(const std::string& code); 28 | void setDesc(const QString& desc); 29 | void setDesc(const std::string& desc); 30 | void setName(const QString& name); 31 | void setName(const std::string& name); 32 | 33 | void getSettings(QSettings& settings); 34 | void loadSettings(QSettings& settings); 35 | 36 | bool getEnabled(); 37 | void setEnabled(const bool enable); 38 | 39 | QDateTime getLastTriggered(); 40 | void setLastTriggered(const QDateTime& dt); 41 | 42 | bool getConstraintEnabled(); 43 | void setConstraintEnabled(const bool enable); 44 | 45 | void getConstraintFromTo(QTime& from, QTime& to); 46 | void setConstraintFromTo(const QTime& from, const QTime& to); 47 | 48 | // trigger 49 | void removeAllTrigger(); 50 | 51 | void addRunEveryTrigger(uint amout, uint unit); 52 | bool getRunEveryChecked(uint& amout, uint& unit); 53 | 54 | void addRunOnceTrigger(const QDateTime& when); 55 | bool getRunOnceChecked(QDateTime& when); 56 | 57 | void addRunStratupTrigger(); 58 | bool getRunStartupChecked(); 59 | 60 | void addRunShutdownTrigger(); 61 | bool getRunShutdownChecked(); 62 | 63 | // event trigger 64 | void addEventTrigger(uint eventId); 65 | bool getEventTriggerChecked(uint eventId); 66 | 67 | private: 68 | LuaCode* _code; 69 | LuaConfig* _config ; 70 | }; 71 | 72 | #endif // LUACONTAINER_H 73 | -------------------------------------------------------------------------------- /Lua/LuaCore.h: -------------------------------------------------------------------------------- 1 | #ifndef LUACORE_H 2 | #define LUACORE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | #include // for RsMutex 11 | 12 | #include "LuaEvent.h" 13 | 14 | extern "C" { 15 | #include 16 | #include 17 | #include 18 | } 19 | 20 | typedef std::map parameterMap; 21 | 22 | class QTreeWidget; 23 | class QTreeWidgetItem; 24 | 25 | class Lua4RSWidget; 26 | class Lua4RSNotify; 27 | class Lua4RSTickThread; 28 | class LuaList; 29 | class LuaContainer; 30 | 31 | class LuaCore : QObject 32 | { 33 | Q_OBJECT 34 | 35 | public: 36 | LuaCore(); 37 | ~LuaCore(); 38 | 39 | void shutDown(); 40 | 41 | void setupRsFunctionsAndTw(QTreeWidget* tw); 42 | 43 | bool processEvent(const LuaEvent& e); 44 | 45 | // invoke lua 46 | void runLuaByString(const QString& code); 47 | void runLuaByName(const QString& name); 48 | //void runLuaByNameWithParams(const QString& name, parameterMap paramMap); 49 | void runLuaByEvent(LuaContainer* container, const LuaEvent& event); 50 | 51 | // getter & setter 52 | Lua4RSWidget* getUI(); 53 | void setUi(Lua4RSWidget* ui); 54 | Lua4RSNotify *notify() const; 55 | LuaList* codeList() const; 56 | 57 | // signals 58 | void emitAppendOutput(const QString& s); 59 | void emitClearOutput(); 60 | 61 | private: 62 | void reportLuaErrors(lua_State *L, int status); 63 | void addFunctionToLuaAndTw(int tableTop, const std::string &namespc, QTreeWidgetItem* item, int (*f)(lua_State*), const std::string& name, const QString& hint); 64 | 65 | const std::string _folderName; 66 | std::string _path; 67 | 68 | lua_State* L; 69 | 70 | RsMutex _mutex; 71 | 72 | LuaList* _luaList; 73 | Lua4RSNotify* _notify; 74 | Lua4RSWidget* _ui; 75 | 76 | bool _processingEvent; 77 | // disable trigger on shutdown 78 | bool _shutDownImminent; 79 | 80 | signals: 81 | void appendLog(const QString& s); 82 | void appendOutput(const QString& s); 83 | void clearOutput(); 84 | }; 85 | 86 | #endif // LUACORE_H 87 | -------------------------------------------------------------------------------- /Lua4RS.pro: -------------------------------------------------------------------------------- 1 | !include("../Common/retroshare_plugin.pri")::error( "Could not include file ../Common/retroshare_plugin.pri" ) 2 | 3 | CONFIG += qt resources uic qrc 4 | 5 | greaterThan(QT_MAJOR_VERSION, 4) { 6 | # Qt 5 7 | QT += widgets 8 | } 9 | 10 | HEADERS -= upnp/upnputil.h 11 | SOURCES -= upnp/upnputil.c 12 | 13 | linux-* { 14 | LIBS += -llua5.2 15 | INCLUDEPATH += /usr/include/lua5.2 16 | } 17 | 18 | win32 { 19 | LIBS += -llua52 20 | INCLUDEPATH += ../../../lua-5.2.3/src 21 | } 22 | 23 | HEADERS += \ 24 | Lua4RSPlugin.h \ 25 | Lua4RSNotify.h \ 26 | gui/Lua4RSWidget.h \ 27 | gui/Lua4RSConfig.h \ 28 | Lua/LuaCore.h \ 29 | Lua/LuaToRS.h \ 30 | Lua/LuaCode.h \ 31 | Lua/LuaList.h \ 32 | Lua/LuaConfig.h \ 33 | Lua/LuaContainer.h \ 34 | Lua/LuaEvent.h \ 35 | Lua/Trigger/LuaTriggerBase.h \ 36 | Lua/Trigger/LuaTriggerTimerInterval.h \ 37 | Lua/Trigger/LuaTriggerStartup.h \ 38 | Lua/Trigger/LuaTriggerEvent.h \ 39 | Lua/Trigger/LuaTriggerShutdown.h \ 40 | Lua/Trigger/LuaTriggerOnce.h \ 41 | service/p3Lua4RS.h \ 42 | interface/L4RInterface.h \ 43 | helper.h 44 | 45 | SOURCES += \ 46 | Lua4RSPlugin.cpp \ 47 | Lua4RSNotify.cpp \ 48 | gui/Lua4RSWidget.cpp \ 49 | gui/Lua4RSConfig.cpp \ 50 | Lua/LuaCore.cpp \ 51 | Lua/LuaToRS.cpp \ 52 | Lua/LuaToRSPeers.cpp \ 53 | Lua/LuaCode.cpp \ 54 | Lua/LuaList.cpp \ 55 | Lua/LuaConfig.cpp \ 56 | Lua/LuaContainer.cpp \ 57 | Lua/Trigger/LuaTriggerBase.cpp \ 58 | Lua/Trigger/LuaTriggerTimerInterval.cpp \ 59 | Lua/Trigger/LuaTriggerStartup.cpp \ 60 | Lua/Trigger/LuaTriggerEvent.cpp \ 61 | Lua/Trigger/LuaTriggerShutdown.cpp \ 62 | Lua/Trigger/LuaTriggerOnce.cpp \ 63 | service/p3Lua4RS.cpp \ 64 | Lua/LuaToRSServerConfig.cpp \ 65 | Lua/LuaToRSDiscovery.cpp \ 66 | Lua/LuaToRSChat.cpp 67 | 68 | FORMS += \ 69 | gui/Lua4RSWidget.ui \ 70 | gui/Lua4RSConfig.ui 71 | 72 | TARGET = Lua4RS 73 | 74 | RESOURCES += \ 75 | Lua4RS_images.qrc \ 76 | Lua4RS_lang.qrc 77 | 78 | TRANSLATIONS += \ 79 | lang/Lua4RS_en.ts \ 80 | lang/Lua4RS_de.ts 81 | 82 | XUP.QT_VERSION = Qt System (4.8.1) 83 | -------------------------------------------------------------------------------- /scripts/friendsVersionOverview.lua: -------------------------------------------------------------------------------- 1 | function getVersionNumber( s ) 2 | b, e = string.find(s, "Revision") 3 | 4 | -- check for ':' 5 | x = s:sub(e + 1, e + 1) 6 | if x == ":" then 7 | e = e + 3 8 | else 9 | e = e + 2 10 | end 11 | 12 | -- revision starts at e and is usually 4 characters long 13 | rev = s:sub(e, e + 3) 14 | 15 | -- check for sane revision 16 | if tonumber(rev) == nil then 17 | -- the revision string might be '0.6.0.xxxx' 18 | e = e + 6 19 | rev = s:sub(e, e + 3) 20 | 21 | -- check for sane revision 22 | if tonumber(rev) == nil then 23 | return rev, false 24 | else 25 | return rev, true 26 | end 27 | else 28 | return rev, true 29 | end 30 | end 31 | 32 | function getName( id ) 33 | return peers.getPeerName( id ) 34 | end 35 | 36 | function pairsByKeys (t, f) 37 | local a = {} 38 | for n in pairs(t) do table.insert(a, n) end 39 | table.sort(a, f) 40 | local i = 0 -- iterator variable 41 | local iter = function () -- iterator function 42 | i = i + 1 43 | if a[i] == nil then return nil 44 | else return a[i], t[a[i]] 45 | end 46 | end 47 | return iter 48 | end 49 | 50 | rs.clear() 51 | 52 | -- down, up = disc.getWaitingDiscCount() 53 | -- rs.print("waiting discovery down=" .. down .. " up=" .. up) 54 | 55 | friends = peers.getFriendList() 56 | revList = {} 57 | revListNum = {} 58 | numFriends = 0 59 | for i = 1 , #friends do 60 | f = friends[i] 61 | revStr = disc.getPeerVersion(f) 62 | if revStr ~= nil then 63 | numFriends = numFriends + 1 64 | revNum, sane = getVersionNumber(revStr) 65 | if sane then 66 | table.insert(revList, revNum .. " - " .. getName(f)) 67 | if revListNum[revNum] ~= nil then 68 | revListNum[revNum] = revListNum[revNum] + 1 69 | else 70 | revListNum[revNum] = 1 71 | end 72 | else 73 | rs.print(getName(f) .. " is using " .. revStr) 74 | end 75 | end 76 | end 77 | 78 | rs.print("--------------------") 79 | rs.print(numFriends .. " discovery entries were found") 80 | for key, value in pairsByKeys(revListNum) do 81 | rs.print(key .. ": " .. value .. " time(s)") 82 | end 83 | 84 | rs.print("--------------------") 85 | table.sort(revList) 86 | for key, value in pairs(revList) do 87 | rs.print(value) 88 | end 89 | -------------------------------------------------------------------------------- /Lua/LuaConfig.h: -------------------------------------------------------------------------------- 1 | #ifndef LUACONFIG_H 2 | #define LUACONFIG_H 3 | 4 | #include 5 | 6 | #include "Trigger/LuaTriggerBase.h" 7 | #include "Trigger/LuaTriggerTimerInterval.h" 8 | #include "Trigger/LuaTriggerStartup.h" 9 | #include "Trigger/LuaTriggerShutdown.h" 10 | #include "Trigger/LuaTriggerEvent.h" 11 | #include "Trigger/LuaTriggerOnce.h" 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | class LuaConfig 21 | { 22 | public: 23 | LuaConfig(); 24 | ~LuaConfig(); 25 | 26 | // test my triggers if triggered by 27 | bool isTriggered(const LuaEvent& luaevent); 28 | 29 | // add a trigger to our triggerlist 30 | void addTrigger(LuaTriggerBase* trigger); 31 | 32 | // gets number of triggers in _myTriggers 33 | uint getTriggerCount(); 34 | 35 | // remove all trigger 36 | void removeAllTrigger(); 37 | 38 | // load this luaconfig from QSettings data 39 | void fromSettings(QSettings& mySettings); 40 | 41 | // serialize this luaconfig to QSettings data 42 | void toSettings(QSettings& mySettings); 43 | 44 | // to access trigger from config 45 | QList::const_iterator triggersBegin(); 46 | QList::const_iterator triggersEnd(); 47 | 48 | 49 | // Getter/Setter 50 | 51 | void enableScript(bool enable); 52 | bool isScriptEnabled(); 53 | 54 | void enableConstraint(bool enable); 55 | bool isConstraintEnabled(); 56 | 57 | void setConstraintFrom(QTime enableconstraintfrom); 58 | QTime getConstraintFrom(); 59 | 60 | void setConstraintTo(QTime constraintto); 61 | QTime getConstraintTo(); 62 | 63 | QString getDescription(); 64 | void setDescription(const QString &description); 65 | 66 | QDateTime getLastTriggered() const; 67 | void setLastTriggered(const QDateTime &lastTriggered); 68 | 69 | 70 | protected: 71 | // the description field is stored in LuaConfig 72 | QString _description; 73 | 74 | // is script enabled? 75 | bool _enableScript; 76 | 77 | // is constraint active? 78 | bool _constraint; 79 | 80 | // constraint window 81 | QTime _constraintFrom; 82 | QTime _constraintTo; 83 | 84 | // List of Trigger objects which may trigger this script 85 | QList _myTriggers; 86 | 87 | QDateTime _lastTriggered; 88 | }; 89 | 90 | #endif // LUACONFIG_H 91 | -------------------------------------------------------------------------------- /Lua/LuaToRSDiscovery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "LuaCore.h" 5 | #include "LuaToRS.h" 6 | 7 | extern "C" { 8 | #include 9 | #include 10 | #include 11 | } 12 | 13 | extern "C" { 14 | //virtual bool getDiscFriends(const RsPeerId &id, std::list& friends) = 0; 15 | int disc_getDiscFriends(lua_State* L) 16 | { 17 | luaL_checktype(L, 1, LUA_TSTRING); 18 | 19 | const RsPeerId id = RsPeerId(luaL_checkstring(L, 1)); 20 | std::list friends; 21 | rsDisc->getDiscFriends(id, friends); 22 | 23 | lua_newtable(L); 24 | int top = lua_gettop(L); 25 | 26 | std::list::iterator it = friends.begin(); 27 | for (size_t i = 1; it != friends.end(); i++, ++it) 28 | pushArray(L, top, i, it->toStdString()); 29 | 30 | return 1; 31 | } 32 | 33 | //virtual bool getDiscPgpFriends(const RsPgpId &pgpid, std::list& gpg_friends) = 0; 34 | int disc_getDiscPgpFriends(lua_State* L) 35 | { 36 | 37 | luaL_checktype(L, 1, LUA_TSTRING); 38 | 39 | const RsPgpId id = RsPgpId(luaL_checkstring(L, 1)); 40 | std::list pgp_friends; 41 | rsDisc->getDiscPgpFriends(id, pgp_friends); 42 | 43 | lua_newtable(L); 44 | int top = lua_gettop(L); 45 | 46 | std::list::iterator it = pgp_friends.begin(); 47 | for (size_t i = 1; it != pgp_friends.end(); i++, ++it) 48 | pushArray(L, top, i, it->toStdString()); 49 | 50 | return 1; 51 | } 52 | 53 | //virtual bool getPeerVersion(const RsPeerId &id, std::string &versions) = 0; 54 | int disc_getPeerVersion(lua_State* L) 55 | { 56 | luaL_checktype(L, 1, LUA_TSTRING); 57 | 58 | const RsPeerId id = RsPeerId(luaL_checkstring(L, 1)); 59 | std::string version; 60 | if(!rsDisc->getPeerVersion(id, version)) 61 | return 0; 62 | 63 | lua_pushstring(L, version.c_str()); 64 | 65 | return 1; 66 | } 67 | 68 | //virtual bool getWaitingDiscCount(unsigned int *sendCount, unsigned int *recvCount) = 0; 69 | int disc_getWaitingDiscCount(lua_State* L) 70 | { 71 | unsigned int sendCount, recvCount; 72 | rsDisc->getWaitingDiscCount(&sendCount, &recvCount); 73 | lua_pushnumber(L, recvCount); 74 | lua_pushnumber(L, sendCount); 75 | return 2; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /Lua/Trigger/LuaTriggerTimerInterval.cpp: -------------------------------------------------------------------------------- 1 | #include "LuaTriggerTimerInterval.h" 2 | 3 | #define INI_KEY_TRIGGER_TIMER_AMOUNT "TimerAmount" 4 | #define INI_KEY_TRIGGER_TIMER_UNIT "TimerUnit" 5 | 6 | LuaTriggerTimerInterval::LuaTriggerTimerInterval() 7 | { 8 | _classname = LUA_TRIGGER_TIMER_INTERVAL; 9 | _timerAmount = 0; 10 | _timerUnit = 0; 11 | _timerInterval = 0; 12 | } 13 | 14 | LuaTriggerTimerInterval::LuaTriggerTimerInterval(uint timerAmount, uint timerUnit) 15 | { 16 | _classname = LUA_TRIGGER_TIMER_INTERVAL; 17 | _timerAmount = timerAmount; 18 | _timerUnit = timerUnit; 19 | 20 | calculateInterval(); 21 | } 22 | 23 | LuaTriggerTimerInterval::~LuaTriggerTimerInterval() {} 24 | 25 | bool LuaTriggerTimerInterval::isTriggered (const LuaEvent& luaevent) 26 | { 27 | if (luaevent.eventId == L4R_TIMERTICK) 28 | { 29 | // if _lastRun was never set before, 30 | // then we need some very old datetime first 31 | if(!_lastRun.isValid()) 32 | { 33 | _lastRun = QDateTime(QDate(1970, 1, 1),QTime(0, 0, 0)); 34 | } 35 | 36 | // is it our big moment? 37 | if( _lastRun.addSecs(_timerInterval) <= luaevent.timeStamp ) 38 | { 39 | _lastRun = QDateTime::currentDateTime(); 40 | // remove ms 41 | _lastRun.setTime(QTime(_lastRun.time().hour(), _lastRun.time().minute(), _lastRun.time().second())); 42 | return true; 43 | } 44 | } 45 | return false; 46 | } 47 | 48 | void LuaTriggerTimerInterval::toSettings(QSettings& mySettings) 49 | { 50 | LuaTriggerBase::toSettings(mySettings); 51 | 52 | mySettings.setValue(INI_KEY_TRIGGER_TIMER_AMOUNT,_timerAmount); 53 | mySettings.setValue(INI_KEY_TRIGGER_TIMER_UNIT,_timerUnit); 54 | } 55 | 56 | void LuaTriggerTimerInterval::fromSettings (const QSettings& mySettings) 57 | { 58 | LuaTriggerBase::fromSettings(mySettings); 59 | _timerAmount = mySettings.value(INI_KEY_TRIGGER_TIMER_AMOUNT, 0).toUInt(); 60 | _timerUnit = mySettings.value(INI_KEY_TRIGGER_TIMER_UNIT, 0).toUInt(); 61 | 62 | calculateInterval(); 63 | } 64 | 65 | QString LuaTriggerTimerInterval::classname() 66 | { 67 | return _classname; 68 | } 69 | 70 | void LuaTriggerTimerInterval::getValues(uint& timerAmount, uint& timerUnit) 71 | { 72 | timerAmount = _timerAmount; 73 | timerUnit = _timerUnit; 74 | } 75 | 76 | void LuaTriggerTimerInterval::calculateInterval() 77 | { 78 | // sec, min, hour, day, week 79 | uint timeUnits[5] = {1, 60, 3600, 86400, 604800}; 80 | _timerInterval = _timerAmount * timeUnits[_timerUnit];; 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /scripts/buryTheDead.lua: -------------------------------------------------------------------------------- 1 | -- script to move long time offline peers to a seperated group 2 | -- !!! this may cause a GUI lag !!! 3 | 4 | -- enter the group name 5 | groupName = "Graveyard" 6 | 7 | -- days offline 8 | limit = 14 9 | 10 | rs.clear() 11 | 12 | -- remove group for offline peers 13 | function removeGroup() 14 | grpList = peers.getGroupInfoList() 15 | for i = 1, #grpList do 16 | if grpList[i]["name"] == groupName then 17 | rs.print("removing " .. grpList[i]["name"] .. " (" .. grpList[i]["id"] .. ")") 18 | peers.removeGroup(grpList[i]["id"]) 19 | end 20 | end 21 | end 22 | 23 | -- add group for offline peers 24 | function addGroup() 25 | grpInfo = {} 26 | grpInfo["flag"] = 0 27 | grpInfo["id"] = "0" 28 | grpInfo["name"] = groupName 29 | grpInfo["peerIds"] = nil 30 | 31 | peers.addGroup(grpInfo) 32 | end 33 | 34 | -- check whether the group exists 35 | function checkGroup() 36 | grpList = peers.getGroupInfoList() 37 | for i = 1, #grpList do 38 | if grpList[i]["name"] == groupName then 39 | return true 40 | end 41 | end 42 | 43 | -- didn't fing anything 44 | return false 45 | end 46 | 47 | function contains(a, array) 48 | for i = 1, #array do 49 | if array[i] == a then 50 | return true 51 | end 52 | end 53 | return false 54 | end 55 | 56 | function getGroupId() 57 | grpList = peers.getGroupInfoList() 58 | for i = 1, #grpList do 59 | if grpList[i]["name"] == groupName then 60 | return grpList[i]["id"] 61 | end 62 | end 63 | end 64 | 65 | -- function to combine everything 66 | function run() 67 | -- create group if needed 68 | if checkGroup() ~= true then addGroup() end 69 | 70 | grpInfo = peers.getGroupInfo( getGroupId() ) 71 | 72 | -- get infos 73 | now = os.time() 74 | friends = peers.getFriendList() 75 | 76 | for i = 1, #friends do 77 | details = peers.getPeerDetails(friends[i]) 78 | diff = now - details["lastUsed"] 79 | 80 | -- get gpg id for groups 81 | id = peers.getGPGId(details["id"]) 82 | 83 | -- how long in days 84 | days = diff / 60 / 60 / 24 85 | days = math.ceil(days) 86 | 87 | -- peer offline for too long? 88 | if days >= limit then 89 | if contains(id, grpInfo["peerIds"]) then 90 | rs.print("burying " .. details["name"] .. " (offline for " .. days .. " days)") 91 | peers.assignPeerToGroup(grpInfo["id"], id, true) 92 | end 93 | else 94 | if contains(id, grpInfo["peerIds"]) then 95 | rs.print("unearthing " .. details["name"]) 96 | peers.assignPeerToGroup(grpInfo["id"], id, false) 97 | end 98 | end 99 | end 100 | end 101 | 102 | 103 | -- use removeGroup() to cleanup your groups (will remove the group for offline peers) 104 | -- go! 105 | run() 106 | -------------------------------------------------------------------------------- /gui/Lua4RSWidget.h: -------------------------------------------------------------------------------- 1 | #ifndef LUA4RSWIDGET_H 2 | #define LUA4RSWIDGET_H 3 | 4 | #include 5 | 6 | namespace Ui { 7 | class Lua4RSWidget; 8 | } 9 | 10 | class QModelIndex; 11 | class QTableWidgetItem; 12 | class QTreeWidgetItem; 13 | 14 | 15 | class LuaCore; 16 | class LuaContainer; 17 | class LuaList; 18 | 19 | class Lua4RSWidget : public MainPage 20 | { 21 | Q_OBJECT 22 | 23 | public: 24 | explicit Lua4RSWidget(QWidget *parent = 0); 25 | ~Lua4RSWidget(); 26 | 27 | void disableOutput(); 28 | 29 | public slots: 30 | void clearOutput(); 31 | void appendOutput(const std::string &s); 32 | void appendOutput(const QString &s); 33 | 34 | void appendLog(const std::string &s); 35 | void appendLog(const QString &s); 36 | 37 | private: 38 | void setLuaCodes(LuaList* list); 39 | void switchContainer(LuaContainer* container); 40 | 41 | void newScript(); 42 | bool saveScript(bool showErrorMsg = true); 43 | 44 | // checks whether things entered in GUI are sane (like name is not empry) 45 | bool saneValues(); 46 | 47 | // all scripts helper 48 | LuaContainer* allScriptsGetLuaContainerFromSelectedRow(); 49 | LuaContainer* allScriptsGetLuaContainerFromRow(const int row); 50 | void allScriptsAddRow(LuaContainer* container); 51 | 52 | // init the gui at startup and after a container switch 53 | void clearUi(); 54 | 55 | // this function will fill every form with it's corresponding values 56 | void luaContainerToUi(LuaContainer* container); 57 | // or the other way round 58 | bool uiToLuaContainer(LuaContainer* container); 59 | 60 | Ui::Lua4RSWidget *ui; 61 | LuaCore* _lua; 62 | 63 | LuaContainer* _activeContainer; 64 | 65 | // needed for shutdown process (when gui is gone but things may still be running) 66 | bool _disableOutput; 67 | 68 | private slots: 69 | void on_pb_run_clicked(); 70 | void on_pb_newscript_clicked(); 71 | void on_pb_editscript_clicked(); 72 | void on_pb_deletescript_clicked(); 73 | void on_pb_load_clicked(); 74 | void on_pb_save_clicked(); 75 | void on_cbx_enable_clicked(bool checked); 76 | void on_cbx_timeconstraint_clicked(bool checked); 77 | void on_tied_timefrom_editingFinished(); 78 | void on_tied_timeto_editingFinished(); 79 | void on_tw_allscripts_cellClicked(int row, int column); 80 | void on_tw_allscripts_cellDoubleClicked(int row, int column); 81 | void on_dd_everyunits_currentIndexChanged(int index); 82 | void on_spb_everycount_editingFinished(); 83 | void on_pb_pastehint_released(); 84 | void on_tw_hints_itemDoubleClicked(QTreeWidgetItem *item, int column); 85 | void on_spb_everycount_valueChanged(int arg1); 86 | 87 | void on_rb_runonevent_toggled(bool checked); 88 | void on_cb_once_toggled(bool checked); 89 | void on_cb_startup_toggled(bool checked); 90 | void on_cb_shutdown_toggled(bool checked); 91 | void on_cb_every_toggled(bool checked); 92 | void on_dd_events_currentIndexChanged(int index); 93 | }; 94 | 95 | #endif // LUA4RSMAINWIDGET_H 96 | -------------------------------------------------------------------------------- /gui/Lua4RSConfig.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lua4RSConfig 4 | 5 | 6 | 7 | 0 8 | 0 9 | 341 10 | 237 11 | 12 | 13 | 14 | Form 15 | 16 | 17 | 18 | 19 | 10 20 | 10 21 | 221 22 | 81 23 | 24 | 25 | 26 | tick options 27 | 28 | 29 | 30 | 31 | 120 32 | 20 33 | 61 34 | 23 35 | 36 | 37 | 38 | This lets you choose after how many seconds the startup event is triggered 39 | 40 | 41 | 1 42 | 43 | 44 | 120 45 | 46 | 47 | 48 | 49 | 50 | 10 51 | 50 52 | 101 53 | 16 54 | 55 | 56 | 57 | tick interval 58 | 59 | 60 | 61 | 62 | 63 | 190 64 | 50 65 | 21 66 | 16 67 | 68 | 69 | 70 | s 71 | 72 | 73 | 74 | 75 | 76 | 120 77 | 50 78 | 61 79 | 23 80 | 81 | 82 | 83 | this lets you choose after how many seconds a tick event is triggered 84 | 85 | 86 | 1 87 | 88 | 89 | 60 90 | 91 | 92 | 93 | 94 | 95 | 10 96 | 20 97 | 101 98 | 16 99 | 100 | 101 | 102 | startup event 103 | 104 | 105 | 106 | 107 | 108 | 190 109 | 20 110 | 16 111 | 16 112 | 113 | 114 | 115 | s 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | -------------------------------------------------------------------------------- /Lua/LuaToRS.cpp: -------------------------------------------------------------------------------- 1 | #include "LuaToRS.h" 2 | #include "interface/L4RInterface.h" 3 | 4 | extern "C" { 5 | #include 6 | #include 7 | #include 8 | } 9 | 10 | // helper 11 | int getArgCount(lua_State* L) 12 | { 13 | return lua_gettop(L); 14 | } 15 | 16 | // no extern "C" for these function since you can overload them like this in C 17 | // table 18 | void pushTable(lua_State* L, int tableTop, const std::string& name, int value) 19 | { 20 | if(name == "") 21 | return; 22 | 23 | lua_pushstring(L, name.c_str()); 24 | lua_pushinteger(L, value); 25 | lua_settable(L, tableTop); 26 | } 27 | 28 | void pushTable(lua_State* L, int tableTop, const std::string& name, uint value) 29 | { 30 | if(name == "") 31 | return; 32 | 33 | lua_pushstring(L, name.c_str()); 34 | lua_pushinteger(L, value); 35 | lua_settable(L, tableTop); 36 | } 37 | 38 | void pushTable(lua_State* L, int tableTop, const std::string& name, const std::string& value) 39 | { 40 | if(name == "" || value == "") 41 | return; 42 | 43 | lua_pushstring(L, name.c_str()); 44 | lua_pushstring(L, value.c_str()); 45 | lua_settable(L, tableTop); 46 | } 47 | 48 | void pushTable(lua_State* L, int tableTop, const std::string& name, int (*f)(lua_State*)) 49 | { 50 | if(name == "" || f == NULL) 51 | return; 52 | 53 | lua_pushstring(L, name.c_str()); 54 | lua_pushcfunction(L, f); 55 | lua_settable(L, tableTop); 56 | } 57 | 58 | // array 59 | void pushArray(lua_State* L, int tableTop, int index, int value) 60 | { 61 | if(index < 1) 62 | return; 63 | 64 | lua_pushinteger(L, index); 65 | lua_pushinteger(L, value); 66 | lua_settable(L, tableTop); 67 | } 68 | 69 | void pushArray(lua_State* L, int tableTop, int index, uint value) 70 | { 71 | if(index < 1) 72 | return; 73 | 74 | lua_pushinteger(L, index); 75 | lua_pushinteger(L, value); 76 | lua_settable(L, tableTop); 77 | } 78 | 79 | void pushArray(lua_State* L, int tableTop, int index, const std::string& value) 80 | { 81 | if(index < 1 || value == "") 82 | return; 83 | 84 | lua_pushinteger(L, index); 85 | lua_pushstring(L, value.c_str()); 86 | lua_settable(L, tableTop); 87 | } 88 | 89 | extern "C" { 90 | // functions 91 | 92 | int rs_print(lua_State* L) 93 | { 94 | int argc = getArgCount(L); 95 | std::string output = ""; 96 | if(argc > 0) 97 | for (int n = 1; n <= argc; ++n ) { 98 | const std::string s = lua_tostring(L, n); 99 | output += s; 100 | if(n < argc) 101 | output += '\t'; 102 | } 103 | 104 | emit L4R::L4RConfig->getCore()->emitAppendOutput(QString::fromUtf8(output.c_str())); 105 | return 0; 106 | } 107 | 108 | int rs_clear(lua_State* /*L*/) 109 | { 110 | emit L4R::L4RConfig->getCore()->emitClearOutput(); 111 | return 0; 112 | } 113 | 114 | // temporary placed here 115 | #include 116 | 117 | // virtual bool FileRequest(const std::string& fname, const RsFileHash& hash, uint64_t size, const std::string& dest, TransferRequestFlags flags, const std::list& srcIds) = 0; 118 | int file_fileRequest(lua_State *L) 119 | { 120 | //int argc = getArgCount(L); 121 | luaL_checktype(L, 1, LUA_TSTRING); 122 | luaL_checktype(L, 2, LUA_TSTRING); 123 | luaL_checktype(L, 3, LUA_TNUMBER); 124 | 125 | const std::string fileName = luaL_checkstring(L, 1); 126 | const RsFileHash fileHash = RsFileHash(luaL_checkstring(L, 2)); 127 | const uint64_t fileSize = luaL_checknumber(L, 3); 128 | 129 | std::list srcIds; 130 | const bool ok = rsFiles->FileRequest(fileName, fileHash, fileSize, "", RS_FILE_REQ_ANONYMOUS_ROUTING, srcIds); 131 | 132 | lua_pushboolean(L, (int)ok); 133 | return 1; 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /Lua4RSPlugin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | #include "gui/Lua4RSConfig.h" 8 | #include "Lua4RSNotify.h" 9 | #include "Lua4RSPlugin.h" 10 | #include "gui/Lua4RSWidget.h" 11 | #include "interface/L4RInterface.h" 12 | #include "Lua/LuaCore.h" 13 | #include "service/p3Lua4RS.h" 14 | 15 | #define LUA_ICON_LINK ":/images/lua_logo.png" 16 | 17 | #if LUA_VERSION_NUM < 502 18 | #error "###############################################" 19 | #error "# You need at least Lua 5.2 for this plugin #" 20 | #error "# See contrib/get_lua5.2.3.sh #" 21 | #error "###############################################" 22 | #endif 23 | 24 | extern "C" { 25 | #ifdef WIN32 26 | __declspec(dllexport) 27 | #endif 28 | void *RETROSHARE_PLUGIN_provide() 29 | { 30 | static Lua4RSPlugin *p = new Lua4RSPlugin() ; 31 | 32 | return (void*)p ; 33 | } 34 | 35 | // This symbol contains the svn revision number grabbed from the executable. 36 | // It will be tested by RS to load the plugin automatically, since it is safe to load plugins 37 | // with same revision numbers, assuming that the revision numbers are up-to-date. 38 | // 39 | #ifdef WIN32 40 | __declspec(dllexport) 41 | #endif 42 | uint32_t RETROSHARE_PLUGIN_revision = RS_REVISION_NUMBER; 43 | 44 | // This symbol contains the svn revision number grabbed from the executable. 45 | // It will be tested by RS to load the plugin automatically, since it is safe to load plugins 46 | // with same revision numbers, assuming that the revision numbers are up-to-date. 47 | // 48 | #ifdef WIN32 49 | __declspec(dllexport) 50 | #endif 51 | uint32_t RETROSHARE_PLUGIN_api = RS_PLUGIN_API_VERSION ; 52 | } 53 | 54 | Lua4RSPlugin::Lua4RSPlugin() 55 | { 56 | _icon = NULL; 57 | _mainpage = NULL; 58 | _notify = NULL; 59 | _peers = NULL; 60 | _pluginHandler = NULL; 61 | _service = NULL; 62 | } 63 | 64 | void Lua4RSPlugin::stop() 65 | { 66 | L4R::L4RConfig->getCore()->shutDown(); 67 | } 68 | 69 | void Lua4RSPlugin::getPluginVersion(int &major, int &minor, int &build, int &svn_rev) const 70 | { 71 | major = RS_MAJOR_VERSION; 72 | minor = RS_MINOR_VERSION; 73 | build = RS_BUILD_NUMBER; 74 | svn_rev = RS_REVISION_NUMBER; 75 | } 76 | 77 | void Lua4RSPlugin::setInterfaces(RsPlugInInterfaces &interfaces) 78 | { 79 | // get stuff 80 | _peers = interfaces.mPeers; 81 | _notify = interfaces.mNotify; 82 | 83 | // setup other stuff - pqi service is nor running yet -> don't use interface pointer 84 | LuaCore* lc = dynamic_cast(rs_pqi_service())->getCore(); 85 | _notify->registerNotifyClient(lc->notify()); 86 | } 87 | 88 | MainPage* Lua4RSPlugin::qt_page() const 89 | { 90 | if(_mainpage == NULL) { 91 | _mainpage = new Lua4RSWidget(); 92 | L4R::L4RConfig->getCore()->setUi(dynamic_cast(_mainpage)); 93 | } 94 | 95 | return _mainpage ; 96 | } 97 | 98 | QIcon* Lua4RSPlugin::qt_icon() const 99 | { 100 | if(_icon == NULL) 101 | { 102 | Q_INIT_RESOURCE(Lua4RS_images); 103 | _icon = new QIcon(LUA_ICON_LINK); 104 | } 105 | 106 | return _icon ; 107 | } 108 | 109 | QTranslator* Lua4RSPlugin::qt_translator(QApplication* /*app*/, const QString& languageCode, const QString& externalDir) const 110 | { 111 | if (languageCode == "en") { 112 | return NULL; 113 | } 114 | 115 | QTranslator* translator = new QTranslator(); 116 | 117 | if (translator->load(externalDir + "/Lua4RS_" + languageCode + ".qm")) { 118 | return translator; 119 | } else if (translator->load(":/lang/Lua4RS_" + languageCode + ".qm")) { 120 | return translator; 121 | } 122 | 123 | delete(translator); 124 | return NULL; 125 | } 126 | 127 | ConfigPage *Lua4RSPlugin::qt_config_page() const 128 | { 129 | return new Lua4RSConfig(); 130 | } 131 | 132 | RsPQIService *Lua4RSPlugin::rs_pqi_service() const 133 | { 134 | if(_service == NULL) 135 | { 136 | _service = new p3Lua4RS(_pluginHandler); 137 | L4R::L4RConfig = _service; 138 | } 139 | 140 | return _service; 141 | } 142 | 143 | uint16_t Lua4RSPlugin::rs_service_id() const 144 | { 145 | return RS_SERVICE_TYPE_L4R_PLUGIN; 146 | } 147 | 148 | void Lua4RSPlugin::setPlugInHandler(RsPluginHandler *pgHandler) 149 | { 150 | _pluginHandler = pgHandler; 151 | } 152 | 153 | std::string Lua4RSPlugin::getShortPluginDescription() const 154 | { 155 | return QObject::tr("This plugin let you script RS with Lua.").toStdString(); 156 | } 157 | 158 | std::string Lua4RSPlugin::getPluginName() const 159 | { 160 | return "Lua4RS"; 161 | } 162 | -------------------------------------------------------------------------------- /Lua4RSNotify.h: -------------------------------------------------------------------------------- 1 | #ifndef LUA4RSNOTIFY_H 2 | #define LUA4RSNOTIFY_H 3 | 4 | #include 5 | #include 6 | 7 | class Lua4RSNotify : public NotifyClient 8 | { 9 | public: 10 | Lua4RSNotify(); 11 | 12 | virtual void notifyListPreChange (int /* list */, int /* type */); 13 | virtual void notifyListChange (int /* list */, int /* type */); 14 | // virtual void notifyErrorMsg (int /* list */, int /* sev */, std::string /* msg */); 15 | virtual void notifyChatMessage (const ChatMessage& /* msg */); 16 | // virtual void notifyChatStatus (const std::string& /* peer_id */, const std::string& /* status_string */ ,bool /* is_private */); 17 | virtual void notifyChatLobbyEvent (uint64_t /* lobby id */, uint32_t /* event type */ ,const std::string& /* nickname */,const std::string& /* any string */); 18 | // virtual void notifyChatLobbyTimeShift (int /* time_shift*/); 19 | virtual void notifyCustomState (const std::string& /* peer_id */, const std::string& /* status_string */); 20 | virtual void notifyHashingInfo (uint32_t /* type */, const std::string& /* fileinfo */); 21 | // virtual void notifyTurtleSearchResult (uint32_t /* search_id */, const std::list& /* files */); 22 | // virtual void notifyPeerHasNewAvatar (std::string /* peer_id */); 23 | // virtual void notifyOwnAvatarChanged (); 24 | // virtual void notifyOwnStatusMessageChanged (); 25 | virtual void notifyDiskFull (uint32_t /* location */, uint32_t /* size limit in MB */); 26 | virtual void notifyPeerStatusChanged (const std::string& /* peer_id */, uint32_t /* status */); 27 | 28 | /* one or more peers has changed the states */ 29 | virtual void notifyPeerStatusChangedSummary (); 30 | virtual void notifyDiscInfoChanged (); 31 | // virtual void notifyForumMsgReadSatusChanged (const std::string& /* channelId */, const std::string& /* msgId */, uint32_t /* status */); 32 | // virtual void notifyChannelMsgReadSatusChanged (const std::string& /* channelId */, const std::string& /* msgId */, uint32_t /* status */); 33 | // virtual bool askForDeferredSelfSignature (const void * /* data */, const uint32_t /* len */, unsigned char * /* sign */, unsigned int * /* signlen */,int& signature_result ) { signature_result = false ;return true; } 34 | virtual void notifyDownloadComplete (const std::string& /* fileHash */); 35 | // virtual void notifyDownloadCompleteCount (uint32_t /* count */); 36 | // virtual void notifyHistoryChanged (uint32_t /* msgId */, int /* type */); 37 | 38 | // virtual bool askForPassword (const std::string& /* key_details */, bool /* prev_is_bad */, std::string& /* password */ ); 39 | // virtual bool askForPluginConfirmation (const std::string& /* plugin_filename */, const std::string& /* plugin_file_hash */); 40 | 41 | }; 42 | 43 | #endif // LUA4RSNOTIFY_H 44 | 45 | 46 | 47 | //virtual void notifyListPreChange (int /* list */, int /* type */) {} 48 | //virtual void notifyListChange (int /* list */, int /* type */) {} 49 | //virtual void notifyErrorMsg (int /* list */, int /* sev */, std::string /* msg */) {} 50 | //virtual void notifyChatMessage (const ChatMessage& /* msg */) {} 51 | //virtual void notifyChatStatus (const ChatId& /* chat_id */, const std::string& /* status_string */) {} 52 | //virtual void notifyChatLobbyEvent (uint64_t /* lobby id */, uint32_t /* event type */ ,const std::string& /* nickname */,const std::string& /* any string */) {} 53 | //virtual void notifyChatLobbyTimeShift (int /* time_shift*/) {} 54 | //virtual void notifyCustomState (const std::string& /* peer_id */, const std::string& /* status_string */) {} 55 | //virtual void notifyHashingInfo (uint32_t /* type */, const std::string& /* fileinfo */) {} 56 | //virtual void notifyTurtleSearchResult (uint32_t /* search_id */, const std::list& /* files */) {} 57 | //virtual void notifyPeerHasNewAvatar (std::string /* peer_id */) {} 58 | //virtual void notifyOwnAvatarChanged () {} 59 | //virtual void notifyOwnStatusMessageChanged () {} 60 | //virtual void notifyDiskFull (uint32_t /* location */, uint32_t /* size limit in MB */) {} 61 | //virtual void notifyPeerStatusChanged (const std::string& /* peer_id */, uint32_t /* status */) {} 62 | -------------------------------------------------------------------------------- /Lua/LuaToRSServerConfig.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "LuaCore.h" 4 | #include "LuaToRS.h" 5 | 6 | extern "C" { 7 | #include 8 | #include 9 | #include 10 | } 11 | 12 | extern "C" { 13 | // /* Operating Mode */ 14 | //virtual uint32_t getOperatingMode() = 0; 15 | int config_getOperatingMode(lua_State* L) 16 | { 17 | const uint32_t opMode = rsConfig->getOperatingMode(); 18 | lua_pushinteger(L, opMode); 19 | 20 | std::string opModeS = ""; 21 | switch (opMode) { 22 | case RS_OPMODE_FULL: 23 | opModeS = "full"; 24 | break; 25 | case RS_OPMODE_NOTURTLE: 26 | opModeS = "noturtle"; 27 | break; 28 | case RS_OPMODE_MINIMAL: 29 | opModeS = "minimal"; 30 | break; 31 | case RS_OPMODE_GAMING: 32 | opModeS = "gamig"; 33 | break; 34 | default: 35 | // return without the string 36 | return 1; 37 | break; 38 | } 39 | lua_pushstring(L, opModeS.c_str()); 40 | return 2; 41 | } 42 | 43 | //virtual bool setOperatingMode(uint32_t opMode) = 0; 44 | int config_setOperatingMode(lua_State* L) 45 | { 46 | if( getArgCount(L) != 1) 47 | return 0; 48 | 49 | uint32_t opMode = 0; 50 | // check for int first! a int can easyly be converted to a string (lua_isstring() returned true even for numbers) 51 | if(lua_isnumber(L, 1)) 52 | { 53 | const int opModeI = luaL_checkinteger(L, 1); 54 | //std::cout << "[Lua] config_setOperatingMode got int: " << opModeI << std::endl; 55 | switch (opModeI) { 56 | case 1: 57 | opMode = RS_OPMODE_FULL; 58 | break; 59 | case 2: 60 | opMode = RS_OPMODE_NOTURTLE; 61 | break; 62 | case 3: 63 | opMode = RS_OPMODE_GAMING; 64 | break; 65 | case 4: 66 | opMode = RS_OPMODE_MINIMAL; 67 | break; 68 | default: 69 | // opMode = 0 70 | break; 71 | } 72 | } else { 73 | const std::string opModeS = luaL_checkstring(L, 1); 74 | //std::cout << "[Lua] config_setOperatingMode got string: " << opModeS << std::endl; 75 | 76 | if(opModeS == "full") 77 | opMode = RS_OPMODE_FULL; 78 | else if (opModeS == "noturtle") 79 | opMode = RS_OPMODE_NOTURTLE; 80 | else if (opModeS == "minimal") 81 | opMode = RS_OPMODE_MINIMAL; 82 | else if (opModeS == "gaming") 83 | opMode = RS_OPMODE_GAMING; 84 | // else opMode = 0 85 | } 86 | 87 | //std::cout << "[Lua] config_setOperatingMode opMode: " << opMode << std::endl; 88 | if(opMode == 0) 89 | // somthing went wrong 90 | return 0; 91 | 92 | rsConfig->setOperatingMode(opMode); 93 | return 0; 94 | } 95 | 96 | // download / upload speed 97 | //virtual int SetMaxDataRates( int downKb, int upKb ) = 0; 98 | int config_setMaxDataRates(lua_State* L) 99 | { 100 | if( getArgCount(L) != 2) 101 | return 0; 102 | 103 | luaL_checktype(L, 1, LUA_TNUMBER); 104 | luaL_checktype(L, 2, LUA_TNUMBER); 105 | 106 | int kbDown = luaL_checkinteger(L, 1); 107 | int kbUp = luaL_checkinteger(L, 2); 108 | 109 | rsConfig->SetMaxDataRates(kbDown, kbUp); 110 | return 0; 111 | } 112 | 113 | //virtual int GetMaxDataRates( int &inKb, int &outKb ) = 0; 114 | int config_getMaxDataRates(lua_State* L) 115 | { 116 | int kbDown, kbUp; 117 | rsConfig->GetMaxDataRates(kbDown, kbUp); 118 | lua_pushinteger(L, kbDown); 119 | lua_pushinteger(L, kbUp); 120 | return 2; 121 | } 122 | 123 | //virtual int GetCurrentDataRates( float &inKb, float &outKb ) = 0; 124 | int config_getCurrentDataRates(lua_State* L) 125 | { 126 | float kbDown, kbUp; 127 | rsConfig->GetCurrentDataRates(kbDown, kbUp); 128 | lua_pushnumber(L, kbDown); 129 | lua_pushnumber(L, kbUp); 130 | return 2; 131 | } 132 | } 133 | 134 | 135 | //virtual int getConfigNetStatus(RsConfigNetStatus &status) = 0; 136 | 137 | //virtual int getTotalBandwidthRates(RsConfigDataRates &rates) = 0; 138 | //virtual int getAllBandwidthRates(std::map &ratemap) = 0; 139 | 140 | //virtual uint32_t getUserLevel() = 0; 141 | 142 | //virtual uint32_t getNetState() = 0; 143 | //virtual uint32_t getNetworkMode() = 0; 144 | //virtual uint32_t getNatTypeMode() = 0; 145 | //virtual uint32_t getNatHoleMode() = 0; 146 | //virtual uint32_t getConnectModes() = 0; 147 | 148 | //virtual bool getConfigurationOption(uint32_t key, std::string &opt) = 0; 149 | //virtual bool setConfigurationOption(uint32_t key, const std::string &opt) = 0; 150 | -------------------------------------------------------------------------------- /Lua4RSNotify.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Lua4RSNotify.h" 6 | #include "Lua/LuaCore.h" 7 | #include "Lua/LuaEvent.h" 8 | #include "interface/L4RInterface.h" 9 | 10 | #include "helper.h" 11 | 12 | Lua4RSNotify::Lua4RSNotify() 13 | { 14 | } 15 | 16 | void Lua4RSNotify::notifyListPreChange (int list, int type) 17 | { 18 | LuaEvent e; 19 | e.eventId = L4R_INFO_PEERLISTCHANGE_PRE; 20 | e.timeStamp = QDateTime::currentDateTime(); 21 | e.dataParm->setValue("intlist", list); 22 | e.dataParm->setValue("inttype", type); 23 | 24 | L4R::L4RConfig->getCore()->processEvent(e); 25 | } 26 | 27 | void Lua4RSNotify::notifyListChange (int list, int type) 28 | { 29 | LuaEvent e; 30 | e.eventId = L4R_INFO_PEERLISTCHANGE; 31 | e.timeStamp = QDateTime::currentDateTime(); 32 | e.dataParm->setValue("intlist", list); 33 | e.dataParm->setValue("inttype", type); 34 | 35 | L4R::L4RConfig->getCore()->processEvent(e); 36 | } 37 | 38 | void Lua4RSNotify::notifyChatMessage(const ChatMessage &msg) 39 | { 40 | LuaEvent e; 41 | e.eventId = L4R_LOBBY_MESSAGERECEIVED; 42 | e.timeStamp = QDateTime::currentDateTime(); 43 | e.dataParm->setValue("strchatid", QString::fromUtf8(msg.chat_id.toStdString().c_str())); 44 | std::string msg2 = msg.msg; 45 | msg2 = stripHTMLTags(msg2); 46 | e.dataParm->setValue("strmsg", QString::fromUtf8(msg2.c_str())); 47 | RsGxsId gxsid = msg.lobby_peer_gxs_id; 48 | RsIdentityDetails gxsIdDetails; 49 | rsIdentity->getIdDetails(gxsid, gxsIdDetails); 50 | e.dataParm->setValue("strgxsid", QString::fromUtf8(gxsid.toStdString().c_str())); 51 | e.dataParm->setValue("strnick", QString::fromUtf8(gxsIdDetails.mNickname.c_str())); 52 | 53 | L4R::L4RConfig->getCore()->processEvent(e); 54 | } 55 | 56 | void Lua4RSNotify::notifyChatLobbyEvent (uint64_t lobby_id, uint32_t event_type, const std::string& nickname, const std::string& any_string) 57 | { 58 | QVariant v = QVariant::fromValue(lobby_id); 59 | LuaEvent e; 60 | e.eventId = L4R_LOBBY_EVENT; 61 | e.timeStamp = QDateTime::currentDateTime(); 62 | e.dataParm->setValue("u64lobbyId", v); 63 | e.dataParm->setValue("u32eventType", event_type); 64 | e.dataParm->setValue("strnickname", QString::fromUtf8(nickname.c_str())); 65 | e.dataParm->setValue("stranyString", QString::fromUtf8(any_string.c_str())); 66 | 67 | L4R::L4RConfig->getCore()->processEvent(e); 68 | } 69 | 70 | void Lua4RSNotify::notifyCustomState (const std::string& peer_id, const std::string& status_string) 71 | { 72 | LuaEvent e; 73 | e.eventId = L4R_FRIEND_CUSTOM_STATE; 74 | e.timeStamp = QDateTime::currentDateTime(); 75 | e.dataParm->setValue("strpeerId", QString::fromUtf8(peer_id.c_str())); 76 | e.dataParm->setValue("strstatusString", QString::fromUtf8(status_string.c_str())); 77 | 78 | L4R::L4RConfig->getCore()->processEvent(e); 79 | } 80 | 81 | void Lua4RSNotify::notifyHashingInfo (uint32_t type, const std::string& fileinfo) 82 | { 83 | LuaEvent e; 84 | e.eventId = L4R_FILE_HASHING_DONE; 85 | e.timeStamp = QDateTime::currentDateTime(); 86 | e.dataParm->setValue("u32type", type); 87 | e.dataParm->setValue("strfileInfo", QString::fromUtf8(fileinfo.c_str())); 88 | 89 | L4R::L4RConfig->getCore()->processEvent(e); 90 | } 91 | 92 | void Lua4RSNotify::notifyDiskFull (uint32_t location, uint32_t size_limit /* in MB */) 93 | { 94 | LuaEvent e; 95 | e.eventId = L4R_INFO_DISK_FULL; 96 | e.timeStamp = QDateTime::currentDateTime(); 97 | e.dataParm->setValue("u32location", location); 98 | e.dataParm->setValue("u32sizeLimit", size_limit); 99 | 100 | L4R::L4RConfig->getCore()->processEvent(e); 101 | } 102 | 103 | void Lua4RSNotify::notifyPeerStatusChanged (const std::string& peer_id, uint32_t status) 104 | { 105 | LuaEvent e; 106 | 107 | if(status == RS_STATUS_ONLINE) 108 | e.eventId = L4R_FRIEND_CAMEONLINE; 109 | else if(status == RS_STATUS_OFFLINE) 110 | e.eventId = L4R_FRIEND_WENTOFFLINE; 111 | else 112 | e.eventId = L4R_FRIEND_STATUS_CHANGED; 113 | 114 | e.timeStamp = QDateTime::currentDateTime(); 115 | e.dataParm->setValue("strpeerId", QString::fromUtf8(peer_id.c_str())); 116 | e.dataParm->setValue("u32status", status); 117 | 118 | L4R::L4RConfig->getCore()->processEvent(e); 119 | } 120 | 121 | void Lua4RSNotify::notifyPeerStatusChangedSummary () 122 | { 123 | LuaEvent e; 124 | e.eventId = L4R_FRIEND_STATUS_CHANGED_MANY; 125 | e.timeStamp = QDateTime::currentDateTime(); 126 | 127 | L4R::L4RConfig->getCore()->processEvent(e); 128 | } 129 | 130 | void Lua4RSNotify::notifyDiscInfoChanged () 131 | { 132 | LuaEvent e; 133 | e.eventId = L4R_INFO_DISCOVERY_UPDATE; 134 | e.timeStamp = QDateTime::currentDateTime(); 135 | 136 | L4R::L4RConfig->getCore()->processEvent(e); 137 | } 138 | 139 | void Lua4RSNotify::notifyDownloadComplete (const std::string& fileHash) 140 | { 141 | LuaEvent e; 142 | e.eventId = L4R_FILE_DOWNLOADFINISHED; 143 | e.timeStamp = QDateTime::currentDateTime(); 144 | e.dataParm->setValue("strfileHash", QString::fromUtf8(fileHash.c_str())); 145 | 146 | L4R::L4RConfig->getCore()->processEvent(e); 147 | } 148 | -------------------------------------------------------------------------------- /Lua/LuaEvent.h: -------------------------------------------------------------------------------- 1 | #ifndef LUAEVENT_H 2 | #define LUAEVENT_H 3 | 4 | #include 5 | #include 6 | 7 | //------------------------------------------------------------------------------ 8 | struct LuaEvent { 9 | LuaEvent() : dataParm(new QSettings()) {} 10 | ~LuaEvent() { delete dataParm; } 11 | 12 | uint eventId; 13 | QDateTime timeStamp; 14 | QSettings* dataParm; 15 | }; 16 | /* 17 | * 3 chars to identify type 18 | * Types: 19 | * - string : str 20 | * - int : int 21 | * - uint32 : u32 22 | * - uint64 : u64 23 | * - bool : boo 24 | * - std::list : tfi 25 | */ 26 | 27 | //------------------------------------------------------------------------------ 28 | // Fundamental Events from the Plugin 29 | //------------------------------------------------------------------------------ 30 | #define L4R_NOEVENT 00000 31 | #define L4R_TIMERTICK 00001 32 | #define L4R_STARTUP 00002 33 | #define L4R_SHUTDOWN 99999 34 | 35 | //------------------------------------------------------------------------------ 36 | // Events from Infolog 37 | //------------------------------------------------------------------------------ 38 | #define L4R_INFO_NEWENTRY 10000 39 | #define L4R_INFO_PEERLISTCHANGE 10010 40 | #define L4R_INFO_PEERLISTCHANGE_PRE 10011 41 | #define L4R_INFO_DISCOVERY_UPDATE 10020 42 | #define L4R_INFO_DISK_FULL 10030 43 | 44 | //------------------------------------------------------------------------------ 45 | // Events from Friends 46 | //------------------------------------------------------------------------------ 47 | #define L4R_FRIEND_CAMEONLINE 11000 48 | #define L4R_FRIEND_WENTOFFLINE 11010 49 | #define L4R_FRIEND_ADDED 11020 50 | #define L4R_FRIEND_BLOCKED 11030 51 | #define L4R_FRIEND_CUSTOM_STATE 11040 52 | #define L4R_FRIEND_STATUS_CHANGED 11050 53 | #define L4R_FRIEND_STATUS_CHANGED_MANY 11060 54 | 55 | //------------------------------------------------------------------------------ 56 | // Events from File Downloads 57 | //------------------------------------------------------------------------------ 58 | #define L4R_FILE_DOWNLOADSTARTED 12000 59 | #define L4R_FILE_DOWNLOADFINISHED 12010 60 | #define L4R_FILE_DOWNLOADCANCELLED 12020 61 | 62 | //------------------------------------------------------------------------------ 63 | // Events from File Uploads 64 | //------------------------------------------------------------------------------ 65 | #define L4R_FILE_UPLOADSTARTED 13000 66 | #define L4R_FILE_UPLOADFINISHED 13010 67 | #define L4R_FILE_HASHING_DONE 13020 68 | 69 | //------------------------------------------------------------------------------ 70 | // Events from File Searches 71 | //------------------------------------------------------------------------------ 72 | #define L4R_FILE_SEARCHSTARTED 14000 73 | #define L4R_FILE_SEARCHRESULT 14010 74 | #define L4R_FILE_SEARCHFINISHED 14020 75 | 76 | //------------------------------------------------------------------------------ 77 | // Events from Mails 78 | //------------------------------------------------------------------------------ 79 | #define L4R_MAIL_RECEIVED 15000 80 | #define L4R_MAIL_SENT 15010 81 | #define L4R_MAIL_DELETED 15020 82 | 83 | //------------------------------------------------------------------------------ 84 | // Events from Chatlobbys 85 | //------------------------------------------------------------------------------ 86 | #define L4R_LOBBY_MESSAGERECEIVED 16000 87 | #define L4R_LOBBY_MESSAGESENT 16010 88 | #define L4R_LOBBY_PEERJOINED 16020 89 | #define L4R_LOBBY_PEERLEFT 16030 90 | #define L4R_LOBBY_PEERMUTED 16040 91 | #define L4R_LOBBY_PEERUNMUTED 16050 92 | #define L4R_LOBBY_EVENT 16060 93 | 94 | //------------------------------------------------------------------------------ 95 | // Events from Private Chats 96 | //------------------------------------------------------------------------------ 97 | #define L4R_PCHAT_CREATED 17000 98 | #define L4R_PCHAT_INVITESENT 17010 99 | #define L4R_PCHAT_INVITERECEIVED 17020 100 | #define L4R_PCHAT_INVITEACCEPTED 17030 101 | #define L4R_PCHAT_INVITEDECLINED 17040 102 | #define L4R_PCHAT_JOINED 17050 103 | #define L4R_PCHAT_LEFT 17060 104 | 105 | //------------------------------------------------------------------------------ 106 | // Events from Forums 107 | //------------------------------------------------------------------------------ 108 | #define L4R_FORUM_POSTRECEIVED 18000 109 | #define L4R_FORUM_POSTSENT 18010 110 | #define L4R_FORUM_CREATED 18020 111 | #define L4R_FORUM_SUBSCRIBED 18030 112 | #define L4R_FORUM_UNSUBSCRIBED 18040 113 | 114 | //------------------------------------------------------------------------------ 115 | // Events from Channels 116 | //------------------------------------------------------------------------------ 117 | #define L4R_CHANNEL_POSTRECEIVED 19000 118 | #define L4R_CHANNEL_POSTSENT 19010 119 | #define L4R_CHANNEL_CREATED 19020 120 | #define L4R_CHANNEL_SUBSCRIBED 19030 121 | #define L4R_CHANNEL_UNSUBSCRIBED 19040 122 | 123 | #endif // LUAEVENT_H 124 | -------------------------------------------------------------------------------- /service/p3Lua4RS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "p3Lua4RS.h" 7 | #include "Lua/LuaCore.h" 8 | 9 | #define L4RCONFIG_TIMER_STARTUP "L4RCONFIG_TIMER_STARTUP" 10 | #define L4RCONFIG_TIMER_TICK "L4RCONFIG_TIMER_TICK" 11 | 12 | /* DEFINE INTERFACE POINTER! */ 13 | L4RInterface* L4R::L4RConfig = NULL; 14 | 15 | p3Lua4RS::p3Lua4RS(RsPluginHandler* rph) : 16 | RsPQIService(RS_SERVICE_TYPE_L4R_PLUGIN, 0, rph), 17 | _lastRun( time(0) ), 18 | _initTime( time(0) ), 19 | _startUpEventTriggered( false ), 20 | _luaCore(new LuaCore()) 21 | { 22 | // setup defaults 23 | _secondsToStarUpEvent = 5; 24 | _tickIntervalInSeconds = 1; 25 | } 26 | 27 | // helper 28 | RsTlvKeyValue push_int_value(const std::string& key, int value) 29 | { 30 | RsTlvKeyValue kv; 31 | kv.key = key ; 32 | rs_sprintf(kv.value, "%d", value); 33 | 34 | return kv ; 35 | } 36 | 37 | RsTlvKeyValue push_uint_value(const std::string& key, uint value) 38 | { 39 | RsTlvKeyValue kv; 40 | kv.key = key ; 41 | rs_sprintf(kv.value, "%d", value); 42 | 43 | return kv ; 44 | } 45 | 46 | int pop_int_value(const std::string& s) 47 | { 48 | std::istringstream is(s) ; 49 | 50 | int val ; 51 | is >> val ; 52 | 53 | return val ; 54 | } 55 | 56 | uint pop_uint_value(const std::string& s) 57 | { 58 | std::istringstream is(s) ; 59 | 60 | uint val ; 61 | is >> val ; 62 | 63 | return val ; 64 | } 65 | 66 | bool p3Lua4RS::saveList(bool& cleanup, std::list& lst) 67 | { 68 | std::cout << "[Lua] p3Lua4RS::saveList" << std::endl; 69 | 70 | // VOIP has it set to true -> can't be that wrong .... 71 | cleanup = true; 72 | 73 | RsConfigKeyValueSet *vitem = new RsConfigKeyValueSet; 74 | 75 | vitem->tlvkvs.pairs.push_back(push_int_value(L4RCONFIG_TIMER_STARTUP, _secondsToStarUpEvent)); 76 | vitem->tlvkvs.pairs.push_back(push_int_value(L4RCONFIG_TIMER_TICK, _tickIntervalInSeconds)); 77 | 78 | lst.push_back(vitem); 79 | 80 | return true; 81 | } 82 | 83 | bool p3Lua4RS::loadList(std::list& load) 84 | { 85 | std::cout << "[Lua] p3Lua4RS::loadList" << std::endl; 86 | for(std::list::const_iterator it = load.begin(); it!=load.end(); ++it) 87 | { 88 | RsConfigKeyValueSet *vitem = dynamic_cast(*it); 89 | if(vitem != NULL) 90 | for(std::list::const_iterator kit = vitem->tlvkvs.pairs.begin(); kit != vitem->tlvkvs.pairs.end(); ++kit) 91 | if(kit->key == L4RCONFIG_TIMER_STARTUP) 92 | _secondsToStarUpEvent = pop_int_value(kit->value); 93 | else if(kit->key == L4RCONFIG_TIMER_TICK) 94 | _tickIntervalInSeconds = pop_int_value(kit->value); 95 | else 96 | std::cerr << "[Lua] L4RConfig::loadList : unknown key: " << kit->key << std::endl; 97 | 98 | delete vitem; 99 | } 100 | 101 | return true; 102 | } 103 | 104 | RsSerialiser *p3Lua4RS::setupSerialiser() 105 | { 106 | RsSerialiser* rsSerialiser = new RsSerialiser(); 107 | rsSerialiser->addSerialType(new RsGeneralConfigSerialiser()); 108 | 109 | return rsSerialiser ; 110 | } 111 | 112 | int p3Lua4RS::tick() 113 | { 114 | // start up event 115 | if(!_startUpEventTriggered && (_initTime + _secondsToStarUpEvent) <= (uint)time(0)) 116 | { 117 | LuaEvent e; 118 | e.eventId = L4R_STARTUP; 119 | e.timeStamp = QDateTime::currentDateTime(); 120 | 121 | if(_luaCore->processEvent(e)) 122 | // startup event wasn't blocked by core 123 | _startUpEventTriggered = true; 124 | } 125 | 126 | // tick each X second 127 | if(_lastRun + _tickIntervalInSeconds <= (uint)time(0)) 128 | { 129 | LuaEvent e; 130 | e.eventId = L4R_TIMERTICK; 131 | e.timeStamp = QDateTime::currentDateTime(); 132 | // remove ms 133 | e.timeStamp.setTime(QTime(e.timeStamp.time().hour(), e.timeStamp.time().minute(), e.timeStamp.time().second())); 134 | 135 | _luaCore->processEvent(e); 136 | 137 | _lastRun = time(0); 138 | } 139 | 140 | return 0; 141 | } 142 | 143 | LuaCore *p3Lua4RS::getCore() 144 | { 145 | return _luaCore; 146 | } 147 | 148 | uint p3Lua4RS::getTickIntervalInSeconds() const 149 | { 150 | return _tickIntervalInSeconds; 151 | } 152 | 153 | void p3Lua4RS::setTickIntervalInSeconds(const uint& value) 154 | { 155 | _tickIntervalInSeconds = value; 156 | IndicateConfigChanged(); 157 | } 158 | 159 | uint p3Lua4RS::getSecondsToStarUpEvent() const 160 | { 161 | return _secondsToStarUpEvent; 162 | } 163 | 164 | void p3Lua4RS::setSecondsToStarUpEvent(const uint& value) 165 | { 166 | _secondsToStarUpEvent = value; 167 | IndicateConfigChanged(); 168 | } 169 | 170 | RsServiceInfo p3Lua4RS::getServiceInfo() 171 | { 172 | const std::string L4R_APP_NAME = "Lua4RS"; 173 | const uint16_t L4R_APP_MAJOR_VERSION = 1; 174 | const uint16_t L4R_APP_MINOR_VERSION = 0; 175 | const uint16_t L4R_MIN_MAJOR_VERSION = 1; 176 | const uint16_t L4R_MIN_MINOR_VERSION = 0; 177 | 178 | return RsServiceInfo(RS_SERVICE_TYPE_L4R_PLUGIN, 179 | L4R_APP_NAME, 180 | L4R_APP_MAJOR_VERSION, 181 | L4R_APP_MINOR_VERSION, 182 | L4R_MIN_MAJOR_VERSION, 183 | L4R_MIN_MINOR_VERSION); 184 | } 185 | -------------------------------------------------------------------------------- /Lua/LuaContainer.cpp: -------------------------------------------------------------------------------- 1 | #include "LuaContainer.h" 2 | #include "LuaCode.h" 3 | #include "LuaConfig.h" 4 | 5 | LuaContainer::LuaContainer() : 6 | _code(new LuaCode()), 7 | _config(new LuaConfig()) 8 | { 9 | } 10 | 11 | LuaContainer::LuaContainer(LuaCode* luacode) : 12 | _config(new LuaConfig()) 13 | { 14 | _code = luacode; 15 | } 16 | 17 | 18 | LuaContainer::LuaContainer(LuaCode* luacode, LuaConfig* luaconfig) 19 | { 20 | _code = luacode; 21 | _config = luaconfig; 22 | } 23 | 24 | LuaContainer::~LuaContainer() 25 | { 26 | delete _code; 27 | delete _config; 28 | } 29 | 30 | bool LuaContainer::isTriggered(const LuaEvent& event) 31 | { 32 | return _config->isTriggered(event); 33 | } 34 | 35 | // getter / setter 36 | 37 | QString LuaContainer::getCode() { return _code->code(); } 38 | void LuaContainer::setCode(const QString& code) { _code->setCode(code); } 39 | void LuaContainer::setCode(const std::string& code) { setCode(QString::fromStdString(code)); } 40 | 41 | QString LuaContainer::getDesc() { return _config->getDescription(); } 42 | void LuaContainer::setDesc(const std::string& desc) { setDesc(QString::fromStdString(desc)); } 43 | void LuaContainer::setDesc(const QString& desc) { _config->setDescription(desc); } 44 | 45 | QString LuaContainer::getName() { return _code->name(); } 46 | void LuaContainer::setName(const std::string& name) { setName(QString::fromStdString(name)); } 47 | void LuaContainer::setName(const QString& name) 48 | { 49 | if(name.endsWith(".lua")) 50 | _code->setName(name); 51 | else 52 | _code->setName(name + ".lua"); 53 | } 54 | 55 | void LuaContainer::getSettings(QSettings& settings) { _config->toSettings(settings); } 56 | void LuaContainer::loadSettings(QSettings& settings) { _config->fromSettings(settings); } 57 | 58 | bool LuaContainer::getEnabled() { return _config->isScriptEnabled(); } 59 | void LuaContainer::setEnabled(const bool enable) { _config->enableScript(enable); } 60 | 61 | QDateTime LuaContainer::getLastTriggered() { return _config->getLastTriggered(); } 62 | void LuaContainer::setLastTriggered(const QDateTime& dt) { _config->setLastTriggered(dt); } 63 | 64 | // constraint 65 | 66 | bool LuaContainer::getConstraintEnabled() { return _config->isConstraintEnabled(); } 67 | void LuaContainer::setConstraintEnabled(const bool enable) { _config->enableConstraint(enable); } 68 | 69 | void LuaContainer::getConstraintFromTo(QTime& from, QTime& to) { from = _config->getConstraintFrom(); to = _config->getConstraintTo(); } 70 | void LuaContainer::setConstraintFromTo(const QTime& from, const QTime& to) { _config->setConstraintFrom(from); _config->setConstraintTo(to); } 71 | 72 | // trigger 73 | void LuaContainer::removeAllTrigger() { _config->removeAllTrigger(); } 74 | 75 | // every 76 | void LuaContainer::addRunEveryTrigger(uint amout, uint unit) 77 | { 78 | std::cout << "[Lua] LuaContainer::addRunEveryTrigger() : triggercount=" << _config->getTriggerCount() << std::endl; 79 | _config->addTrigger(new LuaTriggerTimerInterval(amout, unit)); 80 | } 81 | 82 | bool LuaContainer::getRunEveryChecked(uint& amout, uint& unit) 83 | { 84 | for(QList::const_iterator it = _config->triggersBegin(); it != _config->triggersEnd(); ++it) 85 | { 86 | if((*it)->classname() == LUA_TRIGGER_TIMER_INTERVAL) 87 | { 88 | LuaTriggerTimerInterval* t = dynamic_cast(*it); 89 | if(t == NULL) 90 | { 91 | std::cerr << "[Lua] LuaContainer::getRunEveryChecked() - failed cast!" << std::endl; 92 | continue; // since there should be only one trigger with that name the continue might be useless - keep it for now 93 | } 94 | t->getValues(amout, unit); 95 | return true; 96 | } 97 | } 98 | return false; 99 | } 100 | 101 | // once 102 | void LuaContainer::addRunOnceTrigger(const QDateTime& when) 103 | { 104 | std::cout << "[Lua] LuaContainer::addRunEveryTrigger() : triggercount=" << _config->getTriggerCount() << std::endl; 105 | _config->addTrigger(new LuaTriggerOnce(when)); 106 | } 107 | 108 | bool LuaContainer::getRunOnceChecked(QDateTime& when) 109 | { 110 | for(QList::const_iterator it = _config->triggersBegin(); it != _config->triggersEnd(); ++it) 111 | { 112 | if((*it)->classname() == LUA_TRIGGER_ONCE) 113 | { 114 | LuaTriggerOnce* t = dynamic_cast(*it); 115 | if(t == NULL) 116 | { 117 | std::cerr << "[Lua] LuaContainer::getRunOnceChecked() - failed cast!" << std::endl; 118 | continue; // since there should be only one trigger with that name the continue might be useless - keep it for now 119 | } 120 | when = t->getValues(); 121 | return true; 122 | } 123 | } 124 | return false; 125 | } 126 | 127 | // start up 128 | void LuaContainer::addRunStratupTrigger() 129 | { 130 | std::cout << "[Lua] LuaContainer::addRunEveryTrigger() : triggercount=" << _config->getTriggerCount() << std::endl; 131 | _config->addTrigger(new LuaTriggerStartup()); 132 | } 133 | 134 | bool LuaContainer::getRunStartupChecked() 135 | { 136 | for(QList::const_iterator it = _config->triggersBegin(); it != _config->triggersEnd(); ++it) 137 | { 138 | if((*it)->classname() == LUA_TRIGGER_STARTUP) 139 | return true; 140 | } 141 | return false; 142 | } 143 | 144 | // shut down 145 | void LuaContainer::addRunShutdownTrigger() 146 | { 147 | std::cout << "[Lua] LuaContainer::addRunEveryTrigger() : triggercount=" << _config->getTriggerCount() << std::endl; 148 | _config->addTrigger(new LuaTriggerShutdown()); 149 | } 150 | 151 | bool LuaContainer::getRunShutdownChecked() 152 | { 153 | for(QList::const_iterator it = _config->triggersBegin(); it != _config->triggersEnd(); ++it) 154 | { 155 | if((*it)->classname() == LUA_TRIGGER_SHUTDOWN) 156 | return true; 157 | } 158 | return false; 159 | } 160 | 161 | void LuaContainer::addEventTrigger(uint eventId) 162 | { 163 | std::cout << "[Lua] LuaContainer::addEventTrigger() : triggercount=" << _config->getTriggerCount() << std::endl; 164 | _config->addTrigger(new LuaTriggerEvent(eventId)); 165 | } 166 | 167 | bool LuaContainer::getEventTriggerChecked(uint eventId) 168 | { 169 | for(QList::const_iterator it = _config->triggersBegin(); it != _config->triggersEnd(); ++it) 170 | { 171 | if((*it)->classname() == "LuaTriggerEvent") 172 | { 173 | LuaTriggerEvent *e = dynamic_cast(*it); 174 | if(e != NULL && e->getEventId() == eventId) 175 | return true; 176 | } 177 | } 178 | return false; 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 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. -------------------------------------------------------------------------------- /Lua/LuaConfig.cpp: -------------------------------------------------------------------------------- 1 | #include "LuaConfig.h" 2 | 3 | #define INI_KEY_CLASSNAME "Classname" 4 | #define INI_KEY_CONSTRAINT_ENABLED "ConstraintEnabled" 5 | #define INI_KEY_CONSTRAINT_FROM "ConstraintFrom" 6 | #define INI_KEY_CONSTRAINT_TO "ConstraintTo" 7 | #define INI_KEY_DESC "Description" 8 | #define INI_KEY_ENABLED "Enabled" 9 | #define INI_KEY_LAST_TRIGGERED "LastTriggered" 10 | #define INI_KEY_TRIGGER "Trigger" 11 | 12 | LuaConfig::LuaConfig() : 13 | _description(""), 14 | _enableScript(false), 15 | _constraint(false), 16 | _constraintFrom(QTime(0, 0, 0)), 17 | _constraintTo(QTime(0, 0, 0)), 18 | _lastTriggered(QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0))) 19 | { 20 | } 21 | 22 | LuaConfig::~LuaConfig() 23 | { 24 | for(QList ::const_iterator it = _myTriggers.begin(); it != _myTriggers.end(); ++it) 25 | delete *it; 26 | 27 | _myTriggers.clear(); 28 | } 29 | 30 | // Test all triggers if one or more are triggered by 31 | bool LuaConfig::isTriggered(const LuaEvent& luaevent) 32 | { 33 | // is the script enabled at all? 34 | if(isScriptEnabled()) 35 | { 36 | // is there a constraint on the enabled script? 37 | if(isConstraintEnabled()) 38 | { 39 | // is the event timestamp outside the set constraint of the enabled script? 40 | if ( (luaevent.timeStamp.time() < getConstraintFrom()) || 41 | (luaevent.timeStamp.time() > getConstraintTo()) ) 42 | { 43 | // if the time constraint is enabled and we are outside of the 44 | // constraint window [from..to], then no trigger can happen. 45 | return false; 46 | } 47 | } 48 | 49 | // now test each trigger if it is triggered by the event 50 | for (int i=0 ; i<_myTriggers.size() ; ++i) 51 | { 52 | // if at least one trigger returns true then we *are* triggered. 53 | if ( _myTriggers.at(i)->isTriggered (luaevent) ) 54 | { 55 | // lets remember the time we've been triggered 56 | _lastTriggered = QDateTime().currentDateTime(); 57 | return true; 58 | } 59 | } 60 | } 61 | return false; 62 | } 63 | 64 | // addTrigger - Add a trigger to the trigger list of this LuaConfig 65 | void LuaConfig::addTrigger(LuaTriggerBase* trigger) 66 | { 67 | if (trigger != NULL) 68 | { 69 | _myTriggers.append(trigger); 70 | } 71 | else 72 | { 73 | std::cerr << "[Lua] LuaConfig::addTrigger() : tried to add an invalid trigger obj." << std::endl; 74 | } 75 | } 76 | 77 | // gets no of triggers in _myTriggers 78 | uint LuaConfig::getTriggerCount() 79 | { 80 | return _myTriggers.count(); 81 | } 82 | 83 | void LuaConfig::removeAllTrigger() 84 | { 85 | for(QList ::const_iterator it = _myTriggers.begin(); it != _myTriggers.end(); ++it) 86 | delete *it; 87 | 88 | _myTriggers.clear(); 89 | } 90 | 91 | QList::const_iterator LuaConfig::triggersBegin() { return _myTriggers.begin(); } 92 | QList::const_iterator LuaConfig::triggersEnd() { return _myTriggers.end(); } 93 | 94 | // load this luaconfig from QSettings data 95 | void LuaConfig::fromSettings(QSettings &mySettings) 96 | { 97 | LuaTriggerBase* atrigger; 98 | QStringList childGroups; 99 | QString childGroup; 100 | QString className; 101 | 102 | // first get description and stuff from ini 103 | _description = mySettings.value(INI_KEY_DESC, "").toString(); 104 | _enableScript = mySettings.value(INI_KEY_ENABLED, false).toBool(); 105 | _lastTriggered = mySettings.value(INI_KEY_LAST_TRIGGERED, QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0))).toDateTime(); 106 | 107 | _constraint = mySettings.value(INI_KEY_CONSTRAINT_ENABLED, false).toBool(); 108 | _constraintFrom = mySettings.value(INI_KEY_CONSTRAINT_FROM, QTime()).toTime(); 109 | _constraintTo = mySettings.value(INI_KEY_CONSTRAINT_TO, QTime()).toTime(); 110 | 111 | 112 | // then fetch all groups of ini file 113 | childGroups = mySettings.childGroups(); 114 | 115 | // for all groups of ini file do ... 116 | foreach (childGroup, childGroups) 117 | { 118 | // open current group 119 | mySettings.beginGroup(childGroup); 120 | 121 | // is it a trigger? 122 | if (childGroup.startsWith(INI_KEY_TRIGGER)) 123 | { 124 | className = mySettings.value(INI_KEY_CLASSNAME).toString(); 125 | if(className == "") 126 | { 127 | std::cerr << "[Lua] LuaConfig::fromSettings() : empty trigger classname found in : [" << childGroup.toStdString() << "]" << std::endl; 128 | continue; 129 | } 130 | 131 | if ( className == LUA_TRIGGER_TIMER_INTERVAL) { atrigger = new LuaTriggerTimerInterval(); } 132 | else if (className == LUA_TRIGGER_STARTUP) { atrigger = new LuaTriggerStartup(); } 133 | else if (className == LUA_TRIGGER_SHUTDOWN) { atrigger = new LuaTriggerShutdown(); } 134 | else if (className == LUA_TRIGGER_ONCE) { atrigger = new LuaTriggerOnce(); } 135 | else if (className == LUA_TRIGGER_EVENT) { atrigger = new LuaTriggerEvent(); } 136 | else 137 | { 138 | std::cerr << "[Lua] LuaConfig::fromSettings() : unknown trigger class : '" << className.toStdString() << "' found in [" << childGroup.toStdString() << "]" << std::endl; 139 | atrigger = 0; 140 | } 141 | 142 | // if a trigger has been created, tell him to load his parms 143 | // from mySettings now and add him to our trigger list 144 | if (atrigger != 0) 145 | { 146 | std::cerr << "[Lua] LuaConfig::fromSettings() : trigger : '" << className.toStdString() << "' added." << std::endl; 147 | atrigger->fromSettings(mySettings); 148 | addTrigger(atrigger); 149 | } 150 | } 151 | // close current group 152 | mySettings.endGroup(); 153 | } 154 | } 155 | 156 | // serialize this luaconfig to QSettings data 157 | void LuaConfig::toSettings(QSettings &mySettings) 158 | { 159 | // first save description to ini 160 | if (_description == "") 161 | _description = "It enters a description! It does so whenever it is told to."; 162 | 163 | mySettings.setValue(INI_KEY_DESC, _description); 164 | mySettings.setValue(INI_KEY_ENABLED, _enableScript); 165 | mySettings.setValue(INI_KEY_LAST_TRIGGERED, _lastTriggered); 166 | 167 | mySettings.setValue(INI_KEY_CONSTRAINT_ENABLED, _constraint); 168 | mySettings.setValue(INI_KEY_CONSTRAINT_FROM, _constraintFrom); 169 | mySettings.setValue(INI_KEY_CONSTRAINT_TO, _constraintTo); 170 | 171 | // now save each trigger in a group 172 | QString inigroup; 173 | for (int i = 0 ; i < _myTriggers.size() ; ++i) 174 | { 175 | inigroup = INI_KEY_TRIGGER + QString("_"); 176 | inigroup += QString::number(i); 177 | 178 | // first clean up the ini file for this trigger ... 179 | mySettings.remove(inigroup); 180 | 181 | // ... then rewrite the group with the values of the current trigger 182 | mySettings.beginGroup(inigroup); 183 | _myTriggers.at(i)->toSettings(mySettings); 184 | mySettings.endGroup(); 185 | } 186 | } 187 | 188 | 189 | // getter/setter 190 | 191 | void LuaConfig::enableScript(bool enable) { _enableScript = enable; } 192 | bool LuaConfig::isScriptEnabled() { return _enableScript; } 193 | 194 | void LuaConfig::enableConstraint(bool enable) { _constraint = enable; } 195 | bool LuaConfig::isConstraintEnabled() { return _constraint; } 196 | 197 | void LuaConfig::setConstraintFrom(QTime constraintfrom) { _constraintFrom = constraintfrom; } 198 | QTime LuaConfig::getConstraintFrom() { return _constraintFrom; } 199 | 200 | void LuaConfig::setConstraintTo(QTime constraintto) { _constraintTo = constraintto; } 201 | QTime LuaConfig::getConstraintTo() { return _constraintTo; } 202 | 203 | QString LuaConfig::getDescription() { return _description; } 204 | void LuaConfig::setDescription(const QString& description) { _description = description; } 205 | 206 | QDateTime LuaConfig::getLastTriggered() const { return _lastTriggered; } 207 | void LuaConfig::setLastTriggered(const QDateTime &lastTriggered) { _lastTriggered = lastTriggered; } 208 | 209 | 210 | 211 | -------------------------------------------------------------------------------- /Lua/LuaList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "LuaList.h" 12 | 13 | LuaList::LuaList() : 14 | _filePath("") 15 | { 16 | _luaList.clear(); 17 | } 18 | 19 | LuaList::~LuaList() 20 | { 21 | clearList(); 22 | } 23 | 24 | void LuaList::clearList() 25 | { 26 | for(LuaContainerList::const_iterator it = _luaList.begin(); it != _luaList.end(); ++it) 27 | delete *it; 28 | 29 | _luaList.clear(); 30 | } 31 | 32 | void LuaList::setFilePath(const std::string& path) 33 | { 34 | _filePath = QString::fromStdString(path); 35 | 36 | if(!QDir(_filePath).exists()) 37 | QDir().mkpath(_filePath); 38 | } 39 | 40 | bool LuaList::itemByName(const QString &name, LuaContainer*& container) 41 | { 42 | for(LuaContainerList::const_iterator it = _luaList.begin(); it != _luaList.end(); ++it) 43 | if((*it)->getName() == name) 44 | { 45 | container = (*it); 46 | return true; 47 | } 48 | 49 | return false; 50 | 51 | } 52 | 53 | bool LuaList::itemByName(const std::string& name, LuaContainer*& container) 54 | { 55 | return itemByName(QString::fromStdString(name), container); 56 | } 57 | 58 | LuaContainerList::const_iterator LuaList::begin() 59 | { 60 | return _luaList.begin(); 61 | } 62 | 63 | LuaContainerList::const_iterator LuaList::end() 64 | { 65 | return _luaList.end(); 66 | } 67 | 68 | LuaContainer* LuaList::createItem() 69 | { 70 | LuaContainer* container = new LuaContainer(); 71 | 72 | /* 73 | * name the new container newX.lua with x being the lower number that is not already used 74 | * 75 | * first iterate over all container and get all numbers that are in use (like new1.lua, new3.lua and new4.lua) 76 | * second iterate over all numbers in use and finde the smalles not used 77 | * 78 | * new42.lua 79 | * 012345678 <- position 80 | * 81 | * 42 is at pos 3 and 2 chars long 82 | * 83 | * --> to get the number substring(3, length - 7) will do the trick 84 | */ 85 | QList usedNr; 86 | QString name, nr; 87 | for(LuaContainerList::const_iterator it = _luaList.begin(); it != _luaList.end(); ++it) 88 | { 89 | name = (*it)->getName(); 90 | 91 | // 'new0.lua' is 8 char. long --> minimum length is 8 92 | if(name.length() < 8) 93 | continue; 94 | 95 | if(!(*it)->getName().startsWith("new")) 96 | continue; 97 | 98 | // try to get the number 99 | nr = name.mid(3, name.length() - 7); 100 | int i; 101 | bool ok; 102 | i = nr.toInt(&ok); 103 | if(ok) 104 | usedNr.push_back(i); 105 | } 106 | 107 | // very important 108 | qSort(usedNr); 109 | 110 | if(usedNr.empty()) 111 | // easy! 112 | container->setName(QString("new0.lua")); 113 | else 114 | { 115 | QString name; 116 | for(int i = 0; i < usedNr.size(); ++i) 117 | { 118 | // usedNr[i] shoudl be i! (since we sorted it before) 119 | if(usedNr[i] == i) 120 | continue; 121 | 122 | // if not, we found an unused number 123 | 124 | name = "new"; 125 | name += QString::number(i); 126 | name += ".lua"; 127 | 128 | break; 129 | } 130 | // if all fails ... 131 | if(name == "") 132 | { 133 | name = "new"; 134 | name += QString::number(usedNr.size()); 135 | name += ".lua"; 136 | } 137 | container->setName(name); 138 | } 139 | return container; 140 | } 141 | 142 | void LuaList::addItem(LuaContainer* container) 143 | { 144 | for(LuaContainerList::const_iterator it = _luaList.begin(); it != _luaList.end(); ++it) 145 | if((*it)->getName() == container->getName()) 146 | { 147 | // a file with the same name already exists 148 | std::cerr << "[Lua] LuaList::addItem : ERROR: name already exists " << container->getName().toStdString() << std::endl; 149 | return; 150 | } 151 | 152 | _luaList.push_back(container); 153 | } 154 | 155 | bool LuaList::addItemAndSave(LuaContainer* container) 156 | { 157 | addItem(container); 158 | return save(container); 159 | } 160 | 161 | void LuaList::removeItem(LuaContainer* container) 162 | { 163 | _luaList.remove(container); 164 | } 165 | 166 | bool LuaList::removeItemAndDelete(LuaContainer* container) 167 | { 168 | // remove item from list 169 | removeItem(container); 170 | 171 | // remove item from disk 172 | bool rc = remove(container); 173 | 174 | // remove item from ram 175 | delete container; 176 | 177 | return rc; 178 | } 179 | 180 | size_t LuaList::size() 181 | { 182 | return _luaList.size(); 183 | } 184 | 185 | bool LuaListCompare(LuaContainer* a, LuaContainer* b) 186 | { 187 | return a->getName().compare(b->getName(), Qt::CaseInsensitive); 188 | } 189 | 190 | void LuaList::sort() 191 | { 192 | _luaList.sort(LuaListCompare); 193 | } 194 | 195 | bool LuaList::load(const QString& name, LuaContainer* container, bool ignoreNoSettingsFile) 196 | { 197 | if(_filePath == "") 198 | return false; 199 | 200 | // set name 201 | container->setName(name); 202 | 203 | QString luaFileName, settingsFileName; 204 | getFileNames(name, luaFileName, settingsFileName); 205 | 206 | // load code 207 | { 208 | // check whether file exists 209 | if(!QFile::exists(luaFileName)) 210 | { 211 | std::cerr << "[Lua] can't find Lua file " << luaFileName.toStdString() << std::endl; 212 | return false; 213 | } 214 | 215 | std::ifstream file; 216 | file.open(luaFileName.toLocal8Bit().data(), std::ios::in); 217 | if(!file.good() || !file.is_open()) 218 | { 219 | std::cerr << "[Lua] can't open file " << luaFileName.toStdString() << std::endl; 220 | return false; 221 | } 222 | 223 | std::string line, c = ""; 224 | while(!file.eof()) 225 | { 226 | std::getline(file, line); 227 | c += line; 228 | 229 | // don't add \n at the end of the file 230 | if(!file.eof()) 231 | c += '\n'; 232 | } 233 | container->setCode(c); 234 | 235 | file.close(); 236 | } 237 | 238 | // load config 239 | { 240 | // check whether file exists 241 | if(!QFile::exists(settingsFileName)) 242 | { 243 | if(ignoreNoSettingsFile) 244 | { 245 | std::cerr << "[Lua] can't find settings file " << settingsFileName.toStdString() << " -> continuing" << std::endl; 246 | return true; 247 | } else 248 | { 249 | std::cerr << "[Lua] can't find settings file " << settingsFileName.toStdString() << " -> aborting" << std::endl; 250 | return false; 251 | } 252 | } 253 | 254 | QSettings settings(settingsFileName, QSettings::IniFormat); 255 | container->loadSettings(settings); 256 | } 257 | return true; 258 | } 259 | 260 | bool LuaList::loadAll() 261 | { 262 | std::cout << "[Lua] loading all script files from " << _filePath.toStdString() << std::endl; 263 | 264 | // get everything with ".lua" 265 | QStringList files; 266 | QDirIterator dirIt(_filePath); 267 | while (dirIt.hasNext()) { 268 | dirIt.next(); 269 | if (QFileInfo(dirIt.filePath()).isFile()) 270 | if (QFileInfo(dirIt.filePath()).suffix() == "lua") 271 | files.append(dirIt.fileName()); 272 | } 273 | 274 | if(!_luaList.empty()) 275 | // clear list before loading 276 | clearList(); 277 | 278 | // load lua files 279 | for( QStringList::iterator it = files.begin(); it != files.end(); ++it) 280 | { 281 | LuaContainer* c = new LuaContainer(); 282 | std::cout << "[Lua] loading file " << it->toStdString() << " ..." << std::endl; 283 | if(load((*it), c)) 284 | _luaList.push_back(c); 285 | else 286 | std::cerr << "[Lua] can't load file " << it->toStdString() << std::endl; 287 | } 288 | if(_luaList.empty()) 289 | return false; 290 | else 291 | return true; 292 | } 293 | 294 | bool LuaList::save(LuaContainer* container) 295 | { 296 | if(_filePath == "") 297 | return false; 298 | 299 | QString luaFileName, settingsFileName; 300 | getFileNames(container->getName(), luaFileName, settingsFileName); 301 | 302 | // save code 303 | { 304 | std::ofstream file; 305 | file.open(luaFileName.toLocal8Bit().data(), std::ios::trunc); 306 | if(!file.is_open()) 307 | { 308 | std::cerr << "[Lua] can't open file " << luaFileName.toStdString() << std::endl; 309 | return false; 310 | } 311 | 312 | std::string code; 313 | #ifdef _WIN32 314 | code = container->getCode().toLocal8Bit().constData(); 315 | #else 316 | code = container->getCode().toUtf8().constData(); 317 | #endif 318 | // rest = code 319 | file << code; 320 | 321 | file.flush(); 322 | file.close(); 323 | } 324 | 325 | // save config 326 | { 327 | QSettings settings(settingsFileName, QSettings::IniFormat); 328 | if(!settings.isWritable()) 329 | { 330 | std::cerr << "[Lua] no write access for settings file " << settingsFileName.toStdString() << " -> aborting (code was saved)" << std::endl; 331 | return false; 332 | } 333 | settings.clear(); 334 | container->getSettings(settings); 335 | } 336 | 337 | return true; 338 | } 339 | 340 | bool LuaList::saveAll() 341 | { 342 | std::cout << "[Lua] saving all script files" << std::endl; 343 | bool r = true; 344 | for(LuaContainerList::iterator it = _luaList.begin(); it != _luaList.end(); ++it) 345 | if(!save((*it))) 346 | { 347 | std::cerr << "[Lua] failed to save lua script " << (*it)->getName().toStdString() << std::endl; 348 | r = false; 349 | } 350 | 351 | return r; 352 | } 353 | 354 | bool LuaList::remove(LuaContainer *container) 355 | { 356 | if(_filePath == "") 357 | return false; 358 | 359 | QString fileName = _filePath + container->getName(); 360 | 361 | if( !QFile::exists(fileName)) 362 | { 363 | std::cerr << "[Lua] can't find file " << fileName.toStdString() << std::endl; 364 | return false; 365 | } 366 | 367 | QFile f(fileName); 368 | return f.remove(); 369 | } 370 | 371 | void LuaList::rename(const QString& oldName, const QString& newName) 372 | { 373 | QString luaFileNameOld, luaFileNameNew, settingsFileNameOld, settingsFileNameNew; 374 | getFileNames(oldName, luaFileNameOld, settingsFileNameOld); 375 | getFileNames(newName, luaFileNameNew, settingsFileNameNew); 376 | 377 | QFile fCode(luaFileNameOld); 378 | QFile fSettings(settingsFileNameOld); 379 | 380 | if(fCode.exists()) 381 | fCode.rename(luaFileNameNew); 382 | if(fSettings.exists()) 383 | fSettings.rename(settingsFileNameNew); 384 | } 385 | 386 | void LuaList::getFileNames(const QString& name, QString& luaFileName, QString& settingsFileName) 387 | { 388 | luaFileName = _filePath + name; 389 | settingsFileName = luaFileName + QString(".ini"); 390 | } 391 | 392 | void LuaList::dump() 393 | { 394 | std::cout << "[Lua] dumping LuaList: " << size() << " elements" << std::endl; 395 | uint counter = 0; 396 | for( LuaContainerList::iterator it = _luaList.begin(); it != _luaList.end(); ++it, counter++) 397 | { 398 | LuaContainer* c = *it; 399 | std::cout << " "<< counter << ": "; 400 | if( c == NULL) 401 | std::cout << "NULL" << std::endl; 402 | else 403 | { 404 | std::cout << "Name: " << c->getName().toStdString() << std::endl; 405 | std::cout << "Desc: " << c->getDesc().toStdString() << std::endl; 406 | } 407 | } 408 | } 409 | -------------------------------------------------------------------------------- /Lua/LuaCore.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "LuaCore.h" 10 | #include "LuaList.h" 11 | #include "gui/Lua4RSWidget.h" 12 | #include "Lua4RSNotify.h" 13 | 14 | #include "LuaToRS.cpp" 15 | #include "LuaToRSChat.cpp" 16 | #include "LuaToRSDiscovery.cpp" 17 | #include "LuaToRSPeers.cpp" 18 | #include "LuaToRSServerConfig.cpp" 19 | 20 | #include "helper.h" 21 | 22 | LuaCore::LuaCore() : 23 | _folderName ("Lua4RS"), 24 | _mutex ("Lua4RS"), 25 | _luaList (new LuaList()), 26 | _notify (new Lua4RSNotify()), 27 | _processingEvent(false), 28 | _shutDownImminent (false) 29 | { 30 | /* 31 | * Notes: 32 | * - RS functions get registered to Lua when GUI is initialized 33 | */ 34 | L = luaL_newstate(); 35 | luaL_openlibs(L); 36 | 37 | _path = rsAccounts->PathAccountDirectory() + "/" + _folderName + "/"; 38 | 39 | // load codes 40 | _luaList->setFilePath(_path); 41 | if(_luaList->loadAll()) 42 | std::cout << "[Lua] loaded " << _luaList->size() << " Lua script(s)" << std::endl; 43 | else 44 | std::cout << "[Lua] didn't load any Lua scripts" << std::endl; 45 | } 46 | 47 | LuaCore::~LuaCore() 48 | { 49 | } 50 | 51 | void LuaCore::shutDown() 52 | { 53 | // disbale output - gui might be gone by now! 54 | _ui->disableOutput(); 55 | _shutDownImminent = true; 56 | 57 | // send shotdown event 58 | { 59 | LuaEvent e; 60 | e.eventId = L4R_SHUTDOWN; 61 | e.timeStamp = QDateTime::currentDateTime(); 62 | 63 | processEvent(e); 64 | } 65 | 66 | // save lua scripts 67 | if(_luaList->saveAll()) 68 | std::cout << "[Lua] saved " << _luaList->size() << " Lua script(s)" << std::endl; 69 | else 70 | std::cout << "[Lua] error occured while saving Lua scripts" << std::endl; 71 | 72 | // close lua (after threads is stopped) 73 | lua_close(L); 74 | 75 | delete _notify; 76 | delete _luaList; 77 | } 78 | 79 | void LuaCore::setupRsFunctionsAndTw(QTreeWidget* tw) 80 | { 81 | int top; 82 | std::string namespc; // namespace (with '.' at the end) 83 | 84 | // two namespaces 85 | tw->setColumnCount(2); 86 | 87 | // setup tree widget (after ColumnCount was set!) 88 | // no headers to not to confuse the user 89 | tw->setHeaderHidden(true); 90 | // make it big enough to show the full functionnames 91 | tw->setColumnWidth(0,250); 92 | // second col is only a "container" for the paste value, therefore it needs no width 93 | tw->setColumnWidth(1,0); 94 | 95 | // we need to mess around with lua 96 | RsStackMutex mtx(_mutex); /******* LOCKED MUTEX *****/ 97 | 98 | // rs namespace 99 | namespc = "rs."; 100 | QTreeWidgetItem* rs = new QTreeWidgetItem(tw); 101 | rs->setText(0, QString::fromStdString(namespc)); 102 | rs->setText(1, QString::fromStdString(namespc)); 103 | lua_newtable(L); 104 | top = lua_gettop(L); 105 | 106 | addFunctionToLuaAndTw(top, namespc, rs, rs_clear, "clear()", QObject::tr("clears the output")); 107 | addFunctionToLuaAndTw(top, namespc, rs, rs_print, "print()", QObject::tr("prints to output")); 108 | 109 | lua_setglobal(L, "rs"); 110 | 111 | // peers namespace 112 | namespc = "peers."; 113 | QTreeWidgetItem* peers = new QTreeWidgetItem(tw); 114 | peers->setText(0, QString::fromStdString(namespc)); 115 | peers->setText(1, QString::fromStdString(namespc)); 116 | lua_newtable(L); 117 | top = lua_gettop(L); 118 | 119 | addFunctionToLuaAndTw(top, namespc, peers, peers_getOwnId, "getOwnId()", QObject::tr("returns own SSL id (peer id)")); 120 | addFunctionToLuaAndTw(top, namespc, peers, peers_getOnlineList, "getOnlineList()", QObject::tr("returns list of online friends (SSL id)")); 121 | addFunctionToLuaAndTw(top, namespc, peers, peers_getFriendList, "getFriendList()", QObject::tr("returns list of all friends (SSL id)")); 122 | addFunctionToLuaAndTw(top, namespc, peers, peers_getPeerCount, "getPeerCount()", QObject::tr("returns number of all friends and online friends")); 123 | 124 | addFunctionToLuaAndTw(top, namespc, peers, peers_isFriend, "isFriend()", QObject::tr("returns if a peer is a friend")); 125 | addFunctionToLuaAndTw(top, namespc, peers, peers_isGPGAccepted, "isGPGAccepted()", QObject::tr("returns is a PGP key is accepted")); 126 | addFunctionToLuaAndTw(top, namespc, peers, peers_isOnline, "isOnline()", QObject::tr("returns if a peer is online")); 127 | addFunctionToLuaAndTw(top, namespc, peers, peers_getGPGName, "getGPGName()", QObject::tr("returns the PGP name for a given PGP id")); 128 | addFunctionToLuaAndTw(top, namespc, peers, peers_getPeerName, "getPeerName()", QObject::tr("returns the name for a given SSL/PGP id")); 129 | addFunctionToLuaAndTw(top, namespc, peers, peers_getPeerDetails, "getPeerDetails()", QObject::tr("returns peer details as a table for a given SSL id")); 130 | 131 | addFunctionToLuaAndTw(top, namespc, peers, peers_getGPGOwnId, "getGPGOwnId()", QObject::tr("returns own PGP id")); 132 | addFunctionToLuaAndTw(top, namespc, peers, peers_getGPGId, "getGPGId()", QObject::tr("returns the PGP id for a given SSL/PGP id")); 133 | 134 | //groups 135 | addFunctionToLuaAndTw(top, namespc, peers, peers_addGroup, "addGroup()", QObject::tr("creates a new group")); 136 | addFunctionToLuaAndTw(top, namespc, peers, peers_editGroup, "editGroup()", QObject::tr("edits an existing group")); 137 | addFunctionToLuaAndTw(top, namespc, peers, peers_removeGroup, "removeGroup()", QObject::tr("removes the group with the given groupd id")); 138 | addFunctionToLuaAndTw(top, namespc, peers, peers_getGroupInfo, "getGroupInfo()", QObject::tr("returns group info for a given group id")); 139 | addFunctionToLuaAndTw(top, namespc, peers, peers_getGroupInfoList, "getGroupInfoList()", QObject::tr("returns an array with all groups and their group infos")); 140 | addFunctionToLuaAndTw(top, namespc, peers, peers_assignPeerToGroup, "assignPeerToGroup()", QObject::tr("returns the PGP id for a given SSL/PGP id")); 141 | 142 | lua_setglobal(L, "peers"); 143 | 144 | // server config 145 | namespc = "config."; 146 | QTreeWidgetItem* config = new QTreeWidgetItem(tw); 147 | config->setText(0, QString::fromStdString(namespc)); 148 | config->setText(1, QString::fromStdString(namespc)); 149 | lua_newtable(L); 150 | top = lua_gettop(L); 151 | 152 | addFunctionToLuaAndTw(top, namespc, config, config_getOperatingMode, "getOperatingMode()", QObject::tr("returns the current operation mode as int and string")); 153 | addFunctionToLuaAndTw(top, namespc, config, config_setOperatingMode, "setOperatingMode()", QObject::tr("sets the openration mode (takes int or string)")); 154 | 155 | addFunctionToLuaAndTw(top, namespc, config, config_setMaxDataRates, "setMaxDataRates()", QObject::tr("sets max down-/upload bandwidth in kB")); 156 | addFunctionToLuaAndTw(top, namespc, config, config_getMaxDataRates, "getMaxDataRates()", QObject::tr("gets max down-/upload bandwidth in kB")); 157 | addFunctionToLuaAndTw(top, namespc, config, config_getCurrentDataRates, "getCurrentDataRates()",QObject::tr("gets current down-/upload bandwidth in kB")); 158 | 159 | lua_setglobal(L, "config"); 160 | 161 | // discovery 162 | namespc = "disc."; 163 | QTreeWidgetItem* discovery = new QTreeWidgetItem(tw); 164 | discovery->setText(0, QString::fromStdString(namespc)); 165 | discovery->setText(1, QString::fromStdString(namespc)); 166 | lua_newtable(L); 167 | top = lua_gettop(L); 168 | 169 | addFunctionToLuaAndTw(top, namespc, discovery, disc_getDiscFriends, "getDiscFriends()", QObject::tr("gets discovery infos for a SSLID")); 170 | addFunctionToLuaAndTw(top, namespc, discovery, disc_getDiscPgpFriends, "getDiscPgpFriends()", QObject::tr("gets discovery infos for a PGPID")); 171 | addFunctionToLuaAndTw(top, namespc, discovery, disc_getPeerVersion, "getPeerVersion()", QObject::tr("gets RS version from a given peer")); 172 | addFunctionToLuaAndTw(top, namespc, discovery, disc_getWaitingDiscCount,"getWaitingDiscCount()",QObject::tr("gets current pending discovery packets (down und up)")); 173 | 174 | lua_setglobal(L, "disc"); 175 | 176 | // chat 177 | namespc = "chat."; 178 | QTreeWidgetItem* chat = new QTreeWidgetItem(tw); 179 | chat->setText(0, QString::fromStdString(namespc)); 180 | chat->setText(1, QString::fromStdString(namespc)); 181 | lua_newtable(L); 182 | top = lua_gettop(L); 183 | 184 | addFunctionToLuaAndTw(top, namespc, chat, chat_sendChat, "sendChat()", QObject::tr("send a chat message (ChatId, msg)")); 185 | 186 | lua_setglobal(L, "chat"); 187 | 188 | // files 189 | namespc = "files."; 190 | QTreeWidgetItem* files = new QTreeWidgetItem(tw); 191 | files->setText(0, QString::fromStdString(namespc)); 192 | files->setText(1, QString::fromStdString(namespc)); 193 | lua_newtable(L); 194 | top = lua_gettop(L); 195 | 196 | addFunctionToLuaAndTw(top, namespc, files, file_fileRequest, "fileRequest()", QObject::tr("request a download (params: *name*, *hash*, *size*")); 197 | 198 | lua_setglobal(L, "files"); 199 | } 200 | 201 | void LuaCore::addFunctionToLuaAndTw(int tableTop, const std::string& namespc, QTreeWidgetItem* item, int (*f)(lua_State*), const std::string& name, const QString& hint) 202 | { 203 | QTreeWidgetItem *i = new QTreeWidgetItem(item); 204 | i->setText(0, QString::fromStdString(name)); 205 | i->setText(1, QString::fromStdString(namespc + name)); 206 | i->setToolTip(0, hint); 207 | 208 | // name can be like foo(bar) but function name is just foo 209 | std::string luaFuncName; 210 | size_t pos; 211 | if((pos = name.find_first_of('(')) != std::string::npos) 212 | luaFuncName = name.substr(0, pos); 213 | else 214 | luaFuncName = name; 215 | 216 | pushTable(L, tableTop, luaFuncName, f); 217 | } 218 | 219 | bool LuaCore::processEvent(const LuaEvent& e) 220 | { 221 | // to catch to early events 222 | if(_ui == NULL) 223 | { 224 | std::cerr << "[Lua] LuaCore not ready - event " << e.eventId << std::endl; 225 | return false; 226 | } 227 | 228 | // block everythign except onShutdown when RS is exiting 229 | if(_shutDownImminent && e.eventId != L4R_SHUTDOWN) 230 | return false; 231 | 232 | // exit when we are already executing code for a previous event 233 | if(_processingEvent) 234 | return false; 235 | _processingEvent = true; 236 | // do some magic here 237 | // std::cout << "[Lua] processing event : " << e.eventId << std::endl; 238 | for(LuaContainerList::const_iterator it = _luaList->begin(); it != _luaList->end(); ++it) 239 | { 240 | if((*it)->isTriggered(e)) 241 | runLuaByEvent((*it), e); 242 | } 243 | _processingEvent = false; 244 | return true; 245 | } 246 | 247 | // invoke lua 248 | void LuaCore::runLuaByString(const QString& code) 249 | { 250 | if(_ui == NULL) 251 | { 252 | std::cerr << "[Lua] runLuaByString: ERROR: _ui is NULL -> aborting Lua execution" << std::endl; 253 | return; 254 | } 255 | 256 | std::string code2; 257 | #ifdef _WIN32 258 | code2 = code.toLocal8Bit().constData(); 259 | #else 260 | code2 = code.toUtf8().constData(); 261 | #endif 262 | 263 | RsStackMutex mtx(_mutex); /******* LOCKED MUTEX *****/ 264 | int ret = luaL_dostring(L, code2.c_str()); 265 | reportLuaErrors(L, ret); 266 | } 267 | 268 | void LuaCore::runLuaByName(const QString& name) 269 | { 270 | // get code 271 | LuaContainer* lc = NULL; 272 | if(!_luaList->itemByName(name, lc)) 273 | { 274 | std::cerr << "[Lua] can't find script " << name.toStdString() << std::endl; 275 | return; 276 | } 277 | 278 | runLuaByString(lc->getCode()); 279 | } 280 | 281 | void LuaCore::runLuaByEvent(LuaContainer* container, const LuaEvent& event) 282 | { 283 | { 284 | RsStackMutex mtx(_mutex); /******* LOCKED MUTEX *****/ 285 | 286 | // clear old parameter 287 | luaL_dostring(L, "args = nil"); 288 | 289 | // set parameter 290 | lua_newtable(L); 291 | int top = lua_gettop(L); 292 | 293 | QStringList keys = event.dataParm->allKeys(); 294 | for(QStringList::ConstIterator it = keys.begin(); it != keys.end(); it++) 295 | { 296 | QString key = *it; 297 | QString type = key.mid(0, 3); 298 | //std::cout << "[Lua] runByEvent adding type " << type.toStdString() << " key is " << key.toStdString() << std::endl; 299 | if(!(type == "str" || type == "int" || type == "u32" || type != "u64")) 300 | continue; 301 | 302 | QString name = key.mid(3); 303 | QVariant value = event.dataParm->value(key); 304 | 305 | //std::cout << "[Lua] runByEvent adding " << name.toStdString() << " with " << value.toString().toStdString() << std::endl; 306 | 307 | lua_pushfstring(L, name.toStdString().c_str()); 308 | if(type == "str") { 309 | // PANIC: unprotected error in call to Lua API (invalid option '%1' to 'lua_pushfstring') 310 | ///TODO proper fix 311 | std::string s = value.toString().toStdString(); 312 | replaceAll(s, "%", ""); 313 | lua_pushfstring(L, s.c_str()); 314 | } 315 | else if(type == "int") 316 | lua_pushinteger(L, value.toInt()); 317 | else if(type == "u32") 318 | lua_pushinteger(L, value.toUInt()); 319 | else if(type == "u64") 320 | lua_pushinteger(L, value.toULongLong()); 321 | 322 | lua_settable(L, top); 323 | } 324 | 325 | lua_setglobal(L, "args"); 326 | 327 | } 328 | emit appendLog(QObject::tr("triggered script: ") + container->getName()); 329 | runLuaByString(container->getCode()); 330 | } 331 | 332 | void LuaCore::reportLuaErrors(lua_State *L, int status) 333 | { 334 | std::string s; 335 | if ( status!=0 ) { 336 | s = lua_tostring(L, -1); 337 | std::cerr << "-- " << s << std::endl; 338 | 339 | s = "Lua error: " + s; 340 | emit appendLog(QString::fromStdString(s)); 341 | 342 | lua_pop(L, 1); // remove error message 343 | } 344 | } 345 | 346 | // getter & setter 347 | Lua4RSNotify* LuaCore::notify() const 348 | { 349 | return _notify; 350 | } 351 | 352 | LuaList* LuaCore::codeList() const 353 | { 354 | return _luaList; 355 | } 356 | 357 | void LuaCore::setUi(Lua4RSWidget *ui) 358 | { 359 | _ui = ui; 360 | 361 | QObject::connect(this, SIGNAL(appendLog(QString)), _ui, SLOT(appendLog(QString))); 362 | QObject::connect(this, SIGNAL(appendOutput(QString)), _ui, SLOT(appendOutput(QString))); 363 | QObject::connect(this, SIGNAL(clearOutput()), _ui, SLOT(clearOutput())); 364 | } 365 | 366 | Lua4RSWidget* LuaCore::getUI() 367 | { 368 | ///TODO better fix 369 | assert(_ui); 370 | 371 | return _ui; 372 | } 373 | 374 | // emit 375 | void LuaCore::emitAppendOutput(const QString &s) { emit appendOutput(s); } 376 | void LuaCore::emitClearOutput() { emit clearOutput(); } 377 | -------------------------------------------------------------------------------- /Lua/LuaToRSPeers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "LuaCore.h" 4 | #include "LuaToRS.h" 5 | 6 | extern "C" { 7 | #include 8 | #include 9 | #include 10 | } 11 | 12 | extern "C" { 13 | 14 | /* Peer Details (Net & Auth) */ 15 | 16 | // virtual const RsPeerId& getOwnId() 17 | int peers_getOwnId(lua_State* L) 18 | { 19 | RsPeerId peerId = rsPeers->getOwnId(); 20 | lua_pushstring(L, peerId.toStdString().c_str()); 21 | return 1; 22 | } 23 | 24 | // virtual bool getOnlineList(std::list &ssl_ids) 25 | int peers_getOnlineList(lua_State* L) 26 | { 27 | std::list ids; 28 | if(!rsPeers->getOnlineList(ids)) 29 | return 0; 30 | 31 | lua_newtable(L); 32 | int top = lua_gettop(L); 33 | 34 | std::list::iterator it = ids.begin(); 35 | for (size_t i = 1; it != ids.end(); i++, ++it) 36 | pushArray(L, top, i, it->toStdString().c_str()); 37 | return 1; 38 | } 39 | 40 | // virtual bool getFriendList(std::list &ssl_ids) 41 | int peers_getFriendList(lua_State* L) 42 | { 43 | std::list ids; 44 | if(!rsPeers->getFriendList(ids)) 45 | return 0; 46 | 47 | lua_newtable(L); 48 | int top = lua_gettop(L); 49 | 50 | std::list::iterator it = ids.begin(); 51 | for (size_t i = 1; it != ids.end(); i++, ++it) 52 | pushArray(L, top, i, it->toStdString().c_str()); 53 | 54 | return 1; 55 | } 56 | 57 | // virtual bool getPeerCount (unsigned int *pnFriendCount, unsigned int *pnnOnlineCount, bool ssl) 58 | int peers_getPeerCount(lua_State* L) 59 | { 60 | unsigned int online, all; 61 | if(!rsPeers->getPeerCount(&all, &online, false)) 62 | return 0; 63 | lua_pushinteger(L, all); 64 | lua_pushinteger(L, online); 65 | return 2; 66 | } 67 | 68 | // virtual bool isOnline(const RsPeerId &ssl_id) 69 | int peers_isOnline(lua_State* L) 70 | { 71 | luaL_checktype(L, 1, LUA_TSTRING); 72 | 73 | const RsPeerId sslid = RsPeerId(luaL_checkstring(L, 1)); 74 | const bool isOnline = rsPeers->isOnline(sslid); 75 | lua_pushboolean(L, isOnline); 76 | return 1; 77 | } 78 | 79 | // virtual bool isFriend(const RsPeerId &ssl_id) 80 | int peers_isFriend(lua_State* L) 81 | { 82 | luaL_checktype(L, 1, LUA_TSTRING); 83 | 84 | const RsPeerId sslid = RsPeerId(luaL_checkstring(L, 1)); 85 | const bool isFriend = rsPeers->isFriend(sslid); 86 | lua_pushboolean(L, isFriend); 87 | return 1; 88 | } 89 | 90 | // virtual bool isGPGAccepted(const RsPgpId &gpg_id_is_friend) 91 | int peers_isGPGAccepted(lua_State* L) 92 | { 93 | luaL_checktype(L, 1, LUA_TSTRING); 94 | 95 | const RsPgpId gpgid = RsPgpId(luaL_checkstring(L, 1)); 96 | const bool isAccepted = rsPeers->isGPGAccepted(gpgid); 97 | lua_pushboolean(L, isAccepted); 98 | return 1; 99 | } 100 | 101 | // virtual std::string getPeerName(const RsPeerId &ssl_id) 102 | int peers_getPeerName(lua_State* L) 103 | { 104 | luaL_checktype(L, 1, LUA_TSTRING); 105 | 106 | const RsPeerId id = RsPeerId(luaL_checkstring(L, 1)); 107 | const std::string name = rsPeers->getPeerName(id); 108 | lua_pushstring(L, name.c_str()); 109 | return 1; 110 | } 111 | 112 | // virtual std::string getGPGName(const RsPgpId& gpg_id) 113 | int peers_getGPGName(lua_State* L) 114 | { 115 | luaL_checktype(L, 1, LUA_TSTRING); 116 | 117 | const RsPgpId pgpId = RsPgpId(luaL_checkstring(L, 1)); 118 | const std::string name = rsPeers->getGPGName(pgpId); 119 | lua_pushstring(L, name.c_str()); 120 | return 1; 121 | } 122 | 123 | // virtual bool getPeerDetails(const RsPeerId& ssl_id, RsPeerDetails &d) 124 | int peers_getPeerDetails(lua_State* L) 125 | { 126 | luaL_checktype(L, 1, LUA_TSTRING); 127 | 128 | const RsPeerId id = RsPeerId(luaL_checkstring(L, 1)); 129 | RsPeerDetails details; 130 | if(!rsPeers->getPeerDetails(id, details)) 131 | return 0; 132 | 133 | lua_newtable(L); 134 | int t1 = lua_gettop(L); 135 | 136 | pushTable(L, t1, "id", details.id.toStdString().c_str()); 137 | pushTable(L, t1, "gpg_id", details.gpg_id.toStdString().c_str()); 138 | pushTable(L, t1, "name", details.name.c_str()); 139 | pushTable(L, t1, "email", details.email.c_str()); 140 | pushTable(L, t1, "location", details.location.c_str()); 141 | pushTable(L, t1, "org", details.org.c_str()); 142 | pushTable(L, t1, "state", details.state); 143 | pushTable(L, t1, "connectAddr", details.connectAddr.c_str()); 144 | pushTable(L, t1, "connectPort", details.connectPort); 145 | pushTable(L, t1, "extAddr", details.extAddr.c_str()); 146 | pushTable(L, t1, "extPort", details.extPort); 147 | 148 | lua_pushstring(L, "ipAddressList"); 149 | { 150 | lua_newtable(L); 151 | int t2 = lua_gettop(L); 152 | int i = 1; 153 | for(std::list::iterator it = details.ipAddressList.begin(); it != details.ipAddressList.end(); ++it, i++) 154 | pushArray(L, t2, i, it->c_str()); 155 | 156 | /* this doesn't fit to an array 157 | lua_pushstring(L, "size"); 158 | lua_pushunsigned(L, details.ipAddressList.size()); 159 | lua_settable(L, t2); 160 | */ 161 | } 162 | lua_settable(L, t1); 163 | 164 | pushTable(L, t1, "lastConnect", details.lastConnect); 165 | pushTable(L, t1, "lastUsed", details.lastUsed); 166 | pushTable(L, t1, "connectState", details.connectState); 167 | pushTable(L, t1, "connectStateString", details.connectStateString.c_str()); 168 | 169 | return 1; 170 | } 171 | 172 | 173 | 174 | /* Using PGP Ids */ 175 | 176 | // virtual const RsPgpId& getGPGOwnId() 177 | int peers_getGPGOwnId(lua_State* L) 178 | { 179 | const RsPgpId pgpId = rsPeers->getGPGOwnId(); 180 | lua_pushstring(L, pgpId.toStdString().c_str()); 181 | return 1; 182 | } 183 | 184 | // virtual RsPgpId getGPGId(const RsPeerId& sslid) 185 | int peers_getGPGId(lua_State* L) 186 | { 187 | luaL_checktype(L, 1, LUA_TSTRING); 188 | 189 | const RsPeerId id = RsPeerId(luaL_checkstring(L, 1)); 190 | const RsPgpId gpgId = rsPeers->getGPGId(id); 191 | lua_pushstring(L, gpgId.toStdString().c_str()); 192 | return 1; 193 | } 194 | 195 | 196 | 197 | /* Group Stuff */ 198 | 199 | // virtual bool addGroup(RsGroupInfo &groupInfo) 200 | int peers_addGroup(lua_State* L) 201 | { 202 | luaL_checktype(L, 1, LUA_TTABLE); 203 | 204 | RsGroupInfo grpInfo; 205 | lua_getfield(L, 1, "flag"); 206 | grpInfo.flag = luaL_checkinteger(L, -1); 207 | lua_getfield(L, 1, "id"); 208 | grpInfo.id = luaL_checkstring(L, -1); 209 | lua_getfield(L, 1, "name"); 210 | grpInfo.name = luaL_checkstring(L, -1); 211 | // irgnore peerIds for now 212 | 213 | rsPeers->addGroup(grpInfo); 214 | return 0; 215 | } 216 | 217 | // virtual bool editGroup(const std::string &groupId, RsGroupInfo &groupInfo) 218 | int peers_editGroup(lua_State* L) 219 | { 220 | luaL_checktype(L, 1, LUA_TSTRING); 221 | luaL_checktype(L, 2, LUA_TTABLE); 222 | 223 | const std::string grpId = luaL_checkstring(L, 1); 224 | 225 | RsGroupInfo grpInfo; 226 | lua_getfield(L, 2, "flag"); 227 | grpInfo.flag = luaL_checkinteger(L, -1); 228 | lua_getfield(L, 2, "id"); 229 | grpInfo.id = luaL_checkstring(L, -1); 230 | lua_getfield(L, 2, "name"); 231 | grpInfo.name = luaL_checkstring(L, -1); 232 | // irgnore peerIds for now 233 | 234 | rsPeers->editGroup(grpId, grpInfo); 235 | return 0; 236 | } 237 | 238 | // virtual bool removeGroup(const std::string &groupId) 239 | int peers_removeGroup(lua_State* L) 240 | { 241 | luaL_checktype(L, 1, LUA_TSTRING); 242 | 243 | const std::string grpId = luaL_checkstring(L, 1); 244 | rsPeers->removeGroup(grpId); 245 | return 0; 246 | } 247 | 248 | // virtual bool getGroupInfo(const std::string &groupId, RsGroupInfo &groupInfo) 249 | int peers_getGroupInfo(lua_State* L) 250 | { 251 | 252 | luaL_checktype(L, 1, LUA_TSTRING); 253 | 254 | const std::string grpId = luaL_checkstring(L, 1); 255 | RsGroupInfo grpInfo; 256 | if(!rsPeers->getGroupInfo(grpId, grpInfo)) 257 | return 0; 258 | 259 | lua_newtable(L); 260 | int t1 = lua_gettop(L); 261 | pushTable(L, t1, "flag", grpInfo.flag); 262 | pushTable(L, t1, "id", grpInfo.id); 263 | pushTable(L, t1, "name", grpInfo.name); 264 | 265 | lua_pushstring(L, "peerIds"); 266 | { 267 | lua_newtable(L); 268 | int t2 = lua_gettop(L); 269 | int i = 1; 270 | foreach (RsPgpId peerId, grpInfo.peerIds) { 271 | pushArray(L, t2, i, peerId.toStdString()); 272 | ++i; 273 | } 274 | } 275 | lua_settable(L, t1); 276 | return 1; 277 | } 278 | 279 | // virtual bool getGroupInfoList(std::list &groupInfoList) 280 | int peers_getGroupInfoList(lua_State* L) 281 | { 282 | std::list groupInfoList; 283 | if(!rsPeers->getGroupInfoList(groupInfoList)) 284 | return 0; 285 | 286 | lua_newtable(L); 287 | int t1 = lua_gettop(L); 288 | uint i = 1; 289 | foreach (RsGroupInfo grpInfo, groupInfoList) { 290 | lua_pushinteger(L, i); 291 | { 292 | lua_newtable(L); 293 | int t2 = lua_gettop(L); 294 | pushTable(L, t2, "falg", grpInfo.flag); 295 | pushTable(L, t2, "id", grpInfo.id); 296 | pushTable(L, t2, "name", grpInfo.name); 297 | 298 | lua_pushstring(L, "peerIds"); 299 | { 300 | lua_newtable(L); 301 | int t3 = lua_gettop(L); 302 | int j = 1; 303 | foreach (RsPgpId peerId, grpInfo.peerIds) { 304 | pushArray(L, t3, j, peerId.toStdString()); 305 | ++j; 306 | } 307 | } 308 | lua_settable(L, t2); 309 | } 310 | lua_settable(L, t1); 311 | ++i; 312 | } 313 | return 1; 314 | } 315 | 316 | // groupId == "" && assign == false -> remove from all groups 317 | // virtual bool assignPeerToGroup(const std::string &groupId, const RsPgpId& peerId, bool assign) 318 | int peers_assignPeerToGroup(lua_State* L) 319 | { 320 | luaL_checktype(L, 1, LUA_TSTRING); 321 | luaL_checktype(L, 2, LUA_TSTRING); 322 | luaL_checktype(L, 3, LUA_TBOOLEAN); 323 | 324 | const std::string grpId = luaL_checkstring(L, 1); 325 | const RsPgpId peerId = RsPgpId(luaL_checkstring(L, 2)); 326 | const bool assign = lua_toboolean(L, 3); 327 | rsPeers->assignPeerToGroup(grpId, peerId, assign); 328 | return 0; 329 | } 330 | 331 | } 332 | 333 | /// TODO 334 | ///* Peer Details (Net & Auth) */ 335 | //virtual bool haveSecretKey(const RsPgpId& gpg_id) = 0 ; 336 | //virtual bool getGPGDetails(const RsPgpId& gpg_id, RsPeerDetails &d) = 0; 337 | 338 | ///* Using PGP Ids */ 339 | //virtual bool isKeySupported(const RsPgpId& gpg_ids) = 0; 340 | //virtual bool getGPGAcceptedList(std::list &gpg_ids) = 0; 341 | //virtual bool getGPGSignedList(std::list &gpg_ids) = 0;//friends that we accpet to connect with but we don't want to sign their gpg key 342 | //virtual bool getGPGValidList(std::list &gpg_ids) = 0; 343 | //virtual bool getGPGAllList(std::list &gpg_ids) = 0; 344 | //virtual bool getAssociatedSSLIds(const RsPgpId& gpg_id, std::list& ids) = 0; 345 | //virtual bool gpgSignData(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen) = 0; 346 | 347 | ///* Add/Remove Friends */ 348 | //virtual bool addFriend(const RsPeerId &ssl_id, const RsPgpId &gpg_id,ServicePermissionFlags flags = RS_SERVICE_PERM_ALL) = 0; 349 | //virtual bool removeFriend(const RsPgpId& pgp_id) = 0; 350 | //virtual bool removeFriendLocation(const RsPeerId& sslId) = 0; 351 | 352 | ///* keyring management */ 353 | //virtual bool removeKeysFromPGPKeyring(const std::list& pgp_ids,std::string& backup_file,uint32_t& error_code)=0 ; 354 | 355 | ///* Network Stuff */ 356 | //virtual bool connectAttempt(const RsPeerId& ssl_id) = 0; 357 | //virtual bool setLocation(const RsPeerId &ssl_id, const std::string &location) = 0;//location is shown in the gui to differentiate ssl certs 358 | 359 | //virtual bool setHiddenNode(const RsPeerId &id, const std::string &hidden_node_address) = 0; 360 | //virtual bool setHiddenNode(const RsPeerId &id, const std::string &address, uint16_t port) = 0; 361 | 362 | //virtual bool setLocalAddress(const RsPeerId &ssl_id, const std::string &addr, uint16_t port) = 0; 363 | //virtual bool setExtAddress( const RsPeerId &ssl_id, const std::string &addr, uint16_t port) = 0; 364 | //virtual bool setDynDNS(const RsPeerId &id, const std::string &addr) = 0; 365 | //virtual bool setNetworkMode(const RsPeerId &ssl_id, uint32_t netMode) = 0; 366 | //virtual bool setVisState(const RsPeerId &ssl_id, uint16_t vs_disc, uint16_t vs_dht) = 0; 367 | 368 | //virtual bool getProxyServer(std::string &addr, uint16_t &port,uint32_t& status_flags) = 0; 369 | //virtual bool setProxyServer(const std::string &addr, const uint16_t port) = 0; 370 | 371 | //virtual void getIPServersList(std::list& ip_servers) = 0; 372 | //virtual void allowServerIPDetermination(bool) = 0; 373 | //virtual bool getAllowServerIPDetermination() = 0 ; 374 | 375 | ///* Auth Stuff */ 376 | //virtual std::string GetRetroshareInvite(const RsPeerId& ssl_id,bool include_signatures) = 0; 377 | //virtual std::string getPGPKey(const RsPgpId& pgp_id,bool include_signatures) = 0; 378 | //virtual bool GetPGPBase64StringAndCheckSum(const RsPgpId& gpg_id,std::string& gpg_base64_string,std::string& gpg_base64_checksum) = 0 ; 379 | //virtual std::string GetRetroshareInvite(bool include_signatures) = 0; 380 | //virtual bool hasExportMinimal() = 0 ; 381 | 382 | //// Add keys to the keyring 383 | //virtual bool loadCertificateFromString(const std::string& cert, RsPeerId& ssl_id,RsPgpId& pgp_id, std::string& error_string) = 0; 384 | 385 | //// Gets the GPG details, but does not add the key to the keyring. 386 | //virtual bool loadDetailsFromStringCert(const std::string& certGPG, RsPeerDetails &pd,uint32_t& error_code) = 0; 387 | 388 | //// Certificate utils 389 | //virtual bool cleanCertificate(const std::string &certstr, std::string &cleanCert,int& error_code) = 0; 390 | //virtual bool saveCertificateToFile(const RsPeerId& id, const std::string &fname) = 0; 391 | //virtual std::string saveCertificateToString(const RsPeerId &id) = 0; 392 | 393 | //virtual bool signGPGCertificate(const RsPgpId &gpg_id) = 0; 394 | //virtual bool trustGPGCertificate(const RsPgpId &gpg_id, uint32_t trustlvl) = 0; 395 | 396 | ///* Group Stuff */ 397 | //virtual bool assignPeersToGroup(const std::string &groupId, const std::list &peerIds, bool assign) = 0; 398 | 399 | ///* Group sharing permission */ 400 | 401 | //// Given 402 | //// - the peer id 403 | //// - the permission flags of a given hash, e.g. a combination of 404 | //// RS_DIR_FLAGS_NETWORK_WIDE_OTHERS, RS_DIR_FLAGS_NETWORK_WIDE_GROUPS, RS_DIR_FLAGS_BROWSABLE_OTHERS and RS_DIR_FLAGS_BROWSABLE_GROUPS 405 | //// - the parent groups of the file 406 | //// 407 | //// ... computes the sharing file permission hint flags set for this peer, that is a combination of 408 | //// RS_FILE_HINTS_NETWORK_WIDE and RS_FILE_HINTS_BROWSABLE. 409 | //// 410 | //virtual FileSearchFlags computePeerPermissionFlags(const RsPeerId& peer_id,FileStorageFlags file_sharing_flags,const std::list& file_parent_groups) = 0; 411 | 412 | ///* Service permission flags */ 413 | 414 | //virtual ServicePermissionFlags servicePermissionFlags(const RsPgpId& gpg_id) = 0; 415 | //virtual ServicePermissionFlags servicePermissionFlags(const RsPeerId& ssl_id) = 0; 416 | //virtual void setServicePermissionFlags(const RsPgpId& gpg_id,const ServicePermissionFlags& flags) = 0; 417 | -------------------------------------------------------------------------------- /gui/Lua4RSWidget.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "ui_Lua4RSWidget.h" 10 | #include "Lua4RSWidget.h" 11 | #include "Lua/LuaCore.h" 12 | #include "Lua/LuaList.h" 13 | #include "interface/L4RInterface.h" 14 | 15 | #define ALL_SCRIPTS_COLUMN_ENABLE 4 16 | 17 | Lua4RSWidget::Lua4RSWidget(QWidget *parent) : 18 | MainPage(parent), 19 | ui(new Ui::Lua4RSWidget), 20 | _activeContainer(NULL), 21 | _disableOutput(false) 22 | { 23 | ui->setupUi(this); 24 | 25 | _lua = L4R::L4RConfig->getCore(); 26 | 27 | setLuaCodes(_lua->codeList()); 28 | 29 | clearUi(); 30 | 31 | luaContainerToUi(_activeContainer); 32 | 33 | // Fill Hints TreeWidget with main items 34 | _lua->setupRsFunctionsAndTw(ui->tw_hints); 35 | 36 | // f*c: Set header resize mode of tw_allscripts to content dependant 37 | #if QT_VERSION < 0x050000 38 | ui->tw_allscripts->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents); 39 | #else 40 | ui->tw_allscripts->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); 41 | #endif 42 | 43 | // Help Button 44 | QString help_str = tr( 45 | "

  Lua4RS

\ 46 |

With Lua4RS you get three things with one Plugin:

\ 47 |
    \ 48 |
  • You can write, save, load and run Lua programs within RetroShare.
  • \ 49 |
  • You can use Lua programs like macros (think of macros in LibreOffice) \ 50 | to control and automate many features of RetroShare.
  • \ 51 |
  • You can execute your Lua programs either by timer control (think of \ 52 | cron or at) or by certain RetroShare events (e.g. a friend comes \ 53 | online or a chat message is received and many more).
  • \ 54 |
\ 55 | "); 56 | 57 | registerHelpButton(ui->helpButton, help_str); 58 | 59 | QShortcut *shortcut = new QShortcut(QKeySequence("Ctrl+S"), ui->pte_luacode); 60 | QObject::connect(shortcut, SIGNAL(activated()), this, SLOT(on_pb_save_clicked())); 61 | } 62 | 63 | Lua4RSWidget::~Lua4RSWidget() 64 | { 65 | delete ui; 66 | } 67 | 68 | void Lua4RSWidget::disableOutput() 69 | { 70 | _disableOutput = true; 71 | } 72 | 73 | void Lua4RSWidget::setLuaCodes(LuaList* list) 74 | { 75 | ui->tw_allscripts->setRowCount(0); 76 | 77 | // disable sorting (better performance) 78 | ui->tw_allscripts->setSortingEnabled(false); 79 | LuaContainerList::const_iterator it; 80 | for(it = list->begin(); it != list->end(); ++it) 81 | allScriptsAddRow(*it); 82 | 83 | ui->tw_allscripts->setSortingEnabled(true); 84 | } 85 | 86 | void Lua4RSWidget::clearOutput() 87 | { 88 | if(!_disableOutput) 89 | ui->tb_output->clear(); 90 | } 91 | 92 | void Lua4RSWidget::appendOutput(const std::string& s) 93 | { 94 | appendOutput(QString::fromStdString(s)); 95 | } 96 | 97 | void Lua4RSWidget::appendOutput(const QString& s) 98 | { 99 | if(!_disableOutput) 100 | ui->tb_output->appendPlainText(s); 101 | } 102 | 103 | void Lua4RSWidget::appendLog(const std::string& s) 104 | { 105 | appendLog(QString::fromUtf8(s.c_str())); 106 | } 107 | 108 | void Lua4RSWidget::appendLog(const QString& s) 109 | { 110 | if(!_disableOutput) 111 | ui->tb_log->appendPlainText(QDateTime::currentDateTime().toString("dd.MM.yy hh:mm:ss") + QString(" > ") + s); 112 | } 113 | 114 | /* ############################################################# 115 | * # helper 116 | * ############################################################# 117 | */ 118 | 119 | LuaContainer* Lua4RSWidget::allScriptsGetLuaContainerFromSelectedRow() 120 | { 121 | // get corresponding LuaContainer 122 | QModelIndexList rows = ui->tw_allscripts->selectionModel()->selectedRows(); 123 | if(rows.count() != 1) 124 | return NULL; 125 | 126 | return allScriptsGetLuaContainerFromRow(rows[0].row()); 127 | } 128 | 129 | LuaContainer* Lua4RSWidget::allScriptsGetLuaContainerFromRow(const int row) 130 | { 131 | if(row < 0) 132 | return NULL; 133 | 134 | // get script name 135 | QTableWidgetItem* name = ui->tw_allscripts->item(row, 0); 136 | 137 | std::cout << "[Lua] Lua4RSWidget::allScriptsGetLuaContainerFromRow : trying to load LuaContaienr for " << name->text().toStdString() << " ..."; 138 | 139 | // get container by name 140 | LuaContainer* container; 141 | if(_lua->codeList()->itemByName(name->text(), container)) 142 | { 143 | std::cout << " got it!" << std::endl; 144 | return container; 145 | } 146 | // else 147 | std::cout << " failed!" << std::endl; 148 | return NULL; 149 | } 150 | 151 | void Lua4RSWidget::allScriptsAddRow(LuaContainer* container) 152 | { 153 | int rows = ui->tw_allscripts->rowCount(); 154 | ui->tw_allscripts->setRowCount(rows + 1); 155 | 156 | QTableWidgetItem* name = new QTableWidgetItem(); 157 | QTableWidgetItem* desc = new QTableWidgetItem(); 158 | QTableWidgetItem* lastRun = new QTableWidgetItem(); 159 | QTableWidgetItem* trigger = new QTableWidgetItem(); 160 | QTableWidgetItem* enabled = new QTableWidgetItem(); 161 | 162 | name->setText(container->getName()); 163 | desc->setText(container->getDesc()); 164 | lastRun->setText(container->getLastTriggered().toString()); 165 | trigger->setText("TODO"); 166 | enabled->setCheckState(container->getEnabled() ? Qt::Checked : Qt::Unchecked); 167 | 168 | ui->tw_allscripts->setItem(rows, 0, name); 169 | ui->tw_allscripts->setItem(rows, 1, desc); 170 | ui->tw_allscripts->setItem(rows, 2, lastRun); 171 | ui->tw_allscripts->setItem(rows, 3, trigger); 172 | ui->tw_allscripts->setItem(rows, 4, enabled); 173 | } 174 | 175 | // init the gui at startup and after a container switch before the ini is loaded 176 | void Lua4RSWidget::clearUi() 177 | { 178 | ui->cbx_enable->setChecked(false); 179 | ui->cbx_timeconstraint->setChecked(false); 180 | ui->tied_timefrom->setTime(QTime(0,0,0)); 181 | ui->tied_timeto->setTime(QTime(0,0,0)); 182 | 183 | ui->le_scriptname->clear(); 184 | ui->le_scriptdesc->clear(); 185 | ui->pte_luacode->clear(); 186 | 187 | ui->cb_every->setChecked(false); 188 | ui->cb_once->setChecked(false); 189 | ui->cb_startup->setChecked(false); 190 | ui->cb_shutdown->setChecked(false); 191 | 192 | ui->rb_runonevent->setChecked(false); 193 | ui->dd_events->setCurrentIndex(0); 194 | 195 | ui->spb_everycount->setValue(5); 196 | ui->dd_everyunits->setCurrentIndex(1); 197 | 198 | ui->dte_runonce->setDateTime(QDateTime::currentDateTime()); 199 | 200 | ui->cb_chatmessage->setChecked(false); 201 | } 202 | 203 | void Lua4RSWidget::luaContainerToUi(LuaContainer* container) 204 | { 205 | // clear ui and set needed fields/boxes 206 | clearUi(); 207 | 208 | // for settings things to default / resetting 209 | if(container == NULL) 210 | { 211 | ///TODO there might be better ways that this - good enough for the moment 212 | ui->pte_luacode->setEnabled(false); 213 | } else 214 | { 215 | // name, desc, code 216 | ui->le_scriptname->setText(container->getName()); 217 | ui->le_scriptdesc->setText(container->getDesc()); 218 | ui->pte_luacode->setPlainText(container->getCode()); 219 | 220 | ui->cbx_enable->setChecked(container->getEnabled()); 221 | ui->cbx_timeconstraint->setChecked(container->getConstraintEnabled()); 222 | 223 | QTime from, to; 224 | container->getConstraintFromTo(from, to); 225 | ui->tied_timefrom->setTime(from); 226 | ui->tied_timeto->setTime(to); 227 | 228 | // trigger 229 | uint amount, unit; 230 | if(container->getRunEveryChecked(amount, unit)) 231 | { 232 | ui->cb_every->setChecked(true); 233 | ui->spb_everycount->setValue(amount); 234 | ui->dd_everyunits->setCurrentIndex(unit); 235 | } 236 | QDateTime dt; 237 | if(container->getRunOnceChecked(dt)) 238 | { 239 | ui->cb_once->setChecked(true); 240 | ui->dte_runonce->setDateTime(dt); 241 | } 242 | if(container->getRunShutdownChecked()) 243 | ui->cb_shutdown->setChecked(true); 244 | if(container->getRunStartupChecked()) 245 | ui->cb_startup->setChecked(true); 246 | 247 | // event trigger 248 | if(container->getEventTriggerChecked(L4R_LOBBY_MESSAGERECEIVED)) 249 | ui->cb_chatmessage->setChecked(true); 250 | 251 | 252 | ///TODO rest 253 | 254 | ui->pte_luacode->setEnabled(true); 255 | } 256 | } 257 | 258 | bool Lua4RSWidget::uiToLuaContainer(LuaContainer* container) 259 | { 260 | if(!saneValues()) 261 | { 262 | std::cerr << "[Lua] Lua4RSWidget::uiToLuaContainer : wrong values detected - aborting" << std::endl; 263 | return false; 264 | } 265 | 266 | // name, desc, code 267 | container->setName(ui->le_scriptname->text()); 268 | container->setDesc(ui->le_scriptdesc->text()); 269 | container->setCode(ui->pte_luacode->toPlainText()); 270 | 271 | // enable, constraint 272 | container->setEnabled(ui->cbx_enable->isChecked()); 273 | container->setConstraintEnabled(ui->cbx_timeconstraint->isChecked()); 274 | 275 | QTime from, to; 276 | from = ui->tied_timefrom->time(); 277 | to = ui->tied_timeto->time(); 278 | container->setConstraintFromTo(from, to); 279 | 280 | // trigger 281 | container->removeAllTrigger(); 282 | 283 | // add trigger 284 | if(ui->cb_every->isChecked()) 285 | container->addRunEveryTrigger((uint)ui->spb_everycount->value(), (uint)ui->dd_everyunits->currentIndex()); 286 | if(ui->cb_once->isChecked()) 287 | container->addRunOnceTrigger(ui->dte_runonce->dateTime()); 288 | if(ui->cb_shutdown->isChecked()) 289 | container->addRunShutdownTrigger(); 290 | if(ui->cb_startup->isChecked()) 291 | container->addRunStratupTrigger(); 292 | 293 | // add event trigger (need to make this nice someday) 294 | if(ui->cb_chatmessage->isChecked()) 295 | container->addEventTrigger(L4R_LOBBY_MESSAGERECEIVED); 296 | 297 | ///TODO rest 298 | 299 | return true; 300 | } 301 | 302 | void Lua4RSWidget::switchContainer(LuaContainer* container) 303 | { 304 | // remember conatiner 305 | _activeContainer = container; 306 | 307 | // update UI 308 | // clearUi(); // no need for this since luaContainerToUi() calls clearUi() 309 | luaContainerToUi(_activeContainer); 310 | 311 | if(_activeContainer != NULL) 312 | std::cout << "[Lua] Lua4RSWidget::switchContainer : switched to " << _activeContainer->getName().toStdString() << std::endl; 313 | else 314 | std::cout << "[Lua] Lua4RSWidget::switchContainer : switched to NULL "<< std::endl; 315 | } 316 | 317 | void saneValuesHelper(const QString& msg, QString& allMsgs) 318 | { 319 | std::cerr << "[Lua] Lua4RSWidget::saneValues : " << msg.toStdString() << std::endl; 320 | allMsgs += "- " + msg + '\n'; 321 | } 322 | 323 | bool Lua4RSWidget::saneValues() 324 | { 325 | QString msg = tr("The following problem(s) was/were found:\n"); 326 | bool ret = true; 327 | if(ui->le_scriptname->text().isEmpty()) 328 | { 329 | saneValuesHelper(tr("script name is empty"), msg); 330 | ret = false; 331 | } 332 | 333 | if(ui->cb_once->isChecked() && ui->dte_runonce->dateTime() < QDateTime::currentDateTime()) 334 | { 335 | saneValuesHelper(tr("runOnce value lies in the past"), msg); 336 | ret = false; 337 | } 338 | 339 | if(ui->cbx_timeconstraint->isChecked() && ui->cb_once->isChecked() && (( // contraint enabled + run once 340 | ui->tied_timefrom->time() < ui->tied_timeto->time() && // from < to e.g. from 09:00 to 15:00 341 | (ui->dte_runonce->time() < ui->tied_timefrom->time() || ui->dte_runonce->time() > ui->tied_timeto->time()) // run once is outside of time window 342 | ) || ( 343 | ui->tied_timefrom->time() > ui->tied_timeto->time() && // from > to e.g. from 23:00 to 06:00 344 | (ui->dte_runonce->time() <= ui->tied_timefrom->time() && ui->dte_runonce->time() >= ui->tied_timeto->time()) // run once is outside of time window 345 | // !(ui->dte_runonce->time() > ui->tied_timefrom->time() || ui->dte_runonce->time() < ui->tied_timeto->time()) equivalent - maybe easier to understand 346 | ))) 347 | { 348 | saneValuesHelper(tr("runOnce value lies outside of constraint"), msg); 349 | ret = false; 350 | } 351 | 352 | if(ui->spb_everycount->value() < 0) 353 | { 354 | saneValuesHelper(tr("run every value is below 0"), msg); 355 | ret = false; 356 | } 357 | 358 | ///TODO check rest 359 | 360 | if(!ret) 361 | { 362 | // show errors to user 363 | QMessageBox mbox; 364 | mbox.setIcon(QMessageBox::Warning); 365 | mbox.setText(tr("Error(s) while checking")); 366 | mbox.setInformativeText(msg); 367 | mbox.setStandardButtons(QMessageBox::Ok); 368 | mbox.exec(); 369 | } 370 | 371 | return ret; 372 | } 373 | 374 | void Lua4RSWidget::newScript() 375 | { 376 | _activeContainer = _lua->codeList()->createItem(); 377 | // add new container to list 378 | _lua->codeList()->addItem(_activeContainer); 379 | 380 | // update all scripts 381 | setLuaCodes(_lua->codeList()); 382 | 383 | // update ui 384 | luaContainerToUi(_activeContainer); 385 | } 386 | 387 | bool Lua4RSWidget::saveScript(bool showErrorMsg) 388 | { 389 | if(_activeContainer == NULL) 390 | return true; 391 | 392 | // check for rename 393 | { 394 | QString oldName = _activeContainer->getName(); 395 | // get values from ui 396 | if(!uiToLuaContainer(_activeContainer)) 397 | return false; 398 | 399 | if(_activeContainer->getName() != oldName) 400 | { 401 | std::cout << "[Lua] Lua4RSWidget::on_pb_save_clicked() : renaming " << oldName.toStdString() << " to " << _activeContainer->getName().toStdString() << std::endl; 402 | _lua->codeList()->rename(oldName, _activeContainer->getName()); 403 | } 404 | } 405 | 406 | bool rc = _lua->codeList()->saveAll(); 407 | if(!rc && showErrorMsg) 408 | { 409 | QMessageBox mbox; 410 | mbox.setIcon(QMessageBox::Warning); 411 | mbox.setText(tr("Error")); 412 | mbox.setInformativeText(tr("an error occured while saving")); 413 | mbox.setStandardButtons( QMessageBox::Ok ); 414 | mbox.exec(); 415 | } 416 | return rc; 417 | } 418 | 419 | /* ############################################################# 420 | * # slots 421 | * ############################################################# 422 | */ 423 | 424 | // "Run" clicked : execute the script in the editor control 425 | void Lua4RSWidget::on_pb_run_clicked() 426 | { 427 | appendLog(QString("running: ") + ui->le_scriptname->text()); 428 | 429 | QString code = ui->pte_luacode->toPlainText(); 430 | 431 | _lua->runLuaByString(code); 432 | 433 | } 434 | 435 | // "New" clicked : create a new empty script 436 | void Lua4RSWidget::on_pb_newscript_clicked() 437 | { 438 | newScript(); 439 | } 440 | 441 | // "Edit" clicked : edit the script selected in AllMyScripts 442 | void Lua4RSWidget::on_pb_editscript_clicked() 443 | { 444 | // get corresponding LuaContainer 445 | LuaContainer* container = allScriptsGetLuaContainerFromSelectedRow(); 446 | 447 | if(container == NULL) 448 | { 449 | std::cerr << "[Lua] Lua4RSWidget::on_pb_editscript_clicked : got NULL" << std::endl; 450 | return; 451 | } 452 | switchContainer(container); 453 | } 454 | 455 | // "Delete" clicked : delete the script selected in AllMyScripts 456 | void Lua4RSWidget::on_pb_deletescript_clicked() 457 | { 458 | LuaContainer* container = allScriptsGetLuaContainerFromSelectedRow(); 459 | if(container == NULL) 460 | return; 461 | 462 | // update UI when necessary 463 | if(_activeContainer == container) 464 | { 465 | _activeContainer = NULL; 466 | luaContainerToUi(_activeContainer); 467 | } 468 | 469 | _lua->codeList()->removeItemAndDelete(container); 470 | // container is deleted now 471 | container = NULL; 472 | 473 | // update all scripts 474 | setLuaCodes(_lua->codeList()); 475 | } 476 | 477 | // "Load" clicked : load a scriptfile from disk into the editor control 478 | void Lua4RSWidget::on_pb_load_clicked() 479 | { 480 | QString name = ""; 481 | if(_activeContainer != NULL) 482 | { 483 | // a file was opened -> save its name 484 | name = _activeContainer->getName(); 485 | 486 | // ask for confirmation 487 | QMessageBox mbox; 488 | mbox.setIcon(QMessageBox::Information); 489 | mbox.setText(tr("Continue?")); 490 | mbox.setInformativeText(tr("You have a Lua script opened. Save it before closing it?")); 491 | mbox.setStandardButtons( QMessageBox::Save | QMessageBox::Discard | QMessageBox::Abort); 492 | 493 | int ret = mbox.exec(); 494 | if(ret == QMessageBox::Abort) 495 | return; 496 | 497 | if(ret == QMessageBox::Save) 498 | saveScript(); 499 | } 500 | 501 | LuaList* list = _lua->codeList(); 502 | 503 | list->loadAll(); 504 | // _activeContainer is invalid from now on! 505 | _activeContainer = NULL; 506 | 507 | setLuaCodes(list); 508 | 509 | if(name == "") 510 | // no file was opened - were are done 511 | return; 512 | 513 | LuaContainer* lc; 514 | if(list->itemByName(name, lc)) 515 | switchContainer(lc); 516 | else 517 | // couldn't find the file one was working one ... 518 | switchContainer(NULL); 519 | } 520 | 521 | // "Save" clicked : save the contents of the editor control to a file on disk 522 | void Lua4RSWidget::on_pb_save_clicked() 523 | { 524 | saveScript(); 525 | } 526 | 527 | // "Enabled Script" clicked : 528 | void Lua4RSWidget::on_cbx_enable_clicked(bool checked) 529 | { 530 | if(_activeContainer == NULL) 531 | return; 532 | 533 | _activeContainer->setEnabled(checked); 534 | 535 | // update all scripts 536 | LuaContainer* lc; 537 | for(int i = 0; i < ui->tw_allscripts->rowCount(); ++i) 538 | { 539 | lc = allScriptsGetLuaContainerFromRow(i); 540 | if(lc == _activeContainer) 541 | { 542 | QTableWidgetItem* enabled = ui->tw_allscripts->item(i, ALL_SCRIPTS_COLUMN_ENABLE); 543 | enabled->setCheckState(checked ? Qt::Checked : Qt::Unchecked); 544 | break; 545 | } 546 | } 547 | } 548 | 549 | 550 | //------------------------------------------------------------------------------ 551 | // Execution Constraint 552 | //------------------------------------------------------------------------------ 553 | 554 | // "...between" clicked : Constraint enabled/disabled has changed 555 | // note: think about disabling constraint from and to timeedits if unchecked 556 | void Lua4RSWidget::on_cbx_timeconstraint_clicked(bool checked) 557 | { 558 | if(_activeContainer == NULL) 559 | { 560 | std::cerr << "[Lua] Lua4RSWidget::on_cbx_timeconstraint_toggled : got no activeContainer" << std::endl; 561 | return; 562 | } 563 | _activeContainer->setConstraintEnabled(checked); 564 | } 565 | 566 | // from : Constraint "from"-time has changed 567 | // note: dont forget to check if from < to! 568 | void Lua4RSWidget::on_tied_timefrom_editingFinished() 569 | { 570 | } 571 | 572 | // to : Constraint "to"-time has changed 573 | // note: dont forget to check if from < to! 574 | void Lua4RSWidget::on_tied_timeto_editingFinished() 575 | { 576 | } 577 | 578 | //------------------------------------------------------------------------------ 579 | // All Scripts 580 | //------------------------------------------------------------------------------ 581 | 582 | void Lua4RSWidget::on_tw_allscripts_cellClicked(int row, int column) 583 | { 584 | if(column == ALL_SCRIPTS_COLUMN_ENABLE) // 4 = enabled 585 | { 586 | LuaContainer* container = allScriptsGetLuaContainerFromRow(row); 587 | QTableWidgetItem* cell = ui->tw_allscripts->item(row, column); 588 | 589 | container->setEnabled(cell->checkState() == Qt::Checked ? true : false); 590 | 591 | if(container == _activeContainer) 592 | // update ui->cbx_enable 593 | ui->cbx_enable->setChecked(_activeContainer->getEnabled()); 594 | } 595 | } 596 | 597 | void Lua4RSWidget::on_tw_allscripts_cellDoubleClicked(int row, int /*column*/) 598 | { 599 | if(row < 0) 600 | return; 601 | 602 | // save then load 603 | saveScript(); 604 | 605 | // get container 606 | LuaContainer* container = allScriptsGetLuaContainerFromRow(row); 607 | if(container == NULL) 608 | { 609 | std::cerr << "[Lua] Lua4RSWidget::on_tw_allscripts_doubleClicked : got NULL" << std::endl; 610 | return; 611 | } 612 | switchContainer(container); 613 | } 614 | 615 | //------------------------------------------------------------------------------ 616 | // Tabpage "By Timer" 617 | //------------------------------------------------------------------------------ 618 | void Lua4RSWidget::on_spb_everycount_editingFinished() 619 | { 620 | return; 621 | } 622 | 623 | 624 | // "Run Every" : amount of timer units has changed 625 | // note: if changed, rb_runevery should be selected 626 | void Lua4RSWidget::on_dd_everyunits_currentIndexChanged(int index) 627 | { 628 | uint TIME_UNITS[5] = {1, 60, 3600, 86400, 604800}; 629 | uint unit,amount,interval; 630 | 631 | amount = ui->spb_everycount->value(); 632 | unit = TIME_UNITS[index]; 633 | interval = amount * unit; 634 | 635 | ui->l_runeveryhelper->setText( QString::number(interval) + " secs" ); // just to see 636 | } 637 | 638 | // "RunEvery" : unit of timer units has changed 639 | // note: if changed, rb_runevery should be selected 640 | void Lua4RSWidget::on_spb_everycount_valueChanged(int arg1) 641 | { 642 | uint TIME_UNITS[5] = {1, 60, 3600, 86400, 604800}; 643 | uint unit,amount,interval; 644 | 645 | amount = arg1; 646 | unit = TIME_UNITS[ui->dd_everyunits->currentIndex()]; 647 | interval = amount * unit; 648 | 649 | ui->l_runeveryhelper->setText( QString::number(interval) + " secs" ); 650 | } 651 | 652 | // hack for color 653 | #define ACTIVE_COLOR "background:lightgreen;" 654 | 655 | // Run Every was selected 656 | void Lua4RSWidget::on_cb_every_toggled(bool checked) 657 | { 658 | ui->cb_every->setStyleSheet(checked ? ACTIVE_COLOR : "background:transparent;"); 659 | } 660 | 661 | // Run Once was selected 662 | void Lua4RSWidget::on_cb_once_toggled(bool checked) 663 | { 664 | ui->cb_once->setStyleSheet(checked ? ACTIVE_COLOR : "background:transparent;"); 665 | } 666 | 667 | // Run at startup was selected 668 | void Lua4RSWidget::on_cb_startup_toggled(bool checked) 669 | { 670 | ui->cb_startup->setStyleSheet(checked ? ACTIVE_COLOR : "background:transparent;"); 671 | } 672 | 673 | // Run at shutdown was selected 674 | void Lua4RSWidget::on_cb_shutdown_toggled(bool checked) 675 | { 676 | ui->cb_shutdown->setStyleSheet(checked ? ACTIVE_COLOR : "background:transparent;"); 677 | } 678 | #undef ACTIVE_COLOR 679 | 680 | //------------------------------------------------------------------------------ 681 | // Tabpage "By Event" 682 | //------------------------------------------------------------------------------ 683 | void Lua4RSWidget::on_rb_runonevent_toggled(bool /*checked*/) 684 | { 685 | } 686 | 687 | void Lua4RSWidget::on_dd_events_currentIndexChanged(int /*index*/) 688 | { 689 | } 690 | 691 | 692 | //------------------------------------------------------------------------------ 693 | // hints 694 | //------------------------------------------------------------------------------ 695 | void Lua4RSWidget::on_pb_pastehint_released() 696 | { 697 | QList items = ui->tw_hints->selectedItems(); 698 | if(items.empty() || items.size() != 1 || !ui->pte_luacode->isEnabled()) 699 | return; 700 | 701 | ui->pte_luacode->insertPlainText(items.at(0)->text(1)); 702 | } 703 | 704 | void Lua4RSWidget::on_tw_hints_itemDoubleClicked(QTreeWidgetItem *item, int /*column*/) 705 | { 706 | QString hint = item->text(1); 707 | 708 | // when you want to expant a namespace, you double click it --> don't append hint on a double click on a namespace 709 | if(hint.endsWith('.') || !ui->pte_luacode->isEnabled()) 710 | return; 711 | 712 | ui->pte_luacode->insertPlainText(hint); 713 | } 714 | -------------------------------------------------------------------------------- /lang/Lua4RS_de.ts: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Lua4RSConfig 6 | 7 | 8 | Form 9 | 10 | 11 | 12 | 13 | tick options 14 | Tickoptionen 15 | 16 | 17 | 18 | This lets you choose after how many seconds the startup event is triggered 19 | 20 | 21 | 22 | 23 | tick interval 24 | Tickintervall 25 | 26 | 27 | 28 | 29 | s 30 | Sekunde 31 | s 32 | 33 | 34 | 35 | this lets you choose after how many seconds a tick event is triggered 36 | 37 | 38 | 39 | 40 | startup event 41 | Startup Event 42 | 43 | 44 | 45 | NOBODY WILL HELP YOU :O 46 | 47 | 48 | 49 | 50 | Lua4RSWidget 51 | 52 | 53 | Form 54 | 55 | 56 | 57 | 58 | Lua4RS 59 | Lua4RS 60 | 61 | 62 | 63 | All My Scripts 64 | Meine Scripte 65 | 66 | 67 | 68 | by sehraf & far*call 69 | von sehraf & far*call 70 | 71 | 72 | 73 | 74 | Scriptname 75 | Scriptname 76 | 77 | 78 | 79 | 80 | Description 81 | Beschreibung 82 | 83 | 84 | 85 | Last Run 86 | Letzte Ausführung 87 | 88 | 89 | 90 | Trigger 91 | Auslöser 92 | 93 | 94 | 95 | Enabled 96 | Aktiv 97 | 98 | 99 | 100 | Edit the selected script in the Lua Code Editor 101 | 102 | 103 | 104 | 105 | Edit 106 | Bearbeiten 107 | 108 | 109 | 110 | Create a new empty script 111 | 112 | 113 | 114 | 115 | New 116 | Neu 117 | 118 | 119 | 120 | Delete the selected script 121 | 122 | 123 | 124 | 125 | Delete 126 | Löschen 127 | 128 | 129 | 130 | Load a Lua script from disk into the Lua Code Editor 131 | 132 | 133 | 134 | 135 | Load from disk 136 | Laden 137 | 138 | 139 | 140 | Activation 141 | Aktivierung 142 | 143 | 144 | 145 | Check/Uncheck to enable/disable the execution of the current script 146 | 147 | 148 | 149 | 150 | Enable script 151 | Script aktvieren 152 | 153 | 154 | 155 | Check/Uncheck to constrain script execution to certain hours of the day 156 | 157 | 158 | 159 | 160 | ... from 161 | ... von 162 | 163 | 164 | 165 | Start of execution interval 166 | 167 | 168 | 169 | 170 | 171 | HH:mm:ss 'h 172 | 173 | 174 | 175 | 176 | to 177 | bis 178 | 179 | 180 | 181 | End of execution interval 182 | 183 | 184 | 185 | 186 | The filename of this script in your script folder 187 | 188 | 189 | 190 | 191 | Enter a description of this script to remember what it is doing 192 | 193 | 194 | 195 | 196 | Code 197 | Code 198 | 199 | 200 | 201 | Run current Lua script. View script output in the Output Tab 202 | 203 | 204 | 205 | 206 | Run 207 | Ausführen 208 | 209 | 210 | 211 | Save current Lua script 212 | 213 | 214 | 215 | 216 | Save 217 | Speichern 218 | 219 | 220 | 221 | Undock the editor to a separate resizable window 222 | 223 | 224 | 225 | 226 | Undock 227 | 228 | 229 | 230 | 231 | RS Properties 232 | RS Funktionen 233 | 234 | 235 | 236 | Doubleclick on item to paste it into the Lua Code Editor 237 | 238 | 239 | 240 | 241 | Paste selected Item into the Lua editor at the current cursor position 242 | 243 | 244 | 245 | 246 | Paste 247 | Einfügen 248 | 249 | 250 | 251 | Output 252 | Ausgabe 253 | 254 | 255 | 256 | By Timer 257 | nach Zeit 258 | 259 | 260 | 261 | Control script execution by timer 262 | 263 | 264 | 265 | 266 | Run the current script on an interval basis 267 | 268 | 269 | 270 | 271 | Run every 272 | Ausführung alle 273 | 274 | 275 | 276 | Seconds 277 | Sekunden 278 | 279 | 280 | 281 | Minutes 282 | Minuten 283 | 284 | 285 | 286 | Hours 287 | Stunden 288 | 289 | 290 | 291 | Days 292 | Tage 293 | 294 | 295 | 296 | Weeks 297 | Wochen 298 | 299 | 300 | 301 | (300 secs) 302 | 303 | 304 | 305 | 306 | Run the current script once at the specified time 307 | 308 | 309 | 310 | 311 | Run once at 312 | Einmalig um 313 | 314 | 315 | 316 | d MMM yyyy, HH:mm:ss t 317 | 318 | 319 | 320 | 321 | Run the current script only once when RS starts 322 | 323 | 324 | 325 | 326 | Run at Startup 327 | Ausfurung beim Start 328 | 329 | 330 | 331 | Run at Shutdown 332 | Ausführung beim Beenden 333 | 334 | 335 | 336 | By Event 337 | nach Event 338 | 339 | 340 | 341 | Control script execution by events 342 | 343 | 344 | 345 | 346 | Run on Event 347 | 348 | 349 | 350 | 351 | Friend Comes Online (Friend, When) 352 | 353 | 354 | 355 | 356 | Friend Goes Offline (Friend, When) 357 | 358 | 359 | 360 | 361 | Download Started (Filename, Hash, Size, When) 362 | 363 | 364 | 365 | 366 | Download Finished (Filename, Hash, Size, When) 367 | 368 | 369 | 370 | 371 | Chat Message Received (Lobby, MsgText, Author, When) 372 | 373 | 374 | 375 | 376 | Chat Message Sent (Lobby, MsgText, When) 377 | 378 | 379 | 380 | 381 | Mail Received (From, Subject, Body, When) 382 | 383 | 384 | 385 | 386 | Mail Sent (To, Subject, Body, When) 387 | 388 | 389 | 390 | 391 | RetroShare Shutdown (When) 392 | 393 | 394 | 395 | 396 | User Entered Lobby (User, Lobby, List of Users, When) 397 | 398 | 399 | 400 | 401 | User Left Lobby (User, Lobby, List of Users, When) 402 | 403 | 404 | 405 | 406 | Log 407 | Log 408 | 409 | 410 | 411 | <h1><img width="32" src=":/images/64px_help.png">&nbsp;&nbsp;Lua4RS</h1> <p>With Lua4RS you get three things with one Plugin: </p> <ul> <li>You can write, save, load and run Lua programs within RetroShare.</li> <li>You can use Lua programs like macros (think of macros in LibreOffice) to control and automate many features of RetroShare. </li> <li>You can execute your Lua programs either by timer control (think of cron or at) or by certain RetroShare events (e.g. <i>a friend comes online</i> or <i>a chat message is received</i> and many more).</li> </ul> 412 | 413 | 414 | 415 | 416 | The following problem(s) was/were found: 417 | 418 | Die folgenden Probleme wurden gefunden: 419 | 420 | 421 | 422 | 423 | script name is empty 424 | der Scriptname ist leer 425 | 426 | 427 | 428 | runOnce value lies in the past 429 | die einmalige Ausführung liegt in der Vergangenheit 430 | 431 | 432 | 433 | runOnce value lies outside of constraint 434 | die einmalige Ausführung liegt nicht innerhalb des Zeitfensters 435 | 436 | 437 | 438 | run every value is below 0 439 | der "Ausführung alle"-Wert ist kleiner 0 440 | 441 | 442 | 443 | Error(s) while checking 444 | Fehler beim Überprüfen 445 | 446 | 447 | 448 | Error 449 | Fehler 450 | 451 | 452 | 453 | an error occured while saving 454 | Ein Fehler trat beim Speichern auf 455 | 456 | 457 | 458 | Continue? 459 | Fortfahren? 460 | 461 | 462 | 463 | You have a Lua script opened. Save it before closing it? 464 | Ein Lua Script ist geöffnet. Soll es vor dem Schließen gespeichert werden? 465 | 466 | 467 | 468 | QObject 469 | 470 | 471 | clears the output 472 | Löscht die Ausgabe 473 | 474 | 475 | 476 | prints to output 477 | schreibt etwas in die Ausgabe 478 | 479 | 480 | 481 | returns own SSL id 482 | 483 | 484 | 485 | 486 | returns list of online friends (SSL id) 487 | 488 | 489 | 490 | 491 | returns list of all friends (SSL id) 492 | 493 | 494 | 495 | 496 | returns number of all friends and online friends 497 | 498 | 499 | 500 | 501 | returns if a peer is a friend 502 | 503 | 504 | 505 | 506 | returns is a PGP key is accepted 507 | 508 | 509 | 510 | 511 | returns if a peer is online 512 | 513 | 514 | 515 | 516 | returns the PGP name for a given PGP id 517 | 518 | 519 | 520 | 521 | returns the name for a given SSL/PGP id 522 | 523 | 524 | 525 | 526 | returns peer details as a table for a given SSL id 527 | 528 | 529 | 530 | 531 | returns own PGP id 532 | 533 | 534 | 535 | 536 | 537 | returns the PGP id for a given SSL/PGP id 538 | 539 | 540 | 541 | 542 | creates a new group 543 | 544 | 545 | 546 | 547 | edits an existing group 548 | 549 | 550 | 551 | 552 | removes the group with the given groupd id 553 | 554 | 555 | 556 | 557 | returns group info for a given group id 558 | 559 | 560 | 561 | 562 | returns an array with all groups and their group infos 563 | 564 | 565 | 566 | 567 | returns the current operation mode as int and string 568 | 569 | 570 | 571 | 572 | sets the openration mode (takes int or string) 573 | 574 | 575 | 576 | 577 | sets max down-/upload bandwidth in kB 578 | 579 | 580 | 581 | 582 | gets max down-/upload bandwidth in kB 583 | 584 | 585 | 586 | 587 | gets current down-/upload bandwidth in kB 588 | 589 | 590 | 591 | 592 | triggered script: 593 | 594 | 595 | 596 | 597 | This plugin let you script RS with Lua. 598 | 599 | 600 | 601 | 602 | --------------------------------------------------------------------------------