Changeset 5899:4e7e80f39536 in roaraudio


Ignore:
Timestamp:
04/14/13 15:16:58 (6 years ago)
Author:
phi
Branch:
default
Message:

use a instance-local global state

File:
1 edited

Legend:

Unmodified
Added
Removed
  • plugins/roard/protocol-irc.c

    r5823 r5899  
    5050static void cb_client_delete(struct roar_notify_core * core, struct roar_event * event, void * userdata); 
    5151 
    52 static struct roar_subscriber * subscription_client_delete = NULL; 
    53  
    54 static char * server_name     = "irc.roard"; 
    55 static char * server_fullname = "RoarAudio roard IRC Server plugin"; 
    56  
    57 static char * quit_msg = NULL; 
    58  
    59 static struct channel { 
     52struct channel { 
    6053 char * name; 
    6154 struct { 
     
    6659 size_t client_count; 
    6760 int clients[ROAR_CLIENTS_MAX]; 
    68 } g_channels[MAX_CHANNELS]; 
    69  
    70 static struct command { 
     61}; 
     62 
     63struct state { 
     64 const char * server_name; 
     65 const char * server_fullname; 
     66 const char * quit_msg; 
     67 struct roar_subscriber * subscription_client_delete; 
     68 struct channel g_channels[MAX_CHANNELS]; 
     69}; 
     70 
     71static struct state   g_state_init = { 
     72 .server_name = "irc.roard", 
     73 .server_fullname = "RoarAudio roard IRC Server plugin", 
     74 .quit_msg = NULL, 
     75 .subscription_client_delete = NULL 
     76}; 
     77 
     78static struct state * g_state = &g_state_init; 
     79 
     80static const struct command { 
    7181 const char * name; 
    7282 int (*func)(int client, const char * cmd, char * args, char * text); 
     
    193203 
    194204 for (i = 0; i < MAX_CHANNELS; i++) { 
    195   c = &(g_channels[i]); 
     205  c = &(g_state->g_channels[i]); 
    196206 
    197207  if ( !c->client_count ) 
     
    216226 
    217227 for (i = 0; i < MAX_CHANNELS; i++) { 
    218   c = &(g_channels[i]); 
     228  c = &(g_state->g_channels[i]); 
    219229 
    220230  if ( !c->client_count ) 
     
    281291 
    282292 for (i = 0; i < MAX_CHANNELS; i++) { 
    283   if ( !g_channels[i].client_count ) 
    284    continue; 
    285  
    286   if ( !!strcasecmp(g_channels[i].name, channel) ) 
    287    continue; 
    288  
    289   c = &(g_channels[i]); 
     293  if ( !g_state->g_channels[i].client_count ) 
     294   continue; 
     295 
     296  if ( !!strcasecmp(g_state->g_channels[i].name, channel) ) 
     297   continue; 
     298 
     299  c = &(g_state->g_channels[i]); 
    290300  break; 
    291301 } 
     
    293303 if ( c == NULL ) { 
    294304  for (i = 0; i < MAX_CHANNELS; i++) { 
    295    if ( g_channels[i].client_count ) 
     305   if ( g_state->g_channels[i].client_count ) 
    296306    continue; 
    297307 
    298    c = &(g_channels[i]); 
     308   c = &(g_state->g_channels[i]); 
    299309   break; 
    300310  } 
     
    322332 
    323333 for (i = 0; i < MAX_CHANNELS; i++) { 
    324   if ( !g_channels[i].client_count ) 
    325    continue; 
    326  
    327   if ( !!strcasecmp(g_channels[i].name, channel) ) 
    328    continue; 
    329  
    330   if ( !g_channels[i].clients[client] ) 
     334  if ( !g_state->g_channels[i].client_count ) 
     335   continue; 
     336 
     337  if ( !!strcasecmp(g_state->g_channels[i].name, channel) ) 
     338   continue; 
     339 
     340  if ( !g_state->g_channels[i].clients[client] ) 
    331341   return -1; 
    332342 
    333   c = &(g_channels[i]); 
     343  c = &(g_state->g_channels[i]); 
    334344  break; 
    335345 } 
     
    359369 
    360370 for (i = 0; i < MAX_CHANNELS; i++) { 
    361   if ( !g_channels[i].client_count ) 
    362    continue; 
    363  
    364   if ( !!strcasecmp(g_channels[i].name, channel) ) 
     371  if ( !g_state->g_channels[i].client_count ) 
     372   continue; 
     373 
     374  if ( !!strcasecmp(g_state->g_channels[i].name, channel) ) 
    365375   continue; 
    366376 
     
    368378 
    369379  for (j = 0; j < ROAR_CLIENTS_MAX; j++) { 
    370    if ( g_channels[i].clients[j] == 0 ) 
     380   if ( g_state->g_channels[i].clients[j] == 0 ) 
    371381    continue; 
    372382 
     
    375385   if ( (offset + len + 3) > sizeof(buf) ) { 
    376386    buf[offset] = 0; 
    377     put_printf(client, ":%s 353 %s = %s :%s\n", server_name, nick, channel, buf); 
     387    put_printf(client, ":%s 353 %s = %s :%s\n", g_state->server_name, nick, channel, buf); 
    378388    offset = 0; 
    379389   } 
     
    388398  if ( offset ) { 
    389399   buf[offset] = 0; 
    390    put_printf(client, ":%s 353 %s = %s :%s\n", server_name, nick, channel, buf); 
    391   } 
    392   put_printf(client, ":%s 366 %s %s :End of /NAMES list.\n", server_name, nick, channel); 
     400   put_printf(client, ":%s 353 %s = %s :%s\n", g_state->server_name, nick, channel, buf); 
     401  } 
     402  put_printf(client, ":%s 366 %s %s :End of /NAMES list.\n", g_state->server_name, nick, channel); 
    393403 
    394404  return 0; 
     
    402412 size_t count; 
    403413 struct channel * c; 
    404  const char * text = quit_msg; 
     414 const char * text = g_state->quit_msg; 
    405415 int client = event->target; 
    406416 const char * ufull = get_ufull(client); 
     
    412422  text = "Client deleted. Died, kicked or internal error."; 
    413423 
    414  if ( quit_msg != NULL ) 
     424 if ( g_state->quit_msg != NULL ) 
    415425  put_printf(client, "ERROR :Closing Link: %s (Quit: %s)\n", ufull, text); 
    416426 
     
    420430 
    421431 for (i = 0; i < MAX_CHANNELS; i++) { 
    422   c = &(g_channels[i]); 
     432  c = &(g_state->g_channels[i]); 
    423433 
    424434  if ( !c->client_count ) 
     
    474484      ":%s 372 %s :- MotD goes here...\n" 
    475485      ":%s 376 %s :End of /MOTD command.\n", 
    476    server_name, name, 
    477    server_name, name, server_name, 
    478    server_name, name, 
    479    server_name, name 
     486   g_state->server_name, name, 
     487   g_state->server_name, name, g_state->server_name, 
     488   g_state->server_name, name, 
     489   g_state->server_name, name 
    480490 ); 
    481491 
     
    537547 
    538548 if ( !found ) { 
    539   put_printf(client, ":%s 421 %s %s :Unknown command\n", server_name, get_nick(client), cmd); 
     549  put_printf(client, ":%s 421 %s %s :Unknown command\n", g_state->server_name, get_nick(client), cmd); 
    540550 } 
    541551 
     
    554564 if ( args != NULL && args[0] != 0 && is_valid_name(args) && strlen(args) < ROAR_BUFFER_NAME ) { 
    555565  if ( get_client_by_nick(args) != -1 ) { 
    556    put_printf(client, ":%s 433 %s %s :Nickname is already in use.\n", server_name, nick, args); 
     566   put_printf(client, ":%s 433 %s %s :Nickname is already in use.\n", g_state->server_name, nick, args); 
    557567  } else { 
    558568   ufull = get_ufull(client); 
     
    566576  } 
    567577 } else { 
    568   put_printf(client, ":%s 432 %s %s :Erroneous Nickname: Illegal characters\n", server_name, nick, args); 
     578  put_printf(client, ":%s 432 %s %s :Erroneous Nickname: Illegal characters\n", g_state->server_name, nick, args); 
    569579 } 
    570580 
     
    585595  text = "... have a cuddle ..."; 
    586596 
    587  quit_msg = text; 
     597 g_state->quit_msg = text; 
    588598 ret = clients_delete(client); 
    589  quit_msg = NULL; 
     599 g_state->quit_msg = NULL; 
    590600 
    591601 return ret; 
     
    619629 
    620630 for (i = 0; i < MAX_CHANNELS; i++) { 
    621   if ( !g_channels[i].client_count ) 
     631  if ( !g_state->g_channels[i].client_count ) 
    622632   continue; 
    623633  numchans++; 
     
    625635 
    626636 put_printf(client, ":%s 251 %s :There are %zu users and 0 invisible on 1 servers\n", 
    627             server_name, clientnick, counters_get(cur, clients)); 
     637            g_state->server_name, clientnick, counters_get(cur, clients)); 
    628638 put_printf(client, ":%s 254 %s %zu :channels formed\n", 
    629             server_name, clientnick, numchans); 
     639            g_state->server_name, clientnick, numchans); 
    630640 put_printf(client, ":%s 255 %s :I have %zu clients and 0 servers\n", 
    631             server_name, clientnick, counters_get(cur, clients)); 
     641            g_state->server_name, clientnick, counters_get(cur, clients)); 
    632642 put_printf(client, ":%s 265 %s :Current Local Users: %zu  Max: <unknown>\n", 
    633             server_name, clientnick, counters_get(cur, clients)); 
     643            g_state->server_name, clientnick, counters_get(cur, clients)); 
    634644 put_printf(client, ":%s 266 %s :Current Global Users: %zu  Max: <unknown>\n", 
    635             server_name, clientnick, counters_get(cur, clients)); 
     645            g_state->server_name, clientnick, counters_get(cur, clients)); 
    636646 
    637647 return 0; 
     
    650660 
    651661 if ( (tclient = get_client_by_nick(tnick)) == -1 ) { 
    652   put_printf(client, ":%s 401 %s %s :No such nick/channel\n", server_name, clientnick, tnick); 
     662  put_printf(client, ":%s 401 %s %s :No such nick/channel\n", g_state->server_name, clientnick, tnick); 
    653663 } else { 
    654   put_printf(client, ":%s 311 %s %s %s %s * :%s\n", server_name, clientnick, tnick, 
     664  put_printf(client, ":%s 311 %s %s %s %s * :%s\n", g_state->server_name, clientnick, tnick, 
    655665             get_ident(tclient), get_node(tclient), get_realname(tclient)); 
    656   put_printf(client, ":%s 312 %s %s %s :%s\n", server_name, clientnick, tnick, server_name, server_fullname); 
    657  } 
    658  put_printf(client, ":%s 318 %s %s :End of /WHOIS list.\n", server_name, clientnick, tnick); 
     666  put_printf(client, ":%s 312 %s %s %s :%s\n", g_state->server_name, clientnick, tnick, g_state->server_name, g_state->server_fullname); 
     667 } 
     668 put_printf(client, ":%s 318 %s %s :End of /WHOIS list.\n", g_state->server_name, clientnick, tnick); 
    659669 
    660670 return 0; 
     
    688698  } else { 
    689699   if ( (tmp = get_client_by_nick(args)) == -1 ) { 
    690     put_printf(client, ":%s 401 %s %s :No such nick/channel\n", server_name, get_nick(client), args); 
     700    put_printf(client, ":%s 401 %s %s :No such nick/channel\n", g_state->server_name, get_nick(client), args); 
    691701   } else { 
    692702    put_printf(tmp, ":%s %s %s :%s\n", ufull, cmd, args, text); 
     
    708718 (void)cmd, (void)args, (void)text; 
    709719 
    710  put_printf(client, ":%s 321 %s Channel :Users  Name\n", server_name, clientnick); 
     720 put_printf(client, ":%s 321 %s Channel :Users  Name\n", g_state->server_name, clientnick); 
    711721 
    712722 for (i = 0; i < MAX_CHANNELS; i++) { 
    713   c = &(g_channels[i]); 
     723  c = &(g_state->g_channels[i]); 
    714724  if ( !c->client_count ) 
    715725   continue; 
    716726 
    717727  put_printf(client, ":%s 322 %s %s %zu :[+] %s\n", 
    718              server_name, clientnick, c->name, c->client_count, c->topic.text == NULL ? "" : c->topic.text); 
    719  } 
    720  
    721  put_printf(client, ":%s 323 %s :End of /LIST\n", server_name, clientnick); 
     728             g_state->server_name, clientnick, c->name, c->client_count, c->topic.text == NULL ? "" : c->topic.text); 
     729 } 
     730 
     731 put_printf(client, ":%s 323 %s :End of /LIST\n", g_state->server_name, clientnick); 
    722732 
    723733 return 0; 
     
    750760  put_printf(client, ":%s 332 %s %s :%s\n" 
    751761                     ":%s 333 %s %s %s %li\n", 
    752          server_name, nick, c->name, c->topic.text, 
    753          server_name, nick, c->name, c->topic.user, (long int)c->topic.ts 
     762         g_state->server_name, nick, c->name, c->topic.text, 
     763         g_state->server_name, nick, c->name, c->topic.user, (long int)c->topic.ts 
    754764  ); 
    755765 } 
     
    848858 event.target_type = ROAR_OT_CLIENT; 
    849859 
    850  memset(g_channels, 0, sizeof(g_channels)); 
    851  
    852  subscription_client_delete = roar_notify_core_subscribe(NULL, &event, cb_client_delete, NULL); 
     860 memset(g_state->g_channels, 0, sizeof(g_state->g_channels)); 
     861 
     862 g_state->subscription_client_delete = roar_notify_core_subscribe(NULL, &event, cb_client_delete, NULL); 
    853863 
    854864 return 0; 
     
    858868 (void)para, (void)lib; 
    859869 
    860  roar_notify_core_unsubscribe(NULL, subscription_client_delete); 
     870 roar_notify_core_unsubscribe(NULL, g_state->subscription_client_delete); 
    861871 
    862872 return 0; 
     
    890900 ROAR_DL_PLUGIN_META_DESC("Implementation of the Internet Relay Chat (IRC)"); 
    891901 
     902 ROAR_DL_PLUGIN_REG(ROAR_DL_FN_PROTO, __reg_proto); 
    892903 ROAR_DL_PLUGIN_REG(ROAR_DL_FN_INIT, init); 
    893904 ROAR_DL_PLUGIN_REG_UNLOAD(unload); 
    894  ROAR_DL_PLUGIN_REG(ROAR_DL_FN_PROTO, __reg_proto); 
     905 ROAR_DL_PLUGIN_REG_GLOBAL_DATA(g_state, g_state_init); 
    895906} ROAR_DL_PLUGIN_END 
    896907 
Note: See TracChangeset for help on using the changeset viewer.