This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
list.cc
76 lines (64 loc) · 1.81 KB
/
list.cc
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
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <algorithm>
#include "xchat.h"
#include "TomiTCP/http.h"
#include "TomiTCP/str.h"
using namespace net;
namespace xchat {
static bool stringcmp(const roominfo &a,
const roominfo &b)
{
if (a.registered != b.registered)
return a.registered;
return strcoll(a.name.c_str(), b.name.c_str()) < 0;
}
/**
* Get a list of available rooms.
* \param listout Output array.
* \param type Type of rooms to list, binary mask of ROOM_*.
*/
void XChat::list(listout_t &listout, int type)
{
TomiHTTP s;
string l;
int retries = servers.size();
retry:
try {
int ret = request_GET(s, SERVER_SCRIPTS, "scripts/rooms.php", PATH_PLAIN);
if (ret != 200)
throw runtime_error("Not HTTP 200 Ok while getting list");
} catch (runtime_error &e) {
if (retries--) {
lastsrv_broke();
goto retry;
} else
throw runtime_error(string(e.what()) + " - " + lastsrv_broke());
}
while (s.getline(l)) {
wstrip(l);
roominfo room;
room.rid = string(l, 0, l.find(' '));
l.erase(0, room.rid.length());
l.erase(0, l.find_first_not_of(' '));
int pos = l.find(' ');
room.registered = (string(l, 0, pos) == "1" ? true : false);
l.erase(0, pos);
l.erase(0, l.find_first_not_of(' '));
pos = l.find(' ');
room.count = atoi(string(l, 0, pos).c_str());
l.erase(0, pos);
l.erase(0, l.find_first_not_of(' '));
room.name = l;
if ((type & ROOM_TEMPORARY && room.registered == 0) ||
(type & ROOM_REGISTERED && room.registered == 1))
listout.push_back(room);
}
s.close();
sort(listout.begin(), listout.end(), stringcmp);
for (listout_t::iterator i = listout.begin(); i != listout.end(); i++)
i->name = recode_to_client(i->name);
}
}