aboutsummaryrefslogtreecommitdiffstats
path: root/protocols/skype/skype.c
blob: 6515a801d64514aab7ab004fe01c495335758ea4 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
require 'tzinfo/timezone_definition'

module TZInfo
  module Definitions
    module Atlantic
      module South_Georgia
        include TimezoneDefinition
        
        timezone 'Atlantic/South_Georgia' do |tz|
          tz.offset :o0, -8768, 0, :LMT
          tz.offset :o1, -7200, 0, :GST
          
          tz.transition 1890, 1, :o1, 1627673806, 675
        end
      end
    end
  end
end
697' href='#n697'>697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
/*
 *  skype.c - Skype plugin for BitlBee
 *
 *  Copyright (c) 2007, 2008, 2009, 2010, 2011, 2012 by Miklos Vajna <vmiklos@frugalware.org>
 *
 *  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.
 */

#define _XOPEN_SOURCE
#define _BSD_SOURCE
#include <poll.h>
#include <stdio.h>
#include <bitlbee.h>
#include <ssl_client.h>

#define SKYPE_DEFAULT_SERVER "localhost"
#define SKYPE_DEFAULT_PORT "2727"
#define IRC_LINE_SIZE 1024
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))

/*
 * Enumerations
 */

enum {
	SKYPE_CALL_RINGING = 1,
	SKYPE_CALL_MISSED,
	SKYPE_CALL_CANCELLED,
	SKYPE_CALL_FINISHED,
	SKYPE_CALL_REFUSED
};

enum {
	SKYPE_FILETRANSFER_NEW = 1,
	SKYPE_FILETRANSFER_FAILED
};

/*
 * Structures
 */

struct skype_data {
	struct im_connection *ic;
	char *username;
	/* The effective file descriptor. We store it here so any function can
	 * write() to it. */
	int fd;
	/* File descriptor returned by bitlbee. we store it so we know when
	 * we're connected and when we aren't. */
	int bfd;
	/* ssl_getfd() uses this to get the file desciptor. */
	void *ssl;
	/* When we receive a new message id, we query the properties, finally
	 * the chatname. Store the properties here so that we can use
	 * imcb_buddy_msg() when we got the chatname. */
	char *handle;
	/* List, because of multiline messages. */
	GList *body;
	char *type;
	/* This is necessary because we send a notification when we get the
	 * handle. So we store the state here and then we can send a
	 * notification about the handle is in a given status. */
	int call_status;
	char *call_id;
	char *call_duration;
	/* If the call is outgoing or not */
	int call_out;
	/* Same for file transfers. */
	int filetransfer_status;
	/* Using /j #nick we want to have a groupchat with two people. Usually
	 * not (default). */
	char *groupchat_with;
	/* The user who invited us to the chat. */
	char *adder;
	/* If we are waiting for a confirmation about we changed the topic. */
	int topic_wait;
	/* These are used by the info command. */
	char *info_fullname;
	char *info_phonehome;
	char *info_phoneoffice;
	char *info_phonemobile;
	char *info_nrbuddies;
	char *info_tz;
	char *info_seen;
	char *info_birthday;
	char *info_sex;
	char *info_language;
	char *info_country;
	char *info_province;
	char *info_city;
	char *info_homepage;
	char *info_about;
	/* When a call fails, we get the reason and later we get the failure
	 * event, so store the failure code here till then */
	int failurereason;
	/* If this is just an update of an already received message. */
	int is_edit;
	/* List of struct skype_group* */
	GList *groups;
	/* Pending user which has to be added to the next group which is
	 * created. */
	char *pending_user;
};

struct skype_away_state {
	char *code;
	char *full_name;
};

struct skype_buddy_ask_data {
	struct im_connection *ic;
	/* This is also used for call IDs for simplicity */
	char *handle;
};

struct skype_group {
	int id;
	char *name;
	GList *users;
};

/*
 * Tables
 */

const struct skype_away_state skype_away_state_list[] = {
	{ "AWAY", "Away" },
	{ "NA", "Not available" },
	{ "DND", "Do Not Disturb" },
	{ "INVISIBLE", "Invisible" },
	{ "OFFLINE", "Offline" },
	{ "SKYPEME", "Skype Me" },
	{ "ONLINE", "Online" },
	{ NULL, NULL}
};

/*
 * Functions
 */

int skype_write(struct im_connection *ic, char *buf, int len)
{
	struct skype_data *sd = ic->proto_data;
	struct pollfd pfd[1];

	if (!sd->ssl)
		return FALSE;

	pfd[0].fd = sd->fd;
	pfd[0].events = POLLOUT;

	/* This poll is necessary or we'll get a SIGPIPE when we write() to
	 * sd->fd. */
	poll(pfd, 1, 1000);
	if (pfd[0].revents & POLLHUP) {
		imc_logout(ic, TRUE);
		return FALSE;
	}
	ssl_write(sd->ssl, buf, len);

	return TRUE;
}

int skype_printf(struct im_connection *ic, char *fmt, ...)
{
	va_list args;
	char str[IRC_LINE_SIZE];

	va_start(args, fmt);
	vsnprintf(str, IRC_LINE_SIZE, fmt, args);
	va_end(args);

	return skype_write(ic, str, strlen(str));
}

static void skype_buddy_ask_yes(void *data)
{
	struct skype_buddy_ask_data *bla = data;
	skype_printf(bla->ic, "SET USER %s ISAUTHORIZED TRUE",
		bla->handle);
	g_free(bla->handle);
	g_free(bla);
}

static void skype_buddy_ask_no(void *data)
{
	struct skype_buddy_ask_data *bla = data;
	skype_printf(bla->ic, "SET USER %s ISAUTHORIZED FALSE",
		bla->handle);
	g_free(bla->handle);
	g_free(bla);
}

void skype_buddy_ask(struct im_connection *ic, char *handle, char *message)
{
	struct skype_buddy_ask_data *bla = g_new0(struct skype_buddy_ask_data,
		1);
	char *buf;

	bla->ic = ic;
	bla->handle = g_strdup(handle);

	buf = g_strdup_printf("The user %s wants to add you to "
		"his/her buddy list, saying: '%s'.", handle, message);
	imcb_ask(ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no);
	g_free(buf);
}

static void skype_call_ask_yes(void *data)
{
	struct skype_buddy_ask_data *bla = data;
	skype_printf(bla->ic, "SET CALL %s STATUS INPROGRESS",
		bla->handle);
	g_free(bla->handle);
	g_free(bla);
}

static void skype_call_ask_no(void *data)
{
	struct skype_buddy_ask_data *bla = data;
	skype_printf(bla->ic, "SET CALL %s STATUS FINISHED",
		bla->handle);
	g_free(bla->handle);
	g_free(bla);
}

void skype_call_ask(struct im_connection *ic, char *call_id, char *message)
{
	struct skype_buddy_ask_data *bla = g_new0(struct skype_buddy_ask_data,
		1);

	bla->ic = ic;
	bla->handle = g_strdup(call_id);

	imcb_ask(ic, message, bla, skype_call_ask_yes, skype_call_ask_no);
}

static char *skype_call_strerror(int err)
{
	switch (err) {
	case 1:
		return "Miscellaneous error";
	case 2:
		return "User or phone number does not exist.";
	case 3:
		return "User is offline";
	case 4:
		return "No proxy found";
	case 5:
		return "Session terminated.";
	case 6:
		return "No common codec found.";
	case 7:
		return "Sound I/O error.";
	case 8:
		return "Problem with remote sound device.";
	case 9:
		return "Call blocked by recipient.";
	case 10:
		return "Recipient not a friend.";
	case 11:
		return "Current user not authorized by recipient.";
	case 12:
		return "Sound recording error.";
	default:
		return "Unknown error";
	}
}

static char *skype_group_by_username(struct im_connection *ic, char *username)
{
	struct skype_data *sd = ic->proto_data;
	int i, j;

	/* NEEDSWORK: we just search for the first group of the user, multiple
	 * groups / user is not yet supported by BitlBee. */

	for (i = 0; i < g_list_length(sd->groups); i++) {
		struct skype_group *sg = g_list_nth_data(sd->groups, i);
		for (j = 0; j < g_list_length(sg->users); j++) {
			if (!strcmp(g_list_nth_data(sg->users, j), username))
				return sg->name;
		}
	}
	return NULL;
}

static struct skype_group *skype_group_by_name(struct im_connection *ic, char *name)
{
	struct skype_data *sd = ic->proto_data;
	int i;

	for (i = 0; i < g_list_length(sd->groups); i++) {
		struct skype_group *sg = g_list_nth_data(sd->groups, i);
		if (!strcmp(sg->name, name))
			return sg;
	}
	return NULL;
}

static void skype_parse_users(struct im_connection *ic, char *line)
{
	char **i, **nicks;

	nicks = g_strsplit(line + 6, ", ", 0);
	for (i = nicks; *i; i++)
		skype_printf(ic, "GET USER %s ONLINESTATUS\n", *i);
	g_strfreev(nicks);
}

static void skype_parse_user(struct im_connection *ic, char *line)
{
	int flags = 0;
	char *ptr;
	struct skype_data *sd = ic->proto_data;
	char *user = strchr(line, ' ');
	char *status = strrchr(line, ' ');

	status++;
	ptr = strchr(++user, ' ');
	if (!ptr)
		return;
	*ptr = '\0';
	ptr++;
	if (!strncmp(ptr, "ONLINESTATUS ", 13)) {
			if (!strcmp(user, sd->username))
				return;
			if (!set_getbool(&ic->acc->set, "test_join")
				&& !strcmp(user, "echo123"))
				return;
		ptr = g_strdup_printf("%s@skype.com", user);
		imcb_add_buddy(ic, ptr, skype_group_by_username(ic, user));
		if (strcmp(status, "OFFLINE") && (strcmp(status, "SKYPEOUT") ||
			!set_getbool(&ic->acc->set, "skypeout_offline")))
			flags |= OPT_LOGGED_IN;
		if (strcmp(status, "ONLINE") && strcmp(status, "SKYPEME"))
			flags |= OPT_AWAY;
		imcb_buddy_status(ic, ptr, flags, NULL, NULL);
		g_free(ptr);
	} else if (!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20)) {
		char *message = ptr + 20;
		if (strlen(message))
			skype_buddy_ask(ic, user, message);
	} else if (!strncmp(ptr, "BUDDYSTATUS ", 12)) {
		char *st = ptr + 12;
		if (!strcmp(st, "3")) {
			char *buf = g_strdup_printf("%s@skype.com", user);
			imcb_add_buddy(ic, buf, skype_group_by_username(ic, user));
			g_free(buf);
		}
	} else if (!strncmp(ptr, "MOOD_TEXT ", 10)) {
		char *buf = g_strdup_printf("%s@skype.com", user);
		bee_user_t *bu = bee_user_by_handle(ic->bee, ic, buf);
		g_free(buf);
		buf = ptr + 10;
		if (bu)
			imcb_buddy_status(ic, bu->handle, bu->flags, NULL,
					*buf ? buf : NULL);
		if (set_getbool(&ic->acc->set, "show_moods"))
			imcb_log(ic, "User `%s' changed mood text to `%s'", user, buf);
	} else if (!strncmp(ptr, "FULLNAME ", 9))
		sd->info_fullname = g_strdup(ptr + 9);
	else if (!strncmp(ptr, "PHONE_HOME ", 11))
		sd->info_phonehome = g_strdup(ptr + 11);
	else if (!strncmp(ptr, "PHONE_OFFICE ", 13))
		sd->info_phoneoffice = g_strdup(ptr + 13);
	else if (!strncmp(ptr, "PHONE_MOBILE ", 13))
		sd->info_phonemobile = g_strdup(ptr + 13);
	else if (!strncmp(ptr, "NROF_AUTHED_BUDDIES ", 20))
		sd->info_nrbuddies = g_strdup(ptr + 20);
	else if (!strncmp(ptr, "TIMEZONE ", 9))
		sd->info_tz = g_strdup(ptr + 9);
	else if (!strncmp(ptr, "LASTONLINETIMESTAMP ", 20))
		sd->info_seen = g_strdup(ptr + 20);
	else if (!strncmp(ptr, "SEX ", 4))
		sd->info_sex = g_strdup(ptr + 4);
	else if (!strncmp(ptr, "LANGUAGE ", 9))
		sd->info_language = g_strdup(ptr + 9);
	else if (!strncmp(ptr, "COUNTRY ", 8))
		sd->info_country = g_strdup(ptr + 8);
	else if (!strncmp(ptr, "PROVINCE ", 9))
		sd->info_province = g_strdup(ptr + 9);
	else if (!strncmp(ptr, "CITY ", 5))
		sd->info_city = g_strdup(ptr + 5);
	else if (!strncmp(ptr, "HOMEPAGE ", 9))
		sd->info_homepage = g_strdup(ptr + 9);
	else if (!strncmp(ptr, "ABOUT ", 6)) {
		/* Support multiple about lines. */
		if (!sd->info_about)
			sd->info_about = g_strdup(ptr + 6);
		else {
			GString *st = g_string_new(sd->info_about);
			g_string_append_printf(st, "\n%s", ptr + 6);
			g_free(sd->info_about);
			sd->info_about = g_strdup(st->str);
			g_string_free(st, TRUE);
		}
	}
	else if (!strncmp(ptr, "BIRTHDAY ", 9)) {
		sd->info_birthday = g_strdup(ptr + 9);

		GString *st = g_string_new("Contact Information\n");
		g_string_append_printf(st, "Skype Name: %s\n", user);
		if (sd->info_fullname) {
			if (strlen(sd->info_fullname))
				g_string_append_printf(st, "Full Name: %s\n",
					sd->info_fullname);
			g_free(sd->info_fullname);
			sd->info_fullname = NULL;
		}
		if (sd->info_phonehome) {
			if (strlen(sd->info_phonehome))
				g_string_append_printf(st, "Home Phone: %s\n",
					sd->info_phonehome);
			g_free(sd->info_phonehome);
			sd->info_phonehome = NULL;
		}
		if (sd->info_phoneoffice) {
			if (strlen(sd->info_phoneoffice))
				g_string_append_printf(st, "Office Phone: %s\n",
					sd->info_phoneoffice);
			g_free(sd->info_phoneoffice);
			sd->info_phoneoffice = NULL;
		}
		if (sd->info_phonemobile) {
			if (strlen(sd->info_phonemobile))
				g_string_append_printf(st, "Mobile Phone: %s\n",
					sd->info_phonemobile);
			g_free(sd->info_phonemobile);
			sd->info_phonemobile = NULL;
		}
		g_string_append_printf(st, "Personal Information\n");
		if (sd->info_nrbuddies) {
			if (strlen(sd->info_nrbuddies))
				g_string_append_printf(st,
					"Contacts: %s\n", sd->info_nrbuddies);
			g_free(sd->info_nrbuddies);
			sd->info_nrbuddies = NULL;
		}
		if (sd->info_tz) {
			if (strlen(sd->info_tz)) {
				char ib[256];
				time_t t = time(NULL);
				t += atoi(sd->info_tz)-(60*60*24);
				struct tm *gt = gmtime(&t);
				strftime(ib, 256, "%H:%M:%S", gt);
				g_string_append_printf(st,
					"Local Time: %s\n", ib);
			}
			g_free(sd->info_tz);
			sd->info_tz = NULL;
		}
		if (sd->info_seen) {
			if (strlen(sd->info_seen)) {
				char ib[256];
				time_t it = atoi(sd->info_seen);
				struct tm *tm = localtime(&it);
				strftime(ib, 256, ("%Y. %m. %d. %H:%M"), tm);
				g_string_append_printf(st,
					"Last Seen: %s\n", ib);
			}
			g_free(sd->info_seen);
			sd->info_seen = NULL;
		}
		if (sd->info_birthday) {
			if (strlen(sd->info_birthday) &&
				strcmp(sd->info_birthday, "0")) {
				char ib[256];
				struct tm tm;
				strptime(sd->info_birthday, "%Y%m%d", &tm);
				strftime(ib, 256, "%B %d, %Y", &tm);
				g_string_append_printf(st,
					"Birthday: %s\n", ib);

				strftime(ib, 256, "%Y", &tm);
				int year = atoi(ib);
				time_t t = time(NULL);
				struct tm *lt = localtime(&t);
				g_string_append_printf(st,
					"Age: %d\n", lt->tm_year+1900-year);
			}
			g_free(sd->info_birthday);
			sd->info_birthday = NULL;
		}
		if (sd->info_sex) {
			if (strlen(sd->info_sex)) {
				char *iptr = sd->info_sex;
				while (*iptr++)
					*iptr = tolower(*iptr);
				g_string_append_printf(st,
					"Gender: %s\n", sd->info_sex);
			}
			g_free(sd->info_sex);
			sd->info_sex = NULL;
		}
		if (sd->info_language) {
			if (strlen(sd->info_language)) {
				char *iptr = strchr(sd->info_language, ' ');
				if (iptr)
					iptr++;
				else
					iptr = sd->info_language;
				g_string_append_printf(st,
					"Language: %s\n", iptr);
			}
			g_free(sd->info_language);
			sd->info_language = NULL;
		}
		if (sd->info_country) {
			if (strlen(sd->info_country)) {
				char *iptr = strchr(sd->info_country, ' ');
				if (iptr)
					iptr++;
				else
					iptr = sd->info_country;
				g_string_append_printf(st,
					"Country: %s\n", iptr);
			}
			g_free(sd->info_country);
			sd->info_country = NULL;
		}
		if (sd->info_province) {
			if (strlen(sd->info_province))
				g_string_append_printf(st,
					"Region: %s\n", sd->info_province);
			g_free(sd->info_province);
			sd->info_province = NULL;
		}
		if (sd->info_city) {
			if (strlen(sd->info_city))
				g_string_append_printf(st,
					"City: %s\n", sd->info_city);
			g_free(sd->info_city);
			sd->info_city = NULL;
		}
		if (sd->info_homepage) {
			if (strlen(sd->info_homepage))
				g_string_append_printf(st,
					"Homepage: %s\n", sd->info_homepage);
			g_free(sd->info_homepage);
			sd->info_homepage = NULL;
		}
		if (sd->info_about) {
			if (strlen(sd->info_about))
				g_string_append_printf(st, "%s\n",
					sd->info_about);
			g_free(sd->info_about);
			sd->info_about = NULL;
		}
		imcb_log(ic, "%s", st->str);
		g_string_free(st, TRUE);
	}
}

static void skype_parse_chatmessage(struct im_connection *ic, char *line)
{
	struct skype_data *sd = ic->proto_data;
	char buf[IRC_LINE_SIZE];
	char *id = strchr(line, ' ');

	if (!++id)
		return;
	char *info = strchr(id, ' ');

	if (!info)
		return;
	*info = '\0';
	info++;
	if (!strcmp(info, "STATUS RECEIVED") || !strncmp(info, "EDITED_TIMESTAMP", 16)) {
		/* New message ID:
		 * (1) Request its from field
		 * (2) Request its body
		 * (3) Request its type
		 * (4) Query chatname
		 */
		skype_printf(ic, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
		if (!strcmp(info, "STATUS RECEIVED"))
			skype_printf(ic, "GET CHATMESSAGE %s BODY\n", id);
		else
			sd->is_edit = 1;
		skype_printf(ic, "GET CHATMESSAGE %s TYPE\n", id);
		skype_printf(ic, "GET CHATMESSAGE %s CHATNAME\n", id);
	} else if (!strncmp(info, "FROM_HANDLE ", 12)) {
		info += 12;
		/* New from field value. Store
		 * it, then we can later use it
		 * when we got the message's
		 * body. */
		g_free(sd->handle);
		sd->handle = g_strdup_printf("%s@skype.com", info);
	} else if (!strncmp(info, "EDITED_BY ", 10)) {
		info += 10;
		/* This is the same as
		 * FROM_HANDLE, except that we
		 * never request these lines
		 * from Skype, we just get
		 * them. */
		g_free(sd->handle);
		sd->handle = g_strdup_printf("%s@skype.com", info);
	} else if (!strncmp(info, "BODY ", 5)) {
		info += 5;
		sd->body = g_list_append(sd->body, g_strdup(info));
	}	else if (!strncmp(info, "TYPE ", 5)) {
		info += 5;
		g_free(sd->type);
		sd->type = g_strdup(info);
	} else if (!strncmp(info, "CHATNAME ", 9)) {
		info += 9;
		if (sd->handle && sd->body && sd->type) {
			struct groupchat *gc = bee_chat_by_title(ic->bee, ic, info);
			int i;
			for (i = 0; i < g_list_length(sd->body); i++) {
				char *body = g_list_nth_data(sd->body, i);
				if (!strcmp(sd->type, "SAID") ||
					!strcmp(sd->type, "EMOTED")) {
					if (!strcmp(sd->type, "SAID")) {
						if (!sd->is_edit)
							g_snprintf(buf, IRC_LINE_SIZE, "%s",
								body);
						else {
							g_snprintf(buf, IRC_LINE_SIZE, "%s %s",
								set_getstr(&ic->acc->set, "edit_prefix"),
								body);
							sd->is_edit = 0;
						}
					} else
						g_snprintf(buf, IRC_LINE_SIZE, "/me %s",
							body);
					if (!gc)
						/* Private message */
						imcb_buddy_msg(ic,
							sd->handle, buf, 0, 0);
					else
						/* Groupchat message */
						imcb_chat_msg(gc,
							sd->handle, buf, 0, 0);
				} else if (!strcmp(sd->type, "SETTOPIC") && gc)
					imcb_chat_topic(gc,
						sd->handle, body, 0);
				else if (!strcmp(sd->type, "LEFT") && gc)
					imcb_chat_remove_buddy(gc,
						sd->handle, NULL);
			}
			g_list_free(sd->body);
			sd->body = NULL;
		}
	}
}

static void skype_parse_call(struct im_connection *ic, char *line)
{
	struct skype_data *sd = ic->proto_data;
	char *id = strchr(line, ' ');
	char buf[IRC_LINE_SIZE];

	if (!++id)
		return;
	char *info = strchr(id, ' ');

	if (!info)
		return;
	*info = '\0';
	info++;
	if (!strncmp(info, "FAILUREREASON ", 14))
		sd->failurereason = atoi(strchr(info, ' '));
	else if (!strcmp(info, "STATUS RINGING")) {
		if (sd->call_id)
			g_free(sd->call_id);
		sd->call_id = g_strdup(id);
		skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
		sd->call_status = SKYPE_CALL_RINGING;
	} else if (!strcmp(info, "STATUS MISSED")) {
		skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
		sd->call_status = SKYPE_CALL_MISSED;
	} else if (!strcmp(info, "STATUS CANCELLED")) {
		skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
		sd->call_status = SKYPE_CALL_CANCELLED;
	} else if (!strcmp(info, "STATUS FINISHED")) {
		skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
		sd->call_status = SKYPE_CALL_FINISHED;
	} else if (!strcmp(info, "STATUS REFUSED")) {
		skype_printf(ic, "GET CALL %s PARTNER_HANDLE\n", id);
		sd->call_status = SKYPE_CALL_REFUSED;
	} else if (!strcmp(info, "STATUS UNPLACED")) {
		if (sd->call_id)
			g_free(sd->call_id);
		/* Save the ID for later usage (Cancel/Finish). */
		sd->call_id = g_strdup(id);
		sd->call_out = TRUE;
	} else if (!strcmp(info, "STATUS FAILED")) {
		imcb_error(ic, "Call failed: %s",
			skype_call_strerror(sd->failurereason));
		sd->call_id = NULL;
	} else if (!strncmp(info, "DURATION ", 9)) {
		if (sd->call_duration)
			g_free(sd->call_duration);
		sd->call_duration = g_strdup(info+9);
	} else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
		info += 15;
		if (!sd->call_status)
			return;
		switch (sd->call_status) {
		case SKYPE_CALL_RINGING:
			if (sd->call_out)
				imcb_log(ic, "You are currently ringing "
					"the user %s.", info);
			else {
				g_snprintf(buf, IRC_LINE_SIZE,
					"The user %s is currently ringing you.",
					info);
				skype_call_ask(ic, sd->call_id, buf);
			}
			break;
		case SKYPE_CALL_MISSED:
			imcb_log(ic, "You have missed a call from user %s.",
				info);
			break;
		case SKYPE_CALL_CANCELLED:
			imcb_log(ic, "You cancelled the call to the user %s.",
				info);
			sd->call_status = 0;
			sd->call_out = FALSE;
			break;
		case SKYPE_CALL_REFUSED:
			if (sd->call_out)
				imcb_log(ic, "The user %s refused the call.",
					info);
			else
				imcb_log(ic,
					"You refused the call from user %s.",
					info);
			sd->call_out = FALSE;
			break;
		case SKYPE_CALL_FINISHED:
			if (sd->call_duration)
				imcb_log(ic,
					"You finished the call to the user %s "
					"(duration: %s seconds).",
					info, sd->call_duration);
			else
				imcb_log(ic,
					"You finished the call to the user %s.",
					info);
			sd->call_out = FALSE;
			break;
		default:
			/* Don't be noisy, ignore other statuses for now. */
			break;
		}
		sd->call_status = 0;
	}
}

static void skype_parse_filetransfer(struct im_connection *ic, char *line)
{
	struct skype_data *sd = ic->proto_data;
	char *id = strchr(line, ' ');

	if (!++id)
		return;
	char *info = strchr(id, ' ');

	if (!info)
		return;
	*info = '\0';
	info++;
	if (!strcmp(info, "STATUS NEW")) {
		skype_printf(ic, "GET FILETRANSFER %s PARTNER_HANDLE\n",
			id);
		sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
	} else if (!strcmp(info, "STATUS FAILED")) {
		skype_printf(ic, "GET FILETRANSFER %s PARTNER_HANDLE\n",
			id);
		sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
	} else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
		info += 15;
		if (!sd->filetransfer_status)
			return;
		switch (sd->filetransfer_status) {
		case SKYPE_FILETRANSFER_NEW:
			imcb_log(ic, "The user %s offered a new file for you.",
				info);
			break;
		case SKYPE_FILETRANSFER_FAILED:
			imcb_log(ic, "Failed to transfer file from user %s.",
				info);
			break;
		}
		sd->filetransfer_status = 0;
	}
}

static struct skype_group *skype_group_by_id(struct im_connection *ic, int id)
{
	struct skype_data *sd = ic->proto_data;
	int i;

	for (i = 0; i < g_list_length(sd->groups); i++) {
		struct skype_group *sg = (struct skype_group *)g_list_nth_data(sd->groups, i);

		if (sg->id == id)
			return sg;
	}
	return NULL;
}

static void skype_group_free(struct skype_group *sg, gboolean usersonly)
{
	int i;

	for (i = 0; i < g_list_length(sg->users); i++) {
		char *user = g_list_nth_data(sg->users, i);
		g_free(user);
	}
	sg->users = NULL;
	if (usersonly)
		return;
	g_free(sg->name);
	g_free(sg);
}

/* Update the group of each user in this group */
static void skype_group_users(struct im_connection *ic, struct skype_group *sg)
{
	int i;

	for (i = 0; i < g_list_length(sg->users); i++) {
		char *user = g_list_nth_data(sg->users, i);
		char *buf = g_strdup_printf("%s@skype.com", user);
		imcb_add_buddy(ic, buf, sg->name);
		g_free(buf);
	}
}

static void skype_parse_group(struct im_connection *ic, char *line)
{
	struct skype_data *sd = ic->proto_data;
	char *id = strchr(line, ' ');

	if (!++id)
		return;

	char *info = strchr(id, ' ');

	if (!info)
		return;
	*info = '\0';
	info++;

	if (!strncmp(info, "DISPLAYNAME ", 12)) {
		info += 12;

		/* Name given for a group ID: try to update it or insert a new
		 * one if not found */
		struct skype_group *sg = skype_group_by_id(ic, atoi(id));
		if (sg) {
			g_free(sg->name);
			sg->name = g_strdup(info);
		} else {
			sg = g_new0(struct skype_group, 1);
			sg->id = atoi(id);
			sg->name = g_strdup(info);
			sd->groups = g_list_append(sd->groups, sg);
		}
	} else if (!strncmp(info, "USERS ", 6)) {
		struct skype_group *sg = skype_group_by_id(ic, atoi(id));

		if (sg) {
			char **i;
			char **users = g_strsplit(info + 6, ", ", 0);

			skype_group_free(sg, TRUE);
			i = users;
			while (*i) {
				sg->users = g_list_append(sg->users, g_strdup(*i));
				i++;
			}
			g_strfreev(users);
			skype_group_users(ic, sg);
		} else
			log_message(LOGLVL_ERROR,
				"No skype group with id %s. That's probably a bug.", id);
	} else if (!strncmp(info, "NROFUSERS ", 10)) {
		if (!sd->pending_user) {
			/* Number of users changed in this group, query its type to see
			 * if it's a custom one we should care about. */
			skype_printf(ic, "GET GROUP %s TYPE", id);
			return;
		}

		/* This is a newly created group, we have a single user
		 * to add. */
		struct skype_group *sg = skype_group_by_id(ic, atoi(id));

		if (sg) {
			skype_printf(ic, "ALTER GROUP %d ADDUSER %s", sg->id, sd->pending_user);
			g_free(sd->pending_user);
			sd->pending_user = NULL;
		} else
			log_message(LOGLVL_ERROR,
					"No skype group with id %s. That's probably a bug.", id);
	} else if (!strcmp(info, "TYPE CUSTOM_GROUP"))
		/* This one is interesting, query its users. */
		skype_printf(ic, "GET GROUP %s USERS", id);
}

static void skype_parse_chat(struct im_connection *ic, char *line)
{
	struct skype_data *sd = ic->proto_data;
	char buf[IRC_LINE_SIZE];
	char *id = strchr(line, ' ');

	if (!++id)
		return;
	struct groupchat *gc;
	char *info = strchr(id, ' ');

	if (!info)
		return;
	*info = '\0';
	info++;
	/* Remove fake chat if we created one in skype_chat_with() */
	gc = bee_chat_by_title(ic->bee, ic, "");
	if (gc)
		imcb_chat_free(gc);
	if (!strcmp(info, "STATUS MULTI_SUBSCRIBED")) {
		gc = bee_chat_by_title(ic->bee, ic, id);
		if (!gc) {
			gc = imcb_chat_new(ic, id);
			imcb_chat_name_hint(gc, id);
		}
		skype_printf(ic, "GET CHAT %s ADDER\n", id);
		skype_printf(ic, "GET CHAT %s TOPIC\n", id);
	} else if (!strcmp(info, "STATUS DIALOG") && sd->groupchat_with) {
		gc = imcb_chat_new(ic, id);
		imcb_chat_name_hint(gc, id);
		/* According to the docs this
		 * is necessary. However it
		 * does not seem the situation
		 * and it would open an extra
		 * window on our client, so
		 * just leave it out. */
		/*skype_printf(ic, "OPEN CHAT %s\n", id);*/
		g_snprintf(buf, IRC_LINE_SIZE, "%s@skype.com",
				sd->groupchat_with);
		imcb_chat_add_buddy(gc, buf);
		imcb_chat_add_buddy(gc, sd->username);
		g_free(sd->groupchat_with);
		sd->groupchat_with = NULL;
		skype_printf(ic, "GET CHAT %s ADDER\n", id);
		skype_printf(ic, "GET CHAT %s TOPIC\n", id);
	} else if (!strcmp(info, "STATUS UNSUBSCRIBED")) {
		gc = bee_chat_by_title(ic->bee, ic, id);
		if (gc)
			gc->data = (void *)FALSE;
	} else if (!strncmp(info, "ADDER ", 6)) {
		info += 6;
		g_free(sd->adder);
		sd->adder = g_strdup_printf("%s@skype.com", info);
	} else if (!strncmp(info, "TOPIC ", 6)) {
		info += 6;
		gc = bee_chat_by_title(ic->bee, ic, id);
		if (gc && (sd->adder || sd->topic_wait)) {
			if (sd->topic_wait) {
				sd->adder = g_strdup(sd->username);
				sd->topic_wait = 0;
			}
			imcb_chat_topic(gc, sd->adder, info, 0);
			g_free(sd->adder);
			sd->adder = NULL;
		}
	} else if (!strncmp(info, "ACTIVEMEMBERS ", 14)) {
		info += 14;
		gc = bee_chat_by_title(ic->bee, ic, id);
		/* Hack! We set ->data to TRUE
		 * while we're on the channel
		 * so that we won't rejoin
		 * after a /part. */
		if (!gc || gc->data)
			return;
		char **members = g_strsplit(info, " ", 0);
		int i;
		for (i = 0; members[i]; i++) {
			if (!strcmp(members[i], sd->username))
				continue;
			g_snprintf(buf, IRC_LINE_SIZE, "%s@skype.com",
					members[i]);
			if (!g_list_find_custom(gc->in_room, buf,
				(GCompareFunc)strcmp))
				imcb_chat_add_buddy(gc, buf);
		}
		imcb_chat_add_buddy(gc, sd->username);
		g_strfreev(members);
	}
}

static void skype_parse_password(struct im_connection *ic, char *line)
{
	if (!strncmp(line+9, "OK", 2))
		imcb_connected(ic);
	else {
		imcb_error(ic, "Authentication Failed");
		imc_logout(ic, TRUE);
	}
}

static void skype_parse_profile(struct im_connection *ic, char *line)
{
	imcb_log(ic, "SkypeOut balance value is '%s'.", line+21);
}

static void skype_parse_ping(struct im_connection *ic, char *line)
{
	/* Unused parameter */
	line = line;
	skype_printf(ic, "PONG\n");
}

static void skype_parse_chats(struct im_connection *ic, char *line)
{
	char **i;
	char **chats = g_strsplit(line + 6, ", ", 0);

	i = chats;
	while (*i) {
		skype_printf(ic, "GET CHAT %s STATUS\n", *i);
		skype_printf(ic, "GET CHAT %s ACTIVEMEMBERS\n", *i);
		i++;
	}
	g_strfreev(chats);
}

static void skype_parse_groups(struct im_connection *ic, char *line)
{
	char **i;
	char **groups = g_strsplit(line + 7, ", ", 0);

	i = groups;
	while (*i) {
		skype_printf(ic, "GET GROUP %s DISPLAYNAME\n", *i);
		skype_printf(ic, "GET GROUP %s USERS\n", *i);
		i++;
	}
	g_strfreev(groups);
}

static void skype_parse_alter_group(struct im_connection *ic, char *line)
{
	char *id = line + strlen("ALTER GROUP");

	if (!++id)
		return;

	char *info = strchr(id, ' ');

	if (!info)
		return;
	*info = '\0';
	info++;

	if (!strncmp(info, "ADDUSER ", 8)) {
		struct skype_group *sg = skype_group_by_id(ic, atoi(id));

		info += 8;
		if (sg) {
			char *buf = g_strdup_printf("%s@skype.com", info);
			sg->users = g_list_append(sg->users, g_strdup(info));
			imcb_add_buddy(ic, buf, sg->name);
			g_free(buf);
		} else
			log_message(LOGLVL_ERROR,
				"No skype group with id %s. That's probably a bug.", id);
	}
}

typedef void (*skype_parser)(struct im_connection *ic, char *line);

static gboolean skype_read_callback(gpointer data, gint fd,
				    b_input_condition cond)
{
	struct im_connection *ic = data;
	struct skype_data *sd = ic->proto_data;
	char buf[IRC_LINE_SIZE];
	int st, i;
	char **lines, **lineptr, *line;
	static struct parse_map {
		char *k;
		skype_parser v;
	} parsers[] = {
		{ "USERS ", skype_parse_users },
		{ "USER ", skype_parse_user },
		{ "CHATMESSAGE ", skype_parse_chatmessage },
		{ "CALL ", skype_parse_call },
		{ "FILETRANSFER ", skype_parse_filetransfer },
		{ "CHAT ", skype_parse_chat },
		{ "GROUP ", skype_parse_group },
		{ "PASSWORD ", skype_parse_password },
		{ "PROFILE PSTN_BALANCE ", skype_parse_profile },
		{ "PING", skype_parse_ping },
		{ "CHATS ", skype_parse_chats },
		{ "GROUPS ", skype_parse_groups },
		{ "ALTER GROUP ", skype_parse_alter_group },
	};

	/* Unused parameters */
	fd = fd;
	cond = cond;

	if (!sd || sd->fd == -1)
		return FALSE;
	/* Read the whole data. */
	st = ssl_read(sd->ssl, buf, sizeof(buf));
	if (st > 0) {
		buf[st] = '\0';
		/* Then split it up to lines. */
		lines = g_strsplit(buf, "\n", 0);
		lineptr = lines;
		while ((line = *lineptr)) {
			if (!strlen(line))
				break;
			if (set_getbool(&ic->acc->set, "skypeconsole_receive"))
				imcb_buddy_msg(ic, "skypeconsole", line, 0, 0);
			for (i = 0; i < ARRAY_SIZE(parsers); i++)
				if (!strncmp(line, parsers[i].k,
					strlen(parsers[i].k))) {
					parsers[i].v(ic, line);
					break;
				}
			lineptr++;
		}
		g_strfreev(lines);
	} else if (st == 0 || (st < 0 && !sockerr_again())) {
		ssl_disconnect(sd->ssl);
		sd->fd = -1;
		sd->ssl = NULL;

		imcb_error(ic, "Error while reading from server");
		imc_logout(ic, TRUE);
		return FALSE;
	}
	return TRUE;
}

gboolean skype_start_stream(struct im_connection *ic)
{
	struct skype_data *sd = ic->proto_data;
	int st;

	if (!sd)
		return FALSE;

	if (sd->bfd <= 0)
		sd->bfd = b_input_add(sd->fd, B_EV_IO_READ,
			skype_read_callback, ic);

	/* Log in */
	skype_printf(ic, "USERNAME %s\n", ic->acc->user);
	skype_printf(ic, "PASSWORD %s\n", ic->acc->pass);

	/* This will download all buddies and groups. */
	st = skype_printf(ic, "SEARCH GROUPS CUSTOM\n");
	skype_printf(ic, "SEARCH FRIENDS\n");

	skype_printf(ic, "SET USERSTATUS ONLINE\n");

	/* Auto join to bookmarked chats if requested.*/
	if (set_getbool(&ic->acc->set, "auto_join"))
		skype_printf(ic, "SEARCH BOOKMARKEDCHATS\n");
	return st;
}

gboolean skype_connected(gpointer data, int returncode, void *source, b_input_condition cond)
{
	struct im_connection *ic = data;
	struct skype_data *sd = ic->proto_data;

	/* Unused parameter */
	cond = cond;

	if (!source) {
		sd->ssl = NULL;
		imcb_error(ic, "Could not connect to server");
		imc_logout(ic, TRUE);
		return FALSE;
	}
	imcb_log(ic, "Connected to server, logging in");

	return skype_start_stream(ic);
}

static void skype_login(account_t *acc)
{
	struct im_connection *ic = imcb_new(acc);
	struct skype_data *sd = g_new0(struct skype_data, 1);

	ic->proto_data = sd;

	imcb_log(ic, "Connecting");
	sd->ssl = ssl_connect(set_getstr(&acc->set, "server"),
		set_getint(&acc->set, "port"), FALSE, skype_connected, ic);
	sd->fd = sd->ssl ? ssl_getfd(sd->ssl) : -1;
	sd->username = g_strdup(acc->user);

	sd->ic = ic;

	if (set_getbool(&acc->set, "skypeconsole"))
		imcb_add_buddy(ic, "skypeconsole", NULL);
}

static void skype_logout(struct im_connection *ic)
{
	struct skype_data *sd = ic->proto_data;
	int i;

	skype_printf(ic, "SET USERSTATUS OFFLINE\n");

	while( ic->groupchats )
		imcb_chat_free(ic->groupchats->data);

	for (i = 0; i < g_list_length(sd->groups); i++) {
		struct skype_group *sg = (struct skype_group *)g_list_nth_data(sd->groups, i);
		skype_group_free(sg, FALSE);
	}

	if (sd->ssl)
		ssl_disconnect(sd->ssl);

	g_free(sd->username);
	g_free(sd->handle);
	g_free(sd);
	ic->proto_data = NULL;
}

static int skype_buddy_msg(struct im_connection *ic, char *who, char *message,
			   int flags)
{
	char *ptr, *nick;
	int st;

	/* Unused parameter */
	flags = flags;

	nick = g_strdup(who);
	ptr = strchr(nick, '@');
	if (ptr)
		*ptr = '\0';

	if (!strncmp(who, "skypeconsole", 12))
		st = skype_printf(ic, "%s\n", message);
	else
		st = skype_printf(ic, "MESSAGE %s %s\n", nick, message);
	g_free(nick);

	return st;
}

const struct skype_away_state *skype_away_state_by_name(char *name)
{
	int i;

	for (i = 0; skype_away_state_list[i].full_name; i++)
		if (g_strcasecmp(skype_away_state_list[i].full_name, name) == 0)
			return skype_away_state_list + i;

	return NULL;
}

static void skype_set_away(struct im_connection *ic, char *state_txt,
			   char *message)
{
	const struct skype_away_state *state;

	/* Unused parameter */
	message = message;

	if (state_txt == NULL)
		state = skype_away_state_by_name("Online");
	else
		state = skype_away_state_by_name(state_txt);
	skype_printf(ic, "SET USERSTATUS %s\n", state->code);
}

static GList *skype_away_states(struct im_connection *ic)
{
	static GList *l;
	int i;

	/* Unused parameter */
	ic = ic;

	if (l == NULL)
		for (i = 0; skype_away_state_list[i].full_name; i++)
			l = g_list_append(l,
				(void *)skype_away_state_list[i].full_name);

	return l;
}

static char *skype_set_display_name(set_t *set, char *value)
{
	account_t *acc = set->data;
	struct im_connection *ic = acc->ic;

	skype_printf(ic, "SET PROFILE FULLNAME %s", value);
	return value;
}

static char *skype_set_balance(set_t *set, char *value)
{
	account_t *acc = set->data;
	struct im_connection *ic = acc->ic;

	skype_printf(ic, "GET PROFILE PSTN_BALANCE");
	return value;
}

static void skype_call(struct im_connection *ic, char *value)
{
	char *nick = g_strdup(value);
	char *ptr = strchr(nick, '@');

	if (ptr)
		*ptr = '\0';
	skype_printf(ic, "CALL %s", nick);
	g_free(nick);
}

static void skype_hangup(struct im_connection *ic)
{
	struct skype_data *sd = ic->proto_data;

	if (sd->call_id) {
		skype_printf(ic, "SET CALL %s STATUS FINISHED",
				sd->call_id);
		g_free(sd->call_id);
		sd->call_id = 0;
	} else
		imcb_error(ic, "There are no active calls currently.");
}

static char *skype_set_call(set_t *set, char *value)
{
	account_t *acc = set->data;
	struct im_connection *ic = acc->ic;

	if (value)
		skype_call(ic, value);
	else
		skype_hangup(ic);
	return value;
}

static void skype_add_buddy(struct im_connection *ic, char *who, char *group)
{
	struct skype_data *sd = ic->proto_data;
	char *nick, *ptr;

	nick = g_strdup(who);
	ptr = strchr(nick, '@');
	if (ptr)
		*ptr = '\0';

	if (!group) {
		skype_printf(ic, "SET USER %s BUDDYSTATUS 2 Please authorize me\n",
				nick);
		g_free(nick);
	} else {
		struct skype_group *sg = skype_group_by_name(ic, group);

		if (!sg) {
			/* No such group, we need to create it, then have to
			 * add the user once it's created. */
			skype_printf(ic, "CREATE GROUP %s", group);
			sd->pending_user = g_strdup(nick);
		} else {
			skype_printf(ic, "ALTER GROUP %d ADDUSER %s", sg->id, nick);
		}
	}
}

static void skype_remove_buddy(struct im_connection *ic, char *who, char *group)
{
	char *nick, *ptr;

	/* Unused parameter */
	group = group;

	nick = g_strdup(who);
	ptr = strchr(nick, '@');
	if (ptr)
		*ptr = '\0';
	skype_printf(ic, "SET USER %s BUDDYSTATUS 1\n", nick);
	g_free(nick);
}

void skype_chat_msg(struct groupchat *gc, char *message, int flags)
{
	struct im_connection *ic = gc->ic;

	/* Unused parameter */
	flags = flags;

	skype_printf(ic, "CHATMESSAGE %s %s\n", gc->title, message);
}

void skype_chat_leave(struct groupchat *gc)
{
	struct im_connection *ic = gc->ic;
	skype_printf(ic, "ALTER CHAT %s LEAVE\n", gc->title);
	gc->data = (void *)TRUE;
}

void skype_chat_invite(struct groupchat *gc, char *who, char *message)
{
	struct im_connection *ic = gc->ic;
	char *ptr, *nick;

	nick = g_strdup(who);
	ptr = strchr(nick, '@');
	if (ptr)
		*ptr = '\0';
	skype_printf(ic, "ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
	g_free(nick);
}

void skype_chat_topic(struct groupchat *gc, char *message)
{
	struct im_connection *ic = gc->ic;
	struct skype_data *sd = ic->proto_data;
	skype_printf(ic, "ALTER CHAT %s SETTOPIC %s\n",
		gc->title, message);
	sd->topic_wait = 1;
}

struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
{
	struct skype_data *sd = ic->proto_data;
	char *ptr, *nick;
	nick = g_strdup(who);
	ptr = strchr(nick, '@');
	if (ptr)
		*ptr = '\0';
	skype_printf(ic, "CHAT CREATE %s\n", nick);
	sd->groupchat_with = g_strdup(nick);
	g_free(nick);
	/* We create a fake chat for now. We will replace it with a real one in
	 * the real callback. */
	return imcb_chat_new(ic, "");
}

static void skype_get_info(struct im_connection *ic, char *who)
{
	char *ptr, *nick;
	nick = g_strdup(who);
	ptr = strchr(nick, '@');
	if (ptr)
		*ptr = '\0';
	skype_printf(ic, "GET USER %s FULLNAME\n", nick);
	skype_printf(ic, "GET USER %s PHONE_HOME\n", nick);
	skype_printf(ic, "GET USER %s PHONE_OFFICE\n", nick);
	skype_printf(ic, "GET USER %s PHONE_MOBILE\n", nick);
	skype_printf(ic, "GET USER %s NROF_AUTHED_BUDDIES\n", nick);
	skype_printf(ic, "GET USER %s TIMEZONE\n", nick);
	skype_printf(ic, "GET USER %s LASTONLINETIMESTAMP\n", nick);
	skype_printf(ic, "GET USER %s SEX\n", nick);
	skype_printf(ic, "GET USER %s LANGUAGE\n", nick);
	skype_printf(ic, "GET USER %s COUNTRY\n", nick);
	skype_printf(ic, "GET USER %s PROVINCE\n", nick);
	skype_printf(ic, "GET USER %s CITY\n", nick);
	skype_printf(ic, "GET USER %s HOMEPAGE\n", nick);
	skype_printf(ic, "GET USER %s ABOUT\n", nick);
	/*
	 * Hack: we query the bithday property which is always a single line,
	 * so we can send the collected properties to the user when we have
	 * this one.
	 */
	skype_printf(ic, "GET USER %s BIRTHDAY\n", nick);
}

static void skype_set_my_name(struct im_connection *ic, char *info)
{
	skype_set_display_name(set_find(&ic->acc->set, "display_name"), info);
}

static void skype_init(account_t *acc)
{
	set_t *s;

	s = set_add(&acc->set, "server", SKYPE_DEFAULT_SERVER, set_eval_account,
		acc);
	s->flags |= ACC_SET_OFFLINE_ONLY;

	s = set_add(&acc->set, "port", SKYPE_DEFAULT_PORT, set_eval_int, acc);
	s->flags |= ACC_SET_OFFLINE_ONLY;

	s = set_add(&acc->set, "display_name", NULL, skype_set_display_name,
		acc);
	s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;

	s = set_add(&acc->set, "call", NULL, skype_set_call, acc);
	s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;

	s = set_add(&acc->set, "balance", NULL, skype_set_balance, acc);
	s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;

	s = set_add(&acc->set, "skypeout_offline", "true", set_eval_bool, acc);

	s = set_add(&acc->set, "skypeconsole", "false", set_eval_bool, acc);
	s->flags |= ACC_SET_OFFLINE_ONLY;

	s = set_add(&acc->set, "skypeconsole_receive", "false", set_eval_bool,
		acc);
	s->flags |= ACC_SET_OFFLINE_ONLY;

	s = set_add(&acc->set, "auto_join", "false", set_eval_bool, acc);
	s->flags |= ACC_SET_OFFLINE_ONLY;

	s = set_add(&acc->set, "test_join", "false", set_eval_bool, acc);
	s->flags |= ACC_SET_OFFLINE_ONLY;

	s = set_add(&acc->set, "show_moods", "false", set_eval_bool, acc);

	s = set_add(&acc->set, "edit_prefix", "EDIT:",
			NULL, acc);
}

#if BITLBEE_VERSION_CODE > BITLBEE_VER(3, 0, 1)
GList *skype_buddy_action_list(bee_user_t *bu)
{
	static GList *ret;

	/* Unused parameter */
	bu = bu;

	if (ret == NULL) {
		static const struct buddy_action ba[3] = {
			{"CALL", "Initiate a call" },
			{"HANGUP", "Hang up a call" },
		};

		ret = g_list_prepend(ret, (void *) ba + 0);
	}

	return ret;
}

void *skype_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data)
{
	/* Unused parameters */
	args = args;
	data = data;

	if (!g_strcasecmp(action, "CALL"))
		skype_call(bu->ic, bu->handle);
	else if (!g_strcasecmp(action, "HANGUP"))
		skype_hangup(bu->ic);

	return NULL;
}
#endif

void init_plugin(void)
{
	struct prpl *ret = g_new0(struct prpl, 1);

	ret->name = "skype";
	ret->login = skype_login;
	ret->init = skype_init;
	ret->logout = skype_logout;
	ret->buddy_msg = skype_buddy_msg;
	ret->get_info = skype_get_info;
	ret->set_my_name = skype_set_my_name;
	ret->away_states = skype_away_states;
	ret->set_away = skype_set_away;
	ret->add_buddy = skype_add_buddy;
	ret->remove_buddy = skype_remove_buddy;
	ret->chat_msg = skype_chat_msg;
	ret->chat_leave = skype_chat_leave;
	ret->chat_invite = skype_chat_invite;
	ret->chat_with = skype_chat_with;
	ret->handle_cmp = g_strcasecmp;
	ret->chat_topic = skype_chat_topic;
#if BITLBEE_VERSION_CODE > BITLBEE_VER(3, 0, 1)
	ret->buddy_action_list = skype_buddy_action_list;
	ret->buddy_action = skype_buddy_action;
#endif
	register_protocol(ret);
}