summaryrefslogtreecommitdiff
path: root/plugins/guildbot/handlers.py
blob: 6d032b57764fe35a9ee14534b3dee46ab2302ad1 (plain) (blame)
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
from chat import send_whisper
from utils import extends
from commands import parse_player_name


online_users = None
db = None
max_msg_len = 200
pending_invitations = {}


def ignore():
    pass


def listonline(nick, _):
    curr_msg = ''
    online = online_users.online_users

    for prow in db.all_players_same_guild(nick):
        p = prow[0]
        if p in online:
            if len(curr_msg + ', ' + p) > max_msg_len:
                send_whisper(nick, curr_msg)
                curr_msg = p
            else:
                curr_msg = curr_msg + ', ' + p

    send_whisper(nick, curr_msg)


def leave(nick, _):
    info = db.player_info(nick)
    broadcast(nick, '"{}" left the guild'.format(nick), True)
    db.guild_remove_player(nick)
    send_whisper(nick, 'You left guild {}'.format(info[1]))


def showinfo(nick, _):
    db.player_set_showinfo(nick, True)
    send_whisper(nick, "Information messages are visible")


def hideinfo(nick, _):
    db.player_set_showinfo(nick, False)
    send_whisper(nick, "Information messages are hidden")


def status(nick, _):
    _, guild, access = db.player_info(nick)
    send_whisper(nick, 'Player:{}, Guild:{}, Access:{}'.format(
        nick, guild, access))


# FIXME: not finished
def invite(nick, player):
    if not player:
        send_whisper(nick, "Usage: !invite Player")
        return

    pinfo = db.player_info(player)
    if pinfo and pinfo[0]:
        send_whisper(nick, '"{}" is already a member of guild "{}"'.format(
            player, pinfo[1]))
        return

    online = online_users.online_users
    if player not in online:
        send_whisper(nick, '"{}" is not online'.format(player))
        return

    _, guild, _ = db.player_info(nick)
    invite_msg = ('You have been invited to the "{}" guild chat. '
                  'If you would like to accept this invitation '
                  'please reply "yes" and if not then "no"').format(guild)
    send_whisper(player, invite_msg)
    # FIXME: what if player is offline? online_list can be outdated
    pending_invitations[player] = guild


def remove(nick, player):
    if not player:
        send_whisper(nick, "Usage: !remove Player")
        return

    pinfo = db.player_info(player)
    if not pinfo:
        send_whisper(nick, '{} is not in any guild'.format(player))
        return

    gid, _, _ = db.player_info(nick)
    if gid != pinfo[0]:
        send_whisper(nick, '{} is not in your guild'.format(player))
        return

    broadcast(player, '{} was removed from your guild'.format(player), True)
    db.guild_remove_player(player)
    send_whisper(nick, 'You were removed from "{}" guild'.format(pinfo[1]))


def setmotd(nick, motd):
    guild = db.player_info(nick)[1]
    db.setmotd(guild, motd)
    broadcast(nick, 'MOTD: ' + motd)


def removemotd(nick, _):
    guild = db.player_info(nick)[1]
    db.setmotd(guild, '')
    broadcast(nick, 'MOTD removed')


def setaccess(nick, params):
    try:
        si = params.index(" ")
        lvl = int(params[:si])
        player = params[si + 1:]
        if len(player) < 4:
            raise ValueError
    except ValueError:
        send_whisper(nick, "Usage: !setaccess Level Player")
        return

    gid, guild_name, access = db.player_info(nick)
    gidp, _, accessp = db.player_info(player)

    if gid != gidp:
        send_whisper(nick, '{} is not in your guild "{}"'.format(
            player, guild_name))
        return

    if access <= accessp:
        send_whisper(nick, "You cannot set access level for {}".format(
            player))
        return

    db.player_set_access(player, lvl)
    send_whisper(nick, "Player: {}, access level: {}".format(
        player, lvl))


def disband(nick, _):
    _, guild, _ = db.player_info(nick)
    if db.guild_delete(guild):
        send_whisper(nick, 'Deleted guild "{}"'.format(guild))
    else:
        send_whisper(nick, 'Error deleting guild "{}"'.format(guild))


def addguild(nick, params):
    usage = 'Usage: !addguild Leader Guild (note: Leader can be quoted)'
    if not params:
        send_whisper(nick, usage)
        return

    leader, guild = parse_player_name(params)

    if len(leader) < 4 or len(guild) < 4:
        send_whisper(nick, usage)
        return

    if db.guild_create(guild):
        send_whisper(nick, 'Created guild "{}", leader is "{}"'.format(
            guild, leader))
    else:
        send_whisper(nick, "Error creating guild")


def removeguild(nick, guild_name):
    if not guild_name:
        send_whisper(nick, "Usage: !removeguild Guild")
        return

    if db.guild_delete(guild_name):
        send_whisper(nick, 'Deleted guild "{}"'.format(guild_name))
    else:
        send_whisper(nick, 'Guild not found: "{}"'.format(guild_name))


def globalmsg(nick, msg):
    if not msg:
        send_whisper(nick, "Usage: !global Message")
        return

    online = online_users.online_users
    for prow in db.all_players_any_guild():
        pname = prow[0]
        if pname in online:
            send_whisper(pname, msg)


def joinguild(nick, guild):
    if not guild:
        send_whisper(nick, "Usage: !joinguild Guild")
        return

    if db.player_join_guild(nick, guild, 20):
        send_whisper(nick, 'You joined guild "{}"'.format(guild))
    else:
        send_whisper(nick, 'Guild "{}" does not exist'.format(guild))


def showhelp(nick, _):
    access = db.player_get_access(nick)
    curr_line = ''

    for cmd, (lvl, _, hlp) in commands.iteritems():
        if access < lvl:
            continue

        if hlp[0] == '+':
            help_s = '!' + cmd + ' ' + hlp[1:]
        else:
            help_s = '!' + cmd + ' -- ' + hlp

        if len(curr_line + '; ' + help_s) > max_msg_len:
            send_whisper(nick, curr_line)
            curr_line = help_s
        else:
            curr_line = curr_line + '; ' + help_s

    if curr_line:
        send_whisper(nick, curr_line)


commands = {
    "help":        (-10, showhelp,    "show help"),
    "info":        (0,   status,      "display guild information"),
    "listonline":  (0,   listonline,  "list online players"),
    "leave":       (0,   leave,       "leave your guild"),
    "showinfo":    (0,   showinfo,    "verbose notifications"),
    "hideinfo":    (0,   hideinfo,    "quiet notifications"),
    "invite":      (5,   invite,      "+Player -- invite player to guild"),
    "remove":      (5,   remove,      "+Player -- remove player from guild"),
    "setmotd":     (5,   setmotd,     "+MOTD -- set MOTD"),
    "removemotd":  (5,   removemotd,  "remove MOTD"),
    "setaccess":   (10,  setaccess,   "+Level Player -- set access level"),
    "disband":     (10,  disband,     "disband your guild"),
    "addguild":    (20,  addguild,    "+Leader GuildName -- add guild"),
    "removeguild": (20,  removeguild, "+GuildName -- remove guild"),
    "global":      (20,  globalmsg,   "+Message -- global message"),
    "joinguild":   (20,  joinguild,   "+GuildName -- join a guild"),
}


def exec_command(nick, cmdline):
    end = cmdline.find(" ")
    if end < 0:
        cmd = cmdline[1:]
        arg = ""
    else:
        cmd = cmdline[1:end]
        arg = cmdline[end + 1:]

    if cmd in commands:
        lvl, func, _ = commands[cmd]
        access = db.player_get_access(nick)

        if access < lvl:
            send_whisper(nick, 'That command is fobidden for you!')
        else:
            func(nick, arg)

    else:
        send_whisper(nick, 'Command !{} not found. Try !help'.format(cmd))


def player_joining(player, guild):
    db.player_join_guild(player, guild)
    broadcast(player, '{} joined your guild'.format(player), True)


def broadcast(nick, msg, exclude_nick=False):
    """
    Broadcast message for all players that belong the same guild as nick.
    """
    n = 0
    for prec in db.all_players_same_guild(nick):
        if exclude_nick and prec[0] == nick:
            continue
        n += 1
        send_whisper(prec[0], '{} : {}'.format(nick, msg))

    if n == 0:
        send_whisper(nick, "You don't belong to any guild")


def online_list_update(curr, prev):
    for p in curr - prev:
        ginfo = db.player_info(p)
        if ginfo is not None:
            if ginfo[0] is not None:
                allp = set(db.all_players_same_guild(p))
                n = len(allp.intersection(curr))
                send_whisper(p,
                    'Welcome to {}! ({} Members are online)'.format(
                        ginfo[1], n))
                broadcast(p, '{} is now Online'.format(p), True)

    for p in prev - curr:
        broadcast(p, '{} is now Offline'.format(p), True)


@extends('smsg_whisper')
def got_whisper(data):
    nick, message = data.nick, data.message

    if len(message) < 1:
        return

    if message[0] == '!':
        exec_command(nick, message)
    else:
        if nick in pending_invitations:
            # TODO: inform message
            if message.lower() == 'yes':
                player_joining(nick, pending_invitations[nick])
            del pending_invitations[nick]

        else:
            broadcast(nick, message)


@extends('smsg_whisper_response')
def send_whisper_result(data):
    pass