Debian Codegraph @

src/bnetd/icons.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 HarpyWar ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "common/setup_before.h"
19
20 #include <algorithm>
21 #include <cctype>
22 #include <cstdio>
23 #include <cerrno>
24 #include <cstring>
25 #include <ctime>
26 #include <cstdlib>
27 #include <sstream>
28 #include <vector>
29 #include <string.h>
30
31 #include "compat/strcasecmp.h"
32
33 #include "common/token.h"
34
35 #include "common/list.h"
36 #include "common/eventlog.h"
37 #include "common/xalloc.h"
38 #include "common/xstring.h"
39 #include "common/util.h"
40 #include "common/tag.h"
41
42 #include "account.h"
43 #include "connection.h"
44 #include "icons.h"
45 #include "account_wrap.h"
46
47 #include "message.h"
48 #include "helpfile.h"
49 #include "channel.h"
50 #include "command.h"
51 #include "i18n.h"
52 #include "common/setup_after.h"
53
54 namespace pvpgn
55 {
56
57 namespace bnetd
58 {
59
60 static t_list * icon_head = NULL;
61 /* use prefs_get_custom_icons() for this option */
62 static int enable_custom_icons = 0;
63 /* command groups for master users */
64 static int master_commandgroups = 0;
65 #define MASTER_COMMANDGROUPS_DEFAULT 8; /* default group flags if master_commandgroups is undefined */
66
67 static int skip_comments(char *buff);
68 static t_icon_var_info * _read_option(char *str, unsigned lineno);
69 static char * _find_attr_key(char * clienttag);
70
71
72
73 /* Set usericon (command) */
74 extern int handle_icon_command(t_connection * c, char const *text)
75 {
76 t_account * account;
77 t_connection * user_c;
78 char const * channel_name;
79 char const * subcommand, *username, *iconname;
80 char const *usericon, *iconalias, *iconcode;
81 t_clienttag clienttag;
82 std::string output_icons = "";
83 int count = 1; // 1 icon in stash ("default" icon)
84 bool is_selected = false;
85
86 std::string msgtemp;
87
88 if (!(conn_get_channel(c))) {
89 message_send_text(c, message_type_error, c, localize(c, "This command can only be used inside a channel."));
90 return -1;
91 }
92 else {
93 channel_name = channel_get_name(conn_get_channel(c));
94 }
95
96 // get current clienttag
97 clienttag = conn_get_clienttag(c);
98 if (!clienttag || clienttag == CLIENTTAG_BNCHATBOT_UINT)
99 {
100 message_send_text(c, message_type_error, c, localize(c, "This command can only be used from the game."));
101 return -1;
102 }
103
104
105 // split command args
106 std::vector<std::string> args = split_command(text, 3);
107
108 if (!((master_commandgroups & account_get_command_groups(conn_get_account(c)))))
109 {
110 /* A) Simple command syntax for users */
111
112 iconname = args[1].c_str(); // icon code
113
114 account = conn_get_account(c);
115
116 // get current user icon
117 if (usericon = account_get_user_icon(account, clienttag))
118 usericon = strreverse(xstrdup(usericon));
119
120 bool is_found = false;
121 // get user stash
122 if (char const * iconstash = account_get_user_iconstash(account, clienttag))
123 {
124 std::string s(iconstash);
125 std::istringstream iss(s);
126 do
127 {
128 std::string _icon;
129 iss >> _icon;
130 if (_icon.empty()) continue;
131
132 // output icon_alias instead of icon_code
133 if (iconalias = customicons_stash_find(clienttag, _icon.c_str(), true))
134 _icon = std::string(iconalias);
135
136 if (!(iconcode = customicons_stash_find(clienttag, _icon.c_str())))
137 iconcode = iconalias = _icon.c_str(); // icon was added earlier to user and then removed from server stash
138
139
140 // set selected icon in brackets
141 if (usericon && strcasecmp(usericon, iconcode) == 0)
142 {
143 _icon = "[" + _icon + "]";
144 is_selected = true;
145 }
146
147 if (!output_icons.empty())
148 output_icons += ", ";
149 output_icons += _icon;
150
151 // if iconname parameter passed then find it in user stash
152 if (iconname[0] != '\0')
153 {
154 if (strcasecmp(iconname, iconalias) == 0 || strcasecmp(iconname, iconcode) == 0)
155 is_found = true;
156 }
157
158 count++;
159 } while (iss);
160 }
161
162 if (iconname[0] != '\0')
163 {
164 // unset value
165 if (strcasecmp(iconname, "default") == 0)
166 {
167 msgtemp = localize(c, "Set default icon.");
168 usericon = NULL;
169 }
170 // set usericon (reversed)
171 else
172 {
173 // if icon not found in server stash
174 if (!(iconcode = customicons_stash_find(clienttag, iconname)))
175 {
176 // set icon code from args
177 std::transform(args[1].begin(), args[1].end(), args[1].begin(), ::toupper); // to upper
178 iconcode = args[1].c_str();
179 }
180 if (!is_found || strlen(iconcode) != 4)
181 {
182 message_send_text(c, message_type_error, c, localize(c, "Bad icon."));
183 return -1;
184 }
185 msgtemp = localize(c, "Set new icon is succeed.");
186 usericon = strreverse((char*)iconcode);
187 }
188 account_set_user_icon(account, clienttag, usericon);
189 message_send_text(c, message_type_info, c, msgtemp);
190
191 // if user online then force him to rejoin channel
192 if (c)
193 {
194 conn_update_w3_playerinfo(c);
195 channel_rejoin(c);
196 }
197 return 0;
198 }
199
200 // display icon list in user stash
201 msgtemp = localize(c, "You have {} icons in stash:", count);
202 message_send_text(c, message_type_info, c, msgtemp);
203
204 output_icons = ((is_selected || usericon) ? "default" : "[default]") + std::string((count > 1) ? ", " : "") + output_icons;
205
206 msgtemp = " " + output_icons;
207 message_send_text(c, message_type_info, c, msgtemp);
208
209 return 0;
210 }
211
212 /* B) Complex command syntax for operator and admins */
213
214 if (args[1].empty())
215 {
216 describe_command(c, args[0].c_str());
217 return -1;
218 }
219 subcommand = args[1].c_str(); // sub command
220 username = args[2].c_str(); // username
221 iconname = args[3].c_str(); // icon alias or code
222
223 // display icons from the server stash
224 // subcommand = list
225 if (subcommand[0] == 'l' && username[0] == '\0')
226 {
227 message_send_text(c, message_type_info, c, localize(c, "Available icons in server stash:"));
228 std::string output_icons = customicons_stash_get_list(clienttag, true);
229 msgtemp = " " + output_icons;
230 message_send_text(c, message_type_info, c, msgtemp);
231 return 0;
232 }
233
234
235 // find user account
236 if (!(account = accountlist_find_account(username)))
237 {
238 message_send_text(c, message_type_error, c, localize(c, "Invalid user."));
239 return -1;
240 }
241 user_c = account_get_conn(account);
242
243
244 switch (safe_tolower(subcommand[0]))
245 {
246 // set
247 case 's':
248 if (iconname[0] == '\0')
249 {
250 describe_command(c, args[0].c_str());
251 return -1;
252 }
253
254 // unset value
255 if (strcasecmp(iconname, "default") == 0)
256 {
257 msgtemp = localize(c, "Set default icon for {}", account_get_name(account));
258 usericon = NULL;
259 }
260 // set usericon (reversed)
261 else
262 {
263 // find icon in server stash
264 if (!(iconcode = customicons_stash_find(clienttag, iconname)))
265 {
266 message_send_text(c, message_type_error, c, localize(c, "That icon doesn't exist in server stash."));
267 return -1;
268 }
269 msgtemp = localize(c, "Set new icon is succeed for {}", account_get_name(account));
270 usericon = strreverse((char*)iconcode);
271 }
272 account_set_user_icon(account, clienttag, usericon);
273 message_send_text(c, message_type_info, c, msgtemp);
274
275 // if user online then force him to rejoin channel
276 if (user_c)
277 {
278 conn_update_w3_playerinfo(user_c);
279 channel_rejoin(user_c);
280 }
281
282 return 0;
283 break;
284
285 // list
286 case 'l':
287 // get current user icon
288 if (usericon = account_get_user_icon(account, clienttag))
289 usericon = strreverse(xstrdup(usericon));
290
291 // get user stash
292 if (char const * iconstash = account_get_user_iconstash(account, clienttag))
293 {
294 std::string s(iconstash);
295 std::istringstream iss(s);
296 do
297 {
298 std::string _icon;
299 iss >> _icon;
300 if (_icon.empty()) continue;
301
302 if (!(iconcode = customicons_stash_find(clienttag, _icon.c_str())))
303 iconcode = _icon.c_str(); // icon was added earlier to user and then removed from server stash
304
305 // output icon_alias instead of icon_code
306 if (iconalias = customicons_stash_find(clienttag, _icon.c_str(), true))
307 _icon = std::string(iconalias); // output key instead of code
308
309 // set selected icon in brackets
310 if (usericon && strcasecmp(usericon, iconcode) == 0)
311 {
312 _icon = "[" + _icon + "]";
313 is_selected = true;
314 }
315
316 if (!output_icons.empty())
317 output_icons += ", ";
318
319 output_icons += _icon;
320
321 count++;
322 } while (iss);
323 }
324
325 // display icon list in user stash
326 msgtemp = localize(c, "{} has {} icons in stash : ", account_get_name(account), count);
327 message_send_text(c, message_type_info, c, msgtemp);
328
329 output_icons = ((is_selected || usericon) ? "default" : "[default]") + std::string((count > 1) ? ", " : "") + output_icons;
330
331 msgtemp = " " + output_icons;
332 message_send_text(c, message_type_info, c, msgtemp);
333
334
335 return 0;
336 break;
337
338 // add
339 case 'a':
340 if (iconname[0] == '\0')
341 {
342 describe_command(c, args[0].c_str());
343 return -1;
344 }
345 // find icon in server stash
346 if (!(iconcode = customicons_stash_find(clienttag, iconname)))
347 {
348 message_send_text(c, message_type_error, c, localize(c, "That icon doesn't exist in server stash."));
349 return -1;
350 }
351
352 // find icon in user stash
353 if (char const * iconstash = account_get_user_iconstash(account, clienttag))
354 {
355 std::string s(iconstash);
356 std::istringstream iss(s);
357 do
358 {
359 std::string _icon;
360 iss >> _icon;
361 if (_icon.empty()) continue;
362
363 if (!output_icons.empty())
364 output_icons += " ";
365 output_icons += _icon;
366
367 if (strcasecmp(_icon.c_str(), iconcode) == 0)
368 {
369 message_send_text(c, message_type_error, c, localize(c, "User already has that icon in stash."));
370 return -1;
371 }
372 } while (iss);
373 }
374 // append new icon
375 if (!output_icons.empty())
376 output_icons += " ";
377 output_icons += std::string(iconcode);
378
379 // save stash
380 account_set_user_iconstash(account, clienttag, output_icons.c_str());
381
382 msgtemp = localize(c, "Add new icon to {}'s stash.", account_get_name(account));
383 message_send_text(c, message_type_info, c, msgtemp);
384
385 return 0;
386 break;
387
388 // del
389 case 'd':
390 if (iconname[0] == '\0')
391 {
392 describe_command(c, args[0].c_str());
393 return -1;
394 }
395 bool is_found = false;
396 if (char const * iconstash = account_get_user_iconstash(account, clienttag))
397 {
398 // get current user icon
399 if (usericon = account_get_user_icon(account, clienttag))
400 usericon = strreverse(xstrdup(usericon));
401
402 std::string s(iconstash);
403 std::istringstream iss(s);
404 do
405 {
406 std::string _icon;
407 iss >> _icon;
408 if (_icon.empty()) continue;
409
410 iconalias = customicons_stash_find(clienttag, _icon.c_str(), true);
411 if (!(iconcode = customicons_stash_find(clienttag, _icon.c_str())))
412 iconcode = iconalias = _icon.c_str(); // icon was added earlier to user and then removed from server stash
413
414 // exclude deleted icon (allow to delete icon by code or alias)
415 if (strcasecmp(iconname, iconalias) == 0 || strcasecmp(iconname, iconcode) == 0)
416 {
417 // also unset current user icon if it equals with given
418 if (usericon && strcasecmp(usericon, iconcode) == 0)
419 account_set_user_icon(account, clienttag, NULL);
420
421 is_found = true;
422 continue;
423 }
424
425 if (!output_icons.empty())
426 output_icons += " ";
427 output_icons += _icon;
428 } while (iss);
429 }
430 if (!is_found)
431 {
432 message_send_text(c, message_type_error, c, localize(c, "That icon doesn't exist in user stash."));
433 return -1;
434 }
435 // save stash
436 account_set_user_iconstash(account, clienttag, output_icons.c_str());
437
438 msgtemp = localize(c, "Delete icon from {}'s stash.", account_get_name(account));
439 message_send_text(c, message_type_info, c, msgtemp);
440
441 return 0;
442 break;
443 }
444
445 return -1;
446 }
447
448
449
450 /* Search usericon in available list by icon code or alias, for given clienttag
451 * return icon code if found, and NULL if not found
452 * if (return_alias == true) then aias is returned
453 */
454 extern char const * customicons_stash_find(t_clienttag clienttag, char const * code, bool return_alias)
455 {
456 t_elem * curr;
457 t_elem * curr_var;
458 t_icon_var_info * var;
459 t_iconset_info * iconset;
460
461 if (!code || !clienttag)
462 return NULL;
463
464 char clienttag_str[5];
465 tag_uint_to_str(clienttag_str, clienttag);
466
467 if (icon_head) {
468 LIST_TRAVERSE(icon_head, curr)
469 {
470 if (!(iconset = (t_iconset_info*)elem_get_data(curr))) {
471 eventlog(eventlog_level_error, __FUNCTION__, "icon list contains NULL item");
472 continue;
473 }
474
475 // find a needed tag
476 if (std::strcmp(iconset->clienttag, clienttag_str) != 0)
477 continue;
478
479 LIST_TRAVERSE(iconset->iconstash, curr_var)
480 {
481 if (!(var = (t_icon_var_info*)elem_get_data(curr_var)))
482 {
483 eventlog(eventlog_level_error, __FUNCTION__, "vars list contains NULL item");
484 continue;
485 }
486
487 if (strcasecmp(var->key, code) == 0 || strcasecmp(var->value, code) == 0)
488 {
489 char const * val = (return_alias)
490 ? xstrdup(var->key)
491 : xstrdup(var->value);
492 return val;
493 }
494 }
495 }
496 }
497 return NULL;
498 }
499
500
501 /* Return comma delimeted icons from stash
502 */
503 extern std::string customicons_stash_get_list(t_clienttag clienttag, bool return_alias)
504 {
505 t_elem * curr;
506 t_elem * curr_var;
507 t_icon_var_info * var;
508 t_iconset_info * iconset;
509
510 std::string output = "";
511
512 if (!clienttag)
513 return output;
514
515 char clienttag_str[5];
516 tag_uint_to_str(clienttag_str, clienttag);
517
518 if (icon_head) {
519 LIST_TRAVERSE(icon_head, curr)
520 {
521 if (!(iconset = (t_iconset_info*)elem_get_data(curr))) {
522 eventlog(eventlog_level_error, __FUNCTION__, "icon list contains NULL item");
523 continue;
524 }
525
526 // find a needed tag
527 if (std::strcmp(iconset->clienttag, clienttag_str) != 0)
528 continue;
529
530 LIST_TRAVERSE(iconset->iconstash, curr_var)
531 {
532 if (!(var = (t_icon_var_info*)elem_get_data(curr_var)))
533 {
534 eventlog(eventlog_level_error, __FUNCTION__, "vars list contains NULL item");
535 continue;
536 }
537
538
539 std::string val = (return_alias) ? var->key: var->value;
540
541 if (!output.empty())
542 output += ", ";
543 output += val;
544 }
545 break;
546 }
547 }
548 return output;
549 }
550
551
552 extern int prefs_get_custom_icons()
553 {
554 return enable_custom_icons;
555 }
556
557
558 extern bool customicons_allowed_by_client(t_clienttag clienttag)
559 {
560 switch (clienttag)
561 {
562 // Starcraft variations
563 case CLIENTTAG_STARCRAFT_UINT:
564 case CLIENTTAG_BROODWARS_UINT:
565 case CLIENTTAG_SHAREWARE_UINT:
566 case CLIENTTAG_STARJAPAN_UINT:
567 // Diablo
568 case CLIENTTAG_DIABLORTL_UINT:
569 case CLIENTTAG_DIABLOSHR_UINT:
570 // Warcraft 2
571 case CLIENTTAG_WARCIIBNE_UINT:
572 // Warcraft 3
573 case CLIENTTAG_WARCRAFT3_UINT:
574 case CLIENTTAG_WAR3XP_UINT:
575 return true;
576 default:
577 break;
578 }
579
580 return false;
581 }
582
583
584 /* Format stats text, with attributes from a storage, and output text to a user */
585 extern const char * customicons_get_stats_text(t_account * account, t_clienttag clienttag)
586 {
587 const char *value;
588 const char *text;
589 char clienttag_str[5], tmp[64];
590 t_icon_info * icon;
591 t_iconset_info * iconset;
592 t_icon_var_info * var;
593 t_elem * curr;
594 t_elem * curr_var;
595
596 tag_uint_to_str(clienttag_str, clienttag);
597
598 text = NULL;
599
600 if (icon_head) {
601 LIST_TRAVERSE(icon_head, curr)
602 {
603 if (!(iconset = (t_iconset_info*)elem_get_data(curr)))
604 {
605 eventlog(eventlog_level_error, __FUNCTION__, "icon list contains NULL item");
606 continue;
607 }
608
609 // find a needed tag
610 if (std::strcmp(iconset->clienttag, clienttag_str) != 0)
611 continue;
612
613 if (!iconset->stats)
614 return NULL;
615
616 text = xstrdup(iconset->stats);
617
618 LIST_TRAVERSE(iconset->vars, curr_var)
619 {
620 if (!(var = (t_icon_var_info*)elem_get_data(curr_var)))
621 {
622 eventlog(eventlog_level_error, __FUNCTION__, "vars list contains NULL item");
623 continue;
624 }
625
626 // replace NULL attributes to "0"
627 if (!(value = account_get_strattr(account, var->value)))
628 value = "0";
629
630 // replace in a text
631 std::snprintf(tmp, sizeof(tmp), "{{%s}}", var->key);
632 text = str_replace((char*)text, tmp, (char*)value);
633
634 // also replace {var}->rank
635 if (icon = customicons_get_icon_by_rating(atoi(value), clienttag_str))
636 {
637 std::snprintf(tmp, sizeof(tmp), "{{%s->rank}}", var->key);
638 text = str_replace((char*)text, tmp, icon->rank);
639 }
640 }
641 }
642 }
643
644 return text;
645 }
646
647
648 /* find icon code by rating for the clienttag */
649 extern t_icon_info * customicons_get_icon_by_account(t_account * account, t_clienttag clienttag)
650 {
651 char * attr_key;
652 int rating;
653 char clienttag_str[5];
654 t_icon_info * icon;
655
656 tag_uint_to_str(clienttag_str, clienttag);
657
658 // get attribute field name from a storage
659 if (!(attr_key = _find_attr_key((char*)clienttag_str)))
660 {
661 eventlog(eventlog_level_trace, __FUNCTION__, "no custom iconset defined for client tag {}", clienttag_str);
662 return NULL;
663 }
664
665 rating = account_get_numattr(account, attr_key);
666
667 icon = customicons_get_icon_by_rating(rating, clienttag_str);
668 return icon;
669 }
670
671
672 extern int customicons_unload(void)
673 {
674 t_elem * curr;
675 t_iconset_info * iconset;
676
677 if (icon_head) {
678 LIST_TRAVERSE(icon_head, curr)
679 {
680 if (!(iconset = (t_iconset_info*)elem_get_data(curr))) {
681 eventlog(eventlog_level_error, __FUNCTION__, "icon list contains NULL item");
682 continue;
683 }
684
685 if (list_remove_elem(icon_head, &curr) < 0)
686 eventlog(eventlog_level_error, __FUNCTION__, "could not remove item from list");
687
688 xfree(iconset->clienttag);
689 if (iconset->attr_key)
690 xfree(iconset->attr_key);
691 if (iconset->stats)
692 xfree((void*)iconset->stats);
693 if (iconset->icon_info)
694 xfree(iconset->icon_info);//need to free ->rank and ->icon_code too somehow
695 if (iconset->iconstash)
696 xfree(iconset->iconstash);//need to free ->key and ->value too somehow
697 if (iconset->vars)
698 xfree(iconset->vars);//need to free ->key and ->value too somehow
699 xfree(iconset);
700 }
701
702 if (list_destroy(icon_head) < 0)
703 return -1;
704 icon_head = NULL;
705 }
706 return 0;
707 }
708
709
710 /*****/
711 extern int customicons_load(char const * filename)
712 {
713 std::FILE * fp;
714 unsigned int line, pos, counter = 0;
715 bool end_of_iconset = false;
716 char *buff, *value;
717 char *rating, *rank, *icon;
718 t_icon_var_info * option;
719
720 icon_head = list_create();
721 unsigned int fpos = 0;
722
723 if (!filename) {
724 eventlog(eventlog_level_error, __FUNCTION__, "got NULL filename");
725 return -1;
726 }
727
728 if (!(fp = std::fopen(filename, "r"))) {
729 eventlog(eventlog_level_error, __FUNCTION__, "could not open file \"{}\" for reading (std::fopen: {})", filename, std::strerror(errno));
730 return -1;
731 }
732
733 /* 1) parse root config options */
734 for (line = 1; (buff = file_get_line(fp)); line++)
735 {
736 if (skip_comments(buff) > 0)
737 {
738 continue;
739 }
740 // read root options
741 if (option = _read_option(buff, line))
742 {
743 if (std::strcmp(option->key, "custom_icons") == 0)
744 if (std::strcmp(option->value, "true") == 0)
745 enable_custom_icons = 1;
746 else
747 enable_custom_icons = 0;
748
749 if (std::strcmp(option->key, "master_commandgroups") == 0)
750 {
751 size_t option_valuelen = std::strlen(option->value);
752 // convert string groups from config to integer
753 for (std::size_t i = 0; i < option_valuelen; i++)
754 {
755 if (option->value[i] == '1') master_commandgroups |= 1;
756 else if (option->value[i] == '2') master_commandgroups |= 2;
757 else if (option->value[i] == '3') master_commandgroups |= 4;
758 else if (option->value[i] == '4') master_commandgroups |= 8;
759 else if (option->value[i] == '5') master_commandgroups |= 16;
760 else if (option->value[i] == '6') master_commandgroups |= 32;
761 else if (option->value[i] == '7') master_commandgroups |= 64;
762 else if (option->value[i] == '8') master_commandgroups |= 128;
763 }
764 }
765 // set default value if not defined
766 if (master_commandgroups == 0)
767 master_commandgroups = MASTER_COMMANDGROUPS_DEFAULT;
768
769 xfree(option->key);
770 xfree(option->value);
771 xfree(option);
772 }
773
774
775 /* 2) parse clienttags */
776 if (std::strcmp(buff, "[W3XP]") == 0 || std::strcmp(buff, "[WAR3]") == 0 || std::strcmp(buff, "[STAR]") == 0 ||
777 std::strcmp(buff, "[SEXP]") == 0 || std::strcmp(buff, "[JSTR]") == 0 || std::strcmp(buff, "[SSHR]") == 0 ||
778 std::strcmp(buff, "[W2BN]") == 0 || std::strcmp(buff, "[DRTL]") == 0 || std::strcmp(buff, "[DSHR]") == 0)
779 {
780 if (skip_comments(buff) > 0)
781 {
782 continue;
783 }
784 value = std::strtok(buff, " []"); // extract clienttag
785
786 // new iconset for a clienttag
787 t_iconset_info * icon_set = (t_iconset_info*)xmalloc(sizeof(t_iconset_info));
788 icon_set->clienttag = xstrdup(value);
789 icon_set->attr_key = NULL;
790 icon_set->icon_info = list_create();
791 icon_set->iconstash = list_create();
792 icon_set->vars = list_create();
793 icon_set->stats = NULL;
794
795
796 /* 3) parse inner options under a clienttag */
797 for (; (buff = file_get_line(fp)); line++)
798 {
799 if (std::strcmp(buff, "[W3XP]") == 0 || std::strcmp(buff, "[WAR3]") == 0 || std::strcmp(buff, "[STAR]") == 0 ||
800 std::strcmp(buff, "[SEXP]") == 0 || std::strcmp(buff, "[JSTR]") == 0 || std::strcmp(buff, "[SSHR]") == 0 ||
801 std::strcmp(buff, "[W2BN]") == 0 || std::strcmp(buff, "[DRTL]") == 0 || std::strcmp(buff, "[DSHR]") == 0)
802 {
803 // return position in stream
804 fseek(fp, fpos, SEEK_SET);
805 break;
806 }
807 if (skip_comments(buff) > 0)
808 {
809 continue;
810 }
811 // fill variables list
812 if (option = _read_option(buff, line))
813 {
814 // set attr_key with a first variable
815 if (!icon_set->attr_key)
816 icon_set->attr_key = xstrdup(option->value);
817
818 // add to variables
819 list_append_data(icon_set->vars, option);
820
821 continue;
822 }
823
824 /* 3) parse icons section */
825 if (std::strcmp(buff, "[icons]") == 0)
826 {
827 counter = 0;
828 for (; (buff = file_get_line(fp)); line++)
829 {
830 if (skip_comments(buff) > 0)
831 {
832 continue;
833 }
834 // end if icons
835 if (std::strcmp(buff, "[/icons]") == 0) {
836 break;
837 }
838
839 pos = 0;
840 if (!(rating = next_token(buff, &pos)))
841 {
842 eventlog(eventlog_level_error, __FUNCTION__, "missing value in line {} in file \"{}\"", line, filename);
843 continue;
844 }
845 if (!(rank = next_token(buff, &pos)))
846 {
847 eventlog(eventlog_level_error, __FUNCTION__, "missing rank in line {} in file \"{}\"", line, filename);
848 continue;
849 }
850 if (!(icon = next_token(buff, &pos)))
851 {
852 eventlog(eventlog_level_error, __FUNCTION__, "missing icon in line {} in file \"{}\"", line, filename);
853 continue;
854 }
855 counter++;
856
857 t_icon_info * icon_info = (t_icon_info*)xmalloc(sizeof(t_icon_info));
858 icon_info->rating = atoi(rating);
859 icon_info->rank = xstrdup(rank);
860 icon_info->icon_code = xstrdup(strreverse(icon)); // save reversed icon code
861 list_prepend_data(icon_set->icon_info, icon_info);
862 }
863 }
864
865 /* 3) parse stats section */
866 if (std::strcmp(buff, "[stats]") == 0)
867 {
868 std::string tmp;
869 for (; (buff = file_get_line(fp)); line++)
870 {
871 // end of stats
872 if (std::strcmp(buff, "[/stats]") == 0) {
873 // put whole text of stats after read
874 icon_set->stats = xstrdup(tmp.c_str());
875 break;
876 }
877 tmp = tmp + buff + "\n";
878 }
879 }
880
881
882 /* 4) parse usericons section */
883 if (std::strcmp(buff, "[iconstash]") == 0)
884 {
885 char *key, *value;
886 counter = 0;
887 for (; (buff = file_get_line(fp)); line++)
888 {
889 if (skip_comments(buff) > 0)
890 {
891 continue;
892 }
893 // end if usericons
894 if (std::strcmp(buff, "[/iconstash]") == 0) {
895 break;
896 }
897
898 pos = 0;
899 if (!(key = next_token(buff, &pos)))
900 continue;
901 if (!(value = next_token(buff, &pos)))
902 value = key;
903 counter++;
904
905 t_icon_var_info * icon_item = (t_icon_var_info*)xmalloc(sizeof(t_icon_var_info));
906 icon_item->key = xstrdup(key);
907 icon_item->value = xstrdup(value);
908 list_append_data(icon_set->iconstash, icon_item);
909 }
910 }
911
912 // remember file position
913 fpos = ftell(fp);
914 }
915
916 if (!icon_set->attr_key)
917 {
918 eventlog(eventlog_level_error, __FUNCTION__, "attr_key is null for iconset {}", icon_set->clienttag);
919 continue;
920 }
921 list_append_data(icon_head, icon_set);
922
923 eventlog(eventlog_level_trace, __FUNCTION__, "loaded {} custom icons for {}", counter, icon_set->clienttag);
924 }
925 }
926
927 std::fclose(fp);
928
929 return 0;
930 }
931
932 static int skip_comments(char *buff)
933 {
934 char *temp;
935 int pos;
936
937 for (pos = 0; buff[pos] == '\t' || buff[pos] == ' '; pos++);
938 if (buff[pos] == '\0' || buff[pos] == '#') {
939 return 1;
940 }
941 if ((temp = std::strrchr(buff, '#'))) {
942 unsigned int len;
943 unsigned int endpos;
944
945 *temp = '\0';
946 len = std::strlen(buff) + 1;
947 for (endpos = len - 1; buff[endpos] == '\t' || buff[endpos] == ' '; endpos--);
948 buff[endpos + 1] = '\0';
949 }
950 return 0;
951 }
952
953 /* Read an option like "key = value" from a str line, and split in into a pair: key, value */
954 static t_icon_var_info * _read_option(char *str, unsigned lineno)
955 {
956 char *cp, prev, *directive;
957 t_icon_var_info * icon_var = (t_icon_var_info*)xmalloc(sizeof(t_icon_var_info));
958
959 directive = str;
960 str = str_skip_word(str + 1);
961 if (*str) *(str++) = '\0';
962
963 str = str_skip_space(str);
964 if (*str != '=') {
965 xfree((void*)icon_var);
966 return NULL;
967 }
968
969 str = str_skip_space(str + 1);
970 if (!*str) {
971 eventlog(eventlog_level_error, __FUNCTION__, "missing value at line {}", lineno);
972 xfree((void*)icon_var);
973 return NULL;
974 }
975
976 if (*str == '"') {
977 for (cp = ++str, prev = '\0'; *cp; cp++) {
978 switch (*cp) {
979 case '"':
980 if (prev != '\\') break;
981 prev = '"';
982 continue;
983 case '\\':
984 if (prev == '\\') prev = '\0';
985 else prev = '\\';
986 continue;
987 default:
988 prev = *cp;
989 continue;
990 }
991 break;
992 }
993
994 if (*cp != '"') {
995 eventlog(eventlog_level_error, __FUNCTION__, "missing end quota at line {}", lineno);
996 xfree((void*)icon_var);
997 return NULL;
998 }
999
1000 *cp = '\0';
1001 cp = str_skip_space(cp + 1);
1002 if (*cp) {
1003 eventlog(eventlog_level_error, __FUNCTION__, "extra characters in value after ending quote at line {}", lineno);
1004 xfree((void*)icon_var);
1005 return NULL;
1006 }
1007 }
1008 else {
1009 cp = str_skip_word(str);
1010 if (*cp) {
1011 *cp = '\0';
1012 cp = str_skip_space(cp + 1);
1013 if (*cp) {
1014 eventlog(eventlog_level_error, __FUNCTION__, "extra characters after the value at line {}", lineno);
1015 xfree((void*)icon_var);
1016 return NULL;
1017 }
1018 }
1019 }
1020
1021 //if (!find && std::strcmp(find, directive) == 0)
1022 // return str_skip_space(str);
1023 //else
1024 // return NULL;
1025
1026 icon_var->key = xstrdup(directive);
1027 icon_var->value = xstrdup(str_skip_space(str));
1028
1029 return icon_var;
1030 }
1031
1032
1033
1034
1035 /* Get custom icon by rating for clienttag */
1036 extern t_icon_info * customicons_get_icon_by_rating(int rating, char * clienttag)
1037 {
1038 t_elem * curr;
1039 t_elem * curr_icon;
1040 t_iconset_info * iconset;
1041 t_icon_info * icon = NULL;
1042
1043 if (!clienttag)
1044 return NULL;
1045
1046 if (icon_head) {
1047 LIST_TRAVERSE(icon_head, curr)
1048 {
1049 if (!(iconset = (t_iconset_info*)elem_get_data(curr)))
1050 {
1051 eventlog(eventlog_level_error, __FUNCTION__, "icon list contains NULL item");
1052 continue;
1053 }
1054
1055 // find a needed tag
1056 if (std::strcmp(iconset->clienttag, clienttag) != 0)
1057 continue;
1058
1059 // iterate all icons for the tag
1060 LIST_TRAVERSE(iconset->icon_info, curr_icon)
1061 {
1062 if (!(icon = (t_icon_info*)elem_get_data(curr_icon)))
1063 {
1064 eventlog(eventlog_level_error, __FUNCTION__, "icon list contains NULL item");
1065 continue;
1066 }
1067
1068 if (rating >= icon->rating)
1069 return icon;
1070 }
1071 // if nothing found then return last icon
1072 return icon;
1073 }
1074 }
1075 return NULL;
1076 }
1077
1078
1079 /* Get attr_key for clienttag */
1080 static char * _find_attr_key(char * clienttag)
1081 {
1082 t_elem * curr;
1083 t_iconset_info * iconset;
1084
1085 if (icon_head) {
1086 LIST_TRAVERSE(icon_head, curr)
1087 {
1088 if (!(iconset = (t_iconset_info*)elem_get_data(curr)))
1089 {
1090 eventlog(eventlog_level_error, __FUNCTION__, "icon list contains NULL item");
1091 continue;
1092 }
1093
1094 // find a needed tag
1095 if (std::strcmp(iconset->clienttag, clienttag) == 0)
1096 return iconset->attr_key;
1097 }
1098 }
1099 return NULL;
1100 }
1101
1102
1103
1104
1105 }
1106 }