1 /*
2  * Copyright (c) 2005-2010 William Pitcock <nenolod@atheme.org>.
3  * Copyright (c) 2006-2007 Jilles Tjoelker <jilles@stack.nl>.
4  *
5  * Rights to this code are documented in doc/LICENSE.
6  */
7 
8 #ifndef __GAMESERV_COMMON_H__
9 #define __GAMESERV_COMMON_H__
10 
11 /*
12  * Handle reporting for both fantasy commands and normal commands in GameServ
13  * quickly and easily. Of course, sourceinfo has a vtable that can be manipulated,
14  * but this is quicker and easier...                                  -- nenolod
15  */
gs_command_report(sourceinfo_t * si,const char * fmt,...)16 static inline void gs_command_report(sourceinfo_t *si, const char *fmt, ...)
17 {
18 	va_list args;
19 	char buf[BUFSIZE];
20 
21 	va_start(args, fmt);
22 	vsnprintf(buf, BUFSIZE, fmt, args);
23 	va_end(args);
24 
25 	if (si->c != NULL)
26 	{
27 		service_t *svs = service_find("gameserv");
28 
29 		msg(svs->me->nick, si->c->name, "%s", buf);
30 	}
31 	else
32 		command_success_nodata(si, "%s", buf);
33 }
34 
gs_do_parameters(sourceinfo_t * si,int * parc,char *** parv,mychan_t ** pmc)35 static inline bool gs_do_parameters(sourceinfo_t *si, int *parc, char ***parv, mychan_t **pmc)
36 {
37 	mychan_t *mc;
38 	chanuser_t *cu;
39 	metadata_t *md;
40 	const char *who;
41 	bool allow;
42 
43 	if (*parc == 0)
44 		return true;
45 	if ((*parv)[0][0] == '#')
46 	{
47 		mc = mychan_find((*parv)[0]);
48 		if (mc == NULL)
49 		{
50 			command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), (*parv)[0]);
51 			return false;
52 		}
53 		if (mc->chan == NULL)
54 		{
55 			command_fail(si, fault_nosuch_target, _("\2%s\2 is currently empty."), mc->name);
56 			return false;
57 		}
58 		if (module_find_published("chanserv/set_gameserv"))
59 		{
60 			md = metadata_find(mc, "gameserv");
61 			if (md == NULL)
62 			{
63 				command_fail(si, fault_noprivs, _("%s is not enabled on \2%s\2."), "GAMESERV", mc->name);
64 				return false;
65 			}
66 			cu = chanuser_find(mc->chan, si->su);
67 			if (cu == NULL)
68 			{
69 				command_fail(si, fault_nosuch_target, _("You are not on \2%s\2."), mc->name);
70 				return false;
71 			}
72 			who = md->value;
73 			/* don't subvert +m; other modes can be subverted
74 			 * though
75 			 */
76 			if (mc->chan->modes & CMODE_MOD && !strcasecmp(who, "all"))
77 				who = "voice";
78 			if (!strcasecmp(who, "all"))
79 				allow = true;
80 			else if (!strcasecmp(who, "voice") || !strcmp(who, "1"))
81 				allow = cu->modes != 0 || chanacs_source_flags(mc, si) & (CA_AUTOOP | CA_OP | CA_AUTOVOICE | CA_VOICE);
82 			else if (!strcasecmp(who, "op"))
83 				allow = cu->modes & CSTATUS_OP || chanacs_source_flags(mc, si) & (CA_AUTOOP | CA_OP);
84 			else
85 			{
86 				command_fail(si, fault_noprivs, _("%s is not enabled on \2%s\2."), "GAMESERV", mc->name);
87 				return false;
88 			}
89 			if (!allow)
90 			{
91 				command_fail(si, fault_noprivs, _("You are not authorized to perform this operation."));
92 				return false;
93 			}
94 		}
95 		(*parc)--;
96 		(*parv)++;
97 		*pmc = mc;
98 	}
99 	else
100 		*pmc = NULL;
101 	return true;
102 }
103 
gs_interactive_notification(myuser_t * mu,const char * notification,...)104 static inline void gs_interactive_notification(myuser_t *mu, const char *notification, ...)
105 {
106 	service_t *gameserv;
107 	char buf[BUFSIZE];
108 	va_list va;
109 
110 	va_start(va, notification);
111 	vsnprintf(buf, BUFSIZE, notification, va);
112 	va_end(va);
113 
114 	gameserv = service_find("gameserv");
115 	return_if_fail(gameserv != NULL);
116 
117 	if (MOWGLI_LIST_LENGTH(&mu->logins) > 0)
118 		myuser_notice(gameserv->nick, mu, "%s", buf);
119 	else
120 	{
121 		service_t *svs;
122 
123 		if ((svs = service_find("memoserv")) != NULL)
124 		{
125 			sourceinfo_t nullinfo = { .su = gameserv->me };
126 
127 			command_exec_split(svs, &nullinfo, "SEND", buf, svs->commands);
128 		}
129 	}
130 }
131 
132 #endif
133 
134 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
135  * vim:ts=8
136  * vim:sw=8
137  * vim:noexpandtab
138  */
139