Skip to content

Commit

Permalink
slack: clang-format for coherent indentation and coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxmaniac committed May 18, 2023
1 parent be5c1b5 commit 698d642
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 66 deletions.
117 changes: 57 additions & 60 deletions src/modules/slack/slack.c
Expand Up @@ -40,62 +40,60 @@ static char *slack_icon = SLACK_DEFAULT_ICON;
* Exported functions
*/
static cmd_export_t cmds[] = {
{"slack_send", (cmd_function)slack_send1, 1, slack_fixup, 0, ANY_ROUTE},
{0, 0, 0, 0, 0, 0}
};
{"slack_send", (cmd_function)slack_send1, 1, slack_fixup, 0, ANY_ROUTE},
{0, 0, 0, 0, 0, 0}};


/**
* Exported parameters
*/
static param_export_t mod_params[] = {
{ "slack_url", PARAM_STRING|USE_FUNC_PARAM, (void*)_slack_url_param},
{ "channel", PARAM_STRING, &slack_channel }, // channel starts with #
{ "username", PARAM_STRING, &slack_username },
{ "icon_emoji", PARAM_STRING, &slack_icon },
{0, 0, 0}
};
{"slack_url", PARAM_STRING | USE_FUNC_PARAM, (void *)_slack_url_param},
{"channel", PARAM_STRING, &slack_channel}, // channel starts with #
{"username", PARAM_STRING, &slack_username},
{"icon_emoji", PARAM_STRING, &slack_icon}, {0, 0, 0}};

/**
* Module description
*/
struct module_exports exports = {
"slack", /* 1 module name */
DEFAULT_DLFLAGS, /* 2 dlopen flags */
cmds, /* 3 exported functions */
mod_params, /* 4 exported parameters */
0, /* 5 exported RPC functions */
0, /* 6 exported pseudo-variables */
0, /* 7 response function */
mod_init, /* 8 module initialization function */
0, /* 9 per-child init function */
mod_destroy /* 0 destroy function */
"slack", /* 1 module name */
DEFAULT_DLFLAGS, /* 2 dlopen flags */
cmds, /* 3 exported functions */
mod_params, /* 4 exported parameters */
0, /* 5 exported RPC functions */
0, /* 6 exported pseudo-variables */
0, /* 7 response function */
mod_init, /* 8 module initialization function */
0, /* 9 per-child init function */
mod_destroy /* 0 destroy function */
};

/**
* Module init
*/
static int mod_init(void) {
static int mod_init(void)
{
LM_INFO("slack module init\n");

if(httpc_load_api(&httpapi) != 0) {
LM_ERR("can not bind to http_client API \n");
return -1;
}

_slmsg_buf = (char*)pkg_malloc((buf_size+1)*sizeof(char));
if(_slmsg_buf==NULL)
{
_slmsg_buf = (char *)pkg_malloc((buf_size + 1) * sizeof(char));
if(_slmsg_buf == NULL) {
PKG_MEM_ERROR;
return -1;
}
return(0);
return (0);
}

/**
* Module destroy
*/
static void mod_destroy() {
static void mod_destroy()
{
LM_INFO("slack module destroy\n");
if(_slmsg_buf)
pkg_free(_slmsg_buf);
Expand All @@ -108,7 +106,7 @@ static void mod_destroy() {
static void slack_free_str(str *string)
{
str ptr = STR_NULL;
if (string->s == NULL)
if(string->s == NULL)
return;

ptr = *string;
Expand All @@ -126,24 +124,26 @@ static void slack_free_str(str *string)
* send slack message using http_client api
* @return 0 on success, -1 on error
*/
static int slack_curl_send(struct sip_msg* msg, char* uri, str *post_data)
static int slack_curl_send(struct sip_msg *msg, char *uri, str *post_data)
{
int datasz;
char* send_data;
char *send_data;
str ret = STR_NULL;
int curl = 0;

datasz = snprintf(NULL, 0, BODY_FMT, slack_channel, slack_username, post_data->s, slack_icon);
if (datasz < 0) {
datasz = snprintf(NULL, 0, BODY_FMT, slack_channel, slack_username,
post_data->s, slack_icon);
if(datasz < 0) {
LM_ERR("snprintf error in calculating buffer size\n");
return -1;
return -1;
}
send_data = (char *)pkg_mallocxz((datasz + 1) * sizeof(char));
if(send_data == NULL) {
LM_ERR("can not allocate pkg memory [%d] bytes\n", datasz);
return -1;
}
send_data = (char*)pkg_mallocxz((datasz+1)*sizeof(char));
if(send_data==NULL) {
LM_ERR("can not allocate pkg memory [%d] bytes\n", datasz);
return -1;
}
snprintf(send_data, datasz+1, BODY_FMT, slack_channel, slack_username, post_data->s, slack_icon);
snprintf(send_data, datasz + 1, BODY_FMT, slack_channel, slack_username,
post_data->s, slack_icon);

/* send request */
curl = httpapi.http_client_query(msg, uri, &ret, send_data, NULL);
Expand Down Expand Up @@ -180,8 +180,8 @@ static int _slack_parse_url_param(char *val)

// TODO: parse webhook to multiple channels? eg.: chan1=>https://AAA/BBB/CC, chan2=>...

slack_url = (char*)pkg_malloc(len + 1);
if (slack_url==NULL) {
slack_url = (char *)pkg_malloc(len + 1);
if(slack_url == NULL) {
PKG_MEM_ERROR;
return -1;
}
Expand All @@ -196,44 +196,41 @@ static int _slack_parse_url_param(char *val)
*/
int _slack_url_param(modparam_t type, void *val)
{
if(val==NULL) {
if(val == NULL) {
LM_ERR("webhook url not specified\n");
return -1;
}

return _slack_parse_url_param((char*)val);
return _slack_parse_url_param((char *)val);
}

static int slack_fixup_helper(void** param, int param_no)
static int slack_fixup_helper(void **param, int param_no)
{
sl_msg_t *sm;
str s;

sm = (sl_msg_t*)pkg_malloc(sizeof(sl_msg_t));
if(sm==NULL)
{
sm = (sl_msg_t *)pkg_malloc(sizeof(sl_msg_t));
if(sm == NULL) {
PKG_MEM_ERROR;
return -1;
}
memset(sm, 0, sizeof(sl_msg_t));
s.s = (char*)(*param);
s.s = (char *)(*param);
s.len = strlen(s.s);

if(pv_parse_format(&s, &sm->m)<0)
{
LM_ERR("wrong format[%s]\n", (char*)(*param));
if(pv_parse_format(&s, &sm->m) < 0) {
LM_ERR("wrong format[%s]\n", (char *)(*param));
pkg_free(sm);
return E_UNSPEC;
}
*param = (void*)sm;
*param = (void *)sm;
return 0;
}


static int slack_fixup(void** param, int param_no)
static int slack_fixup(void **param, int param_no)
{
if(param_no!=1 || param==NULL || *param==NULL)
{
if(param_no != 1 || param == NULL || *param == NULL) {
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
Expand All @@ -243,22 +240,22 @@ static int slack_fixup(void** param, int param_no)
/**
* send text message to slack
*/
static inline int slack_helper(struct sip_msg* msg, sl_msg_t *sm)
static inline int slack_helper(struct sip_msg *msg, sl_msg_t *sm)
{
str txt;
txt.len = buf_size;

if(_slack_print_log(msg, sm->m, _slmsg_buf, &txt.len)<0)
if(_slack_print_log(msg, sm->m, _slmsg_buf, &txt.len) < 0)
return -1;

txt.s = _slmsg_buf;

return slack_curl_send(msg, slack_url, &txt);
}

static int slack_send1(struct sip_msg* msg, char* frm, char* str2)
static int slack_send1(struct sip_msg *msg, char *frm, char *str2)
{
return slack_helper(msg, (sl_msg_t*)frm);
return slack_helper(msg, (sl_msg_t *)frm);
}


Expand All @@ -269,15 +266,15 @@ static int slack_send1(struct sip_msg* msg, char* frm, char* str2)
*/
static int ki_slack_send(sip_msg_t *msg, str *slmsg)
{
pv_elem_t *xmodel=NULL;
pv_elem_t *xmodel = NULL;
str txt = STR_NULL;
int res;

if(pv_parse_format(slmsg, &xmodel)<0) {
if(pv_parse_format(slmsg, &xmodel) < 0) {
LM_ERR("wrong format[%s]\n", slmsg->s);
return -1;
}
if(pv_printf_s(msg, xmodel, &txt)!=0) {
if(pv_printf_s(msg, xmodel, &txt) != 0) {
LM_ERR("cannot eval reparsed value\n");
pv_elem_free_all(xmodel);
return -1;
Expand Down
15 changes: 9 additions & 6 deletions src/modules/slack/slack.h
Expand Up @@ -32,13 +32,16 @@

#include <string.h>

#define BODY_FMT "{\"channel\": \"%s\", \"username\": \"%s\", \"text\": \"%s\", \"icon_emoji\": \"%s\" }"
#define BODY_FMT \
"{\"channel\": \"%s\", \"username\": \"%s\", \"text\": \"%s\", " \
"\"icon_emoji\": \"%s\" }"
#define SLACK_URL_MAX_SIZE 128
#define SLACK_DEFAULT_CHANNEL "#webtest"
#define SLACK_DEFAULT_USERNAME "webhookbot"
#define SLACK_DEFAULT_ICON ":ghost:"

static int _slack_print_log(struct sip_msg* msg, pv_elem_p list, char *buf, int *len)
static int _slack_print_log(
struct sip_msg *msg, pv_elem_p list, char *buf, int *len)
{
return pv_printf(msg, list, buf, len);
}
Expand All @@ -47,13 +50,13 @@ static int _slack_print_log(struct sip_msg* msg, pv_elem_p list, char *buf, int
httpc_api_t httpapi;

static void slack_free_str(str *str);
static int slack_curl_send(struct sip_msg* msg, char* uri, str *post_data );
static int slack_curl_send(struct sip_msg *msg, char *uri, str *post_data);
static int _slack_parse_url_param(char *val);
static int _slack_url_param(modparam_t type, void *val);

static int slack_fixup(void** param, int param_no);
static int slack_send1(struct sip_msg* msg, char* frm, char* str2);
static int slack_fixup_helper(void** param, int param_no);
static int slack_fixup(void **param, int param_no);
static int slack_send1(struct sip_msg *msg, char *frm, char *str2);
static int slack_fixup_helper(void **param, int param_no);

typedef struct _sl_msg
{
Expand Down

0 comments on commit 698d642

Please sign in to comment.