下面我将提供一个写好的模块,里面包含了对mysql的操作,你可以直接编译,但直得注意的是,因为你新增了文件,所以还需要修改Makefile来编译它。
一、m2d_commands.c,存于unrealircd/src/models下,模块文件源码
1
2 #include " config.h "
3 #include " struct.h "
4 #include " common.h "
5 #include " sys.h "
6 #include " numeric.h "
7 #include " msg.h "
8 #include " proto.h "
9 #include " channel.h "
10 #include < time.h >
11 #include < sys / stat.h >
12 #include < stdio.h >
13 #include < stdlib.h >
14 #include < string .h >
15 #ifdef _WIN32
16 #include < io.h >
17 #endif
18 #include < fcntl.h >
19 #include " h.h "
20 #ifdef STRIPBADWORDS
21 #include " badwords.h "
22 #endif
23 #ifdef _WIN32
24 #include " version.h "
25 #endif
26
27 DLLFUNC CMD_FUNC(m2d_testcmd);
28
29 #define MSG_TESTCMD "TESTCMD"
30 #define TOK_TESTCMD "2d_a"
31
32 ModuleHeader MOD_HEADER(m2d_testcmd)
33 = {
34 " m2d_commands " ,
35 " Id:m2dcommands.c,v1.0.0.02007/03/02byOldHawk " ,
36 " Wrapper library for m2d_ commands " ,
37 " 3.2-b8-1 " ,
38 NULL
39 };
40
41 DLLFUNC int MOD_INIT(m2d_testcmd)(ModuleInfo * modinfo)
42 {
43 CommandAdd(modinfo -> handle, MSG_TESTCMD, TOK_TESTCMD, m2d_testcmd, MAXPARA, M_USER | M_SERVER);
44
45 MARK_AS_OFFICIAL_MODULE(modinfo);
46 return MOD_SUCCESS;
47 }
48
49 DLLFUNC int MOD_LOAD(m2d_testcmd)( int module_load)
50 {
51 return MOD_SUCCESS;
52 }
53
54 DLLFUNC int MOD_UNLOAD(m2d_testcmd)( int module_unload)
55 {
56 return MOD_SUCCESS;
57 }
58
59 DLLFUNC CMD_FUNC(m2d_testcmd)
60 {
61 if (parc < 2 ){
62 sendto_one (sptr, err_str (ERR_NEEDMOREPARAMS), me.name, parv[ 0 ], " TESTCMD " );
63 return 0 ;
64 }
65
66 if (my_conn){
67 char querymsg[BUFSIZE];
68 MYSQL_RES * res;
69 MYSQL_ROW row;
70 int rowcount,i;
71 snprintf(querymsg, sizeof (querymsg), " select id,question from questions " );
72 res = my_mysql_select(my_conn, querymsg);
73 if (res){
74 rowcount = mysql_num_rows(res);
75 i = 0 ;
76 while ((row = mysql_fetch_row(res))){
77 ircd_log (LOG_SERVER, " questions %d/%d: [%s]%s " , i, rowcount, row[ 0 ], row[ 1 ]);
78 i ++ ;
79 }
80 }
81 if (res) mysql_free_result(res);
82 }
83 sendto_one (sptr, " u input testcmd: %s %d " , parv[ 1 ], max_connection_count);
84 return 0 ;
85 }
86
87
2 #include " config.h "
3 #include " struct.h "
4 #include " common.h "
5 #include " sys.h "
6 #include " numeric.h "
7 #include " msg.h "
8 #include " proto.h "
9 #include " channel.h "
10 #include < time.h >
11 #include < sys / stat.h >
12 #include < stdio.h >
13 #include < stdlib.h >
14 #include < string .h >
15 #ifdef _WIN32
16 #include < io.h >
17 #endif
18 #include < fcntl.h >
19 #include " h.h "
20 #ifdef STRIPBADWORDS
21 #include " badwords.h "
22 #endif
23 #ifdef _WIN32
24 #include " version.h "
25 #endif
26
27 DLLFUNC CMD_FUNC(m2d_testcmd);
28
29 #define MSG_TESTCMD "TESTCMD"
30 #define TOK_TESTCMD "2d_a"
31
32 ModuleHeader MOD_HEADER(m2d_testcmd)
33 = {
34 " m2d_commands " ,
35 " Id:m2dcommands.c,v1.0.0.02007/03/02byOldHawk " ,
36 " Wrapper library for m2d_ commands " ,
37 " 3.2-b8-1 " ,
38 NULL
39 };
40
41 DLLFUNC int MOD_INIT(m2d_testcmd)(ModuleInfo * modinfo)
42 {
43 CommandAdd(modinfo -> handle, MSG_TESTCMD, TOK_TESTCMD, m2d_testcmd, MAXPARA, M_USER | M_SERVER);
44
45 MARK_AS_OFFICIAL_MODULE(modinfo);
46 return MOD_SUCCESS;
47 }
48
49 DLLFUNC int MOD_LOAD(m2d_testcmd)( int module_load)
50 {
51 return MOD_SUCCESS;
52 }
53
54 DLLFUNC int MOD_UNLOAD(m2d_testcmd)( int module_unload)
55 {
56 return MOD_SUCCESS;
57 }
58
59 DLLFUNC CMD_FUNC(m2d_testcmd)
60 {
61 if (parc < 2 ){
62 sendto_one (sptr, err_str (ERR_NEEDMOREPARAMS), me.name, parv[ 0 ], " TESTCMD " );
63 return 0 ;
64 }
65
66 if (my_conn){
67 char querymsg[BUFSIZE];
68 MYSQL_RES * res;
69 MYSQL_ROW row;
70 int rowcount,i;
71 snprintf(querymsg, sizeof (querymsg), " select id,question from questions " );
72 res = my_mysql_select(my_conn, querymsg);
73 if (res){
74 rowcount = mysql_num_rows(res);
75 i = 0 ;
76 while ((row = mysql_fetch_row(res))){
77 ircd_log (LOG_SERVER, " questions %d/%d: [%s]%s " , i, rowcount, row[ 0 ], row[ 1 ]);
78 i ++ ;
79 }
80 }
81 if (res) mysql_free_result(res);
82 }
83 sendto_one (sptr, " u input testcmd: %s %d " , parv[ 1 ], max_connection_count);
84 return 0 ;
85 }
86
87
二、编译,打开unrealircd/src/models/Makefile,做如下修改
R_MODULES
=
\
m_sethost.so m_chghost.so m_chgident.so m_setname.so \
m_setident.so m_sdesc.so m_svsmode.so m_swhois.so\
m_svsmotd.so m_svsnline.so m_who.so m_mkpasswd.so \
m_away.so m_svsnoop.so m_svso.so m_svsnick.so \
m_adminchat.so m_akill.so m_chgname.so m_guest.so m_htm.so m_kill.so \
m_lag.so m_message.so m_nachat.so m_oper.so m_pingpong.so \
m_quit.so m_rakill.so m_rping.so m_sendumode.so m_sqline.so \
m_tsctl.so m_unkline.so m_unsqline.so m_unzline.so m_whois.so \
m_tkl.so m_vhost.so m_cycle.so m_svsjoin.so m_svspart.so \
m_svswatch.so m_svssilence.so m_sendsno.so m_svssno.so \
m_sajoin.so m_sapart.so m_samode.so m_kick.so m_topic.so \
m_invite.so m_list.so m_time.so m_svskill.so m_sjoin.so \
m_pass.so m_userhost.so m_ison.so m_silence.so m_knock.so \
m_umode2.so m_squit.so m_protoctl.so m_addline.so m_addomotd.so \
m_wallops.so m_admin.so m_globops.so m_locops.so m_chatops.so \
m_trace.so m_netinfo.so m_links.so m_help.so m_rules.so \
m_close.so m_map.so m_eos.so m_server.so m_stats.so \
m_svsfline.so m_dccdeny.so m_undccdeny.so m_whowas.so \
m_connect.so m_dccallow.so m_userip.so m_nick.so m_user.so \
m_mode.so m_watch.so m_part.so m_join.so m_motd.so m_opermotd.so \
m_botmotd.so m_lusers.so m_names.so m2d_commands.so
在文件最后,做如下修改:
m_sethost.so m_chghost.so m_chgident.so m_setname.so \
m_setident.so m_sdesc.so m_svsmode.so m_swhois.so\
m_svsmotd.so m_svsnline.so m_who.so m_mkpasswd.so \
m_away.so m_svsnoop.so m_svso.so m_svsnick.so \
m_adminchat.so m_akill.so m_chgname.so m_guest.so m_htm.so m_kill.so \
m_lag.so m_message.so m_nachat.so m_oper.so m_pingpong.so \
m_quit.so m_rakill.so m_rping.so m_sendumode.so m_sqline.so \
m_tsctl.so m_unkline.so m_unsqline.so m_unzline.so m_whois.so \
m_tkl.so m_vhost.so m_cycle.so m_svsjoin.so m_svspart.so \
m_svswatch.so m_svssilence.so m_sendsno.so m_svssno.so \
m_sajoin.so m_sapart.so m_samode.so m_kick.so m_topic.so \
m_invite.so m_list.so m_time.so m_svskill.so m_sjoin.so \
m_pass.so m_userhost.so m_ison.so m_silence.so m_knock.so \
m_umode2.so m_squit.so m_protoctl.so m_addline.so m_addomotd.so \
m_wallops.so m_admin.so m_globops.so m_locops.so m_chatops.so \
m_trace.so m_netinfo.so m_links.so m_help.so m_rules.so \
m_close.so m_map.so m_eos.so m_server.so m_stats.so \
m_svsfline.so m_dccdeny.so m_undccdeny.so m_whowas.so \
m_connect.so m_dccallow.so m_userip.so m_nick.so m_user.so \
m_mode.so m_watch.so m_part.so m_join.so m_motd.so m_opermotd.so \
m_botmotd.so m_lusers.so m_names.so m2d_commands.so
cloak.so: cloak.c (INCLUDES)(CC) (CFLAGS)(MODULEFLAGS) -DDYNAMIC_LINKING \
-o cloak.so cloak.c
m2d_commands.so: m2d_commands.c (INCLUDES)(CC) (CFLAGS)(MODULEFLAGS) -DDYNAMIC_LINKING \
-o m2d_commands.so m2d_commands.c
clean:
$(RM) -f *.o *.so *~ core
然后重新make,就会生成m2d_command.so了(win32下是m2d_command.dll)。
-o cloak.so cloak.c
m2d_commands.so: m2d_commands.c (INCLUDES)(CC) (CFLAGS)(MODULEFLAGS) -DDYNAMIC_LINKING \
-o m2d_commands.so m2d_commands.c
clean:
$(RM) -f *.o *.so *~ core
三、修改unrealircd.conf配置文件,找到include部分,修改以下内容:
loadmodule "src/modules/commands.so";
loadmodule "src/modules/cloak.so";
loadmodule "src/modules/m2d_commands.so";
loadmodule "src/modules/cloak.so";
loadmodule "src/modules/m2d_commands.so";
本文转自 OldHawk 博客园博客,原文链接:http://www.cnblogs.com/taobataoma/archive/2007/04/15/713949.html
,如需转载请自行联系原作者