summaryrefslogtreecommitdiff
path: root/random_message_changer/main.cpp
blob: d07c891af11d412f185f6a795f4a8a788501bfcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//---------------------------------------------------------------------------
#include "common.h"
#include "options.h"

PLUGINLINK *pluginLink;
HINSTANCE hInst;

PLUGININFO pluginInfo={
	sizeof(PLUGININFO),
	"Random Message Changer",
	PLUGIN_MAKE_VERSION(0,0,2,2),
	"Loads a random Status-Message from a file.",
	"Christian Weihs & Scott Ellis",
	"mail@scottellis.com.au",
	"2003 Christian Weihs, 2006 Scott Ellis",
	"http://www.scottellis.com.au",
	0,
	0
};

//---------------------------------------------------------------------------
extern "C" BOOL APIENTRY DllMain(HMODULE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
	hInst=hinstDLL;
	return TRUE;
}

//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) PLUGININFO* MirandaPluginInfo(DWORD mirandaVersion) {
	return &pluginInfo;
}

UINT TimerId;

void ChangeAllProtoMessages(int statusMode, char *msg) {
	int protoCount,i;
	PROTOCOLDESCRIPTOR **proto;

	CallService(MS_PROTO_ENUMPROTOCOLS,(WPARAM)&protoCount,(LPARAM)&proto);
	for(i=0;i<protoCount;i++)
		CallProtoService(proto[i]->szName,PS_SETAWAYMSG,statusMode,(LPARAM)msg);
}

// sucht eine neue Message heraus.
char *GetRandomLine(char *file, char *buff, int bufflen) {
	if (file == 0 || file[0] == 0 || file[0] == ':') return 0;

	FILE *inFile = fopen(file, "rt");
	if (!inFile){
		KillTimer(NULL, TimerId);
		MessageBox(NULL, Translate("Can't open file!"), Translate("RMC Error"), MB_OK | MB_ICONERROR);
		return 0;
	}
	
	char *line[1000];
	char zeile[2048];
	int i=0;
	while( i < 1000 && !feof(inFile)) {
		fgets(zeile, sizeof(zeile), inFile);
		
		unsigned int x=0;
		bool test=false;
		
		while(x < sizeof(zeile) && zeile[x]!=0) {
			if(zeile[x] == '#' || zeile[x] == '\r' || zeile[x] == '\n') {
				zeile[x] = 0;
				x = sizeof(zeile);
			} else {
				if(zeile[x]!= ' ') test=true;

				if (zeile[x] == '\\' && zeile[x+1] == 'n') {
					zeile[x+1]= '\n';
					zeile[x]= '\r';
				}
			}
			x++;
		}

		if(test) line[i++]=strdup(zeile);
	}
	fclose(inFile);

	if(i != 0) {
		int num = rand()%i;
		strncpy(buff,line[num],bufflen);
		for(int x=0;x<i;x++)
			free(line[x]);
		return buff;
	}
	
	return 0;
}

void CALLBACK TimerCall(HWND hwnd, UINT message, UINT idEvent, DWORD dwTime) {
	char *file;
	char *mode;
	char *mode2;
	bool enabled;
	int mirandaStatus=CallService(MS_CLIST_GETSTATUSMODE,0,0);
	
	switch(mirandaStatus) {
		case ID_STATUS_AWAY:
			file = options.file_away;
			enabled = options.cAway;
			mode="AwayDefault";
			mode2="AwayMsg";
			break;
		case ID_STATUS_NA:
			file = options.file_na;
			enabled = options.cNa;
			mode="NaDefault";
			mode2="NaMsg";
			break;
		case ID_STATUS_OCCUPIED:
			file = options.file_occ;
			enabled = options.cOcc;
			mode="OccupiedDefault";
			mode2="OccupiedMsg";
			break;
		case ID_STATUS_DND:
			file = options.file_dnd;
			enabled = options.cDnd;
			mode="DndDefault";
			mode2="DndMsg";
			break;
		case ID_STATUS_FREECHAT:
			file = options.file_ffc;
			enabled = options.cFfc;
			mode="FreeChatDefault";
			mode2="FreeChatMsg";
			break;
		default:
			enabled=false;
	}

	if (enabled) {
		char msg[2048];
		GetRandomLine(file, msg, sizeof(msg));
		if (msg!=0) {
			ChangeAllProtoMessages(mirandaStatus, msg);
			DBWriteContactSettingString(NULL, "SRAway", mode, msg);
			DBWriteContactSettingString(NULL, "SRAway", mode2, msg);
		}
	}
}

//setzt den Timer neu
void SetChangingTime() {
	if(TimerId) KillTimer(NULL, TimerId);
	if(options.cTime) TimerId = SetTimer(0, 0, (UINT)(options.iTimeOut*1000*60), TimerCall);
}

// Was tun, wenn der Status sich ändert?
int StatusChanged(WPARAM wParam,LPARAM lParam) {
	SetChangingTime();
	if(options.cStatus) TimerCall(0, 0, 0, 0);	
	return 0;
}

//---------------------------------------------------------------------------
HANDLE hEventStsChange = 0;
extern "C" int __declspec(dllexport) Load(PLUGINLINK *link) {
	pluginLink=link;

	// Events verbinden
	hEventStsChange = HookEvent(ME_CLIST_STATUSMODECHANGE,StatusChanged);

	srand((unsigned int)time(0));
	//Die Werte aus der Datenbank laden
	LoadOptions();

	// test crash for attache
	//int *i = 0;
	//*i = 0;
	
	return 0;
}

//---------------------------------------------------------------------------
extern "C" int __declspec(dllexport) Unload() {
	UnhookEvent(hEventStsChange);
	KillTimer(NULL, TimerId);
	return 0;
}
//---------------------------------------------------------------------------