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
|
#include "headers.h"
#define MS_FAVCONTACTS_OPEN_CONTACT "FavContacts/OpenContact"
class CHttpProcessor: public IConnectionProcessor
{
private:
CSocket *m_socket;
char *FetchURL(char *s)
{
char *p;
if (p = strstr(s, "\r\n")) *p = 0;
if (p = strrchr(s, ' ')) *p = 0;
if (p = strchr(s, ' ')) while (*p && *p == ' ') p++;
return mir_strdup(p);
}
public:
CHttpProcessor(CSocket *s): m_socket(s) {}
void ProcessConnection()
{
char buf[1024];
int n = m_socket->Recv(buf, sizeof(buf));
buf[n] = 0;
char *s = FetchURL(buf);
if (!strncmp(s, "/fav/list/", 10))
{
SendList();
} else
if (!strncmp(s, "/fav/open/", 10))
{
OpenContact(s);
}
mir_free(s);
m_socket->Close();
}
void OpenContact(char *s)
{
m_socket->Send("HTTP 200 OK\r\n\r\n");
int hContact;
sscanf(s, "/fav/open/%d", &hContact);
if (CallService(MS_DB_CONTACT_IS, hContact, 0))
CallServiceSync(MS_FAVCONTACTS_OPEN_CONTACT, (WPARAM)hContact, 0);
}
void SendList()
{
TFavContacts favList;
favList.build();
m_socket->Send(
"HTTP 200 OK\r\n"
"Content-Type: text/javascript\r\n"
"\r\n");
Send("try {\r\n");
Send("SetContactCount(");
Send(favList.getCount());
Send(");\r\n");
for (int i = 0; i < favList.getCount(); ++i)
{
HANDLE hContact = favList[i]->getHandle();
TCHAR *name = (TCHAR *)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR);
AVATARCACHEENTRY *avatar = (AVATARCACHEENTRY *)CallService(MS_AV_GETAVATARBITMAP, (WPARAM)hContact, 0);
int status = db_get_w(hContact, GetContactProto(hContact), "Status", ID_STATUS_OFFLINE);
Send("SetContact(");
Send(i);
Send(", ");
Send((int)hContact);
Send(", '");
SendQuoted(name);
Send("', ");
Send(status);
Send(", '");
SendQuoted(avatar ? avatar->szFilename : _T(""));
Send("');\r\n");
}
Send("} catch(e) {}\r\n");
}
void Send(char *s)
{
m_socket->Send(s);
}
void Send(WCHAR *ws)
{
char *s = mir_utf8encodeW(ws);
m_socket->Send(s);
mir_free(s);
}
void Send(int i)
{
char buf[32];
mir_snprintf(buf, SIZEOF(buf), "%d", i);
Send(buf);
}
template<class XCHAR>
void SendQuoted(const XCHAR *s)
{
int length = 0;
const XCHAR *p;
for (p = s; *p; p++)
{
if (*p == '\'' || *p == '\\' || *p == '\"')
length++;
length++;
}
XCHAR *buf = (XCHAR *)mir_alloc(sizeof(XCHAR) * (length + 1));
XCHAR *q = buf;
for (p = s; *p; p++)
{
if (*p == '\'' || *p == '\\' || *p == '\"')
{
*q = '\\';
q++;
}
*q = *p;
q++;
}
*q = 0;
Send(buf);
mir_free(buf);
}
};
class CHttpProcessorFactory: public IConnectionProcessorFactory
{
public:
IConnectionProcessor *Create(CSocket *s)
{
return new CHttpProcessor(s);
}
};
static CHttpProcessorFactory g_httpProcessorFactory;
static CServer g_httpServer;
void LoadHttpApi()
{
g_httpServer.Start(60888, &g_httpProcessorFactory, true);
}
void UnloadHttpApi()
{
g_httpServer.Stop();
}
|