1 /*
2  * Copyright 2012-2016 James Geboski <jgeboski@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (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, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <string.h>
19 
20 #include "steam-user.h"
21 #include "steam-util.h"
22 
23 SteamUser *
steam_user_new(bee_user_t * bu)24 steam_user_new(bee_user_t *bu)
25 {
26     SteamUser *user;
27 
28     user = g_new0(SteamUser, 1);
29     user->buser = bu;
30     return user;
31 }
32 
33 void
steam_user_free(SteamUser * user)34 steam_user_free(SteamUser *user)
35 {
36     if (G_UNLIKELY(user == NULL)) {
37         return;
38     }
39 
40     g_free(user->server);
41     g_free(user->game);
42     g_free(user);
43 }
44 
45 void
steam_user_chans_msg(SteamUser * user,const gchar * format,...)46 steam_user_chans_msg(SteamUser *user, const gchar *format, ...)
47 {
48     gchar *str;
49     GSList *l;
50     irc_channel_t *ic;
51     irc_user_t *iu;
52     va_list ap;
53 
54     g_return_if_fail(user != NULL);
55     g_return_if_fail(format != NULL);
56     iu = user->buser->ui_data;
57 
58     va_start(ap, format);
59     str = g_strdup_vprintf(format, ap);
60     va_end(ap);
61 
62     for (l = iu->irc->channels; l != NULL; l = l->next) {
63         ic = l->data;
64 
65         if (irc_channel_has_user(ic, iu) != NULL) {
66             irc_send_msg(iu, "PRIVMSG", ic->name, str, NULL);
67         }
68     }
69 
70     g_free(str);
71 }
72 
73 gchar *
steam_user_flags_str(SteamUserFlags flags)74 steam_user_flags_str(SteamUserFlags flags)
75 {
76     gchar *str;
77     gchar **strs;
78 
79     static const SteamUtilEnum enums[] = {
80         {STEAM_USER_FLAG_WEB, "Web"},
81         {STEAM_USER_FLAG_MOBILE, "Mobile"},
82         {STEAM_USER_FLAG_BIGPIC, "Big Picture"},
83         STEAM_UTIL_ENUM_NULL
84     };
85 
86     strs = (gchar **) steam_util_enum_ptrs(enums, flags);
87 
88     if (strs[0] == NULL) {
89         g_free(strs);
90         return NULL;
91     }
92 
93     str = g_strjoinv(", ", strs);
94 
95     g_free(strs);
96     return str;
97 }
98 
99 SteamUserInfo *
steam_user_info_new(SteamId id)100 steam_user_info_new(SteamId id)
101 {
102     SteamUserInfo *info;
103 
104     info = g_new0(SteamUserInfo, 1);
105     info->id = id;
106     info->act = STEAM_USER_ACT_NONE;
107 
108     return info;
109 }
110 
111 void
steam_user_info_free(SteamUserInfo * info)112 steam_user_info_free(SteamUserInfo *info)
113 {
114     if (G_UNLIKELY(info == NULL)) {
115         return;
116     }
117 
118     g_slist_free_full(info->nicks, g_free);
119 
120     g_free(info->profile);
121     g_free(info->server);
122     g_free(info->game);
123     g_free(info->fullname);
124     g_free(info->nick);
125     g_free(info);
126 }
127 
128 SteamUserMsg *
steam_user_msg_new(SteamId id)129 steam_user_msg_new(SteamId id)
130 {
131     SteamUserMsg *msg;
132 
133     msg = g_new0(SteamUserMsg, 1);
134     msg->info = steam_user_info_new(id);
135     return msg;
136 }
137 
138 void
steam_user_msg_free(SteamUserMsg * msg)139 steam_user_msg_free(SteamUserMsg *msg)
140 {
141     if (G_UNLIKELY(msg == NULL)) {
142         return;
143     }
144 
145     steam_user_info_free(msg->info);
146     g_free(msg->text);
147     g_free(msg);
148 }
149 
150 const gchar *
steam_user_msg_type_str(SteamUserMsgType type)151 steam_user_msg_type_str(SteamUserMsgType type)
152 {
153     static const SteamUtilEnum enums[] = {
154         {STEAM_USER_MSG_TYPE_SAYTEXT, "saytext"},
155         {STEAM_USER_MSG_TYPE_EMOTE, "emote"},
156         {STEAM_USER_MSG_TYPE_LEFT_CONV, "leftconversation"},
157         {STEAM_USER_MSG_TYPE_RELATIONSHIP, "personarelationship"},
158         {STEAM_USER_MSG_TYPE_STATE, "personastate"},
159         {STEAM_USER_MSG_TYPE_TYPING, "typing"},
160         {STEAM_USER_MSG_TYPE_MY_SAYTEXT, "my_saytext"},
161         {STEAM_USER_MSG_TYPE_MY_EMOTE, "my_emote"},
162         STEAM_UTIL_ENUM_NULL
163     };
164 
165     return steam_util_enum_ptr(enums, NULL, type);
166 }
167 
168 SteamUserMsgType
steam_user_msg_type_from_str(const gchar * type)169 steam_user_msg_type_from_str(const gchar *type)
170 {
171     static const SteamUtilEnum enums[] = {
172         {STEAM_USER_MSG_TYPE_SAYTEXT, "saytext"},
173         {STEAM_USER_MSG_TYPE_EMOTE, "emote"},
174         {STEAM_USER_MSG_TYPE_LEFT_CONV, "leftconversation"},
175         {STEAM_USER_MSG_TYPE_RELATIONSHIP, "personarelationship"},
176         {STEAM_USER_MSG_TYPE_STATE, "personastate"},
177         {STEAM_USER_MSG_TYPE_TYPING, "typing"},
178         {STEAM_USER_MSG_TYPE_MY_SAYTEXT, "my_saytext"},
179         {STEAM_USER_MSG_TYPE_MY_EMOTE, "my_emote"},
180         STEAM_UTIL_ENUM_NULL
181     };
182 
183     return steam_util_enum_val(enums, STEAM_USER_MSG_TYPE_UNKNOWN, type,
184                                (GCompareFunc) g_ascii_strcasecmp);
185 }
186 
187 const gchar *
steam_user_state_str(SteamUserState state)188 steam_user_state_str(SteamUserState state)
189 {
190     static const SteamUtilEnum enums[] = {
191         {STEAM_USER_STATE_OFFLINE, "Offline"},
192         {STEAM_USER_STATE_ONLINE, "Online"},
193         {STEAM_USER_STATE_BUSY, "Busy"},
194         {STEAM_USER_STATE_AWAY, "Away"},
195         {STEAM_USER_STATE_SNOOZE, "Snooze"},
196         {STEAM_USER_STATE_TRADE, "Looking to Trade"},
197         {STEAM_USER_STATE_PLAY, "Looking to Play"},
198         STEAM_UTIL_ENUM_NULL
199     };
200 
201     return steam_util_enum_ptr(enums, NULL, state);
202 }
203