]>
jfr.im git - irc/evilnet/x3.git/blob - src/modpython.py
6 # - impliment handle_* functions for everything x3 has register fetaures for
7 # - impliment script load/unload for user scripts.
8 # - load a script via this script.
9 # - script calls functions from here to set its functions up for calling on various actions
10 # - provide helper functions for subscripts to save settings attached to users/chanels
11 # - provide helper functions for scripts to do common things like msg a person or a channel,
22 """Used to interact with the world of IRC from module scripts"""
24 # some defaults to make shorthand easy
29 def __init__(self
, service
= None, caller
= None, target
= None):
31 self
.caller
= caller
#the person who sent the command/message
32 self
.service
= service
#the service who saw the message
33 self
.target
= target
#the channel message was in (if public)
35 def send_target_privmsg(self
, source
, target
, message
):
36 svc
.send_target_privmsg(source
, target
, "%s "%(message))
38 def reply(self
, message
):
39 """ Send a private reply to the user using convenience values"""
40 #print "DEBUG: sending a message from %s to %s: %s"%(self.service, self.caller, message)
42 self
.send_target_privmsg(self
.service
, self
.target
, "%s: %s"%(self
.caller
, message
))
44 self
.send_target_privmsg(self
.service
, self
.caller
, message
)
47 """ Main hub of python system. Handle callbacks from c. """
50 #print "DEBUG: constructor for handler initing"
51 self
.plugins
= plugins(self
)
53 print "DEBUG: unable to make self.plugins!?!"
55 def init(self
, irc
): # not to be confused with __init__!
56 """ This gets called once all the objects are up and running. Otherwise,
57 were not done initing this own instance to be able to start calling it """
58 #print "DEBUG: in handler.init()"
62 def join(self
, irc
, channel
, nick
):
63 #user = svc.get_user(nick)
64 #print "DEBUG: handler.join()"
65 return self
.plugins
.callhandler("join", irc
, [channel
, nick
], [channel
, nick
])
67 def server_link(self
, irc
, name
, desc
):
68 return self
.plugins
.callhandler("server_link", irc
, [name
, desc
], [name
, desc
])
70 def new_user(self
, irc
, nick
, ident
, hostname
, info
):
71 # we may filter on all the user fields, but we only pass the nick because
72 # the plugin can get the rest itself
73 return self
.plugins
.callhandler("new_user", irc
, [nick
, ident
, hostname
, info
], [nick
])
75 def nick_change(self
, irc
, nick
, old_nick
):
76 return self
.plugins
.callhandler("nick_change", irc
, [nick
, old_nick
], [nick
, old_nick
])
78 def cmd_run(self
, irc
, cmd
):
79 #print "DEBUG: handler.cmd_run: %s"%cmd
83 def addhook(self
, event
, method
, filter=[None], data
=None):
84 self
.plugins
.addhook(event
, method
, filter, data
)
87 def addcommand(self
, plugin
, command
, method
):
88 self
.addhook("command", method
, [plugin
, command
])
90 def cmd_command(self
, irc
, plugin
, cmd
, args
):
91 #print "DEBUG: handel.cmd_command; %s %s; args= %s"%(plugin, cmd, args)
92 return self
.plugins
.callhandler("command", irc
, [plugin
, cmd
], [args
])
94 def load(self
, irc
, plugin
):
95 return self
.plugins
.load(plugin
)
98 """Class to handle loading/unloading of plugins"""
103 """ This is a request from a plugin to be called on an event """
104 event
= "" # Event to be called on (eg "join")
105 method
= None # Method to call
106 filter = None # Arguments to filter
107 data
= "" # plugin-supplied data for plugin use
109 def __init__(self
, event
, method
, filter, data
):
115 def event_is(self
, event
, evdata
):
116 if(self
.event
== event
):
117 for i
in range(len(self
.filter)):
118 if( self
.filter[i
] != None
119 and self
.filter[i
] != evdata
[i
]): # should be case insensitive? or how to compare?
120 #print "DEBUG: rejecting event, %s is not %s"%(self.filter[i], evdata[i])
126 def trigger(self
, irc
, args
):
127 #print "DEBUG: Triggering %s event. with '%s' arguments."%(self.event, args)
128 self
.method(irc
, *args
)
130 def __init__(self
, handler
):
132 #print "DEBUG: constructor for plugins initing"
133 self
.handler
= handler
136 #print "DEBUG: in plugins.init()"
140 def addhook(self
, event
, method
, filter=[None], data
=None):
141 #print "DEBUG: Adding hook for %s."%event
142 self
.hooks
.append(self
.hook(event
, method
, filter, data
))
144 def findhooksforevent(self
, event
, data
):
146 #print "DEBUG: findhooksforevent() looking..."
147 for hook
in self
.hooks
:
148 #print "DEBUG: looking at a %s hook..."%hook.event
149 if(hook
.event_is(event
, data
)):
153 def callhandler(self
, event
, irc
, filter, args
):
154 for hook
in self
.findhooksforevent(event
, filter):
155 if(hook
.trigger(irc
, args
)):
159 def load(self
, name
):
160 """ Loads a plugin by name """
161 mod_name
= "plugins.%s"%name
163 if(sys
.modules
.has_key(mod_name
)):
165 #TODO: try to catch compile errors etc.
167 if(need_reload
== False):
169 module
= sys
.modules
[mod_name
]
170 if(need_reload
== True):
171 reload(module
) # to ensure its read fresh
173 pluginObj
= Class(self
.handler
, irc())
174 self
.loaded_plugins
[mod_name
] = pluginObj