diff options
Diffstat (limited to 'examples/redis-unstable/tests/modules/getchannels.c')
| -rw-r--r-- | examples/redis-unstable/tests/modules/getchannels.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/examples/redis-unstable/tests/modules/getchannels.c b/examples/redis-unstable/tests/modules/getchannels.c new file mode 100644 index 0000000..330531d --- /dev/null +++ b/examples/redis-unstable/tests/modules/getchannels.c | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | #include "redismodule.h" | ||
| 2 | #include <strings.h> | ||
| 3 | #include <assert.h> | ||
| 4 | #include <unistd.h> | ||
| 5 | #include <errno.h> | ||
| 6 | |||
| 7 | /* A sample with declarable channels, that are used to validate against ACLs */ | ||
| 8 | int getChannels_subscribe(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 9 | if ((argc - 1) % 3 != 0) { | ||
| 10 | RedisModule_WrongArity(ctx); | ||
| 11 | return REDISMODULE_OK; | ||
| 12 | } | ||
| 13 | char *err = NULL; | ||
| 14 | |||
| 15 | /* getchannels.command [[subscribe|unsubscribe|publish] [pattern|literal] <channel> ...] | ||
| 16 | * This command marks the given channel is accessed based on the | ||
| 17 | * provided modifiers. */ | ||
| 18 | for (int i = 1; i < argc; i += 3) { | ||
| 19 | const char *operation = RedisModule_StringPtrLen(argv[i], NULL); | ||
| 20 | const char *type = RedisModule_StringPtrLen(argv[i+1], NULL); | ||
| 21 | int flags = 0; | ||
| 22 | |||
| 23 | if (!strcasecmp(operation, "subscribe")) { | ||
| 24 | flags |= REDISMODULE_CMD_CHANNEL_SUBSCRIBE; | ||
| 25 | } else if (!strcasecmp(operation, "unsubscribe")) { | ||
| 26 | flags |= REDISMODULE_CMD_CHANNEL_UNSUBSCRIBE; | ||
| 27 | } else if (!strcasecmp(operation, "publish")) { | ||
| 28 | flags |= REDISMODULE_CMD_CHANNEL_PUBLISH; | ||
| 29 | } else { | ||
| 30 | err = "Invalid channel operation"; | ||
| 31 | break; | ||
| 32 | } | ||
| 33 | |||
| 34 | if (!strcasecmp(type, "literal")) { | ||
| 35 | /* No op */ | ||
| 36 | } else if (!strcasecmp(type, "pattern")) { | ||
| 37 | flags |= REDISMODULE_CMD_CHANNEL_PATTERN; | ||
| 38 | } else { | ||
| 39 | err = "Invalid channel type"; | ||
| 40 | break; | ||
| 41 | } | ||
| 42 | if (RedisModule_IsChannelsPositionRequest(ctx)) { | ||
| 43 | RedisModule_ChannelAtPosWithFlags(ctx, i+2, flags); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | if (!RedisModule_IsChannelsPositionRequest(ctx)) { | ||
| 48 | if (err) { | ||
| 49 | RedisModule_ReplyWithError(ctx, err); | ||
| 50 | } else { | ||
| 51 | /* Normal implementation would go here, but for tests just return okay */ | ||
| 52 | RedisModule_ReplyWithSimpleString(ctx, "OK"); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | return REDISMODULE_OK; | ||
| 57 | } | ||
| 58 | |||
| 59 | int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { | ||
| 60 | REDISMODULE_NOT_USED(argv); | ||
| 61 | REDISMODULE_NOT_USED(argc); | ||
| 62 | if (RedisModule_Init(ctx, "getchannels", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) | ||
| 63 | return REDISMODULE_ERR; | ||
| 64 | |||
| 65 | if (RedisModule_CreateCommand(ctx, "getchannels.command", getChannels_subscribe, "getchannels-api", 0, 0, 0) == REDISMODULE_ERR) | ||
| 66 | return REDISMODULE_ERR; | ||
| 67 | |||
| 68 | return REDISMODULE_OK; | ||
| 69 | } | ||
