summaryrefslogtreecommitdiff
path: root/FortuneAwayMsg/main.c
diff options
context:
space:
mode:
authormataes2007 <mataes2007@e753b5eb-9565-29b2-b5c5-2cc6f99dfbcb>2011-11-22 17:35:02 +0000
committermataes2007 <mataes2007@e753b5eb-9565-29b2-b5c5-2cc6f99dfbcb>2011-11-22 17:35:02 +0000
commitda1c34bde32e040a0a431ffb809c3b1e425dc558 (patch)
treeb3b54a0fa38677c63d3d719c346b7bd20282e93d /FortuneAwayMsg/main.c
parentb8647d2c45eca1d56334458ccd74d2acc996ae32 (diff)
added FortuneAwayMsg
git-svn-id: http://miranda-plugins.googlecode.com/svn/trunk@196 e753b5eb-9565-29b2-b5c5-2cc6f99dfbcb
Diffstat (limited to 'FortuneAwayMsg/main.c')
-rw-r--r--FortuneAwayMsg/main.c280
1 files changed, 280 insertions, 0 deletions
diff --git a/FortuneAwayMsg/main.c b/FortuneAwayMsg/main.c
new file mode 100644
index 0000000..77d2931
--- /dev/null
+++ b/FortuneAwayMsg/main.c
@@ -0,0 +1,280 @@
+/*
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+Description
+-----------
+This plugin for Miranda-IM doesn't add any functionality on itself. It
+provides basically three services for retrieving: a) a BSD Fortune message
+from a random file, b) a BSD Fortune message from a protocol-defined file,
+and c) a BSD Fortune message from a status-defined file. So, hopefully,
+the only thing plugin developers will have to do is define three variables
+like %fortunemsg%, %protofortunemsg% and %statusfortunemsg% and call the
+corresponding service in this plugin in order to retrieve the fortune msgs.
+
+Options
+-------
+Options page Options->Status->Fortune Messages.
+
+Thanks
+------
+- UnregistereD for help, ideas and support in variables plugin
+- Leecher for encouraging and providing with the necessary tools
+- Blabla for kindly sending me the sources of the original fortune plugin (from which I took completely the random file selection code)
+- Harven for discussion and support to this plugin
+- JDGordon/Targaff for helping with the Endianism thing
+- Amp from the forums who provided a patched version compatible with latest Variables
+- simpleaway, ieview, messagesound and skype plugins' authors for part of their code I use in the Options page
+- Miranda IM developers for this amazing program
+- all other people from Miranda community
+
+
+*/
+
+// System includes
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <windows.h>
+
+// Miranda Includes
+#include <newpluginapi.h>
+#include <m_database.h>
+#include <m_options.h>
+#include <m_protosvc.h>
+#include <m_system.h>
+#include <m_utils.h>
+#include "m_variables.h"
+#include "m_updater.h"
+
+// Fortune include
+#include "fortune.h"
+#include "m_fortunemsg.h"
+
+// Prototypes
+static int ModulesLoaded(WPARAM, LPARAM);
+static int GetFortune(WPARAM, LPARAM);
+static int GetProtoFortune(WPARAM, LPARAM);
+static int GetStatusFortune(WPARAM, LPARAM);
+static int FreeAllocatedMemory(WPARAM, LPARAM);
+static int CalledFromVariables(WPARAM, LPARAM);
+char *StatusModeToDbSetting(int, const char *);
+extern int InitOptions(WPARAM, LPARAM);
+
+
+// Program globals
+HINSTANCE hInst;
+PLUGINLINK *pluginLink;
+HANDLE hModulesLoaded, hInitOptions, hGetFortune, hGetProtoFortune, hGetStatusFortune, hFreeAllocatedMemory, hCalledFromVariables;
+
+PLUGININFOEX pluginInfo = {
+ sizeof(PLUGININFO),
+ "FortuneAwayMsg",
+ PLUGIN_MAKE_VERSION(0,0,0,10),
+ "Fortune Messages for the AwayMessages",
+ "tioduke",
+ "tioduke@yahoo.ca",
+ "© 2005-2008 TioDuke",
+ "http://addons.miranda-im.org/details.php?action=viewfile&id=1933",
+ 0,
+ 0,
+ {0x9c00ab77, 0x41ce, 0x4e93, {0x9a, 0x83, 0xf9, 0xa8, 0x20, 0x5c, 0xa9, 0x1b}} //{9c00ab77-41ce-4e93-9a83-f9a8205ca91b}
+};
+
+// DLL Stuff //
+
+__declspec(dllexport) PLUGININFO* MirandaPluginInfo(DWORD mirandaVersion)
+{
+ pluginInfo.cbSize = sizeof(PLUGININFO);
+ return (PLUGININFO*)&pluginInfo;
+}
+
+__declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD mirandaVersion)
+{
+ pluginInfo.cbSize = sizeof(PLUGININFOEX);
+ return &pluginInfo;
+}
+
+#define MIID_FORTUNEAWAYMSG {0x9c00ab77, 0x41ce, 0x4e93, {0x9a, 0x83, 0xf9, 0xa8, 0x20, 0x5c, 0xa9, 0x1b}}
+static const MUUID interfaces[] = {MIID_FORTUNEAWAYMSG, MIID_LAST};
+__declspec(dllexport) const MUUID* MirandaPluginInterfaces(void)
+{
+ return interfaces;
+}
+
+BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
+{
+ hInst=hinstDLL;
+ DisableThreadLibraryCalls(hInst);
+ return TRUE;
+}
+
+int __declspec(dllexport) Load(PLUGINLINK *link)
+{
+ pluginLink=link;
+
+ hModulesLoaded = HookEvent(ME_SYSTEM_MODULESLOADED, ModulesLoaded);
+ return 0;
+}
+
+int __declspec(dllexport) Unload(void)
+{
+ UnhookEvent(hModulesLoaded);
+ UnhookEvent(hInitOptions);
+ DestroyServiceFunction(hGetFortune);
+ DestroyServiceFunction(hGetProtoFortune);
+ DestroyServiceFunction(hGetStatusFortune);
+ DestroyServiceFunction(hFreeAllocatedMemory);
+ DestroyServiceFunction(hCalledFromVariables);
+ return 0;
+}
+
+// Hooked events
+
+static int ModulesLoaded(WPARAM wParam, LPARAM lParam)
+{
+ hInitOptions = HookEvent(ME_OPT_INITIALISE, InitOptions);
+ hGetFortune = CreateServiceFunction(MS_FORTUNEMSG_GETMESSAGE, GetFortune);
+ hGetProtoFortune = CreateServiceFunction(MS_FORTUNEMSG_GETPROTOMSG, GetProtoFortune);
+ hGetStatusFortune = CreateServiceFunction(MS_FORTUNEMSG_GETSTATUSMSG, GetStatusFortune);
+ hFreeAllocatedMemory = CreateServiceFunction(MS_FORTUNEMSG_FREEMEMORY, FreeAllocatedMemory);
+ hCalledFromVariables = CreateServiceFunction(MS_FORTUNEMSG_FROMVARIABLES, CalledFromVariables);
+
+ if (ServiceExists(MS_VARS_REGISTERTOKEN)) {
+ TOKENREGISTER vr;
+ ZeroMemory(&vr, sizeof(vr));
+ vr.cbSize = sizeof(TOKENREGISTER);
+ vr.szTokenString = "fortunemsg";
+ vr.szService = MS_FORTUNEMSG_FROMVARIABLES;
+ vr.szCleanupService = MS_FORTUNEMSG_FREEMEMORY;
+ vr.szHelpText = "retrieves a random fortune string";
+ vr.memType = TR_MEM_OWNER;
+ vr.flags = TRF_CLEANUP;
+ CallService(MS_VARS_REGISTERTOKEN, 0, (LPARAM)&vr);
+ }
+
+ if (ServiceExists("DBEditorpp/RegisterSingleModule"))
+ CallService("DBEditorpp/RegisterSingleModule", (WPARAM)MODULE_NAME, 0);
+ if (ServiceExists(MS_UPDATE_REGISTERFL))
+ CallService(MS_UPDATE_REGISTERFL, (WPARAM)1933, (LPARAM)&pluginInfo);
+
+
+ return 0;
+}
+
+// Services
+
+static int GetFortune(WPARAM wParam, LPARAM lParam)
+{
+ char datFilename[MAX_PATH+1], fortuneMsg[FORTUNE_BUFSIZE];
+ char *returnMsg = (char *)lParam;
+
+ if (selectRandomFile(datFilename))
+ DBWriteContactSettingString(NULL, MODULE_NAME, "RandomFile", datFilename);
+
+ if (!readFortune("RandomFile", fortuneMsg, MAX_FORTUNEMSG)) return 0;
+
+ if (!returnMsg)
+ if (!(returnMsg = malloc(strlen(fortuneMsg)+1)))
+ return 0;
+ strcpy(returnMsg, fortuneMsg);
+
+ return (int)returnMsg;
+}
+
+static int GetProtoFortune(WPARAM wParam, LPARAM lParam)
+{
+ unsigned int maxFortuneMsg;
+ char dbSetting[MAX_PATH+1], fortuneMsg[FORTUNE_BUFSIZE];
+ char *protoName = (char *)wParam;
+ char *returnMsg = (char *)lParam;
+
+ if (!protoName) //Harven asked me for this, as sometimes SimpleAway doesn't know which protocol
+ return CallService(MS_FORTUNEMSG_GETMESSAGE, 0, 0);
+
+ _snprintf(dbSetting, MAX_PATH, "%sLength", protoName);
+ maxFortuneMsg = (unsigned int) DBGetContactSettingWord(NULL, MODULE_NAME, dbSetting, MAX_FORTUNEMSG);
+ if (maxFortuneMsg > MAX_FORTUNEMSG) maxFortuneMsg = MAX_FORTUNEMSG;
+
+ _snprintf(dbSetting, MAX_PATH, "%sFile", protoName);
+
+ if (!readFortune(dbSetting, fortuneMsg, maxFortuneMsg)) return 0;
+
+ if (!returnMsg)
+ if (!(returnMsg = malloc(strlen(fortuneMsg)+1)))
+ return 0;
+ strcpy(returnMsg, fortuneMsg);
+
+ return (int)returnMsg;
+}
+
+static int GetStatusFortune(WPARAM wParam, LPARAM lParam)
+{
+ char fortuneMsg[FORTUNE_BUFSIZE];
+ int status = (int)wParam;
+ char *returnMsg = (char *)lParam;
+
+ if (status < ID_STATUS_OFFLINE || status > ID_STATUS_OUTTOLUNCH) return 0;
+
+ if (!readFortune(StatusModeToDbSetting(status, "File"), fortuneMsg, MAX_FORTUNEMSG)) return 0;
+
+ if (!returnMsg)
+ if (!(returnMsg = malloc(strlen(fortuneMsg)+1)))
+ return 0;
+ strcpy(returnMsg, fortuneMsg);
+
+ return (int)returnMsg;
+}
+
+static int FreeAllocatedMemory(WPARAM wParam, LPARAM lParam)
+{
+ void *lPtr = (void *)lParam;
+
+ if (!lPtr)
+ return -1;
+
+ free(lPtr);
+ return 0;
+}
+
+static int CalledFromVariables(WPARAM wParam, LPARAM lParam)
+{
+ return CallService(MS_FORTUNEMSG_GETMESSAGE, 0, 0);
+}
+
+
+// helpers from awaymsg.c
+char *StatusModeToDbSetting(int status, const char *suffix)
+{
+ char *prefix;
+ static char str[MAX_PATH+1];
+
+ switch(status) {
+ case ID_STATUS_OFFLINE: prefix="Off"; break;
+ case ID_STATUS_ONLINE: prefix="On"; break;
+ case ID_STATUS_AWAY: prefix="Away"; break;
+ case ID_STATUS_NA: prefix="Na"; break;
+ case ID_STATUS_OCCUPIED: prefix="Occupied"; break;
+ case ID_STATUS_DND: prefix="Dnd"; break;
+ case ID_STATUS_FREECHAT: prefix="FreeChat"; break;
+ case ID_STATUS_INVISIBLE: prefix="Inv"; break;
+ case ID_STATUS_ONTHEPHONE: prefix="Otp"; break;
+ case ID_STATUS_OUTTOLUNCH: prefix="Otl"; break;
+ default: return NULL;
+ }
+ strcpy(str, prefix); strcat(str, suffix);
+ return str;
+}