⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msg.c

📁 一个C语言的聊天室源代码
💻 C
字号:
/*
	Version: 0.2.0(alpha)
	Author: Computer_xu
	Email: Computer_xu@sina.com
	HomePage: http://www.socketchat.com
	LastModify: 2001-05-22 (yyyy-mm-dd)
*/

#include "msg.h"

pthread_mutex_t msg_mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t msg_cond=PTHREAD_COND_INITIALIZER;
pthread_mutex_t addwords_mutex=PTHREAD_MUTEX_INITIALIZER;

/* 强制发送 */
void ForceSend()
{
        pthread_cond_signal(&msg_cond);
}
/* 发送一句话到信息队列 */
void AddWords(unsigned int roomid, char *id, char *toid, unsigned int action, char *color, char *pic, char *message, unsigned int hidden)
{
        CHATMSG *p;
	USERINFO *p1;
	char buf0[1024],buf1[1024],buf2[1024],buf3[1024],buf4[1024];
	char buf5[2048],buf6[2048],buf7[2048];
	char buf[8192];
	char *msg;

#ifdef DEBUGMODE
	printf("Enter AddWords...\n");
#endif
        pthread_mutex_lock(&addwords_mutex);
	buf0[0]=buf1[0]=buf2[0]=buf3[0]='\0';
#ifdef DEBUGMODE
	printf("Roomid: %d, ID=[%s], ToID=[%s], Hidden=%d\n",roomid,(id==NULL)?"null":id,(toid==NULL)?"null":toid,hidden);
#endif
        if( message!=NULL )
        {
#ifdef DEBUGMODE
		printf("Process...\n");
#endif
                p=sysinfo.MSG_END->next;
                p->roomid=roomid;
                p->action=action;

		msg=(char *)Malloc(Strlen(message)+1);
		strncpy(msg,message,Strlen(message));
		msg[Strlen(message)]='\0';
#ifdef DEBUGMODE
		printf("Msg Length = %d\n",Strlen(message));
#endif

		GetTimeString(buf0);

		if( id==NULL || Strlen(id)==0 )
		{
			sprintf(&buf1[strlen(buf1)],"<font color=green>【注意】</font>");
			strcpy(p->id,"");
		}
		else
		{
			sprintf(&buf1[strlen(buf1)],"<a href=\"\" OnClick=\"parent.SelectUser(\\\'%s\\\');return false;\">%s</a>",id,GetNN(id));
			strncpy(p->id,id,20);
		}
		if( toid==NULL || Strlen(toid)==0 )
		{
			sprintf(&buf2[strlen(buf2)],"<font color=blue>大家</font>");
	                strcpy(p->toid,"");
		}
		else
		{
			sprintf(&buf2[strlen(buf2)],"<a href=\"\" OnClick=\"parent.SelectUser(\\\'%s\\\');return false;\">%s</a>",toid,GetNN(toid));
	                strncpy(p->toid,toid,20);
		}

		if( id==NULL || Strlen(id)==0 )
		{
			if( hidden==1 )	sprintf(buf,"%s %s &nbsp;<span style=\"font-size: 11pt; color: green; FONT-FAMILY: Wingdings;\">)</span>",buf1,msg);
			else		sprintf(buf,"%s %s",buf1,msg);
		}
		else
		{
			/* 语法支持替换 必须*/
			msg=ExChange(msg,"\\","\\\\");
			msg=ExChange(msg,"'","\\'");
			msg=ExChange(msg,"&","&#38");
			msg=ExChange(msg,"%","%%");

			/* HTML 标签过滤 */
			if( GetNN(id)!=NULL && (p1=GetUP(GetNN(id)))!=NULL )
			{
#ifdef DEBUGMODE
				printf("status: %x\n",p1->status);
#endif
				if( (p1->status&0x2000) ==0 )
				{
					msg=ExChange(msg,"<","&lt");
					msg=ExChange(msg,">","&gt");
				}
			}

			if( msg[0]=='/' )
			{
				if( msg[1]=='/' )
				{	/* //[command]  reference to func.c */
					goto normalsay;
				}
				else
				{
					/* /[action] */
					Func_Act(&msg[1],buf5,id,toid);
					if( strlen(buf5)==0 )	goto normalsay;
					if( hidden==1 )	sprintf(buf3," <font color=%s>%s</font>&nbsp;<span style=\"font-size: 11pt; color: green; FONT-FAMILY: Wingdings;\">)</span>%%s",color,buf5);
					else		sprintf(buf3," <font color=%s>%s</font>%%s",color,buf5);

					if( pic!=NULL && Strlen(pic) )
					{
						sprintf(buf4,"<img src=%s width=30 height=30>",pic);
						sprintf(buf,buf3,buf4);
					}
					else	sprintf(buf,buf3,"");
				}
			}
			else
			{	/* Normal Say Operation */
				normalsay:
				if( hidden==1 )	sprintf(buf3," %s对%%s说:<font color=%s>%s</font>&nbsp;<span style=\"font-size: 11pt; color: green; FONT-FAMILY: Wingdings;\">)</span>%%s",buf1,color,msg);
				else		sprintf(buf3," %s对%%s说:<font color=%s>%s</font>%%s",buf1,color,msg);

				if( pic!=NULL && Strlen(pic) )
				{
					sprintf(buf4,"<img src=%s width=30 height=30>",pic);
					sprintf(buf,buf3,buf2,buf4);
				}
				else	sprintf(buf,buf3,buf2,"");
			}
		}
#ifdef DEBUGMODE
		printf("ID=[%s] ToID=[%s]\n",p->id,p->toid);
#endif

		if( hidden & 2 )
		{
			snprintf(p->msg,1023,"%s",msg);
			hidden &= 0xFFFD;
		}
                else
		{
			snprintf(p->msg,1023,"<script>parent.WriteLine('%s','%s','%s','%s','%s','%s','%d')</script>\n",buf0,id,GetNN(id),toid,GetNN(toid),buf,hidden);
		}

                p->hidden=hidden;
                sysinfo.MSG_END=p;
		Free(msg);
        }
        pthread_mutex_unlock(&addwords_mutex);
#ifdef DEBUGMODE
	printf("Return From AddWords\n");
#endif
}

/* 发送数据线程主体 */
void *SendData(void* connfd)
{
        SOCKET_INFO *p1;
        CHATMSG *p2=NULL;
        char msg[16384];
	int rn;

        pthread_detach(pthread_self());
#ifdef DEBUGMODE
        printf("Enter SendData...\n");
#endif

        while(1)
        {
#ifdef DEBUGMODE
                printf("Recyling in SendData...\n");
#endif
                p1=sysinfo.SOCKET_HEAD;
                pthread_mutex_lock(&msg_mutex);
                while( p2 == sysinfo.MSG_END )        pthread_cond_wait(&msg_cond,&msg_mutex);
                p2=sysinfo.MSG_END;
                while( p1 != NULL )
                {
                        if( p1->status )
                        {
                                if( p1->last != NULL )
                                {
                                        do{
                                                /* printf("Sending...\n");*/
                                                p1->last=p1->last->next;
						if( p1->last->roomid==p1->user->roomid )
						{
#ifdef DEBUGMODE
							printf("[%d] [%s] [%s]\n",p1->last->hidden,p1->last->toid,p1->user->userid);
#endif
							if( p1->user->status&0x0200 || p1->last->hidden&0x0001==0 || StrCmp(p1->user->userid,p1->last->toid)==0 || StrCmp(p1->user->userid,p1->last->id)==0 || Strlen(p1->last->toid)==0 )
							{
                                                		sprintf(msg,"%s",p1->last->msg);
		                                                rn=write(p1->socketfd,msg,Strlen(msg));
								if( rn==-1 && p1->status )
								{
					                                /* printf("Some one break line\n"); */
									sprintf(msg,"<a href=\"\" OnClick=\"parent.SelectUser(\\\'%s\\\'); return false;\">%s</a> 掉线了!",p1->user->userid, p1->user->nickname);
			                		                AddWords(p1->user->roomid, "", "", 0, "", "", msg, 0);
									sprintf(msg,"<script>parent.RemoveUser('%s','%s','%s');</script>\n",p1->user->userid, p1->user->nickname, p1->user->sex);
			                		                AddWords(p1->user->roomid, "", "", 0, "", "", msg, 2);
									LogOut(p1->user->nickname,p1->user->sid);
								}
								/* printf("Write %d byte\n",rn); */
        	        	                                /* printf("Msg: %s\n",p1->last->msg); */
							}
						}
                                        } while( p1->last != p2 );
                                }
                                p1->lasttime=time(NULL);
                        }
                        p1=p1->next;
                }
                pthread_cond_broadcast(&msg_cond);
                pthread_mutex_unlock(&msg_mutex);
        }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -