Index: plugins/roard/protocol-irc.c
===================================================================
--- plugins/roard/protocol-irc.c	(revision 5823)
+++ plugins/roard/protocol-irc.c	(revision 5899)
@@ -50,12 +50,5 @@
 static void cb_client_delete(struct roar_notify_core * core, struct roar_event * event, void * userdata);
 
-static struct roar_subscriber * subscription_client_delete = NULL;
-
-static char * server_name     = "irc.roard";
-static char * server_fullname = "RoarAudio roard IRC Server plugin";
-
-static char * quit_msg = NULL;
-
-static struct channel {
+struct channel {
  char * name;
  struct {
@@ -66,7 +59,24 @@
  size_t client_count;
  int clients[ROAR_CLIENTS_MAX];
-} g_channels[MAX_CHANNELS];
-
-static struct command {
+};
+
+struct state {
+ const char * server_name;
+ const char * server_fullname;
+ const char * quit_msg;
+ struct roar_subscriber * subscription_client_delete;
+ struct channel g_channels[MAX_CHANNELS];
+};
+
+static struct state   g_state_init = {
+ .server_name = "irc.roard",
+ .server_fullname = "RoarAudio roard IRC Server plugin",
+ .quit_msg = NULL,
+ .subscription_client_delete = NULL
+};
+
+static struct state * g_state = &g_state_init;
+
+static const struct command {
  const char * name;
  int (*func)(int client, const char * cmd, char * args, char * text);
@@ -193,5 +203,5 @@
 
  for (i = 0; i < MAX_CHANNELS; i++) {
-  c = &(g_channels[i]);
+  c = &(g_state->g_channels[i]);
 
   if ( !c->client_count )
@@ -216,5 +226,5 @@
 
  for (i = 0; i < MAX_CHANNELS; i++) {
-  c = &(g_channels[i]);
+  c = &(g_state->g_channels[i]);
 
   if ( !c->client_count )
@@ -281,11 +291,11 @@
 
  for (i = 0; i < MAX_CHANNELS; i++) {
-  if ( !g_channels[i].client_count )
-   continue;
-
-  if ( !!strcasecmp(g_channels[i].name, channel) )
-   continue;
-
-  c = &(g_channels[i]);
+  if ( !g_state->g_channels[i].client_count )
+   continue;
+
+  if ( !!strcasecmp(g_state->g_channels[i].name, channel) )
+   continue;
+
+  c = &(g_state->g_channels[i]);
   break;
  }
@@ -293,8 +303,8 @@
  if ( c == NULL ) {
   for (i = 0; i < MAX_CHANNELS; i++) {
-   if ( g_channels[i].client_count )
+   if ( g_state->g_channels[i].client_count )
     continue;
 
-   c = &(g_channels[i]);
+   c = &(g_state->g_channels[i]);
    break;
   }
@@ -322,14 +332,14 @@
 
  for (i = 0; i < MAX_CHANNELS; i++) {
-  if ( !g_channels[i].client_count )
-   continue;
-
-  if ( !!strcasecmp(g_channels[i].name, channel) )
-   continue;
-
-  if ( !g_channels[i].clients[client] )
+  if ( !g_state->g_channels[i].client_count )
+   continue;
+
+  if ( !!strcasecmp(g_state->g_channels[i].name, channel) )
+   continue;
+
+  if ( !g_state->g_channels[i].clients[client] )
    return -1;
 
-  c = &(g_channels[i]);
+  c = &(g_state->g_channels[i]);
   break;
  }
@@ -359,8 +369,8 @@
 
  for (i = 0; i < MAX_CHANNELS; i++) {
-  if ( !g_channels[i].client_count )
-   continue;
-
-  if ( !!strcasecmp(g_channels[i].name, channel) )
+  if ( !g_state->g_channels[i].client_count )
+   continue;
+
+  if ( !!strcasecmp(g_state->g_channels[i].name, channel) )
    continue;
 
@@ -368,5 +378,5 @@
 
   for (j = 0; j < ROAR_CLIENTS_MAX; j++) {
-   if ( g_channels[i].clients[j] == 0 )
+   if ( g_state->g_channels[i].clients[j] == 0 )
     continue;
 
@@ -375,5 +385,5 @@
    if ( (offset + len + 3) > sizeof(buf) ) {
     buf[offset] = 0;
-    put_printf(client, ":%s 353 %s = %s :%s\n", server_name, nick, channel, buf);
+    put_printf(client, ":%s 353 %s = %s :%s\n", g_state->server_name, nick, channel, buf);
     offset = 0;
    }
@@ -388,7 +398,7 @@
   if ( offset ) {
    buf[offset] = 0;
-   put_printf(client, ":%s 353 %s = %s :%s\n", server_name, nick, channel, buf);
-  }
-  put_printf(client, ":%s 366 %s %s :End of /NAMES list.\n", server_name, nick, channel);
+   put_printf(client, ":%s 353 %s = %s :%s\n", g_state->server_name, nick, channel, buf);
+  }
+  put_printf(client, ":%s 366 %s %s :End of /NAMES list.\n", g_state->server_name, nick, channel);
 
   return 0;
@@ -402,5 +412,5 @@
  size_t count;
  struct channel * c;
- const char * text = quit_msg;
+ const char * text = g_state->quit_msg;
  int client = event->target;
  const char * ufull = get_ufull(client);
@@ -412,5 +422,5 @@
   text = "Client deleted. Died, kicked or internal error.";
 
- if ( quit_msg != NULL )
+ if ( g_state->quit_msg != NULL )
   put_printf(client, "ERROR :Closing Link: %s (Quit: %s)\n", ufull, text);
 
@@ -420,5 +430,5 @@
 
  for (i = 0; i < MAX_CHANNELS; i++) {
-  c = &(g_channels[i]);
+  c = &(g_state->g_channels[i]);
 
   if ( !c->client_count )
@@ -474,8 +484,8 @@
       ":%s 372 %s :- MotD goes here...\n"
       ":%s 376 %s :End of /MOTD command.\n",
-   server_name, name,
-   server_name, name, server_name,
-   server_name, name,
-   server_name, name
+   g_state->server_name, name,
+   g_state->server_name, name, g_state->server_name,
+   g_state->server_name, name,
+   g_state->server_name, name
  );
 
@@ -537,5 +547,5 @@
 
  if ( !found ) {
-  put_printf(client, ":%s 421 %s %s :Unknown command\n", server_name, get_nick(client), cmd);
+  put_printf(client, ":%s 421 %s %s :Unknown command\n", g_state->server_name, get_nick(client), cmd);
  }
 
@@ -554,5 +564,5 @@
  if ( args != NULL && args[0] != 0 && is_valid_name(args) && strlen(args) < ROAR_BUFFER_NAME ) {
   if ( get_client_by_nick(args) != -1 ) {
-   put_printf(client, ":%s 433 %s %s :Nickname is already in use.\n", server_name, nick, args);
+   put_printf(client, ":%s 433 %s %s :Nickname is already in use.\n", g_state->server_name, nick, args);
   } else {
    ufull = get_ufull(client);
@@ -566,5 +576,5 @@
   }
  } else {
-  put_printf(client, ":%s 432 %s %s :Erroneous Nickname: Illegal characters\n", server_name, nick, args);
+  put_printf(client, ":%s 432 %s %s :Erroneous Nickname: Illegal characters\n", g_state->server_name, nick, args);
  }
 
@@ -585,7 +595,7 @@
   text = "... have a cuddle ...";
 
- quit_msg = text;
+ g_state->quit_msg = text;
  ret = clients_delete(client);
- quit_msg = NULL;
+ g_state->quit_msg = NULL;
 
  return ret;
@@ -619,5 +629,5 @@
 
  for (i = 0; i < MAX_CHANNELS; i++) {
-  if ( !g_channels[i].client_count )
+  if ( !g_state->g_channels[i].client_count )
    continue;
   numchans++;
@@ -625,13 +635,13 @@
 
  put_printf(client, ":%s 251 %s :There are %zu users and 0 invisible on 1 servers\n",
-            server_name, clientnick, counters_get(cur, clients));
+            g_state->server_name, clientnick, counters_get(cur, clients));
  put_printf(client, ":%s 254 %s %zu :channels formed\n",
-            server_name, clientnick, numchans);
+            g_state->server_name, clientnick, numchans);
  put_printf(client, ":%s 255 %s :I have %zu clients and 0 servers\n",
-            server_name, clientnick, counters_get(cur, clients));
+            g_state->server_name, clientnick, counters_get(cur, clients));
  put_printf(client, ":%s 265 %s :Current Local Users: %zu  Max: <unknown>\n",
-            server_name, clientnick, counters_get(cur, clients));
+            g_state->server_name, clientnick, counters_get(cur, clients));
  put_printf(client, ":%s 266 %s :Current Global Users: %zu  Max: <unknown>\n",
-            server_name, clientnick, counters_get(cur, clients));
+            g_state->server_name, clientnick, counters_get(cur, clients));
 
  return 0;
@@ -650,11 +660,11 @@
 
  if ( (tclient = get_client_by_nick(tnick)) == -1 ) {
-  put_printf(client, ":%s 401 %s %s :No such nick/channel\n", server_name, clientnick, tnick);
+  put_printf(client, ":%s 401 %s %s :No such nick/channel\n", g_state->server_name, clientnick, tnick);
  } else {
-  put_printf(client, ":%s 311 %s %s %s %s * :%s\n", server_name, clientnick, tnick,
+  put_printf(client, ":%s 311 %s %s %s %s * :%s\n", g_state->server_name, clientnick, tnick,
              get_ident(tclient), get_node(tclient), get_realname(tclient));
-  put_printf(client, ":%s 312 %s %s %s :%s\n", server_name, clientnick, tnick, server_name, server_fullname);
- }
- put_printf(client, ":%s 318 %s %s :End of /WHOIS list.\n", server_name, clientnick, tnick);
+  put_printf(client, ":%s 312 %s %s %s :%s\n", g_state->server_name, clientnick, tnick, g_state->server_name, g_state->server_fullname);
+ }
+ put_printf(client, ":%s 318 %s %s :End of /WHOIS list.\n", g_state->server_name, clientnick, tnick);
 
  return 0;
@@ -688,5 +698,5 @@
   } else {
    if ( (tmp = get_client_by_nick(args)) == -1 ) {
-    put_printf(client, ":%s 401 %s %s :No such nick/channel\n", server_name, get_nick(client), args);
+    put_printf(client, ":%s 401 %s %s :No such nick/channel\n", g_state->server_name, get_nick(client), args);
    } else {
     put_printf(tmp, ":%s %s %s :%s\n", ufull, cmd, args, text);
@@ -708,16 +718,16 @@
  (void)cmd, (void)args, (void)text;
 
- put_printf(client, ":%s 321 %s Channel :Users  Name\n", server_name, clientnick);
+ put_printf(client, ":%s 321 %s Channel :Users  Name\n", g_state->server_name, clientnick);
 
  for (i = 0; i < MAX_CHANNELS; i++) {
-  c = &(g_channels[i]);
+  c = &(g_state->g_channels[i]);
   if ( !c->client_count )
    continue;
 
   put_printf(client, ":%s 322 %s %s %zu :[+] %s\n",
-             server_name, clientnick, c->name, c->client_count, c->topic.text == NULL ? "" : c->topic.text);
- }
-
- put_printf(client, ":%s 323 %s :End of /LIST\n", server_name, clientnick);
+             g_state->server_name, clientnick, c->name, c->client_count, c->topic.text == NULL ? "" : c->topic.text);
+ }
+
+ put_printf(client, ":%s 323 %s :End of /LIST\n", g_state->server_name, clientnick);
 
  return 0;
@@ -750,6 +760,6 @@
   put_printf(client, ":%s 332 %s %s :%s\n"
                      ":%s 333 %s %s %s %li\n",
-         server_name, nick, c->name, c->topic.text,
-         server_name, nick, c->name, c->topic.user, (long int)c->topic.ts
+         g_state->server_name, nick, c->name, c->topic.text,
+         g_state->server_name, nick, c->name, c->topic.user, (long int)c->topic.ts
   );
  }
@@ -848,7 +858,7 @@
  event.target_type = ROAR_OT_CLIENT;
 
- memset(g_channels, 0, sizeof(g_channels));
-
- subscription_client_delete = roar_notify_core_subscribe(NULL, &event, cb_client_delete, NULL);
+ memset(g_state->g_channels, 0, sizeof(g_state->g_channels));
+
+ g_state->subscription_client_delete = roar_notify_core_subscribe(NULL, &event, cb_client_delete, NULL);
 
  return 0;
@@ -858,5 +868,5 @@
  (void)para, (void)lib;
 
- roar_notify_core_unsubscribe(NULL, subscription_client_delete);
+ roar_notify_core_unsubscribe(NULL, g_state->subscription_client_delete);
 
  return 0;
@@ -890,7 +900,8 @@
  ROAR_DL_PLUGIN_META_DESC("Implementation of the Internet Relay Chat (IRC)");
 
+ ROAR_DL_PLUGIN_REG(ROAR_DL_FN_PROTO, __reg_proto);
  ROAR_DL_PLUGIN_REG(ROAR_DL_FN_INIT, init);
  ROAR_DL_PLUGIN_REG_UNLOAD(unload);
- ROAR_DL_PLUGIN_REG(ROAR_DL_FN_PROTO, __reg_proto);
+ ROAR_DL_PLUGIN_REG_GLOBAL_DATA(g_state, g_state_init);
 } ROAR_DL_PLUGIN_END
 
