summaryrefslogtreecommitdiff
path: root/miranda-wine/src/modules/autoaway/autoaway.c
blob: 5cb10ce2d4042fcb04acb37f59769dd44de1df6a (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
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2006 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

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.
*/
#include "commonheaders.h"

#define AA_MODULE "AutoAway"

static void AutoAwaySetProtocol(const char * proto, unsigned status)
{
	char * awayMsg = (char *) CallService(MS_AWAYMSG_GETSTATUSMSG, (WPARAM) status, 0);								
	CallProtoService(proto, PS_SETSTATUS, status, 0);
	if ( awayMsg != NULL )  {
		if (CallProtoService(proto,PS_GETCAPS,PFLAGNUM_1,0)&PF1_MODEMSGSEND)
		CallProtoService(proto, PS_SETAWAYMSG, status, (LPARAM) awayMsg);
		miranda_sys_free(awayMsg);
	}
}

static int AutoAwayEvent(WPARAM wParam, LPARAM lParam)
{
	PROTOCOLDESCRIPTOR **proto=0;
	int protoCount=0;
	int j;
	MIRANDA_IDLE_INFO mii;
	int status;

	mii.cbSize = sizeof(mii);
	CallService(MS_IDLE_GETIDLEINFO, 0, (LPARAM)&mii);
	if (mii.aaStatus==0) return 0;
	CallService(MS_PROTO_ENUMPROTOCOLS, (WPARAM)&protoCount, (LPARAM)&proto);
	for (j=0; j<protoCount; j++) {
		if ( proto[j]->type == PROTOTYPE_PROTOCOL )  {
			int statusbits = CallProtoService(proto[j]->szName, PS_GETCAPS, PFLAGNUM_2, 0);
			int currentstatus = CallProtoService(proto[j]->szName, PS_GETSTATUS, 0, 0);
			status = mii.aaStatus;
			if ( !(statusbits & Proto_Status2Flag(status)) ) {
				// the protocol doesnt support the given status
				if ( statusbits & Proto_Status2Flag(ID_STATUS_AWAY) ) status=ID_STATUS_AWAY;
				else {
					// the proto doesnt support user mode or even away, bail.
					continue;
				}
			}
			if ( currentstatus >= ID_STATUS_ONLINE && currentstatus != ID_STATUS_INVISIBLE ) {			
				if ( (lParam&IDF_ISIDLE) && ( currentstatus == ID_STATUS_ONLINE || currentstatus == ID_STATUS_FREECHAT ))  {
					DBWriteContactSettingByte(NULL,AA_MODULE,proto[j]->szName,1);
					AutoAwaySetProtocol(proto[j]->szName, status);
				} else if ( !(lParam&IDF_ISIDLE) && DBGetContactSettingByte(NULL,AA_MODULE,proto[j]->szName,0) ) {
					// returning from idle and this proto was set away, set it back
					DBWriteContactSettingByte(NULL,AA_MODULE,proto[j]->szName,0);
					if ( !mii.aaLock ) AutoAwaySetProtocol(proto[j]->szName, ID_STATUS_ONLINE);
	}	}	}	}

	return 0;
}

int LoadAutoAwayModule(void)
{
	HookEvent(ME_IDLE_CHANGED, AutoAwayEvent);
	return 0;
}